Search in sources :

Example 1 with MassSpectrumType

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

the class ScanSelectionParameter method loadValueFromXML.

@Override
public void loadValueFromXML(Element xmlElement) {
    Range<Integer> scanNumberRange = null;
    Integer baseFilteringInteger = null;
    Range<Double> scanRTRange = null;
    PolarityType polarity = null;
    MassSpectrumType spectrumType = null;
    Integer msLevel = null;
    String scanDefinition = null;
    scanNumberRange = XMLUtils.parseIntegerRange(xmlElement, "scan_numbers");
    scanRTRange = XMLUtils.parseDoubleRange(xmlElement, "retention_time");
    NodeList items = xmlElement.getElementsByTagName("ms_level");
    for (int i = 0; i < items.getLength(); i++) {
        msLevel = Integer.valueOf(items.item(i).getTextContent());
    }
    items = xmlElement.getElementsByTagName("polarity");
    for (int i = 0; i < items.getLength(); i++) {
        try {
            polarity = PolarityType.valueOf(items.item(i).getTextContent());
        } catch (Exception e) {
            polarity = PolarityType.fromSingleChar(items.item(i).getTextContent());
        }
    }
    items = xmlElement.getElementsByTagName("spectrum_type");
    for (int i = 0; i < items.getLength(); i++) {
        spectrumType = MassSpectrumType.valueOf(items.item(i).getTextContent());
    }
    items = xmlElement.getElementsByTagName("scan_definition");
    for (int i = 0; i < items.getLength(); i++) {
        scanDefinition = items.item(i).getTextContent();
    }
    this.value = new ScanSelection(scanNumberRange, baseFilteringInteger, scanRTRange, polarity, spectrumType, msLevel, scanDefinition);
}
Also used : PolarityType(net.sf.mzmine.datamodel.PolarityType) NodeList(org.w3c.dom.NodeList) MassSpectrumType(net.sf.mzmine.datamodel.MassSpectrumType)

Example 2 with MassSpectrumType

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

the class ScanSelectionParameter method saveValueToXML.

@Override
public void saveValueToXML(Element xmlElement) {
    if (value == null)
        return;
    Document parentDocument = xmlElement.getOwnerDocument();
    final Range<Integer> scanNumberRange = value.getScanNumberRange();
    final Range<Double> scanRetentionTimeRange = value.getScanRTRange();
    final Integer baseFilteringInteger = value.getBaseFilteringInteger();
    final PolarityType polarity = value.getPolarity();
    final MassSpectrumType spectrumType = value.getSpectrumType();
    final Integer msLevel = value.getMsLevel();
    final String scanDefinition = value.getScanDefinition();
    XMLUtils.appendRange(xmlElement, "scan_numbers", scanNumberRange);
    XMLUtils.appendRange(xmlElement, "retention_time", scanRetentionTimeRange);
    if (baseFilteringInteger != null) {
        Element newElement = parentDocument.createElement("baseFilteringInteger");
        newElement.setTextContent(baseFilteringInteger.toString());
        xmlElement.appendChild(newElement);
    }
    if (polarity != null) {
        Element newElement = parentDocument.createElement("polarity");
        newElement.setTextContent(polarity.toString());
        xmlElement.appendChild(newElement);
    }
    if (spectrumType != null) {
        Element newElement = parentDocument.createElement("spectrum_type");
        newElement.setTextContent(spectrumType.toString());
        xmlElement.appendChild(newElement);
    }
    if (msLevel != null) {
        Element newElement = parentDocument.createElement("ms_level");
        newElement.setTextContent(String.valueOf(msLevel));
        xmlElement.appendChild(newElement);
    }
    if (scanDefinition != null) {
        Element newElement = parentDocument.createElement("scan_definition");
        newElement.setTextContent(scanDefinition);
        xmlElement.appendChild(newElement);
    }
}
Also used : PolarityType(net.sf.mzmine.datamodel.PolarityType) Element(org.w3c.dom.Element) MassSpectrumType(net.sf.mzmine.datamodel.MassSpectrumType) Document(org.w3c.dom.Document)

Example 3 with MassSpectrumType

use of net.sf.mzmine.datamodel.MassSpectrumType 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 4 with MassSpectrumType

use of net.sf.mzmine.datamodel.MassSpectrumType 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)

Example 5 with MassSpectrumType

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

the class MzMLReadTask method run.

/**
 * @see java.lang.Runnable#run()
 */
public void run() {
    setStatus(TaskStatus.PROCESSING);
    logger.info("Started parsing file " + file);
    MzMLUnmarshaller unmarshaller = new MzMLUnmarshaller(file);
    totalScans = unmarshaller.getObjectCountForXpath("/run/spectrumList/spectrum");
    fillScanIdTable(unmarshaller.unmarshalCollectionFromXpath("/run/spectrumList/spectrum", Spectrum.class), totalScans);
    MzMLObjectIterator<Spectrum> spectrumIterator = unmarshaller.unmarshalCollectionFromXpath("/run/spectrumList/spectrum", Spectrum.class);
    try {
        while (spectrumIterator.hasNext()) {
            if (isCanceled())
                return;
            Spectrum spectrum = spectrumIterator.next();
            // Ignore scans that are not MS, e.g. UV
            if (!isMsSpectrum(spectrum)) {
                parsedScans++;
                continue;
            }
            String scanId = spectrum.getId();
            Integer scanNumber = scanIdTable.get(scanId);
            if (scanNumber == null)
                throw new IllegalStateException("Cannot determine scan number: " + scanId);
            // Extract scan data
            int msLevel = extractMSLevel(spectrum);
            double retentionTime = extractRetentionTime(spectrum);
            PolarityType polarity = extractPolarity(spectrum);
            int parentScan = extractParentScanNumber(spectrum);
            double precursorMz = extractPrecursorMz(spectrum);
            int precursorCharge = extractPrecursorCharge(spectrum);
            String scanDefinition = extractScanDefinition(spectrum);
            DataPoint[] dataPoints = extractDataPoints(spectrum);
            // Auto-detect whether this scan is centroided
            MassSpectrumType spectrumType = ScanUtils.detectSpectrumType(dataPoints);
            SimpleScan scan = new SimpleScan(null, scanNumber, msLevel, retentionTime, precursorMz, precursorCharge, null, dataPoints, spectrumType, polarity, scanDefinition, null);
            for (SimpleScan s : parentStack) {
                if (s.getScanNumber() == parentScan) {
                    s.addFragmentScan(scanNumber);
                }
            }
            /*
         * Verify the size of parentStack. The actual size of the window to cover possible
         * candidates is defined by limitSize.
         */
            if (parentStack.size() > PARENT_STACK_SIZE) {
                SimpleScan firstScan = parentStack.removeLast();
                newMZmineFile.addScan(firstScan);
            }
            parentStack.addFirst(scan);
            parsedScans++;
        }
        while (!parentStack.isEmpty()) {
            SimpleScan scan = parentStack.removeLast();
            newMZmineFile.addScan(scan);
        }
        finalRawDataFile = newMZmineFile.finishWriting();
        project.addFile(finalRawDataFile);
    } catch (Throwable e) {
        e.printStackTrace();
        setStatus(TaskStatus.ERROR);
        setErrorMessage("Error parsing mzML: " + ExceptionUtils.exceptionToString(e));
        e.printStackTrace();
        return;
    }
    if (parsedScans == 0) {
        setStatus(TaskStatus.ERROR);
        setErrorMessage("No scans found");
        return;
    }
    logger.info("Finished parsing " + file + ", parsed " + parsedScans + " scans");
    setStatus(TaskStatus.FINISHED);
}
Also used : PolarityType(net.sf.mzmine.datamodel.PolarityType) MassSpectrumType(net.sf.mzmine.datamodel.MassSpectrumType) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) Spectrum(uk.ac.ebi.jmzml.model.mzml.Spectrum) SimpleScan(net.sf.mzmine.datamodel.impl.SimpleScan) MzMLUnmarshaller(uk.ac.ebi.jmzml.xml.io.MzMLUnmarshaller) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint)

Aggregations

MassSpectrumType (net.sf.mzmine.datamodel.MassSpectrumType)5 PolarityType (net.sf.mzmine.datamodel.PolarityType)4 DataPoint (net.sf.mzmine.datamodel.DataPoint)3 SimpleDataPoint (net.sf.mzmine.datamodel.impl.SimpleDataPoint)3 SimpleScan (net.sf.mzmine.datamodel.impl.SimpleScan)3 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1 Array (ucar.ma2.Array)1 Index (ucar.ma2.Index)1 Spectrum (uk.ac.ebi.jmzml.model.mzml.Spectrum)1 MzMLUnmarshaller (uk.ac.ebi.jmzml.xml.io.MzMLUnmarshaller)1