Search in sources :

Example 11 with IMolecularFormula

use of org.openscience.cdk.interfaces.IMolecularFormula in project mzmine2 by mzmine.

the class FormulaUtils method createMajorIsotopeMolFormula.

/**
 * Creates a formula with the major isotopes (important to use this method for exact mass
 * calculation over the CDK version, which generates formulas without an exact mass)
 *
 * @param formula
 * @return the formula or null
 */
public static IMolecularFormula createMajorIsotopeMolFormula(String formula) {
    try {
        // new formula consists of isotopes without exact mass
        IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
        IMolecularFormula f = MolecularFormulaManipulator.getMajorIsotopeMolecularFormula(formula.replace(" ", ""), builder);
        if (f == null)
            return null;
        // needed, as MolecularFormulaManipulator method returns isotopes without exact mass info
        try {
            return replaceAllIsotopesWithoutExactMass(f);
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Cannot create formula for: " + formula, e);
            return null;
        }
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Cannot create formula for: " + formula, e);
        return null;
    }
}
Also used : IMolecularFormula(org.openscience.cdk.interfaces.IMolecularFormula) IChemObjectBuilder(org.openscience.cdk.interfaces.IChemObjectBuilder) IOException(java.io.IOException)

Example 12 with IMolecularFormula

use of org.openscience.cdk.interfaces.IMolecularFormula in project mzmine2 by mzmine.

the class FormulaUtils method checkMolecularFormula.

/**
 * Checks if a formula string only contains valid isotopes/elements.
 *
 * @param formula String of the molecular formula.
 * @return true / false
 */
public static boolean checkMolecularFormula(String formula) {
    if (formula.matches(".*[äöüÄÖÜß°§$%&/()=?ß²³´`+*~'#;:<>|]")) {
        // check for this first
        logger.info("Formula contains illegal characters.");
        return false;
    }
    IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
    IMolecularFormula molFormula;
    molFormula = MolecularFormulaManipulator.getMajorIsotopeMolecularFormula(formula, builder);
    boolean valid = true;
    for (IIsotope iso : molFormula.isotopes()) {
        if ((iso.getAtomicNumber() == null) || (iso.getAtomicNumber() == 0)) {
            // iso.getAtomicNumber() != null has to be checked, e.g. for some reason an element with
            // Symbol "R" and number 0 exists in the CDK
            valid = false;
        }
    }
    if (!valid) {
        logger.warning("Formula invalid! Formula contains element symbols that do not exist.");
        return false;
    }
    return true;
}
Also used : IIsotope(org.openscience.cdk.interfaces.IIsotope) IMolecularFormula(org.openscience.cdk.interfaces.IMolecularFormula) IChemObjectBuilder(org.openscience.cdk.interfaces.IChemObjectBuilder)

Example 13 with IMolecularFormula

use of org.openscience.cdk.interfaces.IMolecularFormula in project mzmine2 by mzmine.

the class FormulaUtils method getFormulaSize.

public static long getFormulaSize(String formula) {
    long size = 1;
    IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
    IMolecularFormula molFormula;
    molFormula = MolecularFormulaManipulator.getMajorIsotopeMolecularFormula(formula, builder);
    Isotopes isotopeFactory;
    try {
        isotopeFactory = Isotopes.getInstance();
        for (IIsotope iso : molFormula.isotopes()) {
            int naturalIsotopes = 0;
            for (IIsotope i : isotopeFactory.getIsotopes(iso.getSymbol())) {
                if (i.getNaturalAbundance() > 0.0) {
                    naturalIsotopes++;
                }
            }
            try {
                size = Math.multiplyExact(size, (molFormula.getIsotopeCount(iso) * naturalIsotopes));
            } catch (ArithmeticException e) {
                e.printStackTrace();
                logger.info("Formula size of " + formula + " is too big.");
                return -1;
            }
        }
    } catch (IOException e) {
        logger.warning("Unable to initialise Isotopes.");
        e.printStackTrace();
    }
    return size;
}
Also used : IIsotope(org.openscience.cdk.interfaces.IIsotope) IMolecularFormula(org.openscience.cdk.interfaces.IMolecularFormula) IOException(java.io.IOException) Isotopes(org.openscience.cdk.config.Isotopes) IChemObjectBuilder(org.openscience.cdk.interfaces.IChemObjectBuilder)

Aggregations

IMolecularFormula (org.openscience.cdk.interfaces.IMolecularFormula)13 IChemObjectBuilder (org.openscience.cdk.interfaces.IChemObjectBuilder)8 MolecularFormulaRange (org.openscience.cdk.formula.MolecularFormulaRange)4 IIsotope (org.openscience.cdk.interfaces.IIsotope)4 Range (com.google.common.collect.Range)3 ArrayList (java.util.ArrayList)3 DataPoint (net.sf.mzmine.datamodel.DataPoint)3 MZmineCore (net.sf.mzmine.main.MZmineCore)3 FormulaUtils (net.sf.mzmine.util.FormulaUtils)3 MolecularFormulaGenerator (org.openscience.cdk.formula.MolecularFormulaGenerator)3 IOException (java.io.IOException)2 Comparator (java.util.Comparator)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 Logger (java.util.logging.Logger)2 IonizationType (net.sf.mzmine.datamodel.IonizationType)2 IsotopePattern (net.sf.mzmine.datamodel.IsotopePattern)2 MassList (net.sf.mzmine.datamodel.MassList)2 Scan (net.sf.mzmine.datamodel.Scan)2 ExtendedIsotopePattern (net.sf.mzmine.datamodel.impl.ExtendedIsotopePattern)2