OpenMS
MS2File.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Timo Sachsenberg $
6 // $Authors: Andreas Bertsch $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
13 #include <OpenMS/SYSTEM/File.h>
16 
17 #include <vector>
18 #include <fstream>
19 
20 namespace OpenMS
21 {
38  class OPENMS_DLLAPI MS2File :
39  public ProgressLogger
40  {
41 public:
42 
45 
47  ~MS2File() override;
48 
49  template <typename MapType>
50  void load(const String & filename, MapType & exp)
51  {
52  //startProgress(0,0,"loading DTA2D file");
53 
54  if (!File::exists(filename))
55  {
56  throw Exception::FileNotFound(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, filename);
57  }
58  if (!File::readable(filename))
59  {
60  throw Exception::FileNotReadable(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, filename);
61  }
62 
63  exp.reset();
64 
65  //set DocumentIdentifier
66  exp.setLoadedFileType(filename);
67  exp.setLoadedFilePath(filename);
68 
69  std::ifstream in(filename.c_str());
70 
71  UInt spectrum_number = 0;
72  typename MapType::SpectrumType spec;
74 
75  String line;
76  bool first_spec(true);
77 
78  // line number counter
79  Size line_number = 0;
80 
81  while (getline(in, line, '\n'))
82  {
83  ++line_number;
84 
85  line.trim();
86  if (line.empty()) continue;
87 
88  // header
89  if (line[0] == 'H')
90  {
91  continue;
92  }
93 
94  // scan
95  if (line[0] == 'S')
96  {
97  if (!first_spec)
98  {
99  spec.setMSLevel(2);
100  spec.setNativeID(String("index=") + (spectrum_number++));
101  exp.addSpectrum(spec);
102  }
103  else
104  {
105  first_spec = false;
106  }
107  spec.clear(true);
108  line.simplify();
109  std::vector<String> split;
110  line.split(' ', split);
111  if (split.size() != 4)
112  {
113  throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "line (" + String(line_number) + ") '" + line + "' should contain four values, got " + String(split.size()) + "!", "");
114  }
115  spec.getPrecursors().resize(1);
116  spec.getPrecursors()[0].setMZ(split[3].toDouble());
117  continue;
118  }
119 
120  // charge-independent analysis
121  if (line[0] == 'I')
122  {
123  continue;
124  }
125 
126  // charge specification
127  if (line[0] == 'Z')
128  {
129  continue;
130  }
131 
132  // charge-dependent analysis
133  if (line[0] == 'D')
134  {
135  continue;
136  }
137 
138  // yet another peak, hopefully
139  line.simplify();
140  std::vector<String> split;
141  line.split(' ', split);
142  if (split.size() != 2)
143  {
144  throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "line (" + String(line_number) + ") '" + line + "' should contain two values, got " + String(split.size()) + "!", "");
145  }
146 
147  try
148  {
149  p.setPosition(split[0].toDouble());
150  p.setIntensity(split[1].toFloat());
151  }
152  catch ( Exception::ConversionError& )
153  {
154  throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "ConversionError: line (" + String(line_number) + ") '" + line + "' does not contain two numbers!", "");
155  }
156  spec.push_back(p);
157  }
158 
159  if (!first_spec)
160  {
161  spec.setMSLevel(2);
162  spec.setNativeID(String("index=") + (spectrum_number++));
163  exp.addSpectrum(spec);
164  }
165  }
166 
167  /*
168  template <typename MapType> void store(const String& filename, MapType& map)
169  {
170 
171  }
172  */
173 
174 protected:
175 
176  };
177 
178 } // namespace OpenMS
179 
void setLoadedFilePath(const String &file_name)
set the file_name_ according to absolute path of the file loaded from preferably done whilst loading
void setLoadedFileType(const String &file_name)
set the file_type according to the type of the file loaded from (see FileHandler::Type) preferably do...
Invalid conversion exception.
Definition: Exception.h:330
File not found exception.
Definition: Exception.h:474
File not readable exception.
Definition: Exception.h:500
Parse Error exception.
Definition: Exception.h:592
static bool exists(const String &file)
Method used to test if a file exists.
static bool readable(const String &file)
Return true if the file exists and is readable.
Definition: MS2File.h:40
MS2File()
constructor
void load(const String &filename, MapType &exp)
Definition: MS2File.h:50
~MS2File() override
constructor
In-Memory representation of a mass spectrometry run.
Definition: MSExperiment.h:46
void addSpectrum(const MSSpectrum &spectrum)
adds a spectrum to the list
void reset()
Clear all internal data (spectra, ranges, metadata)
The representation of a 1D spectrum.
Definition: MSSpectrum.h:44
void setMSLevel(UInt ms_level)
Sets the MS level.
void clear(bool clear_meta_data)
Clears all data and meta data.
A 1-dimensional raw data point or peak.
Definition: Peak1D.h:28
void setIntensity(IntensityType intensity)
Mutable access to the data point intensity (height)
Definition: Peak1D.h:84
void setPosition(PositionType const &position)
Mutable access to the position.
Definition: Peak1D.h:123
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:27
const std::vector< Precursor > & getPrecursors() const
returns a const reference to the precursors
void setNativeID(const String &native_id)
sets the native identifier for the spectrum, used by the acquisition software.
A more convenient string class.
Definition: String.h:34
String & simplify()
merges subsequent whitespaces to one blank character
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...
unsigned int UInt
Unsigned integer type.
Definition: Types.h:64
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:97
static bool split(const String &this_s, const char splitter, std::vector< String > &substrings, bool quote_protect)
Definition: StringUtilsSimple.h:340
static double toDouble(const String &this_s)
Definition: StringUtils.h:216
static float toFloat(const String &this_s)
Definition: StringUtils.h:211
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19