Skip to content

Commit

Permalink
to_rdkit_molecule converter implemented
Browse files Browse the repository at this point in the history
version BUMP
  • Loading branch information
stsouko committed Mar 22, 2019
1 parent dc6981f commit 859fb1f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
40 changes: 36 additions & 4 deletions CGRtools/utils/rdkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, see <https://www.gnu.org/licenses/>.
#
from rdkit.Chem import BondType
from rdkit.Chem import BondType, Atom, RWMol, SanitizeMol, Conformer
from ..containers import MoleculeContainer


Expand All @@ -38,7 +38,7 @@ def from_rdkit_molecule(data):
atom['isotope'] = isotope
radical = a.GetNumRadicalElectrons()
if radical:
atom['multiplicity'] = 2 if radical == 1 else 3
atom['multiplicity'] = radical + 1

conformers = data.GetConformers()
if conformers:
Expand All @@ -56,7 +56,39 @@ def from_rdkit_molecule(data):
return m


_rdkit_bond_map = {BondType.SINGLE: 1, BondType.DOUBLE: 2, BondType.TRIPLE: 3, BondType.AROMATIC: 4}
def to_rdkit_molecule(data):
"""
MoleculeContainer to RDKit molecule object converter
"""
mol = RWMol()
conf = Conformer()
mapping = {}
is_3d = False
for n, a in data.atoms():
ra = Atom(a.number)
ra.SetAtomMapNum(n)
if a.charge:
ra.SetFormalCharge(a.charge)
if a.isotope != a.common_isotope:
ra.SetIsotope(a.isotope)
if a.radical:
ra.SetNumRadicalElectrons(a.radical)
mapping[n] = m = mol.AddAtom(ra)
conf.SetAtomPosition(m, (a.x, a.y, a.z))
if a.z:
is_3d = True
if not is_3d:
conf.Set3D(False)

for n, m, b in data.bonds():
mol.AddBond(mapping[n], mapping[m], _bond_map[b.order])

mol.AddConformer(conf)
SanitizeMol(mol)
return mol


_rdkit_bond_map = {BondType.SINGLE: 1, BondType.DOUBLE: 2, BondType.TRIPLE: 3, BondType.AROMATIC: 4}
_bond_map = {1: BondType.SINGLE, 2: BondType.DOUBLE, 3: BondType.TRIPLE, 4: BondType.AROMATIC}

__all__ = ['from_rdkit_molecule']
__all__ = ['from_rdkit_molecule', 'to_rdkit_molecule']
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

setup(
name='CGRtools',
version='3.1.3',
version='3.1.4',
packages=['CGRtools', 'CGRtools.algorithms', 'CGRtools.attributes', 'CGRtools.containers', 'CGRtools.files',
'CGRtools.files.dll', 'CGRtools.periodictable', 'CGRtools.utils'],
url='https://github.com/cimm-kzn/CGRtools',
Expand Down

0 comments on commit 859fb1f

Please sign in to comment.