def tree_frags_from_mol(mol, prioritization_rules=None): """Generate a scaffold tree from a single molecule without using networkx Parameters ---------- mol: rdkit molecule for processing prioritization_rules: rules for prioritizing parent scaffolds (optional, default: None) Returns ------- parents: an ordered list of rdkit Mols representing a scaffold tree """ rdlogger.setLevel(4) scaffold = Scaffold(get_murcko_scaffold(mol)) rdmolops.RemoveStereochemistry(scaffold.mol) parents = [scaffold] fragmenter = MurckoRingFragmenter(use_scheme_4=True) rules = prioritization_rules if prioritization_rules else original_ruleset def _next_scaffold(child): next_parents = [p for p in fragmenter.fragment(child) if p] if not next_parents: return next_parent = rules(child, next_parents) parents.append(next_parent) if next_parent.rings.count > 1: _next_scaffold(next_parent) _next_scaffold(scaffold) rdlogger.setLevel(3) return [p.mol for p in parents]
def remove_am(self, smi_am): mol = Chem.MolFromSmiles(smi_am) for atom in mol.GetAtoms(): # get rid of clearing atom mapping function, so the output will have atom maps atom.ClearProp('molAtomMapNumber') # since wln doesn't have stereochemistry, we remove stereochemistry here rdmolops.RemoveStereochemistry(mol) return Chem.MolToSmiles(mol)
def _construct(self, molecules, ring_cutoff=10, progress=False, annotate=True): """Private method for graph construction, called by constructors. Parameters ---------- molecules : iterable An iterable of rdkit molecules for processing ring_cutoff : int, optional Ignore molecules with more than the specified number of rings to avoid extended processing times. The default is 10. annotate : bool, optional If True write an annotated murcko scaffold SMILES string to each molecule edge (molecule --> scaffold). The default is True. progress : bool If True show a progress bar monitoring progress. The default is False """ rdlogger.setLevel(4) # Suppress the RDKit logs progress = progress is False desc = self.__class__.__name__ for molecule in tqdm(molecules, disable=progress, desc=desc, miniters=1, dynamic_ncols=True): if molecule is None: # logged in suppliers continue init_molecule_name(molecule) if CalcNumRings(molecule) > ring_cutoff: name = molecule.GetProp('_Name') logger.warning( f'Molecule {name} filtered (> {ring_cutoff} rings)') continue rdmolops.RemoveStereochemistry(molecule) scaffold = Scaffold(get_murcko_scaffold(molecule)) if scaffold: # Checks that a scaffold has at least 1 atom annotation = None if annotate: annotation = get_annotated_murcko_scaffold( molecule, scaffold.mol, False) self.add_scaffold_node(scaffold) self.add_molecule_node(molecule) self.add_molecule_edge(molecule, scaffold, annotation=annotation) if scaffold.rings.count > 1: self._recursive_constructor(scaffold) else: name = molecule.GetProp('_Name') logger.warning(f'No top level scaffold for molecule {name}') rdlogger.setLevel(3) # Enable the RDKit logs
def get_all_murcko_fragments(mol, break_fused_rings=True): """ Get all possible murcko fragments from a molecule through recursive removal of peripheral rings. Parameters ---------- mol : rdkit.Chem.rdchem.Mol break_fused_rings : bool, optional If True dissect fused rings. The default is True. Returns ------- list A list of Murcko fragments for the input molecule. Examples -------- Generating Murcko fragments: >>> from rdkit import Chem >>> smiles = 'Cc1[nH]cnc1Cn1cccc(-c2ccccc2O)c1=O' >>> molecule = Chem.MolFromSmiles(smiles) >>> frags = get_all_murcko_fragments(molecule) """ rdlogger.setLevel(4) if break_fused_rings: fragmenter = MurckoRingFragmenter() else: fragmenter = MurckoRingSystemFragmenter() mol = get_murcko_scaffold(mol) rdmolops.RemoveStereochemistry(mol) scaffold = Scaffold(mol) parents = {scaffold} def recursive_generation(child): for parent in fragmenter.fragment(child): if parent in parents: continue parents.add(parent) recursive_generation(parent) recursive_generation(scaffold) rdlogger.setLevel(3) return [f.mol for f in parents]
def tree_frags_from_mol(mol, prioritization_rules=None): """Generate a scaffold tree from a single molecule without using networkx. Parameters ---------- mol: rdkit.Chem.rdchem.Mol rdkit molecule for processing. prioritization_rules : ScaffoldRuleSet, optional rules for prioritizing parent scaffolds. If not supplied the original rules are used. The default is None. Returns ------- parents An ordered list of rdkit Mols representing a scaffold tree. Examples -------- Generating scaffold tree fragments: >>> from rdkit import Chem >>> smiles = 'Cc1[nH]cnc1Cn1cccc(-c2ccccc2O)c1=O' >>> molecule = Chem.MolFromSmiles(smiles) >>> frags = tree_frags_from_mol(molecule) """ scaffold = Scaffold(get_murcko_scaffold(mol)) rdmolops.RemoveStereochemistry(scaffold.mol) parents = [scaffold] fragmenter = MurckoRingFragmenter(use_scheme_4=True) rules = prioritization_rules if prioritization_rules else original_ruleset def _next_scaffold(child): next_parents = [p for p in fragmenter.fragment(child) if p] if not next_parents: return next_parent = rules(child, next_parents) parents.append(next_parent) if next_parent.rings.count > 1: _next_scaffold(next_parent) _next_scaffold(scaffold) return [p.mol for p in parents]
def render(self, mode="human"): """ Generates a 3D rendering of the current molecule in the environment. Parameters ------- mode: string Just a way of indicating whether the rendering is mainly for humans vs machines in OpenAI """ if self.check_valency() and self.check_chemical_validity(): if not self.pymol_window_flag: self.start_pymol() molecule = self.mol self.mol.UpdatePropertyCache( strict=False) # Update valence information FastFindRings( self.mol ) # Quick for finding out if an atom is in a ring, use Chem.GetSymmSSR() if more reliability is desired Chem.AddHs( molecule) # Add explicit hydrogen atoms for rendering purposes # remove stereochemistry information rdmolops.RemoveStereochemistry(molecule) # Generate 3D structure AllChem.EmbedMolecule(molecule) AllChem.MMFFOptimizeMolecule(molecule) v = MolViewer() v.ShowMol(molecule) v.GetPNG(h=400) # Save the rendering in pse format pymol.cmd.save("./pymol_renderings/" + Chem.MolToSmiles(molecule) + ".pse", format="pse") Chem.RemoveHs(molecule) # Remove explicit hydrogen else: print( "The molecule is not chemically valid, and rendering has been terminated." )
def _preprocess_scaffold(self, scaffold_rdmol, init_args): """Preprocess a scaffold before initialization. When subclassing a user is able to customize/extend preprocessing options to suit their needs. The default method has the option to; remove stereochemistry, remove specific isotopes, keep the largest disconnected fragment and discharge and deradicalize a molecule. The options are controlled by the init_args dictionary. This argument dictionary is formed by the constructors. Note that the stereochemistry argument is specific to the hierarchy generating method so isn't specified in the constructors. Parameters ---------- scaffold_rdmol : rdkit.Chem.rdchem.Mol rdkit molecule of scaffold to be preprocessed init_args : dict A dictionary containing arguments for scaffold initialization and preprocessing. Returns ------- scaffold_rdmol : rdkit.Chem.rdchem.Mol rdkit molecule containing preprocessed scaffold. """ if init_args.get('remove_stereo', True) is True: rdmolops.RemoveStereochemistry(scaffold_rdmol) if init_args.get('flatten_isotopes', False) is True: flatten_isotopes(scaffold_rdmol) if init_args.get('keep_largest', False) is True: scaffold_rdmol = keep_largest_fragment(scaffold_rdmol) if init_args.get('discharge', False) is True: scaffold_rdmol = discharge_and_deradicalize(scaffold_rdmol) return scaffold_rdmol
def get_all_murcko_fragments(mol, break_fused_rings=True): """Get all possible murcko fragments from a molecule through recursive removal of peripheral rings Parameters ---------- mol: rdkit molecule to be processed break_fused_rings (bool): If True dissect fused rings (default: True) Returns ------- A list of rdkit Mols representing all possible murcko fragments """ rdlogger.setLevel(4) if break_fused_rings: fragmenter = MurckoRingFragmenter() else: fragmenter = MurckoRingSystemFragmenter() mol = get_murcko_scaffold(mol) rdmolops.RemoveStereochemistry(mol) scaffold = Scaffold(mol) parents = {scaffold} def recursive_generation(child): for parent in fragmenter.fragment(child): if parent in parents: continue parents.add(parent) recursive_generation(parent) recursive_generation(scaffold) rdlogger.setLevel(3) return [f.mol for f in parents]
chembl[i][0]) == '': delete = True if delete == True: i = i + 1 continue chembl_help.append(list(chembl[i])) i = i + 1 #pprint (chembl_help) #Chembl standardize for lig in range(0, len(chembl_help)): #print ('Now I do this from Chembl: ' + chembl_help[lig][0]) mol = inchi.MolFromInchi(chembl_help[lig][0], sanitize=False) try: rdmolops.RemoveStereochemistry(mol) except Exception: print("Not able to remove stereochemistry. Chembl.") try: mol = standardise.run(mol) except standardise.StandardiseException as e: logging.warn(e.message) try: mol = s.standardize(mol) except Exception: print("Not able to standardize. Chembl.") try: mol = s.tautomer_parent(mol, skip_standardize=True) except Exception: print("Not able to make tautomer parent. Chembl.") mol = s.stereo_parent(mol, skip_standardize=True)