use of net.sf.mzmine.datamodel.DataPoint in project mzmine2 by mzmine.
the class IsotopePatternCalculator method calculateIsotopePattern.
public static IsotopePattern calculateIsotopePattern(IMolecularFormula cdkFormula, double minAbundance, double mergeWidth, int charge, PolarityType polarity, boolean storeFormula) {
// TODO: check if the formula is not too big (>100 of a single atom?).
// if so, just cancel the prediction
// Set the minimum abundance of isotope
// TODO: in the CDK minAbundance is now called minIntensity and refers to the relative intensity
// in the isotope pattern, should change it here, too
IsotopePatternGenerator generator = new IsotopePatternGenerator(minAbundance);
generator.setMinResolution(mergeWidth);
generator.setStoreFormulas(storeFormula);
org.openscience.cdk.formula.IsotopePattern pattern = generator.getIsotopes(cdkFormula);
int numOfIsotopes = pattern.getNumberOfIsotopes();
DataPoint[] dataPoints = new DataPoint[numOfIsotopes];
String[] isotopeComposition = new String[numOfIsotopes];
for (int i = 0; i < numOfIsotopes; i++) {
IsotopeContainer isotope = pattern.getIsotope(i);
// For each unit of charge, we have to add or remove a mass of a
// single electron. If the charge is positive, we remove electron
// mass. If the charge is negative, we add it.
double mass = isotope.getMass() + (polarity.getSign() * -1 * charge * ELECTRON_MASS);
if (charge != 0)
mass /= charge;
double intensity = isotope.getIntensity();
dataPoints[i] = new SimpleDataPoint(mass, intensity);
if (storeFormula)
isotopeComposition[i] = formatCDKString(isotope.toString());
}
String formulaString = MolecularFormulaManipulator.getString(cdkFormula);
if (storeFormula)
return new ExtendedIsotopePattern(dataPoints, IsotopePatternStatus.PREDICTED, formulaString, isotopeComposition);
else
return new SimpleIsotopePattern(dataPoints, IsotopePatternStatus.PREDICTED, formulaString);
}
use of net.sf.mzmine.datamodel.DataPoint in project mzmine2 by mzmine.
the class SQLExportTask method exportPeakListRow.
private void exportPeakListRow(PeakListRow row) throws SQLException {
// Cancel?
if (isCanceled()) {
return;
}
// Value for looping through raw data files
boolean loopDataFiles = false;
StringBuilder sql = new StringBuilder();
sql.append("INSERT INTO ");
sql.append(tableName);
sql.append(" (");
for (int i = 0; i < exportColumns.getRowCount(); i++) {
sql.append(exportColumns.getValueAt(i, 0));
if (i < exportColumns.getRowCount() - 1)
sql.append(",");
}
sql.append(" ) VALUES (");
for (int i = 0; i < exportColumns.getRowCount(); i++) {
sql.append("?");
if (i < exportColumns.getRowCount() - 1)
sql.append(",");
}
sql.append(")");
PreparedStatement statement = dbConnection.prepareStatement(sql.toString());
if (row == null) {
for (int i = 0; i < exportColumns.getRowCount(); i++) {
SQLExportDataType dataType = (SQLExportDataType) exportColumns.getValueAt(i, 1);
String dataValue = (String) exportColumns.getValueAt(i, 2);
switch(dataType) {
case CONSTANT:
statement.setString(i + 1, dataValue);
break;
case RAWFILE:
RawDataFile[] rawdatafiles = peakList.getRawDataFiles();
statement.setString(i + 1, rawdatafiles[0].getName());
break;
default:
statement.setString(i + 1, null);
break;
}
}
statement.executeUpdate();
} else {
for (RawDataFile rawDataFile : row.getRawDataFiles()) {
Feature peak = row.getPeak(rawDataFile);
for (int i = 0; i < exportColumns.getRowCount(); i++) {
SQLExportDataType dataType = (SQLExportDataType) exportColumns.getValueAt(i, 1);
String dataValue = (String) exportColumns.getValueAt(i, 2);
switch(dataType) {
case CONSTANT:
statement.setString(i + 1, dataValue);
break;
case MZ:
statement.setDouble(i + 1, row.getAverageMZ());
break;
case RT:
statement.setDouble(i + 1, row.getAverageRT());
break;
case ID:
statement.setInt(i + 1, row.getID());
break;
case PEAKCHARGE:
statement.setDouble(i + 1, peak.getCharge());
loopDataFiles = true;
break;
case PEAKDURATION:
statement.setDouble(i + 1, RangeUtils.rangeLength(peak.getRawDataPointsRTRange()));
loopDataFiles = true;
break;
case PEAKSTATUS:
statement.setString(i + 1, peak.getFeatureStatus().name());
loopDataFiles = true;
break;
case PEAKMZ:
statement.setDouble(i + 1, peak.getMZ());
loopDataFiles = true;
break;
case PEAKRT:
statement.setDouble(i + 1, peak.getRT());
loopDataFiles = true;
break;
case PEAKRT_START:
statement.setDouble(i + 1, peak.getRawDataPointsRTRange().lowerEndpoint());
loopDataFiles = true;
break;
case PEAKRT_END:
statement.setDouble(i + 1, peak.getRawDataPointsRTRange().upperEndpoint());
loopDataFiles = true;
break;
case PEAKHEIGHT:
statement.setDouble(i + 1, peak.getHeight());
loopDataFiles = true;
break;
case PEAKAREA:
statement.setDouble(i + 1, peak.getArea());
loopDataFiles = true;
break;
case DATAPOINTS:
statement.setDouble(i + 1, peak.getScanNumbers().length);
loopDataFiles = true;
break;
case FWHM:
statement.setDouble(i + 1, peak.getFWHM());
loopDataFiles = true;
break;
case TAILINGFACTOR:
statement.setDouble(i + 1, peak.getTailingFactor());
loopDataFiles = true;
break;
case ASYMMETRYFACTOR:
statement.setDouble(i + 1, peak.getAsymmetryFactor());
loopDataFiles = true;
break;
case RAWFILE:
statement.setString(i + 1, rawDataFile.getName());
loopDataFiles = true;
break;
case HEIGHT:
statement.setDouble(i + 1, row.getAverageHeight());
break;
case AREA:
statement.setDouble(i + 1, row.getAverageArea());
break;
case COMMENT:
statement.setString(i + 1, row.getComment());
break;
case IDENTITY:
PeakIdentity id = row.getPreferredPeakIdentity();
if (id != null) {
statement.setString(i + 1, id.getName());
} else {
statement.setNull(i + 1, Types.VARCHAR);
}
break;
case ISOTOPEPATTERN:
IsotopePattern isotopes = row.getBestIsotopePattern();
if (isotopes == null) {
statement.setNull(i + 1, Types.BLOB);
break;
}
DataPoint[] dataPoints = isotopes.getDataPoints();
byte[] bytes = ScanUtils.encodeDataPointsToBytes(dataPoints);
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
statement.setBlob(i + 1, is);
break;
case MSMS:
int msmsScanNum = row.getBestPeak().getMostIntenseFragmentScanNumber();
// Check if there is any MS/MS scan
if (msmsScanNum <= 0) {
statement.setNull(i + 1, Types.BLOB);
break;
}
RawDataFile dataFile = row.getBestPeak().getDataFile();
Scan msmsScan = dataFile.getScan(msmsScanNum);
MassList msmsMassList = msmsScan.getMassList(dataValue);
// Check if there is a masslist for the scan
if (msmsMassList == null) {
statement.setNull(i + 1, Types.BLOB);
break;
}
dataPoints = msmsMassList.getDataPoints();
bytes = ScanUtils.encodeDataPointsToBytes(dataPoints);
is = new ByteArrayInputStream(bytes);
statement.setBlob(i + 1, is);
break;
default:
break;
}
}
statement.executeUpdate();
// data files in feature list
if (!loopDataFiles) {
break;
}
}
}
}
use of net.sf.mzmine.datamodel.DataPoint 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.DataPoint in project mzmine2 by mzmine.
the class Candidates method getAvgPeakHeights.
/**
* @param ID
* @return avg heights of all with the ids, but only if they are contained in same scans and mass
* lists
*/
private double[] getAvgPeakHeights(int[] ID) {
PeakListRow[] rows = plh.getRowsByID(ID);
RawDataFile[] raws = rows[0].getRawDataFiles();
if (raws.length < 1)
return null;
double[] mzs = new double[ID.length];
for (int i = 0; i < rows.length; i++) mzs[i] = rows[i].getAverageMZ();
double[] avgHeights = new double[ID.length];
int pointsAdded = 0;
for (RawDataFile raw : raws) {
if (!raw.getDataMZRange().contains(rows[0].getAverageMZ()))
continue;
int[] scanNums = raw.getScanNumbers();
for (int i = 0; i < scanNums.length; i++) {
Scan scan = raw.getScan(scanNums[i]);
MassList list = scan.getMassList(massListName);
if (list == null || !massListContainsEveryMZ(list, mzs, minHeight))
continue;
double[] avgBuffer = new double[mzs.length];
boolean allFound = true;
for (int j = 0; j < mzs.length; j++) {
DataPoint[] points = getMassListDataPointsByMass(list, mzTolerance.getToleranceRange(mzs[j]));
if (points.length == 0)
continue;
DataPoint dp = getClosestDataPoint(points, rows[j].getAverageMZ(), minHeight);
if (// yes the list contained something close to every datapoint that was over
dp == null) // minHeight, BUT
{
// the closest might not have been. Check is done inside getClosestDataPoint();
allFound = false;
break;
}
avgBuffer[j] = dp.getIntensity();
}
if (allFound) {
pointsAdded++;
for (int j = 0; j < mzs.length; j++) avgHeights[j] += avgBuffer[j];
}
}
}
if (pointsAdded == 0) {
logger.warning("Error: Peaks with ids: " + Arrays.toString(ID) + " were not in same scans at all. Please update the parameters.");
return null;
}
for (int i = 0; i < avgHeights.length; i++) avgHeights[i] /= (pointsAdded);
return avgHeights;
}
use of net.sf.mzmine.datamodel.DataPoint in project mzmine2 by mzmine.
the class Candidates method getMassListDataPointsByMass.
/**
* @param list
* @param massRange
* @return dataPoints within given massRange contained in mass list
*/
private DataPoint[] getMassListDataPointsByMass(MassList list, Range<Double> massRange) {
DataPoint[] dps = list.getDataPoints();
int start = 0, end = 0;
for (start = 0; start < dps.length; start++) if (massRange.lowerEndpoint() >= dps[start].getMZ())
break;
for (end = start; end < dps.length; end++) if (massRange.upperEndpoint() < dps[end].getMZ())
break;
DataPoint[] dpReturn = new DataPoint[end - start];
System.arraycopy(dps, start, dpReturn, 0, end - start);
return dpReturn;
}
Aggregations