Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
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-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: Andreas Bertsch $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_FORMAT_MS2FILE_H
36 #define OPENMS_FORMAT_MS2FILE_H
37 
40 #include <OpenMS/SYSTEM/File.h>
43 
44 #include <vector>
45 #include <fstream>
46 
47 namespace OpenMS
48 {
65  class OPENMS_DLLAPI MS2File :
66  public ProgressLogger
67  {
68 public:
69 
71  MS2File();
72 
74  virtual ~MS2File();
75 
76  template <typename MapType>
77  void load(const String & filename, MapType & exp)
78  {
79  //startProgress(0,0,"loading DTA2D file");
80 
81  if (!File::exists(filename))
82  {
83  throw Exception::FileNotFound(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, filename);
84  }
85  if (!File::readable(filename))
86  {
87  throw Exception::FileNotReadable(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, filename);
88  }
89 
90  exp.reset();
91 
92  //set DocumentIdentifier
93  exp.setLoadedFileType(filename);
94  exp.setLoadedFilePath(filename);
95 
96  std::ifstream in(filename.c_str());
97 
98  UInt spectrum_number = 0;
99  typename MapType::SpectrumType spec;
101 
102  String line;
103  bool first_spec(true);
104 
105  // line number counter
106  Size line_number = 0;
107 
108  while (getline(in, line, '\n'))
109  {
110  ++line_number;
111 
112  line.trim();
113  if (line.empty()) continue;
114 
115  // header
116  if (line[0] == 'H')
117  {
118  continue;
119  }
120 
121  // scan
122  if (line[0] == 'S')
123  {
124  if (!first_spec)
125  {
126  spec.setMSLevel(2);
127  spec.setNativeID(String("index=") + (spectrum_number++));
128  exp.addSpectrum(spec);
129  }
130  else
131  {
132  first_spec = false;
133  }
134  spec.clear(true);
135  line.simplify();
136  std::vector<String> split;
137  line.split(' ', split);
138  if (split.size() != 4)
139  {
140  throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "line (" + String(line_number) + ") '" + line + "' should contain four values, got " + String(split.size()) + "!", "");
141  }
142  spec.getPrecursors().resize(1);
143  spec.getPrecursors()[0].setMZ(split[3].toDouble());
144  continue;
145  }
146 
147  // charge-independent analysis
148  if (line[0] == 'I')
149  {
150  continue;
151  }
152 
153  // charge specification
154  if (line[0] == 'Z')
155  {
156  continue;
157  }
158 
159  // charge-dependent analysis
160  if (line[0] == 'D')
161  {
162  continue;
163  }
164 
165  // yet another peak, hopefully
166  line.simplify();
167  std::vector<String> split;
168  line.split(' ', split);
169  if (split.size() != 2)
170  {
171  throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "line (" + String(line_number) + ") '" + line + "' should contain two values, got " + String(split.size()) + "!", "");
172  }
173 
174  try
175  {
176  p.setPosition(split[0].toDouble());
177  p.setIntensity(split[1].toFloat());
178  }
179  catch (Exception::ConversionError /*&e*/)
180  {
181  throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "ConversionError: line (" + String(line_number) + ") '" + line + "' does not contain two numbers!", "");
182  }
183  spec.push_back(p);
184  }
185 
186  if (!first_spec)
187  {
188  spec.setMSLevel(2);
189  spec.setNativeID(String("index=") + (spectrum_number++));
190  exp.addSpectrum(spec);
191  }
192  }
193 
194  /*
195  template <typename MapType> void store(const String& filename, MapType& map)
196  {
197 
198  }
199  */
200 
201 protected:
202 
203  };
204 
205 } // namespace OpenMS
206 
207 #endif // OPENMS_FORMAT_MS2FILE_H
String & simplify()
merges subsequent whitespaces to one blank character
A more convenient string class.
Definition: String.h:57
void reset()
Resets all internal values.
Definition: MSExperiment.h:709
void addSpectrum(const MSSpectrum &spectrum)
adds a spectrum to the list
Definition: MSExperiment.h:831
unsigned int UInt
Unsigned integer type.
Definition: Types.h:95
File not found exception.
Definition: Exception.h:524
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
void setIntensity(IntensityType intensity)
Mutable access to the data point intensity (height)
Definition: Peak1D.h:111
void setLoadedFileType(const String &file_name)
set the file_type according to the type of the file loaded from (see FileHandler::Type) preferably do...
The representation of a 1D spectrum.
Definition: MSSpectrum.h:67
File not readable exception.
Definition: Exception.h:538
static bool exists(const String &file)
Method used to test if a file exists.
void load(const String &filename, MapType &exp)
Definition: MS2File.h:77
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.
Invalid conversion exception.
Definition: Exception.h:363
void clear(bool clear_meta_data)
Clears all data and meta data.
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:82
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
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:55
void setLoadedFilePath(const String &file_name)
set the file_name_ according to absolute path of the file loaded from preferably done whilst loading ...
void setNativeID(const String &native_id)
sets the native identifier for the spectrum, used by the acquisition software.
MS2 input file adapter.
Definition: MS2File.h:65
static bool readable(const String &file)
Return true if the file exists and is readable.
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:02 using doxygen 1.8.13