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-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: 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 
120  template <class PeakType>
121  bool loadExperiment(const String& filename, MSExperiment<PeakType>& exp, FileTypes::Type force_type = FileTypes::UNKNOWN, ProgressLogger::LogType log = ProgressLogger::NONE, const bool compute_hash = true)
122  {
123  //determine file type
124  FileTypes::Type type;
125  if (force_type != FileTypes::UNKNOWN)
126  {
127  type = force_type;
128  }
129  else
130  {
131  try
132  {
133  type = getType(filename);
134  }
136  {
137  return false;
138  }
139  }
140 
141  //load right file
142  switch (type)
143  {
144  case FileTypes::DTA:
145  exp.reset();
146  exp.resize(1);
147  DTAFile().load(filename, exp[0]);
148  break;
149 
150  case FileTypes::DTA2D:
151  {
152  DTA2DFile f;
153  f.getOptions() = options_;
154  f.setLogType(log);
155  f.load(filename, exp);
156  }
157 
158  break;
159 
160  case FileTypes::MZXML:
161  {
162  MzXMLFile f;
163  f.getOptions() = options_;
164  f.setLogType(log);
165  f.load(filename, exp);
166  }
167 
168  break;
169 
170  case FileTypes::MZDATA:
171  {
172  MzDataFile f;
173  f.getOptions() = options_;
174  f.setLogType(log);
175  f.load(filename, exp);
176  }
177  break;
178 
179  case FileTypes::MZML:
180  {
181  MzMLFile f;
182  f.getOptions() = options_;
183  f.setLogType(log);
184  f.load(filename, exp);
186  }
187  break;
188 
189  case FileTypes::MGF:
190  {
192  f.setLogType(log);
193  f.load(filename, exp);
194  }
195 
196  break;
197 
198  case FileTypes::MS2:
199  {
200  MS2File f;
201  f.setLogType(log);
202  f.load(filename, exp);
203  }
204 
205  break;
206 
207  case FileTypes::XMASS:
208  exp.reset();
209  exp.resize(1);
210  XMassFile().load(filename, exp[0]);
211  XMassFile().importExperimentalSettings(filename, exp);
212 
213  break;
214 
215  default:
216  return false;
217 
218  break;
219  }
220 
221  SourceFile src_file;
222  src_file.setNameOfFile(File::basename(filename));
223  src_file.setPathToFile(String("file:///") + File::path(filename));
224  // this is more complicated since the data formats allowed by mzML are very verbose.
225  // this is prone to changing CV's... our writer will fall back to a default if the name given here is invalid.
226  src_file.setFileType(FileTypes::typeToMZML(type));
227 
228  if (compute_hash)
229  {
230  src_file.setChecksum(computeFileHash_(filename), SourceFile::SHA1);
231  }
232 
233  exp.getSourceFiles().clear();
234  exp.getSourceFiles().push_back(src_file);
235 
236  return true;
237  }
238 
250  template <class PeakType>
252  {
253  //load right file
254  switch (getTypeByFileName(filename))
255  {
256  case FileTypes::DTA2D:
257  {
258  DTA2DFile f;
259  f.getOptions() = options_;
260  f.setLogType(log);
261  f.store(filename, exp);
262  }
263  break;
264 
265  case FileTypes::MZXML:
266  {
267  MzXMLFile f;
268  f.getOptions() = options_;
269  f.setLogType(log);
270  if (!exp.getChromatograms().empty())
271  {
272  MSExperiment<PeakType> exp2 = exp;
274  f.store(filename, exp2);
275  }
276  else
277  {
278  f.store(filename, exp);
279  }
280  }
281  break;
282 
283  case FileTypes::MZDATA:
284  {
285  MzDataFile f;
286  f.getOptions() = options_;
287  f.setLogType(log);
288  if (!exp.getChromatograms().empty())
289  {
290  MSExperiment<PeakType> exp2 = exp;
292  f.store(filename, exp2);
293  }
294  else
295  {
296  f.store(filename, exp);
297  }
298  }
299  break;
300 
301  default:
302  {
303  MzMLFile f;
304  f.getOptions() = options_;
305  f.setLogType(log);
306  f.store(filename, exp);
307  }
308  break;
309  }
310  }
311 
324  template <class FeatureType>
326  {
327  //determine file type
328  FileTypes::Type type;
329  if (force_type != FileTypes::UNKNOWN)
330  {
331  type = force_type;
332  }
333  else
334  {
335  try
336  {
337  type = getType(filename);
338  }
340  {
341  return false;
342  }
343  }
344 
345  //load right file
346  if (type == FileTypes::FEATUREXML)
347  {
348  FeatureXMLFile().load(filename, map);
349  }
350  else if (type == FileTypes::TSV)
351  {
352  MsInspectFile().load(filename, map);
353  }
354  else if (type == FileTypes::PEPLIST)
355  {
356  SpecArrayFile().load(filename, map);
357  }
358  else if (type == FileTypes::KROENIK)
359  {
360  KroenikFile().load(filename, map);
361  }
362  else
363  {
364  return false;
365  }
366 
367  return true;
368  }
369 
370 private:
372 
378  String computeFileHash_(const String& filename) const;
379  };
380 
381 } //namespace
382 
383 #endif //OPENMS_FORMAT_FILEHANDLER_H
Type
Actual file types enum.
Definition: FileTypes.h:59
DTA2D File adapter.
Definition: DTA2DFile.h:64
msInspect file (.tsv)
Definition: FileTypes.h:87
PeakFileOptions options_
Definition: FileHandler.h:371
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:101
A more convenient string class.
Definition: String.h:56
MS2 file (.ms2)
Definition: FileTypes.h:74
File adapter for MzXML 2.1 files.
Definition: MzXMLFile.h:52
void reset()
Resets all internal values.
Definition: MSExperiment.h:635
LogType
Possible log types.
Definition: ProgressLogger.h:66
void load(const String &filename, FeatureMapType &feature_map)
Loads a MsInspect file into a featureXML.
Definition: MsInspectFile.h:78
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:80
A container for features.
Definition: FeatureMap.h:111
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
bool loadFeatures(const String &filename, FeatureMap< FeatureType > &map, FileTypes::Type force_type=FileTypes::UNKNOWN)
Loads a file into a FeatureMap.
Definition: FileHandler.h:325
File adapter for DTA files.
Definition: DTAFile.h:58
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
File adapter for Kroenik (HardKloer sibling) files.
Definition: KroenikFile.h:67
void setChecksum(const String &checksum, ChecksumType type)
sets the file&#39;s checksum
Mascot input file adapter.
Definition: MascotGenericFile.h:64
specArray file (.peplist)
Definition: FileTypes.h:88
File adapter for SpecArray (.pepList) files.
Definition: SpecArrayFile.h:61
void load(const String &filename, FeatureMap<> &feature_map)
loads the file with name filename into map and calls updateRanges().
File adapter for MzML files.
Definition: MzMLFile.h:58
void load(const String &filename, SpectrumType &spectrum)
Loads a DTA file to a spectrum.
Definition: DTAFile.h:76
Unknown file extension.
Definition: FileTypes.h:61
File adapter for MzData files.
Definition: MzDataFile.h:51
No progress logging.
Definition: ProgressLogger.h:70
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
kroenik file (.kroenik)
Definition: FileTypes.h:90
void load(const String &filename, MapType &map)
Loads a map from a MzML file.
Definition: MzMLFile.h:86
void store(const String &filename, const MapType &map) const
Stores a map in a MzML file.
Definition: MzMLFile.h:126
void store(const String &filename, const MapType &map) const
Stores a map in a DTA2D file.
Definition: DTA2DFile.h:268
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).
OpenMS feature file (.featureXML)
Definition: FileTypes.h:66
Representation of a mass spectrometry experiment.
Definition: MSExperiment.h:68
Secure Hash Algorithm-1.
Definition: SourceFile.h:55
This class provides Input/Output functionality for feature maps.
Definition: FeatureXMLFile.h:59
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:251
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:121
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
void load(const String &filename, FeatureMapType &feature_map)
Loads a SpecArray file into a featureXML.
Definition: SpecArrayFile.h:78
Conversion class to interconvert chromatograms.
Definition: ChromatogramTools.h:55
Options for loading files containing peak data.
Definition: PeakFileOptions.h:47
File adapter for MsInspect files.
Definition: MsInspectFile.h:61
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:90
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, FeatureMapType &feature_map)
Loads a Kroenik file into a featureXML.
Definition: KroenikFile.h:84
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:95
const std::vector< MSChromatogram< ChromatogramPeakType > > & getChromatograms() const
returns the chromatogram list
Definition: MSExperiment.h:768
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 1.11.1 Documentation generated on Thu Nov 14 2013 11:19:14 using doxygen 1.8.5