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

DTAFile.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_DTAFILE_H
00028 #define OPENMS_FORMAT_DTAFILE_H
00029 
00030 #include <OpenMS/DATASTRUCTURES/String.h>
00031 
00032 #include <fstream>
00033 #include <vector>
00034 
00035 namespace OpenMS
00036 {
00047   class DTAFile
00048   {
00049     public:
00051       DTAFile();
00053       ~DTAFile();
00054       
00061       template <typename SpectrumType>
00062       void load(const String& filename, SpectrumType& spectrum) throw (Exception::FileNotFound,Exception::ParseError)
00063       {
00064         std::ifstream is(filename.c_str());
00065         if (!is)
00066         {
00067           throw Exception::FileNotFound(__FILE__, __LINE__, __PRETTY_FUNCTION__, filename);
00068         }
00069     
00070         //  Delete old spectrum
00071         spectrum.getContainer().clear();
00072         
00073         //temporary variables
00074         String line;
00075         std::vector<String> strings(2);
00076         typename SpectrumType::PeakType p;
00077         char delimiter;
00078         
00079         //read first line and store precursor m/z and charge
00080         getline(is,line,'\n');
00081         line.trim();
00082     
00083         //test which delimiter is used in the line
00084         if (line.has('\t'))
00085         {
00086           delimiter = '\t';
00087         }
00088         else
00089         {
00090           delimiter = ' ';
00091         }
00092         
00093         try
00094         {
00095           line.split(delimiter,strings);
00096           if (strings.size()!=2)
00097           {
00098             throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string("Bad data line: \"")+line+"\"" ,filename);
00099           }
00100           double mh_mass = strings[0].toDouble();
00101           Int charge = strings[1].toInt();
00102           if (charge != 0)
00103           {
00104             spectrum.getPrecursorPeak().setPosition( (mh_mass - 1.0) / charge + 1.0);
00105           }
00106           else
00107           {
00108             spectrum.getPrecursorPeak().setPosition( mh_mass );
00109           }
00110           spectrum.getPrecursorPeak().setCharge(charge);
00111         }
00112         catch(...)
00113         {
00114           throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string("Bad data line: \"")+line+"\"" ,filename);
00115         }
00116         
00117         while (getline(is,line,'\n'))
00118         {
00119           line.trim();
00120           if ( line.empty() ) continue;
00121           
00122           //test which delimiter is used in the line
00123           if (line.has('\t'))
00124           {
00125             delimiter = '\t';
00126           }
00127           else
00128           {
00129             delimiter = ' ';
00130           }
00131     
00132           try 
00133           {
00134             line.split(delimiter,strings);
00135             if (strings.size()!=2)
00136             {
00137               throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string("Bad data line: \"")+line+"\"" ,filename);
00138             }       
00139             
00140             //fill peak
00141             p.setPosition(strings[0].toDouble());
00142             p.setIntensity(strings[1].toDouble());
00143           } 
00144           catch ( Exception::Base & e )
00145           {
00146             throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string("Bad data line: \"")+line+"\"" ,filename);
00147           }
00148           spectrum.getContainer().push_back(p);
00149         }
00150         
00151         is.close();   
00152       }
00153 
00160       template <typename SpectrumType>
00161       void store(const String& filename, const SpectrumType& spectrum) const throw (Exception::UnableToCreateFile)
00162       {
00163         std::ofstream os(filename.c_str());
00164         if (!os)
00165         {
00166           throw Exception::UnableToCreateFile(__FILE__, __LINE__, __PRETTY_FUNCTION__, filename);
00167         }
00168     
00169         // Write mh+ mass
00170         if (spectrum.getPrecursorPeak().getCharge()==0)
00171         {
00172           //unknown charge
00173           os << spectrum.getPrecursorPeak().getPosition()[0];
00174         }
00175         else
00176         {
00177           //known charge
00178           os << ((spectrum.getPrecursorPeak().getPosition()[0] - 1.0) * spectrum.getPrecursorPeak().getCharge() +1.0);
00179         }
00180          
00181         //charge
00182         os << " " << spectrum.getPrecursorPeak().getCharge() << std::endl;
00183     
00184         // Iterate over all peaks of the spectrum and
00185         // write one line for each peak of the spectrum.
00186         typename SpectrumType::ConstIterator it(spectrum.begin());
00187         for (; it != spectrum.end(); ++it)
00188         {
00189           // Write m/z and intensity.
00190           os << it->getPosition() << " " << it->getIntensity() << std::endl;
00191         }
00192     
00193         // Done.
00194         os.close();   
00195       }
00196   };
00197 } // namespace OpenMS
00198 
00199 #endif // OPENMS_FORMAT_DTAFILE_H
00200 

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