Exemple #1
0
  private static MeasurementInfo parseMeasurement(Node parent) throws XmlParserException {
    String id = "";
    String label = "";
    String sampleid = "";
    Vector<FileInfo> files = null;
    Vector<ScanInfo> scans = null;

    NodeList nodes = parent.getChildNodes();
    for (int nodeid = 0; nodeid < nodes.getLength(); ++nodeid) {
      Node node = nodes.item(nodeid);
      if (node.getNodeType() != Node.ELEMENT_NODE) continue;

      Element element = (Element) node;
      if (element.getTagName().equals("id")) id = element.getTextContent();
      else if (element.getTagName().equals("label")) label = element.getTextContent();
      else if (element.getTagName().equals("sampleid")) sampleid = element.getTextContent();
      else if (element.getTagName().equals("scans")) scans = parseScans(node);
      else if (element.getTagName().equals("files")) files = parseFiles(node);
    }

    MeasurementInfo measurement = new MeasurementInfo(Integer.parseInt(id), sampleid);
    measurement.setLabel(label);
    measurement.addFileInfos(files);
    if (scans != null) measurement.addScanInfos(scans);

    return measurement;
  }
Exemple #2
0
  private static SetInfo parseSet(Node parent) throws XmlParserException {
    String id = "";
    String type = "";
    String measurementids = null;

    NodeList nodes = parent.getChildNodes();
    Vector<SetInfo> sets = new Vector<SetInfo>();
    for (int nodeid = 0; nodeid < nodes.getLength(); ++nodeid) {
      Node node = nodes.item(nodeid);
      if (node.getNodeType() != Node.ELEMENT_NODE) continue;

      Element element = (Element) node;
      if (element.getTagName().equals("id")) id = element.getTextContent();
      else if (element.getTagName().equals("set")) sets.add(parseSet(element));
      else if (element.getTagName().equals("type")) type = element.getTextContent();
      else if (element.getTagName().equals("measurementids"))
        measurementids = element.getTextContent();
    }

    // create the set
    SetInfo set = new SetInfo(id, Integer.parseInt(type));
    if (measurementids != null) {
      int mids[] = ByteArray.toIntArray(Base64.decode(measurementids), ByteArray.ENDIAN_LITTLE, 32);
      for (int mid : mids) set.addMeasurementID(mid);
    }

    // add the children
    for (SetInfo s : sets) set.addChild(s);

    return set;
  }
Exemple #3
0
  private static Header parseHeader(Node parent) throws XmlParserException {
    Header header = new Header();

    NodeList nodes = parent.getChildNodes();
    for (int nodeid = 0; nodeid < nodes.getLength(); ++nodeid) {
      Node node = nodes.item(nodeid);
      if (node.getNodeType() != Node.ELEMENT_NODE) continue;

      Element element = (Element) node;
      try {
        if (element.getTagName().equals("nrpeaks"))
          header.setNrPeaks(Integer.parseInt(element.getTextContent()));
        else if (element.getTagName().equals("date")) header.setDate(element.getTextContent());
        else if (element.getTagName().equals("owner")) header.setOwner(element.getTextContent());
        else if (element.getTagName().equals("description"))
          header.setDescription(element.getTextContent());
        else if (element.getTagName().equals("sets")) header.addSetInfos(parseSets(element));
        else if (element.getTagName().equals("measurements"))
          header.addMeasurementInfos(parseMeasurements(element));
        else if (element.getTagName().equals("annotations")) {
          Vector<Annotation> annotations = parseAnnotations(element);
          if (annotations != null)
            for (Annotation annotation : annotations) header.addAnnotation(annotation);
        }
      } catch (Exception e) {
        throw new XmlParserException(
            "Invalid value in header (" + element.getTagName() + "): '" + e.getMessage() + "'.");
      }
    }

    return header;
  }
Exemple #4
0
      public void onDocument(Document document, String xpath) throws XmlParserException {
        if (xpath.equals(XPATH_IPEAK)) {
          Node node = document.getFirstChild();

          // 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_BACKGROUNDION))
            throw new XmlParserException(
                "IPeak ("
                    + typeattribute.getNodeValue()
                    + ") is not of type: '"
                    + PeakMLWriter.TYPE_BACKGROUNDION
                    + "'");

          // parse this node as a mass chromatogram
          BackgroundIon<? extends Peak> backgroundion = parseBackgroundIon(node);
          if (backgroundion != null) peaks.add(backgroundion);

          //
          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());
        }
      }
Exemple #5
0
 private static IPeak parseIPeak(Node parent) throws XmlParserException {
   Node type = parent.getAttributes().getNamedItem(PeakMLWriter.TYPE);
   if (type == null) return null;
   else if (type.getNodeValue().equals(PeakMLWriter.TYPE_PEAKSET)) return parsePeakSet(parent);
   else if (type.getNodeValue().equals(PeakMLWriter.TYPE_BACKGROUNDION))
     return parseBackgroundIon(parent);
   else if (type.getNodeValue().equals(PeakMLWriter.TYPE_MASSCHROMATOGRAM))
     return parseMassChromatogram(parent);
   else return null;
 }
Exemple #6
0
  private static void parseIPeak(Node parent, IPeak peak) throws XmlParserException {
    // retrieve all the properties
    int scan = -1;
    double retentiontime = -1;
    double mass = -1;
    double intensity = -1;
    int patternid = -1;
    int measurementid = -1;
    //		String sha1 = null;
    Vector<Annotation> annotations = null;

    NodeList nodes = parent.getChildNodes();
    for (int nodeid = 0; nodeid < nodes.getLength(); ++nodeid) {
      Node node = nodes.item(nodeid);
      if (node.getNodeType() != Node.ELEMENT_NODE) continue;

      Element element = (Element) node;
      if (element.getTagName().equals("patternid"))
        patternid = Integer.parseInt(element.getTextContent());
      else if (element.getTagName().equals("measurementid"))
        measurementid = Integer.parseInt(element.getTextContent());
      else if (element.getTagName().equals("annotations")) annotations = parseAnnotations(element);
      else if (element.getTagName().equals("scan"))
        scan = Integer.parseInt(element.getTextContent());
      else if (element.getTagName().equals("retentiontime"))
        retentiontime = Double.parseDouble(element.getTextContent());
      else if (element.getTagName().equals("mass"))
        mass = Double.parseDouble(element.getTextContent());
      else if (element.getTagName().equals("intensity"))
        intensity = Double.parseDouble(element.getTextContent());
      //			else if (element.getTagName().equals("sha1sum"))
      //				sha1 = element.getTextContent();
    }

    // check whether obligatory values are missing
    if (mass == -1 || intensity == -1)
      throw new XmlParserException("Mass and/or intensity information is missing for IPeak.");

    peak.setScanID(scan);
    peak.setRetentionTime(retentiontime);
    peak.setMass(mass);
    peak.setIntensity(intensity);
    peak.setPatternID(patternid);
    peak.setMeasurementID(measurementid);

    if (annotations != null)
      for (Annotation annotation : annotations) peak.addAnnotation(annotation);

    // check whether
    //		if (sha1!=null && !sha1.equals(peak.sha1()))
    //			throw new XmlParserException("SHA1-sum for individual ipeak element does not match.");
  }
Exemple #7
0
  private static Vector<FileInfo> parseFiles(Node parent) throws XmlParserException {
    Vector<FileInfo> files = new Vector<FileInfo>();

    NodeList nodes = parent.getChildNodes();
    for (int nodeid = 0; nodeid < nodes.getLength(); ++nodeid) {
      Node node = nodes.item(nodeid);
      if (node.getNodeType() != Node.ELEMENT_NODE) continue;

      Element element = (Element) node;
      if (element.getTagName().equals("file")) files.add(parseFile(node));
    }

    return files;
  }
Exemple #8
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) _listener.onIPeak(peak);
        } else if (xpath.equals(XPATH_HEADER)) {
          Header header = parseHeader(document.getFirstChild());
          _listener.onHeader(header);
        }
      }
Exemple #9
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
              || !typeattribute.getNodeValue().equals(PeakMLWriter.TYPE_PEAKSET))
            throw new XmlParserException("Failed to locate a type attribute.");

          // parse this node as a mass chromatogram
          IPeakSet<? extends IPeak> peakset = parsePeakSet(node);
          if (peakset != null) peaks.add(peakset);

          //
          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());
        }
      }
Exemple #10
0
  private static Vector<Annotation> parseAnnotations(Node parent) throws XmlParserException {
    Vector<Annotation> annotations = new Vector<Annotation>();

    NodeList nodes = parent.getChildNodes();
    for (int nodeid = 0; nodeid < nodes.getLength(); ++nodeid) {
      Node node = nodes.item(nodeid);
      if (node.getNodeType() != Node.ELEMENT_NODE) continue;

      Element element = (Element) node;
      if (element.getTagName().equals("annotation")) {
        String label = null, value = null, valuetype = null, unit = null;
        NodeList annotation_nodes = element.getChildNodes();
        for (int annotationid = 0; annotationid < annotation_nodes.getLength(); ++annotationid) {
          Node annotation_node = annotation_nodes.item(annotationid);
          if (annotation_node.getNodeType() != Node.ELEMENT_NODE) continue;

          Element annotation_element = (Element) annotation_node;
          if (annotation_element.getTagName().equals("label"))
            label = annotation_element.getTextContent();
          else if (annotation_element.getTagName().equals("value"))
            value = annotation_element.getTextContent();
          else if (annotation_element.getTagName().equals("valuetype"))
            valuetype = annotation_element.getTextContent();
        }

        if (label == null || value == null || valuetype == null)
          throw new XmlParserException("Annotation is missing either: label, value or valuetype");

        Annotation annotation =
            new Annotation(label, value, Annotation.ValueType.valueOf(valuetype));
        annotation.setUnit(unit);
        if (annotation.getValueType() == Annotation.ValueType.ONTOLOGY)
          annotation.setOntologyRef(element.getAttribute("ontologyref"));
        if (element.getAttribute("unit") != null) annotation.setUnit(element.getAttribute("unit"));
        annotations.add(annotation);
      }
    }

    return annotations;
  }
Exemple #11
0
  private static IPeakSet<? extends IPeak> parsePeakSet(Node parent) throws XmlParserException {
    // retrieve all the properties
    Vector<IPeak> peaks = new Vector<IPeak>();

    NodeList nodes = parent.getChildNodes();
    for (int nodeid = 0; nodeid < nodes.getLength(); ++nodeid) {
      Node node = nodes.item(nodeid);
      if (node.getNodeType() != Node.ELEMENT_NODE) continue;

      Element element = (Element) node;
      if (element.getTagName().equals("peaks")) {
        NodeList nodes2 = node.getChildNodes();
        for (int nodeid2 = 0; nodeid2 < nodes2.getLength(); ++nodeid2) {
          Node node2 = nodes2.item(nodeid2);
          if (node2.getNodeType() != Node.ELEMENT_NODE) continue;

          IPeak peak = parseIPeak(node2);
          if (peak != null) peaks.add(peak);
        }
      }
    }

    // create the bugger
    IPeakSet<IPeak> peakset = new IPeakSet<IPeak>(peaks);
    parseIPeak(parent, peakset);
    return peakset;
  }
Exemple #12
0
  private static FileInfo parseFile(Node parent) throws XmlParserException {
    String label = "";
    String name = "";
    String location = "";
    Vector<Annotation> annotations = null;

    NodeList nodes = parent.getChildNodes();
    for (int nodeid = 0; nodeid < nodes.getLength(); ++nodeid) {
      Node node = nodes.item(nodeid);
      if (node.getNodeType() != Node.ELEMENT_NODE) continue;

      Element element = (Element) node;
      if (element.getTagName().equals("label")) label = element.getTextContent();
      else if (element.getTagName().equals("name")) name = element.getTextContent();
      else if (element.getTagName().equals("location")) location = element.getTextContent();
      else if (element.getTagName().equals("annotations")) annotations = parseAnnotations(node);
    }

    FileInfo file = new FileInfo(label, name, location);
    if (annotations != null) file.addAnnotations(annotations);
    return file;
  }
Exemple #13
0
  private static ScanInfo parseScan(Node parent) throws XmlParserException {
    double retentiontime = 0;
    Polarity polarity = Polarity.NEUTRAL;
    Vector<Annotation> annotations = null;

    NodeList nodes = parent.getChildNodes();
    for (int nodeid = 0; nodeid < nodes.getLength(); ++nodeid) {
      Node node = nodes.item(nodeid);
      if (node.getNodeType() != Node.ELEMENT_NODE) continue;

      Element element = (Element) node;
      if (element.getTagName().equals("polarity"))
        polarity = Polarity.valueOf(element.getTextContent());
      else if (element.getTagName().equals("retentiontime"))
        retentiontime = Double.parseDouble(element.getTextContent());
      else if (element.getTagName().equals("annotations")) annotations = parseAnnotations(element);
    }

    ScanInfo scan = new ScanInfo(retentiontime, polarity);
    if (annotations != null) scan.addAnnotations(annotations);
    return scan;
  }
Exemple #14
0
  @SuppressWarnings("unchecked")
  private static BackgroundIon<? extends Peak> parseBackgroundIon(Node parent)
      throws XmlParserException {
    // retrieve the separate peaks
    PeakData<? extends Peak> peakdata = null;

    NodeList nodes = parent.getChildNodes();
    for (int nodeid = 0; nodeid < nodes.getLength(); ++nodeid) {
      Node node = nodes.item(nodeid);
      if (node.getNodeType() != Node.ELEMENT_NODE) continue;

      Element element = (Element) node;
      if (element.getTagName().equals("peakdata")) {
        peakdata = parsePeakData(element);
      }
    }

    // create the bugger
    BackgroundIon<? extends Peak> backgroundion = null;
    if (peakdata.getFactory().getPeakClass().equals(Centroid.class))
      backgroundion = new BackgroundIon<Centroid>((PeakData<Centroid>) peakdata);
    parseIPeak(parent, backgroundion);
    return backgroundion;
  }
Exemple #15
0
  private static PeakData<? extends Peak> parsePeakData(Node parent) throws XmlParserException {
    // get the attributes
    Node typeattribute = parent.getAttributes().getNamedItem(PeakMLWriter.TYPE);
    if (typeattribute == null) throw new XmlParserException("Failed to locate a type attribute.");
    Node sizeattribute = parent.getAttributes().getNamedItem(PeakMLWriter.SIZE);
    if (sizeattribute == null) throw new XmlParserException("Failed to locate a size attribute.");

    int size = Integer.parseInt(sizeattribute.getNodeValue());
    String type = typeattribute.getNodeValue();

    // create the arrays
    int scanids[] = null;
    int patternids[] = null;
    int measurementids[] = null;
    double masses[] = null;
    double intensities[] = null;
    double retentiontimes[] = null;

    // retrieve all the data
    NodeList nodes = parent.getChildNodes();
    for (int nodeid = 0; nodeid < nodes.getLength(); ++nodeid) {
      Node node = nodes.item(nodeid);
      if (node.getNodeType() != Node.ELEMENT_NODE) continue;

      Element element = (Element) node;
      if (element.getTagName().equals("scanids"))
        scanids =
            ByteArray.toIntArray(
                Base64.decode(element.getTextContent()), ByteArray.ENDIAN_LITTLE, 32);
      else if (element.getTagName().equals("patternids"))
        patternids =
            ByteArray.toIntArray(
                Base64.decode(element.getTextContent()), ByteArray.ENDIAN_LITTLE, 32);
      else if (element.getTagName().equals("measurementids"))
        measurementids =
            ByteArray.toIntArray(
                Base64.decode(element.getTextContent()), ByteArray.ENDIAN_LITTLE, 32);
      else if (element.getTagName().equals("masses"))
        masses =
            ByteArray.toDoubleArray(
                Base64.decode(element.getTextContent()), ByteArray.ENDIAN_LITTLE, 32);
      else if (element.getTagName().equals("intensities"))
        intensities =
            ByteArray.toDoubleArray(
                Base64.decode(element.getTextContent()), ByteArray.ENDIAN_LITTLE, 32);
      else if (element.getTagName().equals("retentiontimes"))
        retentiontimes =
            ByteArray.toDoubleArray(
                Base64.decode(element.getTextContent()), ByteArray.ENDIAN_LITTLE, 32);
    }

    // create the PeakData instance
    if (type.equals("centroid"))
      return new PeakData<Centroid>(
          Centroid.factory,
          size,
          scanids,
          patternids,
          measurementids,
          masses,
          intensities,
          retentiontimes);
    return null;
  }