use of org.openscience.cdk.interfaces.IMolecularFormula in project mzmine2 by mzmine.
the class FormulaPredictionPeakListTask method run.
/**
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
setStatus(TaskStatus.PROCESSING);
totalRows = peakList.getNumberOfRows();
for (PeakListRow row : peakList.getRows()) {
if (row.getPeakIdentities().length > 0) {
continue;
}
this.searchedMass = (row.getAverageMZ() - ionType.getAddedMass()) * charge;
message = "Formula prediction for " + MZmineCore.getConfiguration().getMZFormat().format(searchedMass);
massRange = mzTolerance.getToleranceRange(searchedMass);
IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
generator = new MolecularFormulaGenerator(builder, massRange.lowerEndpoint(), massRange.upperEndpoint(), elementCounts);
IMolecularFormula cdkFormula;
// create a map to store ResultFormula and relative mass deviation for sorting
Map<Double, String> possibleFormulas = new TreeMap<>();
while ((cdkFormula = generator.getNextFormula()) != null) {
if (isCanceled())
return;
// Mass is ok, so test other constraints
if (checkConstraints(cdkFormula, row) == true) {
String formula = MolecularFormulaManipulator.getString(cdkFormula);
// calc rel mass deviation
Double relMassDev = ((searchedMass - (FormulaUtils.calculateExactMass(formula))) / searchedMass) * 1000000;
// write to map
possibleFormulas.put(relMassDev, formula);
}
}
if (isCanceled())
return;
// create a map to store ResultFormula and relative mass deviation for sorting
Map<Double, String> possibleFormulasSorted = new TreeMap<>((Comparator<Double>) (o1, o2) -> Double.compare(Math.abs(o1), Math.abs(o2)));
possibleFormulasSorted.putAll(possibleFormulas);
// Add the new formula entry top results
int ctr = 0;
for (Map.Entry<Double, String> entry : possibleFormulasSorted.entrySet()) {
if (ctr < maxBestFormulasPerPeak) {
SimplePeakIdentity newIdentity = new SimplePeakIdentity(entry.getValue(), entry.getValue(), this.getClass().getName(), null, null);
row.addPeakIdentity(newIdentity, false);
ctr++;
}
}
if (isCanceled())
return;
finishedRows++;
}
if (isCanceled())
return;
logger.finest("Finished formula search for all the peaks");
setStatus(TaskStatus.FINISHED);
}
use of org.openscience.cdk.interfaces.IMolecularFormula in project mzmine2 by mzmine.
the class IonType method addToFormula.
/**
* Is adding or removing all sub adducts / modifications from the molecular formula
*
* @param formula
* @return
* @throws CloneNotSupportedException
*/
public IMolecularFormula addToFormula(IMolecularFormula formula) throws CloneNotSupportedException {
IMolecularFormula result = (IMolecularFormula) formula.clone();
// add for n molecules the M formula
for (int i = 2; i <= molecules; i++) FormulaUtils.addFormula(result, formula);
// add
Arrays.stream(adduct.getAdducts()).filter(m -> m.getMass() >= 0 && m.getCDKFormula() != null).forEach(m -> FormulaUtils.addFormula(result, m.getCDKFormula()));
if (mod != null)
Arrays.stream(mod.getAdducts()).filter(m -> m.getMass() >= 0 && m.getCDKFormula() != null).forEach(m -> FormulaUtils.addFormula(result, m.getCDKFormula()));
// subtract
Arrays.stream(adduct.getAdducts()).filter(m -> m.getMass() < 0 && m.getCDKFormula() != null).forEach(m -> FormulaUtils.subtractFormula(result, m.getCDKFormula()));
if (mod != null)
Arrays.stream(mod.getAdducts()).filter(m -> m.getMass() < 0 && m.getCDKFormula() != null).forEach(m -> FormulaUtils.subtractFormula(result, m.getCDKFormula()));
return result;
}
use of org.openscience.cdk.interfaces.IMolecularFormula in project mzmine2 by mzmine.
the class IsotopePatternCalculator method calculateIsotopePattern.
public static IsotopePattern calculateIsotopePattern(String molecularFormula, double minAbundance, double mergeWidth, int charge, PolarityType polarity, boolean storeFormula) {
IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
molecularFormula = molecularFormula.replace(" ", "");
IMolecularFormula cdkFormula = MolecularFormulaManipulator.getMolecularFormula(molecularFormula, builder);
return calculateIsotopePattern(cdkFormula, minAbundance, mergeWidth, charge, polarity, storeFormula);
}
use of org.openscience.cdk.interfaces.IMolecularFormula in project mzmine2 by mzmine.
the class DPPSumFormulaPredictionTask method generateFormulas.
/**
* Predicts sum formulas for a given m/z and parameters.
*
* @param mz m/z to generate sum formulas from
* @param massRange Mass range for sum formulas
* @param charge Charge of the molecule
* @param generator instance of MolecularFormulaGenerator
* @return List<PredResult> sorted by relative ppm difference and String of the formula.
*/
private List<PredResult> generateFormulas(ProcessedDataPoint dp, Range<Double> massRange, int charge, MolecularFormulaGenerator generator) {
List<PredResult> possibleFormulas = new ArrayList<>();
IMolecularFormula cdkFormula;
while ((cdkFormula = generator.getNextFormula()) != null) {
// Mass is ok, so test other constraints
if (!checkConstraints(cdkFormula))
continue;
String formula = MolecularFormulaManipulator.getString(cdkFormula);
// calc rel mass deviation
Double relMassDev = ((((//
dp.getMZ() - ionType.getAddedMass()) / //
charge) - (//
FormulaUtils.calculateExactMass(MolecularFormulaManipulator.getString(cdkFormula))) / charge) / ((//
dp.getMZ() - ionType.getAddedMass()) / charge)) * 1000000;
// write to map
if (checkIsotopes && dp.resultTypeExists(ResultType.ISOTOPEPATTERN)) {
double score = getIsotopeSimilarityScore(cdkFormula, (IsotopePattern) dp.getFirstResultByType(ResultType.ISOTOPEPATTERN).getValue());
possibleFormulas.add(new PredResult(relMassDev, formula, score));
} else {
possibleFormulas.add(new PredResult(relMassDev, formula));
}
}
evaluateAndSortFormulas(dp, possibleFormulas);
return possibleFormulas;
}
use of org.openscience.cdk.interfaces.IMolecularFormula in project mzmine2 by mzmine.
the class DynamicParameterUtils method buildFormulaRangeOnIsotopePatternResults.
/**
* Creates an ElementParameter based on the previous processing results. If no results were
* detected, the default value is returned. Upper and lower boundaries are chosen according to
* lowerElementBoundaryPercentage and upperElementBoundaryPercentage values of this utility class.
* These values can be set via {@link #setLowerElementBoundaryPercentage} and
* {@link #setUpperElementBoundaryPercentage}. The elements contained in
*
* @param dp The data point to build a parameter for.
* @param def The default set of parameters.
* @return The built ElementsParameter
*/
public static MolecularFormulaRange buildFormulaRangeOnIsotopePatternResults(ProcessedDataPoint dp, MolecularFormulaRange def) {
DPPIsotopePatternResult result = (DPPIsotopePatternResult) dp.getFirstResultByType(ResultType.ISOTOPEPATTERN);
if (result == null)
return def;
if (!(result.getValue() instanceof ExtendedIsotopePattern))
return def;
ExtendedIsotopePattern pattern = (ExtendedIsotopePattern) result.getValue();
String form = IsotopePatternUtils.makePatternSuggestion(pattern.getIsotopeCompositions());
MolecularFormulaRange range = new MolecularFormulaRange();
IMolecularFormula formula = FormulaUtils.createMajorIsotopeMolFormula(form);
if (formula == null) {
logger.finest("could not generate formula for m/z " + dp.getMZ() + " " + form);
return def;
}
for (IIsotope isotope : def.isotopes()) range.addIsotope(isotope, def.getIsotopeCountMin(isotope), def.getIsotopeCountMax(isotope));
for (IIsotope isotope : formula.isotopes()) {
if (range.contains(isotope))
continue;
int count = formula.getIsotopeCount(isotope);
range.addIsotope(isotope, (int) (count * lowerElementBoundaryPercentage), (int) (count * upperElementBoundaryPercentage));
}
for (IIsotope isotope : range.isotopes()) {
int min = range.getIsotopeCountMin(isotope);
int max = range.getIsotopeCountMax(isotope);
// logger.info("m/z = " + dp.getMZ() + " " + isotope.getSymbol() + " " + min + " - " + max);
}
return range;
}
Aggregations