use of net.sf.mzmine.datamodel.DataPoint in project mzmine2 by mzmine.
the class ADAP3DecompositionV1_5Task method getPeaks.
/**
* Convert MZmine PeakList to a list of ADAP Peaks
*
* @param peakList MZmine PeakList object
* @param edgeToHeightThreshold edge-to-height threshold to determine peaks that can be merged
* @param deltaToHeightThreshold delta-to-height threshold to determine peaks that can be merged
* @return list of ADAP Peaks
*/
@Nonnull
public static List<Peak> getPeaks(final PeakList peakList, final double edgeToHeightThreshold, final double deltaToHeightThreshold) {
RawDataFile dataFile = peakList.getRawDataFile(0);
List<Peak> peaks = new ArrayList<>();
for (PeakListRow row : peakList.getRows()) {
Feature peak = row.getBestPeak();
int[] scanNumbers = peak.getScanNumbers();
// Build chromatogram
NavigableMap<Double, Double> chromatogram = new TreeMap<>();
for (int scanNumber : scanNumbers) {
DataPoint dataPoint = peak.getDataPoint(scanNumber);
if (dataPoint != null)
chromatogram.put(dataFile.getScan(scanNumber).getRetentionTime(), dataPoint.getIntensity());
}
if (chromatogram.size() <= 1)
continue;
// Fill out PeakInfo
PeakInfo info = new PeakInfo();
try {
// Note: info.peakID is the index of PeakListRow in PeakList.peakListRows (starts from 0)
// row.getID is row.myID (starts from 1)
info.peakID = row.getID() - 1;
double height = -Double.MIN_VALUE;
for (int scan : scanNumbers) {
double intensity = peak.getDataPoint(scan).getIntensity();
if (intensity > height) {
height = intensity;
info.peakIndex = scan;
}
}
info.leftApexIndex = scanNumbers[0];
info.rightApexIndex = scanNumbers[scanNumbers.length - 1];
info.retTime = peak.getRT();
info.mzValue = peak.getMZ();
info.intensity = peak.getHeight();
info.leftPeakIndex = info.leftApexIndex;
info.rightPeakIndex = info.rightApexIndex;
} catch (Exception e) {
LOG.info("Skipping " + row + ": " + e.getMessage());
continue;
}
peaks.add(new Peak(chromatogram, info));
}
FeatureTools.correctPeakBoundaries(peaks, edgeToHeightThreshold, deltaToHeightThreshold);
return peaks;
}
use of net.sf.mzmine.datamodel.DataPoint in project mzmine2 by mzmine.
the class ADAP3DecompositionV1_5Task method decomposePeaks.
private PeakList decomposePeaks(PeakList peakList) throws CloneNotSupportedException, IOException {
RawDataFile dataFile = peakList.getRawDataFile(0);
// Create new feature list.
final PeakList resolvedPeakList = new SimplePeakList(peakList + " " + parameters.getParameter(ADAP3DecompositionV1_5Parameters.SUFFIX).getValue(), dataFile);
// Load previous applied methods.
for (final PeakList.PeakListAppliedMethod method : peakList.getAppliedMethods()) {
resolvedPeakList.addDescriptionOfAppliedTask(method);
}
// Add task description to feature list.
resolvedPeakList.addDescriptionOfAppliedTask(new SimplePeakListAppliedMethod("Peak deconvolution by ADAP-3", parameters));
// Collect peak information
List<Peak> peaks = getPeaks(peakList, this.parameters.getParameter(ADAP3DecompositionV1_5Parameters.EDGE_TO_HEIGHT_RATIO).getValue(), this.parameters.getParameter(ADAP3DecompositionV1_5Parameters.DELTA_TO_HEIGHT_RATIO).getValue());
// Find components (a.k.a. clusters of peaks with fragmentation spectra)
List<Component> components = getComponents(peaks);
// Create PeakListRow for each components
List<PeakListRow> newPeakListRows = new ArrayList<>();
int rowID = 0;
for (final Component component : components) {
if (component.getSpectrum().isEmpty())
continue;
PeakListRow row = new SimplePeakListRow(++rowID);
// Add the reference peak
PeakListRow refPeakRow = originalPeakList.getRow(component.getBestPeak().getInfo().peakID);
Feature refPeak = new SimpleFeature(refPeakRow.getBestPeak());
// Add spectrum
List<DataPoint> dataPoints = new ArrayList<>();
for (Map.Entry<Double, Double> entry : component.getSpectrum().entrySet()) {
dataPoints.add(new SimpleDataPoint(entry.getKey(), entry.getValue()));
}
refPeak.setIsotopePattern(new SimpleIsotopePattern(dataPoints.toArray(new DataPoint[dataPoints.size()]), IsotopePattern.IsotopePatternStatus.PREDICTED, "Spectrum"));
row.addPeak(dataFile, refPeak);
// Add PeakInformation
if (refPeakRow.getPeakInformation() == null) {
SimplePeakInformation information = new SimplePeakInformation(new HashMap<>(refPeakRow.getPeakInformation().getAllProperties()));
row.setPeakInformation(information);
}
// Set row properties
row.setAverageMZ(refPeakRow.getAverageMZ());
row.setAverageRT(refPeakRow.getAverageRT());
// resolvedPeakList.addRow(row);
newPeakListRows.add(row);
}
// ------------------------------------
// Sort new peak rows by retention time
// ------------------------------------
Collections.sort(newPeakListRows, new Comparator<PeakListRow>() {
@Override
public int compare(PeakListRow row1, PeakListRow row2) {
double retTime1 = row1.getAverageRT();
double retTime2 = row2.getAverageRT();
return Double.compare(retTime1, retTime2);
}
});
for (PeakListRow row : newPeakListRows) resolvedPeakList.addRow(row);
return resolvedPeakList;
}
use of net.sf.mzmine.datamodel.DataPoint in project mzmine2 by mzmine.
the class IsotopePeakScannerTask method run.
@Override
public void run() {
if (!checkParameters())
return;
setStatus(TaskStatus.PROCESSING);
totalRows = peakList.getNumberOfRows();
double[][] diff = setUpDiffAutoCarbon();
if (diff == null) {
message = "ERROR: could not set up diff.";
setStatus(TaskStatus.ERROR);
return;
}
logger.info("diff.length: " + diff.length);
logger.info("maxPatternIndex: " + maxPatternIndex);
logger.info("maxPatternSize: " + maxPatternSize);
// get all rows and sort by m/z
PeakListRow[] rows = peakList.getRows();
Arrays.sort(rows, new PeakListRowSorter(SortingProperty.MZ, SortingDirection.Ascending));
PeakListHandler plh = new PeakListHandler();
plh.setUp(peakList);
resultPeakList = new SimplePeakList(peakList.getName() + suffix, peakList.getRawDataFiles());
PeakListHandler resultMap = new PeakListHandler();
for (int i = 0; i < totalRows; i++) {
// i will represent the index of the row in peakList
if (rows[i].getPeakIdentities().length > 0) {
finishedRows++;
continue;
}
message = "Row " + i + "/" + totalRows;
// now get all peaks that lie within RT and maxIsotopeMassRange: pL[index].mz ->
// pL[index].mz+maxMass
ArrayList<PeakListRow> groupedPeaks = groupPeaks(rows, i, diff[maxPatternIndex][diff[maxPatternIndex].length - 1]);
if (groupedPeaks.size() < 2) {
finishedRows++;
continue;
}
// else
// logger.info("groupedPeaks.size > 2 in row: " + i + " size: " +
// groupedPeaks.size());
// this will store row
ResultBuffer[][] resultBuffer = new ResultBuffer[diff.length][];
for (int p = 0; p < diff.length; p++) {
// resultBuffer[i] index will represent Isotope[i] (if
// numAtoms = 0)
resultBuffer[p] = new ResultBuffer[diff[p].length];
for (int k = 0; k < diff[p].length; k++) // [p][0] will be the isotope with lowest mass#
resultBuffer[p][k] = new ResultBuffer();
}
// of all features with fitting rt
// and mz
boolean[] trueBuffers = new boolean[diff.length];
Arrays.fill(trueBuffers, false);
for (// go through all possible peaks
int j = 0; // go through all possible peaks
j < groupedPeaks.size(); // go through all possible peaks
j++) {
for (int p = 0; p < diff.length; p++) {
for (// check for each peak if it is a possible
int k = 0; // check for each peak if it is a possible
k < diff[p].length; // check for each peak if it is a possible
k++) // feature
// for
// every diff[](isotope)
{
// p = pattern index for autoCarbon
if (mzTolerance.checkWithinTolerance(groupedPeaks.get(0).getAverageMZ() + diff[p][k], groupedPeaks.get(j).getAverageMZ())) {
// this will automatically add groupedPeaks[0] to the list -> isotope with
// lowest mass
// +1 result for isotope k
resultBuffer[p][k].addFound();
// row in groupedPeaks[]
resultBuffer[p][k].addRow(j);
resultBuffer[p][k].addID(groupedPeaks.get(j).getID());
}
}
}
}
boolean foundOne = false;
for (int p = 0; p < diff.length; p++) if (checkIfAllTrue(resultBuffer[p])) {
// this means that for every isotope we expected to
// find,
// we found one or more possible features
foundOne = true;
trueBuffers[p] = true;
// logger.info("Row: " + i + " filled buffer[" + p +"]");
}
if (!foundOne) {
finishedRows++;
continue;
}
Candidates[] candidates = new Candidates[diff.length];
for (int p = 0; p < diff.length; p++) candidates[p] = new Candidates(diff[p].length, minHeight, mzTolerance, pattern[p], massListName, plh, ratingType);
for (int p = 0; p < diff.length; p++) {
if (!trueBuffers[p])
continue;
for (// reminder: resultBuffer.length =
int k = 0; // reminder: resultBuffer.length =
k < resultBuffer[p].length; // reminder: resultBuffer.length =
k++) // diff.length
{
for (int l = 0; l < resultBuffer[p][k].getFoundCount(); l++) {
// k represents index resultBuffer[k] and thereby the isotope number
// l represents the number of results in resultBuffer[k]
candidates[p].checkForBetterRating(k, groupedPeaks.get(0), groupedPeaks.get(resultBuffer[p][k].getRow(l)), minRating, checkIntensity);
}
}
}
foundOne = false;
boolean[] trueCandidates = new boolean[diff.length];
Arrays.fill(trueCandidates, false);
for (int p = 0; p < diff.length; p++) {
if (trueBuffers[p] && checkIfAllTrue(candidates[p].getCandidates())) {
trueCandidates[p] = true;
foundOne = true;
// logger.info("Row: " + i + " filled candidates[" + p + "]");
}
}
if (!foundOne) {
finishedRows++;
// jump to next i
continue;
}
// find best result now, first we have to calc avg ratings if specified by user
int bestPatternIndex = 0;
double bestRating = 0.0;
for (int p = 0; p < diff.length; p++) {
if (!trueCandidates[p])
continue;
if (accurateAvgIntensity)
candidates[p].calcAvgRatings();
if (accurateAvgIntensity && candidates[p].getAvgAccAvgRating() > bestRating) {
bestPatternIndex = p;
bestRating = candidates[p].getAvgAccAvgRating();
} else if (!accurateAvgIntensity && candidates[p].getSimpleAvgRating() > bestRating) {
bestPatternIndex = p;
bestRating = candidates[p].getSimpleAvgRating();
}
}
if (!checkIfAllTrue(candidates[bestPatternIndex].getCandidates())) {
logger.warning("We were about to add candidates with null pointers.\nThis was no valid result. Continueing.");
continue;
}
// TODO: this shouldnt be needed, fix the bug that causes the crash later on.
// this happens occasionally if the user wants to do accurate average but does not filter
// by RT. then possible isotope peaks are found, although they are not detected at the same
// time. This will result in the candidates return -1.0 which will sooner or later return a
// null pointer Fixing this will be done in a future update, but needs a rework of the
// candidates class.
// The results you miss by skipping here would have not been valid results anyway, so this
// is not urgent. Will be nicer though, because of cleaner code.
// PeakListRow parent = copyPeakRow(peakList.getRow(i));
boolean allPeaksAddable = true;
List<PeakListRow> rowBuffer = new ArrayList<PeakListRow>();
PeakListRow original = getRowFromCandidate(candidates, bestPatternIndex, 0, plh);
if (original == null)
continue;
PeakListRow parent = copyPeakRow(original);
if (// if we can assign this row multiple times we
resultMap.containsID(parent.getID()))
// have to copy the comment, because adding it to
// the map twice will overwrite the results
addComment(parent, resultMap.getRowByID(parent.getID()).getComment());
// ID is added to be able to sort by
addComment(parent, parent.getID() + "--IS PARENT--");
if (carbonRange != 1)
addComment(parent, "BestPattern: " + pattern[bestPatternIndex].getDescription());
rowBuffer.add(parent);
DataPoint[] dp = new DataPoint[pattern[bestPatternIndex].getNumberOfDataPoints()];
if (accurateAvgIntensity) {
dp[0] = new SimpleDataPoint(parent.getAverageMZ(), candidates[bestPatternIndex].getAvgHeight(0));
} else {
dp[0] = new SimpleDataPoint(parent.getAverageMZ(), parent.getAverageHeight());
}
for (// we skip k=0 because ==
int k = 1; // we skip k=0 because ==
k < candidates[bestPatternIndex].size(); // we skip k=0 because ==
k++) // groupedPeaks[0]/
// ==candidates.get(0) which we added before
{
PeakListRow originalChild = getRowFromCandidate(candidates, bestPatternIndex, k, plh);
if (originalChild == null) {
allPeaksAddable = false;
continue;
}
PeakListRow child = copyPeakRow(originalChild);
if (accurateAvgIntensity) {
dp[k] = new SimpleDataPoint(child.getAverageMZ(), candidates[bestPatternIndex].getAvgHeight(k));
} else {
dp[k] = new SimpleDataPoint(child.getAverageMZ(), child.getAverageHeight());
}
String average = "";
if (accurateAvgIntensity) {
average = " AvgRating: " + round(candidates[bestPatternIndex].getAvgRating(k), 3);
}
addComment(parent, "Intensity ratios: " + getIntensityRatios(pattern[bestPatternIndex], pattern[bestPatternIndex].getHighestDataPointIndex()));
if (accurateAvgIntensity)
addComment(parent, " Avg pattern rating: " + round(candidates[bestPatternIndex].getAvgAccAvgRating(), 3));
else
addComment(parent, " pattern rating: " + round(candidates[bestPatternIndex].getSimpleAvgRating(), 3));
addComment(child, (parent.getID() + "-Parent ID" + " m/z-shift(ppm): " + round(((child.getAverageMZ() - parent.getAverageMZ()) - diff[bestPatternIndex][k]) / child.getAverageMZ() * 1E6, 2) + " I(c)/I(p): " + round(child.getAverageHeight() / plh.getRowByID(candidates[bestPatternIndex].get(pattern[bestPatternIndex].getHighestDataPointIndex()).getCandID()).getAverageHeight(), 2) + " Identity: " + pattern[bestPatternIndex].getIsotopeComposition(k) + " Rating: " + round(candidates[bestPatternIndex].get(k).getRating(), 3) + average));
rowBuffer.add(child);
}
if (!allPeaksAddable)
continue;
IsotopePattern resultPattern = new SimpleIsotopePattern(dp, IsotopePatternStatus.DETECTED, element + " monoisotopic mass: " + parent.getAverageMZ());
parent.getBestPeak().setIsotopePattern(resultPattern);
for (PeakListRow row : rowBuffer) {
row.getBestPeak().setIsotopePattern(resultPattern);
resultMap.addRow(row);
}
if (isCanceled())
return;
finishedRows++;
}
ArrayList<Integer> keys = resultMap.getAllKeys();
for (int j = 0; j < keys.size(); j++) resultPeakList.addRow(resultMap.getRowByID(keys.get(j)));
if (resultPeakList.getNumberOfRows() > 1)
addResultToProject();
else
message = "Element not found.";
setStatus(TaskStatus.FINISHED);
}
use of net.sf.mzmine.datamodel.DataPoint in project mzmine2 by mzmine.
the class IsotopePatternCalculator method normalizeIsotopePattern.
/**
* Returns same isotope pattern (same ratios between isotope intensities) with maximum intensity
* normalized to given intensity
*/
public static IsotopePattern normalizeIsotopePattern(IsotopePattern pattern, double normalizedValue) {
DataPoint highestIsotope = pattern.getHighestDataPoint();
DataPoint[] dataPoints = pattern.getDataPoints();
double maxIntensity = highestIsotope.getIntensity();
DataPoint[] newDataPoints = new DataPoint[dataPoints.length];
for (int i = 0; i < dataPoints.length; i++) {
double mz = dataPoints[i].getMZ();
double intensity = dataPoints[i].getIntensity() / maxIntensity * normalizedValue;
newDataPoints[i] = new SimpleDataPoint(mz, intensity);
}
if (pattern instanceof ExtendedIsotopePattern && ((ExtendedIsotopePattern) pattern).getIsotopeCompositions() != null)
return new ExtendedIsotopePattern(newDataPoints, pattern.getStatus(), pattern.getDescription(), ((ExtendedIsotopePattern) pattern).getIsotopeCompositions());
else
return new SimpleIsotopePattern(newDataPoints, pattern.getStatus(), pattern.getDescription());
}
use of net.sf.mzmine.datamodel.DataPoint in project mzmine2 by mzmine.
the class IsotopePatternCalculator method mergeIsotopes.
/**
* Merges the isotopes falling within the given m/z tolerance. If the m/z difference between the
* isotopes is smaller than mzTolerance, their intensity is added together and new m/z value is
* calculated as a weighted average.
*/
public static IsotopePattern mergeIsotopes(IsotopePattern pattern, double mzTolerance) {
DataPoint[] dataPoints = pattern.getDataPoints().clone();
String[] newIsotopeComposition = new String[pattern.getNumberOfDataPoints()];
if (pattern instanceof ExtendedIsotopePattern && ((ExtendedIsotopePattern) pattern).getIsotopeCompositions() != null)
newIsotopeComposition = ((ExtendedIsotopePattern) pattern).getIsotopeCompositions();
for (int i = 0; i < dataPoints.length - 1; i++) {
if (Math.abs(dataPoints[i].getMZ() - dataPoints[i + 1].getMZ()) < mzTolerance) {
double newIntensity = dataPoints[i].getIntensity() + dataPoints[i + 1].getIntensity();
double newMZ = (dataPoints[i].getMZ() * dataPoints[i].getIntensity() + dataPoints[i + 1].getMZ() * dataPoints[i + 1].getIntensity()) / newIntensity;
dataPoints[i + 1] = new SimpleDataPoint(newMZ, newIntensity);
dataPoints[i] = null;
if (pattern instanceof ExtendedIsotopePattern && ((ExtendedIsotopePattern) pattern).getIsotopeCompositions() != null) {
newIsotopeComposition[i + 1] = ((ExtendedIsotopePattern) pattern).getIsotopeComposition(i) + ", " + ((ExtendedIsotopePattern) pattern).getIsotopeComposition(i + 1);
newIsotopeComposition[i] = null;
}
}
}
ArrayList<DataPoint> newDataPoints = new ArrayList<DataPoint>();
for (DataPoint dp : dataPoints) {
if (dp != null)
newDataPoints.add(dp);
}
if (pattern instanceof ExtendedIsotopePattern && ((ExtendedIsotopePattern) pattern).getIsotopeCompositions() != null) {
ArrayList<String> newComp = new ArrayList<String>();
for (String comp : newIsotopeComposition) {
if (comp != null)
newComp.add(comp);
}
return new ExtendedIsotopePattern(newDataPoints.toArray(new DataPoint[0]), pattern.getStatus(), pattern.getDescription(), newComp.toArray(new String[0]));
}
return new SimpleIsotopePattern(newDataPoints.toArray(new DataPoint[0]), pattern.getStatus(), pattern.getDescription());
}
Aggregations