def to_rdkit_molecule(data): """ MoleculeContainer to RDKit molecule object converter """ mol = RWMol() mapping = {} for n, a in data.atoms(): ra = Atom(a.atomic_number) ra.SetAtomMapNum(n) if a.charge: ra.SetFormalCharge(a.charge) if a.isotope: ra.SetIsotope(a.isotope) if a.is_radical: ra.SetNumRadicalElectrons(1) mapping[n] = mol.AddAtom(ra) for n, m, b in data.bonds(): mol.AddBond(mapping[n], mapping[m], _bond_map[b.order]) conf = Conformer() for n, a in data.atoms(): conf.SetAtomPosition(mapping[n], (a.x, a.y, 0)) conf.Set3D(False) mol.AddConformer(conf) for c in data._conformers: conf = Conformer() for n, xyz in c.items(): conf.SetAtomPosition(mapping[n], xyz) mol.AddConformer(conf) SanitizeMol(mol) return mol
def to_rdkit_molecule(data: MoleculeContainer): """ MoleculeContainer to RDKit molecule object converter """ mol = RWMol() mapping = {} bonds = data._bonds for n, a in data.atoms(): ra = Atom(a.atomic_number) ra.SetAtomMapNum(n) if a.charge: ra.SetFormalCharge(a.charge) if a.isotope: ra.SetIsotope(a.isotope) if a.is_radical: ra.SetNumRadicalElectrons(1) mapping[n] = mol.AddAtom(ra) for n, m, b in data.bonds(): mol.AddBond(mapping[n], mapping[m], _bond_map[b.order]) for n in data._atoms_stereo: ra = mol.GetAtomWithIdx(mapping[n]) env = bonds[n] s = data._translate_tetrahedron_sign(n, [x for x in mapping if x in env]) ra.SetChiralTag(_chiral_ccw if s else _chiral_cw) for nm, s in data._cis_trans_stereo.items(): n, m = nm if m in bonds[n]: # cumulenes unsupported nn, nm, *_ = data._stereo_cis_trans[nm] b = mol.GetBondBetweenAtoms(mapping[n], mapping[m]) b.SetStereoAtoms(mapping[nn], mapping[nm]) b.SetStereo(_cis if s else _trans) conf = Conformer() for n, a in data.atoms(): conf.SetAtomPosition(mapping[n], (a.x, a.y, 0)) conf.Set3D(False) mol.AddConformer(conf, assignId=True) for c in data._conformers: conf = Conformer() for n, xyz in c.items(): conf.SetAtomPosition(mapping[n], xyz) mol.AddConformer(conf, assignId=True) SanitizeMol(mol) AssignStereochemistry(mol, flagPossibleStereoCenters=True, force=True) return mol
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