Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenBabelReader and Tests #16

Merged
merged 4 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 48 additions & 10 deletions mda_openbabel_converter/OpenBabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,73 @@
from MDAnalysis.converters.base import ConverterBase
from MDAnalysis.coordinates.memory import MemoryReader
from MDAnalysis.core.groups import AtomGroup
import numpy as np
import warnings

HAS_OBABEL = False

try:
from openbabel import openbabel as OB
from openbabel import OBMol
from openbabel import openbabel as ob
from openbabel.openbabel import OBMol, OBAtom, OBMolAtomIter
HAS_OBABEL = True
except ImportError:
print("Cannot find openbabel, install with 'pip install openbabel==2.4.0'")
warnings.warn("Cannot find openbabel, install with `mamba install -c "
"conda-forge openbabel`")


class OpenBabelReader(MemoryReader):
"""
Convert an OpenBabel OBMol (from the file) to a MDAnalysis AtomGroup
Inherits from MemoryReader and converts OpenBabel OBMol Coordinates to a
MDAnalysis Trajectory which is used to build a Universe. This reader
does not work in the reverse direction.
"""
format = 'OPENBABEL'

# Structure.coordinates always in Angstrom
units = {'time': None, 'length': 'Angstrom'}

@staticmethod
def _format_hint(thing):
"""
Base function to check if the reader can actually read this “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?)
"""
try:
import openbabel as OB
except ImportError:
if HAS_OBABEL is False:
return False
else:
return isinstance(thing, OB.OBMol)
return isinstance(thing, OBMol)

def __init__(self, filename: OBMol, **kwargs):
"""
Converts file to OBMol to AtomGroup
"""
pass
n_atoms = filename.NumAtoms()
# single position
if filename.NumConformers() == 1:
coordinates = np.array([
[(coords := atom.GetVector()).GetX(),
coords.GetY(),
coords.GetZ()] for atom in OBMolAtomIter(filename)],
dtype=np.float32)
else:
# multiple conformers, such as for a trajectory
numConf = filename.NumConformers()
coordinates = np.zeros((numConf, n_atoms, 3))
for conf_id in range(numConf):
filename.SetConformer(conf_id)
for atom in OBMolAtomIter(filename):
coordinates_inner = np.array([
[(coords := atom.GetVector()).GetX(),
coords.GetY(),
coords.GetZ()] for atom in OBMolAtomIter(filename)],
dtype=np.float32)
coordinates[conf_id] = coordinates_inner
# no coordinates present
if not np.any(coordinates):
cbouy marked this conversation as resolved.
Show resolved Hide resolved
warnings.warn("No coordinates found in the OBMol")
coordinates = np.empty((1, n_atoms, 3), dtype=np.float32)
coordinates[:] = np.nan
super(OpenBabelReader, self).__init__(coordinates, order='fac', **kwargs)

class OpenBabelConverter(ConverterBase):
"""
Expand Down
426 changes: 426 additions & 0 deletions mda_openbabel_converter/data/1crn.pdb

Large diffs are not rendered by default.

98 changes: 98 additions & 0 deletions mda_openbabel_converter/data/ChEBI_26355.mol
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@

Marvin 01281309122D

43 50 0 0 0 0 999 V2000
10.0400 -6.3601 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
10.9791 -5.4001 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
11.6887 -4.9827 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
12.4191 -5.4001 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
13.3583 -6.3601 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
13.7757 -7.0697 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
13.3583 -7.7793 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
12.4191 -8.7393 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
11.6887 -9.1567 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
10.9791 -8.7393 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
10.0400 -7.7793 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
9.6226 -7.0697 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
12.5444 -6.2140 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0
12.5444 -7.9253 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0
10.8539 -7.9253 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0
9.6643 -5.6088 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
8.8674 -5.3953 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
13.1497 -5.0244 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
13.3632 -4.2275 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
13.7548 -8.5306 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
14.5516 -8.7441 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
9.6643 -8.5306 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
8.8674 -8.7441 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
10.2487 -5.0244 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
10.0352 -4.2275 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
9.2383 -4.0140 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
13.7548 -5.6088 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
14.5516 -5.3953 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
14.7652 -4.5984 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
10.2487 -9.1149 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
10.0352 -9.9118 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
9.2383 -10.1253 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
13.1497 -9.1149 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
13.3632 -9.9118 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
14.1601 -10.1253 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
8.2279 -11.1357 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
9.0248 -10.9222 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
9.6081 -11.5056 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
13.7902 -11.5056 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
14.3734 -10.9222 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
15.1703 -11.1357 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
10.8539 -6.2140 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0
11.6887 -7.0697 0.0000 Fe 0 0 0 0 0 0 0 0 0 0 0 0
1 42 1 0 0 0 0
16 1 2 0 0 0 0
1 12 1 0 0 0 0
2 3 1 0 0 0 0
42 2 1 0 0 0 0
24 2 2 0 0 0 0
3 4 2 0 0 0 0
4 13 1 0 0 0 0
18 4 1 0 0 0 0
5 6 1 0 0 0 0
13 5 2 0 0 0 0
27 5 1 0 0 0 0
6 7 2 0 0 0 0
7 14 1 0 0 0 0
7 20 1 0 0 0 0
8 9 2 0 0 0 0
33 8 1 0 0 0 0
14 8 1 0 0 0 0
10 9 1 0 0 0 0
15 10 2 0 0 0 0
30 10 1 0 0 0 0
11 12 2 0 0 0 0
15 11 1 0 0 0 0
22 11 1 0 0 0 0
13 43 1 0 0 0 0
43 14 1 0 0 0 0
15 43 1 0 0 0 0
24 16 1 0 0 0 0
16 17 1 0 0 0 0
18 27 2 0 0 0 0
18 19 1 0 0 0 0
20 33 2 0 0 0 0
20 21 1 0 0 0 0
22 30 2 0 0 0 0
22 23 1 0 0 0 0
24 25 1 0 0 0 0
25 26 2 0 0 0 0
27 28 1 0 0 0 0
28 29 2 0 0 0 0
30 31 1 0 0 0 0
31 32 1 0 0 0 0
32 37 1 0 0 0 0
33 34 1 0 0 0 0
34 35 1 0 0 0 0
35 40 1 0 0 0 0
37 36 1 0 0 0 0
37 38 2 0 0 0 0
40 39 1 0 0 0 0
40 41 2 0 0 0 0
42 43 1 0 0 0 0
M END
Loading
Loading