use of net.sf.mzmine.datamodel.impl.SimpleIsotopePattern in project mzmine2 by mzmine.
the class IsotopeGrouperTask method run.
/**
* @see Runnable#run()
*/
public void run() {
setStatus(TaskStatus.PROCESSING);
logger.info("Running isotopic peak grouper on " + peakList);
// We assume source peakList contains one datafile
RawDataFile dataFile = peakList.getRawDataFile(0);
// Create a new deisotoped peakList
deisotopedPeakList = new SimplePeakList(peakList + " " + suffix, peakList.getRawDataFiles());
// Collect all selected charge states
int[] charges = new int[maximumCharge];
for (int i = 0; i < maximumCharge; i++) charges[i] = i + 1;
// Sort peaks by descending height
Feature[] sortedPeaks = peakList.getPeaks(dataFile);
Arrays.sort(sortedPeaks, new PeakSorter(SortingProperty.Height, SortingDirection.Descending));
// Loop through all peaks
totalPeaks = sortedPeaks.length;
for (int ind = 0; ind < totalPeaks; ind++) {
if (isCanceled())
return;
Feature aPeak = sortedPeaks[ind];
// Check if peak was already deleted
if (aPeak == null) {
processedPeaks++;
continue;
}
// Check which charge state fits best around this peak
int bestFitCharge = 0;
int bestFitScore = -1;
Vector<Feature> bestFitPeaks = null;
for (int charge : charges) {
Vector<Feature> fittedPeaks = new Vector<Feature>();
fittedPeaks.add(aPeak);
fitPattern(fittedPeaks, aPeak, charge, sortedPeaks);
int score = fittedPeaks.size();
if ((score > bestFitScore) || ((score == bestFitScore) && (bestFitCharge > charge))) {
bestFitScore = score;
bestFitCharge = charge;
bestFitPeaks = fittedPeaks;
}
}
PeakListRow oldRow = peakList.getPeakRow(aPeak);
assert bestFitPeaks != null;
// isotope, we skip this left the original peak in the feature list.
if (bestFitPeaks.size() == 1) {
deisotopedPeakList.addRow(oldRow);
processedPeaks++;
continue;
}
// Convert the peak pattern to array
Feature[] originalPeaks = bestFitPeaks.toArray(new Feature[0]);
// Create a new SimpleIsotopePattern
DataPoint[] isotopes = new DataPoint[bestFitPeaks.size()];
for (int i = 0; i < isotopes.length; i++) {
Feature p = originalPeaks[i];
isotopes[i] = new SimpleDataPoint(p.getMZ(), p.getHeight());
}
SimpleIsotopePattern newPattern = new SimpleIsotopePattern(isotopes, IsotopePatternStatus.DETECTED, aPeak.toString());
// the lowest m/z peak
if (chooseMostIntense) {
Arrays.sort(originalPeaks, new PeakSorter(SortingProperty.Height, SortingDirection.Descending));
} else {
Arrays.sort(originalPeaks, new PeakSorter(SortingProperty.MZ, SortingDirection.Ascending));
}
Feature newPeak = new SimpleFeature(originalPeaks[0]);
newPeak.setIsotopePattern(newPattern);
newPeak.setCharge(bestFitCharge);
// Keep old ID
int oldID = oldRow.getID();
SimplePeakListRow newRow = new SimplePeakListRow(oldID);
PeakUtils.copyPeakListRowProperties(oldRow, newRow);
newRow.addPeak(dataFile, newPeak);
deisotopedPeakList.addRow(newRow);
// Remove all peaks already assigned to isotope pattern
for (int i = 0; i < sortedPeaks.length; i++) {
if (bestFitPeaks.contains(sortedPeaks[i]))
sortedPeaks[i] = null;
}
// Update completion rate
processedPeaks++;
}
// Add new peakList to the project
project.addPeakList(deisotopedPeakList);
// Load previous applied methods
for (PeakListAppliedMethod proc : peakList.getAppliedMethods()) {
deisotopedPeakList.addDescriptionOfAppliedTask(proc);
}
// Add task description to peakList
deisotopedPeakList.addDescriptionOfAppliedTask(new SimplePeakListAppliedMethod("Isotopic peaks grouper", parameters));
// Remove the original peakList if requested
if (removeOriginal)
project.removePeakList(peakList);
logger.info("Finished isotopic peak grouper on " + peakList);
setStatus(TaskStatus.FINISHED);
}
use of net.sf.mzmine.datamodel.impl.SimpleIsotopePattern in project mzmine2 by mzmine.
the class DPPIsotopeGrouperTask method compressIsotopeDataSets.
/**
* This method generates a single IsotopesDataSet from all detected isotope patterns in the
* results.
*
* @param dataPoints
* @return
*/
private IsotopesDataSet compressIsotopeDataSets(ProcessedDataPoint[] dataPoints) {
List<IsotopePattern> list = new ArrayList<>();
for (ProcessedDataPoint dp : dataPoints) {
if (dp.resultTypeExists(ResultType.ISOTOPEPATTERN)) {
list.add(((DPPIsotopePatternResult) dp.getFirstResultByType(ResultType.ISOTOPEPATTERN)).getValue());
}
}
if (list.isEmpty())
return null;
List<DataPoint> dpList = new ArrayList<>();
for (IsotopePattern pattern : list) {
for (DataPoint dp : pattern.getDataPoints()) dpList.add(dp);
}
if (dpList.isEmpty())
return null;
IsotopePattern full = new SimpleIsotopePattern(dpList.toArray(new DataPoint[0]), IsotopePatternStatus.DETECTED, "Isotope patterns");
return new IsotopesDataSet(full);
}
use of net.sf.mzmine.datamodel.impl.SimpleIsotopePattern in project mzmine2 by mzmine.
the class IsotopePatternUtils method mergeIsotopePatternResults.
// -------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------
// old-new
public static void mergeIsotopePatternResults(ProcessedDataPoint dp) {
if (!dp.resultTypeExists(ResultType.ISOTOPEPATTERN))
return;
List<DPPIsotopePatternResult> patternResults = getIsotopePatternResults(dp);
List<DPPResult<?>> newResults = new ArrayList<>();
for (DPPIsotopePatternResult dpPatternResult : patternResults) {
ProcessedDataPoint[] dpPattern = dpPatternResult.getLinkedDataPoints();
int patternCharge = dpPatternResult.getCharge();
for (ProcessedDataPoint p : dpPattern) {
List<DPPIsotopePatternResult> pPatternResults = getIsotopePatternResults(p);
for (DPPIsotopePatternResult pPatternResult : pPatternResults) {
if (pPatternResult.getCharge() != patternCharge)
continue;
ProcessedDataPoint[] dataPoints = pPatternResult.getLinkedDataPoints();
p.removeResult(pPatternResult);
newResults.add(new DPPIsotopePatternResult(new SimpleIsotopePattern(dataPoints, IsotopePatternStatus.DETECTED, ""), dataPoints, patternCharge));
}
}
}
dp.getAllResultsByType(ResultType.ISOTOPEPATTERN);
dp.addAllResults(newResults);
logger.finest("-------------------------");
for (DPPResult<?> result : newResults) logger.finest("FINAL: " + format.format(dp.getMZ()) + " pattern: " + getResultIsoComp((DPPIsotopePatternResult) result));
// TODO: test
}
use of net.sf.mzmine.datamodel.impl.SimpleIsotopePattern in project mzmine2 by mzmine.
the class IsotopePatternUtils method checkOverlappingIsotopes.
public static IsotopePattern checkOverlappingIsotopes(IsotopePattern pattern, IIsotope[] isotopes, double mergeWidth, double minAbundance) {
DataPoint[] dp = pattern.getDataPoints();
double basemz = dp[0].getMZ();
List<DataPoint> newPeaks = new ArrayList<DataPoint>();
double isotopeBaseMass = 0d;
for (IIsotope isotope : isotopes) {
if (isotope.getNaturalAbundance() > minAbundance) {
isotopeBaseMass = isotope.getExactMass();
logger.info("isotopeBaseMass of " + isotope.getSymbol() + " = " + isotopeBaseMass);
break;
}
}
// loop all new isotopes
for (IIsotope isotope : isotopes) {
if (isotope.getNaturalAbundance() < minAbundance)
continue;
// the difference added by the heavier isotope peak
double possiblemzdiff = isotope.getExactMass() - isotopeBaseMass;
if (possiblemzdiff < 0.000001)
continue;
boolean add = true;
for (DataPoint patternDataPoint : dp) {
// here check for every peak in the pattern, if a new peak would overlap
// if it overlaps good, we dont need to add a new peak
int i = 1;
do {
if (Math.abs(patternDataPoint.getMZ() * i - possiblemzdiff) <= mergeWidth) {
// TODO: maybe we should do a average of the masses? i can'T say if it makes sense,
// since
// we're just looking for isotope mass differences and dont look at the total
// composition,
// so we dont know the intensity ratios
logger.info("possible overlap found: " + i + " * pattern dp = " + patternDataPoint.getMZ() + "\toverlaps with " + isotope.getMassNumber() + isotope.getSymbol() + " (" + (isotopeBaseMass - isotope.getExactMass()) + ")\tdiff: " + Math.abs(patternDataPoint.getMZ() * i - possiblemzdiff));
add = false;
}
i++;
// logger.info("do");
} while (patternDataPoint.getMZ() * i <= possiblemzdiff + mergeWidth && patternDataPoint.getMZ() != 0.0);
}
if (add)
newPeaks.add(new SimpleDataPoint(possiblemzdiff, 1));
}
// DataPoint[] newDataPoints = new SimpleDataPoint[dp.length + newPeaks.size()];
for (DataPoint p : dp) {
newPeaks.add(p);
}
newPeaks.sort((o1, o2) -> {
return Double.compare(o1.getMZ(), o2.getMZ());
});
return new SimpleIsotopePattern(newPeaks.toArray(new DataPoint[0]), IsotopePatternStatus.PREDICTED, "");
}
use of net.sf.mzmine.datamodel.impl.SimpleIsotopePattern 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