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_DTAFILE_H
00036 #define OPENMS_FORMAT_DTAFILE_H
00037
00038 #include <OpenMS/DATASTRUCTURES/String.h>
00039 #include <OpenMS/CONCEPT/Constants.h>
00040 #include <OpenMS/METADATA/Precursor.h>
00041 #include <OpenMS/SYSTEM/File.h>
00042
00043 #include <fstream>
00044 #include <vector>
00045
00046 namespace OpenMS
00047 {
00058 class OPENMS_DLLAPI DTAFile
00059 {
00060 public:
00062 DTAFile();
00064 virtual ~DTAFile();
00065
00075 template <typename SpectrumType>
00076 void load(const String & filename, SpectrumType & spectrum)
00077 {
00078 std::ifstream is(filename.c_str());
00079 if (!is)
00080 {
00081 throw Exception::FileNotFound(__FILE__, __LINE__, __PRETTY_FUNCTION__, filename);
00082 }
00083
00084
00085 spectrum.clear(true);
00086
00087
00088 String line;
00089 std::vector<String> strings(2);
00090 typename SpectrumType::PeakType p;
00091 char delimiter;
00092
00093
00094 Size line_number = 1;
00095
00096
00097 getline(is, line, '\n');
00098 line.trim();
00099
00100
00101 if (line.has('\t'))
00102 {
00103 delimiter = '\t';
00104 }
00105 else
00106 {
00107 delimiter = ' ';
00108 }
00109
00110 line.split(delimiter, strings);
00111 if (strings.size() != 2)
00112 {
00113 throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\" (got " + String(strings.size()) + ", expected 2 entries)", filename);
00114 }
00115 Precursor precursor;
00116 DoubleReal mh_mass;
00117 Int charge;
00118 try
00119 {
00120
00121 mh_mass = strings[0].toDouble();
00122 charge = strings[1].toInt();
00123 }
00124 catch (...)
00125 {
00126 throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\": not a float number.", filename);
00127 }
00128 if (charge != 0)
00129 {
00130 precursor.setMZ((mh_mass - Constants::PROTON_MASS_U) / charge + Constants::PROTON_MASS_U);
00131 }
00132 else
00133 {
00134 precursor.setMZ(mh_mass);
00135 }
00136 precursor.setCharge(charge);
00137 spectrum.getPrecursors().push_back(precursor);
00138
00139 while (getline(is, line, '\n'))
00140 {
00141 ++line_number;
00142 line.trim();
00143 if (line.empty()) continue;
00144
00145
00146 if (line.has('\t'))
00147 {
00148 delimiter = '\t';
00149 }
00150 else
00151 {
00152 delimiter = ' ';
00153 }
00154
00155 line.split(delimiter, strings);
00156 if (strings.size() != 2)
00157 {
00158 throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\" (got " + String(strings.size()) + ", expected 2 entries)", filename);
00159 }
00160 try
00161 {
00162
00163 p.setPosition((typename SpectrumType::PeakType::PositionType)strings[0].toDouble());
00164 p.setIntensity((typename SpectrumType::PeakType::IntensityType)strings[1].toDouble());
00165 }
00166 catch (Exception::BaseException & )
00167 {
00168 throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\": not a float number.", filename);
00169 }
00170 spectrum.push_back(p);
00171 }
00172
00173 spectrum.setName(File::basename(filename));
00174 is.close();
00175 }
00176
00185 template <typename SpectrumType>
00186 void store(const String & filename, const SpectrumType & spectrum) const
00187 {
00188 std::ofstream os(filename.c_str());
00189 if (!os)
00190 {
00191 throw Exception::UnableToCreateFile(__FILE__, __LINE__, __PRETTY_FUNCTION__, filename);
00192 }
00193 os.precision(writtenDigits<DoubleReal>());
00194
00195
00196 Precursor precursor;
00197 if (spectrum.getPrecursors().size() > 0)
00198 {
00199 precursor = spectrum.getPrecursors()[0];
00200 }
00201 if (spectrum.getPrecursors().size() > 1)
00202 {
00203 std::cerr << "Warning: The spectrum written to the DTA file '" << filename << "' has more than one precursor. The first precursor is used!" << "\n";
00204 }
00205
00206 if (precursor.getCharge() == 0)
00207 {
00208 os << precursor.getMZ();
00209 }
00210
00211 else
00212 {
00213 os << ((precursor.getMZ() - 1.0) * precursor.getCharge() + 1.0);
00214 }
00215
00216 os << " " << precursor.getCharge() << "\n";
00217
00218
00219
00220 typename SpectrumType::ConstIterator it(spectrum.begin());
00221 for (; it != spectrum.end(); ++it)
00222 {
00223
00224 os << it->getPosition() << " " << it->getIntensity() << "\n";
00225 }
00226
00227
00228 os.close();
00229 }
00230
00231 };
00232 }
00233
00234 #endif // OPENMS_FORMAT_DTAFILE_H