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_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
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
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
00108 UInt rt_dim = 0;
00109 UInt mz_dim = 1;
00110 UInt int_dim = 2;
00111
00112
00113 bool time_in_minutes = false;
00114
00115
00116 String line;
00117
00118 while (getline(is,line,'\n'))
00119 {
00120 line.trim();
00121
00122 if ( line.empty() ) continue;
00123
00124
00125 if (line.has('\t'))
00126 {
00127 delimiter = '\t';
00128 }
00129 else
00130 {
00131 delimiter = ' ';
00132 }
00133
00134
00135 if (line.hasPrefix("#"))
00136 {
00137 line = line.substr(1).trim();
00138 line.split(delimiter,strings);
00139
00140
00141 bool rt_set = false;
00142 bool mz_set = false;
00143 bool int_set = false;
00144
00145
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
00190
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;
00202 }
00203 }
00204
00205 catch ( Exception::Base & e )
00206 {
00207 throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string("Bad data line: \"")+line+"\"" ,filename);
00208 }
00209
00210
00211
00212 if (rt != spec.getRT())
00213 {
00214 if (spec.getRT() >= 0)
00215 {
00216 map.push_back(spec);
00217 setProgress(0);
00218
00219 }
00220 spec.getContainer().clear();
00221 spec.setRT(rt);
00222 }
00223
00224 spec.getContainer().push_back(p);
00225 }
00226
00227 if (spec.getRT() >= 0) map.push_back(spec);
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
00249
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
00257 os << spec->getRT() << " "<< it->getPosition() << " "<< it->getIntensity() << std::endl;
00258 }
00259
00260 }
00261 os.close();
00262 endProgress();
00263 }
00264 };
00265
00266 }
00267
00268 #endif // OPENMS_FORMAT_DTA2DFILE_H