Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
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-2017.
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 #ifndef OPENMS_FORMAT_DTAFILE_H
36 #define OPENMS_FORMAT_DTAFILE_H
37 
41 #include <OpenMS/SYSTEM/File.h>
42 
43 #include <fstream>
44 #include <vector>
45 
46 namespace OpenMS
47 {
48 
61  class OPENMS_DLLAPI DTAFile
62  {
63 
64 public:
65 
67  DTAFile();
68 
70  virtual ~DTAFile();
71 
81  template <typename SpectrumType>
82  void load(const String & filename, SpectrumType & spectrum)
83  {
84  std::ifstream is(filename.c_str());
85  if (!is)
86  {
87  throw Exception::FileNotFound(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, filename);
88  }
89 
90  // delete old spectrum
91  spectrum.clear(true);
92 
93  // temporary variables
94  String line;
95  std::vector<String> strings(2);
96  typename SpectrumType::PeakType p;
97  char delimiter;
98 
99  // line number counter
100  Size line_number = 1;
101 
102  // read first line and store precursor m/z and charge
103  getline(is, line, '\n');
104  line.trim();
105 
106  // test which delimiter is used in the line
107  if (line.has('\t'))
108  {
109  delimiter = '\t';
110  }
111  else
112  {
113  delimiter = ' ';
114  }
115 
116  line.split(delimiter, strings);
117  if (strings.size() != 2)
118  {
119  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);
120  }
121  Precursor precursor;
122  double mh_mass;
123  Int charge;
124  try
125  {
126  // by convention the first line holds: singly protonated peptide mass, charge state
127  mh_mass = strings[0].toDouble();
128  charge = strings[1].toInt();
129  }
130  catch (...)
131  {
132  throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\": not a float number.", filename);
133  }
134  if (charge != 0)
135  {
136  precursor.setMZ((mh_mass - Constants::PROTON_MASS_U) / charge + Constants::PROTON_MASS_U);
137  }
138  else
139  {
140  precursor.setMZ(mh_mass);
141  }
142  precursor.setCharge(charge);
143  spectrum.getPrecursors().push_back(precursor);
144  spectrum.setMSLevel(default_ms_level_);
145 
146  while (getline(is, line, '\n'))
147  {
148  ++line_number;
149  line.trim();
150  if (line.empty()) continue;
151 
152  //test which delimiter is used in the line
153  if (line.has('\t'))
154  {
155  delimiter = '\t';
156  }
157  else
158  {
159  delimiter = ' ';
160  }
161 
162  line.split(delimiter, strings);
163  if (strings.size() != 2)
164  {
165  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);
166  }
167  try
168  {
169  //fill peak
170  p.setPosition((typename SpectrumType::PeakType::PositionType)strings[0].toDouble());
171  p.setIntensity((typename SpectrumType::PeakType::IntensityType)strings[1].toDouble());
172  }
173  catch (Exception::BaseException & /*e*/)
174  {
175  throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\": not a float number.", filename);
176  }
177  spectrum.push_back(p);
178  }
179 
180  spectrum.setName(File::basename(filename));
181  is.close();
182  }
183 
192  template <typename SpectrumType>
193  void store(const String & filename, const SpectrumType & spectrum) const
194  {
195  std::ofstream os(filename.c_str());
196  if (!os)
197  {
198  throw Exception::UnableToCreateFile(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, filename);
199  }
200  os.precision(writtenDigits<double>(0.0));
201 
202  // write precursor information
203  Precursor precursor;
204  if (spectrum.getPrecursors().size() > 0)
205  {
206  precursor = spectrum.getPrecursors()[0];
207  }
208  if (spectrum.getPrecursors().size() > 1)
209  {
210  std::cerr << "Warning: The spectrum written to the DTA file '" << filename << "' has more than one precursor. The first precursor is used!" << "\n";
211  }
212  // unknown charge
213  if (precursor.getCharge() == 0)
214  {
215  os << precursor.getMZ();
216  }
217  // known charge
218  else
219  {
220  os << ((precursor.getMZ() - 1.0) * precursor.getCharge() + 1.0);
221  }
222  // charge
223  os << " " << precursor.getCharge() << "\n";
224 
225  // iterate over all peaks of the spectrum and
226  // write one line for each peak of the spectrum.
227  typename SpectrumType::ConstIterator it(spectrum.begin());
228  for (; it != spectrum.end(); ++it)
229  {
230  // write m/z and intensity
231  os << it->getPosition() << " " << it->getIntensity() << "\n";
232  }
233 
234  // done
235  os.close();
236  }
237 
238 protected:
239 
242 
243  };
244 } // namespace OpenMS
245 
246 #endif // OPENMS_FORMAT_DTAFILE_H
bool has(Byte byte) const
true if String contains the byte, false otherwise
A more convenient string class.
Definition: String.h:57
Precursor meta information.
Definition: Precursor.h:58
CoordinateType getMZ() const
Non-mutable access to m/z.
Definition: Peak1D.h:114
UInt default_ms_level_
Default MS level used when reading the file.
Definition: DTAFile.h:241
unsigned int UInt
Unsigned integer type.
Definition: Types.h:95
ContainerType::const_iterator ConstIterator
Non-mutable iterator.
Definition: MSSpectrum.h:104
File adapter for DTA files.
Definition: DTAFile.h:61
void setName(const String &name)
Sets the name.
File not found exception.
Definition: Exception.h:524
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
void setMZ(CoordinateType mz)
Mutable access to m/z.
Definition: Peak1D.h:120
void setIntensity(IntensityType intensity)
Mutable access to the data point intensity (height)
Definition: Peak1D.h:111
const double PROTON_MASS_U
The representation of a 1D spectrum.
Definition: MSSpectrum.h:67
void load(const String &filename, SpectrumType &spectrum)
Loads a DTA file to a spectrum.
Definition: DTAFile.h:82
String & trim()
removes whitespaces (space, tab, line feed, carriage return) at the beginning and the end of the stri...
void setPosition(PositionType const &position)
Mutable access to the position.
Definition: Peak1D.h:150
A 1-dimensional raw data point or peak.
Definition: Peak1D.h:55
void setMSLevel(UInt ms_level)
Sets the MS level.
Exception base class.
Definition: Exception.h:90
void clear(bool clear_meta_data)
Clears all data and meta data.
static String basename(const String &file)
Returns the basename of the file (without the path).
const std::vector< Precursor > & getPrecursors() const
returns a const reference to the precursors
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:128
Unable to create file exception.
Definition: Exception.h:637
float IntensityType
Intensity type.
Definition: Peak1D.h:64
void store(const String &filename, const SpectrumType &spectrum) const
Stores a spectrum in a DTA file.
Definition: DTAFile.h:193
Int writtenDigits< double >(const double &)
Number of digits commonly used for writing a double (a.k.a. precision).
Definition: Types.h:220
void setCharge(Int charge)
Mutable access to the charge.
Int getCharge() const
Non-mutable access to the charge.
int Int
Signed integer type.
Definition: Types.h:103
bool split(const char splitter, std::vector< String > &substrings, bool quote_protect=false) const
Splits a string into substrings using splitter as delimiter.
Parse Error exception.
Definition: Exception.h:623

OpenMS / TOPP release 2.3.0 Documentation generated on Tue Jan 9 2018 18:22:00 using doxygen 1.8.13