Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
FileHandler.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: Stephan Aiche $
32 // $Authors: Marc Sturm $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_FORMAT_FILEHANDLER_H
36 #define OPENMS_FORMAT_FILEHANDLER_H
37 
38 #include <OpenMS/config.h>
40 
41 #include <OpenMS/FORMAT/DTAFile.h>
44 #include <OpenMS/FORMAT/MzMLFile.h>
48 #include <OpenMS/FORMAT/MS2File.h>
50 
54 
57 
58 namespace OpenMS
59 {
73  class OPENMS_DLLAPI FileHandler
74  {
75 public:
84  static FileTypes::Type getType(const String& filename);
85 
86 
88  static FileTypes::Type getTypeByFileName(const String& filename);
89 
95  static FileTypes::Type getTypeByContent(const String& filename);
96 
98  static bool isSupported(FileTypes::Type type);
99 
101  PeakFileOptions& getOptions();
102 
104  const PeakFileOptions& getOptions() const;
105 
107  void setOptions(const PeakFileOptions&);
108 
123  template <class PeakType>
124  bool loadExperiment(const String& filename, MSExperiment<PeakType>& exp, FileTypes::Type force_type = FileTypes::UNKNOWN, ProgressLogger::LogType log = ProgressLogger::NONE, const bool compute_hash = true)
125  {
126  //determine file type
127  FileTypes::Type type;
128  if (force_type != FileTypes::UNKNOWN)
129  {
130  type = force_type;
131  }
132  else
133  {
134  try
135  {
136  type = getType(filename);
137  }
139  {
140  return false;
141  }
142  }
143 
144  //load right file
145  switch (type)
146  {
147  case FileTypes::DTA:
148  exp.reset();
149  exp.resize(1);
150  DTAFile().load(filename, exp[0]);
151  break;
152 
153  case FileTypes::DTA2D:
154  {
155  DTA2DFile f;
156  f.getOptions() = options_;
157  f.setLogType(log);
158  f.load(filename, exp);
159  }
160 
161  break;
162 
163  case FileTypes::MZXML:
164  {
165  MzXMLFile f;
166  f.getOptions() = options_;
167  f.setLogType(log);
168  f.load(filename, exp);
169  }
170 
171  break;
172 
173  case FileTypes::MZDATA:
174  {
175  MzDataFile f;
176  f.getOptions() = options_;
177  f.setLogType(log);
178  f.load(filename, exp);
179  }
180  break;
181 
182  case FileTypes::MZML:
183  {
184  MzMLFile f;
185  f.getOptions() = options_;
186  f.setLogType(log);
187  f.load(filename, exp);
189  }
190  break;
191 
192  case FileTypes::MGF:
193  {
195  f.setLogType(log);
196  f.load(filename, exp);
197  }
198 
199  break;
200 
201  case FileTypes::MS2:
202  {
203  MS2File f;
204  f.setLogType(log);
205  f.load(filename, exp);
206  }
207 
208  break;
209 
210  case FileTypes::XMASS:
211  exp.reset();
212  exp.resize(1);
213  XMassFile().load(filename, exp[0]);
214  XMassFile().importExperimentalSettings(filename, exp);
215 
216  break;
217 
218  default:
219  return false;
220 
221  break;
222  }
223 
224  SourceFile src_file;
225  src_file.setNameOfFile(File::basename(filename));
226  src_file.setPathToFile(String("file:///") + File::path(filename));
227  // this is more complicated since the data formats allowed by mzML are very verbose.
228  // this is prone to changing CV's... our writer will fall back to a default if the name given here is invalid.
229  src_file.setFileType(FileTypes::typeToMZML(type));
230 
231  if (compute_hash)
232  {
233  src_file.setChecksum(computeFileHash_(filename), SourceFile::SHA1);
234  }
235 
236  exp.getSourceFiles().clear();
237  exp.getSourceFiles().push_back(src_file);
238 
239  return true;
240  }
241 
253  template <class PeakType>
255  {
256  //load right file
257  switch (getTypeByFileName(filename))
258  {
259  case FileTypes::DTA2D:
260  {
261  DTA2DFile f;
262  f.getOptions() = options_;
263  f.setLogType(log);
264  f.store(filename, exp);
265  }
266  break;
267 
268  case FileTypes::MZXML:
269  {
270  MzXMLFile f;
271  f.getOptions() = options_;
272  f.setLogType(log);
273  if (!exp.getChromatograms().empty())
274  {
275  MSExperiment<PeakType> exp2 = exp;
277  f.store(filename, exp2);
278  }
279  else
280  {
281  f.store(filename, exp);
282  }
283  }
284  break;
285 
286  case FileTypes::MZDATA:
287  {
288  MzDataFile f;
289  f.getOptions() = options_;
290  f.setLogType(log);
291  if (!exp.getChromatograms().empty())
292  {
293  MSExperiment<PeakType> exp2 = exp;
295  f.store(filename, exp2);
296  }
297  else
298  {
299  f.store(filename, exp);
300  }
301  }
302  break;
303 
304  default:
305  {
306  MzMLFile f;
307  f.getOptions() = options_;
308  f.setLogType(log);
309  f.store(filename, exp);
310  }
311  break;
312  }
313  }
314 
327  bool loadFeatures(const String& filename, FeatureMap& map, FileTypes::Type force_type = FileTypes::UNKNOWN);
328 
329 private:
331 
337  String computeFileHash_(const String& filename) const;
338  };
339 
340 } //namespace
341 
342 #endif //OPENMS_FORMAT_FILEHANDLER_H
Type
Actual file types enum.
Definition: FileTypes.h:59
DTA2D File adapter.
Definition: DTA2DFile.h:64
PeakFileOptions options_
Definition: FileHandler.h:330
void load(const String &filename, MSSpectrum< PeakType > &spectrum)
Loads a spectrum from a XMass file.
Definition: XMassFile.h:84
void store(const String &filename, const MapType &map) const
Stores a map in a MzXML file.
Definition: MzXMLFile.h:102
A more convenient string class.
Definition: String.h:57
MS2 file (.ms2)
Definition: FileTypes.h:74
File adapter for MzXML 2.1 files.
Definition: MzXMLFile.h:53
void reset()
Resets all internal values.
Definition: MSExperiment.h:660
LogType
Possible log types.
Definition: ProgressLogger.h:71
void convertChromatogramsToSpectra(ExperimentType &exp)
converts the chromatogram to a list of spectra with instrument settings SRM
Definition: ChromatogramTools.h:86
void load(const String &filename, MapType &map)
Loads a map from a MzXML file.
Definition: MzXMLFile.h:81
A container for features.
Definition: FeatureMap.h:93
static String path(const String &file)
Returns the path of the file (without the file name).
void setFileType(const String &file_type)
sets the file type
File adapter for DTA files.
Definition: DTAFile.h:61
MzData file (.mzData)
Definition: FileTypes.h:64
Description of a file location, used to store the origin of (meta) data.
Definition: SourceFile.h:47
void resize(Size s)
Definition: MSExperiment.h:122
File not found exception.
Definition: Exception.h:524
File adapter for &#39;XMass Analysis (fid)&#39; files.
Definition: XMassFile.h:66
void setChecksum(const String &checksum, ChecksumType type)
sets the file&#39;s checksum
Mascot input file adapter.
Definition: MascotGenericFile.h:64
File adapter for MzML files.
Definition: MzMLFile.h:55
void load(const String &filename, SpectrumType &spectrum)
Loads a DTA file to a spectrum.
Definition: DTAFile.h:82
Unknown file extension.
Definition: FileTypes.h:61
File adapter for MzData files.
Definition: MzDataFile.h:51
No progress logging.
Definition: ProgressLogger.h:75
void load(const String &filename, MapType &exp)
Definition: MS2File.h:77
DTA file (.dta)
Definition: FileTypes.h:62
XMass Analysis file (fid)
Definition: FileTypes.h:86
void load(const String &filename, MapType &map)
Loads a map from a MzML file. Spectra and chromatograms are sorted by default (this can be disabled u...
Definition: MzMLFile.h:83
void store(const String &filename, const MapType &map) const
Stores a map in a MzML file.
Definition: MzMLFile.h:109
void store(const String &filename, const MapType &map) const
Stores a map in a DTA2D file.
Definition: DTA2DFile.h:270
void store(const String &filename, const MapType &map) const
Stores a map in a MzData file.
Definition: MzDataFile.h:102
void convertSpectraToChromatograms(ExperimentType &exp, bool remove_spectra=false)
converts e.g. SRM spectra to chromatograms
Definition: ChromatogramTools.h:138
void setNameOfFile(const String &name_of_file)
sets the file name
static String basename(const String &file)
Returns the basename of the file (without the path).
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:69
Secure Hash Algorithm-1.
Definition: SourceFile.h:55
PeakFileOptions & getOptions()
Mutable access to the options for loading/storing.
PeakFileOptions & getOptions()
Mutable access to the options for loading/storing.
PeakFileOptions & getOptions()
Mutable access to the options for loading/storing.
void storeExperiment(const String &filename, const MSExperiment< PeakType > &exp, ProgressLogger::LogType log=ProgressLogger::NONE)
Stores an MSExperiment to a file.
Definition: FileHandler.h:254
bool loadExperiment(const String &filename, MSExperiment< PeakType > &exp, FileTypes::Type force_type=FileTypes::UNKNOWN, ProgressLogger::LogType log=ProgressLogger::NONE, const bool compute_hash=true)
Loads a file into an MSExperiment.
Definition: FileHandler.h:124
static String typeToMZML(Type type)
Returns the mzML name (TODO: switch to accession since they are more stable!)
DTA2D file (.dta2d)
Definition: FileTypes.h:63
void importExperimentalSettings(const String &filename, MSExperiment< PeakType > &exp)
Import settings from a XMass file.
Definition: XMassFile.h:182
Conversion class to convert chromatograms.
Definition: ChromatogramTools.h:55
Options for loading files containing peak data.
Definition: PeakFileOptions.h:48
MS2 input file adapter.
Definition: MS2File.h:65
void load(const String &filename, MapType &exp)
loads a Mascot Generic File into a PeakMap
Definition: MascotGenericFile.h:95
void setPathToFile(const String &path_path_to_file)
sets the file path
PeakFileOptions & getOptions()
Mutable access to the options for loading/storing.
Mascot Generic Format (.mgf)
Definition: FileTypes.h:69
void load(const String &filename, MapType &map)
Loads a map from a MzData file.
Definition: MzDataFile.h:81
MzML file (.mzML)
Definition: FileTypes.h:73
Facilitates file handling by file type recognition.
Definition: FileHandler.h:73
void load(const String &filename, MapType &map)
Loads a map from a DTA2D file.
Definition: DTA2DFile.h:96
const std::vector< MSChromatogram< ChromatogramPeakType > > & getChromatograms() const
returns the chromatogram list
Definition: MSExperiment.h:788
void setLogType(LogType type) const
Sets the progress log that should be used. The default type is NONE!
const std::vector< SourceFile > & getSourceFiles() const
returns a const reference to the source data file
MzXML file (.mzXML)
Definition: FileTypes.h:65

OpenMS / TOPP release 2.0.0 Documentation generated on Wed Mar 30 2016 16:18:39 using doxygen 1.8.5