Skip to content

Commit 0bd9b13

Browse files
committed
Change SMILES writer to remove stereochemistry.
We (currently) don't have any stereochemistry, it's just guessed by OpenBabel, and makes the SMILES strings longer and more ugly than they need to be. This removes the @@h signs, and also might make it faster as we make fewer calls through pybel.
1 parent 108886f commit 0bd9b13

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

rmgpy/molecule/molecule.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ cdef class Atom(Vertex):
5656

5757
################################################################################
5858

59+
cpdef object SMILEwriter
60+
5961
cdef class Bond(Edge):
6062

6163
cdef public str order

rmgpy/molecule/molecule.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@
3939
import os
4040
import re
4141
import element as elements
42+
import openbabel
4243
from .graph import Vertex, Edge, Graph
4344
from .group import GroupAtom, GroupBond, Group, ActionError
4445
from .atomtype import AtomType, atomTypes, getAtomType
45-
46+
4647
################################################################################
4748

4849
class Atom(Vertex):
@@ -439,7 +440,10 @@ def applyAction(self, action):
439440
raise ActionError('Unable to update GroupBond: Invalid action {0}.'.format(action))
440441

441442
################################################################################
442-
443+
SMILEwriter = openbabel.OBConversion()
444+
SMILEwriter.SetOutFormat('smi')
445+
SMILEwriter.SetOptions("i",SMILEwriter.OUTOPTIONS) # turn off isomer and stereochemistry information (the @ signs!)
446+
443447
class Molecule(Graph):
444448
"""
445449
A representation of a molecular structure using a graph data type, extending
@@ -907,7 +911,6 @@ def toInChI(self):
907911
Convert a molecular structure to an InChI string. Uses
908912
`OpenBabel <http://openbabel.org/>`_ to perform the conversion.
909913
"""
910-
import openbabel
911914
# This version does not write a warning to stderr if stereochemistry is undefined
912915
obmol = self.toOBMol()
913916
obConversion = openbabel.OBConversion()
@@ -959,24 +962,21 @@ def toAugmentedInChIKey(self):
959962
return key+'mult'+str(radicalNumber+1)
960963
else:
961964
return key
962-
965+
966+
963967
def toSMILES(self):
964968
"""
965969
Convert a molecular structure to an SMILES string. Uses
966970
`OpenBabel <http://openbabel.org/>`_ to perform the conversion.
967971
"""
968-
import pybel
969-
mol = pybel.Molecule(self.toOBMol())
970-
return mol.write('smiles').strip()
972+
mol = self.toOBMol()
973+
return SMILEwriter.WriteString(mol).strip()
971974

972975
def toOBMol(self):
973976
"""
974977
Convert a molecular structure to an OpenBabel OBMol object. Uses
975978
`OpenBabel <http://openbabel.org/>`_ to perform the conversion.
976979
"""
977-
978-
import openbabel
979-
980980
cython.declare(atom=Atom, atom1=Atom, bonds=dict, atom2=Atom, bond=Bond)
981981
cython.declare(index1=cython.int, index2=cython.int, order=cython.int)
982982

0 commit comments

Comments
 (0)