def _GetSMILES(mol, idxlist): tmol = mol.__copy__() #(t)emporary tmol = RWMol(tmol) for AtomIdx in xrange(tmol.GetNumAtoms() - 1, -1, -1): if AtomIdx not in idxlist: tmol.RemoveAtom(AtomIdx) return Chem.MolToSmiles(tmol)
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 join_overclose(self, mol: Chem.RWMol, to_check, cutoff=2.2): # was 1.8 """ Cutoff is adapted to element. :param mol: :param to_check: list of atoms indices that need joining (but not to each other) :param cutoff: CC bond :return: """ pt = Chem.GetPeriodicTable() dm = Chem.Get3DDistanceMatrix(mol) for i in to_check: atom_i = mol.GetAtomWithIdx(i) for j, atom_j in enumerate(mol.GetAtoms()): # calculate cutoff if not C-C if atom_i.GetSymbol() == '*' or atom_j.GetSymbol() == '*': ij_cutoff = cutoff elif atom_i.GetSymbol() == 'C' and atom_j.GetSymbol() == 'C': ij_cutoff = cutoff else: ij_cutoff = cutoff - 1.36 + sum([pt.GetRcovalent(atom.GetAtomicNum()) for atom in (atom_i, atom_j)]) # determine if to join if i == j or j in to_check: continue elif dm[i, j] > ij_cutoff: continue else: self._add_bond_if_possible(mol, atom_i, atom_j)
def remove_exocyclic_attachments(mol): """ Remove exocyclic and exolinker attachments from a molecule. Parameters ---------- mol : rdkit.Chem.rdchem.Mol Returns ------- rdkit.Chem.rdchem.Mol Molecule with exocyclic/exolinker attachments removed. """ edit = RWMol(mol) remove_atoms = set() for atom in edit.GetAtoms(): degree = atom.GetDegree() if degree == 1: bond = atom.GetBonds()[0] if bond.GetBondTypeAsDouble() == 2.0: nbr = bond.GetOtherAtom(atom) hcount = nbr.GetTotalNumHs() nbr.SetNumExplicitHs(hcount + 2) nbr.SetNoImplicit(True) remove_atoms.add(atom.GetIdx()) for aix in sorted(remove_atoms, reverse=True): edit.RemoveAtom(aix) rdmolops.AssignRadicals(edit) GetSymmSSSR(edit) return edit.GetMol()
def _delete_marked(self, mol: Chem.RWMol): morituri = list( reversed( mol.GetAtomsMatchingQuery( Chem.rdqueries.HasPropQueryAtom('DELETE')))) for atom in morituri: mol.RemoveAtom(atom.GetIdx())
def get_ring_removals(smi): rw_mol = RWMol(Chem.MolFromSmiles(smi)) rings = rw_mol.GetRingInfo().AtomRings() out_mols = {} for ring in rings: new_mol = Chem.MolFromSmiles(smi) for atom in ring: new_mol.GetAtomWithIdx(atom).SetAtomicNum(0) Chem.DeleteSubstructs(new_mol, Chem.MolFromSmarts("[#0]")) Chem.GetMolFrags(new_mol) out_mols[Chem.MolToSmiles(new_mol, isomericSmiles=True)] = ring return out_mols
def _prevent_two_bonds_on_dummy(self, mol: Chem.RWMol): for atom in mol.GetAtoms(): if atom.GetSymbol() != '*': pass elif len(atom.GetNeighbors()) <= 1: pass elif len(atom.GetNeighbors()) >= 2: neighs = atom.GetNeighbors() for second in neighs[1:]: self._absorb(mol, atom.GetIdx(), second.GetIdx()) mol.RemoveAtom(second.GetIdx()) self._prevent_two_bonds_on_dummy(mol) break
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 _prevent_bridge_ring(self, mol: Chem.RWMol, examplar: Tuple[int]): ## This is really # examplar is ring ringatoms = self._get_ring_info(mol) #GetRingInfo().AtomRings() ringatoms = [ring for ring in ringatoms if set(ring).intersection(examplar)] ring_idx = list(range(len(ringatoms))) shared_count = {} for ra, rb in itertools.combinations(ring_idx, r=2): shared_count[(ra, rb)] = len(set(ringatoms[ra]).intersection(set(ringatoms[rb]))) if len(shared_count) == 0: return mol ra, rb = list(shared_count.keys())[0] shared = list(set(ringatoms[ra]).intersection(ringatoms[rb])) pairs = [(a, b) for a, b in itertools.combinations(shared, r=2) if mol.GetBondBetweenAtoms(a, b) is not None] c = Counter([i for pair in pairs for i in pair]) ring_A, ring_B = ringatoms[ra], ringatoms[rb] small, big = sorted([ring_A, ring_B], key=lambda ring: len(ring)) inners = [i for i in c if c[i] > 1] x = list(set(shared).difference(inners)) if len(x) != 2: log.critical(f'This is impossible. {ringatoms} share {shared} with {inners} in the inside and {x} on the edge?') return mol a, b = x if len(big) > 6: log.warning(f'Removing {len(inners)} bridging atoms and replacing with fused ring') # bond the vertices bt = Chem.BondType.SINGLE # ??? if mol.GetBondBetweenAtoms(a, b) is None: mol.AddBond(a, b, bt) else: log.warning('This is really odd! Why is there a bond already??') # remove the middle atoms. for i in sorted(inners, reverse=True): mol.RemoveAtom(i) else: log.warning(f'Shriking the smaller ring to change from bridged to fused.') # get the neighbour in the small atom to a vertex. neighs = [neigh for neigh in mol.GetAtomWithIdx(a).GetNeighbors() if neigh.GetIdx() not in shared and neigh.GetIdx() in small] neigh = sorted(neighs, key=lambda atom: atom.GetSymbol() != 'C')[0] bt = mol.GetBondBetweenAtoms(a, neigh.GetIdx()).GetBondType() mol.RemoveBond(a, neigh.GetIdx()) new_neigh = [neigh for neigh in mol.GetAtomWithIdx(a).GetNeighbors() if neigh.GetIdx() in shared][0] mol.AddBond(neigh.GetIdx(), new_neigh.GetIdx(), bt) neigh.SetBoolProp('_Novel', True) new_neigh.SetBoolProp('_Novel', True) mol.GetAtomWithIdx(a).SetBoolProp('_Novel', True) return mol
def join_rings(self, mol: Chem.RWMol, cutoff=1.8): # special case: x0749. bond between two rings # namely bonds are added to non-ring atoms. so in the case of bonded rings this is required. rings = self._get_ring_info(mol) dm = Chem.Get3DDistanceMatrix(mol) for ringA, ringB in itertools.combinations(rings, 2): if not self._are_rings_bonded(mol, ringA, ringB): mini = np.take(dm, ringA, 0) mini = np.take(mini, ringB, 1) d = np.nanmin(mini) if d < cutoff: p = np.where(mini == d) f = ringA[int(p[0][0])] s = ringB[int(p[1][0])] #mol.AddBond(f, s, Chem.BondType.SINGLE) self._add_bond_if_possible(mol, mol.GetAtomWithIdx(f), mol.GetAtomWithIdx(s))
def step(self, action_ob): """ Used to perform actions on the current molecule in the environment :param action_ob: The action to be taken on the current molecule :type action_ob: Action :return: Information about the resulting molecule :rtype: Observation """ action = action_ob.action_c.lower() position = action_ob.pos mol = action_ob.mol query = action_ob.query if (isinstance(action_ob.query, np.ndarray)): self._queryStep(action, query) else: self._simpleStep(action, position, mol) self.current_molecule = RWMol(Chem.MolFromSmiles(self._listToSmiles())) self.datacapture.processing() self.obs.update(self.current_molecule) self.mol_Steps.append(self.current_molecule) legend = str(len(self.mol_Steps)) + ". " + Chem.MolToSmiles( self.current_molecule) self.smiles.append(legend) if os.environ.get('DISPLAY', '') != '': img = Draw.MolToImage(self.current_molecule, size=(300, 300), kekulize=True, wedgeBonds=True) self.gui.update(img) return self.obs
def __init__(self): """ This is the constructor """ super().__init__() default_smile = 'C' self.current_molecule = RWMol(Chem.MolFromSmiles(default_smile)) self.obs = Observation(self.current_molecule) self.molecule_list = [Mol_Feature(default_smile)] self.datacapture = Datacapture(self.current_molecule) self.datacapture.processing() self.mol_Steps = [self.current_molecule] legend = str(len(self.mol_Steps)) + ". " + Chem.MolToSmiles( self.current_molecule) self.smiles = [legend] if os.environ.get('DISPLAY', '') != '': #check if there is a display available self.root = Toplevel() self.gui = Render(self.root) img = Draw.MolToImage(self.current_molecule, size=(300, 300), kekulize=True, wedgeBonds=True) self.gui.update(img) else: print('No display found!')
def _prevent_two_bonds_on_dummy(self, mol: Chem.RWMol): """ The case '*(C)C' is seen legitimately in some warheads... but in most cases these are not. :param mol: :return: """ for atom in mol.GetAtoms(): if atom.GetSymbol() != '*': pass elif len(atom.GetNeighbors()) <= 1: pass elif len(atom.GetNeighbors()) >= 2: self.journal.info( f'Dummy atom (idx={atom.GetIdx()}) has {len(atom.GetNeighbors())} bonds!' ) neighs = atom.GetNeighbors() first = neighs[0] for second in neighs[1:]: rejected = second.GetIdx( ) # that will be absorbed (deleted) keeper = first.GetIdx() # that absorbs (kept) self._copy_bonding(mol, keeper, rejected) self._mark_for_deletion(mol, rejected) self._delete_marked(mol) return self._prevent_two_bonds_on_dummy(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 _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 complement_reaction(rxn_template): if rxn_template.GetNumProductTemplates() != 1: print("[ERROR] A reaction template has only one product template.") sys.exit(1) pro = rxn_template.GetProductTemplate(0) rw_pro = RWMol(pro) amaps_pro = {a.GetAtomMapNum() for a in pro.GetAtoms()} amaps_rcts = {a.GetAtomMapNum() for rct in rxn_template.GetReactants() for a in rct.GetAtoms()} amaps_not_in_rcts = amaps_pro.intersection(amaps_rcts) for amap in amaps_not_in_rcts: aidx = [a.GetIdx() for a in rw_pro.GetAtoms() if a.GetAtomMapNum() == amap][0] rw_pro.RemoveAtom(aidx) m = rw_pro.GetMol() if '.' in Chem.MolToSmarts(m): return if (m.GetNumAtoms() == 0) or (m.GetNumAtoms() == 1 and m.GetAtomWithIdx(0).GetSymbol() in {"*", None}): return rxn_template.AddReactantTemplate(m)
def fragment(self, scaffold): """Fragment a scaffold into its next set of Murcko fragments. Parameters ---------- scaffold : scaffoldgraph.core.Scaffold Child scaffold to be fragmented. Returns ------- list A list of parent scaffolds representing the next hierarchy. """ parents = [] rings = scaffold.ring_systems # ring system information info = scaffold.rings.info if rings.count == 1: return [] for rix, ring in enumerate(rings): edit = RWMol(scaffold.mol) remove_atoms = set() for index, atom in zip(ring.aix, ring.atoms): if info.NumAtomRings(index) == 1 or any( [not b.IsInRing() for b in atom.GetBonds()]): if atom.GetDegree() > 2: # Evoke linker collection collect_linker_atoms(edit.GetAtomWithIdx(index), remove_atoms) else: remove_atoms.add(index) else: remove_atoms.add(index) for aix in sorted(remove_atoms, reverse=True): edit.RemoveAtom(aix) for parent in get_scaffold_frags(edit): if parent.ring_systems.count == len(rings) - 1: parent.removed_ring_idx = rix parents.append(parent) return parents
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 fragment(self, scaffold): """Fragment a scaffold into its next set of murcko fragments. This fragmenter will not dissect fused ring systems. Parameters ---------- scaffold (sg.core.Scaffold): scaffold to be fragmented. Returns ------- parents (list): a list of the next scaffold parents. """ parents = [] rings = scaffold.ring_systems # ring system information info = scaffold.rings.info if rings.count == 1: return [] for rix, ring in enumerate(rings): edit = RWMol(scaffold.mol) remove_atoms = set() for index, atom in zip(ring.aix, ring.atoms): if info.NumAtomRings(index) == 1: if atom.GetDegree() > 2: # Evoke linker collection collect_linker_atoms(edit.GetAtomWithIdx(index), remove_atoms) else: remove_atoms.add(index) else: remove_atoms.add(index) for aix in sorted(remove_atoms, reverse=True): edit.RemoveAtom(aix) for parent in get_scaffold_frags(edit): if parent.ring_systems.count == len(rings) - 1: parent.removed_ring_idx = rix parents.append(parent) return parents
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 apply(self, mol: RWMol) -> RWMol: atom_ind = get_atom_ind(mol, self.atom_map1) atom = mol.GetAtomWithIdx(atom_ind) atom.SetFormalCharge(self.formal_charge) a_chiral = rdchem.ChiralType.values[self.chiral_tag] atom.SetChiralTag(a_chiral) atom.SetNumExplicitHs(self.num_explicit_hs) atom.SetIsAromatic(self.is_aromatic) atom.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() 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 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 apply(self, mol: RWMol) -> RWMol: atom1 = get_atom_ind(mol, self.atom_map1) atom2 = get_atom_ind(mol, self.atom_map2) if self.bond_type is None: # delete bond bond = mol.GetBondBetweenAtoms(atom1, atom2) if bond is not None: mol.RemoveBond(atom1, atom2) else: b_type = rdchem.BondType.values[self.bond_type] b_stereo = rdchem.BondStereo.values[self.bond_stereo] bond = mol.GetBondBetweenAtoms(atom1, atom2) if bond is None: # add new bond bond_ind = mol.AddBond(atom1, atom2, order=b_type) - 1 bond = mol.GetBondWithIdx(bond_ind) else: # change an existing bond bond.SetBondType(b_type) bond.SetStereo(b_stereo) bond.SetBoolProp('is_edited', True) if b_type == BondType.AROMATIC: bond.SetIsAromatic(True) mol.GetAtomWithIdx(atom1).SetIsAromatic(True) mol.GetAtomWithIdx(atom2).SetIsAromatic(True) return mol
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 _restore_original_bonding(self, mol: Chem.RWMol, rings) -> None: to_be_waited_for = [] for ring in rings: for i in range(len(ring['elements'])): d = self._get_expansion_for_atom(ring, i) new_i = self._get_new_index(mol, d['ori_i'], search_collapsed=False) for old_neigh, bond in zip(d['neighbor'], d['bond']): bt = getattr(Chem.BondType, bond) try: new_neigh = self._get_new_index(mol, old_neigh, search_collapsed=False) present_bond = mol.GetBondBetweenAtoms(new_i, new_neigh) if present_bond is None: mol.AddBond(new_i, new_neigh, bt) elif present_bond.GetBondType().name != bond: if self._debug_draw: print( f'bond between {new_i} {new_neigh} exists already (has {present_bond.GetBondType().name} expected {bt})') present_bond.SetBondType(bt) else: if self._debug_draw: print(f'bond between {new_i} {new_neigh} exists already ' + \ f'(has {present_bond.GetBondType().name} expected {bt})') pass except ValueError: if self._debug_draw: print(f"The neighbour {old_neigh} of {d['ori_i']} with {bt} does not yet exist") to_be_waited_for.append((new_i, old_neigh, bt)) for new_i, old_neigh, bt in to_be_waited_for: try: new_neigh = self._get_new_index(mol, old_neigh, name_restriction=mol.GetAtomWithIdx(new_i).GetProp('_ori_name')) if self._debug_draw: print(f'{old_neigh} was missing, but has appeared since as {new_neigh}') if not mol.GetBondBetweenAtoms(new_i, new_neigh): mol.AddBond(new_i, new_neigh, bt) except (KeyError, ValueError) as err: warn(str(err))
def test_step(self): #test add-back smile = "CC" smile = Chem.CanonSmiles(smile) m = Chem.CanonSmiles(self.env._listToSmiles()) self.assertEqual(m, smile) mols = [] legends = [] mols.append(RWMol(Chem.MolFromSmiles("C"))) legends.append("1. C") mols.append(RWMol(Chem.MolFromSmiles("CC"))) legends.append("2. CC") #test add-front self.action.setAction("add", pos="front", mol="C1=CC=CC=C1") self.env.step(self.action) smile = "CCC1=CC=CC=C1" smile = Chem.CanonSmiles(smile) m = Chem.CanonSmiles(self.env._listToSmiles()) self.assertEqual(m, smile) mols.append(RWMol(Chem.MolFromSmiles("CCC1=CC=CC=C1"))) l = "3. " + self.env._listToSmiles() legends.append(l) #test remove-back self.action.setAction("remove", pos="back", mol="C") self.env.step(self.action) smile = "C1=CC=CC=C1C" smile = Chem.CanonSmiles(smile) m = Chem.CanonSmiles(self.env._listToSmiles()) self.assertEqual(m, smile) mols.append(RWMol(Chem.MolFromSmiles("CC1=CC=CC=C1"))) l = "3. " + self.env._listToSmiles() legends.append(l) #test remove-front self.action.setAction("remove", pos="front", mol="C1=CC=CC=C1") self.env.step(self.action) smile = "C" smile = Chem.CanonSmiles(smile) m = Chem.CanonSmiles(self.env._listToSmiles()) self.assertEqual(m, smile) mols.append(RWMol(Chem.MolFromSmiles("C"))) l = "3. " + self.env._listToSmiles() legends.append(l) #test current molecule mol = self.env.current_molecule self.action.setAction("add", pos="front", mol="CC") self.env.step(self.action) self.assertNotEqual(self.env.current_molecule, mol) mols.append(RWMol(Chem.MolFromSmiles("CCC"))) l = "3. " + self.env._listToSmiles() legends.append(l)
def seed(self, Smiles): """ Resets the environment to a specified molecule :param Smiles: Smiles string for the molecule :type Smiles: string """ self.current_molecule = RWMol(Chem.MolFromSmiles(Smiles)) self.molecule_list = [Mol_Feature(Smiles)] self.obs = Observation(self.current_molecule) self.mol_Steps = [self.current_molecule] legend = str(len(self.mol_Steps)) + ". " + Chem.MolToSmiles( self.current_molecule) self.smiles.append(legend) self.datacapture = Datacapture(self.current_molecule) self.datacapture.processing()
def _minimize_rings(mol): """Private: Minimize rings in a scaffold. In this process, all remaining vertices/atoms of degree two are removed by performing an edge merging operation. The only exception being when both vertices neighbours are connected (i.e. we have a triangle), when edge merging would lead to the loss of a cycle. The result is a minimum cycle topological representation of the original molecule. This function is used in the computation of ring topology scaffolds (Oprea). If a ring contains a non-carbon atom, this atom is maintained. Neighbouring ring atoms which are of the same type are merged together into a single atom of the corresponding type. Parameters ---------- mol : rdkit.Chem.rdchem.Mol Returns ------- rdkit.Chem.rdchem.RWMol Minimum cycle topological graph. """ edit = RWMol(mol) remove_atoms = set() for atom in edit.GetAtoms(): if atom.GetDegree() == 2: n1, n2 = atom.GetNeighbors() n1_idx, n2_idx = n1.GetIdx(), n2.GetIdx() connected = edit.GetBondBetweenAtoms(n1_idx, n2_idx) if not connected and (n1.GetAtomicNum() == atom.GetAtomicNum() or n2.GetAtomicNum() == atom.GetAtomicNum()): a_idx = atom.GetIdx() edit.RemoveBond(n1_idx, a_idx) edit.RemoveBond(n2_idx, a_idx) edit.AddBond(n1_idx, n2_idx, BondType.SINGLE) remove_atoms.add(a_idx) for a_idx in sorted(remove_atoms, reverse=True): edit.RemoveAtom(a_idx) return edit