OpenMS
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 
19 namespace OpenMS
20 {
21 
34  class OPENMS_DLLAPI DTAFile
35  {
36 
37 public:
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  throw Exception::FileNotFound(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, filename);
61  }
62 
63  // delete old spectrum
64  spectrum.clear(true);
65 
66  // temporary variables
67  String line;
68  std::vector<String> strings(2);
69  typename SpectrumType::PeakType p;
70  char delimiter;
71 
72  // line number counter
73  Size line_number = 1;
74 
75  // read first line and store precursor m/z and charge
76  getline(is, line, '\n');
77  line.trim();
78 
79  // test which delimiter is used in the line
80  if (line.has('\t'))
81  {
82  delimiter = '\t';
83  }
84  else
85  {
86  delimiter = ' ';
87  }
88 
89  line.split(delimiter, strings);
90  if (strings.size() != 2)
91  {
92  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);
93  }
94  Precursor precursor;
95  double mh_mass;
96  Int charge;
97  try
98  {
99  // by convention the first line holds: singly protonated peptide mass, charge state
100  mh_mass = strings[0].toDouble();
101  charge = strings[1].toInt();
102  }
103  catch (...)
104  {
105  throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\": not a float number.", filename);
106  }
107  if (charge != 0)
108  {
109  precursor.setMZ((mh_mass - Constants::PROTON_MASS_U) / charge + Constants::PROTON_MASS_U);
110  }
111  else
112  {
113  precursor.setMZ(mh_mass);
114  }
115  precursor.setCharge(charge);
116  spectrum.getPrecursors().push_back(precursor);
117  spectrum.setMSLevel(default_ms_level_);
118 
119  while (getline(is, line, '\n'))
120  {
121  ++line_number;
122  line.trim();
123  if (line.empty()) continue;
124 
125  //test which delimiter is used in the line
126  if (line.has('\t'))
127  {
128  delimiter = '\t';
129  }
130  else
131  {
132  delimiter = ' ';
133  }
134 
135  line.split(delimiter, strings);
136  if (strings.size() != 2)
137  {
138  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);
139  }
140  try
141  {
142  //fill peak
145  }
146  catch (Exception::BaseException & /*e*/)
147  {
148  throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\": not a float number.", filename);
149  }
150  spectrum.push_back(p);
151  }
152 
153  spectrum.setName(File::basename(filename));
154  is.close();
155  }
156 
165  template <typename SpectrumType>
166  void store(const String & filename, const SpectrumType & spectrum) const
167  {
168  std::ofstream os(filename.c_str());
169  if (!os)
170  {
171  throw Exception::UnableToCreateFile(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, filename);
172  }
173  os.precision(writtenDigits<double>(0.0));
174 
175  // write precursor information
176  Precursor precursor;
177  if (spectrum.getPrecursors().size() > 0)
178  {
179  precursor = spectrum.getPrecursors()[0];
180  }
181  if (spectrum.getPrecursors().size() > 1)
182  {
183  std::cerr << "Warning: The spectrum written to the DTA file '" << filename << "' has more than one precursor. The first precursor is used!" << "\n";
184  }
185  // unknown charge
186  if (precursor.getCharge() == 0)
187  {
188  os << precursor.getMZ();
189  }
190  // known charge
191  else
192  {
193  os << ((precursor.getMZ() - 1.0) * precursor.getCharge() + 1.0);
194  }
195  // charge
196  os << " " << precursor.getCharge() << "\n";
197 
198  // iterate over all peaks of the spectrum and
199  // write one line for each peak of the spectrum.
200  typename SpectrumType::ConstIterator it(spectrum.begin());
201  for (; it != spectrum.end(); ++it)
202  {
203  // write m/z and intensity
204  os << it->getPosition() << " " << it->getIntensity() << "\n";
205  }
206 
207  // done
208  os.close();
209  }
210 
211 protected:
212 
215 
216  };
217 } // namespace OpenMS
218 
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:166
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:214
Exception base class.
Definition: Exception.h:63
File not found exception.
Definition: Exception.h:474
Parse Error exception.
Definition: Exception.h:579
Unable to create file exception.
Definition: Exception.h:592
static String basename(const String &file)
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:110
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:28
CoordinateType getMZ() const
Non-mutable access to m/z.
Definition: Peak1D.h:87
void setIntensity(IntensityType intensity)
Mutable access to the data point intensity (height)
Definition: Peak1D.h:84
void setMZ(CoordinateType mz)
Mutable access to m/z.
Definition: Peak1D.h:93
void setPosition(PositionType const &position)
Mutable access to the position.
Definition: Peak1D.h:123
DPosition< 1 > PositionType
Position type.
Definition: Peak1D.h:38
float IntensityType
Intensity type.
Definition: Peak1D.h:36
Precursor meta information.
Definition: Precursor.h:35
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
const double PROTON_MASS_U
Definition: Constants.h:90
static double toDouble(const String &this_s)
Definition: StringUtils.h:216
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
constexpr Int writtenDigits< double >(const double &)
Number of digits commonly used for writing a double (a.k.a. precision).
Definition: Types.h:189