def depict(self, filename=None, ipython=False): from rdkit.Chem.Draw import IPythonConsole from rdkit.Chem.Draw import MolToImage from rdkit.Chem.Draw import rdMolDraw2D from rdkit.Chem.AllChem import EmbedMolecule from IPython.display import SVG from rdkit.Chem import RWMol, MolFromSmiles, Atom, BondType, ChiralType _ = MolFromSmiles('C') rmol = RWMol(_) dict_old_new_idx = {} n = 1 for a in self.atoms: old_idx = a.GetIdx() rmol.AddAtom(a) dict_old_new_idx[old_idx] = n n += 1 for a in self.enviroments: old_idx = a.GetIdx() a.SetChiralTag(ChiralType.CHI_UNSPECIFIED) a.SetIsAromatic(0) rmol.AddAtom(a) dict_old_new_idx[old_idx] = n n += 1 for b in self.Bonds: rmol.AddBond(dict_old_new_idx[b.GetBeginAtomIdx()], dict_old_new_idx[b.GetEndAtomIdx()], b.GetBondType()) for b in self.bondsenvironments: rmol.AddBond(dict_old_new_idx[b.GetBeginAtomIdx()], dict_old_new_idx[b.GetEndAtomIdx()], b.GetBondType()) rmol.RemoveAtom(0) EmbedMolecule(rmol) drawer = rdMolDraw2D.MolDraw2DSVG(400, 200) drawer.DrawMolecule(rmol) drawer.FinishDrawing() svg = drawer.GetDrawingText() if filename != None: f = open(filename, 'w') f.write(svg) f.close() if ipython: svg = svg.replace('svg:', '') return SVG(svg) else: return None
def _place_between(self, mol: Chem.RWMol, a: int, b: int, aromatic=True): oribond = mol.GetBondBetweenAtoms(a, b) if oribond is None: print('FAIL') return None # fail elif aromatic: bt = Chem.BondType.AROMATIC else: bt = oribond.GetBondType() idx = mol.AddAtom(Chem.Atom(6)) neoatom = mol.GetAtomWithIdx(idx) atom_a = mol.GetAtomWithIdx(a) atom_b = mol.GetAtomWithIdx(b) if aromatic: neoatom.SetIsAromatic(True) atom_a.SetIsAromatic(True) atom_b.SetIsAromatic(True) # prevent constraints neoatom.SetBoolProp('_Novel', True) atom_a.SetBoolProp('_Novel', True) atom_b.SetBoolProp('_Novel', True) # fix position conf = mol.GetConformer() pos_A = conf.GetAtomPosition(a) pos_B = conf.GetAtomPosition(b) x = pos_A.x / 2 + pos_B.x / 2 y = pos_A.y / 2 + pos_B.y / 2 z = pos_A.z / 2 + pos_B.z / 2 conf.SetAtomPosition(idx, Point3D(x, y, z)) # fix bonds mol.RemoveBond(a, b) mol.AddBond(a, idx, bt) mol.AddBond(b, idx, bt)
def convert_dict_to_mols(tot_dict): """ :param tot_dict: :return: """ mol_list = [] for smiles in tot_dict: # Now generate the molecules for that mol = RWMol() atoms = tot_dict[smiles] print(atoms) for atom in atoms: atom = Atom(6) mol.AddAtom(atom) # for i in range(len(atoms)-1): # mol.AddBond(i,i+1) mol = mol.GetMol() AllChem.EmbedMolecule(mol) conf = mol.GetConformer() for i, atom in enumerate(atoms): point_3d = Point3D(atom[0], atom[1], atom[2]) conf.SetAtomPosition(i, point_3d) mol = conf.GetOwningMol() mol.SetProp("_Name", smiles) mol_list.append(mol) return mol_list
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 apply(self, mol: RWMol) -> RWMol: num_atoms = mol.GetNumAtoms() if self.detach: for i, a in enumerate(mol.GetAtoms()): m = a.GetAtomMapNum() if m == self.atom_map2: for bond in a.GetBonds(): mol.RemoveBond(bond.GetBeginAtomIdx(), bond.GetEndAtomIdx()) mol.RemoveAtom(i) num_atoms -= 1 break atom_ind = get_atom_ind(mol, self.atom_map1) b_type = rdchem.BondType.values[self.bond_type] b_stereo = rdchem.BondStereo.values[self.bond_stereo] old_atom = mol.GetAtomWithIdx(atom_ind) if old_atom.HasProp('in_reactant'): self.new_a.SetBoolProp('in_reactant', old_atom.GetBoolProp('in_reactant')) if old_atom.HasProp('mol_id'): self.new_a.SetIntProp('mol_id', old_atom.GetIntProp('mol_id')) mol.AddAtom(self.new_a) new_atom_ind = num_atoms bond_ind = mol.AddBond(atom_ind, new_atom_ind, order=b_type) - 1 new_bond = mol.GetBondWithIdx(bond_ind) new_bond.SetStereo(b_stereo) new_bond.SetBoolProp('is_edited', True) return mol
def decode(v): """Decode a molvector into a molecule :param v: molvector :result rdkit.RWMol: """ chunksize = atom_size + bond_chunk_size nchunks = len(v) // chunksize m = RWMol() bonds = {} for i in range(nchunks): start = i * (atom_size + bond_chunk_size) el, c, h, b1, o1, b2, o2, b3, o3, b4, o4 = v[start:start + chunksize] atom = Atom(el) atom.SetFormalCharge(c) atom.SetNumExplicitHs(h) atom_idx = m.AddAtom(atom) assert atom_idx == i for b, o in ((b1, o1), (b2, o2), (b3, o3), (b4, o4)): if o: to_atom = atom_idx + o bonds[tuple(sorted((atom_idx, to_atom)))] = b for (a1, a2), btype in bonds.items(): try: m.AddBond(a1 % m.GetNumAtoms(), a2 % m.GetNumAtoms(), BondType.values[btype]) except: pass return m
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
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 rd_map_from_ob(mol): from rdkit.Chem import RWMol, Atom, BondType rm = RWMol() for i in range(mol.NumAtoms()): a = mol.GetAtomById(i) ra = Atom(a.GetAtomicNum()) rm.AddAtom(ra) for i in range(mol.NumBonds()): b = mol.GetBondById(i) b.GetBeginAtom().GetId() order = BondType.SINGLE if b.GetBO() == 2: order = BondType.DOUBLE if b.GetBO() == 3: order = BondType.TRIPLE rm.AddBond(b.GetBeginAtom().GetId(), b.GetEndAtom().GetId(),order)#b.GetBondOrder()) return rm
def Write(self, degrees, edges, canonical=True): if set(degrees).issubset(self.allowed): # Define the molecule cp = RWMol() _ = [cp.AddAtom(Atom(self.d2atno[D])) for D in degrees] _ = [cp.AddBond(f, t, BondType.SINGLE) for f, t in edges] # Export as canonical SMILES or a random SMILES if canonical: out = MolToSmiles(cp, canonical=True) else: out = MolToSmiles(cp, canonical=False, doRandom=True) # Carry out replacements for src, dst in self.replacements: out = out.replace(src, dst) return out.upper() else: return None
def _join_atoms(self, combo: Chem.RWMol, anchor_A: int, anchor_B: int, distance: float, linking: bool = True): """ extrapolate positions between. by adding linkers if needed. """ conf = combo.GetConformer() pos_A = conf.GetAtomPosition(anchor_A) pos_B = conf.GetAtomPosition(anchor_B) n_new = int(round(distance / 1.22) - 1) xs = np.linspace(pos_A.x, pos_B.x, n_new + 2)[1:-1] ys = np.linspace(pos_A.y, pos_B.y, n_new + 2)[1:-1] zs = np.linspace(pos_A.z, pos_B.z, n_new + 2)[1:-1] # correcting for ring marker atoms def is_ring_atom(anchor: int) -> bool: atom = combo.GetAtomWithIdx(anchor) if atom.HasProp('_ori_i') and atom.GetIntProp('_ori_i') == -1: return True else: return False if is_ring_atom(anchor_A): distance -= 1.35 + 0.2 # Arbitrary + 0.2 to compensate for the ring not reaching (out of plane). n_new -= 1 xs = xs[1:] ys = ys[1:] zs = zs[1:] if is_ring_atom(anchor_B): distance -= 1.35 + 0.2 # Arbitrary + 0.2 to compensate for the ring not reaching (out of plane). n_new -= 1 xs = xs[:-1] ys = ys[:-1] zs = zs[:-1] # notify that things could be leary. if distance < 0: self.journal.debug( f'Two ring atoms detected to be close. Joining for now.' + ' They will be bonded/fused/spiro afterwards') # check if valid. if distance > self.joining_cutoff: msg = f'Atoms {anchor_A}+{anchor_B} are {distance} Å away. Cutoff is {self.joining_cutoff}.' self.journal.warning(msg) raise ConnectionError(msg) # place new atoms self.journal.debug( f'Molecules will be joined via atoms {anchor_A}+{anchor_B} ({distance} Å) via the addition of {n_new} atoms.' ) previous = anchor_A if linking is False and n_new > 0: self.journal.warning( f'Was going to bond {anchor_A} and {anchor_B} but reconsidered.' ) elif linking is True and n_new <= 0: combo.AddBond(previous, anchor_B, Chem.BondType.SINGLE) new_bond = combo.GetBondBetweenAtoms(previous, anchor_B) BondProvenance.set_bond(new_bond, 'main_novel') elif linking is False and n_new <= 0: combo.AddBond(previous, anchor_B, Chem.BondType.SINGLE) new_bond = combo.GetBondBetweenAtoms(previous, anchor_B) BondProvenance.set_bond(new_bond, 'other_novel') elif linking is True and n_new > 0: for i in range(n_new): # make oxygen the first and last bridging atom. if i == 0 and combo.GetAtomWithIdx( anchor_A).GetSymbol() == 'C': new_atomic = 8 elif i > 2 and i == n_new - 1 and combo.GetAtomWithIdx( anchor_B).GetSymbol() == 'C': new_atomic = 8 else: new_atomic = 6 idx = combo.AddAtom(Chem.Atom(new_atomic)) new = combo.GetAtomWithIdx(idx) new.SetBoolProp('_Novel', True) new.SetIntProp('_ori_i', 999) conf.SetAtomPosition( idx, Point3D(float(xs[i]), float(ys[i]), float(zs[i]))) combo.AddBond(idx, previous, Chem.BondType.SINGLE) new_bond = combo.GetBondBetweenAtoms(idx, previous) BondProvenance.set_bond(new_bond, 'linker') previous = idx combo.AddBond(previous, anchor_B, Chem.BondType.SINGLE) new_bond = combo.GetBondBetweenAtoms(previous, anchor_B) BondProvenance.set_bond(new_bond, 'linker') else: raise ValueError('Impossible') return combo.GetMol()