/** {@inheritDoc} */
  @SuppressWarnings("null")
  @Override
  public RawDataFile execute() throws MSDKException {

    String osName = System.getProperty("os.name").toUpperCase();
    if (!osName.contains("WINDOWS")) {
      throw new MSDKException("Native data format import only works on MS Windows");
    }

    logger.info("Started parsing file " + sourceFile);

    try {

      // Decompress the thermo raw dump executable to a temporary folder
      File tempFolder = Files.createTempDirectory("msdk").toFile();
      tempFolder.deleteOnExit();
      InputStream dumpArchive =
          this.getClass().getClassLoader().getResourceAsStream("thermorawdump.zip");
      if (dumpArchive == null)
        throw new MSDKException("Failed to load the thermorawdump.zip archive from the MSDK jar");
      ZipUtils.extractStreamToFolder(dumpArchive, tempFolder);

      // Path to the rawdump executable
      File rawDumpPath = new File(tempFolder, "ThermoRawDump.exe");

      if (!rawDumpPath.canExecute())
        throw new MSDKException("Cannot execute program " + rawDumpPath);

      // Create a separate process and execute RAWdump.exe
      final String cmdLine[] = new String[] {rawDumpPath.getPath(), sourceFile.getPath()};
      dumperProcess = Runtime.getRuntime().exec(cmdLine);

      // Get the stdout of RAWdump process as InputStream
      InputStream dumpStream = dumperProcess.getInputStream();

      // Create the new RawDataFile
      String fileName = sourceFile.getName();
      newRawFile = MSDKObjectBuilder.getRawDataFile(fileName, sourceFile, fileType, dataStore);

      // Read the dump data
      parser = new RawDumpParser(newRawFile, dataStore);
      parser.readRAWDump(dumpStream);

      // Cleanup
      dumpStream.close();
      dumperProcess.destroy();

      try {
        FileUtils.deleteDirectory(tempFolder);
      } catch (IOException e) {
        // Ignore errors while deleting the tmp folder
      }

      if (canceled) return null;

    } catch (Throwable e) {
      if (dumperProcess != null) dumperProcess.destroy();

      throw new MSDKException(e);
    }

    logger.info("Finished parsing " + sourceFile);

    return newRawFile;
  }
Beispiel #2
0
  /**
   * {@inheritDoc}
   *
   * <p>endElement()
   */
  @SuppressWarnings("null")
  public void endElement(String namespaceURI, String sName, String qName) throws SAXException {

    if (canceled) throw new SAXException("Parsing Cancelled");

    // <spectrumInstrument>
    if (qName.equalsIgnoreCase("spectrumInstrument")) {
      spectrumInstrumentFlag = false;
    }

    // <precursor>
    if (qName.equalsIgnoreCase("precursor")) {
      precursorFlag = false;
    }

    // <spectrum>
    if (qName.equalsIgnoreCase("spectrum")) {

      spectrumInstrumentFlag = false;

      // Auto-detect whether this scan is centroided
      MsSpectrumType spectrumType =
          SpectrumTypeDetectionAlgorithm.detectSpectrumType(mzBuffer, intensityBuffer, peaksCount);

      // Create a new scan
      MsFunction msFunction = MSDKObjectBuilder.getMsFunction(msLevel);

      MsScan newScan = MSDKObjectBuilder.getMsScan(dataStore, scanNumber, msFunction);

      newScan.setDataPoints(mzBuffer, intensityBuffer, peaksCount);
      newScan.setSpectrumType(spectrumType);
      newScan.setPolarity(polarity);

      if (retentionTime != null) {
        ChromatographyInfo chromInfo =
            MSDKObjectBuilder.getChromatographyInfo1D(SeparationType.UNKNOWN, retentionTime);
        newScan.setChromatographyInfo(chromInfo);
      }

      if (precursorMz != null) {
        IsolationInfo isolation =
            MSDKObjectBuilder.getIsolationInfo(
                Range.singleton(precursorMz), null, precursorMz, precursorCharge, null);
        newScan.getIsolations().add(isolation);
      }

      // Add the scan to the file
      newRawFile.addScan(newScan);
      parsedScans++;
    }

    // <mzArrayBinary>
    if (qName.equalsIgnoreCase("mzArrayBinary")) {

      mzArrayBinaryFlag = false;

      // Allocate space for the whole array
      if (mzBuffer.length < peaksCount) mzBuffer = new double[peaksCount * 2];

      byte[] peakBytes = Base64.decodeBase64(charBuffer.toString().getBytes());

      ByteBuffer currentMzBytes = ByteBuffer.wrap(peakBytes);

      if (endian.equals("big")) {
        currentMzBytes = currentMzBytes.order(ByteOrder.BIG_ENDIAN);
      } else {
        currentMzBytes = currentMzBytes.order(ByteOrder.LITTLE_ENDIAN);
      }

      for (int i = 0; i < peaksCount; i++) {
        if (precision == null || precision.equals("32"))
          mzBuffer[i] = (double) currentMzBytes.getFloat();
        else mzBuffer[i] = currentMzBytes.getDouble();
      }
    }

    // <intenArrayBinary>
    if (qName.equalsIgnoreCase("intenArrayBinary")) {

      intenArrayBinaryFlag = false;

      // Allocate space for the whole array
      if (intensityBuffer.length < peaksCount) intensityBuffer = new float[peaksCount * 2];

      byte[] peakBytes = Base64.decodeBase64(charBuffer.toString().getBytes());

      ByteBuffer currentIntensityBytes = ByteBuffer.wrap(peakBytes);

      if (endian.equals("big")) {
        currentIntensityBytes = currentIntensityBytes.order(ByteOrder.BIG_ENDIAN);
      } else {
        currentIntensityBytes = currentIntensityBytes.order(ByteOrder.LITTLE_ENDIAN);
      }

      for (int i = 0; i < peaksCount; i++) {
        if (precision == null || precision.equals("32"))
          intensityBuffer[i] = currentIntensityBytes.getFloat();
        else intensityBuffer[i] = (float) currentIntensityBytes.getDouble();
      }
    }
  }
Beispiel #3
0
  @SuppressWarnings("null")
  @Test
  public void testOrbitrap() throws MSDKException {

    // Create the data structures
    final DataPointStore dataStore = DataPointStoreFactory.getMemoryDataStore();

    // Import the file
    File inputFile = new File(TEST_DATA_PATH + "orbitrap_300-600mz.mzML");
    Assert.assertTrue("Cannot read test data", inputFile.canRead());
    MzMLFileImportMethod importer = new MzMLFileImportMethod(inputFile);
    RawDataFile rawFile = importer.execute();
    Assert.assertNotNull(rawFile);
    Assert.assertEquals(1.0, importer.getFinishedPercentage(), 0.0001);

    // Ion 1
    List<IonAnnotation> ionAnnotations = new ArrayList<IonAnnotation>();
    IonAnnotation ion1 = MSDKObjectBuilder.getSimpleIonAnnotation();
    ion1.setExpectedMz(332.56);
    ion1.setAnnotationId("Feature 332.56");
    ion1.setChromatographyInfo(
        MSDKObjectBuilder.getChromatographyInfo1D(SeparationType.LC, (float) 772.8));
    ionAnnotations.add(ion1);

    // Ion 2
    IonAnnotation ion2 = MSDKObjectBuilder.getSimpleIonAnnotation();
    ion2.setExpectedMz(508.004);
    ion2.setAnnotationId("Feature 508.004");
    ion2.setChromatographyInfo(
        MSDKObjectBuilder.getChromatographyInfo1D(SeparationType.LC, (float) 868.8));
    ionAnnotations.add(ion2);

    // Ion 3
    IonAnnotation ion3 = MSDKObjectBuilder.getSimpleIonAnnotation();
    ion3.setExpectedMz(362.102);
    ion3.setAnnotationId("Feature 362.102");
    ion3.setChromatographyInfo(
        MSDKObjectBuilder.getChromatographyInfo1D(SeparationType.LC, (float) 643.2));
    ionAnnotations.add(ion3);

    // Variables
    final MZTolerance mzTolerance = new MZTolerance(0.003, 5.0);
    final RTTolerance rtTolerance = new RTTolerance(3, false);
    final Double intensityTolerance = 0.10d;
    final Double noiseLevel = 5000d;

    TargetedDetectionMethod chromBuilder =
        new TargetedDetectionMethod(
            ionAnnotations,
            rawFile,
            dataStore,
            mzTolerance,
            rtTolerance,
            intensityTolerance,
            noiseLevel);
    final List<Chromatogram> chromatograms = chromBuilder.execute();
    Assert.assertEquals(1.0, chromBuilder.getFinishedPercentage(), 0.0001);
    Assert.assertNotNull(chromatograms);
    Assert.assertEquals(3, chromatograms.size());

    // Create a new feature table
    FeatureTable featureTable = MSDKObjectBuilder.getFeatureTable("orbitrap_300-600mz", dataStore);
    Sample sample = MSDKObjectBuilder.getSimpleSample("orbitrap_300-600mz");

    ChromatogramToFeatureTableMethod tableBuilder =
        new ChromatogramToFeatureTableMethod(chromatograms, featureTable, sample);
    tableBuilder.execute();

    // 1. Filter parameters
    boolean filterByMz = true;
    boolean filterByRt = true;
    boolean filterByDuration = true;
    boolean filterByCount = true;
    boolean filterByIsotopes = true;
    boolean filterByIonAnnotation = true;
    boolean requireAnnotation = false;
    boolean removeDuplicates = false;
    boolean duplicateRequireSameID = true;
    Range<Double> mzRange = Range.closed(300.0, 600.0);
    Range<Double> rtRange = Range.closed(600.0, 900.0); // Seconds
    Range<Double> durationRange = Range.closed(0.0, 47.0); // Seconds
    final MZTolerance duplicateMzTolerance = new MZTolerance(0.003, 5.0);
    final RTTolerance duplicateRtTolerance = new RTTolerance(0.2, false);
    int minCount = 1;
    int minIsotopes = 1;
    String ionAnnotation = null;
    String nameSuffix = "-rowFiltered";

    // 1. Filter the rows
    RowFilterMethod filterMethod =
        new RowFilterMethod(
            featureTable,
            dataStore,
            nameSuffix,
            filterByMz,
            filterByRt,
            filterByDuration,
            filterByCount,
            filterByIsotopes,
            filterByIonAnnotation,
            requireAnnotation,
            mzRange,
            rtRange,
            durationRange,
            minCount,
            minIsotopes,
            ionAnnotation,
            removeDuplicates,
            duplicateMzTolerance,
            duplicateRtTolerance,
            duplicateRequireSameID);
    filterMethod.execute();
    Assert.assertEquals(1.0, filterMethod.getFinishedPercentage(), 0.0001);

    // 1. Verify data
    FeatureTable filteredTable = filterMethod.getResult();
    Assert.assertNotNull(filteredTable);
    Assert.assertEquals(3, filteredTable.getRows().size());

    // 2. Filter parameters
    mzRange = Range.closed(350.0, 600.0);
    durationRange = Range.closed(46.0, 47.0); // Seconds

    // 2. Filter the features
    filterMethod =
        new RowFilterMethod(
            featureTable,
            dataStore,
            nameSuffix,
            filterByMz,
            filterByRt,
            filterByDuration,
            filterByCount,
            filterByIsotopes,
            filterByIonAnnotation,
            requireAnnotation,
            mzRange,
            rtRange,
            durationRange,
            minCount,
            minIsotopes,
            ionAnnotation,
            removeDuplicates,
            duplicateMzTolerance,
            duplicateRtTolerance,
            duplicateRequireSameID);
    filterMethod.execute();
    Assert.assertEquals(1.0, filterMethod.getFinishedPercentage(), 0.0001);

    // 2. Verify data
    filteredTable = filterMethod.getResult();
    Assert.assertNotNull(filteredTable);
    Assert.assertEquals(1, filteredTable.getRows().size());

    // 3. Filter parameters
    minCount = 2;

    // 3. Filter the features
    filterMethod =
        new RowFilterMethod(
            filteredTable,
            dataStore,
            nameSuffix,
            filterByMz,
            filterByRt,
            filterByDuration,
            filterByCount,
            filterByIsotopes,
            filterByIonAnnotation,
            requireAnnotation,
            mzRange,
            rtRange,
            durationRange,
            minCount,
            minIsotopes,
            ionAnnotation,
            removeDuplicates,
            duplicateMzTolerance,
            duplicateRtTolerance,
            duplicateRequireSameID);
    filterMethod.execute();
    Assert.assertEquals(1.0, filterMethod.getFinishedPercentage(), 0.0001);

    // 3. Verify data
    filteredTable = filterMethod.getResult();
    Assert.assertNotNull(filteredTable);
    Assert.assertEquals(0, filteredTable.getRows().size());

    featureTable.dispose();
    filteredTable.dispose();
  }
Beispiel #4
0
  @Override
  public MsScan performFilter(MsScan scan) {

    if (!Avalues.containsKey(numOfDataPoints) || !Hvalues.containsKey(numOfDataPoints)) {
      return scan;
    }

    int[] aVals = Avalues.get(numOfDataPoints);
    int h = Hvalues.get(numOfDataPoints);

    int marginSize = (numOfDataPoints + 1) / 2 - 1;
    float sumOfInts;

    // Create data point list object and fill it with the scan data points
    MsSpectrumDataPointList dataPoints = MSDKObjectBuilder.getMsSpectrumDataPointList();
    scan.getDataPoints(dataPoints);
    float intensityValues[] = dataPoints.getIntensityBuffer();
    double mzValues[] = dataPoints.getMzBuffer();

    int newDataPointsLength = dataPoints.getSize() - (marginSize * 2);

    // only process MS level 1 scans
    if (scan.getMsFunction().getMsLevel() != 1) {
      return scan;
    }

    // only process scans with datapoints
    if (newDataPointsLength < 1) {
      return scan;
    }

    MsSpectrumDataPointList newDataPoints = MSDKObjectBuilder.getMsSpectrumDataPointList();

    for (int spectrumInd = marginSize;
        spectrumInd < (dataPoints.getSize() - marginSize);
        spectrumInd++) {

      // zero intensity data points must be left unchanged
      if (intensityValues[spectrumInd] == 0) {
        intensityValues[spectrumInd - marginSize] = intensityValues[spectrumInd];
        continue;
      }

      sumOfInts = aVals[0] * intensityValues[spectrumInd];

      for (int windowInd = 1; windowInd <= marginSize; windowInd++) {
        sumOfInts +=
            aVals[windowInd]
                * (intensityValues[spectrumInd + windowInd]
                    + intensityValues[spectrumInd - windowInd]);
      }

      sumOfInts = sumOfInts / h;

      if (sumOfInts < 0) {
        sumOfInts = 0;
      }
      newDataPoints.add(mzValues[spectrumInd], sumOfInts);
    }

    // Return a new scan with the new data points
    MsScan result = MSDKObjectBuilder.getMsScan(store, scan.getScanNumber(), scan.getMsFunction());
    result.setDataPoints(newDataPoints);
    result.setChromatographyInfo(scan.getChromatographyInfo());
    result.setRawDataFile(scan.getRawDataFile());

    return result;
  }