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

CachedmzML.h

Go to the documentation of this file.
00001 // --------------------------------------------------------------------------
00002 //                   OpenMS -- Open-Source Mass Spectrometry
00003 // --------------------------------------------------------------------------
00004 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
00005 // ETH Zurich, and Freie Universitaet Berlin 2002-2012.
00006 //
00007 // This software is released under a three-clause BSD license:
00008 //  * Redistributions of source code must retain the above copyright
00009 //    notice, this list of conditions and the following disclaimer.
00010 //  * Redistributions in binary form must reproduce the above copyright
00011 //    notice, this list of conditions and the following disclaimer in the
00012 //    documentation and/or other materials provided with the distribution.
00013 //  * Neither the name of any author or any participating institution
00014 //    may be used to endorse or promote products derived from this software
00015 //    without specific prior written permission.
00016 // For a full list of authors, refer to the file AUTHORS.
00017 // --------------------------------------------------------------------------
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
00022 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00025 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00027 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00028 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // --------------------------------------------------------------------------
00031 // $Maintainer: Hannes Roest $
00032 // $Authors: Hannes Roest $
00033 // --------------------------------------------------------------------------
00034 
00035 #ifndef OPENMS_ANALYSIS_OPENSWATH_CACHEDMZML_H
00036 #define OPENMS_ANALYSIS_OPENSWATH_CACHEDMZML_H
00037 
00038 #include <OpenMS/ANALYSIS/OPENSWATH/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h>
00039 
00040 #include <OpenMS/CONCEPT/Types.h>
00041 #include <OpenMS/CONCEPT/ProgressLogger.h>
00042 
00043 #include <OpenMS/KERNEL/MSExperiment.h>
00044 #include <OpenMS/FORMAT/MzMLFile.h>
00045 
00046 #include <fstream>
00047 
00048 #define MAGIC_NUMBER 8093
00049 
00050 namespace OpenMS
00051 {
00060   class OPENMS_DLLAPI CachedmzML :
00061     public ProgressLogger
00062   {
00063     int int_field_;
00064     double dbl_field_;
00065 
00066 public:
00067 
00068     typedef MSExperiment<Peak1D> MapType;
00069     typedef MSSpectrum<Peak1D> SpectrumType;
00070     typedef MSChromatogram<ChromatogramPeak> ChromatogramType;
00071 #if 1
00072     // double means twice the file size
00073     typedef double DatumSingleton;
00074 #else
00075     // float means half the file size
00076     typedef float DatumSingleton;
00077 #endif
00078     typedef std::vector<DatumSingleton> Datavector;
00079 
00083 
00084     CachedmzML()
00085     {
00086     }
00087 
00089     ~CachedmzML()
00090     {
00091     }
00092 
00094     CachedmzML& operator=(const CachedmzML& rhs)
00095     {
00096       if (&rhs == this)
00097         return *this;
00098 
00099       spectra_index_ = rhs.spectra_index_;
00100       chrom_index_ = rhs.chrom_index_;
00101 
00102       return *this;
00103     }
00104 
00106 
00110 
00111     void writeMemdump(MapType& exp, String out)
00112     {
00113       std::ofstream ofs(out.c_str(), std::ios::binary);
00114       Size exp_size = exp.size();
00115       Size chrom_size = exp.getChromatograms().size();
00116       int magic_number = MAGIC_NUMBER;
00117       ofs.write((char*)&magic_number, sizeof(magic_number));
00118       ofs.write((char*)&exp_size, sizeof(exp_size));
00119       ofs.write((char*)&chrom_size, sizeof(chrom_size));
00120 
00121       startProgress(0, exp.size() + exp.getChromatograms().size(), "storing binary spectra");
00122       for (Size i = 0; i < exp.size(); i++)
00123       {
00124         setProgress(i);
00125         writeSpectrum_(exp[i], ofs);
00126       }
00127 
00128       for (Size i = 0; i < exp.getChromatograms().size(); i++)
00129       {
00130         setProgress(i);
00131         writeChromatogram_(exp.getChromatograms()[i], ofs);
00132       }
00133 
00134       ofs.close();
00135       endProgress();
00136     }
00137 
00139     void readMemdump(MapType& exp_reading, String filename) const
00140     {
00141       std::ifstream ifs(filename.c_str(), std::ios::binary);
00142       Size exp_size, chrom_size;
00143       Peak1D current_peak;
00144 
00145       int magic_number;
00146       ifs.read((char*)&magic_number, sizeof(magic_number));
00147       if (magic_number != MAGIC_NUMBER)
00148       {
00149         throw "wrong file, does not start with MAGIC_NUMBER";
00150       }
00151 
00152       ifs.read((char*)&exp_size, sizeof(exp_size));
00153       ifs.read((char*)&chrom_size, sizeof(chrom_size));
00154 
00155       exp_reading.reserve(exp_size);
00156       startProgress(0, exp_size + chrom_size, "reading binary spectra");
00157       for (Size i = 0; i < exp_size; i++)
00158       {
00159         setProgress(i);
00160         SpectrumType spectrum;
00161         readSpectrum_(spectrum, ifs);
00162         exp_reading.push_back(spectrum);
00163       }
00164       std::vector<ChromatogramType> chromatograms;
00165       for (Size i = 0; i < chrom_size; i++)
00166       {
00167         setProgress(i);
00168         ChromatogramType chromatogram;
00169         readChromatogram_(chromatogram, ifs);
00170         chromatograms.push_back(chromatogram);
00171       }
00172       exp_reading.setChromatograms(chromatograms);
00173 
00174       ifs.close();
00175       endProgress();
00176     }
00177 
00179 
00183 
00184     void readSingleSpectrum(MSSpectrum<Peak1D>& spectrum, const String& filename, const Size& idx) const
00185     {
00186       // open stream, read
00187       std::ifstream ifs(filename.c_str(), std::ios::binary);
00188       readSingleSpectrum(spectrum, ifs, idx);
00189     }
00190 
00191     // Read a single spectrum from the given filestream
00192     void readSingleSpectrum(MSSpectrum<Peak1D>& spectrum, std::ifstream& ifs, const Size& idx) const
00193     {
00194       // go to the specified index
00195       ifs.seekg(idx);
00196       readSpectrum_(spectrum, ifs);
00197     }
00198 
00200 
00204     const std::vector<Size>& getSpectraIndex() const
00205     {
00206       return spectra_index_;
00207     }
00208 
00209     const std::vector<Size>& getChromatogramIndex() const
00210     {
00211       return chrom_index_;
00212     }
00213 
00215 
00217     void createMemdumpIndex(String filename)
00218     {
00219       std::ifstream ifs(filename.c_str(), std::ios::binary);
00220       Size exp_size, chrom_size;
00221       Peak1D current_peak;
00222 
00223       spectra_index_.clear();
00224       chrom_index_.clear();
00225       int magic_number;
00226       int extra_offset = sizeof(dbl_field_) + sizeof(int_field_);
00227       int chrom_offset = 0;
00228 
00229       ifs.read((char*)&magic_number, sizeof(magic_number));
00230       if (magic_number != MAGIC_NUMBER)
00231       {
00232         throw "wrong file, does not start with MAGIC_NUMBER";
00233       }
00234 
00235       // For spectra and chromatograms go through file, read the size of the
00236       // spectrum/chromatogram and record the starting index of the element, then
00237       // skip ahead to the next spectrum/chromatogram.
00238       ifs.read((char*)&exp_size, sizeof(exp_size));
00239       ifs.read((char*)&chrom_size, sizeof(chrom_size));
00240       startProgress(0, exp_size + chrom_size, "Creating index for binary spectra");
00241       for (Size i = 0; i < exp_size; i++)
00242       {
00243         setProgress(i);
00244 
00245         Size spec_size;
00246         spectra_index_.push_back(ifs.tellg());
00247         ifs.read((char*)&spec_size, sizeof(spec_size));
00248         ifs.seekg((int)ifs.tellg() + extra_offset + (sizeof(DatumSingleton)) * 2 * (spec_size));
00249 
00250       }
00251 
00252       for (Size i = 0; i < chrom_size; i++)
00253       {
00254         setProgress(i);
00255 
00256         Size chrom_size;
00257         chrom_index_.push_back(ifs.tellg());
00258         ifs.read((char*)&chrom_size, sizeof(chrom_size));
00259         ifs.seekg((int)ifs.tellg() + chrom_offset + (sizeof(DatumSingleton)) * 2 * (chrom_size));
00260 
00261       }
00262 
00263       ifs.close();
00264       endProgress();
00265     }
00266 
00268     void writeMetadata(MapType exp, String out_meta)
00269     {
00270       // delete the actual data for all spectra and chromatograms, leave only metadata
00271       std::vector<MSChromatogram<ChromatogramPeak> > chromatograms = exp.getChromatograms(); // copy
00272       for (Size i = 0; i < exp.size(); i++)
00273       {
00274         exp[i].clear(false);
00275       }
00276       for (Size i = 0; i < exp.getChromatograms().size(); i++)
00277       {
00278         // delete the actual data, leave only metadata
00279         //exp.getChromatograms()[i].clear(false);
00280         chromatograms[i].clear(false);
00281       }
00282       exp.setChromatograms(chromatograms);
00283 
00284       // store the meta data that is left in out_meta file
00285       MzMLFile f;
00286       f.store(out_meta, exp);
00287     }
00288 
00290     static inline void readSpectrumFast(OpenSwath::BinaryDataArrayPtr data1,
00291                                         OpenSwath::BinaryDataArrayPtr data2, std::ifstream& ifs, int ms_level,
00292                                         double rt)
00293     {
00294       Size spec_size = -1;
00295       ifs.read((char*) &spec_size, sizeof(spec_size));
00296       ifs.read((char*) &ms_level, sizeof(ms_level));
00297       ifs.read((char*) &rt, sizeof(rt));
00298 
00299       data1->data.resize(spec_size);
00300       data2->data.resize(spec_size);
00301       ifs.read((char*) &(data1->data)[0], spec_size * sizeof(double));
00302       ifs.read((char*) &(data2->data)[0], spec_size * sizeof(double));
00303     }
00304 
00306     static inline void readChromatogramFast(OpenSwath::BinaryDataArrayPtr data1,
00307                                             OpenSwath::BinaryDataArrayPtr data2, std::ifstream& ifs)
00308     {
00309       Size spec_size = -1;
00310       ifs.read((char*) &spec_size, sizeof(spec_size));
00311       data1->data.resize(spec_size);
00312 
00313       data2->data.resize(spec_size);
00314       ifs.read((char*) &(data1->data)[0], spec_size * sizeof(double));
00315       ifs.read((char*) &(data2->data)[0], spec_size * sizeof(double));
00316     }
00317 
00318 private:
00319 
00320     // read a single spectrum directly into a datavector (assuming file is already at the correct position)
00321     void readSpectrum_(Datavector& data1, Datavector& data2, std::ifstream& ifs, int& ms_level, double& rt) const
00322     {
00323       Size spec_size = -1;
00324       ifs.read((char*)&spec_size, sizeof(spec_size));
00325       ifs.read((char*)&ms_level, sizeof(ms_level));
00326       ifs.read((char*)&rt, sizeof(rt));
00327 
00328       data1.resize(spec_size);
00329       data2.resize(spec_size);
00330       ifs.read((char*)&data1[0], spec_size * sizeof(DatumSingleton));
00331       ifs.read((char*)&data2[0], spec_size * sizeof(DatumSingleton));
00332     }
00333 
00334     // read a single chromatogram directly into a datavector (assuming file is already at the correct position)
00335     void readChromatogram_(Datavector& data1, Datavector& data2, std::ifstream& ifs) const
00336     {
00337       Size spec_size = -1;
00338       ifs.read((char*)&spec_size, sizeof(spec_size));
00339       data1.resize(spec_size);
00340       data2.resize(spec_size);
00341       ifs.read((char*)&data1[0], spec_size * sizeof(DatumSingleton));
00342       ifs.read((char*)&data2[0], spec_size * sizeof(DatumSingleton));
00343     }
00344 
00345     // read a single spectrum directly into an OpenMS MSSpectrum (assuming file is already at the correct position)
00346     void readSpectrum_(SpectrumType& spectrum, std::ifstream& ifs) const
00347     {
00348       Datavector mz_data;
00349       Datavector int_data;
00350 
00351       int ms_level;
00352       double rt;
00353       readSpectrum_(mz_data, int_data, ifs, ms_level, rt);
00354       spectrum.reserve(mz_data.size());
00355       spectrum.setMSLevel(ms_level);
00356       spectrum.setRT(rt);
00357 
00358       for (Size j = 0; j < mz_data.size(); j++)
00359       {
00360         Peak1D p;
00361         p.setMZ(mz_data[j]);
00362         p.setIntensity(int_data[j]);
00363         spectrum.push_back(p);
00364       }
00365 
00366     }
00367 
00368     // read a single chromatogram directly into an OpenMS MSChromatograms (assuming file is already at the correct position)
00369     void readChromatogram_(ChromatogramType& chromatogram, std::ifstream& ifs) const
00370     {
00371       Datavector rt_data;
00372       Datavector int_data;
00373       readChromatogram_(rt_data, int_data, ifs);
00374       chromatogram.reserve(rt_data.size());
00375 
00376       for (Size j = 0; j < rt_data.size(); j++)
00377       {
00378         ChromatogramPeak p;
00379         p.setRT(rt_data[j]);
00380         p.setIntensity(int_data[j]);
00381         chromatogram.push_back(p);
00382       }
00383 
00384     }
00385 
00386     // write a single spectrum to filestream
00387     void writeSpectrum_(SpectrumType& spectrum, std::ofstream& ofs)
00388     {
00389       Size exp_size = spectrum.size();
00390       ofs.write((char*)&exp_size, sizeof(exp_size));
00391       int_field_ = spectrum.getMSLevel();
00392       ofs.write((char*)&int_field_, sizeof(int_field_));
00393       dbl_field_ = spectrum.getRT();
00394       ofs.write((char*)&dbl_field_, sizeof(dbl_field_));
00395 
00396 #if 0
00397       ofs.write((char*)&exp[i].front(), exp[i].size() * sizeof(exp[i].front()));
00398       std::cout << " storing spectrum " << i << " with size " << exp[i].size() << std::endl;
00399 #else
00400       Datavector mz_data;
00401       Datavector int_data;
00402       for (Size j = 0; j < spectrum.size(); j++)
00403       {
00404         mz_data.push_back(spectrum[j].getMZ());
00405         int_data.push_back(spectrum[j].getIntensity());
00406       }
00407       ofs.write((char*)&mz_data.front(), mz_data.size() * sizeof(mz_data.front()));
00408       ofs.write((char*)&int_data.front(), int_data.size() * sizeof(int_data.front()));
00409 #endif
00410       //std::cout << exp[i] << std::endl;
00411     }
00412 
00413     // write a single chromatogram to filestream
00414     void writeChromatogram_(const ChromatogramType& chromatogram, std::ofstream& ofs)
00415     {
00416       Size exp_size = chromatogram.size();
00417       ofs.write((char*)&exp_size, sizeof(exp_size));
00418       Datavector rt_data;
00419       Datavector int_data;
00420       for (Size j = 0; j < chromatogram.size(); j++)
00421       {
00422         rt_data.push_back(chromatogram[j].getRT());
00423         int_data.push_back(chromatogram[j].getIntensity());
00424       }
00425       ofs.write((char*)&rt_data.front(), rt_data.size() * sizeof(rt_data.front()));
00426       ofs.write((char*)&int_data.front(), int_data.size() * sizeof(int_data.front()));
00427     }
00428 
00429     std::vector<Size> spectra_index_;
00430     std::vector<Size> chrom_index_;
00431 
00432   };
00433 }
00434 #endif

OpenMS / TOPP release 1.10.0 Documentation generated on Thu Mar 7 2013 09:42:37 using doxygen 1.7.1