use of net.sf.mzmine.datamodel.impl.SimplePeakList in project mzmine2 by mzmine.
the class NeutralLossFilterTask method run.
@Override
public void run() {
setStatus(TaskStatus.PROCESSING);
totalRows = peakList.getNumberOfRows();
ArrayList<Double> diff = setUpDiff();
if (diff == null || Double.compare(dMassLoss, 0.0d) == 0) {
setErrorMessage("Could not set up neutral loss. Mass loss could not be calculated from the formula or is 0.0");
setStatus(TaskStatus.ERROR);
return;
}
if (suffix.equals("auto")) {
if (molecule.equals(""))
suffix = " NL: " + dMassLoss + " RTtol: " + rtTolerance.getTolerance() + "_results";
else
suffix = " NL (" + molecule + "): " + dMassLoss + " RTtol: " + rtTolerance.getTolerance() + "_results";
}
// 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.get(diff.size() - 1).doubleValue());
if (groupedPeaks.size() < 2) {
finishedRows++;
continue;
}
// this will store row indexes of
ResultBuffer[] resultBuffer = new ResultBuffer[diff.size()];
// and mz
for (// resultBuffer[i] index will represent Isotope[i] (if
int a = 0; // resultBuffer[i] index will represent Isotope[i] (if
a < diff.size(); // resultBuffer[i] index will represent Isotope[i] (if
a++) // numAtoms = 0)
// [0] will be the isotope with lowest mass#
resultBuffer[a] = new ResultBuffer();
for (// go through all possible peaks
int j = 0; // go through all possible peaks
j < groupedPeaks.size(); // go through all possible peaks
j++) {
for (// check for each peak if it is a possible feature for
int k = 0; // check for each peak if it is a possible feature for
k < diff.size(); // check for each peak if it is a possible feature for
k++) // every diff[](isotope)
{
// k represents the isotope number the peak will be a candidate for
if (mzTolerance.checkWithinTolerance(groupedPeaks.get(0).getAverageMZ() + diff.get(k), groupedPeaks.get(j).getAverageMZ())) {
// this will automatically add groupedPeaks[0] to the list -> isotope with
// lowest mass
// +1 result for isotope k
resultBuffer[k].addFound();
// row in groupedPeaks[]
resultBuffer[k].addRow(j);
resultBuffer[k].addID(groupedPeaks.get(j).getID());
}
}
}
if (// this means that for every isotope we expected to find,
!checkIfAllTrue(resultBuffer)) // we found one or more possible features
{
finishedRows++;
continue;
}
Candidates candidates = new Candidates(diff.size(), minHeight, mzTolerance, plh);
for (// reminder: resultBuffer.length = diff.size()
int k = 0; // reminder: resultBuffer.length = diff.size()
k < resultBuffer.length; // reminder: resultBuffer.length = diff.size()
k++) {
for (int l = 0; l < resultBuffer[k].getFoundCount(); l++) {
// k represents index resultBuffer[k] and thereby the isotope number
// l represents the number of results in resultBuffer[k]
candidates.get(k).checkForBetterRating(groupedPeaks, 0, resultBuffer[k].getRow(l), diff.get(k), minRating);
}
}
if (!checkIfAllTrue(candidates.getCandidates())) {
finishedRows++;
// jump to next i
continue;
}
String comParent = "", comChild = "";
PeakListRow originalChild = getRowFromCandidate(candidates, 0, plh);
if (originalChild == null) {
finishedRows++;
continue;
}
PeakListRow child = copyPeakRow(originalChild);
if (resultMap.containsID(child.getID()))
comChild += resultMap.getRowByID(child.getID()).getComment();
comChild += "Parent ID: " + candidates.get(1).getCandID();
addComment(child, comChild);
List<PeakListRow> rowBuffer = new ArrayList<PeakListRow>();
boolean allPeaksAddable = true;
rowBuffer.add(child);
for (// we skip k=0 because == groupedPeaks[0] which we
int k = 1; // we skip k=0 because == groupedPeaks[0] which we
k < candidates.size(); // we skip k=0 because == groupedPeaks[0] which we
k++) // added before
{
PeakListRow originalParent = getRowFromCandidate(candidates, 1, plh);
if (originalParent == null) {
allPeaksAddable = false;
continue;
}
PeakListRow parent = copyPeakRow(originalParent);
if (resultMap.containsID(parent.getID()))
comParent += resultMap.getRowByID(parent.getID()).getComment();
comParent += ("[--IS PARENT-- child ID: " + child.getID() + " ] | ");
addComment(parent, comParent);
addComment(child, " m/z shift(ppm): " + round(((parent.getAverageMZ() - child.getAverageMZ()) - diff.get(1)) / parent.getAverageMZ() * 1E6, 2) + " ");
rowBuffer.add(parent);
}
if (allPeaksAddable)
for (PeakListRow row : rowBuffer) 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.impl.SimplePeakList in project mzmine2 by mzmine.
the class PeakFilterTask method filterPeakList.
/**
* Filter the feature list.
*
* @param peakList feature list to filter.
* @return a new feature list with entries of the original feature list that pass the filtering.
*/
private PeakList filterPeakList(final PeakList peakList) {
// Make a copy of the peakList
final PeakList newPeakList = new SimplePeakList(peakList.getName() + ' ' + parameters.getParameter(RowsFilterParameters.SUFFIX).getValue(), peakList.getRawDataFiles());
// Get parameters - which filters are active
final boolean filterByDuration = parameters.getParameter(PeakFilterParameters.PEAK_DURATION).getValue();
final boolean filterByArea = parameters.getParameter(PeakFilterParameters.PEAK_AREA).getValue();
final boolean filterByHeight = parameters.getParameter(PeakFilterParameters.PEAK_HEIGHT).getValue();
final boolean filterByDatapoints = parameters.getParameter(PeakFilterParameters.PEAK_DATAPOINTS).getValue();
final boolean filterByFWHM = parameters.getParameter(PeakFilterParameters.PEAK_FWHM).getValue();
final boolean filterByTailingFactor = parameters.getParameter(PeakFilterParameters.PEAK_TAILINGFACTOR).getValue();
final boolean filterByAsymmetryFactor = parameters.getParameter(PeakFilterParameters.PEAK_ASYMMETRYFACTOR).getValue();
final boolean filterByMS2 = parameters.getParameter(PeakFilterParameters.MS2_Filter).getValue();
// Loop through all rows in feature list
final PeakListRow[] rows = peakList.getRows();
totalRows = rows.length;
for (processedRows = 0; !isCanceled() && processedRows < totalRows; processedRows++) {
final PeakListRow row = rows[processedRows];
final RawDataFile[] rawdatafiles = row.getRawDataFiles();
int totalRawDataFiles = rawdatafiles.length;
boolean[] keepPeak = new boolean[totalRawDataFiles];
for (int i = 0; i < totalRawDataFiles; i++) {
// Peak values
keepPeak[i] = true;
final Feature peak = row.getPeak(rawdatafiles[i]);
final double peakDuration = peak.getRawDataPointsRTRange().upperEndpoint() - peak.getRawDataPointsRTRange().lowerEndpoint();
final double peakArea = peak.getArea();
final double peakHeight = peak.getHeight();
final int peakDatapoints = peak.getScanNumbers().length;
final int msmsScanNumber = peak.getMostIntenseFragmentScanNumber();
Double peakFWHM = peak.getFWHM();
Double peakTailingFactor = peak.getTailingFactor();
Double peakAsymmetryFactor = peak.getAsymmetryFactor();
if (peakFWHM == null) {
peakFWHM = -1.0;
}
if (peakTailingFactor == null) {
peakTailingFactor = -1.0;
}
if (peakAsymmetryFactor == null) {
peakAsymmetryFactor = -1.0;
}
// Check Duration
if (filterByDuration) {
final Range<Double> durationRange = parameters.getParameter(PeakFilterParameters.PEAK_DURATION).getEmbeddedParameter().getValue();
if (!durationRange.contains(peakDuration)) {
// Mark peak to be removed
keepPeak[i] = false;
}
}
// Check Area
if (filterByArea) {
final Range<Double> areaRange = parameters.getParameter(PeakFilterParameters.PEAK_AREA).getEmbeddedParameter().getValue();
if (!areaRange.contains(peakArea)) {
// Mark peak to be removed
keepPeak[i] = false;
}
}
// Check Height
if (filterByHeight) {
final Range<Double> heightRange = parameters.getParameter(PeakFilterParameters.PEAK_HEIGHT).getEmbeddedParameter().getValue();
if (!heightRange.contains(peakHeight)) {
// Mark peak to be removed
keepPeak[i] = false;
}
}
// Check # Data Points
if (filterByDatapoints) {
final Range<Integer> datapointsRange = parameters.getParameter(PeakFilterParameters.PEAK_DATAPOINTS).getEmbeddedParameter().getValue();
if (!datapointsRange.contains(peakDatapoints)) {
// Mark peak to be removed
keepPeak[i] = false;
}
}
// Check FWHM
if (filterByFWHM) {
final Range<Double> fwhmRange = parameters.getParameter(PeakFilterParameters.PEAK_FWHM).getEmbeddedParameter().getValue();
if (!fwhmRange.contains(peakFWHM)) {
// Mark peak to be removed
keepPeak[i] = false;
}
}
// Check Tailing Factor
if (filterByTailingFactor) {
final Range<Double> tailingRange = parameters.getParameter(PeakFilterParameters.PEAK_TAILINGFACTOR).getEmbeddedParameter().getValue();
if (!tailingRange.contains(peakTailingFactor)) {
// Mark peak to be removed
keepPeak[i] = false;
}
}
// Check height
if (filterByAsymmetryFactor) {
final Range<Double> asymmetryRange = parameters.getParameter(PeakFilterParameters.PEAK_ASYMMETRYFACTOR).getEmbeddedParameter().getValue();
if (!asymmetryRange.contains(peakAsymmetryFactor)) {
// Mark peak to be removed
keepPeak[i] = false;
}
}
// Check MS/MS filter
if (filterByMS2) {
if (msmsScanNumber < 1)
keepPeak[i] = false;
}
}
// empty row?
boolean isEmpty = Booleans.asList(keepPeak).stream().allMatch(keep -> !keep);
if (!isEmpty)
newPeakList.addRow(copyPeakRow(row, keepPeak));
}
return newPeakList;
}
use of net.sf.mzmine.datamodel.impl.SimplePeakList in project mzmine2 by mzmine.
the class PeakComparisonRowFilterTask method filterPeakListRows.
/**
* Filter the feature list rows by comparing peaks within a row.
*
* @param peakList feature list to filter.
* @return a new feature list with rows of the original feature list that pass the filtering.
*/
private PeakList filterPeakListRows(final PeakList peakList) {
// Create new feature list.
final PeakList newPeakList = new SimplePeakList(peakList.getName() + ' ' + parameters.getParameter(PeakComparisonRowFilterParameters.SUFFIX).getValue(), peakList.getRawDataFiles());
// Copy previous applied methods.
for (final PeakListAppliedMethod method : peakList.getAppliedMethods()) {
newPeakList.addDescriptionOfAppliedTask(method);
}
// Add task description to peakList.
newPeakList.addDescriptionOfAppliedTask(new SimplePeakListAppliedMethod(getTaskDescription(), parameters));
// Get parameters.
final boolean evalutateFoldChange = parameters.getParameter(PeakComparisonRowFilterParameters.FOLD_CHANGE).getValue();
final boolean evalutatePPMdiff = parameters.getParameter(PeakComparisonRowFilterParameters.MZ_PPM_DIFF).getValue();
final boolean evalutateRTdiff = parameters.getParameter(PeakComparisonRowFilterParameters.RT_DIFF).getValue();
final int columnIndex1 = parameters.getParameter(PeakComparisonRowFilterParameters.COLUMN_INDEX_1).getValue();
final int columnIndex2 = parameters.getParameter(PeakComparisonRowFilterParameters.COLUMN_INDEX_2).getValue();
final Range<Double> foldChangeRange = parameters.getParameter(PeakComparisonRowFilterParameters.FOLD_CHANGE).getEmbeddedParameter().getValue();
final Range<Double> ppmDiffRange = parameters.getParameter(PeakComparisonRowFilterParameters.FOLD_CHANGE).getEmbeddedParameter().getValue();
final Range<Double> rtDiffRange = parameters.getParameter(PeakComparisonRowFilterParameters.FOLD_CHANGE).getEmbeddedParameter().getValue();
// Setup variables
final PeakListRow[] rows = peakList.getRows();
RawDataFile rawDataFile1;
RawDataFile rawDataFile2;
Feature peak1;
Feature peak2;
totalRows = rows.length;
final RawDataFile[] rawDataFiles = peakList.getRawDataFiles();
boolean allCriteriaMatched = true;
// doesn't exist.
if (columnIndex1 > rawDataFiles.length) {
setErrorMessage("Column 1 set too large.");
setStatus(TaskStatus.ERROR);
return null;
}
if (columnIndex2 > rawDataFiles.length) {
setErrorMessage("Column 2 set too large.");
setStatus(TaskStatus.ERROR);
return null;
}
// Loop over the rows & filter
for (processedRows = 0; !isCanceled() && processedRows < totalRows; processedRows++) {
if (isCanceled())
return null;
allCriteriaMatched = true;
// Default value in case of null peak
double peak1Area = 1.0;
double peak2Area = 1.0;
double peak1MZ = -1.0;
double peak2MZ = -1.0;
double peak1RT = -1.0;
double peak2RT = -1.0;
double foldChange = 0.0;
double ppmDiff = 0.0;
double rtDiff = 0.0;
final PeakListRow row = rows[processedRows];
rawDataFile1 = rawDataFiles[columnIndex1];
rawDataFile2 = rawDataFiles[columnIndex2];
peak1 = row.getPeak(rawDataFile1);
peak2 = row.getPeak(rawDataFile2);
if (peak1 != null) {
peak1Area = peak1.getArea();
peak1MZ = peak1.getMZ();
peak1RT = peak1.getRT();
}
if (peak2 != null) {
peak2Area = peak2.getArea();
peak2MZ = peak2.getMZ();
peak2RT = peak2.getRT();
}
// Fold change criteria checking.
if (evalutateFoldChange) {
foldChange = Math.log(peak1Area / peak2Area) / Math.log(2);
if (!foldChangeRange.contains(foldChange))
allCriteriaMatched = false;
// PPM difference evaluation
if (evalutatePPMdiff) {
ppmDiff = (peak1MZ - peak2MZ) / peak1MZ * 1E6;
if (!ppmDiffRange.contains(ppmDiff))
allCriteriaMatched = false;
}
// RT difference evaluation
if (evalutateRTdiff) {
rtDiff = peak1RT - peak2RT;
if (!rtDiffRange.contains(rtDiff))
allCriteriaMatched = false;
}
}
// Good row?
if (allCriteriaMatched)
newPeakList.addRow(copyPeakRow(row));
}
return newPeakList;
}
use of net.sf.mzmine.datamodel.impl.SimplePeakList in project mzmine2 by mzmine.
the class DuplicateFilterTask method filterDuplicatePeakListRows.
/**
* Filter our duplicate feature list rows.
*
* @param origPeakList the original feature list.
* @param suffix the suffix to apply to the new feature list name.
* @param mzTolerance m/z tolerance.
* @param rtTolerance RT tolerance.
* @param requireSameId must duplicate peaks have the same identities?
* @return the filtered feature list.
*/
private PeakList filterDuplicatePeakListRows(final PeakList origPeakList, final String suffix, final MZTolerance mzTolerance, final RTTolerance rtTolerance, final boolean requireSameId, FilterMode mode) {
final PeakListRow[] peakListRows = origPeakList.getRows();
final int rowCount = peakListRows.length;
RawDataFile[] rawFiles = origPeakList.getRawDataFiles();
// Create the new feature list.
final PeakList newPeakList = new SimplePeakList(origPeakList + " " + suffix, origPeakList.getRawDataFiles());
// sort rows
if (mode.equals(FilterMode.OLD_AVERAGE))
Arrays.sort(peakListRows, new PeakListRowSorter(SortingProperty.Area, SortingDirection.Descending));
else
Arrays.sort(peakListRows, new PeakListRowSorter(SortingProperty.ID, SortingDirection.Ascending));
// filter by average mz and rt
boolean filterByAvgRTMZ = !mode.equals(FilterMode.SINGLE_FEATURE);
// Loop through all feature list rows
processedRows = 0;
int n = 0;
totalRows = rowCount;
for (int firstRowIndex = 0; !isCanceled() && firstRowIndex < rowCount; firstRowIndex++) {
final PeakListRow mainRow = peakListRows[firstRowIndex];
if (mainRow != null) {
// copy first row
PeakListRow firstRow = copyRow(mainRow);
for (int secondRowIndex = firstRowIndex + 1; !isCanceled() && secondRowIndex < rowCount; secondRowIndex++) {
final PeakListRow secondRow = peakListRows[secondRowIndex];
if (secondRow != null) {
// Compare identifications
final boolean sameID = !requireSameId || PeakUtils.compareIdentities(firstRow, secondRow);
boolean sameMZRT = // average or single feature
filterByAvgRTMZ ? checkSameAverageRTMZ(firstRow, secondRow, mzTolerance, rtTolerance) : checkSameSingleFeatureRTMZ(rawFiles, firstRow, secondRow, mzTolerance, rtTolerance);
// Duplicate peaks?
if (sameID && sameMZRT) {
// create consensus row in new filter
if (!mode.equals(FilterMode.OLD_AVERAGE)) {
// copy all detected features of row2 into row1
// to exchange gap-filled against detected features
createConsensusFirstRow(rawFiles, firstRow, secondRow);
}
// second row deleted
n++;
peakListRows[secondRowIndex] = null;
}
}
}
// add to new list
newPeakList.addRow(firstRow);
}
processedRows++;
}
// finalize
if (!isCanceled()) {
// Load previous applied methods.
for (final PeakListAppliedMethod method : origPeakList.getAppliedMethods()) {
newPeakList.addDescriptionOfAppliedTask(method);
}
// Add task description to peakList
newPeakList.addDescriptionOfAppliedTask(new SimplePeakListAppliedMethod("Duplicate feature list rows filter", parameters));
LOG.info("Removed " + n + " duplicate rows");
}
return newPeakList;
}
use of net.sf.mzmine.datamodel.impl.SimplePeakList in project mzmine2 by mzmine.
the class CameraSearchTask method groupPeaksByIsotope.
/**
* Uses Isotope-field in PeakIdentity to group isotopes and build spectrum
*
* @param peakList PeakList object
* @return new PeakList object
*/
private PeakList groupPeaksByIsotope(PeakList peakList) {
// Create new feature list.
final PeakList combinedPeakList = new SimplePeakList(peakList + " " + parameters.getParameter(CameraSearchParameters.SUFFIX).getValue(), peakList.getRawDataFiles());
// Load previous applied methods.
for (final PeakList.PeakListAppliedMethod method : peakList.getAppliedMethods()) {
combinedPeakList.addDescriptionOfAppliedTask(method);
}
// Add task description to feature list.
combinedPeakList.addDescriptionOfAppliedTask(new SimplePeakListAppliedMethod("Bioconductor CAMERA", parameters));
// ------------------------------------------------
// Find unique isotopes belonging to the same group
// ------------------------------------------------
Set<String> isotopeGroups = new HashSet<>();
for (PeakListRow row : peakList.getRows()) {
PeakIdentity identity = row.getPreferredPeakIdentity();
if (identity == null)
continue;
String isotope = identity.getPropertyValue("Isotope");
if (isotope == null)
continue;
String isotopeGroup = isotope.substring(1, isotope.indexOf("]"));
if (isotopeGroup == null || isotopeGroup.length() == 0)
continue;
isotopeGroups.add(isotopeGroup);
}
List<PeakListRow> groupRows = new ArrayList<>();
Set<String> groupNames = new HashSet<>();
Map<Double, Double> spectrum = new HashMap<>();
List<PeakListRow> newPeakListRows = new ArrayList<>();
for (String isotopeGroup : isotopeGroups) {
// -----------------------------------------
// Find all peaks belonging to isotopeGroups
// -----------------------------------------
groupRows.clear();
groupNames.clear();
spectrum.clear();
int minLength = Integer.MAX_VALUE;
PeakListRow groupRow = null;
for (PeakListRow row : peakList.getRows()) {
PeakIdentity identity = row.getPreferredPeakIdentity();
if (identity == null)
continue;
String isotope = identity.getPropertyValue("Isotope");
if (isotope == null)
continue;
String isoGroup = isotope.substring(1, isotope.indexOf("]"));
if (isoGroup == null)
continue;
if (isoGroup.equals(isotopeGroup)) {
groupRows.add(row);
groupNames.add(identity.getName());
spectrum.put(row.getAverageMZ(), row.getAverageHeight());
if (isoGroup.length() < minLength) {
minLength = isoGroup.length();
groupRow = row;
}
}
}
// Skip peaks that have different identity names (belong to different pcgroup)
if (groupRow == null || groupNames.size() != 1)
continue;
if (groupRow == null)
continue;
PeakIdentity identity = groupRow.getPreferredPeakIdentity();
if (identity == null)
continue;
DataPoint[] dataPoints = new DataPoint[spectrum.size()];
int count = 0;
for (Entry<Double, Double> e : spectrum.entrySet()) dataPoints[count++] = new SimpleDataPoint(e.getKey(), e.getValue());
IsotopePattern pattern = new SimpleIsotopePattern(dataPoints, IsotopePatternStatus.PREDICTED, "Spectrum");
groupRow.getBestPeak().setIsotopePattern(pattern);
// combinedPeakList.addRow(groupRow);
newPeakListRows.add(groupRow);
}
if (includeSingletons) {
for (PeakListRow row : peakList.getRows()) {
PeakIdentity identity = row.getPreferredPeakIdentity();
if (identity == null)
continue;
String isotope = identity.getPropertyValue("Isotope");
if (isotope == null || isotope.length() == 0) {
DataPoint[] dataPoints = new DataPoint[1];
dataPoints[0] = new SimpleDataPoint(row.getAverageMZ(), row.getAverageHeight());
IsotopePattern pattern = new SimpleIsotopePattern(dataPoints, IsotopePatternStatus.PREDICTED, "Spectrum");
row.getBestPeak().setIsotopePattern(pattern);
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) combinedPeakList.addRow(row);
return combinedPeakList;
}
Aggregations