def check_mol( mol ): match, mList = EmbedLib.MatchPharmacophoreToMol( mol, feat_fact, pcophore) if match: res = [] num_match = len( mList ) for i in range( num_match ): num_feature = len( mList[i] ) for j in range( num_feature ): print mList[i][j].GetAtomIds(), mList[i][j].GetFamily() bounds = rdDistGeom.GetMoleculeBoundsMatrix( mol ) pList = EmbedLib.GetAllPharmacophoreMatches( mList, bounds, pcophore ) #pList = EmbedLib.MatchPharmacophore( mList, bounds, pcophore ) print pList #print raw_input("-----") num_match = len( pList ) print num_match phMatches = [] for i in range( num_match ): num_feature = len( pList[i] ) phMatch = [] for j in range( num_feature ): phMatch.append( pList[i][j].GetAtomIds() ) phMatches.append( phMatch ) for phMatch in phMatches: bm, embeds, nFail = EmbedLib.EmbedPharmacophore( mol, phMatch, pcophore, count=20, silent=1 ) print "-----> embeds num:", len( embeds ) for embed in embeds: AllChem.UFFOPtimizeMolecule( embed ) align_data = rdAliginment.GetAlignmetTransform( bm, bounds ) AllChem.TransformMol( embed, align_data[1] ) res.append( embed ) return res
def _align_molecules(self, molecules: List[Chem.Mol]) -> None: """ Align a list of molecules to a given pharmacophore. Parameters ---------- molecules : list of rdkit.Chem.Mol List of molecules to align. """ self.n_molecules += len(molecules) rdkit_pharmacophore, radii = self.pharmacophore.to_rdkit() apply_radii_to_bounds(radii, rdkit_pharmacophore) fdef = os.path.join(RDConfig.RDDataDir, 'BaseFeatures.fdef') featFactory = ChemicalFeatures.BuildFeatureFactory(fdef) MolScore = namedtuple("MolScore", ["score", "id", "mol"]) for mol in tqdm(molecules): bounds_matrix = rdDistGeom.GetMoleculeBoundsMatrix(mol) can_match, all_matches = EmbedLib.MatchPharmacophoreToMol( mol, featFactory, rdkit_pharmacophore) if can_match: failed, _, matched_mols, _ = EmbedLib.MatchPharmacophore( all_matches, bounds_matrix, rdkit_pharmacophore, useDownsampling=True) if failed: matched_mol = MolScore(0.0, mol.GetProp("_Name"), mol) self.molecules.append(matched_mol) continue else: matched_mol = MolScore(0.0, mol.GetProp("_Name"), mol) self.molecules.append(matched_mol) continue atom_match = [list(x.GetAtomIds()) for x in matched_mols] try: mol_H = Chem.AddHs(mol) _, embeddings, _ = EmbedLib.EmbedPharmacophore( mol_H, atom_match, rdkit_pharmacophore, count=10) except: continue SSDs = transform_embeddings(rdkit_pharmacophore, embeddings, atom_match) if len(SSDs) == 0: matched_mol = MolScore(0.0, mol.GetProp("_Name"), mol) self.molecules.append(matched_mol) continue best_fit_index = min(enumerate(SSDs), key=itemgetter(1))[0] score = 1 / SSDs[best_fit_index] matched_mol = MolScore(score, mol.GetProp("_Name"), embeddings[best_fit_index]) self.molecules.append(matched_mol)
def check_mol(mol): res = [] mol.RemoveAllConformers() match, mList = EmbedLib.MatchPharmacophoreToMol(mol, feat_fact, pcophore) #mList = [ m for m in Set( mList ) ] if match: num_match = len(mList) for i in range(num_match): num_feature = len(mList[i]) for j in range(num_feature): print mList[i][j].GetAtomIds(), mList[i][j].GetFamily() bounds = rdDistGeom.GetMoleculeBoundsMatrix(mol) pList = EmbedLib.GetAllPharmacophoreMatches(mList, bounds, pcophore) num_match = len(pList) phMatches = [] for i in range(num_match): num_feature = len(pList[i]) phMatch = [] for j in range(num_feature): phMatch.append(pList[i][j].GetAtomIds()) phMatches.append(phMatch) for phMatch in phMatches: bm, embeds, nFail = EmbedLib.EmbedPharmacophore(mol, phMatch, pcophore, count=5, silent=1) print "-----> embeds num:", len(embeds) for embed in embeds: AllChem.UFFOptimizeMolecule(embed) feats = feat_fact.GetFeaturesForMol(embed) feats_dict = GetFeatsPerAtoms(feats) match_feats = [feats_dict[atomid] for atomid in phMatch] pro_mat = [list(feat.GetPos()) for feat in match_feats] align_data = rdAlignment.GetAlignmentTransform( ref_mat, pro_mat, maxIterations=200) AllChem.TransformMol(embed, align_data[1]) print align_data[0] print align_data[1] res.append(embed) else: print "no hits" return res
def _align_molecule(mol, pharmacophore, matches, featFactory, sort=False): """ Align a molecule to a given pharmacophore. Uses rdkit alignment algorithm Parameters ---------- mol : rdkit.Chem.Mol Molecule to align. matches : list If a moleculed is matched to the pharmacophore it will be appended to this list. pharmacophore : rdkit.Chem.Pharm3D.Pharmacophore An rdkit pharmacophore featFactory : rdkit.Chem.rdMolChemicalFeatures.MolChemicalFeatureFactory The feature factory. sort : bool, default=False Whether to sort the list with the matches """ bounds_matrix = rdDistGeom.GetMoleculeBoundsMatrix(mol) # Check if the molecule features can match with the pharmacophore. can_match, all_matches = EmbedLib.MatchPharmacophoreToMol(mol, featFactory, pharmacophore) # all_matches is a list of tuples where each tuple contains the chemical features if can_match: # Match the molecule to the pharmacophore without aligning it failed, bounds_matrix_matched, matched_mols, match_details = EmbedLib.MatchPharmacophore(all_matches, bounds_matrix, pharmacophore, useDownsampling=True) if failed: return else: return atom_match = [list(x.GetAtomIds()) for x in matched_mols] try: mol_H = Chem.AddHs(mol) # Embed molecule onto the pharmacophore # embeddings is a list of molecules with a single conformer b_matrix, embeddings, num_fail = EmbedLib.EmbedPharmacophore(mol_H, atom_match, pharmacophore, count=10) except: return # Align embeddings to the pharmacophore SSDs = transform_embeddings(pharmacophore, embeddings, atom_match) if len(SSDs) == 0: return best_fit_index = min(enumerate(SSDs), key=itemgetter(1))[0] try: mol_id = mol.GetProp("_Name") except: mol_id = None matched_mol = Match(SSDs[best_fit_index], mol_id, embeddings[best_fit_index]) if sort: # Append to list in ordered manner try: # Case when a molecule is repeated. It will throw an error since bisect # cannot compare molecules. bisect.insort(matches, matched_mol) except: return else: matches.append(matched_mol)