00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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
00073 typedef double DatumSingleton;
00074 #else
00075
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
00187 std::ifstream ifs(filename.c_str(), std::ios::binary);
00188 readSingleSpectrum(spectrum, ifs, idx);
00189 }
00190
00191
00192 void readSingleSpectrum(MSSpectrum<Peak1D>& spectrum, std::ifstream& ifs, const Size& idx) const
00193 {
00194
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
00236
00237
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
00271 std::vector<MSChromatogram<ChromatogramPeak> > chromatograms = exp.getChromatograms();
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
00279
00280 chromatograms[i].clear(false);
00281 }
00282 exp.setChromatograms(chromatograms);
00283
00284
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
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
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
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
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
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
00411 }
00412
00413
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