OpenMS  2.8.0
MS2File.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-2021.
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: Andreas Bertsch $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
39 #include <OpenMS/SYSTEM/File.h>
42 
43 #include <vector>
44 #include <fstream>
45 
46 namespace OpenMS
47 {
64  class OPENMS_DLLAPI MS2File :
65  public ProgressLogger
66  {
67 public:
68 
71 
73  ~MS2File() override;
74 
75  template <typename MapType>
76  void load(const String & filename, MapType & exp)
77  {
78  //startProgress(0,0,"loading DTA2D file");
79 
80  if (!File::exists(filename))
81  {
82  throw Exception::FileNotFound(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, filename);
83  }
84  if (!File::readable(filename))
85  {
86  throw Exception::FileNotReadable(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, filename);
87  }
88 
89  exp.reset();
90 
91  //set DocumentIdentifier
92  exp.setLoadedFileType(filename);
93  exp.setLoadedFilePath(filename);
94 
95  std::ifstream in(filename.c_str());
96 
97  UInt spectrum_number = 0;
98  typename MapType::SpectrumType spec;
100 
101  String line;
102  bool first_spec(true);
103 
104  // line number counter
105  Size line_number = 0;
106 
107  while (getline(in, line, '\n'))
108  {
109  ++line_number;
110 
111  line.trim();
112  if (line.empty()) continue;
113 
114  // header
115  if (line[0] == 'H')
116  {
117  continue;
118  }
119 
120  // scan
121  if (line[0] == 'S')
122  {
123  if (!first_spec)
124  {
125  spec.setMSLevel(2);
126  spec.setNativeID(String("index=") + (spectrum_number++));
127  exp.addSpectrum(spec);
128  }
129  else
130  {
131  first_spec = false;
132  }
133  spec.clear(true);
134  line.simplify();
135  std::vector<String> split;
136  line.split(' ', split);
137  if (split.size() != 4)
138  {
139  throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "line (" + String(line_number) + ") '" + line + "' should contain four values, got " + String(split.size()) + "!", "");
140  }
141  spec.getPrecursors().resize(1);
142  spec.getPrecursors()[0].setMZ(split[3].toDouble());
143  continue;
144  }
145 
146  // charge-independent analysis
147  if (line[0] == 'I')
148  {
149  continue;
150  }
151 
152  // charge specification
153  if (line[0] == 'Z')
154  {
155  continue;
156  }
157 
158  // charge-dependent analysis
159  if (line[0] == 'D')
160  {
161  continue;
162  }
163 
164  // yet another peak, hopefully
165  line.simplify();
166  std::vector<String> split;
167  line.split(' ', split);
168  if (split.size() != 2)
169  {
170  throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "line (" + String(line_number) + ") '" + line + "' should contain two values, got " + String(split.size()) + "!", "");
171  }
172 
173  try
174  {
175  p.setPosition(split[0].toDouble());
176  p.setIntensity(split[1].toFloat());
177  }
178  catch ( Exception::ConversionError& )
179  {
180  throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "ConversionError: line (" + String(line_number) + ") '" + line + "' does not contain two numbers!", "");
181  }
182  spec.push_back(p);
183  }
184 
185  if (!first_spec)
186  {
187  spec.setMSLevel(2);
188  spec.setNativeID(String("index=") + (spectrum_number++));
189  exp.addSpectrum(spec);
190  }
191  }
192 
193  /*
194  template <typename MapType> void store(const String& filename, MapType& map)
195  {
196 
197  }
198  */
199 
200 protected:
201 
202  };
203 
204 } // namespace OpenMS
205 
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:356
File not found exception.
Definition: Exception.h:511
File not readable exception.
Definition: Exception.h:525
Parse Error exception.
Definition: Exception.h:624
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.
MS2 input file adapter.
Definition: MS2File.h:66
MS2File()
constructor
void load(const String &filename, MapType &exp)
Definition: MS2File.h:76
~MS2File() override
constructor
In-Memory representation of a mass spectrometry run.
Definition: MSExperiment.h:73
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:70
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:54
void setIntensity(IntensityType intensity)
Mutable access to the data point intensity (height)
Definition: Peak1D.h:104
void setPosition(PositionType const &position)
Mutable access to the position.
Definition: Peak1D.h:143
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:53
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:60
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:94
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
static bool split(const String &this_s, const char splitter, std::vector< String > &substrings, bool quote_protect)
Definition: StringUtilsSimple.h:360
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: FeatureDeconvolution.h:47