def add_atom(mol, atomic_number, atom): emol = EditableMol(mol) new_index = emol.AddAtom(Chem.Atom(atomic_number)) emol.AddBond(atom.GetIdx(), new_index, Chem.BondType.SINGLE) mol_ = emol.GetMol() Chem.SanitizeMol(mol_) return mol_
def graph(self, m): from rdkit.Chem import EditableMol, Atom, rdchem hcount = m.GetNumAtoms(False) - m.GetNumAtoms(True) # create new molecule using single bonds only em = EditableMol(Mol()) nbridx = [None] * m.GetNumAtoms() iatom = 0 for atom in m.GetAtoms(): atnum = atom.GetAtomicNum() if atnum == 1 and atom.GetIsotope() == 1: #if atom.GetMass() > 1: pass hcount += 1 else: newatom = Atom(atnum) #if atom.GetTotalDegree() == 0: newatom.SetNoImplicit(True) # otherwise [Na]. becomes [NaH]. #newatom.SetFormalCharge(atom.GetFormalCharge()) newatom.SetFormalCharge(0) em.AddAtom(newatom) aidx = atom.GetIdx() nbridx[aidx] = iatom iatom += 1 for a2 in atom.GetNeighbors(): a2idx = nbridx[a2.GetIdx()] if a2idx != None: em.AddBond(aidx, a2idx, rdchem.BondType.SINGLE) #cansmi = self.cansmiles(em.GetMol()) cansmi = MolToSmiles(m, isomericSmiles=True) #cansmi = cansmi.replace('+','').replace('-','').replace('[N]','N').replace('[O]','O').replace('[C]','C').replace('[I]','I').replace('[S]','S').replace('[P]','P').replace('[B]','B').replace('[Br]','Br').replace('[Cl]','Cl') return "%s%s%d%+d" % (cansmi, ' H', hcount, GetFormalCharge(m))
def get_matching_parts(mol, *matches): indices = reversed(range(mol.GetNumAtoms())) for match in matches: match = set(match) part = EditableMol(mol) for idx in indices: if idx not in match: part.RemoveAtom(idx) yield part.GetMol()
def replace_dummy_with_h(m): emol = EditableMol(m) for atom in m.GetAtoms(): if atom.GetAtomicNum() == 1: emol.ReplaceAtom(atom.GetIdx(), Chem.Atom(0)) m = emol.GetMol() # for atom in m.GetAtoms(): # if atom.GetAtomicNum() == 0: # for bond in atom.GetBonds(): # bond.SetBondType(Chem.BondType.UNSPECIFIED) # atom.SetIsAromatic(False) return m
def to_single_bond_remove_hs(mol): atom_map = {} emol = EditableMol(Chem.MolFromSmiles("")) for atom in mol.GetAtoms(): if atom.GetAtomicNum() > 1: new_idx = emol.AddAtom(Chem.Atom(atom.GetAtomicNum())) atom_map[atom.GetIdx()] = new_idx for bond in mol.GetBonds(): begin, end = bond.GetBeginAtom(), bond.GetEndAtom() if begin.GetAtomicNum() <= 1: continue if end.GetAtomicNum() <= 1: continue new_idxs = atom_map[begin.GetIdx()], atom_map[end.GetIdx()] emol.AddBond(new_idxs[0], new_idxs[1], Chem.BondType.SINGLE) return emol.GetMol()
def convert_molecule(molecule): from rdkit.Chem import Mol, EditableMol, Atom, Conformer mol = Mol() emol = EditableMol(mol) conformer = Conformer() atom_map = {} for atom in molecule.atoms: a = Atom(atom.element.number) atom_map[atom] = i = emol.AddAtom(a) conformer.SetAtomPosition(i, atom.coord().data()) for bond in molecule.bonds: a1, a2 = bond.atoms emol.AddBond(atom_map[a1], atom_map[a2]) mol = emol.GetMol() mol.AddConformer(conformer) return mol, atom_map
def remove_any_atom_bonds(m): if not has_ring(m): return m emol = EditableMol(Chem.MolFromSmiles("")) atom_map = {} for atom in m.GetAtoms(): if atom.GetAtomicNum() == 0: continue new_idx = emol.AddAtom(Chem.Atom(atom.GetAtomicNum())) atom_map[atom.GetIdx()] = new_idx for bond in m.GetBonds(): start, end = bond.GetBeginAtom(), bond.GetEndAtom() if start.GetAtomicNum() == 0: continue if end.GetAtomicNum() == 0: continue start, end = atom_map[start.GetIdx()], atom_map[end.GetIdx()] emol.AddBond(start, end, bond.GetBondType()) return emol.GetMol()
def graph2(self, m): from rdkit.Chem import EditableMol, RemoveHs, Atom, rdchem #, SanitizeMol, rdmolops #natoms = m.GetNumAtoms() # create new molecule using single bonds only em = EditableMol(Mol()) hcount = 0 for atom in m.GetAtoms(): atnum = atom.GetAtomicNum() hcount += atom.GetTotalNumHs(False) newatom = Atom(atnum) #newatom.SetFormalCharge(atom.GetFormalCharge()) em.AddAtom(newatom) for bond in m.GetBonds(): em.AddBond(bond.GetBeginAtomIdx(), bond.GetEndAtomIdx(), rdchem.BondType.SINGLE) try: mol = RemoveHs(em.GetMol()) except: mol = em.GetMol() #mol = em.GetMol() #SanitizeMol(mol, SanitizeFlags.SANITIZE_ADJUSTHS) #Chem.rdmolops.SanitizeFlags.SANITIZE_ADJUSTHS cansmi = self.cansmiles(mol) return "%s%s%d%+d" % (cansmi, ' H', hcount, GetFormalCharge(m))
def deprocess(vertices, edges): from rdkit.Chem import EditableMol, MolFromSmiles, Atom, MolToSmiles, Bond, BondType, Mol m = EditableMol(Mol()) for v in vertices: if v != 0: m.AddAtom(Atom(v)) for begin, end, type in edges: if (begin, end) == (0, 0): continue if begin == end: continue if begin == len(vertices) - 1: continue if end == len(vertices) - 1: continue if begin > end: begin, end = end, begin if type == 0: continue if m.GetMol().GetBondBetweenAtoms(begin, end): continue m.AddBond(begin, end, BondType.values[type]) s = MolToSmiles(m.GetMol()) return s
def remove_isotopes(mol, sanitize=True): edmol = EditableMol(mol) for atom in mol.GetAtoms(): atom.SetIsotope(0) if sanitize: SanitizeMol(mol)
def add_bond_helper(mol, a1, a2, bond_type): emol = EditableMol(mol) emol.AddBond(a1.GetIdx(), a2.GetIdx(), bond_type) return sanitize_emol(emol)
def create_emol(mol): return EditableMol(mol)