/// <summary> /// Performs the pharmacophore matching. /// </summary> /// <param name="atomContainer">The target molecule. Must have 3D coordinates</param> /// <param name="initializeTarget">If <see langword="true"/>, the target molecule specified in the /// first argument will be analyzed to identify matching pharmacophore groups. If <see langword="false"/> /// this is not performed. The latter case is only useful when dealing with conformers /// since for a given molecule, all conformers will have the same pharmacophore groups /// and only the constraints will change from one conformer to another.</param> /// <returns><see langword="true"/> is the target molecule contains the query pharmacophore</returns> /// <exception cref="CDKException"> /// if the query pharmacophore was not set or the query is invalid or if the molecule /// does not have 3D coordinates</exception> public bool Matches(IAtomContainer atomContainer, bool initializeTarget) { if (!GeometryUtil.Has3DCoordinates(atomContainer)) { throw new CDKException("Molecule must have 3D coordinates"); } if (pharmacophoreQuery == null) { throw new CDKException("Must set the query pharmacophore before matching"); } if (!CheckQuery(pharmacophoreQuery)) { throw new CDKException("A problem in the query. Make sure all pharmacophore groups of the same symbol have the same same SMARTS"); } var title = atomContainer.Title; if (initializeTarget) { pharmacophoreMolecule = GetPharmacophoreMolecule(atomContainer); } else { // even though the atoms comprising the pcore groups are // constant, their coords will differ, so we need to make // sure we get the latest set of effective coordinates foreach (var iAtom in pharmacophoreMolecule.Atoms) { var patom = PharmacophoreAtom.Get(iAtom); var tmpList = new List <int>(); foreach (var idx in patom.GetMatchingAtoms()) { tmpList.Add(idx); } var coords = GetEffectiveCoordinates(atomContainer, tmpList); patom.Point3D = coords; } } if (pharmacophoreMolecule.Atoms.Count < pharmacophoreQuery.Atoms.Count) { Debug.WriteLine($"Target [{title}] did not match the query SMARTS. Skipping constraints"); return(false); } mappings = Pattern.FindSubstructure(pharmacophoreQuery).MatchAll(pharmacophoreMolecule); // XXX: doing one search then discarding return(mappings.AtLeast(1)); }
/// <summary> /// Checks whether this query atom matches a target atom. /// <para> /// Currently a query pharmacophore atom will match a target pharmacophore group if the /// symbols of the two groups match. This is based on the assumption that /// pharmacophore groups with the same symbol will have the same SMARTS /// pattern. /// </para> /// </summary> /// <param name="atom">A target pharmacophore group</param> /// <returns>true if the current query group has the same symbol as the target group</returns> public bool Matches(IAtom atom) { var patom = PharmacophoreAtom.Get(atom); return(patom.Symbol.Equals(Symbol, StringComparison.Ordinal)); }
/// <summary> /// Convert the input into a pcore molecule. /// </summary> /// <param name="input">the compound being converted from</param> /// <returns>pcore molecule </returns> /// <exception cref="CDKException">match failed</exception> private IAtomContainer GetPharmacophoreMolecule(IAtomContainer input) { // XXX: prepare query, to be moved PrepareInput(input); var pharmacophoreMolecule = input.Builder.NewAtomContainer(); var matched = new HashSet <string>(); var uniqueAtoms = new LinkedHashSet <PharmacophoreAtom>(); Debug.WriteLine($"Converting [{input.Title}] to a pcore molecule"); // lets loop over each pcore query atom foreach (var atom in pharmacophoreQuery.Atoms) { var qatom = (PharmacophoreQueryAtom)atom; var smarts = qatom.Smarts; // a pcore query might have multiple instances of a given pcore atom (say // 2 hydrophobic groups separated by X unit). In such a case we want to find // the atoms matching the pgroup SMARTS just once, rather than redoing the // matching for each instance of the pcore query atom. if (!matched.Add(qatom.Symbol)) { continue; } // see if the smarts for this pcore query atom gets any matches // in our query molecule. If so, then collect each set of // matching atoms and for each set make a new pcore atom and // add it to the pcore atom container object int count = 0; foreach (var query in qatom.CompiledSmarts) { // create the lazy mappings iterator var mappings = query.MatchAll(input).GetUniqueAtoms(); foreach (var mapping in mappings) { uniqueAtoms.Add(NewPCoreAtom(input, qatom, smarts, mapping)); count++; } } Debug.WriteLine($"\tFound {count} unique matches for {smarts}"); } pharmacophoreMolecule.SetAtoms(uniqueAtoms.ToArray()); // now that we have added all the pcore atoms to the container // we need to join all atoms with pcore bonds (i.e. distance constraints) if (HasDistanceConstraints(pharmacophoreQuery)) { var npatom = pharmacophoreMolecule.Atoms.Count; for (int i = 0; i < npatom - 1; i++) { for (int j = i + 1; j < npatom; j++) { var atom1 = PharmacophoreAtom.Get(pharmacophoreMolecule.Atoms[i]); var atom2 = PharmacophoreAtom.Get(pharmacophoreMolecule.Atoms[j]); var bond = new PharmacophoreBond(atom1, atom2); pharmacophoreMolecule.Bonds.Add(bond); } } } // if we have angle constraints, generate only the valid // possible angle relationships, rather than all possible if (HasAngleConstraints(pharmacophoreQuery)) { int nangleDefs = 0; foreach (var bond in pharmacophoreQuery.Bonds) { if (!(bond is PharmacophoreQueryAngleBond)) { continue; } var startQAtom = bond.Atoms[0]; var middleQAtom = bond.Atoms[1]; var endQAtom = bond.Atoms[2]; // make a list of the patoms in the target that match // each type of angle atom var startl = new List <IAtom>(); var middlel = new List <IAtom>(); var endl = new List <IAtom>(); foreach (var tatom in pharmacophoreMolecule.Atoms) { if (tatom.Symbol.Equals(startQAtom.Symbol, StringComparison.Ordinal)) { startl.Add(tatom); } if (tatom.Symbol.Equals(middleQAtom.Symbol, StringComparison.Ordinal)) { middlel.Add(tatom); } if (tatom.Symbol.Equals(endQAtom.Symbol, StringComparison.Ordinal)) { endl.Add(tatom); } } // now we form the relevant angles, but we will // have reversed repeats var tmpl = new List <IAtom[]>(); foreach (var middle in middlel) { foreach (var start in startl) { if (middle.Equals(start)) { continue; } foreach (var end in endl) { if (start.Equals(end) || middle.Equals(end)) { continue; } tmpl.Add(new IAtom[] { start, middle, end }); } } } // now clean up reversed repeats var unique = new List <IAtom[]>(); for (int i = 0; i < tmpl.Count; i++) { var seq1 = tmpl[i]; bool isRepeat = false; for (int j = 0; j < unique.Count; j++) { if (i == j) { continue; } var seq2 = unique[j]; if (Compares.AreDeepEqual(seq1[1], seq2[1]) && Compares.AreDeepEqual(seq1[0], seq2[2]) && Compares.AreDeepEqual(seq1[2], seq2[0])) { isRepeat = true; } } if (!isRepeat) { unique.Add(seq1); } } // finally we can add the unique angle to the target foreach (var seq in unique) { var pbond = new PharmacophoreAngleBond(PharmacophoreAtom.Get(seq[0]), PharmacophoreAtom.Get(seq[1]), PharmacophoreAtom.Get(seq[2])); pharmacophoreMolecule.Bonds.Add(pbond); nangleDefs++; } } Debug.WriteLine($"Added {nangleDefs} defs to the target pcore molecule"); } return(pharmacophoreMolecule); }