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-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: 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  void store(const String& filename, const PeakMap& experiment);
78 
80  void store(std::ostream& os, const String& filename, const PeakMap& experiment);
81 
89  template <typename MapType>
90  void load(const String& filename, MapType& exp)
91  {
92  if (!File::exists(filename))
93  {
94  throw Exception::FileNotFound(__FILE__, __LINE__, __PRETTY_FUNCTION__, filename);
95  }
96 
97  exp.reset();
98 
99  std::ifstream is(filename.c_str());
100  bool has_next(true);
101  UInt spectrum_number(0);
102 #ifdef _OPENMP
103 #pragma omp parallel
104 #endif
105  {
106  std::vector<std::pair<String, String> > spec;
107  UInt charge(0);
108  double pre_mz(0), pre_int(0), rt(-1);
109  String title;
110  Size line_number(0);
111 
112  typename MapType::SpectrumType spectrum;
113  spectrum.setMSLevel(2);
114  spectrum.getPrecursors().resize(1);
115  typename MapType::PeakType p;
116  UInt thread_spectrum_number(-1);
117  while (has_next)
118  {
119 #ifdef _OPENMP
120 #pragma omp critical
121 #endif
122  {
123  has_next = getNextSpectrum_(is, spec, charge, pre_mz, pre_int, rt, title, line_number);
124  ++spectrum_number;
125  thread_spectrum_number = spectrum_number;
126  }
127  if (!has_next) break;
128  spectrum.resize(spec.size());
129 
130  for (Size i = 0; i < spec.size(); ++i)
131  {
132  p.setPosition(spec[i].first.toDouble()); // toDouble() is expensive (nothing can be done about this - boost::lexical_cast does not help), thats why we do it in threads
133  p.setIntensity(spec[i].second.toDouble());
134  spectrum[i] = p;
135  }
136  spectrum.getPrecursors()[0].setMZ(pre_mz);
137  spectrum.getPrecursors()[0].setIntensity(pre_int);
138  spectrum.getPrecursors()[0].setCharge(charge);
139  spectrum.setRT(rt);
140  if (title != "")
141  {
142  spectrum.setMetaValue("TITLE", title);
143  }
144  else
145  {
146  spectrum.removeMetaValue("TITLE");
147  }
148 
149  spectrum.setNativeID(String("index=") + (thread_spectrum_number));
150 #ifdef _OPENMP
151 #pragma omp critical
152 #endif
153  {
154  exp.addSpectrum(spectrum);
155  }
156  } // next spectrum
157  } // OMP parallel
158 
159  // order might be random, depending on which thread finished conversion first
160  exp.sortSpectra(true);
161 
162  }
163 
171  std::pair<String, String> getHTTPPeakListEnclosure(const String& filename) const;
172 
173 protected:
174 
176  void writeParameterHeader_(const String& name, std::ostream& os);
177 
179  void writeHeader_(std::ostream& os);
180 
182  void writeSpectrum_(std::ostream& os, const PeakSpectrum& spec, const String& filename);
183 
185  void writeMSExperiment_(std::ostream& os, const String& filename, const PeakMap& experiment);
186 
188  bool getNextSpectrum_(std::istream& is, std::vector<std::pair<String, String> >& spectrum, UInt& charge, double& precursor_mz, double& precursor_int, double& rt, String& title, Size& line_number);
189  };
190 
191 } // namespace OpenMS
192 
193 #endif // OPENMS_FORMAT_MASCOTGENERICFILE_H
A more convenient string class.
Definition: String.h:56
Peak2D PeakType
Definition: MassTrace.h:49
File not found exception.
Definition: Exception.h:524
Mascot input file adapter.
Definition: MascotGenericFile.h:64
static bool exists(const String &file)
Method used to test if a file exists.
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
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:90
void load(const String &filename, MapType &exp)
loads a Mascot Generic File into a PeakMap
Definition: MascotGenericFile.h:90

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