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 #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
00071 spectrum.getContainer().clear();
00072
00073
00074 String line;
00075 std::vector<String> strings(2);
00076 typename SpectrumType::PeakType p;
00077 char delimiter;
00078
00079
00080 getline(is,line,'\n');
00081 line.trim();
00082
00083
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
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
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
00170 if (spectrum.getPrecursorPeak().getCharge()==0)
00171 {
00172
00173 os << spectrum.getPrecursorPeak().getPosition()[0];
00174 }
00175 else
00176 {
00177
00178 os << ((spectrum.getPrecursorPeak().getPosition()[0] - 1.0) * spectrum.getPrecursorPeak().getCharge() +1.0);
00179 }
00180
00181
00182 os << " " << spectrum.getPrecursorPeak().getCharge() << std::endl;
00183
00184
00185
00186 typename SpectrumType::ConstIterator it(spectrum.begin());
00187 for (; it != spectrum.end(); ++it)
00188 {
00189
00190 os << it->getPosition() << " " << it->getIntensity() << std::endl;
00191 }
00192
00193
00194 os.close();
00195 }
00196 };
00197 }
00198
00199 #endif // OPENMS_FORMAT_DTAFILE_H
00200