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-2013.
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: Andreas Bertsch $
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 {
58  class OPENMS_DLLAPI DTAFile
59  {
60 public:
62  DTAFile();
64  virtual ~DTAFile();
65 
75  template <typename SpectrumType>
76  void load(const String & filename, SpectrumType & spectrum)
77  {
78  std::ifstream is(filename.c_str());
79  if (!is)
80  {
81  throw Exception::FileNotFound(__FILE__, __LINE__, __PRETTY_FUNCTION__, filename);
82  }
83 
84  // Delete old spectrum
85  spectrum.clear(true);
86 
87  //temporary variables
88  String line;
89  std::vector<String> strings(2);
90  typename SpectrumType::PeakType p;
91  char delimiter;
92 
93  // line number counter
94  Size line_number = 1;
95 
96  //read first line and store precursor m/z and charge
97  getline(is, line, '\n');
98  line.trim();
99 
100  //test which delimiter is used in the line
101  if (line.has('\t'))
102  {
103  delimiter = '\t';
104  }
105  else
106  {
107  delimiter = ' ';
108  }
109 
110  line.split(delimiter, strings);
111  if (strings.size() != 2)
112  {
113  throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\" (got " + String(strings.size()) + ", expected 2 entries)", filename);
114  }
115  Precursor precursor;
116  DoubleReal mh_mass;
117  Int charge;
118  try
119  {
120  // by convention the first line holds: singly protonated peptide mass, charge state
121  mh_mass = strings[0].toDouble();
122  charge = strings[1].toInt();
123  }
124  catch (...)
125  {
126  throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\": not a float number.", filename);
127  }
128  if (charge != 0)
129  {
130  precursor.setMZ((mh_mass - Constants::PROTON_MASS_U) / charge + Constants::PROTON_MASS_U);
131  }
132  else
133  {
134  precursor.setMZ(mh_mass);
135  }
136  precursor.setCharge(charge);
137  spectrum.getPrecursors().push_back(precursor);
138 
139  while (getline(is, line, '\n'))
140  {
141  ++line_number;
142  line.trim();
143  if (line.empty()) continue;
144 
145  //test which delimiter is used in the line
146  if (line.has('\t'))
147  {
148  delimiter = '\t';
149  }
150  else
151  {
152  delimiter = ' ';
153  }
154 
155  line.split(delimiter, strings);
156  if (strings.size() != 2)
157  {
158  throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\" (got " + String(strings.size()) + ", expected 2 entries)", filename);
159  }
160  try
161  {
162  //fill peak
163  p.setPosition((typename SpectrumType::PeakType::PositionType)strings[0].toDouble());
164  p.setIntensity((typename SpectrumType::PeakType::IntensityType)strings[1].toDouble());
165  }
166  catch (Exception::BaseException & /*e*/)
167  {
168  throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\": not a float number.", filename);
169  }
170  spectrum.push_back(p);
171  }
172 
173  spectrum.setName(File::basename(filename));
174  is.close();
175  }
176 
185  template <typename SpectrumType>
186  void store(const String & filename, const SpectrumType & spectrum) const
187  {
188  std::ofstream os(filename.c_str());
189  if (!os)
190  {
191  throw Exception::UnableToCreateFile(__FILE__, __LINE__, __PRETTY_FUNCTION__, filename);
192  }
193  os.precision(writtenDigits<DoubleReal>());
194 
195  //write precursor information
196  Precursor precursor;
197  if (spectrum.getPrecursors().size() > 0)
198  {
199  precursor = spectrum.getPrecursors()[0];
200  }
201  if (spectrum.getPrecursors().size() > 1)
202  {
203  std::cerr << "Warning: The spectrum written to the DTA file '" << filename << "' has more than one precursor. The first precursor is used!" << "\n";
204  }
205  //unknown charge
206  if (precursor.getCharge() == 0)
207  {
208  os << precursor.getMZ();
209  }
210  //known charge
211  else
212  {
213  os << ((precursor.getMZ() - 1.0) * precursor.getCharge() + 1.0);
214  }
215  //charge
216  os << " " << precursor.getCharge() << "\n";
217 
218  // Iterate over all peaks of the spectrum and
219  // write one line for each peak of the spectrum.
220  typename SpectrumType::ConstIterator it(spectrum.begin());
221  for (; it != spectrum.end(); ++it)
222  {
223  // Write m/z and intensity.
224  os << it->getPosition() << " " << it->getIntensity() << "\n";
225  }
226 
227  // Done.
228  os.close();
229  }
230 
231  };
232 } // namespace OpenMS
233 
234 #endif // OPENMS_FORMAT_DTAFILE_H
A more convenient string class.
Definition: String.h:56
Precursor meta information.
Definition: Precursor.h:56
Peak2D PeakType
Definition: MassTrace.h:49
CoordinateType getMZ() const
Non-mutable access to m/z.
Definition: Peak1D.h:108
File adapter for DTA files.
Definition: DTAFile.h:58
Int getCharge() const
Non-mutable access to the charge.
File not found exception.
Definition: Exception.h:524
bool has(Byte byte) const
true if String contains the byte, false otherwise
void setMZ(CoordinateType mz)
Mutable access to m/z.
Definition: Peak1D.h:114
const double PROTON_MASS_U
void load(const String &filename, SpectrumType &spectrum)
Loads a DTA file to a spectrum.
Definition: DTAFile.h:76
String & trim()
removes whitespaces (space, tab, line feed, carriage return) at the beginning and the end of the stri...
Exception base class.
Definition: Exception.h:90
static String basename(const String &file)
Returns the basename of the file (without the path).
void store(const String &filename, const SpectrumType &spectrum) const
Stores a spectrum in a DTA file.
Definition: DTAFile.h:186
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:144
Unable to create file exception.
Definition: Exception.h:622
void setCharge(Int charge)
Mutable access to the charge.
int Int
Signed integer type.
Definition: Types.h:100
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:608

OpenMS / TOPP release 1.11.1 Documentation generated on Thu Nov 14 2013 11:19:13 using doxygen 1.8.5