Skip to content

Commit bf9ca66

Browse files
committed
New method Molecule.deleteHydrogens()
This is a bit like makeHydrogensImplicit, except that it doesn't increment the implicitHydrogens counter on the neighboring atoms, so there is no getting them back! This is useful for some situations where you have a shallow copy of a molecule and you want to consider it without the hydrogens, but do not want to edit attributes of the atoms that are shared with the original molecule.
1 parent 753455b commit bf9ca66

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

rmgpy/molecule.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ cdef class Molecule(Graph):
104104

105105
cpdef Graph copy(self, bint deep=?)
106106

107+
cpdef deleteHydrogens(self)
108+
107109
cpdef makeHydrogensImplicit(self)
108110

109111
cpdef makeHydrogensExplicit(self)

rmgpy/molecule.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,32 @@ def split(self):
594594
molecules.append(molecule)
595595
return molecules
596596

597+
def deleteHydrogens(self):
598+
"""
599+
Irreversibly delete all non-labeled hydrogens, without incrementing
600+
the "implicitHydrogens" count of the neighbouring atom, or updating
601+
Connectivity Values or the implicitHydrogens flag. If there's nothing
602+
but Hydrogens, it does nothing.
603+
It destroys information; be careful with it.
604+
"""
605+
cython.declare(atom=Atom, neighbor=Atom, hydrogens=list)
606+
# Check that the structure contains at least one heavy atom
607+
for atom in self.vertices:
608+
if not atom.isHydrogen():
609+
break
610+
else:
611+
# No heavy atoms, so leave explicit
612+
return
613+
hydrogens = []
614+
for atom in self.vertices:
615+
if atom.isHydrogen() and atom.label == '':
616+
neighbor = self.edges[atom].keys()[0]
617+
hydrogens.append(atom)
618+
# Remove the hydrogen atoms from the structure
619+
for atom in hydrogens:
620+
self.removeAtom(atom)
621+
622+
597623
def makeHydrogensImplicit(self):
598624
"""
599625
Convert all explicitly stored hydrogen atoms to be stored implicitly,

0 commit comments

Comments
 (0)