35 #ifndef OPENMS_FORMAT_DTAFILE_H
36 #define OPENMS_FORMAT_DTAFILE_H
75 template <
typename SpectrumType>
76 void load(
const String & filename, SpectrumType & spectrum)
78 std::ifstream is(filename.c_str());
89 std::vector<String> strings(2);
97 getline(is, line,
'\n');
110 line.
split(delimiter, strings);
111 if (strings.size() != 2)
113 throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string(
"Bad data line (" +
String(line_number) +
"): \"") + line +
"\" (got " +
String(strings.size()) +
", expected 2 entries)", filename);
121 mh_mass = strings[0].toDouble();
122 charge = strings[1].toInt();
126 throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string(
"Bad data line (" +
String(line_number) +
"): \"") + line +
"\": not a float number.", filename);
134 precursor.
setMZ(mh_mass);
137 spectrum.getPrecursors().push_back(precursor);
139 while (getline(is, line,
'\n'))
143 if (line.empty())
continue;
155 line.
split(delimiter, strings);
156 if (strings.size() != 2)
158 throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string(
"Bad data line (" +
String(line_number) +
"): \"") + line +
"\" (got " +
String(strings.size()) +
", expected 2 entries)", filename);
163 p.setPosition((
typename SpectrumType::PeakType::PositionType)strings[0].toDouble());
164 p.setIntensity((
typename SpectrumType::PeakType::IntensityType)strings[1].toDouble());
168 throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string(
"Bad data line (" +
String(line_number) +
"): \"") + line +
"\": not a float number.", filename);
170 spectrum.push_back(p);
185 template <
typename SpectrumType>
186 void store(
const String & filename,
const SpectrumType & spectrum)
const
188 std::ofstream os(filename.c_str());
193 os.precision(writtenDigits<DoubleReal>());
197 if (spectrum.getPrecursors().size() > 0)
199 precursor = spectrum.getPrecursors()[0];
201 if (spectrum.getPrecursors().size() > 1)
203 std::cerr <<
"Warning: The spectrum written to the DTA file '" << filename <<
"' has more than one precursor. The first precursor is used!" <<
"\n";
208 os << precursor.
getMZ();
216 os <<
" " << precursor.
getCharge() <<
"\n";
220 typename SpectrumType::ConstIterator it(spectrum.begin());
221 for (; it != spectrum.end(); ++it)
224 os << it->getPosition() <<
" " << it->getIntensity() <<
"\n";
234 #endif // OPENMS_FORMAT_DTAFILE_H
A more convenient string class.
Definition: String.h:56
Precursor meta information.
Definition: Precursor.h:56
Peak2D PeakType
Definition: MassTrace.h:49
CoordinateType getMZ() const
Non-mutable access to m/z.
Definition: Peak1D.h:108
File adapter for DTA files.
Definition: DTAFile.h:58
Int getCharge() const
Non-mutable access to the charge.
File not found exception.
Definition: Exception.h:524
bool has(Byte byte) const
true if String contains the byte, false otherwise
void setMZ(CoordinateType mz)
Mutable access to m/z.
Definition: Peak1D.h:114
const double PROTON_MASS_U
void load(const String &filename, SpectrumType &spectrum)
Loads a DTA file to a spectrum.
Definition: DTAFile.h:76
String & trim()
removes whitespaces (space, tab, line feed, carriage return) at the beginning and the end of the stri...
Exception base class.
Definition: Exception.h:90
static String basename(const String &file)
Returns the basename of the file (without the path).
void store(const String &filename, const SpectrumType &spectrum) const
Stores a spectrum in a DTA file.
Definition: DTAFile.h:186
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:144
Unable to create file exception.
Definition: Exception.h:622
void setCharge(Int charge)
Mutable access to the charge.
int Int
Signed integer type.
Definition: Types.h:100
bool split(const char splitter, std::vector< String > &substrings, bool quote_protect=false) const
Splits a string into substrings using splitter as delimiter.
Parse Error exception.
Definition: Exception.h:608