OpenMS  2.7.0
DTAFile.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2021.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg $
32 // $Authors: Marc Sturm $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
40 #include <OpenMS/SYSTEM/File.h>
41 
42 #include <fstream>
43 #include <vector>
44 
45 namespace OpenMS
46 {
47 
60  class OPENMS_DLLAPI DTAFile
61  {
62 
63 public:
64 
67 
69  virtual ~DTAFile();
70 
80  template <typename SpectrumType>
81  void load(const String & filename, SpectrumType & spectrum)
82  {
83  std::ifstream is(filename.c_str());
84  if (!is)
85  {
86  throw Exception::FileNotFound(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, filename);
87  }
88 
89  // delete old spectrum
90  spectrum.clear(true);
91 
92  // temporary variables
93  String line;
94  std::vector<String> strings(2);
95  typename SpectrumType::PeakType p;
96  char delimiter;
97 
98  // line number counter
99  Size line_number = 1;
100 
101  // read first line and store precursor m/z and charge
102  getline(is, line, '\n');
103  line.trim();
104 
105  // test which delimiter is used in the line
106  if (line.has('\t'))
107  {
108  delimiter = '\t';
109  }
110  else
111  {
112  delimiter = ' ';
113  }
114 
115  line.split(delimiter, strings);
116  if (strings.size() != 2)
117  {
118  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);
119  }
120  Precursor precursor;
121  double mh_mass;
122  Int charge;
123  try
124  {
125  // by convention the first line holds: singly protonated peptide mass, charge state
126  mh_mass = strings[0].toDouble();
127  charge = strings[1].toInt();
128  }
129  catch (...)
130  {
131  throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\": not a float number.", filename);
132  }
133  if (charge != 0)
134  {
135  precursor.setMZ((mh_mass - Constants::PROTON_MASS_U) / charge + Constants::PROTON_MASS_U);
136  }
137  else
138  {
139  precursor.setMZ(mh_mass);
140  }
141  precursor.setCharge(charge);
142  spectrum.getPrecursors().push_back(precursor);
143  spectrum.setMSLevel(default_ms_level_);
144 
145  while (getline(is, line, '\n'))
146  {
147  ++line_number;
148  line.trim();
149  if (line.empty()) continue;
150 
151  //test which delimiter is used in the line
152  if (line.has('\t'))
153  {
154  delimiter = '\t';
155  }
156  else
157  {
158  delimiter = ' ';
159  }
160 
161  line.split(delimiter, strings);
162  if (strings.size() != 2)
163  {
164  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);
165  }
166  try
167  {
168  //fill peak
169  p.setPosition((typename SpectrumType::PeakType::PositionType)strings[0].toDouble());
170  p.setIntensity((typename SpectrumType::PeakType::IntensityType)strings[1].toDouble());
171  }
172  catch (Exception::BaseException & /*e*/)
173  {
174  throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\": not a float number.", filename);
175  }
176  spectrum.push_back(p);
177  }
178 
179  spectrum.setName(File::basename(filename));
180  is.close();
181  }
182 
191  template <typename SpectrumType>
192  void store(const String & filename, const SpectrumType & spectrum) const
193  {
194  std::ofstream os(filename.c_str());
195  if (!os)
196  {
197  throw Exception::UnableToCreateFile(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, filename);
198  }
199  os.precision(writtenDigits<double>(0.0));
200 
201  // write precursor information
202  Precursor precursor;
203  if (spectrum.getPrecursors().size() > 0)
204  {
205  precursor = spectrum.getPrecursors()[0];
206  }
207  if (spectrum.getPrecursors().size() > 1)
208  {
209  std::cerr << "Warning: The spectrum written to the DTA file '" << filename << "' has more than one precursor. The first precursor is used!" << "\n";
210  }
211  // unknown charge
212  if (precursor.getCharge() == 0)
213  {
214  os << precursor.getMZ();
215  }
216  // known charge
217  else
218  {
219  os << ((precursor.getMZ() - 1.0) * precursor.getCharge() + 1.0);
220  }
221  // charge
222  os << " " << precursor.getCharge() << "\n";
223 
224  // iterate over all peaks of the spectrum and
225  // write one line for each peak of the spectrum.
226  typename SpectrumType::ConstIterator it(spectrum.begin());
227  for (; it != spectrum.end(); ++it)
228  {
229  // write m/z and intensity
230  os << it->getPosition() << " " << it->getIntensity() << "\n";
231  }
232 
233  // done
234  os.close();
235  }
236 
237 protected:
238 
241 
242  };
243 } // namespace OpenMS
244 
File adapter for DTA files.
Definition: DTAFile.h:61
void store(const String &filename, const SpectrumType &spectrum) const
Stores a spectrum in a DTA file.
Definition: DTAFile.h:192
void load(const String &filename, SpectrumType &spectrum)
Loads a DTA file to a spectrum.
Definition: DTAFile.h:81
DTAFile()
Default constructor.
virtual ~DTAFile()
Destructor.
UInt default_ms_level_
Default MS level used when reading the file.
Definition: DTAFile.h:240
Exception base class.
Definition: Exception.h:92
File not found exception.
Definition: Exception.h:517
Parse Error exception.
Definition: Exception.h:630
Unable to create file exception.
Definition: Exception.h:644
static String basename(const String &file)
The representation of a 1D spectrum.
Definition: MSSpectrum.h:71
void setMSLevel(UInt ms_level)
Sets the MS level.
ContainerType::const_iterator ConstIterator
Non-mutable iterator.
Definition: MSSpectrum.h:128
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:54
CoordinateType getMZ() const
Non-mutable access to m/z.
Definition: Peak1D.h:107
float IntensityType
Intensity type.
Definition: Peak1D.h:62
void setIntensity(IntensityType intensity)
Mutable access to the data point intensity (height)
Definition: Peak1D.h:104
void setMZ(CoordinateType mz)
Mutable access to m/z.
Definition: Peak1D.h:113
void setPosition(PositionType const &position)
Mutable access to the position.
Definition: Peak1D.h:143
Precursor meta information.
Definition: Precursor.h:61
Int getCharge() const
Non-mutable access to the charge.
void setCharge(Int charge)
Mutable access to the charge.
DPosition< D > PositionType
Position Type.
Definition: RangeManager.h:55
const std::vector< Precursor > & getPrecursors() const
returns a const reference to the precursors
A more convenient string class.
Definition: String.h:61
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:102
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
const double PROTON_MASS_U
Definition: Constants.h:116
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
constexpr Int writtenDigits< double >(const double &)
Number of digits commonly used for writing a double (a.k.a. precision).
Definition: Types.h:219