Skip to content

Commit a2f11cd

Browse files
committed
drawMolecule() can now cope with either implicit or explicit Hydrogens.
It doesn't need to change. It deletes the hydrogens that it's not going to draw from the lists and dicts that it has copied, leaving the originals alone, and it keeps track independently of how many times it has done this for each heavy atom.
1 parent 55de310 commit a2f11cd

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

rmgpy/molecule_draw.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,9 +1134,12 @@ def drawMolecule(molecule, path=None, surface=''):
11341134
print 'Cairo not found; molecule will not be drawn.'
11351135
return
11361136

1137-
# This algorithm requires that the hydrogen atoms be implicit
1138-
implicitH = molecule.implicitHydrogens
1139-
molecule.makeHydrogensImplicit()
1137+
# This algorithm now works with explicit hydrogen atoms on the molecule.
1138+
# Please ensure all the subroutines do also.
1139+
# We will delete them from the *copied* list of atoms, and store them here:
1140+
implicitHydrogensToDraw = {}
1141+
for atom in molecule.atoms:
1142+
implicitHydrogensToDraw[atom] = atom.implicitHydrogens
11401143

11411144
atoms = molecule.atoms[:]
11421145
# bonds = molecule.bonds.copy() is too shallow for a dict-of-dicts,
@@ -1149,15 +1152,17 @@ def drawMolecule(molecule, path=None, surface=''):
11491152

11501153
# Special cases: H, H2, anything with one heavy atom
11511154

1152-
# Remove all unlabeled hydrogen atoms from the molecule, as they are not drawn
1155+
# Remove all unlabeled hydrogen atoms from the copied atoms and bonds, as they are not drawn
11531156
# However, if this would remove all atoms, then don't remove any
11541157
atomsToRemove = []
11551158
for atom in atoms:
11561159
if atom.isHydrogen() and atom.label == '': atomsToRemove.append(atom)
11571160
if len(atomsToRemove) < len(atoms):
11581161
for atom in atomsToRemove:
11591162
atoms.remove(atom)
1160-
for atom2 in bonds[atom]: del bonds[atom2][atom]
1163+
for atom2 in bonds[atom]:
1164+
del bonds[atom2][atom]
1165+
implicitHydrogensToDraw[atom2] = implicitHydrogensToDraw[atom2] + 1
11611166
del bonds[atom]
11621167

11631168
# Generate the coordinates to use to draw the molecule
@@ -1191,8 +1196,8 @@ def drawMolecule(molecule, path=None, surface=''):
11911196
# Add implicit hydrogens
11921197
for i in range(len(symbols)):
11931198
if symbols[i] != '':
1194-
if atoms[i].implicitHydrogens == 1: symbols[i] = symbols[i] + 'H'
1195-
elif atoms[i].implicitHydrogens > 1: symbols[i] = symbols[i] + 'H{0:d}'.format(atoms[i].implicitHydrogens)
1199+
if implicitHydrogensToDraw.get(atoms[i],0) == 1: symbols[i] = symbols[i] + 'H'
1200+
elif implicitHydrogensToDraw.get(atoms[i],0) > 1: symbols[i] = symbols[i] + 'H{0:d}'.format(implicitHydrogensToDraw[atoms[i]])
11961201

11971202
# Special case: H2 (render as H2 and not H-H)
11981203
if symbols == ['H','H']:
@@ -1228,8 +1233,6 @@ def drawMolecule(molecule, path=None, surface=''):
12281233
else:
12291234
surface.finish()
12301235

1231-
if not implicitH: molecule.makeHydrogensExplicit()
1232-
12331236
return surface, cr, (0, 0, width, height)
12341237

12351238
################################################################################

0 commit comments

Comments
 (0)