Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

MzDataHandler.h (Maintainer: Marc Sturm)

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // --------------------------------------------------------------------------
00005 //                   OpenMS Mass Spectrometry Framework
00006 // --------------------------------------------------------------------------
00007 //  Copyright (C) 2003-2008 -- Oliver Kohlbacher, Knut Reinert
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Lesser General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2.1 of the License, or (at your option) any later version.
00013 //
00014 //  This library is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //  Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with this library; if not, write to the Free Software
00021 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // --------------------------------------------------------------------------
00024 // $Maintainer: Marc Sturm $
00025 // --------------------------------------------------------------------------
00026 
00027 #ifndef OPENMS_FORMAT_HANDLERS_MZDATAHANDLER_H
00028 #define OPENMS_FORMAT_HANDLERS_MZDATAHANDLER_H
00029 
00030 #include <OpenMS/CONCEPT/Exception.h>
00031 
00032 #include <OpenMS/FORMAT/HANDLERS/XMLHandler.h>
00033 #include <OpenMS/FORMAT/HANDLERS/MzDataExpSettHandler.h>
00034 #include <OpenMS/FORMAT/PeakFileOptions.h>
00035 #include <OpenMS/FORMAT/Base64.h>
00036 #include <OpenMS/KERNEL/MSExperiment.h>
00037 #include <OpenMS/KERNEL/DPeak.h>
00038 #include <OpenMS/KERNEL/PickedPeak1D.h>
00039 #include <OpenMS/CONCEPT/ProgressLogger.h>
00040 
00041 #include <xercesc/sax2/SAX2XMLReader.hpp>
00042 #include <xercesc/framework/MemBufInputSource.hpp>
00043 #include <xercesc/sax2/XMLReaderFactory.hpp>
00044 
00045 #include <sstream>
00046 
00047 namespace OpenMS
00048 {
00049   namespace Internal
00050   {
00051 
00060     template <typename MapType>
00061     class MzDataHandler
00062       : public XMLHandler
00063     {
00064      public:
00067 
00068       MzDataHandler(MapType& exp, const String& filename, const String& version, ProgressLogger& logger)
00069         : XMLHandler(filename, version),
00070           exp_(&exp),
00071           cexp_(0),
00072           peak_count_(0),
00073           meta_id_(),
00074           exp_sett_(),
00075           decoder_(),
00076           spec_write_counter_(1),
00077           in_description_(false),
00078           skip_spectrum_(false),
00079           logger_(logger)
00080       {
00081         cv_terms_.resize(4);
00082         // EnergyUnits
00083         String(";eV;Percent").split(';',cv_terms_[0]);
00084         // ScanMode
00085         String(";SelectedIonDetection;MassScan").split(';',cv_terms_[1]);
00086         // Polarity
00087         String(";Positive;Negative").split(';',cv_terms_[2]);
00088         // ActivationMethod
00089         String(";CID;PSD;PD;SID").split(';',cv_terms_[3]);
00090       }
00091 
00093       MzDataHandler(const MapType& exp, const String& filename, const String& version, const ProgressLogger& logger)
00094         : XMLHandler(filename, version),
00095           exp_(0),
00096           cexp_(&exp),
00097           peak_count_(0),
00098           meta_id_(),
00099           exp_sett_(),
00100           decoder_(),
00101           spec_write_counter_(1),
00102           in_description_(false),
00103           skip_spectrum_(false),
00104           logger_(logger)
00105       {
00106         cv_terms_.resize(4);
00107         // EnergyUnits
00108         String(";eV;Percent").split(';',cv_terms_[0]);
00109         // ScanMode
00110         String(";SelectedIonDetection;MassScan").split(';',cv_terms_[1]);
00111         // Polarity
00112         String(";Positive;Negative").split(';',cv_terms_[2]);
00113         // ActivationMethod
00114         String(";CID;PSD;PD;SID").split(';',cv_terms_[3]);
00115       }
00116 
00118       virtual ~MzDataHandler()
00119       {
00120       }
00122 
00123 
00124       // Docu in base class
00125       virtual void endElement(const XMLCh* const /*uri*/, const XMLCh* const /*local_name*/, const XMLCh* const qname);
00126       
00127       // Docu in base class
00128       virtual void startElement(const XMLCh* const /*uri*/, const XMLCh* const /*local_name*/, const XMLCh* const qname, const xercesc::Attributes& attributes);
00129       
00130       // Docu in base class
00131       virtual void characters(const XMLCh* const chars, const unsigned int length);
00132 
00134       void writeTo(std::ostream& os);
00135       
00136       void setOptions(const PeakFileOptions& opt) { options_ = opt; }
00137 
00138      protected:
00139       
00141       typedef typename MapType::PeakType PeakType;
00143       typedef MSSpectrum<PeakType, std::allocator<PeakType> > SpectrumType;
00144       
00146       MapType* exp_;
00148       const MapType* cexp_;
00149       
00151       PeakFileOptions options_;
00152     
00155       UInt peak_count_;
00156       SpectrumType spec_;
00157       String meta_id_;
00159       std::vector<String> data_to_decode_;
00161       std::vector<Real> data_to_encode_;
00162       std::vector<std::vector<Real> > decoded_list_;
00163       std::vector<std::vector<DoubleReal> > decoded_double_list_;
00164       std::vector<String> array_name_;
00165       std::vector<String> precisions_;
00166       std::vector<String> endians_;
00168       std::stringstream exp_sett_;
00170 
00172       Base64 decoder_;
00173 
00175       UInt spec_write_counter_;
00176       
00178       bool in_description_;
00179       
00181       bool skip_spectrum_;
00182       
00184       const ProgressLogger& logger_;
00185 
00187       void fillData_();
00188 
00196       void cvParam_(const String& name, const String& value);
00197 
00199       inline void writeBinary_(std::ostream& os, UInt size, const String& tag, const String& desc="", int id=-1)
00200       {
00201         os  << "\t\t\t<" << tag;
00202         if (id>=0)
00203         {
00204           os << " id=\"" << id << "\"";
00205         }
00206         os << ">\n";
00207         if (desc!="")
00208         {
00209           os << "\t\t\t\t<arrayName>" << desc << "</arrayName>\n";
00210         }
00211 
00212         std::string str;        
00213         decoder_.encode(data_to_encode_, Base64::BYTEORDER_LITTLEENDIAN, str);
00214         data_to_encode_.clear();
00215         os << "\t\t\t\t<data precision=\"32\" endian=\"little\" length=\""
00216            << size << "\">"
00217            << str
00218            << "</data>\n\t\t\t</" << tag << ">\n";
00219       }
00220 
00225       template <typename ContainerType>
00226       void writeDerivedPeakSupplementalData_( std::ostream& /* os */ , ContainerType const & /* container */ )
00227       {
00228       }
00229 
00234       template <typename PeakType>
00235       void readPeakSupplementalData_( PeakType& /*peak*/, UInt /*n*/)
00236       {
00237       }
00238       
00240       inline double getDecodedValue_(UInt lindex, UInt vindex)
00241       {
00242         if (precisions_[lindex]=="64")
00243         {
00244           //std::cout << "fetching double-precision index " << index << " of member" << member << std::endl;
00245           return decoded_double_list_[lindex][vindex];
00246         }
00247         else
00248         {
00249           //std::cout << "fetching single-precision index " << index << " of member" << member << std::endl;
00250           return (double) decoded_list_[lindex][vindex];
00251         }
00252       }
00253       
00254       private:
00256         MzDataHandler(); 
00257     };
00258 
00259     //--------------------------------------------------------------------------------
00260 
00261     template <typename MapType>
00262     void MzDataHandler<MapType>::characters(const XMLCh* const chars, unsigned int /*length*/)
00263     {
00264       // skip current spectrum
00265       if (skip_spectrum_) return;
00266       
00267       char* transcoded_chars = sm_.convert(chars);
00268       
00269       // collect Experimental Settings
00270       if (in_description_)
00271       {
00272         exp_sett_ << transcoded_chars;
00273         return;
00274       }
00275       
00276       String& current_tag = open_tags_.back();
00277       String& parent_tag = *(open_tags_.end()-2);
00278       
00279       if (current_tag == "comments" && parent_tag=="spectrumDesc")
00280       {
00281         spec_.setComment( transcoded_chars );
00282       }
00283       else if (current_tag == "data")
00284       {
00285         //chars may be split to several chunks => concatenate them
00286         data_to_decode_.back() += transcoded_chars;
00287       }
00288       else if (current_tag == "arrayName")
00289       {
00290         array_name_.push_back(transcoded_chars);
00291         if (spec_.getMetaInfoDescriptions().find(meta_id_) != spec_.getMetaInfoDescriptions().end())
00292         {
00293           spec_.getMetaInfoDescriptions()[meta_id_].setName(transcoded_chars);
00294         }
00295       }
00296       else if (current_tag == "nameOfFile" && parent_tag == "supSourceFile")
00297       {
00298         spec_.getMetaInfoDescriptions()[meta_id_].getSourceFile().setNameOfFile( transcoded_chars );
00299       }
00300       else if (current_tag == "pathToFile" && parent_tag == "supSourceFile")
00301       {
00302         spec_.getMetaInfoDescriptions()[meta_id_].getSourceFile().setPathToFile( transcoded_chars );
00303       }
00304       else if (current_tag == "fileType" && parent_tag == "supSourceFile")
00305       {
00306         spec_.getMetaInfoDescriptions()[meta_id_].getSourceFile().setFileType( transcoded_chars );
00307       }
00308       else
00309       {
00310         String transcoded_chars2 = transcoded_chars;
00311         transcoded_chars2.trim();
00312         if (transcoded_chars2!="") warning(String("Unhandled character content in tag '") + current_tag + "': " + transcoded_chars2);
00313       }
00314     }
00315 
00316     template <typename MapType>
00317     void MzDataHandler<MapType>::startElement(const XMLCh* const /*uri*/, const XMLCh* const /*local_name*/, const XMLCh* const qname, const xercesc::Attributes& attributes)
00318     {
00319       static const XMLCh* s_name = xercesc::XMLString::transcode("name");
00320       static const XMLCh* s_accession = xercesc::XMLString::transcode("accession");
00321       static const XMLCh* s_value = xercesc::XMLString::transcode("value");
00322       static const XMLCh* s_id = xercesc::XMLString::transcode("id");
00323       static const XMLCh* s_count = xercesc::XMLString::transcode("count");
00324       static const XMLCh* s_spectrumtype = xercesc::XMLString::transcode("spectrumType");
00325       static const XMLCh* s_methodofcombination = xercesc::XMLString::transcode("methodOfCombination");
00326       static const XMLCh* s_acqnumber = xercesc::XMLString::transcode("acqNumber");
00327       static const XMLCh* s_mslevel = xercesc::XMLString::transcode("msLevel");
00328       static const XMLCh* s_mzrangestart = xercesc::XMLString::transcode("mzRangeStart");
00329       static const XMLCh* s_mzrangestop = xercesc::XMLString::transcode("mzRangeStop");
00330       static const XMLCh* s_supdataarrayref = xercesc::XMLString::transcode("supDataArrayRef");
00331       static const XMLCh* s_precision = xercesc::XMLString::transcode("precision");
00332       static const XMLCh* s_endian = xercesc::XMLString::transcode("endian");
00333       static const XMLCh* s_length = xercesc::XMLString::transcode("length");
00334       
00335       String tag = sm_.convert(qname);
00336       open_tags_.push_back(tag);
00337       //std::cout << "Start: '" << tag << "'" << std::endl;
00338       
00339       //do nothing as until a new spectrum is reached
00340       if (tag!="spectrum" && skip_spectrum_) return;
00341       
00342       // collect Experimental Settings
00343       if (in_description_)
00344       {
00345         exp_sett_ << '<' << tag;
00346         UInt n=attributes.getLength();
00347         for (UInt i=0; i<n; ++i)
00348         {
00349           exp_sett_ << ' ' << sm_.convert(attributes.getQName(i)) << "=\""  << sm_.convert(attributes.getValue(i)) << '\"';
00350         }
00351         exp_sett_ << '>';
00352         return;
00353       }
00354       
00355       if (tag=="description")
00356       {
00357         exp_sett_ << "<description>";
00358         in_description_ = true; 
00359       }
00360       else if (tag=="cvParam")
00361       {
00362         String accession = attributeAsString_(attributes, s_accession);
00363         String value = "";
00364         optionalAttributeAsString_(value, attributes, s_value);
00365         cvParam_(accession, value);
00366       }
00367       else if (tag=="userParam")
00368       {
00369         String name = attributeAsString_(attributes, s_name);
00370         String value = "";
00371         optionalAttributeAsString_(value, attributes, s_value);
00372         
00373         String& previous_tag = *(open_tags_.end() -2);
00374         if(previous_tag=="spectrumInstrument")
00375         {
00376           spec_.getInstrumentSettings().setMetaValue(name, value);
00377         }
00378         else if(previous_tag=="acquisition")
00379         {
00380           spec_.getAcquisitionInfo().back().setMetaValue(name, value);
00381         }
00382         else if (previous_tag=="ionSelection")
00383         {
00384           spec_.getPrecursorPeak().setMetaValue(name, value);
00385         }
00386         else if (previous_tag=="activation")
00387         {
00388           spec_.getPrecursor().setMetaValue(name, value);
00389         }
00390         else if (previous_tag=="supDataDesc")
00391         {
00392           spec_.getMetaInfoDescriptions()[meta_id_].setMetaValue(name, value);
00393         }
00394         else
00395         {
00396           warning("Invalid userParam: name=\"" + name + ", value=\"" + value + "\"");
00397         }
00398       }
00399       else if (tag=="supDataArrayBinary")
00400       {
00401         meta_id_ = attributeAsString_(attributes, s_id);
00402       }
00403       else if (tag=="spectrum")
00404       {
00405         spec_ = SpectrumType();
00406       }
00407       else if (tag=="spectrumList")
00408       {
00409         if (options_.getMetadataOnly()) throw EndParsingSoftly(__FILE__,__LINE__,__PRETTY_FUNCTION__);
00410         //std::cout << Date::now() << " Reserving space for spectra" << std::endl;
00411         UInt count = attributeAsInt_(attributes, s_count);
00412         exp_->reserve(count);
00413         logger_.startProgress(0,count,"loading mzData file");
00414         //std::cout << Date::now() << " done" << std::endl;
00415       }
00416       else if (tag=="acqSpecification")
00417       {
00418         String tmp_type = attributeAsString_(attributes, s_spectrumtype);
00419         if  (tmp_type == "discrete")
00420         {
00421           spec_.setType(SpectrumSettings::PEAKS);
00422         }
00423         else if (tmp_type == "continuous")
00424         {
00425           spec_.setType(SpectrumSettings::RAWDATA);
00426         }
00427         else
00428         {
00429           spec_.setType(SpectrumSettings::UNKNOWN);
00430           warning(String("Invalid MzData/SpectrumList/Spectrum/SpectrumDescription/SpectrumSettings/acqSpecification/SpectrumType '") + tmp_type + "'.");
00431         }
00432         
00433         spec_.getAcquisitionInfo().setMethodOfCombination(attributeAsString_(attributes, s_methodofcombination));
00434       }
00435       else if (tag=="acquisition")
00436       {
00437         spec_.getAcquisitionInfo().insert(spec_.getAcquisitionInfo().end(), Acquisition());
00438         spec_.getAcquisitionInfo().back().setNumber(attributeAsInt_(attributes, s_acqnumber));
00439       }
00440       else if (tag=="spectrumInstrument" || tag=="acqInstrument")
00441       {
00442         spec_.setMSLevel(attributeAsInt_(attributes, s_mslevel));
00443         DoubleReal start=0.0, stop=0.0;
00444         optionalAttributeAsDouble_(start, attributes, s_mzrangestart);
00445         optionalAttributeAsDouble_(stop, attributes, s_mzrangestop);
00446         spec_.getInstrumentSettings().setMzRangeStart(start);
00447         spec_.getInstrumentSettings().setMzRangeStop(stop);
00448         
00449         if (options_.hasMSLevels() && !options_.containsMSLevel(spec_.getMSLevel()))
00450         {
00451           skip_spectrum_ = true;
00452         }
00453       }
00454       else if (tag=="supDesc")
00455       {
00456         meta_id_ = attributeAsString_(attributes, s_supdataarrayref);
00457       }
00458       else if (tag=="data")
00459       {
00460         // store precision for later
00461         precisions_.push_back(attributeAsString_(attributes, s_precision));
00462         endians_.push_back(attributeAsString_(attributes, s_endian));
00463 
00464         //reserve enough space in spectrum
00465         if (*(open_tags_.end()-2)=="mzArrayBinary")
00466         {
00467           peak_count_ = attributeAsInt_(attributes, s_length);
00468           spec_.getContainer().reserve(peak_count_);
00469         }     
00470       }
00471       else if (tag=="mzArrayBinary")
00472       {
00473         array_name_.push_back("mz");
00474         data_to_decode_.resize(data_to_decode_.size()+1);
00475       }
00476       else if (tag=="intenArrayBinary")
00477       {
00478           array_name_.push_back("intens");
00479           data_to_decode_.resize(data_to_decode_.size()+1);
00480       }
00481       else if (tag=="arrayName")
00482       {
00483           // Note: name is set in closing tag as it is CDATA
00484           data_to_decode_.resize(data_to_decode_.size()+1);
00485       }
00486       //std::cout << "end startelement" << std::endl;
00487     }
00488 
00489 
00490     template <typename MapType>
00491     void MzDataHandler<MapType>::endElement(const XMLCh* const /*uri*/, const XMLCh* const /*local_name*/, const XMLCh* const qname)
00492     {
00493       static UInt scan_count = 0;
00494       
00495       static const XMLCh* s_description = xercesc::XMLString::transcode("description");
00496       static const XMLCh* s_spectrum = xercesc::XMLString::transcode("spectrum");
00497       static const XMLCh* s_mzdata = xercesc::XMLString::transcode("mzData");
00498       
00499       open_tags_.pop_back();      
00500       //std::cout << "End: '" << sm_.convert(qname) << "'" << std::endl;
00501       
00502       if (in_description_)  // collect Experimental Settings
00503       {
00504         exp_sett_ << "</" << sm_.convert(qname) << ">\n";
00505         if (!equal_(qname,s_description)) return;
00506       }
00507       
00508       if(equal_(qname,s_description))
00509       {
00510         // initialize parser
00511         xercesc::XMLPlatformUtils::Initialize();
00512         xercesc::SAX2XMLReader* parser = xercesc::XMLReaderFactory::createXMLReader();
00513         parser->setFeature(xercesc::XMLUni::fgSAX2CoreNameSpaces,false);
00514         parser->setFeature(xercesc::XMLUni::fgSAX2CoreNameSpacePrefixes,false);
00515         
00516         MzDataExpSettHandler handler(exp_->getExperimentalSettings(),file_);
00517         handler.resetErrors();
00518         parser->setContentHandler(&handler);
00519         parser->setErrorHandler(&handler);
00520         
00521         String tmp(exp_sett_.str().c_str());
00522         xercesc::MemBufInputSource source((const XMLByte*)(tmp.c_str()), tmp.size(), "dummy");
00523         parser->parse(source);
00524         delete(parser);
00525         
00526         in_description_ = false;
00527       }
00528       else if(equal_(qname,s_spectrum))
00529       {
00530         if (!skip_spectrum_)
00531         {
00532           fillData_();
00533           exp_->push_back(spec_);
00534         }
00535         skip_spectrum_ = false;
00536         logger_.setProgress(++scan_count);
00537         decoded_list_.clear();
00538         decoded_double_list_.clear();
00539         data_to_decode_.clear();
00540         array_name_.clear();
00541         precisions_.clear();
00542         endians_.clear();
00543       }
00544       else if(equal_(qname,s_mzdata))
00545       {
00546         logger_.endProgress();
00547         scan_count = 0;
00548       }
00549 
00550       sm_.clear();
00551     }
00552 
00553     template <typename MapType>
00554     void MzDataHandler<MapType>::cvParam_(const String& accession, const String& value)
00555     {
00556       String error = "";
00557 
00558       String& parent_tag = *(open_tags_.end()-2);
00559       if(parent_tag=="spectrumInstrument")
00560       {
00561         if (accession=="PSI:1000036") //Scan Mode
00562         {
00563           spec_.getInstrumentSettings().setScanMode((InstrumentSettings::ScanMode)cvStringToEnum_(1, value,"scan mode"));
00564         }
00565         else if (accession=="PSI:1000038") //Time in minutes
00566         {
00567           spec_.setRT(asFloat_(value)*60); //Minutes to seconds
00568           if (options_.hasRTRange() && !options_.getRTRange().encloses(DPosition<1>(spec_.getRT())))
00569           {
00570             skip_spectrum_=true;
00571           }
00572         }
00573         else if (accession=="PSI:1000039") //Time in seconds
00574         {
00575           spec_.setRT(asFloat_(value));
00576           if (options_.hasRTRange() && !options_.getRTRange().encloses(DPosition<1>(spec_.getRT())))
00577           {
00578             skip_spectrum_=true;
00579           }
00580         }
00581         else if (accession=="PSI:1000037") //Polarity
00582         {
00583           spec_.getInstrumentSettings().setPolarity((IonSource::Polarity)cvStringToEnum_(2, value,"polarity") );        
00584         }
00585         else 
00586         {
00587           error = "SpectrumDescription.SpectrumSettings.SpectrumInstrument";
00588         }
00589       }
00590       else if(parent_tag=="ionSelection")
00591       {
00592         if (accession=="PSI:1000040") //m/z
00593         {
00594           spec_.getPrecursorPeak().getPosition()[0] = asFloat_(value);      
00595         }
00596         else if (accession=="PSI:1000041") //Charge
00597         {
00598           spec_.getPrecursorPeak().setCharge(asInt_(value));    
00599         }
00600         else if (accession=="PSI:1000042") //Intensity
00601         {
00602           spec_.getPrecursorPeak().setIntensity(asFloat_(value));   
00603         }
00604         else if (accession=="PSI:1000043") //Intensity unit (not really handled)
00605         {
00606           spec_.getPrecursorPeak().setMetaValue("#IntensityUnits", value);
00607         }
00608         else
00609         {
00610           error = "PrecursorList.Precursor.IonSelection.UserParam";
00611         }
00612       }
00613       else if (parent_tag=="activation") 
00614       {
00615         if (accession=="PSI:1000044") //Method
00616         {
00617           spec_.getPrecursor().setActivationMethod((Precursor::ActivationMethod)cvStringToEnum_(3, value,"activation method"));
00618         }
00619         else if (accession=="PSI:1000045") //Energy
00620         {
00621           spec_.getPrecursor().setActivationEnergy( asFloat_(value) );
00622         }
00623         else if (accession=="PSI:1000046") //Energy unit
00624         {
00625           spec_.getPrecursor().setActivationEnergyUnit((Precursor::EnergyUnits)cvStringToEnum_(0, value, "energy unit"));
00626         }
00627         else 
00628         {
00629           error = "PrecursorList.Precursor.Activation.UserParam";
00630         }
00631       }
00632       else if (parent_tag=="supDataDesc") 
00633       {
00634         //no terms defined in ontology
00635         error = "supDataDesc.UserParam";
00636       }
00637       else if (parent_tag=="acquisition")
00638       {
00639          //no terms defined in ontology
00640          error = "spectrumDesc.spectrumSettings.acquisitionSpecification.acquisition.UserParam";
00641       }
00642       else
00643       {
00644         warning(String("Unexpected cvParam: accession=\"") + accession + ", value=\"" + value + "\" in tag " + parent_tag);
00645       }
00646 
00647       if (error != "")
00648       {
00649         warning(String("Invalid cvParam: accession=\"") + accession + ", value=\"" + value + "\" in " + error);
00650       }
00651       //std::cout << "End of MzDataHander::cvParam_" << std::endl;
00652     }
00653 
00654     template <typename MapType>
00655     void MzDataHandler<MapType>::fillData_()
00656     {
00657       std::vector<Real> decoded;
00658       std::vector<DoubleReal> decoded_double;
00659       
00660       // data_to_decode is an encoded spectrum, represented as
00661       // vector of base64-encoded strings:
00662       // Each string represents one property (e.g. mzData) and decodes
00663       // to a vector of property values - one value for every peak in the spectrum.
00664       for (UInt i=0; i<data_to_decode_.size(); i++)
00665       {
00666         if (precisions_[i]=="64") // precision 64 Bit
00667         {
00668           if (endians_[i]=="big")
00669           {
00670             //std::cout << "nr. " << i << ": decoding as high-precision big endian" << std::endl;
00671             decoder_.decode(data_to_decode_[i], Base64::BYTEORDER_BIGENDIAN, decoded_double);
00672           }
00673           else
00674           {
00675             //std::cout << "nr. " << i << ": decoding as high-precision little endian" << std::endl;
00676             decoder_.decode(data_to_decode_[i], Base64::BYTEORDER_LITTLEENDIAN, decoded_double);
00677           }
00678           // push_back the decoded double data - and an empty one into
00679           // the dingle-precision vector, so that we don't mess up the index
00680           //std::cout << "list size: " << decoded_double.size() << std::endl;
00681           decoded_double_list_.push_back(decoded_double);
00682           decoded_list_.push_back(std::vector<float>());
00683         }
00684         else
00685         {                     // precision 32 Bit
00686           if (endians_[i]=="big")
00687           {
00688             //std::cout << "nr. " << i << ": decoding as low-precision big endian" << std::endl;
00689             decoder_.decode(data_to_decode_[i], Base64::BYTEORDER_BIGENDIAN, decoded);
00690           }
00691           else
00692           {
00693             //std::cout << "nr. " << i << ": decoding as low-precision little endian" << std::endl;
00694             decoder_.decode(data_to_decode_[i], Base64::BYTEORDER_LITTLEENDIAN, decoded);
00695           }
00696           //std::cout << "list size: " << decoded.size() << std::endl;
00697           decoded_list_.push_back(decoded);
00698           decoded_double_list_.push_back(std::vector<double>());
00699         }
00700       }
00701 
00702       // this works only if MapType::PeakType is at leat DRawDataPoint
00703       {
00704         const int MZ = 0;
00705         const int INTENS = 1;
00706       
00707         bool mz_precision_64 = true;
00708         if (precisions_[MZ]=="32")
00709         {
00710           mz_precision_64 = false;
00711         }
00712         bool int_precision_64 = true;
00713         if (precisions_[INTENS]=="32")
00714         {
00715           int_precision_64 = false;
00716         }
00717 
00718         //push_back the peaks into the container        
00719         for (UInt n = 0 ; n < peak_count_ ; n++)
00720         {
00721           DoubleReal mz = mz_precision_64 ? decoded_double_list_[MZ][n] : (DoubleReal)(decoded_list_[MZ][n]);
00722           DoubleReal intensity = int_precision_64 ? decoded_double_list_[INTENS][n] : (DoubleReal)(decoded_list_[INTENS][n]);
00723           if ((!options_.hasMZRange() || options_.getMZRange().encloses(DPosition<1>(mz)))
00724            && (!options_.hasIntensityRange() || options_.getIntensityRange().encloses(DPosition<1>(intensity))))
00725           {
00726             spec_.insert(spec_.end(), PeakType());
00727             spec_.back().setIntensity(intensity);
00728             spec_.back().setPosition(mz);
00729             //read supplemental data for derived classes (do nothing for DPeak)
00730             readPeakSupplementalData_(spec_.back(), n);
00731           }
00732         }
00733       }
00734     }
00735 
00736     template <typename MapType>
00737     void MzDataHandler<MapType>::writeTo(std::ostream& os)
00738     {
00739       logger_.startProgress(0,cexp_->size(),"storing mzData file");
00740       
00741       os << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
00742          << "<mzData version=\"1.05\" accessionNumber=\"\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://psidev.sourceforge.net/ms/xml/mzdata/mzdata.xsd\">\n";
00743 
00744       // delegate control to ExperimentalSettings handler
00745       Internal::MzDataExpSettHandler handler( cexp_->getExperimentalSettings(),"");
00746       handler.writeTo(os);
00747 
00748       if (cexp_->size()!=0)
00749       {
00750         os << "\t<spectrumList count=\"" << cexp_->size() << "\">\n";
00751         int spectrum_ref = -1;
00752         for (UInt s=0; s<cexp_->size(); s++)
00753         {
00754           logger_.setProgress(s);
00755           //std::cout << "writing scan" << std::endl;
00756           
00757           const SpectrumType& spec = (*cexp_)[s];
00758   
00759           os << "\t\t<spectrum id=\"" << spec_write_counter_++ << "\">\n"
00760              << "\t\t\t<spectrumDesc>\n"
00761              << "\t\t\t\t<spectrumSettings>\n";
00762   
00763           if (!spec.getAcquisitionInfo().empty())
00764           {
00765             os << "\t\t\t\t\t<acqSpecification spectrumType=\"";
00766             if (spec.getType()==SpectrumSettings::PEAKS)
00767             {
00768               os << "discrete";
00769             }
00770             else if (spec.getType()==SpectrumSettings::RAWDATA)
00771             {
00772               os << "continuous";
00773             }
00774   
00775             os << "\" methodOfCombination=\""
00776                << spec.getAcquisitionInfo().getMethodOfCombination() << "\" count=\""
00777                << spec.getAcquisitionInfo().size() << "\">\n";
00778             for (UInt i=0; i<spec.getAcquisitionInfo().size(); ++i)
00779             {
00780               const Acquisition& ac = spec.getAcquisitionInfo()[i];
00781               os << "\t\t\t\t\t\t<acquisition acqNumber=\"" << ac.getNumber() << "\">\n";
00782               writeUserParam_(os, ac, 7);
00783               os << "\t\t\t\t\t\t</acquisition>\n";
00784             }
00785             os << "\t\t\t\t\t</acqSpecification>\n";
00786           }
00787   
00788           const InstrumentSettings& iset = spec.getInstrumentSettings();
00789           os << "\t\t\t\t\t<spectrumInstrument msLevel=\"" << spec.getMSLevel()
00790              << "\"";
00791   
00792           if (spec.getMSLevel()==1) spectrum_ref = spec_write_counter_-1;
00793           if (iset.getMzRangeStart() != 0 && iset.getMzRangeStop() != 0)
00794           {
00795             os << " mzRangeStart=\""
00796                << iset.getMzRangeStart() << "\" mzRangeStop=\""
00797                << iset.getMzRangeStop() << "\"";
00798           }
00799           os << ">\n";
00800   
00801           writeCVS_(os, spec.getInstrumentSettings().getScanMode(), 1, "1000036", "ScanMode",6);
00802           writeCVS_(os, spec.getInstrumentSettings().getPolarity(), 2, "1000037", "Polarity",6);
00803           //Retiontion time already in TimeInSeconds
00804           writeCVS_(os, spec.getRT(), "1000039", "TimeInSeconds",6);
00805           writeUserParam_(os, spec.getInstrumentSettings(), 6);
00806           os  << "\t\t\t\t\t</spectrumInstrument>\n\t\t\t\t</spectrumSettings>\n";
00807   
00808           typedef typename SpectrumType::PrecursorPeakType PrecursorPeak;
00809           if (spec.getPrecursorPeak() != PrecursorPeak()
00810               || spec.getPrecursor() != Precursor())
00811           {
00812             os  << "\t\t\t\t<precursorList count=\"1\">\n"
00813                 << "\t\t\t\t\t<precursor msLevel=\"2\" spectrumRef=\""
00814                 << spectrum_ref << "\">\n";
00815             os << "\t\t\t\t\t\t<ionSelection>\n";
00816             if (spec.getPrecursorPeak() != PrecursorPeak())
00817             {
00818               const PrecursorPeak& peak = spec.getPrecursorPeak();
00819               writeCVS_(os, peak.getPosition()[0], "1000040", "MassToChargeRatio",7);
00820               writeCVS_(os, peak.getCharge(), "1000041", "ChargeState",7);
00821               writeCVS_(os, peak.getIntensity(), "1000042", "Intensity",7);
00822               if (peak.metaValueExists("#IntensityUnits"))
00823               {
00824                 writeCVS_(os, String(peak.getMetaValue("#IntensityUnits")), "1000043", "IntensityUnits",7);
00825               }
00826               writeUserParam_(os, peak, 7);
00827             }
00828             os << "\t\t\t\t\t\t</ionSelection>\n";
00829             os << "\t\t\t\t\t\t<activation>\n";
00830             if (spec.getPrecursor() != Precursor())
00831             {
00832               const Precursor& prec = spec.getPrecursor();
00833               writeCVS_(os, prec.getActivationMethod(), 3, "1000044", "Method",7);
00834               writeCVS_(os, prec.getActivationEnergy(), "1000045", "CollisionEnergy",7);
00835               writeCVS_(os, prec.getActivationEnergyUnit(), 0,"1000046", "EnergyUnits",7);
00836               writeUserParam_(os, prec,7);
00837             }
00838             os << "\t\t\t\t\t\t</activation>\n";
00839             os << "\t\t\t\t\t</precursor>\n"
00840                << "\t\t\t\t</precursorList>\n";
00841           }
00842           os << "\t\t\t</spectrumDesc>\n";
00843   
00844           typedef const std::map<String,MetaInfoDescription> Map;
00845           if (spec.getMetaInfoDescriptions().size()>0)
00846           {
00847             for (Map::const_iterator it = spec.getMetaInfoDescriptions().begin();
00848                  it != spec.getMetaInfoDescriptions().end(); ++it)
00849             {
00850               os << "\t\t\t<supDesc supDataArrayRef=\"" << it->first << "\">\n";
00851               if (!it->second.isMetaEmpty())
00852               {
00853                 os << "\t\t\t\t<supDataDesc>\n";
00854                 writeUserParam_(os, it->second, 5);
00855                 os << "\t\t\t\t</supDataDesc>\n";
00856               }
00857               if (it->second.getSourceFile()!=SourceFile())
00858               {
00859                 os << "\t\t\t\t<supSourceFile>\n"
00860                     << "\t\t\t\t\t<nameOfFile>" << it->second.getSourceFile().getNameOfFile()
00861                     << "</nameOfFile>\n"
00862                     << "\t\t\t\t\t<pathToFile>" << it->second.getSourceFile().getPathToFile()
00863                     << "</pathToFile>\n";
00864                 if (it->second.getSourceFile().getFileType()!="") os << "\t\t\t\t\t<fileType>"
00865                   << it->second.getSourceFile().getFileType() << "</fileType>\n";
00866                 os << "\t\t\t\t</supSourceFile>\n";
00867               }
00868               os << "\t\t\t</supDesc>\n";
00869             }
00870           }
00871   
00872           // m/z
00873   //        float* tmp = decoder_[0].getFloatBuffer(spec.size());
00874           data_to_encode_.clear();
00875           for (UInt i=0; i<spec.size(); i++)
00876           {
00877             data_to_encode_.push_back(spec.getContainer()[i].getPosition()[0]);
00878           }
00879           
00880           writeBinary_(os,spec.size(),"mzArrayBinary");
00881   
00882           // intensity
00883           data_to_encode_.clear();
00884           for (UInt i=0; i<spec.size(); i++)
00885           {
00886             data_to_encode_.push_back(spec.getContainer()[i].getIntensity());
00887           }
00888           
00889           writeBinary_(os,spec.size(),"intenArrayBinary");
00890   
00891           // write the supplementary data for picked peaks (is a no-op otherwise)
00892           if (options_.getWriteSupplementalData())
00893           {
00894             this->writeDerivedPeakSupplementalData_(os, spec.getContainer());
00895           }
00896   
00897           os <<"\t\t</spectrum>\n";
00898         }
00899       }
00900       else
00901       {
00902         os << "\t<spectrumList count=\"1\">\n";
00903         os <<"\t\t<spectrum id=\"1\">\n";
00904         os <<"\t\t\t<spectrumDesc>\n";
00905         os <<"\t\t\t\t<spectrumSettings>\n";
00906         os <<"\t\t\t\t\t<spectrumInstrument msLevel=\"1\"/>\n";
00907         os <<"\t\t\t\t</spectrumSettings>\n";
00908         os <<"\t\t\t</spectrumDesc>\n";
00909         os <<"\t\t\t<mzArrayBinary>\n";
00910         os <<"\t\t\t\t<data length=\"0\" endian=\"little\" precision=\"32\"></data>\n";
00911         os <<"\t\t\t</mzArrayBinary>\n";
00912         os <<"\t\t\t<intenArrayBinary>\n";
00913         os <<"\t\t\t\t<data length=\"0\" endian=\"little\" precision=\"32\"></data>\n";
00914         os <<"\t\t\t</intenArrayBinary>\n";
00915         os <<"\t\t</spectrum>\n";
00916       }
00917       os << "\t</spectrumList>\n</mzData>\n";
00918       
00919       logger_.endProgress();
00920     }
00921 
00922 
00928     template <>
00929     template <>
00930     void MzDataHandler <MSExperiment<PickedPeak1D > >::writeDerivedPeakSupplementalData_ < DPeakArray<PickedPeak1D > >( std::ostream& os, DPeakArray<PickedPeak1D > const & container);
00931 
00937     template <>
00938     template <>
00939     void MzDataHandler <MSExperiment<PickedPeak1D > >::readPeakSupplementalData_ < PickedPeak1D >( PickedPeak1D& peak, UInt n);
00940 
00941 
00942   } // namespace Internal
00943 
00944 } // namespace OpenMS
00945 
00946 #endif

Generated Tue Apr 1 15:36:36 2008 -- using doxygen 1.5.4 OpenMS / TOPP 1.1