/* (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); } }
/** * Helper to handle metadata in the document * * @throws CheckedIOException */ private void writeMetaData(PeakList peaklist) { // skip if previously done if (hasWrittenMetaData) { return; } // flag meta-data as writter hasWrittenMetaData = true; // try to get meta-data if it is available if (peaklist instanceof MzXMLPeakList) { MzXMLPeakList mzxml = (MzXMLPeakList) peaklist; msRun = mzxml.getMsRun(); } else { msRun = new MsRun(); } try { // write out all the parent files for (ParentFile pf : msRun.getParentFiles()) { xmlw.writeEmptyElement("parentFile"); writeNonNull(xmlw, "fileName", pf.getFileName()); writeNonNull(xmlw, "fileType", pf.getFileType()); writeNonNull(xmlw, "fileSha1", pf.getFileSHA()); } // write out all the msInstrument information MsInstrument msInstrument = msRun.getMsInstrument(); if (msInstrument != null) { xmlw.writeStartElement("msInstrument"); // write out msManufacture if (msInstrument.getMsManufacturer() != null) { xmlw.writeEmptyElement("msManufacturer"); writeNonNull(xmlw, "category", msInstrument.getMsManufacturer().getCategory()); if (msInstrument.getMsManufacturer().getValue() != null) writeNonNull(xmlw, "value", msInstrument.getMsManufacturer().getValue()); } // write out msModel if (msInstrument.getMsModel() != null) { xmlw.writeEmptyElement("msModel"); writeNonNull(xmlw, "category", msInstrument.getMsModel().getCategory()); writeNonNull(xmlw, "value", msInstrument.getMsModel().getValue()); } // write out msManufacture if (msInstrument.getMsIonisation() != null) { xmlw.writeEmptyElement("msIonisation"); writeNonNull(xmlw, "category", msInstrument.getMsIonisation().getCategory()); writeNonNull(xmlw, "value", msInstrument.getMsIonisation().getValue()); } // write out msManufacture if (msInstrument.getMsMassAnalyzer() != null) { xmlw.writeEmptyElement("msMassAnalyzer"); writeNonNull(xmlw, "category", msInstrument.getMsMassAnalyzer().getCategory()); writeNonNull(xmlw, "value", msInstrument.getMsMassAnalyzer().getValue()); } // write out msManufacture if (msInstrument.getMsDetector() != null) { xmlw.writeEmptyElement("msDetector"); writeNonNull(xmlw, "category", msInstrument.getMsDetector().getCategory()); writeNonNull(xmlw, "value", msInstrument.getMsDetector().getValue()); } // write out software if (msInstrument.getSoftware() != null) { xmlw.writeEmptyElement("software"); writeNonNull(xmlw, "type", msInstrument.getSoftware().getType()); writeNonNull(xmlw, "name", msInstrument.getSoftware().getName()); writeNonNull(xmlw, "version", msInstrument.getSoftware().getVersion()); writeNonNull(xmlw, "completionTime", msInstrument.getSoftware().getCompletionTime()); } // write out msResolution if (msInstrument.getMsResolution() != null) { xmlw.writeEmptyElement("msResolution"); writeNonNull(xmlw, "category", msInstrument.getMsResolution().getCategory()); writeNonNull(xmlw, "value", msInstrument.getMsResolution().getValue()); } // write out operator if (msInstrument.getOperator() != null) { xmlw.writeEmptyElement("operator"); writeNonNull(xmlw, "first", msInstrument.getOperator().getFirst()); writeNonNull(xmlw, "last", msInstrument.getOperator().getLast()); writeNonNull(xmlw, "email", msInstrument.getOperator().getEmail()); writeNonNull(xmlw, "phone", msInstrument.getOperator().getPhone()); writeNonNull(xmlw, "URI", msInstrument.getOperator().getURI()); } xmlw.writeEndElement(); } // write out all the dataProcessing information for (DataProcessing dp : msRun.getDataProcessings()) { xmlw.writeStartElement("dataProcessing"); writeNonNull(xmlw, "intensityCutoff", dp.getIntensityCutoff()); writeNonNull(xmlw, "centroided", dp.getCentroided()); writeNonNull(xmlw, "deisotoped", dp.getDeisotoped()); writeNonNull(xmlw, "chargeDeconvoluted", dp.getChargeDeconvoluted()); writeNonNull(xmlw, "spotIntegration", dp.getSpotIntegration()); // get all software Software software = dp.getSoftware(); // write out software if (software != null) { xmlw.writeEmptyElement("software"); writeNonNull(xmlw, "type", software.getType()); writeNonNull(xmlw, "name", software.getName()); writeNonNull(xmlw, "version", software.getVersion()); writeNonNull(xmlw, "completionTime", software.getCompletionTime()); } xmlw.writeEndElement(); } } catch (XMLStreamException e) { throw new RuntimeException("Can't write mzXML meta-data!", e); } }