Search in sources :

Example 66 with DataPoint

use of net.sf.mzmine.datamodel.DataPoint in project mzmine2 by mzmine.

the class SameRangeTask method fillGap.

private Feature fillGap(PeakListRow row, RawDataFile column) {
    SameRangePeak newPeak = new SameRangePeak(column);
    Range<Double> mzRange = null, rtRange = null;
    // Check the peaks for selected data files
    for (RawDataFile dataFile : row.getRawDataFiles()) {
        Feature peak = row.getPeak(dataFile);
        if (peak == null)
            continue;
        if ((mzRange == null) || (rtRange == null)) {
            mzRange = peak.getRawDataPointsMZRange();
            rtRange = peak.getRawDataPointsRTRange();
        } else {
            mzRange = mzRange.span(peak.getRawDataPointsMZRange());
            rtRange = rtRange.span(peak.getRawDataPointsRTRange());
        }
    }
    assert mzRange != null;
    assert rtRange != null;
    Range<Double> mzRangeWithTol = mzTolerance.getToleranceRange(mzRange);
    // Get scan numbers
    int[] scanNumbers = column.getScanNumbers(1, rtRange);
    boolean dataPointFound = false;
    for (int scanNumber : scanNumbers) {
        if (isCanceled())
            return null;
        // Get next scan
        Scan scan = column.getScan(scanNumber);
        // Find most intense m/z peak
        DataPoint basePeak = ScanUtils.findBasePeak(scan, mzRangeWithTol);
        if (basePeak != null) {
            if (basePeak.getIntensity() > 0)
                dataPointFound = true;
            newPeak.addDatapoint(scan.getScanNumber(), basePeak);
        } else {
            DataPoint fakeDataPoint = new SimpleDataPoint(RangeUtils.rangeCenter(mzRangeWithTol), 0);
            newPeak.addDatapoint(scan.getScanNumber(), fakeDataPoint);
        }
    }
    if (dataPointFound) {
        newPeak.finalizePeak();
        if (newPeak.getArea() == 0)
            return null;
        return newPeak;
    }
    return null;
}
Also used : SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) Scan(net.sf.mzmine.datamodel.Scan) Feature(net.sf.mzmine.datamodel.Feature) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint)

Example 67 with DataPoint

use of net.sf.mzmine.datamodel.DataPoint 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;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IsotopePattern(net.sf.mzmine.datamodel.IsotopePattern) SimpleIsotopePattern(net.sf.mzmine.datamodel.impl.SimpleIsotopePattern) SimplePeakListAppliedMethod(net.sf.mzmine.datamodel.impl.SimplePeakListAppliedMethod) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) SimplePeakIdentity(net.sf.mzmine.datamodel.impl.SimplePeakIdentity) PeakIdentity(net.sf.mzmine.datamodel.PeakIdentity) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) PeakListRow(net.sf.mzmine.datamodel.PeakListRow) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) SimpleIsotopePattern(net.sf.mzmine.datamodel.impl.SimpleIsotopePattern) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) PeakList(net.sf.mzmine.datamodel.PeakList) HashSet(java.util.HashSet)

Example 68 with DataPoint

use of net.sf.mzmine.datamodel.DataPoint in project mzmine2 by mzmine.

the class IsotopePatternPreviewDialog method updateTable.

/**
 * this is being called by the calculation task to update the table
 * @param pattern
 */
protected void updateTable(ExtendedIsotopePattern pattern) {
    DataPoint[] dp = pattern.getDataPoints();
    Object[][] data = new Object[dp.length][];
    for (int i = 0; i < pattern.getNumberOfDataPoints(); i++) {
        data[i] = new Object[3];
        data[i][0] = mzFormat.format(dp[i].getMZ());
        data[i][1] = relFormat.format(dp[i].getIntensity());
        data[i][2] = pattern.getIsotopeComposition(i);
    }
    if (pol == PolarityType.NEUTRAL)
        // column 1 = "Exact mass / Da"
        table = new JTable(data, columns[0]);
    else
        // column 2 = "m/z"
        table = new JTable(data, columns[1]);
    pnText.setViewportView(table);
    // make editing impossible
    table.setDefaultEditor(Object.class, null);
}
Also used : DataPoint(net.sf.mzmine.datamodel.DataPoint) JTable(javax.swing.JTable) DataPoint(net.sf.mzmine.datamodel.DataPoint)

Example 69 with DataPoint

use of net.sf.mzmine.datamodel.DataPoint in project mzmine2 by mzmine.

the class NativeFileReadTask method readRAWDump.

/**
 * This method reads the dump of the RAW data file produced by RAWdump.exe utility (see
 * RAWdump.cpp source for details).
 */
private void readRAWDump(InputStream dumpStream) throws IOException {
    String line;
    byte[] byteBuffer = new byte[100000];
    double[] mzValuesBuffer = new double[10000];
    double[] intensityValuesBuffer = new double[10000];
    while ((line = TextUtils.readLineFromStream(dumpStream)) != null) {
        if (isCanceled()) {
            return;
        }
        if (line.startsWith("ERROR: ")) {
            throw (new IOException(line.substring("ERROR: ".length())));
        }
        if (line.startsWith("NUMBER OF SCANS: ")) {
            totalScans = Integer.parseInt(line.substring("NUMBER OF SCANS: ".length()));
        }
        if (line.startsWith("SCAN NUMBER: ")) {
            scanNumber = Integer.parseInt(line.substring("SCAN NUMBER: ".length()));
        }
        if (line.startsWith("SCAN ID: ")) {
            scanId = line.substring("SCAN ID: ".length());
        }
        if (line.startsWith("MS LEVEL: ")) {
            msLevel = Integer.parseInt(line.substring("MS LEVEL: ".length()));
        }
        if (line.startsWith("POLARITY: ")) {
            if (line.contains("-"))
                polarity = PolarityType.NEGATIVE;
            else if (line.contains("+"))
                polarity = PolarityType.POSITIVE;
            else
                polarity = PolarityType.UNKNOWN;
            // In such case, we can parse it from the scan filter line (scanId).
            if ((polarity == PolarityType.UNKNOWN) && (fileType == RawDataFileType.THERMO_RAW) && (!Strings.isNullOrEmpty(scanId))) {
                if (scanId.startsWith("-"))
                    polarity = PolarityType.NEGATIVE;
                else if (scanId.startsWith("+"))
                    polarity = PolarityType.POSITIVE;
            }
        }
        if (line.startsWith("RETENTION TIME: ")) {
            // Retention time is reported in minutes.
            retentionTime = Double.parseDouble(line.substring("RETENTION TIME: ".length()));
        }
        if (line.startsWith("PRECURSOR: ")) {
            String[] tokens = line.split(" ");
            double token2 = Double.parseDouble(tokens[1]);
            int token3 = Integer.parseInt(tokens[2]);
            precursorMZ = token2;
            precursorCharge = token3;
            // FTMS + p ESI d Full ms2 279.16@hcd25.00 [50.00-305.00]
            if (precursorMZ == 0.0 && fileType == RawDataFileType.THERMO_RAW && (!Strings.isNullOrEmpty(scanId))) {
                Pattern precursorPattern = Pattern.compile(".* ms\\d+ (\\d+\\.\\d+)[@ ]");
                Matcher m = precursorPattern.matcher(scanId);
                if (m.find()) {
                    String precursorMzString = m.group(1);
                    try {
                        precursorMZ = Double.parseDouble(precursorMzString);
                    } catch (Exception e) {
                        e.printStackTrace();
                    // ignore
                    }
                }
            }
        }
        if (line.startsWith("MASS VALUES: ")) {
            Pattern p = Pattern.compile("MASS VALUES: (\\d+) x (\\d+) BYTES");
            Matcher m = p.matcher(line);
            if (!m.matches())
                throw new IOException("Could not parse line " + line);
            numOfDataPoints = Integer.parseInt(m.group(1));
            final int byteSize = Integer.parseInt(m.group(2));
            final int numOfBytes = numOfDataPoints * byteSize;
            if (byteBuffer.length < numOfBytes)
                byteBuffer = new byte[numOfBytes * 2];
            dumpStream.read(byteBuffer, 0, numOfBytes);
            ByteBuffer mzByteBuffer = ByteBuffer.wrap(byteBuffer, 0, numOfBytes).order(ByteOrder.LITTLE_ENDIAN);
            if (mzValuesBuffer.length < numOfDataPoints)
                mzValuesBuffer = new double[numOfDataPoints * 2];
            for (int i = 0; i < numOfDataPoints; i++) {
                double newValue;
                if (byteSize == 8)
                    newValue = mzByteBuffer.getDouble();
                else
                    newValue = mzByteBuffer.getFloat();
                mzValuesBuffer[i] = newValue;
            }
        }
        if (line.startsWith("INTENSITY VALUES: ")) {
            Pattern p = Pattern.compile("INTENSITY VALUES: (\\d+) x (\\d+) BYTES");
            Matcher m = p.matcher(line);
            if (!m.matches())
                throw new IOException("Could not parse line " + line);
            // VALUES
            if (numOfDataPoints != Integer.parseInt(m.group(1))) {
                throw new IOException("Scan " + scanNumber + " contained " + numOfDataPoints + " mass values, but " + m.group(1) + " intensity values");
            }
            final int byteSize = Integer.parseInt(m.group(2));
            final int numOfBytes = numOfDataPoints * byteSize;
            if (byteBuffer.length < numOfBytes)
                byteBuffer = new byte[numOfBytes * 2];
            dumpStream.read(byteBuffer, 0, numOfBytes);
            ByteBuffer intensityByteBuffer = ByteBuffer.wrap(byteBuffer, 0, numOfBytes).order(ByteOrder.LITTLE_ENDIAN);
            if (intensityValuesBuffer.length < numOfDataPoints)
                intensityValuesBuffer = new double[numOfDataPoints * 2];
            for (int i = 0; i < numOfDataPoints; i++) {
                double newValue;
                if (byteSize == 8)
                    newValue = intensityByteBuffer.getDouble();
                else
                    newValue = intensityByteBuffer.getFloat();
                intensityValuesBuffer[i] = newValue;
            }
            // INTENSITY VALUES was the last item of the scan, so now we can
            // convert the data to DataPoint[] array and create a new scan
            DataPoint[] dataPoints = new DataPoint[numOfDataPoints];
            for (int i = 0; i < numOfDataPoints; i++) {
                dataPoints[i] = new SimpleDataPoint(mzValuesBuffer[i], intensityValuesBuffer[i]);
            }
            // Auto-detect whether this scan is centroided
            MassSpectrumType spectrumType = ScanUtils.detectSpectrumType(dataPoints);
            SimpleScan newScan = new SimpleScan(null, scanNumber, msLevel, retentionTime, precursorMZ, precursorCharge, null, dataPoints, spectrumType, polarity, scanId, mzRange);
            newMZmineFile.addScan(newScan);
            parsedScans++;
            // Clean the variables for next scan
            scanNumber = 0;
            scanId = null;
            polarity = null;
            mzRange = null;
            msLevel = 0;
            retentionTime = 0;
            precursorMZ = 0;
            precursorCharge = 0;
            numOfDataPoints = 0;
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) MassSpectrumType(net.sf.mzmine.datamodel.MassSpectrumType) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) IOException(java.io.IOException) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) SimpleScan(net.sf.mzmine.datamodel.impl.SimpleScan) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint)

Example 70 with DataPoint

use of net.sf.mzmine.datamodel.DataPoint in project mzmine2 by mzmine.

the class NetCDFReadTask method readNextScan.

/**
 * Reads one scan from the file. Requires that general information has already been read.
 */
private Scan readNextScan() throws IOException {
    // Get scan starting position and length
    int[] scanStartPosition = new int[1];
    int[] scanLength = new int[1];
    Integer[] startAndLength = scansIndex.get(scanNum);
    // End of file
    if (startAndLength == null) {
        return null;
    }
    scanStartPosition[0] = startAndLength[0];
    scanLength[0] = startAndLength[1];
    // Get retention time of the scan
    Double retentionTime = scansRetentionTimes.get(scanNum);
    if (retentionTime == null) {
        logger.severe("Could not find retention time for scan " + scanNum);
        throw (new IOException("Could not find retention time for scan " + scanNum));
    }
    // An empty scan needs special attention..
    if (scanLength[0] == 0) {
        scanNum++;
        return new SimpleScan(null, scanNum, 1, retentionTime.doubleValue(), 0, 0, null, new DataPoint[0], MassSpectrumType.CENTROIDED, PolarityType.UNKNOWN, "", null);
    }
    // Is there any way how to extract polarity from netcdf?
    PolarityType polarity = PolarityType.UNKNOWN;
    // Is there any way how to extract scan definition from netcdf?
    String scanDefinition = "";
    // Read mass and intensity values
    Array massValueArray;
    Array intensityValueArray;
    try {
        massValueArray = massValueVariable.read(scanStartPosition, scanLength);
        intensityValueArray = intensityValueVariable.read(scanStartPosition, scanLength);
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Could not read from variables mass_values and/or intensity_values.", e);
        throw (new IOException("Could not read from variables mass_values and/or intensity_values."));
    }
    Index massValuesIndex = massValueArray.getIndex();
    Index intensityValuesIndex = intensityValueArray.getIndex();
    int arrayLength = massValueArray.getShape()[0];
    DataPoint[] dataPoints = new DataPoint[arrayLength];
    for (int j = 0; j < arrayLength; j++) {
        Index massIndex0 = massValuesIndex.set0(j);
        Index intensityIndex0 = intensityValuesIndex.set0(j);
        double mz = massValueArray.getDouble(massIndex0) * massValueScaleFactor;
        double intensity = intensityValueArray.getDouble(intensityIndex0) * intensityValueScaleFactor;
        dataPoints[j] = new SimpleDataPoint(mz, intensity);
    }
    scanNum++;
    // Auto-detect whether this scan is centroided
    MassSpectrumType spectrumType = ScanUtils.detectSpectrumType(dataPoints);
    SimpleScan buildingScan = new SimpleScan(null, scanNum, 1, retentionTime.doubleValue(), 0, 0, null, dataPoints, spectrumType, polarity, scanDefinition, null);
    return buildingScan;
}
Also used : PolarityType(net.sf.mzmine.datamodel.PolarityType) Index(ucar.ma2.Index) MassSpectrumType(net.sf.mzmine.datamodel.MassSpectrumType) IOException(java.io.IOException) IOException(java.io.IOException) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) Array(ucar.ma2.Array) SimpleScan(net.sf.mzmine.datamodel.impl.SimpleScan) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint)

Aggregations

DataPoint (net.sf.mzmine.datamodel.DataPoint)214 SimpleDataPoint (net.sf.mzmine.datamodel.impl.SimpleDataPoint)98 Scan (net.sf.mzmine.datamodel.Scan)64 ArrayList (java.util.ArrayList)50 RawDataFile (net.sf.mzmine.datamodel.RawDataFile)44 Feature (net.sf.mzmine.datamodel.Feature)27 MassList (net.sf.mzmine.datamodel.MassList)24 PeakListRow (net.sf.mzmine.datamodel.PeakListRow)22 IOException (java.io.IOException)20 SimpleScan (net.sf.mzmine.datamodel.impl.SimpleScan)18 IsotopePattern (net.sf.mzmine.datamodel.IsotopePattern)17 SimpleIsotopePattern (net.sf.mzmine.datamodel.impl.SimpleIsotopePattern)16 SimpleFeature (net.sf.mzmine.datamodel.impl.SimpleFeature)15 SimplePeakListRow (net.sf.mzmine.datamodel.impl.SimplePeakListRow)15 SimplePeakList (net.sf.mzmine.datamodel.impl.SimplePeakList)12 DataPointSorter (net.sf.mzmine.util.DataPointSorter)12 HashMap (java.util.HashMap)10 Vector (java.util.Vector)10 Range (com.google.common.collect.Range)8 TreeMap (java.util.TreeMap)8