Class FormulaSearch


  • @PublicAPI
    public class FormulaSearch
    extends Object
    Chemical formula based search class.

    Features:

    • case insensitive notation, upper case letters however define a new atom for the formula parser
    • no Hill notation is required
    • parentheses can be used for: multiplication - (C2H6)3, polymers - (C4H6)n, combinatorical groups - (Br + I + F)4
    • interval definition: N-M - at least N, at most M
    • multiple components are separated with a period (.), in case of multiple components a coefficient can be set for each component. Coefficient examples: x - any, 2 - two, 2 - 4 - interval between two and four, use 1 / 5 for fractions.
    • isotope handling is available using brackets [12C]. Trivial abbreviations are also accepted D = [2H], T = [3H].

    Search types:

    • Exact search - The result list contains molecular formulas equal to the given search criteria, atoms with differing numbers and other atom types are not allowed to be present.
    • Exact subformula search - The result list contains molecular formulas equal to the given search criteria, atoms with differing numbers will not, but other atom types may be present. E.g. query formula C6 H6 O6 matches C6 H6 O6 S but does not match C4 H6 O6
    • Subformula search - The result list contains molecular formulas matching at least the given search criteria but higher number of atoms and other atom types may also be present. E.g. query formula C6 H6 O2 matches C6 H12 O6, C6 H6 O2 S but does not match C4 H6 O6 or C2 H6 O N. According to search rules, in subformula search the query formula can match polymer formula as well.

    Example code:

     FormulaSearch formulaSearch = new FormulaSearch();
     
     String queryFormula;
     ... // obtain chemical formula of the query
     String targetFormula;
     ... // obtain chemical formula of the target
     
     formulaSearch.setSearchType(FormulaSearch.EXACT); // see search types
     formulaSearch.setQuery(queryFormula); // set query formula
     formulaSearch.setTarget(targetFormula); // set target formula
     
     return formulaSearch.isMatching();
     
    • Field Detail

      • NONE

        public static final int NONE
        Constant value specifying the none search type.
        See Also:
        Constant Field Values
      • EXACT

        public static final int EXACT
        Constant value specifying the exact search type. The target needs to match the query exactly for all atoms and cannot contain other atoms.
        See Also:
        Constant Field Values
      • EXACT_SUBFORMULA

        public static final int EXACT_SUBFORMULA
        Constant value specifying the exact sub formula search type. The target needs to match the query exactly for all atoms but can contain unmentioned atoms as well.
        See Also:
        Constant Field Values
      • SUBFORMULA

        public static final int SUBFORMULA
        Constant value specifying the sub formula search type. The target needs to contain at least the specified number of atoms given in the query.
        See Also:
        Constant Field Values
    • Constructor Detail

      • FormulaSearch

        public FormulaSearch()
    • Method Detail

      • setSearchType

        public void setSearchType​(int value)
        Sets the type of the formula search.
        Parameters:
        value - the search type
        See Also:
        EXACT, EXACT_SUBFORMULA, SUBFORMULA
      • getQuery

        public String getQuery()
        Gets the chemical formula query string used for searching
        Returns:
        the chemical formula string
      • getTarget

        public String getTarget()
        Gets the chemical formula of the target used to search in
        Returns:
        the chemical formula string
      • isMatching

        public boolean isMatching()
        Checks if the query formula matches the target formula according to the formula search type.
        Returns:
        true if query matches the target, false if query or target was not supplied correctly, or no match is possible
        See Also:
        setQuery(String), setTarget(String), setSearchType(int)
      • isValidQuery

        public static boolean isValidQuery​(String formula)
        Checks the given formula whether it is a valid query formula
        Parameters:
        formula - the formula to be checked
        Returns:
        true if the formula is valid
      • isValidTarget

        public static boolean isValidTarget​(String formula)
        Checks the given formula whether it is a valid target formula
        Parameters:
        formula - the formula to be checked
        Returns:
        true if the formula is valid