예제 #1
0
  /**
   * Perform a SMARTS match and check whether the query is present in the target molecule.
   *
   * <p>This function simply checks whether the query pattern matches the specified molecule.
   * However the function will also, internally, save the mapping of query atoms to the target
   * molecule
   *
   * @param atomContainer The target moleculoe
   * @param forceInitialization If true, then the molecule is initialized (ring perception,
   *     aromaticity etc). If false, the molecule is only initialized if it is different (in terms
   *     of object reference) than one supplied in a previous call to this method.
   * @return true if the pattern is found in the target molecule, false otherwise
   * @throws CDKException if there is an error in ring, aromaticity or isomorphism perception
   * @see #getMatchingAtoms()
   * @see #countMatches()
   * @see #matches(org.openscience.cdk.interfaces.IAtomContainer)
   */
  @TestMethod("testQueryTool, testQueryToolSingleAtomCase, testQuery")
  public boolean matches(IAtomContainer atomContainer, boolean forceInitialization)
      throws CDKException {

    if (this.atomContainer == atomContainer) {
      if (forceInitialization) initializeMolecule();
    } else {
      this.atomContainer = atomContainer;
      initializeMolecule();
    }

    // First calculate the recursive smarts
    initializeRecursiveSmarts(this.atomContainer);

    // lets see if we have a single atom query
    if (query.getAtomCount() == 1) {
      // lets get the query atom
      IQueryAtom queryAtom = (IQueryAtom) query.getAtom(0);

      matchingAtoms = new ArrayList<List<Integer>>();
      for (IAtom atom : this.atomContainer.atoms()) {
        if (queryAtom.matches(atom)) {
          List<Integer> tmp = new ArrayList<Integer>();
          tmp.add(this.atomContainer.getAtomNumber(atom));
          matchingAtoms.add(tmp);
        }
      }
    } else {
      List bondMapping =
          new UniversalIsomorphismTester().getSubgraphMaps(this.atomContainer, query);
      matchingAtoms = getAtomMappings(bondMapping, this.atomContainer);
    }

    return matchingAtoms.size() != 0;
  }
예제 #2
0
 /**
  * Initializes recursive smarts atoms in the query.
  *
  * <p>We loop over the SMARTS atoms in the query and associate the target molecule with each of
  * the SMARTS atoms that need it
  *
  * @param atomContainer
  * @throws CDKException
  */
 private void initializeRecursiveSmarts(IAtomContainer atomContainer) throws CDKException {
   for (IAtom atom : query.atoms()) {
     initializeRecursiveSmartsAtom(atom, atomContainer);
   }
 }