From 5590d9fd123701a59b50ebe97d7f9657d16f028b Mon Sep 17 00:00:00 2001 From: lunamorrow Date: Tue, 28 May 2024 21:25:56 +1000 Subject: [PATCH] 28/5/24 starting on OpenBabelReader --- mda_openbabel_converter/OpenBabel.py | 50 ++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/mda_openbabel_converter/OpenBabel.py b/mda_openbabel_converter/OpenBabel.py index 2b070e6..838166c 100644 --- a/mda_openbabel_converter/OpenBabel.py +++ b/mda_openbabel_converter/OpenBabel.py @@ -5,22 +5,68 @@ import MDAnalysis as mda from MDAnalysis.converters.base import ConverterBase from MDAnalysis.coordinates.base import SingleFrameReaderBase +from MDAnalysis.core.groups import AtomGroup + +try: + import openbabel as OB + from openbabel import OBMol +except ImportError: + print("Cannot find openbabel, install with 'pip install openbabel==2.4.0'") class OpenBabelReader(SingleFrameReaderBase): """ Convert an OpenBabel OBMol (from the file) to a MDAnalysis AtomGroup """ + @staticmethod def _format_hint(thing): """ Base function to check if the reader can actually read this “thing” (i.e., is it a file that can be converted to an OpenBabel OBMol?) """ - pass + try: + import openbabel as OB + except ImportError: + return False + else: + return isinstance(thing, OB.OBMol) - def __init__(self, filename, **kwargs): + def __init__(self, filename: OBMol, **kwargs): """ Converts file to OBMol to AtomGroup """ + self.atoms = [] + self.n_atoms = 0 + self.residues = [] + self.n_residues = 0 + self.segments = [] + self.n_segments = 0 + + obmol = filename + + # Atoms + names = [] + chiralities = [] + resnums = [] + resnames = [] + elements = [] + masses = [] + charges = [] + aromatics = [] + ids = [] + atomtypes = [] + segids = [] + altlocs = [] + chainids = [] + icodes = [] + occupancies = [] + tempfactors = [] + + for i in range(1, obmol): + atom = obmol.GetAtomById(i-1) + # need to add handling incase attributes are invalid or null in OBMol + names.append(atom.GetType()) #char + + pass class OpenBabelConverter(ConverterBase):