OpenMS
Loading...
Searching...
No Matches
DTA2DFile.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
15#include <OpenMS/SYSTEM/File.h>
16
17#include <fstream>
18#include <iostream>
19
20namespace OpenMS
21{
40 class OPENMS_DLLAPI DTA2DFile :
41 public ProgressLogger
42 {
43private:
45
46public:
47
53 ~DTA2DFile() override;
55
58
61
71 template <typename MapType>
72 void load(const String& filename, MapType& map)
73 {
74 startProgress(0, 0, "loading DTA2D file");
75
76 //try to open file
77 std::ifstream is(filename.c_str());
78 if (!is)
79 {
80 if (!File::exists(filename))
81 {
82 throw Exception::FileNotFound(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, filename);
83 }
84 else if (!File::readable(filename))
85 {
86 throw Exception::FileNotReadable(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, filename);
87 }
88 else
89 {
90 throw Exception::IOException(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, filename);
91 }
92 }
93
94 map.reset();
95
96 //set DocumentIdentifier
97 map.setLoadedFileType(filename);
98 map.setLoadedFilePath(filename);
99
100 // temporary variables to store the data in
101 std::vector<String> strings(3);
102 typename MapType::SpectrumType spec;
103 spec.setRT(-1.0); //to make sure the first RT is different from the the initialized value
105 double rt(0.0);
106 char delimiter;
107
108 // default dimension of the data
109 Size rt_dim = 0;
110 Size mz_dim = 1;
111 Size int_dim = 2;
112
113 //RT unit (default is seconds)
114 bool time_in_minutes = false;
115
116 // string to store the current line in
117 String line;
118
119 // native ID (numbers from 0)
120 UInt native_id = 0;
121
122 // line number counter
123 Size line_number = 0;
124
125 while (getline(is, line, '\n'))
126 {
127 ++line_number;
128 line.trim();
129
130 if (line.empty()) continue;
131
132 //test which delimiter is used in the line
133 if (line.has('\t'))
134 {
135 delimiter = '\t';
136 }
137 else
138 {
139 delimiter = ' ';
140 }
141
142 //is header line
143 if (line.hasPrefix("#"))
144 {
145 line = line.substr(1).trim().toUpper();
146 line.split(delimiter, strings);
147
148 // flags to check if dimension is set correctly
149 bool rt_set = false;
150 bool mz_set = false;
151 bool int_set = false;
152
153 //assign new order
154 for (Size i = 0; i < 3; ++i)
155 {
156 if (strings[i] == "RT" || strings[i] == "RETENTION_TIME" || strings[i] == "MASS-TO-CHARGE" || strings[i] == "IT" || strings[i] == "INTENSITY")
157 {
158 std::cerr << "Warning: This file contains the deprecated keyword '" << strings[i] << "'." << "\n";
159 std::cerr << " Please use only the new keywords SEC/MIN, MZ, INT." << "\n";
160 }
161 if ((strings[i] == "SEC" || strings[i] == "RT" || strings[i] == "RETENTION_TIME") && rt_set == false)
162 {
163 rt_dim = i;
164 rt_set = true;
165 }
166 else if ((strings[i] == "MIN") && rt_set == false)
167 {
168 rt_dim = i;
169 rt_set = true;
170 time_in_minutes = true;
171 }
172 else if ((strings[i] == "MZ" || strings[i] == "MASS-TO-CHARGE") && mz_set == false)
173 {
174 mz_dim = i;
175 mz_set = true;
176 }
177 else if ((strings[i] == "INT" || strings[i] == "IT" || strings[i] == "INTENSITY") && int_set == false)
178 {
179 int_dim = i;
180 int_set = true;
181 }
182 else
183 {
184 throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Misformatted header line!", filename);
185 }
186 }
187 continue;
188 }
189
190 try
191 {
192 line.split(delimiter, strings);
193 if (strings.size() != 3)
194 {
195 throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\" (got " + String(strings.size()) + ", expected 3 entries)", filename);
196 }
197 p.setIntensity(strings[int_dim].toFloat());
198 p.setMZ(strings[mz_dim].toDouble());
199 rt = (strings[rt_dim].toDouble()) * (time_in_minutes ? 60.0 : 1.0);
200 }
201 // conversion to double or something else could have gone wrong
202 catch (Exception::BaseException& /*e*/)
203 {
204 throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\"", filename);
205 }
206
207 // Retention time changed -> new Spectrum
208 if (fabs(rt - spec.getRT()) > 0.0001)
209 {
210 if (spec.size() != 0
211 &&
212 (!options_.hasRTRange() || options_.getRTRange().encloses(DPosition<1>(spec.getRT())))) // RT restriction fulfilled
213 {
214 map.addSpectrum(spec);
215 }
216 setProgress(0);
217 spec.clear(true);
218 spec.setRT(rt);
219 spec.setNativeID(String("index=") + native_id);
220 ++native_id;
221 }
222
223 //Skip peaks with invalid m/z or intensity value
224 if (
225 (!options_.hasMZRange() || options_.getMZRange().encloses(DPosition<1>(p.getMZ())))
226 &&
228 )
229 {
230 spec.push_back(p);
231 }
232 }
233
234 // add last Spectrum
235 if (
236 spec.size() != 0
237 &&
238 (!options_.hasRTRange() || options_.getRTRange().encloses(DPosition<1>(spec.getRT()))) // RT restriction fulfilled
239 )
240 {
241 map.addSpectrum(spec);
242 }
243
244 is.close();
245 map.updateRanges();
246 endProgress();
247 }
248
257 template <typename MapType>
258 void store(const String& filename, const MapType& map) const
259 {
260 startProgress(0, map.size(), "storing DTA2D file");
261
262 std::ofstream os(filename.c_str());
263 if (!os)
264 {
265 throw Exception::UnableToCreateFile(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, filename);
266 }
267
268 // write header
269 os << "#SEC\tMZ\tINT\n";
270
271 // Iterate over all peaks of each spectrum and
272 // write one line for each peak of the spectrum.
273 UInt count = 0;
274 for (typename MapType::const_iterator spec = map.begin(); spec != map.end(); ++spec)
275 {
276 setProgress(count++);
277 for (typename MapType::SpectrumType::ConstIterator it = spec->begin(); it != spec->end(); ++it)
278 {
279 // Write rt, m/z and intensity.
280 os << precisionWrapper(spec->getRT()) << "\t" << precisionWrapper(it->getPos()) << "\t" << precisionWrapper(it->getIntensity()) << "\n";
281 }
282
283 }
284 os.close();
285 endProgress();
286 }
287
296 template <typename MapType>
297 void storeTIC(const String& filename, const MapType& map) const
298 {
299 startProgress(0, map.size(), "storing DTA2D file");
300
301 std::ofstream os(filename.c_str());
302 if (!os)
303 {
304 throw Exception::UnableToCreateFile(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, filename);
305 }
306
307 // write header (Always MZ=0 for chromatograms in DTA2D.)
308 os << "#SEC\tMZ\tINT\n";
309
311 for (typename MapType::ChromatogramType::ConstIterator it = TIC.begin(); it != TIC.end(); ++it)
312 {
313 // write rt, (mz=0) and intensity.
314 os << precisionWrapper(it->getRT()) << "\t0\t" << precisionWrapper(it->getIntensity()) << "\n";
315 }
316
317 os.close();
318 endProgress();
319 }
320
321 };
322
323} // namespace OpenMS
324
Representation of a coordinate in D-dimensional space.
Definition DPosition.h:32
bool encloses(const PositionType &position) const
Checks whether this range (half open interval!) contains a certain point.
Definition DRange.h:152
DTA2D File adapter.
Definition DTA2DFile.h:42
~DTA2DFile() override
Destructor.
DTA2DFile()
Default constructor.
const PeakFileOptions & getOptions() const
Non-mutable access to the options for loading/storing.
PeakFileOptions options_
Definition DTA2DFile.h:44
PeakFileOptions & getOptions()
Mutable access to the options for loading/storing.
void store(const String &filename, const MapType &map) const
Stores a map in a DTA2D file.
Definition DTA2DFile.h:258
void load(const String &filename, MapType &map)
Loads a map from a DTA2D file.
Definition DTA2DFile.h:72
void storeTIC(const String &filename, const MapType &map) const
Stores the TIC of a map in a DTA2D file.
Definition DTA2DFile.h:297
void setLoadedFilePath(const String &file_name)
set the file_name_ according to absolute path of the file loaded from preferably done whilst loading
void setLoadedFileType(const String &file_name)
set the file_type according to the type of the file loaded from (see FileHandler::Type) preferably do...
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 chromatogram.
Definition MSChromatogram.h:30
In-Memory representation of a mass spectrometry run.
Definition MSExperiment.h:49
void addSpectrum(const MSSpectrum &spectrum)
adds a spectrum to the list
Iterator begin() noexcept
Size size() const noexcept
The number of spectra.
const MSChromatogram calculateTIC(float rt_bin_size=0, UInt ms_level=1) const
Computes the total ion chromatogram (TIC) for a given MS level (use ms_level = 0 for all levels).
void reset()
Clear all internal data (spectra, ranges, metadata)
void updateRanges()
Updates the m/z, intensity, mobility, and retention time ranges of all spectra and chromatograms.
Base::const_iterator const_iterator
Definition MSExperiment.h:98
std::vector< SpectrumType >::const_iterator ConstIterator
Non-mutable iterator.
Definition MSExperiment.h:86
The representation of a 1D spectrum.
Definition MSSpectrum.h:44
double getRT() const
void clear(bool clear_meta_data)
Clears all data and meta data.
void setRT(double rt)
Sets the absolute retention time (in seconds)
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
IntensityType getIntensity() const
Definition Peak1D.h:84
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
Options for loading files containing peak data.
Definition PeakFileOptions.h:22
bool hasRTRange() const
returns true if an RT range has been set
const DRange< 1 > & getIntensityRange() const
returns the intensity range
bool hasMZRange() const
returns true if an MZ range has been set
bool hasIntensityRange() const
returns true if an intensity range has been set
const DRange< 1 > & getMZRange() const
returns the MZ range
const DRange< 1 > & getRTRange() const
returns the RT range
Base class for all classes that want to report their progress.
Definition ProgressLogger.h:27
void setNativeID(const String &native_id)
sets the native identifier for the spectrum, used by the acquisition software.
A more convenient string class.
Definition String.h:34
String substr(size_t pos=0, size_t n=npos) const
Wrapper for the STL substr() method. Returns a String object with its contents initialized to a subst...
bool hasPrefix(const String &string) const
true if String begins with string, false otherwise
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...
String & toUpper()
Converts the string to uppercase.
Definition TIC.h:30
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
const PrecisionWrapper< FloatingPointType > precisionWrapper(const FloatingPointType rhs)
Wrapper function that sets the appropriate precision for output temporarily. The original precision i...
Definition PrecisionWrapper.h:69