Go to the documentation of this file.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_FORMAT_MS2FILE_H
00036 #define OPENMS_FORMAT_MS2FILE_H
00037
00038 #include <OpenMS/DATASTRUCTURES/String.h>
00039 #include <OpenMS/CONCEPT/Exception.h>
00040 #include <OpenMS/SYSTEM/File.h>
00041 #include <OpenMS/CONCEPT/ProgressLogger.h>
00042 #include <OpenMS/METADATA/DocumentIdentifier.h>
00043
00044 #include <vector>
00045 #include <fstream>
00046
00047 namespace OpenMS
00048 {
00065 class OPENMS_DLLAPI MS2File :
00066 public ProgressLogger
00067 {
00068 public:
00069
00071 MS2File();
00072
00074 virtual ~MS2File();
00075
00076 template <typename MapType>
00077 void load(const String & filename, MapType & exp)
00078 {
00079
00080
00081 if (!File::exists(filename))
00082 {
00083 throw Exception::FileNotFound(__FILE__, __LINE__, __PRETTY_FUNCTION__, filename);
00084 }
00085 if (!File::readable(filename))
00086 {
00087 throw Exception::FileNotReadable(__FILE__, __LINE__, __PRETTY_FUNCTION__, filename);
00088 }
00089
00090 exp.reset();
00091
00092
00093 exp.setLoadedFileType(filename);
00094 exp.setLoadedFilePath(filename);
00095
00096 std::ifstream in(filename.c_str());
00097
00098 UInt spectrum_number = 0;
00099 typename MapType::SpectrumType spec;
00100 typename MapType::SpectrumType::PeakType p;
00101
00102 String line;
00103 bool first_spec(true);
00104
00105
00106 Size line_number = 0;
00107
00108 while (getline(in, line, '\n'))
00109 {
00110 ++line_number;
00111
00112 line.trim();
00113 if (line.empty()) continue;
00114
00115
00116 if (line[0] == 'H')
00117 {
00118 continue;
00119 }
00120
00121
00122 if (line[0] == 'S')
00123 {
00124 if (!first_spec)
00125 {
00126 spec.setMSLevel(2);
00127 spec.setNativeID(String("index=") + (spectrum_number++));
00128 exp.push_back(spec);
00129 }
00130 else
00131 {
00132 first_spec = false;
00133 }
00134 spec.clear(true);
00135 line.simplify();
00136 std::vector<String> split;
00137 line.split(' ', split);
00138 if (split.size() != 4)
00139 {
00140 throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "line (" + String(line_number) + ") '" + line + "' should contain four values, got " + String(split.size()) + "!", "");
00141 }
00142 spec.getPrecursors().resize(1);
00143 spec.getPrecursors()[0].setMZ(split[3].toDouble());
00144 continue;
00145 }
00146
00147
00148 if (line[0] == 'I')
00149 {
00150 continue;
00151 }
00152
00153
00154 if (line[0] == 'Z')
00155 {
00156 continue;
00157 }
00158
00159
00160 if (line[0] == 'D')
00161 {
00162 continue;
00163 }
00164
00165
00166 line.simplify();
00167 std::vector<String> split;
00168 line.split(' ', split);
00169 if (split.size() != 2)
00170 {
00171 throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "line (" + String(line_number) + ") '" + line + "' should contain two values, got " + String(split.size()) + "!", "");
00172 }
00173
00174 try
00175 {
00176 p.setPosition(split[0].toDouble());
00177 p.setIntensity(split[1].toFloat());
00178 }
00179 catch (Exception::ConversionError )
00180 {
00181 throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "ConversionError: line (" + String(line_number) + ") '" + line + "' does not contain two numbers!", "");
00182 }
00183 spec.push_back(p);
00184 }
00185
00186 if (!first_spec)
00187 {
00188 spec.setMSLevel(2);
00189 spec.setNativeID(String("index=") + (spectrum_number++));
00190 exp.push_back(spec);
00191 }
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201 protected:
00202
00203 };
00204
00205 }
00206
00207 #endif // OPENMS_FORMAT_MS2FILE_H