Class SmartsPattern


  • public final class SmartsPattern
    extends Pattern
    A Pattern for matching a single SMARTS query against multiple target compounds. The class can be used for efficiently matching many queries against a single target if setPrepare(boolean) is disabled (prepare(IAtomContainer)) should be called manually once for each molecule. Simple usage:
     Pattern ptrn = SmartsPattern.create("O[C@?H](C)CC");
    
     for (IAtomContainer ac : acs) {
       if (ptrn.matches(ac)) {
           // 'ac' contains the pattern
       }
     }
     
    Obtaining a Mappings instance and determine the number of unique matches.
     Pattern ptrn = SmartsPattern.create("O[C@?H](C)CC");
    
     for (IAtomContainer ac : acs) {
       nUniqueHits += ptrn.matchAll(ac)
                          .countUnique();
     }
     
    Author:
    John May
    • Method Detail

      • setPrepare

        public SmartsPattern setPrepare​(boolean doPrep)
        Sets whether the molecule should be "prepared" for a SMARTS match, including set ring flags and perceiving aromaticity. The main reason to skip preparation (via prepare(IAtomContainer)) is if it has already been done, for example when matching multiple SMARTS patterns.
        Parameters:
        doPrep - whether preparation should be done
        Returns:
        self for inline calling
      • match

        public int[] match​(IAtomContainer container)
        Find a matching of this pattern in the target. If no such order exist an empty mapping is returned. Depending on the implementation stereochemistry may be checked (recommended).
        
         Pattern        pattern = ...; // create pattern
         for (IAtomContainer m : ms) {
             int[] mapping = pattern.match(m);
             if (mapping.length > 0) {
                 // found mapping!
             }
         }
         
        Specified by:
        match in class Pattern
        Parameters:
        container - the container to search for the pattern in
        Returns:
        the mapping from the pattern to the target or an empty array
      • matchAll

        public Mappings matchAll​(IAtomContainer target)
        Obtain the mappings of the query pattern against the target compound. Any initialisations required for the SMARTS match are automatically performed. The Daylight aromaticity model is applied clearing existing aromaticity. Do not use this for matching multiple SMARTS againsts the same container.
         Pattern ptrn = SmartsPattern.create("O[C@?H](C)CC");
         int nUniqueHits = 0;
        
         for (IAtomContainer ac : acs) {
           nUniqueHits += ptrn.matchAll(ac)
                              .countUnique();
         }
         
        See Mappings for available methods.
        Specified by:
        matchAll in class Pattern
        Parameters:
        target - the target compound in which we want to match the pattern
        Returns:
        mappings of the query to the target compound
        See Also:
        Mappings
      • create

        public static SmartsPattern create​(String smarts,
                                           IChemObjectBuilder builder)
        Create a Pattern that will match the given smarts query.
        Parameters:
        smarts - SMARTS pattern string
        builder - chem object builder used to create objects
        Returns:
        a new pattern
      • create

        public static SmartsPattern create​(String smarts)
        Default SMARTS pattern constructor, passes in a null chem object builder.
        Parameters:
        smarts - SMARTS pattern string
        Returns:
        a SMARTS pattern