コード例 #1
0
  /** Test of filter method, of class org.proteomecommons.io.filter.IntensityPeakListFilter. */
  public void testFilter() {
    System.out.println("filter");

    GenericPeakList peaklist = new GenericPeakList();
    GenericPeak gpa = new GenericPeak();
    GenericPeak gpb = new GenericPeak();
    GenericPeak parent = new GenericPeak();
    gpa.setIntensity(5);
    gpa.setMassOverCharge(2);

    gpb.setIntensity(1);
    gpb.setMassOverCharge(1);

    parent.setCharge(1);
    parent.setIntensity(4);
    parent.setMassOverCharge(5);
    peaklist.setParentPeak(parent);
    peaklist.setPeaks(new Peak[] {gpa, gpb});

    IntensityPeakListFilter instance = new IntensityPeakListFilter(1);

    PeakList res = instance.filter(peaklist);

    assertEquals(res.getParentPeak().getIntensity(), parent.getIntensity());
    assertEquals(res.getPeaks().length, 1);
    assertEquals(res.getPeaks()[0].getIntensity(), gpa.getIntensity());
    assertEquals(res.getPeaks()[0].getMassOverCharge(), gpa.getMassOverCharge());
  }
コード例 #2
0
ファイル: MzXMLPeakListWriter.java プロジェクト: mrjato/IO
  /* (non-Javadoc)
   * @see org.proteomecommons.io.PeakListWriter#startPeakList()
   */
  public void write(PeakList pl) {
    try {
      // TODO Auto-generated method stub
      if (!hasWrittenMetaData) {
        writeMetaData(pl);
      }

      // write peak list
      Peak[] peaks = pl.getPeaks();
      // assume 32 bit for compatibility
      int byteBufferCapacity = (32 / 8) * 2 * peaks.length;
      // if 64 bit, recalc precision
      if (!isUse32BitPrecision()) {
        // bytes needed = (bits per double/bits per byte)*2 double per peak*number of peaks
        byteBufferCapacity = (64 / 8) * 2 * peaks.length;
      }

      // make the buffer
      ByteBuffer bb = ByteBuffer.allocate(byteBufferCapacity);

      // network byte order is big endian http://mindprod.com/jgloss/endian.html
      bb = bb.order(ByteOrder.BIG_ENDIAN);

      // flatten the peaks into the buffer
      for (int i = 0; i < peaks.length; i++) {
        // dump the peak bytes into the buffer
        Peak current = (Peak) peaks[i];
        // assume 32 bit precision
        if (isUse32BitPrecision()) {
          bb.putFloat((float) current.getMassOverCharge());
          bb.putFloat((float) current.getIntensity());
        } else {
          bb.putDouble(current.getMassOverCharge());
          bb.putDouble(current.getIntensity());
        }
      }

      // convert bytes
      String encodedBytes = Base64.encodeBytes(bb.array(), false);

      // finally, write out the xml
      try {
        // setup scan element
        xmlw.writeStartElement("scan");

        // write out a unique scan number to identify it
        xmlw.writeAttribute("num", "" + scanNumber);
        scanNumber++;
        // write out the ms level
        if (pl.getTandemCount() != PeakList.UNKNOWN_TANDEM_COUNT) {
          xmlw.writeAttribute("msLevel", "" + pl.getTandemCount());
        }
        // number of peaks
        xmlw.writeAttribute("peaksCount", "" + peaks.length);

        // if it is an MZXMLPeakList write the other meta-data
        if (pl instanceof MzXMLPeakList) {
          MzXMLPeakList mzxmlpl = (MzXMLPeakList) pl;
          writeNonNull(xmlw, "polarity", mzxmlpl.getPolarity());
          writeNonNull(xmlw, "scanType", mzxmlpl.getScanType());
          writeNonNull(xmlw, "centroided", mzxmlpl.getCentroided());
          writeNonNull(xmlw, "deisotoped", mzxmlpl.getDeisotoped());
          writeNonNull(xmlw, "chargeDeconvoluted", mzxmlpl.getChargeDeconvoluted());
          writeNonNull(xmlw, "retentionTime", mzxmlpl.getRetentionTime());
          writeNonNull(xmlw, "ionisationEnergy", mzxmlpl.getIonisationEnergy());
          writeNonNull(xmlw, "collisionEnergy", mzxmlpl.getCollisionEnergy());
          writeNonNull(xmlw, "startMz", mzxmlpl.getStartMz());
          writeNonNull(xmlw, "endMz", mzxmlpl.getEndMz());
          writeNonNull(xmlw, "lowMz", mzxmlpl.getLowMz());
          writeNonNull(xmlw, "highMz", mzxmlpl.getHighMz());
          writeNonNull(xmlw, "basePeakMz", mzxmlpl.getBasePeakMz());
          writeNonNull(xmlw, "basePeakIntensity", mzxmlpl.getBasePeakIntensity());
          writeNonNull(xmlw, "totIonCurrent", mzxmlpl.getTotIonCurrent());
        }

        // write precursor mz info
        if (pl.getParentPeak() != null) {
          Peak parentPeak = pl.getParentPeak();
          if (parentPeak.getIntensity() == Peak.UNKNOWN_INTENSITY) {
            throw new RuntimeException("mzXML requires an intensity value for precursor peaks.");
          }
          if (parentPeak.getMassOverCharge() == Peak.UNKNOWN_MZ) {
            throw new RuntimeException("mzXML requires an mz value for precursor peaks.");
          }
          // close the precursorMz element
          xmlw.writeStartElement("precursorMz");

          writeNonNull(
              xmlw, "precursorIntensity", Float.toString((float) parentPeak.getIntensity()));
          if (parentPeak.getCharge() != Peak.UNKNOWN_CHARGE) {
            xmlw.writeAttribute("precursorCharge", Integer.toString(parentPeak.getCharge()));
          }
          xmlw.writeCharacters(Float.toString((float) parentPeak.getMassOverCharge()));
          // close the precursorMz element
          xmlw.writeEndElement();
        }

        // setup peaks element
        xmlw.writeStartElement("peaks");

        if (isUse32BitPrecision()) {
          xmlw.writeAttribute("precision", "32");
        } else {
          xmlw.writeAttribute("precision", "64");
        }
        // these two values are always the same
        xmlw.writeAttribute("byteOrder", "network");
        xmlw.writeAttribute("pairOrder", "m/z-int");
        xmlw.writeCharacters(encodedBytes);

        // end everything
        xmlw.writeEndElement(); // peaks
        xmlw.writeEndElement(); // scan
      } catch (XMLStreamException e) {
        throw new RuntimeException("Can't write peak list!", e);
      }
    } catch (Exception e) {
      throw new RuntimeException("Can't write peak list!", e);
    }
  }