OpenMS
Loading...
Searching...
No Matches
DTAFile.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Timo Sachsenberg $
6// $Authors: Marc Sturm $
7// --------------------------------------------------------------------------
8
9#pragma once
10
14#include <OpenMS/SYSTEM/File.h>
15
16#include <fstream>
17#include <vector>
18
19namespace OpenMS
20{
21
34 class OPENMS_DLLAPI DTAFile
35 {
36
37public:
38
41
43 virtual ~DTAFile();
44
54 template <typename SpectrumType>
55 void load(const String & filename, SpectrumType & spectrum)
56 {
57 std::ifstream is(filename.c_str());
58 if (!is)
59 {
60 if (!File::exists(filename))
61 {
62 throw Exception::FileNotFound(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, filename);
63 }
64 else if (!File::readable(filename))
65 {
66 throw Exception::FileNotReadable(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, filename);
67 }
68 else
69 {
70 throw Exception::IOException(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, filename);
71 }
72 }
73
74 // delete old spectrum
75 spectrum.clear(true);
76
77 // temporary variables
78 String line;
79 std::vector<String> strings(2);
80 typename SpectrumType::PeakType p;
81 char delimiter;
82
83 // line number counter
84 Size line_number = 1;
85
86 // read first line and store precursor m/z and charge
87 getline(is, line, '\n');
88 line.trim();
89
90 // test which delimiter is used in the line
91 if (line.has('\t'))
92 {
93 delimiter = '\t';
94 }
95 else
96 {
97 delimiter = ' ';
98 }
99
100 line.split(delimiter, strings);
101 if (strings.size() != 2)
102 {
103 throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\" (got " + String(strings.size()) + ", expected 2 entries)", filename);
104 }
105 Precursor precursor;
106 double mh_mass;
107 Int charge;
108 try
109 {
110 // by convention the first line holds: singly protonated peptide mass, charge state
111 mh_mass = strings[0].toDouble();
112 charge = strings[1].toInt();
113 }
114 catch (...)
115 {
116 throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\": not a float number.", filename);
117 }
118 if (charge != 0)
119 {
120 precursor.setMZ((mh_mass - Constants::PROTON_MASS_U) / charge + Constants::PROTON_MASS_U);
121 }
122 else
123 {
124 precursor.setMZ(mh_mass);
125 }
126 precursor.setCharge(charge);
127 spectrum.getPrecursors().push_back(precursor);
128 spectrum.setMSLevel(default_ms_level_);
129
130 while (getline(is, line, '\n'))
131 {
132 ++line_number;
133 line.trim();
134 if (line.empty()) continue;
135
136 //test which delimiter is used in the line
137 if (line.has('\t'))
138 {
139 delimiter = '\t';
140 }
141 else
142 {
143 delimiter = ' ';
144 }
145
146 line.split(delimiter, strings);
147 if (strings.size() != 2)
148 {
149 throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\" (got " + String(strings.size()) + ", expected 2 entries)", filename);
150 }
151 try
152 {
153 //fill peak
154 p.setPosition((typename SpectrumType::PeakType::PositionType)strings[0].toDouble());
155 p.setIntensity((typename SpectrumType::PeakType::IntensityType)strings[1].toDouble());
156 }
157 catch (Exception::BaseException & /*e*/)
158 {
159 throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\": not a float number.", filename);
160 }
161 spectrum.push_back(p);
162 }
163
164 spectrum.setName(File::basename(filename));
165 is.close();
166 }
167
176 template <typename SpectrumType>
177 void store(const String & filename, const SpectrumType & spectrum) const
178 {
179 std::ofstream os(filename.c_str());
180 if (!os)
181 {
182 throw Exception::UnableToCreateFile(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, filename);
183 }
184 os.precision(writtenDigits<double>(0.0));
185
186 // write precursor information
187 Precursor precursor;
188 if (spectrum.getPrecursors().size() > 0)
189 {
190 precursor = spectrum.getPrecursors()[0];
191 }
192 if (spectrum.getPrecursors().size() > 1)
193 {
194 std::cerr << "Warning: The spectrum written to the DTA file '" << filename << "' has more than one precursor. The first precursor is used!" << "\n";
195 }
196 // unknown charge
197 if (precursor.getCharge() == 0)
198 {
199 os << precursor.getMZ();
200 }
201 // known charge
202 else
203 {
204 os << ((precursor.getMZ() - 1.0) * precursor.getCharge() + 1.0);
205 }
206 // charge
207 os << " " << precursor.getCharge() << "\n";
208
209 // iterate over all peaks of the spectrum and
210 // write one line for each peak of the spectrum.
211 typename SpectrumType::ConstIterator it(spectrum.begin());
212 for (; it != spectrum.end(); ++it)
213 {
214 // write m/z and intensity
215 os << it->getPosition() << " " << it->getIntensity() << "\n";
216 }
217
218 // done
219 os.close();
220 }
221
222protected:
223
226
227 };
228} // namespace OpenMS
229
File adapter for DTA files.
Definition DTAFile.h:35
void store(const String &filename, const SpectrumType &spectrum) const
Stores a spectrum in a DTA file.
Definition DTAFile.h:177
void load(const String &filename, SpectrumType &spectrum)
Loads a DTA file to a spectrum.
Definition DTAFile.h:55
DTAFile()
Default constructor.
virtual ~DTAFile()
Destructor.
UInt default_ms_level_
Default MS level used when reading the file.
Definition DTAFile.h:225
Exception base class.
Definition Exception.h:63
File not found exception.
Definition Exception.h:475
File not readable exception.
Definition Exception.h:501
General IOException.
Definition Exception.h:541
Parse Error exception.
Definition Exception.h:593
Unable to create file exception.
Definition Exception.h:606
The representation of a 1D spectrum.
Definition MSSpectrum.h:44
void setMSLevel(UInt ms_level)
Sets the MS level.
ContainerType::const_iterator ConstIterator
Non-mutable iterator.
Definition MSSpectrum.h:120
void setName(const String &name)
Sets the name.
void clear(bool clear_meta_data)
Clears all data and meta data.
A 1-dimensional raw data point or peak.
Definition Peak1D.h:30
CoordinateType getMZ() const
Non-mutable access to m/z.
Definition Peak1D.h:89
void setIntensity(IntensityType intensity)
Mutable access to the data point intensity (height)
Definition Peak1D.h:86
void setMZ(CoordinateType mz)
Mutable access to m/z.
Definition Peak1D.h:95
void setPosition(PositionType const &position)
Mutable access to the position.
Definition Peak1D.h:125
Precursor meta information.
Definition Precursor.h:37
Int getCharge() const
Non-mutable access to the charge.
void setCharge(Int charge)
Mutable access to the charge.
const std::vector< Precursor > & getPrecursors() const
returns a const reference to the precursors
A more convenient string class.
Definition String.h:34
bool has(Byte byte) const
true if String contains the byte, false otherwise
bool split(const char splitter, std::vector< String > &substrings, bool quote_protect=false) const
Splits a string into substrings using splitter as delimiter.
String & trim()
removes whitespaces (space, tab, line feed, carriage return) at the beginning and the end of the stri...
int Int
Signed integer type.
Definition Types.h:72
unsigned int UInt
Unsigned integer type.
Definition Types.h:64
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19