Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
DTA2DFile.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_DTA2DFILE_H
36 #define OPENMS_FORMAT_DTA2DFILE_H
37 
41 
42 #include <fstream>
43 #include <iostream>
44 
45 namespace OpenMS
46 {
47  class String;
64  class OPENMS_DLLAPI DTA2DFile :
65  public ProgressLogger
66  {
67 private:
69 
70 public:
71 
74  DTA2DFile();
77  ~DTA2DFile();
79 
81  PeakFileOptions & getOptions();
82 
84  const PeakFileOptions & getOptions() const;
85 
94  template <typename MapType>
95  void load(const String & filename, MapType & map)
96  {
97  startProgress(0, 0, "loading DTA2D file");
98 
99  //try to open file
100  std::ifstream is(filename.c_str());
101  if (!is)
102  {
103  throw Exception::FileNotFound(__FILE__, __LINE__, __PRETTY_FUNCTION__, filename);
104  }
105 
106  map.reset();
107 
108  //set DocumentIdentifier
109  map.setLoadedFileType(filename);
110  map.setLoadedFilePath(filename);
111 
112  // temporary variables to store the data in
113  std::vector<String> strings(3);
114  typename MapType::SpectrumType spec;
115  spec.setRT(-1.0); //to make sure the first RT is different from the the initialized value
117  DoubleReal rt(0.0);
118  char delimiter;
119 
120  // default dimension of the data
121  Size rt_dim = 0;
122  Size mz_dim = 1;
123  Size int_dim = 2;
124 
125  //RT unit (default is seconds)
126  bool time_in_minutes = false;
127 
128  // string to store the current line in
129  String line;
130 
131  // native ID (numbers from 0)
132  UInt native_id = 0;
133 
134  // line number counter
135  Size line_number = 0;
136 
137  while (getline(is, line, '\n'))
138  {
139  ++line_number;
140  line.trim();
141 
142  if (line.empty()) continue;
143 
144  //test which delimiter is used in the line
145  if (line.has('\t'))
146  {
147  delimiter = '\t';
148  }
149  else
150  {
151  delimiter = ' ';
152  }
153 
154  //is header line
155  if (line.hasPrefix("#"))
156  {
157  line = line.substr(1).trim();
158  line.split(delimiter, strings);
159 
160  // flags to check if dimension is set correctly
161  bool rt_set = false;
162  bool mz_set = false;
163  bool int_set = false;
164 
165  //assign new order
166  for (Size i = 0; i < 3; ++i)
167  {
168  if (strings[i] == "RT" || strings[i] == "RETENTION_TIME" || strings[i] == "MASS-TO-CHARGE" || strings[i] == "IT" || strings[i] == "INTENSITY")
169  {
170  std::cerr << "Warning: This file contains the deprecated keyword '" << strings[i] << "'." << "\n";
171  std::cerr << " Please use only the new keywords SEC/MIN, MZ, INT." << "\n";
172  }
173  if ((strings[i] == "SEC" || strings[i] == "RT" || strings[i] == "RETENTION_TIME") && rt_set == false)
174  {
175  rt_dim = i;
176  rt_set = true;
177  }
178  else if ((strings[i] == "MIN") && rt_set == false)
179  {
180  rt_dim = i;
181  rt_set = true;
182  time_in_minutes = true;
183  }
184  else if ((strings[i] == "MZ" || strings[i] == "MASS-TO-CHARGE") && mz_set == false)
185  {
186  mz_dim = i;
187  mz_set = true;
188  }
189  else if ((strings[i] == "INT" || strings[i] == "IT" || strings[i] == "INTENSITY") && int_set == false)
190  {
191  int_dim = i;
192  int_set = true;
193  }
194  else
195  {
196  throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Misformatted header line!", filename);
197  }
198  }
199  continue;
200  }
201 
202  try
203  {
204  line.split(delimiter, strings);
205  if (strings.size() != 3)
206  {
207  throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\" (got " + String(strings.size()) + ", expected 3 entries)", filename);
208  }
209  p.setIntensity(strings[int_dim].toFloat());
210  p.setMZ(strings[mz_dim].toDouble());
211  rt = (strings[rt_dim].toDouble()) * (time_in_minutes ? 60.0 : 1.0);
212  }
213  // conversion to double or something else could have gone wrong
214  catch (Exception::BaseException & /*e*/)
215  {
216  throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\"", filename);
217  }
218 
219  // Retention time changed -> new Spectrum
220  if (fabs(rt - spec.getRT()) > 0.0001)
221  {
222  if (spec.size() != 0
223  &&
224  (!options_.hasRTRange() || options_.getRTRange().encloses(DPosition<1>(spec.getRT())))) // RT restriction fulfilled
225  {
226  map.addSpectrum(spec);
227  }
228  setProgress(0);
229  spec.clear(true);
230  spec.setRT(rt);
231  spec.setNativeID(String("index=") + native_id);
232  ++native_id;
233  }
234 
235  //Skip peaks with invalid m/z or intensity value
236  if (
237  (!options_.hasMZRange() || options_.getMZRange().encloses(DPosition<1>(p.getMZ())))
238  &&
239  (!options_.hasIntensityRange() || options_.getIntensityRange().encloses(DPosition<1>(p.getIntensity())))
240  )
241  {
242  spec.push_back(p);
243  }
244  }
245 
246  // add last Spectrum
247  if (
248  spec.size() != 0
249  &&
250  (!options_.hasRTRange() || options_.getRTRange().encloses(DPosition<1>(spec.getRT()))) // RT restriction fulfilled
251  )
252  {
253  map.addSpectrum(spec);
254  }
255 
256  is.close();
257  endProgress();
258  }
259 
267  template <typename MapType>
268  void store(const String & filename, const MapType & map) const
269  {
270  startProgress(0, map.size(), "storing DTA2D file");
271 
272  std::ofstream os(filename.c_str());
273  if (!os)
274  {
275  throw Exception::UnableToCreateFile(__FILE__, __LINE__, __PRETTY_FUNCTION__, filename);
276  }
277 
278  // write header
279  os << "#SEC\tMZ\tINT\n";
280 
281  // Iterate over all peaks of each spectrum and
282  // write one line for each peak of the spectrum.
283  UInt count = 0;
284  for (typename MapType::const_iterator spec = map.begin(); spec != map.end(); ++spec)
285  {
286  setProgress(count++);
287  for (typename MapType::SpectrumType::ConstIterator it = spec->begin(); it != spec->end(); ++it)
288  {
289  // Write rt, m/z and intensity.
290  os << precisionWrapper(spec->getRT()) << "\t" << precisionWrapper(it->getPos()) << "\t" << precisionWrapper(it->getIntensity()) << "\n";
291  }
292 
293  }
294  os.close();
295  endProgress();
296  }
297 
305  template <typename MapType>
306  void storeTIC(const String & filename, const MapType & map) const
307  {
308  startProgress(0, map.size(), "storing DTA2D file");
309 
310  std::ofstream os(filename.c_str());
311  if (!os)
312  {
313  throw Exception::UnableToCreateFile(__FILE__, __LINE__, __PRETTY_FUNCTION__, filename);
314  }
315 
316  // write header (Always MZ=0 for chromatograms in DTA2D.)
317  os << "#SEC\tMZ\tINT\n";
318 
319  typename MapType::ChromatogramType TIC = map.getTIC();
320  for (typename MapType::ChromatogramType::ConstIterator it = TIC.begin(); it != TIC.end(); ++it)
321  {
322  // write rt and intensity.
323  os << precisionWrapper(it->getRT()) << "\t" << precisionWrapper(0) << "\t" << precisionWrapper(it->getIntensity()) << "\n";
324  }
325 
326  os.close();
327  endProgress();
328  }
329 
330  };
331 
332 } // namespace OpenMS
333 
334 #endif // OPENMS_FORMAT_DTA2DFILE_H
DTA2D File adapter.
Definition: DTA2DFile.h:64
A more convenient string class.
Definition: String.h:56
Peak2D PeakType
Definition: MassTrace.h:49
PeakFileOptions options_
Definition: DTA2DFile.h:68
File not found exception.
Definition: Exception.h:524
bool has(Byte byte) const
true if String contains the byte, false otherwise
void storeTIC(const String &filename, const MapType &map) const
Stores the TIC of a map in a DTA2D file.
Definition: DTA2DFile.h:306
const PrecisionWrapper< FloatingPointType > precisionWrapper(const FloatingPointType rhs)
Wrapper function that sets the appropriate precision for output temporarily. The original precision i...
Definition: Types.h:358
String & trim()
removes whitespaces (space, tab, line feed, carriage return) at the beginning and the end of the stri...
void store(const String &filename, const MapType &map) const
Stores a map in a DTA2D file.
Definition: DTA2DFile.h:268
Exception base class.
Definition: Exception.h:90
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:144
Base class for all classes that want to report their progess.
Definition: ProgressLogger.h:56
Unable to create file exception.
Definition: Exception.h:622
String substr(size_t pos=0, size_t n=npos) const
Wrapper for the STL substr() method. Returns a String object with its contents initialized to a subst...
Options for loading files containing peak data.
Definition: PeakFileOptions.h:47
bool split(const char splitter, std::vector< String > &substrings, bool quote_protect=false) const
Splits a string into substrings using splitter as delimiter.
void load(const String &filename, MapType &map)
Loads a map from a DTA2D file.
Definition: DTA2DFile.h:95
bool hasPrefix(const String &string) const
true if String begins with string, false otherwise
Parse Error exception.
Definition: Exception.h:608

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