use of net.sf.mzmine.datamodel.MassList in project mzmine2 by mzmine.
the class ChromatogramBuilderTask method run.
/**
* @see Runnable#run()
*/
public void run() {
setStatus(TaskStatus.PROCESSING);
logger.info("Started chromatogram builder on " + dataFile);
scans = scanSelection.getMatchingScans(dataFile);
int[] allScanNumbers = scanSelection.getMatchingScanNumbers(dataFile);
totalScans = scans.length;
// Check if the scans are properly ordered by RT
double prevRT = Double.NEGATIVE_INFINITY;
for (Scan s : scans) {
if (s.getRetentionTime() < prevRT) {
setStatus(TaskStatus.ERROR);
final String msg = "Retention time of scan #" + s.getScanNumber() + " is smaller then the retention time of the previous scan." + " Please make sure you only use scans with increasing retention times." + " You can restrict the scan numbers in the parameters, or you can use the Crop filter module";
setErrorMessage(msg);
return;
}
prevRT = s.getRetentionTime();
}
// Create new feature list
newPeakList = new SimplePeakList(dataFile + " " + suffix, dataFile);
Chromatogram[] chromatograms;
HighestDataPointConnector massConnector = new HighestDataPointConnector(dataFile, allScanNumbers, minimumTimeSpan, minimumHeight, mzTolerance);
for (Scan scan : scans) {
if (isCanceled())
return;
MassList massList = scan.getMassList(massListName);
if (massList == null) {
setStatus(TaskStatus.ERROR);
setErrorMessage("Scan " + dataFile + " #" + scan.getScanNumber() + " does not have a mass list " + massListName);
return;
}
DataPoint[] mzValues = massList.getDataPoints();
if (mzValues == null) {
setStatus(TaskStatus.ERROR);
setErrorMessage("Mass list " + massListName + " does not contain m/z values for scan #" + scan.getScanNumber() + " of file " + dataFile);
return;
}
massConnector.addScan(scan.getScanNumber(), mzValues);
processedScans++;
}
chromatograms = massConnector.finishChromatograms();
// Sort the final chromatograms by m/z
Arrays.sort(chromatograms, new PeakSorter(SortingProperty.MZ, SortingDirection.Ascending));
// Add the chromatograms to the new feature list
for (Feature finishedPeak : chromatograms) {
SimplePeakListRow newRow = new SimplePeakListRow(newPeakID);
newPeakID++;
newRow.addPeak(dataFile, finishedPeak);
newPeakList.addRow(newRow);
}
// Add new peaklist to the project
project.addPeakList(newPeakList);
// Add quality parameters to peaks
QualityParameters.calculateQualityParameters(newPeakList);
setStatus(TaskStatus.FINISHED);
logger.info("Finished chromatogram builder on " + dataFile);
}
use of net.sf.mzmine.datamodel.MassList in project mzmine2 by mzmine.
the class ShoulderPeaksFilterTask method run.
/**
* @see Runnable#run()
*/
public void run() {
setStatus(TaskStatus.PROCESSING);
logger.info("Started mass filter on " + dataFile);
scanNumbers = dataFile.getScanNumbers();
totalScans = scanNumbers.length;
// Check if we have at least one scan with a mass list of given name
boolean haveMassList = false;
for (int i = 0; i < totalScans; i++) {
Scan scan = dataFile.getScan(scanNumbers[i]);
MassList massList = scan.getMassList(massListName);
if (massList != null) {
haveMassList = true;
break;
}
}
if (!haveMassList) {
setStatus(TaskStatus.ERROR);
setErrorMessage(dataFile.getName() + " has no mass list called '" + massListName + "'");
return;
}
// Process all scans
for (int i = 0; i < totalScans; i++) {
if (isCanceled())
return;
Scan scan = dataFile.getScan(scanNumbers[i]);
MassList massList = scan.getMassList(massListName);
// Skip those scans which do not have a mass list of given name
if (massList == null) {
processedScans++;
continue;
}
DataPoint[] mzPeaks = massList.getDataPoints();
DataPoint[] newMzPeaks = ShoulderPeaksFilter.filterMassValues(mzPeaks, parameters);
SimpleMassList newMassList = new SimpleMassList(massListName + " " + suffix, scan, newMzPeaks);
scan.addMassList(newMassList);
// Remove old mass list
if (autoRemove)
scan.removeMassList(massList);
processedScans++;
}
setStatus(TaskStatus.FINISHED);
logger.info("Finished shoulder peaks filter on " + dataFile);
}
use of net.sf.mzmine.datamodel.MassList in project mzmine2 by mzmine.
the class MsMsSpectraMergeModule method mergeConsecutiveScans.
/**
* Internal method for merging a list of consecutive MS/MS scans.
*
* @param scans MS/MS scans with their precursor information
* @param massList name of the mass list to use when extracting peaks
* @param scoreModel scoring model to use when removing low quality MS/MS and selecting the best
* quality MS/MS
* @return merged spectrum
*/
protected MergedSpectrum mergeConsecutiveScans(ParameterSet parameters, FragmentScan scans, String massList, Ms2QualityScoreModel scoreModel) {
int totalNumberOfScans = scans.ms2ScanNumbers.length;
/*
* find scan with best quality
*/
final double[] scores = scoreModel.calculateQualityScore(scans);
int best = 0;
for (int k = 1; k < scores.length; ++k) {
if (scores[k] > scores[best]) {
best = k;
}
}
if (scores[best] <= 0)
return MergedSpectrum.empty(totalNumberOfScans);
final List<Scan> scansToMerge = new ArrayList<>();
scansToMerge.add(scans.origin.getScan(scans.ms2ScanNumbers[best]));
final Scan firstScan = scansToMerge.get(0);
final MassList firstML = firstScan.getMassList(massList);
if (firstML == null)
throw new RuntimeException("Scan " + firstScan.getDataFile().getName() + "#" + firstScan.getScanNumber() + " does not have a mass list " + massList);
if (firstML.getDataPoints().length <= 1)
return MergedSpectrum.empty(totalNumberOfScans);
/*
* remove scans which are considerably worse than the best scan
*/
final double scoreThreshold = best / 5d;
for (int i = 1; i < scores.length; ++i) {
int k = best - i;
if (k >= 0 && scores[k] > scoreThreshold) {
scansToMerge.add(scans.origin.getScan(scans.ms2ScanNumbers[k]));
}
k = best + i;
if (k < scores.length && scores[k] > scoreThreshold) {
scansToMerge.add(scans.origin.getScan(scans.ms2ScanNumbers[k]));
}
}
if (scansToMerge.size() == 1) {
final MergedSpectrum single = new MergedSpectrum(scansToMerge.get(0), massList);
single.bestFragmentScanScore = scores[best];
single.removedScansByLowQuality = scores.length - 1;
return single;
}
/*
* merge every scan if its cosine is above the cosine threshold
*/
final double cosineThreshold = parameters.getParameter(MsMsSpectraMergeParameters.COSINE_PARAMETER).getValue();
final MZTolerance mzTolerance = parameters.getParameter(MsMsSpectraMergeParameters.MASS_ACCURACY).getValue();
final MzMergeMode mzMergeMode = parameters.getParameter(MsMsSpectraMergeParameters.MZ_MERGE_MODE).getValue();
final IntensityMergeMode intensityMergeMode = parameters.getParameter(MsMsSpectraMergeParameters.INTENSITY_MERGE_MODE).getValue();
MergedSpectrum initial = new MergedSpectrum(scansToMerge.get(0), massList);
initial.bestFragmentScanScore = best;
final double lowestMassToConsider = Math.min(50d, scans.feature.getMZ() - 50d);
final DataPoint[] initialMostIntense = ScanUtils.extractMostIntensePeaksAcrossMassRange(initial.data, Range.closed(lowestMassToConsider, 150d), 6);
double lowestIntensityToConsider;
final int mostIntensPeakWithin = ScanUtils.findMostIntensePeakWithin(initialMostIntense, Range.closed(lowestMassToConsider, scans.feature.getMZ()));
if (mostIntensPeakWithin >= 0)
lowestIntensityToConsider = 0.005d * initialMostIntense[mostIntensPeakWithin].getIntensity();
else
lowestIntensityToConsider = 0d;
Range<Double> cosineRange = Range.closed(lowestMassToConsider, scans.feature.getMZ() - 20);
final double initialCosine = ScanUtils.probabilityProductUnnormalized(initialMostIntense, initialMostIntense, mzTolerance, lowestIntensityToConsider, cosineRange);
for (int k = 1; k < scansToMerge.size(); ++k) {
Scan scan = scansToMerge.get(k);
if (!(scan.getPolarity().equals(initial.polarity) && scan.getPrecursorCharge() == initial.precursorCharge && mzTolerance.checkWithinTolerance(scan.getPrecursorMZ(), initial.precursorMz))) {
LoggerFactory.getLogger(MsMsSpectraMergeModule.class).warn("Scan " + scan.getScanNumber() + " cannot be merged: it seems to belong to a different feature.");
continue;
}
DataPoint[] dataPoints = scan.getMassList(massList).getDataPoints();
final DataPoint[] mostIntense = ScanUtils.extractMostIntensePeaksAcrossMassRange(dataPoints, cosineRange, 6);
final double norm = ScanUtils.probabilityProductUnnormalized(mostIntense, mostIntense, mzTolerance, lowestIntensityToConsider, cosineRange);
final double cosine = ScanUtils.probabilityProductUnnormalized(initialMostIntense, mostIntense, mzTolerance, lowestIntensityToConsider, cosineRange) / Math.sqrt(norm * initialCosine);
if (cosine >= cosineThreshold) {
initial = merge(initial, scan, dataPoints, mzMergeMode, intensityMergeMode, mzTolerance);
} else {
initial.removedScansByLowCosine++;
}
}
initial.removedScansByLowQuality += (totalNumberOfScans - scansToMerge.size());
return initial;
}
use of net.sf.mzmine.datamodel.MassList in project mzmine2 by mzmine.
the class GnpsFbmnExportTask method export.
private void export(PeakList peakList, FileWriter writer, File curFile) throws IOException {
final String newLine = System.lineSeparator();
for (PeakListRow row : peakList.getRows()) {
String rowID = Integer.toString(row.getID());
String retTimeInSeconds = Double.toString(Math.round(row.getAverageRT() * 60 * 100) / 100.);
// Get the MS/MS scan number
Feature bestPeak = row.getBestPeak();
if (bestPeak == null)
continue;
int msmsScanNumber = bestPeak.getMostIntenseFragmentScanNumber();
if (rowID != null) {
PeakListRow copyRow = copyPeakRow(row);
// Best peak always exists, because feature list row has at least one peak
bestPeak = copyRow.getBestPeak();
// Get the MS/MS scan number
msmsScanNumber = bestPeak.getMostIntenseFragmentScanNumber();
while (msmsScanNumber < 1) {
copyRow.removePeak(bestPeak.getDataFile());
if (copyRow.getPeaks().length == 0)
break;
bestPeak = copyRow.getBestPeak();
msmsScanNumber = bestPeak.getMostIntenseFragmentScanNumber();
}
}
if (msmsScanNumber >= 1) {
// MS/MS scan must exist, because msmsScanNumber was > 0
Scan msmsScan = bestPeak.getDataFile().getScan(msmsScanNumber);
MassList massList = msmsScan.getMassList(massListName);
if (massList == null) {
MZmineCore.getDesktop().displayErrorMessage(MZmineCore.getDesktop().getMainWindow(), "There is no mass list called " + massListName + " for MS/MS scan #" + msmsScanNumber + " (" + bestPeak.getDataFile() + ")");
return;
}
writer.write("BEGIN IONS" + newLine);
if (rowID != null)
writer.write("FEATURE_ID=" + rowID + newLine);
String mass = Double.toString(Math.round(row.getAverageMZ() * 10000) / 10000.);
if (mass != null)
writer.write("PEPMASS=" + mass + newLine);
if (rowID != null) {
writer.write("SCANS=" + rowID + newLine);
writer.write("RTINSECONDS=" + retTimeInSeconds + newLine);
}
int msmsCharge = msmsScan.getPrecursorCharge();
String msmsPolarity = msmsScan.getPolarity().asSingleChar();
if (msmsPolarity.equals("0"))
msmsPolarity = "";
if (msmsCharge == 0) {
msmsCharge = 1;
msmsPolarity = "";
}
writer.write("CHARGE=" + msmsCharge + msmsPolarity + newLine);
writer.write("MSLEVEL=2" + newLine);
DataPoint[] dataPoints = massList.getDataPoints();
if (mergeParameters != null) {
MsMsSpectraMergeModule merger = MZmineCore.getModuleInstance(MsMsSpectraMergeModule.class);
MergedSpectrum spectrum = merger.getBestMergedSpectrum(mergeParameters, row, massListName);
if (spectrum != null) {
dataPoints = spectrum.data;
writer.write("MERGED_STATS=");
writer.write(spectrum.getMergeStatsDescription());
writer.write(newLine);
}
}
for (DataPoint peak : dataPoints) {
writer.write(peak.getMZ() + " " + peak.getIntensity() + newLine);
}
writer.write("END IONS" + newLine);
writer.write(newLine);
}
}
}
use of net.sf.mzmine.datamodel.MassList in project mzmine2 by mzmine.
the class RowsSpectralMatchTask method getDataPoints.
/**
* Thresholded masslist
*
* @param row
* @return
* @throws MissingMassListException
*/
private DataPoint[] getDataPoints(Scan scan, boolean noiseFilter) throws MissingMassListException {
if (scan == null || scan.getMassList(massListName) == null) {
return new DataPoint[0];
}
MassList masses = scan.getMassList(massListName);
DataPoint[] dps = masses.getDataPoints();
return noiseFilter ? ScanUtils.getFiltered(dps, noiseLevel) : dps;
}
Aggregations