Macs in Chemistry

Insanely great science


Scripting Vortex 5

In the first script I showed how to calculate molecular similarity between the first molecule in the file and all the other molecules using OpenBabel as shown below.

PROMPT>  babel  mysmiles.smi  mymols.sdf -ofpt
MOL_00000067   Tanimoto from first mol = 0.0888889
MOL_00000083   Tanimoto from first mol = 0.0869565
MOL_00000105   Tanimoto from first mol = 0.0888889
MOL_00000296   Tanimoto from first mol = 0.0714286
MOL_00000320   Tanimoto from first mol = 0.0888889

This is fine for examples where the first structure is perhaps the active lead structure and the other molecules are potential compounds for evaluation derived from a substructure, pharmacophore searches, docking etc. However on many occasions you may want to simply calculate the similarity to a probe structure that is not part of the file. To do this in Vortex we can capture the structural query from the in built sketcher. Vortex supports a variety of 3rd party chemical drawing packages as well as their own in house package Elemental, however by capturing the content of the structural query box we can simply ignore which package was used to generate the content.

This command will capture MolFile

vtable.findColumnWithName("Structure", 0).getChemLink().getMolFile()

whilst this will get the structure in SMARTS format

vtable.findColumnWithName("Structure", 0).getChemLink().getSmarts()

Unfortunately if you run this from the console you get are rather unexpected result.

smiQuery = vtable.findColumnWithName("Structure",0).getChemLink().getSmarts() 

It seems the coordinates are put inline with the SMARTS, but you can fix it with the following one liner

re.sub("{[^}]+}", "", smiQuery)

(remember to import re at the top)

OpenBabel provides two programs obabel and babel which are cross-platform programs designed to interconvert between many file formats used in molecular modelling and computational chemistry and related areas. They can also be used for filtering molecules and for simple manipulation of chemical data. Whilst there are many similarities between the programs essentially obabel is a modern version of babel with additional capabilities and a more standard interface. Over time, obabel will replace babel.

Specifically, the differences are as follows:

More than one can be used, and a molecule title can be included if enclosed in quotes:

obabel -:"O=C(O)c1ccccc1OC(=O)C" -ocan

obabel -:"O=C(O)c1ccccc1OC(=O)C aspirin" -:"Oc1ccccc1C(=O)O salicylic acid"  -ofpt

So the babel command we need is

 obabel -:"CC(=O)C1=CC=CC=C1Br" /Users/username/Desktop/ChemicalStructures/acetophenones.sdf -ofpt

We can add the -: and the query SMARTS becomes

 newSmi= '-:' + smi_Query

The rest of the script is very similar to the previous scripts, we run the process and capture the output. Create two new columns, you can pass a third argument to the findColumnWithName which will set the type for the column if it gets created, the value 3 will set it to string, 1 is double.

column = vtable.findColumnWithName('Q_Sim_FP2', 1, 1)
column = vtable.findColumnWithName('Superstructure', 1, 3)

We then parse the output and populate the columns. One minor issue is the fireTableStructureChanged() call triggers a reset of all the filter cells including the structure query which means you will need to redraw the query if you want to modify it.

The Vortex Script

import sys

# Uncomment the following 2 lines if running in console
#vortex = console.vortex
#vtable = console.vtable

sys.path.append(vortex.getVortexFolder() + '/modules/jythonlib')
import re
import subprocess

# Get the path to the currently open sdf file
sdfFile = vortex.getFileForPropertyCalculation(vtable)

# Get the SMARTS of the drawn structure
smiQuery = vtable.findColumnWithName("Structure", 0).getChemLink().getSmarts() 

# Remove coordinates from the SMARTS string
smi_Query = re.sub("{[^}]+}", "", smiQuery)

newSmi= '-:' + smi_Query

# obabel -:"CC(=O)C1=CC=CC=C1Br" /Users/username/Desktop/ChemicalStructures/acetophenones.sdf -ofpt

p = subprocess.Popen(['/usr/local/bin/obabel', newSmi, sdfFile, '-ofpt', '-xfFP2'], stdout=subprocess.PIPE)
output = p.communicate()[0]

column = vtable.findColumnWithName('Q_Sim_FP2', 1, 1)
column = vtable.findColumnWithName('Superstructure', 1, 3)

# Parse output
lines = output.split('\n')
currentRow = 0
for i in range(1, len(lines)-1):
if lines[i][0] == '>':
    column = vtable.findColumnWithName('Q_Sim_FP2', 0)
    column.setValueFromString(currentRow, lines[i].split()[-1])
    currentRow += 1
# Clear Superstructure column of any previous data      
    column = vtable.findColumnWithName('Superstructure', 0)
    column.setValueFromString(currentRow-1, '')
elif lines[i][0:23] == 'Possible superstructure':
    column = vtable.findColumnWithName('Superstructure', 0)
    column.setValueFromString(currentRow-1, 'YES')

In this example I’ve used the FP2 fingerprint but OpenBabel supports several different fingerprints

PROMPT> babel -L fingerprints
FP2    Indexes linear fragments up to 7 atoms.
FP3    SMARTS patterns specified in the file patterns.txt
FP4    SMARTS patterns specified in the file SMARTS_InteLigand.txt
MACCS    SMARTS patterns specified in the file MACCS.txt

In a similar way to the first script it is straight-forward to choose an alternative fingerprint. The results should look like the image shown below.


If you just want to compare the similarity with a structure in the worksheet, simply right-click on the structure and select Copy to ChemLink from the drop down menu, the structure will then be copied to the chemical query box.


The script can be downloaded here

The Vortex Scripts

Scripting Vortex Using OpenBabel
Scripting Vortex 2 Using filter-it
Scripting Votrex 3 Using cxcalc
Scripting Vortex 4 Using MOE
Scripting Vortex 5 Calculating similarities using OpenBabel
Scripting Vortex 6 Filtering compounds
Scripting Vortex 7 Using MayaChemTools
Scripting Vortex 8 Molecular Shape matching
Scripting Vortex 9 Getting a 2D depiction
Scripting Vortex 10 Interacting with the user
Scripting Vortex 11 Interacting with a web service
Scripting Vortex 12 JSON import
Scripting Vortex 13 Using OpenBabel fastsearch
Other Hints, Tips and Tutorials

Last Updated 7 Feb 2011