OpenBabel Interface

In this tutorial we will demonstrate how molli Molecule objects can be efficiently converted into openbabel objects.

Note: openbabel is not natively installed within Molli, but it can be added through conda using the line: conda install openbabel

[1]:
#Imports molli
import molli as ml

#Imports the Openbabel
from openbabel import openbabel as ob

#Imports molli interface with openbabel for additional functionality
from molli.external import openbabel as mob
ml.visual.configure(bgcolor='white')

The following cell shows a conversion of an example molecule into openbabel OBMol object

[2]:
#This loads the Molli molecule object
ml_mol = ml.load(ml.files.dendrobine_mol2)

#This creates an instance of an OBMol object
obmol = mob.to_obmol(ml_mol)

Now let’s make sure that what we get out of that operation is a valid OBMol object by converting it into a human readable representation.

[ ]:
converter = ob.OBConversion()
converter.SetInAndOutFormats("mol2", "mol2")
print(converter.WriteString(obmol))
@<TRIPOS>MOLECULE
dendrobine
 44 47 0 0 0
SMALL
GASTEIGER

@<TRIPOS>ATOM
      1 N           1.2960   -0.2319    1.2670 N.3     1  UNL1       -0.2981
      2 C           0.0573   -0.0226    2.1227 C.3     1  UNL1        0.0024
      3 C          -1.0974   -0.4738    1.2059 C.3     1  UNL1       -0.0210
      4 C          -0.4284   -0.4113   -0.1687 C.3     1  UNL1       -0.0055
      5 C           0.8683    0.3598   -0.0004 C.3     1  UNL1        0.0534
      6 C           2.4562    0.4417    1.8361 C.3     1  UNL1       -0.0123
      7 C          -2.4328    0.2659    0.9832 C.3     1  UNL1       -0.0482
      8 C          -2.6553    0.2472   -0.5801 C.3     1  UNL1       -0.0486
      9 C          -1.2428    0.5626   -1.0163 C.3     1  UNL1       -0.0205
     10 C           1.5353    0.5790   -1.4179 C.3     1  UNL1        0.1228
     11 O           1.3410    2.0363   -1.6507 O.3     1  UNL1       -0.4592
     12 C           0.0782    2.2291   -2.2166 C.2     1  UNL1        0.3112
     13 C          -0.5977    0.8539   -2.4020 C.3     1  UNL1        0.0662
     14 O          -0.4135    3.3180   -2.4552 O.2     1  UNL1       -0.2505
     15 C           0.7602    0.1678   -2.7001 C.3     1  UNL1        0.0132
     16 C           0.7812   -1.2141   -3.3773 C.3     1  UNL1       -0.0397
     17 H           1.2426    0.7684   -3.4982 H       1  UNL1        0.0347
     18 H          -1.1794    1.5580   -0.5377 H       1  UNL1        0.0315
     19 H           0.6516    1.3952    0.3174 H       1  UNL1        0.0504
     20 H          -1.3722   -1.4910    1.5134 H       1  UNL1        0.0319
     21 C          -0.2828   -1.8835   -0.5957 C.3     1  UNL1       -0.0574
     22 C           2.1775   -1.8412   -3.3215 C.3     1  UNL1       -0.0623
     23 C           0.3485   -1.1006   -4.8514 C.3     1  UNL1       -0.0623
     24 H           0.0590   -1.9009   -2.9478 H       1  UNL1        0.0300
     25 H          -0.0649    1.0264    2.4198 H       1  UNL1        0.0429
     26 H           0.1162   -0.6408    3.0244 H       1  UNL1        0.0429
     27 H           2.7150   -0.0049    2.8019 H       1  UNL1        0.0391
     28 H           2.2927    1.5147    1.9874 H       1  UNL1        0.0391
     29 H           3.3255    0.3162    1.1818 H       1  UNL1        0.0391
     30 H          -2.3716    1.2962    1.3534 H       1  UNL1        0.0269
     31 H          -3.2639   -0.2261    1.4982 H       1  UNL1        0.0269
     32 H          -3.3810    1.0069   -0.8836 H       1  UNL1        0.0268
     33 H          -2.9934   -0.7358   -0.9222 H       1  UNL1        0.0268
     34 H           2.6088    0.3798   -1.4394 H       1  UNL1        0.0753
     35 H          -1.2929    0.8772   -3.2423 H       1  UNL1        0.0420
     36 H          -0.3075   -2.5854    0.2475 H       1  UNL1        0.0236
     37 H           0.6920   -2.0890   -1.0319 H       1  UNL1        0.0236
     38 H          -1.0943   -2.1904   -1.2614 H       1  UNL1        0.0236
     39 H           2.1795   -2.8241   -3.8049 H       1  UNL1        0.0232
     40 H           2.5176   -1.9822   -2.2914 H       1  UNL1        0.0232
     41 H           2.9143   -1.2135   -3.8343 H       1  UNL1        0.0232
     42 H           0.3522   -2.0859   -5.3307 H       1  UNL1        0.0232
     43 H           1.0232   -0.4520   -5.4210 H       1  UNL1        0.0232
     44 H          -0.6652   -0.6967   -4.9346 H       1  UNL1        0.0232
@<TRIPOS>BOND
     1    43    23    1
     2    42    23    1
     3    44    23    1
     4    23    16    1
     5    41    22    1
     6    39    22    1
     7    17    15    1
     8    16    22    1
     9    16    24    1
    10    16    15    1
    11    22    40    1
    12    35    13    1
    13    15    13    1
    14    15    10    1
    15    14    12    2
    16    13    12    1
    17    13     9    1
    18    12    11    1
    19    11    10    1
    20    34    10    1
    21    10     5    1
    22    38    21    1
    23    37    21    1
    24     9     8    1
    25     9    18    1
    26     9     4    1
    27    33     8    1
    28    32     8    1
    29    21     4    1
    30    21    36    1
    31     8     7    1
    32     4     5    1
    33     4     3    1
    34     5    19    1
    35     5     1    1
    36     7     3    1
    37     7    30    1
    38     7    31    1
    39    29     6    1
    40     3    20    1
    41     3     2    1
    42     1     6    1
    43     1     2    1
    44     6    28    1
    45     6    27    1
    46     2    25    1
    47     2    26    1

Molli has built in wrappers that can interface within Openbabel. For example, an ml_mol object could be converted to a file format that is not natively available as an output within Molli, such as SDF or SMILES, using the ml.dumps function.

[4]:
sdf_str = ml.dumps(ml_mol, fmt='sdf', writer='openbabel')

print(sdf_str)

smi_str = ml.dumps(ml_mol, fmt='smi', writer='openbabel')

print(smi_str)
dendrobine
 OpenBabel10212414373D

 44 47  0  0  1  0  0  0  0  0999 V2000
    1.2960   -0.2319    1.2670 N   0  0  0  0  0  0  0  0  0  0  0  0
    0.0573   -0.0226    2.1227 C   0  0  0  0  0  0  0  0  0  0  0  0
   -1.0974   -0.4738    1.2059 C   0  0  2  0  0  0  0  0  0  0  0  0
   -0.4284   -0.4113   -0.1687 C   0  0  2  0  0  0  0  0  0  0  0  0
    0.8683    0.3598   -0.0004 C   0  0  2  0  0  0  0  0  0  0  0  0
    2.4562    0.4417    1.8361 C   0  0  0  0  0  0  0  0  0  0  0  0
   -2.4328    0.2659    0.9832 C   0  0  0  0  0  0  0  0  0  0  0  0
   -2.6553    0.2472   -0.5801 C   0  0  0  0  0  0  0  0  0  0  0  0
   -1.2428    0.5626   -1.0163 C   0  0  2  0  0  0  0  0  0  0  0  0
    1.5353    0.5790   -1.4179 C   0  0  2  0  0  0  0  0  0  0  0  0
    1.3410    2.0363   -1.6507 O   0  0  0  0  0  0  0  0  0  0  0  0
    0.0782    2.2291   -2.2166 C   0  0  0  0  0  0  0  0  0  0  0  0
   -0.5977    0.8539   -2.4020 C   0  0  1  0  0  0  0  0  0  0  0  0
   -0.4135    3.3180   -2.4552 O   0  0  0  0  0  0  0  0  0  0  0  0
    0.7602    0.1678   -2.7001 C   0  0  2  0  0  0  0  0  0  0  0  0
    0.7812   -1.2141   -3.3773 C   0  0  0  0  0  0  0  0  0  0  0  0
    1.2426    0.7684   -3.4982 H   0  0  0  0  0  0  0  0  0  0  0  0
   -1.1794    1.5580   -0.5377 H   0  0  0  0  0  0  0  0  0  0  0  0
    0.6516    1.3952    0.3174 H   0  0  0  0  0  0  0  0  0  0  0  0
   -1.3722   -1.4910    1.5134 H   0  0  0  0  0  0  0  0  0  0  0  0
   -0.2828   -1.8835   -0.5957 C   0  0  0  0  0  0  0  0  0  0  0  0
    2.1775   -1.8412   -3.3215 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.3485   -1.1006   -4.8514 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.0590   -1.9009   -2.9478 H   0  0  0  0  0  0  0  0  0  0  0  0
   -0.0649    1.0264    2.4198 H   0  0  0  0  0  0  0  0  0  0  0  0
    0.1162   -0.6408    3.0244 H   0  0  0  0  0  0  0  0  0  0  0  0
    2.7150   -0.0049    2.8019 H   0  0  0  0  0  0  0  0  0  0  0  0
    2.2927    1.5147    1.9874 H   0  0  0  0  0  0  0  0  0  0  0  0
    3.3255    0.3162    1.1818 H   0  0  0  0  0  0  0  0  0  0  0  0
   -2.3716    1.2962    1.3534 H   0  0  0  0  0  0  0  0  0  0  0  0
   -3.2639   -0.2261    1.4982 H   0  0  0  0  0  0  0  0  0  0  0  0
   -3.3810    1.0069   -0.8836 H   0  0  0  0  0  0  0  0  0  0  0  0
   -2.9934   -0.7358   -0.9222 H   0  0  0  0  0  0  0  0  0  0  0  0
    2.6088    0.3798   -1.4394 H   0  0  0  0  0  0  0  0  0  0  0  0
   -1.2929    0.8772   -3.2423 H   0  0  0  0  0  0  0  0  0  0  0  0
   -0.3075   -2.5854    0.2475 H   0  0  0  0  0  0  0  0  0  0  0  0
    0.6920   -2.0890   -1.0319 H   0  0  0  0  0  0  0  0  0  0  0  0
   -1.0943   -2.1904   -1.2614 H   0  0  0  0  0  0  0  0  0  0  0  0
    2.1795   -2.8241   -3.8049 H   0  0  0  0  0  0  0  0  0  0  0  0
    2.5176   -1.9822   -2.2914 H   0  0  0  0  0  0  0  0  0  0  0  0
    2.9143   -1.2135   -3.8343 H   0  0  0  0  0  0  0  0  0  0  0  0
    0.3522   -2.0859   -5.3307 H   0  0  0  0  0  0  0  0  0  0  0  0
    1.0232   -0.4520   -5.4210 H   0  0  0  0  0  0  0  0  0  0  0  0
   -0.6652   -0.6967   -4.9346 H   0  0  0  0  0  0  0  0  0  0  0  0
  1  6  1  0  0  0  0
  1  2  1  0  0  0  0
  2 25  1  0  0  0  0
  2 26  1  0  0  0  0
  3 20  1  1  0  0  0
  3  2  1  0  0  0  0
  4 21  1  6  0  0  0
  4  5  1  0  0  0  0
  4  3  1  0  0  0  0
  5 19  1  1  0  0  0
  5  1  1  0  0  0  0
  6 28  1  0  0  0  0
  6 27  1  0  0  0  0
  7  3  1  0  0  0  0
  7 30  1  0  0  0  0
  7 31  1  0  0  0  0
  8  7  1  0  0  0  0
  9  8  1  0  0  0  0
  9 18  1  1  0  0  0
  9  4  1  0  0  0  0
 10 34  1  6  0  0  0
 10  5  1  0  0  0  0
 11 10  1  0  0  0  0
 12 11  1  0  0  0  0
 13 35  1  6  0  0  0
 13 12  1  0  0  0  0
 13  9  1  0  0  0  0
 14 12  2  0  0  0  0
 15 17  1  6  0  0  0
 15 13  1  0  0  0  0
 15 10  1  0  0  0  0
 16 22  1  0  0  0  0
 16 24  1  0  0  0  0
 16 15  1  0  0  0  0
 21 36  1  0  0  0  0
 22 40  1  0  0  0  0
 23 16  1  0  0  0  0
 29  6  1  0  0  0  0
 32  8  1  0  0  0  0
 33  8  1  0  0  0  0
 37 21  1  0  0  0  0
 38 21  1  0  0  0  0
 39 22  1  0  0  0  0
 41 22  1  0  0  0  0
 42 23  1  0  0  0  0
 43 23  1  0  0  0  0
 44 23  1  0  0  0  0
M  END
$$$$

N1(C[C@@H]2[C@@]3([C@@H]1[C@@H]1OC(=O)[C@H]([C@H]3CC2)[C@@H]1C(C)C)C)C  dendrobine

CDXMLFile to Optimized Structure Example

The molli package does not have native geometry optimization; however, the interface with openbabel allows for the use of molecular forcefields to return updated coordinates for the ml_mol. This will be shown with an axial chiral structure R-BINOL.

image.png

[5]:
# Reads CDXML File
cdxf = ml.CDXMLFile(ml.files.parser_demo_cdxml)

# Access molecule object
ml_mol = cdxf['r_binol']

# Adds implicit hydrogens
ml_mol.add_implicit_hydrogens()
ml_mol
/home/blakeo2/new_molli/molli_dev/molli/molli/ftypes/cdxml.py:157: CDXMLSyntaxWarning: CDXML file /home/blakeo2/new_molli/molli_dev/molli/molli/files/parser_demo.cdxml contains redundant label 'naphthalene' Only the first occurrence will be kept.
  warn(

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
jupyter labextension install jupyterlab_3dmol

[5]:
Molecule(name='r_binol', formula='C20 H14 O2')

Structures can also be optimized using the obabel_optimize function. ### Warning In certain cases, straight bonds parsed sometimes do not optimize. This can be fixed by doing a slight coordinate displacement. If you still experience problems with openbabel refusing to optimize the structures, do not forget to set the BABEL_DATADIR environment variable to point to a valid location with openbabel data files. With conda installations, it seems to be consistently pointing to $CONDA_PREFIX/share/openbabel

[6]:
#Optimizes the structure
mob.obabel_optimize(
    ml_mol,
    ff = "MMFF94",
    max_steps = 1000,
    inplace=True,
    coord_displace=True #Sometimes necessary to get flat bonds/structures to correctly relax
)
ml_mol

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
jupyter labextension install jupyterlab_3dmol

[6]:
Molecule(name='r_binol', formula='C20 H14 O2')

Multi-Attachment Optimization from CDXML File

molli has also introduced a prototype optimization function optimize_coordination, that has been successfully tested with a few different structures. It has run into some issues with Linux and Mac operating systems, but has shown greater success on Windows. It is available in the openbabel interface within molli, but due to the problems on varying OS, it is not fully illustrated in this example.