Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
MascotGenericFile.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-2015.
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: Andreas Bertsch $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_FORMAT_MASCOTGENERICFILE_H
36 #define OPENMS_FORMAT_MASCOTGENERICFILE_H
37 
39 #include <OpenMS/SYSTEM/File.h>
43 
44 #include <vector>
45 #include <fstream>
46 
47 #ifdef _OPENMP
48 #include <omp.h>
49 #endif
50 
51 namespace OpenMS
52 {
64  class OPENMS_DLLAPI MascotGenericFile :
65  public ProgressLogger,
66  public DefaultParamHandler
67  {
68 public:
69 
72 
74  virtual ~MascotGenericFile();
75 
77  virtual void updateMembers_();
78 
80  void store(const String& filename, const PeakMap& experiment,
81  bool compact = false);
82 
84  void store(std::ostream& os, const String& filename,
85  const PeakMap& experiment, bool compact = false);
86 
94  template <typename MapType>
95  void load(const String& filename, MapType& exp)
96  {
97  if (!File::exists(filename))
98  {
99  throw Exception::FileNotFound(__FILE__, __LINE__, __PRETTY_FUNCTION__, filename);
100  }
101 
102  exp.reset();
103 
104  std::ifstream is(filename.c_str());
105  // get size of file
106  is.seekg(0, std::ios::end);
107  startProgress(0, is.tellg(), "loading MGF");
108  is.seekg(0, std::ios::beg);
109 
110  UInt spectrum_number(0);
111  Size line_number(0); // carry line number for error messages within getNextSpectrum()
112 
113  typename MapType::SpectrumType spectrum;
114  spectrum.setMSLevel(2);
115  spectrum.getPrecursors().resize(1);
116  while (getNextSpectrum_(is, spectrum, line_number, spectrum_number))
117  {
118  exp.addSpectrum(spectrum);
119  setProgress(is.tellg());
120  ++spectrum_number;
121  } // next spectrum
122 
123 
124  endProgress();
125  }
126 
134  std::pair<String, String> getHTTPPeakListEnclosure(const String& filename) const;
135 
136 protected:
137 
140 
142  std::map<String, String> mod_group_map_;
143 
145  void writeParameterHeader_(const String& name, std::ostream& os);
146 
148  void writeModifications_(const std::vector<String>& mods, std::ostream& os,
149  bool variable_mods = false);
150 
152  void writeHeader_(std::ostream& os);
153 
155  void writeSpectrum_(std::ostream& os, const PeakSpectrum& spec, const String& filename);
156 
158  void writeMSExperiment_(std::ostream& os, const String& filename, const PeakMap& experiment);
159 
161  template <typename SpectrumType>
162  bool getNextSpectrum_(std::ifstream& is, SpectrumType& spectrum, Size& line_number, const Size& spectrum_number)
163  {
164  spectrum.resize(0);
165 
166  spectrum.setNativeID(String("index=") + (spectrum_number));
167  if (spectrum.metaValueExists("TITLE"))
168  {
169  spectrum.removeMetaValue("TITLE");
170  }
171  typename SpectrumType::PeakType p;
172 
173  String line;
174  // seek to next peak list block
175  while (getline(is, line, '\n'))
176  {
177  ++line_number;
178 
179  line.trim(); // remove whitespaces, line-endings etc
180 
181  // found peak list block?
182  if (line == "BEGIN IONS")
183  {
184  while (getline(is, line, '\n'))
185  {
186  ++line_number;
187  line.trim(); // remove whitespaces, line-endings etc
188 
189  if (line.empty()) continue;
190 
191  if (isdigit(line[0])) // actual data .. this comes first, since its the most common case
192  {
193  std::vector<String> split;
194  do
195  {
196  if (line.empty())
197  {
198  continue;
199  }
200 
201  //line.substitute('\t', ' ');
202  line.split(' ', split);
203  if (split.size() >= 2)
204  {
205  p.setPosition(split[0].toDouble());
206  p.setIntensity(split[1].toDouble());
207  spectrum.push_back(p);
208  }
209  else
210  {
211  throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "the line (" + line + ") should contain m/z and intensity value separated by whitespace!", "");
212  }
213  }
214  while (getline(is, line, '\n') && ++line_number && line.trim() != "END IONS"); // line.trim() is important here!
215 
216  if (line == "END IONS")
217  {
218  return true; // found end of spectrum
219  }
220  else
221  {
222  throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Reached end of file. Found \"BEGIN IONS\" but not the corresponding \"END IONS\"!", "");
223  }
224  }
225  else if (line.hasPrefix("PEPMASS")) // parse precursor position
226  {
227  String tmp = line.substr(8);
228  tmp.substitute('\t', ' ');
229  std::vector<String> split;
230  tmp.split(' ', split);
231  if (split.size() == 1)
232  {
233  spectrum.getPrecursors()[0].setMZ(split[0].trim().toDouble());
234  }
235  else if (split.size() == 2)
236  {
237  spectrum.getPrecursors()[0].setMZ(split[0].trim().toDouble());
238  spectrum.getPrecursors()[0].setIntensity(split[1].trim().toDouble());
239  }
240  else
241  {
242  throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Cannot parse PEPMASS: '" + line + "' in line " + String(line_number) + " (expected 1 or 2 entries, but " + String(split.size()) + " were present!", "");
243  }
244  }
245  else if (line.hasPrefix("CHARGE"))
246  {
247  String tmp = line.substr(7);
248  tmp.remove('+');
249  spectrum.getPrecursors()[0].setCharge(tmp.toInt());
250  }
251  else if (line.hasPrefix("RTINSECONDS"))
252  {
253  String tmp = line.substr(12);
254  spectrum.setRT(tmp.toDouble());
255  }
256  else if (line.hasPrefix("TITLE"))
257  {
258  // test if we have a line like "TITLE= Cmpd 1, +MSn(595.3), 10.9 min"
259  if (line.hasSubstring("min"))
260  {
261  try
262  {
263  std::vector<String> split;
264  line.split(',', split);
265  if (!split.empty())
266  {
267  for (Size i = 0; i != split.size(); ++i)
268  {
269  if (split[i].hasSubstring("min"))
270  {
271  std::vector<String> split2;
272  split[i].trim().split(' ', split2);
273  if (!split2.empty())
274  {
275  spectrum.setRT(split2[0].trim().toDouble() * 60.0);
276  }
277  }
278  }
279  }
280  }
281  catch (Exception::BaseException& /*e*/)
282  {
283  // just do nothing and write the whole title to spec
284  std::vector<String> split;
285  line.split('=', split);
286  if (split.size() >= 2)
287  {
288  if (split[1] != "") spectrum.setMetaValue("TITLE", split[1]);
289  }
290  }
291  }
292  else // just write the title as metainfo to the spectrum
293  {
294  std::vector<String> split;
295  line.split('=', split);
296  if (split.size() == 2)
297  {
298  if (split[1] != "") spectrum.setMetaValue("TITLE", split[1]);
299  }
300  // TODO concatenate the other parts if the title contains additional '=' chars
301  }
302  }
303  }
304  }
305  }
306 
307  return false; // found end of file
308  }
309 
310  };
311 
312 } // namespace OpenMS
313 
314 #endif // OPENMS_FORMAT_MASCOTGENERICFILE_H
bool getNextSpectrum_(std::ifstream &is, SpectrumType &spectrum, Size &line_number, const Size &spectrum_number)
reads a spectrum block, the section between 'BEGIN IONS' and 'END IONS' of a MGF file ...
Definition: MascotGenericFile.h:162
A more convenient string class.
Definition: String.h:57
void reset()
Resets all internal values.
Definition: MSExperiment.h:660
Peak2D PeakType
Definition: MassTrace.h:48
File not found exception.
Definition: Exception.h:524
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Mascot input file adapter.
Definition: MascotGenericFile.h:64
String & remove(char what)
Remove all occurrences of the character what.
double toDouble() const
Conversion to double.
std::map< String, String > mod_group_map_
mapping of modifications with specificity groups, that have to be treated specially (e...
Definition: MascotGenericFile.h:142
Int toInt() const
Conversion to int.
static bool exists(const String &file)
Method used to test if a file exists.
bool hasSubstring(const String &string) const
true if String contains the string, false otherwise
String & trim()
removes whitespaces (space, tab, line feed, carriage return) at the beginning and the end of the stri...
void setMSLevel(UInt ms_level)
Sets the MS level.
Definition: MSSpectrum.h:265
Exception base class.
Definition: Exception.h:90
void addSpectrum(const MSSpectrum< PeakT > &spectrum)
adds a spectra to the list
Definition: MSExperiment.h:758
String & substitute(char from, char to)
Replaces all occurrences of the character from by the character to.
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:55
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...
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:92
const std::vector< Precursor > & getPrecursors() const
returns a const reference to the precursors
void load(const String &filename, MapType &exp)
loads a Mascot Generic File into a PeakMap
Definition: MascotGenericFile.h:95
bool split(const char splitter, std::vector< String > &substrings, bool quote_protect=false) const
Splits a string into substrings using splitter as delimiter.
bool store_compact_
use a compact format for storing (no zero-intensity peaks, limited number of decimal places)...
Definition: MascotGenericFile.h:139
bool hasPrefix(const String &string) const
true if String begins with string, false otherwise
Parse Error exception.
Definition: Exception.h:608

OpenMS / TOPP release 2.0.0 Documentation generated on Thu Aug 20 2015 01:44:24 using doxygen 1.8.9.1