예제 #1
0
  /**
   * Parse function for loading a PeakML file containing only masschromatogram entries. When an
   * entry of another type is encountered an {@link XmlParserException} is thrown. The resulting
   * {@link ParseResult} instance is type-bound to {@link MassChromatogram} to force only
   * masschromatogram types.
   *
   * @param in The input-stream to load the data from.
   * @return The header and peak information stored in the file.
   * @throws IOException Thrown on an IOException.
   * @throws XmlParserException Thrown when an unknown IPeak object is encountered.
   */
  public static ParseResult parseMassChromatograms(InputStream in, ParserProgressListener listener)
      throws IOException, XmlParserException {
    final ParseResult result = new ParseResult();
    final Vector<MassChromatogram<? extends Peak>> peaks =
        new Vector<MassChromatogram<? extends Peak>>();
    final ParserProgressListener _listener = listener;

    class myListener implements XmlParserListener {
      int index = 0;

      public void onDocument(Document document, String xpath) throws XmlParserException {
        if (xpath.equals(XPATH_IPEAK)) {
          Node node = document.getChildNodes().item(0);

          // check whether we're getting the correct ipeak
          Node typeattribute = node.getAttributes().getNamedItem(PeakMLWriter.TYPE);
          if (typeattribute == null)
            throw new XmlParserException("Failed to locate the type attribute.");
          if (!typeattribute.getNodeValue().equals(PeakMLWriter.TYPE_MASSCHROMATOGRAM))
            throw new XmlParserException(
                "IPeak ("
                    + typeattribute.getNodeValue()
                    + ") is not of type: '"
                    + PeakMLWriter.TYPE_MASSCHROMATOGRAM
                    + "'");

          // parse this node as a mass chromatogram
          MassChromatogram<? extends Peak> masschromatogram = parseMassChromatogram(node);
          if (masschromatogram != null) peaks.add(masschromatogram);

          //
          if (_listener != null && result.header != null && result.header.getNrPeaks() != 0)
            _listener.update((100. * index++) / result.header.getNrPeaks());
        } else if (xpath.equals(XPATH_HEADER)) {
          result.header = parseHeader(document.getFirstChild());
        }
      }
    }
    run(in, new myListener());

    result.measurement = new IPeakSet<MassChromatogram<? extends Peak>>(peaks);
    return result;
  }
예제 #2
0
  /**
   * Parse function for blindly loading data from a PeakML file. This method loads the data in a
   * PeakML file as it encounters it in the file. This means that a mix-model is essentially
   * possible for peak-data stored in a PeakML file. The resulting {@link ParseResult} instance is
   * parameterized with {@link IPeak}. The class-information made available through the Java
   * language can be used to determine the original type of the peak. The function employs a
   * callback mechanism with {@link ParserProgressListener} to return information about the progress
   * through the file. This is calculated with the information returned by {@link
   * Header#getNrPeaks()}.
   *
   * <p>The loadall parameter can be used to restrict the amount of data actually being loaded by
   * the function. If this is set to false only data known by the {@link IPeak} class is loaded (in
   * this implementation this means that an instance of the {@link Centroid} is made). The class
   * information cannot be used to determine the original type of the peak when loadall is set to
   * false.
   *
   * @param in The input-stream to load the data from.
   * @param listener The progress listener.
   * @param loadall If set to false only the data known to IPeak is loaded as class Peak.
   * @return The header and peak information stored in the file.
   * @throws IOException Thrown on an IOException.
   * @throws XmlParserException Thrown when an unknown IPeak object is encountered.
   */
  public static ParseResult parse(InputStream in, ParserProgressListener listener, boolean loadall)
      throws IOException, XmlParserException {
    //		final boolean _loadall = loadall;
    final ParserProgressListener _listener = listener;

    final ParseResult result = new ParseResult();
    final Vector<IPeak> peaks = new Vector<IPeak>();

    class myListener implements XmlParserListener {
      int index = 0;

      public void onDocument(Document document, String xpath) throws XmlParserException {
        if (xpath.equals(XPATH_IPEAK)) {
          Node node = document.getChildNodes().item(0);

          // check whether we're getting the correct ipeak
          Node typeattribute = node.getAttributes().getNamedItem(PeakMLWriter.TYPE);
          if (typeattribute == null)
            throw new XmlParserException("Failed to locate a type attribute.");

          // ...
          //					IPeak peak = (_loadall ? parseIPeak(node) : parseCentroid(node));
          IPeak peak = parseIPeak(node);
          if (peak != null) peaks.add(peak);

          //
          if (_listener != null && result.header != null && result.header.getNrPeaks() != 0)
            _listener.update((100. * index++) / result.header.getNrPeaks());
        } else if (xpath.equals(XPATH_HEADER)) {
          result.header = parseHeader(document.getFirstChild());
        }
      }
    }
    run(in, new myListener());

    result.measurement = new IPeakSet<IPeak>(peaks);
    return result;
  }