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

DTA2DFile.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_DTA2DFILE_H
00028 #define OPENMS_FORMAT_DTA2DFILE_H
00029 
00030 #include <OpenMS/KERNEL/Peak2D.h>
00031 #include <OpenMS/DATASTRUCTURES/String.h>
00032 #include <OpenMS/FORMAT/PeakFileOptions.h>
00033 #include <OpenMS/CONCEPT/ProgressLogger.h>
00034 
00035 #include <fstream>
00036 #include <iostream>
00037 
00038 namespace OpenMS
00039 {
00040   class String;
00041 
00058   class DTA2DFile
00059     : public ProgressLogger
00060   {
00061    private:
00062     PeakFileOptions options_;
00063 
00064    public:
00065 
00068 
00069     DTA2DFile();
00071     ~DTA2DFile();
00073 
00075     PeakFileOptions& getOptions();
00076 
00078     const PeakFileOptions& getOptions() const;
00079 
00085     template <typename MapType>
00086     void load(const String& filename, MapType& map) throw (Exception::FileNotFound, Exception::ParseError)
00087     {
00088       startProgress(0,0,"loading DTA2D file");
00089       
00090       //try to open file
00091       std::ifstream is(filename.c_str());
00092       if (!is)
00093       {
00094         throw Exception::FileNotFound(__FILE__, __LINE__, __PRETTY_FUNCTION__, filename);
00095       }
00096 
00097       map.reset();
00098 
00099       // temporary variables to store the data in
00100       std::vector<String> strings(3);
00101       typename MapType::SpectrumType spec;
00102       spec.setRT(-1.0);
00103       typename MapType::SpectrumType::PeakType p;
00104       typename MapType::SpectrumType::PeakType::CoordinateType rt(0.0);
00105       char delimiter;
00106 
00107       // default dimension of the data
00108       UInt rt_dim = 0;
00109       UInt mz_dim = 1;
00110       UInt int_dim = 2;
00111 
00112       //RT unit (default is seconds)
00113       bool time_in_minutes = false;
00114 
00115       // string to store the current line in
00116       String line;
00117 
00118       while (getline(is,line,'\n'))
00119       {
00120         line.trim();
00121 
00122         if ( line.empty() ) continue;
00123 
00124         //test which delimiter is used in the line
00125         if (line.has('\t'))
00126         {
00127           delimiter = '\t';
00128         }
00129         else
00130         {
00131           delimiter = ' ';
00132         }
00133 
00134         //is header line
00135         if (line.hasPrefix("#"))
00136         {
00137           line = line.substr(1).trim();
00138           line.split(delimiter,strings);
00139 
00140           // flags to check if dimension is set correctly
00141           bool rt_set = false;
00142           bool mz_set = false;
00143           bool int_set = false;
00144 
00145           //assign new order
00146           for (UInt i = 0 ; i<3;++i)
00147           {
00148             if ( strings[i]=="RT" || strings[i]=="RETENTION_TIME" || strings[i]=="MASS-TO-CHARGE" || strings[i]=="IT" || strings[i]=="INTENSITY")
00149             {
00150               std::cerr << "Warning: This file contains the deprecated keyword '" << strings[i] << "'." << std::endl;
00151               std::cerr << "         Please use only the new keywords SEC/MIN, MZ, INT." << std::endl;
00152             }
00153             if ( ( strings[i]=="SEC" || strings[i]=="RT" || strings[i]=="RETENTION_TIME" ) && rt_set==false)
00154             {
00155               rt_dim = i;
00156               rt_set = true;
00157             }
00158             else if ( ( strings[i]=="MIN") && rt_set==false)
00159             {
00160               rt_dim = i;
00161               rt_set = true;
00162               time_in_minutes = true;
00163             }
00164             else if ( ( strings[i]=="MZ" || strings[i]=="MASS-TO-CHARGE" ) && mz_set==false)
00165             {
00166               mz_dim = i;
00167               mz_set = true;
00168             }
00169             else if ( ( strings[i]=="INT" || strings[i]=="IT" || strings[i]=="INTENSITY" ) && int_set==false )
00170             {
00171               int_dim = i;
00172               int_set = true;
00173             }
00174             else
00175             {
00176               throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Misformatted header line!" ,filename);
00177             }
00178           }
00179           continue;
00180         }
00181 
00182         try
00183         {
00184           line.split(delimiter,strings);
00185           if (strings.size()!=3)
00186           {
00187             throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string("Bad data line: \"")+line+"\"" ,filename);
00188           }
00189           //std::cout <<"'"<< strings[0] << "' '" << strings[1] << "' '" << strings[2] << "'"<< std::endl;
00190           //fill peak
00191           DoubleReal mz = strings[mz_dim].toDouble();
00192           DoubleReal intensity = strings[int_dim].toDouble();
00193           p.setIntensity(intensity);
00194           p.setPosition(mz);
00195           rt = (strings[rt_dim].toDouble()) * (time_in_minutes ? 60.0 : 1.0);
00196 
00197           if ((options_.hasMZRange() && !options_.getMZRange().encloses(DPosition<1>(mz)))
00198               || (options_.hasRTRange() && !options_.getRTRange().encloses(DPosition<1>(rt)))
00199               || (options_.hasIntensityRange() && !options_.getIntensityRange().encloses(DPosition<1>(intensity))))
00200           {
00201             continue; // if peak is out of specified range
00202           }
00203         }
00204         // conversion to double or something else could have gone wrong
00205         catch ( Exception::Base & e )
00206         {
00207           throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string("Bad data line: \"")+line+"\"" ,filename);
00208         }
00209 
00210         // Retention time changed -> new Spectrum
00211         //std::cout << "rt: " << rt << " spec: " <<  spec.getRT() << "test: "<< rt-spec.getRT()<< std::endl;
00212         if (rt != spec.getRT())
00213         {
00214           if (spec.getRT() >= 0) // if not initial Spectrum
00215           {
00216             map.push_back(spec);
00217             setProgress(0);
00218             //std::cout << "NEW SPEC"<< std::endl;
00219           }
00220           spec.getContainer().clear();
00221           spec.setRT(rt);
00222         }
00223         //insert peak into the spectrum
00224         spec.getContainer().push_back(p);
00225       }
00226 
00227       if (spec.getRT() >= 0) map.push_back(spec);  // add last Spectrum
00228       is.close();
00229       endProgress();
00230     }
00231 
00237     template <typename MapType>
00238     void store(const String& filename, const MapType& map) const throw (Exception::UnableToCreateFile)
00239     {
00240       startProgress(0,map.size(),"storing DTA2D file");
00241       
00242       std::ofstream os(filename.c_str());
00243       if (!os)
00244       {
00245         throw Exception::UnableToCreateFile(__FILE__, __LINE__, __PRETTY_FUNCTION__, filename);
00246       }
00247 
00248       // Iterate over all peaks of each spectrum and
00249       // write one line for each peak of the spectrum.
00250       UInt count = 0;
00251       for (typename MapType::const_iterator spec=map.begin(); spec!=map.end(); ++spec)
00252       {
00253         setProgress(count++);
00254         for (typename MapType::SpectrumType::ConstIterator it = spec->begin(); it != spec->end(); ++it)
00255         {
00256           // Write rt, m/z and intensity.
00257           os  << spec->getRT() << " "<< it->getPosition() << " "<< it->getIntensity() << std::endl;
00258         }
00259 
00260       }
00261       os.close();
00262       endProgress();
00263     }
00264   };
00265 
00266 } // namespace OpenMS
00267 
00268 #endif // OPENMS_FORMAT_DTA2DFILE_H

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