use of net.sf.mzmine.modules.visualization.spectra.simplespectra.datasets.DataPointsDataSet in project mzmine2 by mzmine.
the class SpectralMatchTask method addIdentities.
private void addIdentities(List<SpectralDBPeakIdentity> matches) {
for (SpectralDBPeakIdentity match : matches) {
try {
// TODO put into separate method and add comments
// get data points of matching scans
DataPoint[] spectraMassList = getDataPoints(currentScan);
List<DataPoint[]> alignedDataPoints = ScanAlignment.align(mzToleranceSpectra, match.getEntry().getDataPoints(), spectraMassList);
alignedSignals = ScanAlignment.removeUnaligned(alignedDataPoints);
// add new mass list to the spectra for match
DataPoint[] dataset = new DataPoint[alignedSignals.size()];
for (int i = 0; i < dataset.length; i++) {
dataset[i] = alignedSignals.get(i)[1];
}
String compoundName = match.getEntry().getField(DBEntryField.NAME).toString();
String shortName = compoundName;
// TODO remove or specify more - special naming format?
int start = compoundName.indexOf("[");
int end = compoundName.indexOf("]");
if (start != -1 && start + 1 < compoundName.length() && end != -1 && end < compoundName.length())
shortName = compoundName.substring(start + 1, end);
DataPointsDataSet detectedCompoundsDataset = new DataPointsDataSet(shortName + " " + "Score: " + COS_FORM.format(match.getSimilarity().getScore()), dataset);
spectraPlot.addDataSet(detectedCompoundsDataset, new Color((int) (Math.random() * 0x1000000)), true);
} catch (MissingMassListException e) {
logger.log(Level.WARNING, "No mass list for the selected spectrum", e);
errorCounter++;
}
}
resultWindow.addMatches(matches);
resultWindow.revalidate();
resultWindow.repaint();
setStatus(TaskStatus.FINISHED);
}
use of net.sf.mzmine.modules.visualization.spectra.simplespectra.datasets.DataPointsDataSet in project mzmine2 by mzmine.
the class SpectraIdentificationSumFormulaTask method run.
/**
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
setStatus(TaskStatus.PROCESSING);
logger.finest("Starting search for formulas for " + massRange + " Da");
// create mass list for scan
DataPoint[] massList = null;
ArrayList<DataPoint> massListAnnotated = new ArrayList<>();
MassDetector massDetector = null;
ArrayList<String> allCompoundIDs = new ArrayList<>();
// Create a new mass list for MS/MS scan. Check if sprectrum is profile or centroid mode
if (currentScan.getSpectrumType() == MassSpectrumType.CENTROIDED) {
massDetector = new CentroidMassDetector();
CentroidMassDetectorParameters parameters = new CentroidMassDetectorParameters();
CentroidMassDetectorParameters.noiseLevel.setValue(noiseLevel);
massList = massDetector.getMassValues(currentScan.getDataPoints(), parameters);
} else {
massDetector = new ExactMassDetector();
ExactMassDetectorParameters parameters = new ExactMassDetectorParameters();
ExactMassDetectorParameters.noiseLevel.setValue(noiseLevel);
massList = massDetector.getMassValues(currentScan.getDataPoints(), parameters);
}
numItems = massList.length;
// loop through every peak in mass list
if (getStatus() != TaskStatus.PROCESSING) {
return;
}
IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
for (int i = 0; i < massList.length; i++) {
massRange = mzTolerance.getToleranceRange((massList[i].getMZ() - ionType.getAddedMass()) / charge);
generator = new MolecularFormulaGenerator(builder, massRange.lowerEndpoint(), massRange.upperEndpoint(), elementCounts);
IMolecularFormula cdkFormula;
String annotation = "";
// 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) == true) {
String formula = MolecularFormulaManipulator.getString(cdkFormula);
// calc rel mass deviation
Double relMassDev = ((((//
massList[i].getMZ() - ionType.getAddedMass()) / //
charge) - (//
FormulaUtils.calculateExactMass(MolecularFormulaManipulator.getString(cdkFormula))) / charge) / ((//
massList[i].getMZ() - ionType.getAddedMass()) / charge)) * 1000000;
// write to map
possibleFormulas.put(relMassDev, formula);
}
}
Map<Double, String> treeMap = new TreeMap<>((Comparator<Double>) (o1, o2) -> Double.compare(Math.abs(o1), Math.abs(o2)));
treeMap.putAll(possibleFormulas);
// get top 3
int ctr = 0;
for (Map.Entry<Double, String> entry : treeMap.entrySet()) {
int number = ctr + 1;
if (ctr > 2)
break;
annotation = annotation + number + ". " + entry.getValue() + " Δ " + NumberFormat.getInstance().format(entry.getKey()) + " ppm; ";
ctr++;
if (isCanceled())
return;
}
if (annotation != "") {
allCompoundIDs.add(annotation);
massListAnnotated.add(massList[i]);
}
logger.finest("Finished formula search for " + massRange + " m/z, found " + foundFormulas + " formulas");
}
// new mass list
DataPoint[] annotatedMassList = new DataPoint[massListAnnotated.size()];
massListAnnotated.toArray(annotatedMassList);
String[] annotations = new String[annotatedMassList.length];
allCompoundIDs.toArray(annotations);
DataPointsDataSet detectedCompoundsDataset = new DataPointsDataSet("Detected compounds", annotatedMassList);
// Add label generator for the dataset
SpectraDatabaseSearchLabelGenerator labelGenerator = new SpectraDatabaseSearchLabelGenerator(annotations, spectraPlot);
spectraPlot.addDataSet(detectedCompoundsDataset, Color.orange, true, labelGenerator);
spectraPlot.getXYPlot().getRenderer().setSeriesItemLabelGenerator(spectraPlot.getXYPlot().getSeriesCount(), labelGenerator);
spectraPlot.getXYPlot().getRenderer().setDefaultPositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.TOP_LEFT, TextAnchor.BOTTOM_CENTER, 0.0), true);
setStatus(TaskStatus.FINISHED);
}
use of net.sf.mzmine.modules.visualization.spectra.simplespectra.datasets.DataPointsDataSet in project mzmine2 by mzmine.
the class SpectraIdentificationCustomDatabaseTask method run.
/**
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
setStatus(TaskStatus.PROCESSING);
// create mass list for scan
DataPoint[] massList = null;
ArrayList<DataPoint> massListAnnotated = new ArrayList<>();
MassDetector massDetector = null;
ArrayList<String> allCompoundIDs = new ArrayList<>();
// Create a new mass list for MS/MS scan. Check if sprectrum is profile or centroid mode
if (currentScan.getSpectrumType() == MassSpectrumType.CENTROIDED) {
massDetector = new CentroidMassDetector();
CentroidMassDetectorParameters parameters = new CentroidMassDetectorParameters();
CentroidMassDetectorParameters.noiseLevel.setValue(noiseLevel);
massList = massDetector.getMassValues(currentScan.getDataPoints(), parameters);
} else {
massDetector = new ExactMassDetector();
ExactMassDetectorParameters parameters = new ExactMassDetectorParameters();
ExactMassDetectorParameters.noiseLevel.setValue(noiseLevel);
massList = massDetector.getMassValues(currentScan.getDataPoints(), parameters);
}
numItems = massList.length;
// load custom database
try {
// read database contents in memory
FileReader dbFileReader = new FileReader(dataBaseFile);
databaseValues = CSVParser.parse(dbFileReader, fieldSeparator.charAt(0));
if (ignoreFirstLine)
finishedLines++;
for (; finishedLines < databaseValues.length; finishedLines++) {
if (isCanceled()) {
dbFileReader.close();
return;
}
int numOfColumns = Math.min(fieldOrder.length, databaseValues[finishedLines].length);
String lineName = null;
double lineMZ = 0;
for (int i = 0; i < numOfColumns; i++) {
if (fieldOrder[i] == FieldItem.FIELD_NAME)
lineName = databaseValues[finishedLines][i].toString();
if (fieldOrder[i] == FieldItem.FIELD_MZ)
lineMZ = Double.parseDouble(databaseValues[finishedLines][i].toString());
}
for (int i = 0; i < massList.length; i++) {
// loop through every peak in mass list
if (getStatus() != TaskStatus.PROCESSING) {
return;
}
double searchedMass = massList[i].getMZ();
Range<Double> mzRange = mzTolerance.getToleranceRange(searchedMass);
boolean mzMatches = (lineMZ == 0d) || mzRange.contains(lineMZ);
String annotation = "";
if (mzMatches) {
// calc rel mass deviation
double relMassDev = ((searchedMass - lineMZ) / searchedMass) * 1000000;
logger.finest("Found compound " + lineName + " m/z " + NumberFormat.getInstance().format(searchedMass) + " Δ " + NumberFormat.getInstance().format(relMassDev) + " ppm");
annotation = lineName + " Δ " + NumberFormat.getInstance().format(relMassDev) + " ppm";
}
if (annotation != "") {
allCompoundIDs.add(annotation);
massListAnnotated.add(massList[i]);
}
}
finishedLines++;
}
// close the file reader
dbFileReader.close();
} catch (Exception e) {
logger.log(Level.WARNING, "Could not read file " + dataBaseFile, e);
setStatus(TaskStatus.ERROR);
setErrorMessage(e.toString());
return;
}
// new mass list
DataPoint[] annotatedMassList = new DataPoint[massListAnnotated.size()];
massListAnnotated.toArray(annotatedMassList);
String[] annotations = new String[annotatedMassList.length];
allCompoundIDs.toArray(annotations);
DataPointsDataSet detectedCompoundsDataset = new DataPointsDataSet("Detected compounds", annotatedMassList);
// Add label generator for the dataset
SpectraDatabaseSearchLabelGenerator labelGenerator = new SpectraDatabaseSearchLabelGenerator(annotations, spectraPlot);
spectraPlot.addDataSet(detectedCompoundsDataset, Color.orange, true, labelGenerator);
spectraPlot.getXYPlot().getRenderer().setSeriesItemLabelGenerator(spectraPlot.getXYPlot().getSeriesCount(), labelGenerator);
spectraPlot.getXYPlot().getRenderer().setDefaultPositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.TOP_LEFT, TextAnchor.BOTTOM_CENTER, 0.0), true);
setStatus(TaskStatus.FINISHED);
}
Aggregations