OpenMS  2.5.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-2020.
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 
66  DTAFile();
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 
OpenMS::TOPPBase
Base class for TOPP applications.
Definition: TOPPBase.h:144
OpenMS::DTAFile::default_ms_level_
UInt default_ms_level_
Default MS level used when reading the file.
Definition: DTAFile.h:240
OpenMS::File::basename
static String basename(const String &file)
Returns the basename of the file (without the path).
OpenMS::Peak1D::setMZ
void setMZ(CoordinateType mz)
Mutable access to m/z.
Definition: Peak1D.h:121
OpenMS::MzMLFile
File adapter for MzML files.
Definition: MzMLFile.h:55
OpenMS::String
A more convenient string class.
Definition: String.h:58
OpenMS::MSExperiment::begin
Iterator begin()
Definition: MSExperiment.h:157
OpenMS::DRange< 1 >
OpenMS::Exception::FileNotFound
File not found exception.
Definition: Exception.h:523
OpenMS::String::trim
String & trim()
removes whitespaces (space, tab, line feed, carriage return) at the beginning and the end of the stri...
MzMLFile.h
OpenMS::MSExperiment
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:77
OpenMS::SpectrumSettings::getPrecursors
const std::vector< Precursor > & getPrecursors() const
returns a const reference to the precursors
OpenMS::Exception::ConversionError
Invalid conversion exception.
Definition: Exception.h:362
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
OpenMS::Peak1D::setIntensity
void setIntensity(IntensityType intensity)
Mutable access to the data point intensity (height)
Definition: Peak1D.h:112
OpenMS::Constants::PROTON_MASS_U
const double PROTON_MASS_U
Constants.h
OpenMS::DTAFile
File adapter for DTA files.
Definition: DTAFile.h:60
OpenMS::Precursor
Precursor meta information.
Definition: Precursor.h:57
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::MzMLFile::load
void load(const String &filename, PeakMap &map)
Loads a map from a MzML file. Spectra and chromatograms are sorted by default (this can be disabled u...
OpenMS::MSExperiment::iterator
Base::iterator iterator
Definition: MSExperiment.h:124
int
OpenMS::String::has
bool has(Byte byte) const
true if String contains the byte, false otherwise
DTAFile.h
OpenMS::Exception::UnableToCreateFile
Unable to create file exception.
Definition: Exception.h:636
OpenMS::Peak1D::getMZ
CoordinateType getMZ() const
Non-mutable access to m/z.
Definition: Peak1D.h:115
OpenMS::Peak1D::setPosition
void setPosition(PositionType const &position)
Mutable access to the position.
Definition: Peak1D.h:151
OpenMS::Exception::BaseException
Exception base class.
Definition: Exception.h:89
OpenMS::String::split
bool split(const char splitter, std::vector< String > &substrings, bool quote_protect=false) const
Splits a string into substrings using splitter as delimiter.
OpenMS::MSSpectrum::clear
void clear(bool clear_meta_data)
Clears all data and meta data.
OpenMS::MSSpectrum::setMSLevel
void setMSLevel(UInt ms_level)
Sets the MS level.
seqan::find
bool find(TFinder &finder, const Pattern< TNeedle, FuzzyAC > &me, PatternAuxData< TNeedle > &dh)
Definition: AhoCorasickAmbiguous.h:884
OpenMS::Peak1D
A 1-dimensional raw data point or peak.
Definition: Peak1D.h:54
OpenMS::DPosition< D >
OpenMS::UInt
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
OpenMS::Peak1D::IntensityType
float IntensityType
Intensity type.
Definition: Peak1D.h:63
Precursor.h
main
int main(int argc, const char **argv)
Definition: INIFileEditor.cpp:73
OpenMS::writtenDigits< double >
constexpr Int writtenDigits< double >(const double &)
Number of digits commonly used for writing a double (a.k.a. precision).
Definition: Types.h:219
MSExperiment.h
OpenMS::Exception::ParseError
Parse Error exception.
Definition: Exception.h:622
OpenMS::DTAFile::load
void load(const String &filename, SpectrumType &spectrum)
Loads a DTA file to a spectrum.
Definition: DTAFile.h:81
OpenMS::PeakFileOptions::setRTRange
void setRTRange(const DRange< 1 > &range)
restricts the range of RT values for peaks to load
OpenMS::MSExperiment::end
Iterator end()
Definition: MSExperiment.h:167
OpenMS::MSSpectrum::setName
void setName(const String &name)
Sets the name.
String.h
OpenMS::Precursor::getCharge
Int getCharge() const
Non-mutable access to the charge.
OpenMS::Precursor::setCharge
void setCharge(Int charge)
Mutable access to the charge.
OpenMS::MSSpectrum::ConstIterator
ContainerType::const_iterator ConstIterator
Non-mutable iterator.
Definition: MSSpectrum.h:104
OpenMS::MSSpectrum
The representation of a 1D spectrum.
Definition: MSSpectrum.h:67
OpenMS::String::toInt
Int toInt() const
Conversion to int.
File.h
OpenMS::MzMLFile::getOptions
PeakFileOptions & getOptions()
Mutable access to the options for loading/storing.
OpenMS::ProgressLogger::setLogType
void setLogType(LogType type) const
Sets the progress log that should be used. The default type is NONE!
TOPPBase.h
OpenMS::DTAFile::store
void store(const String &filename, const SpectrumType &spectrum) const
Stores a spectrum in a DTA file.
Definition: DTAFile.h:192