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-2016.
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 
124  template <class PeakType>
125  bool loadExperiment(const String& filename, MSExperiment<PeakType>& exp, FileTypes::Type force_type = FileTypes::UNKNOWN, ProgressLogger::LogType log = ProgressLogger::NONE, const bool rewrite_source_file = true, const bool compute_hash = true)
126  {
127  // setting the flag for hash recomputation only works if source file entries are rewritten
128  OPENMS_PRECONDITION(rewrite_source_file || !compute_hash, "Can't compute hash if no SourceFile written");
129 
130  //determine file type
131  FileTypes::Type type;
132  if (force_type != FileTypes::UNKNOWN)
133  {
134  type = force_type;
135  }
136  else
137  {
138  try
139  {
140  type = getType(filename);
141  }
143  {
144  return false;
145  }
146  }
147 
148  //load right file
149  switch (type)
150  {
151  case FileTypes::DTA:
152  exp.reset();
153  exp.resize(1);
154  DTAFile().load(filename, exp[0]);
155  break;
156 
157  case FileTypes::DTA2D:
158  {
159  DTA2DFile f;
160  f.getOptions() = options_;
161  f.setLogType(log);
162  f.load(filename, exp);
163  }
164 
165  break;
166 
167  case FileTypes::MZXML:
168  {
169  MzXMLFile f;
170  f.getOptions() = options_;
171  f.setLogType(log);
172  f.load(filename, exp);
173  }
174 
175  break;
176 
177  case FileTypes::MZDATA:
178  {
179  MzDataFile f;
180  f.getOptions() = options_;
181  f.setLogType(log);
182  f.load(filename, exp);
183  }
184  break;
185 
186  case FileTypes::MZML:
187  {
188  MzMLFile f;
189  f.getOptions() = options_;
190  f.setLogType(log);
191  f.load(filename, exp);
193  }
194  break;
195 
196  case FileTypes::MGF:
197  {
199  f.setLogType(log);
200  f.load(filename, exp);
201  }
202 
203  break;
204 
205  case FileTypes::MS2:
206  {
207  MS2File f;
208  f.setLogType(log);
209  f.load(filename, exp);
210  }
211 
212  break;
213 
214  case FileTypes::XMASS:
215  exp.reset();
216  exp.resize(1);
217  XMassFile().load(filename, exp[0]);
218  XMassFile().importExperimentalSettings(filename, exp);
219 
220  break;
221 
222  default:
223  return false;
224 
225  break;
226  }
227 
228  if (rewrite_source_file)
229  {
230  SourceFile src_file;
231  src_file.setNameOfFile(File::basename(filename));
232  String path_to_file = File::path(File::absolutePath(filename)); //convert to absolute path and strip file name
233 
234  // make sure we end up with at most 3 forward slashes
235  String uri = path_to_file.hasPrefix("/") ? String("file://") + path_to_file : String("file:///") + path_to_file;
236  src_file.setPathToFile(uri);
237  // this is more complicated since the data formats allowed by mzML are very verbose.
238  // this is prone to changing CV's... our writer will fall back to a default if the name given here is invalid.
239  src_file.setFileType(FileTypes::typeToMZML(type));
240 
241  if (compute_hash)
242  {
243  src_file.setChecksum(computeFileHash(filename), SourceFile::SHA1);
244  }
245 
246  exp.getSourceFiles().clear();
247  exp.getSourceFiles().push_back(src_file);
248  }
249 
250  return true;
251  }
252 
264  template <class PeakType>
266  {
267  //load right file
268  switch (getTypeByFileName(filename))
269  {
270  case FileTypes::DTA2D:
271  {
272  DTA2DFile f;
273  f.getOptions() = options_;
274  f.setLogType(log);
275  f.store(filename, exp);
276  }
277  break;
278 
279  case FileTypes::MZXML:
280  {
281  MzXMLFile f;
282  f.getOptions() = options_;
283  f.setLogType(log);
284  if (!exp.getChromatograms().empty())
285  {
286  MSExperiment<PeakType> exp2 = exp;
288  f.store(filename, exp2);
289  }
290  else
291  {
292  f.store(filename, exp);
293  }
294  }
295  break;
296 
297  case FileTypes::MZDATA:
298  {
299  MzDataFile f;
300  f.getOptions() = options_;
301  f.setLogType(log);
302  if (!exp.getChromatograms().empty())
303  {
304  MSExperiment<PeakType> exp2 = exp;
306  f.store(filename, exp2);
307  }
308  else
309  {
310  f.store(filename, exp);
311  }
312  }
313  break;
314 
315  default:
316  {
317  MzMLFile f;
318  f.getOptions() = options_;
319  f.setLogType(log);
320  f.store(filename, exp);
321  }
322  break;
323  }
324  }
325 
338  bool loadFeatures(const String& filename, FeatureMap& map, FileTypes::Type force_type = FileTypes::UNKNOWN);
339 
345  static String computeFileHash(const String& filename);
346 
347 private:
349 
350  };
351 
352 } //namespace
353 
354 #endif //OPENMS_FORMAT_FILEHANDLER_H
DTA2D File adapter.
Definition: DTA2DFile.h:64
PeakFileOptions options_
Definition: FileHandler.h:348
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
static String absolutePath(const String &file)
Replaces the relative path in the argument with the absolute path.
MzML file (.mzML)
Definition: FileTypes.h:73
A more convenient string class.
Definition: String.h:57
bool loadExperiment(const String &filename, MSExperiment< PeakType > &exp, FileTypes::Type force_type=FileTypes::UNKNOWN, ProgressLogger::LogType log=ProgressLogger::NONE, const bool rewrite_source_file=true, const bool compute_hash=true)
Loads a file into an MSExperiment.
Definition: FileHandler.h:125
File adapter for MzXML 2.1 files.
Definition: MzXMLFile.h:53
void reset()
Resets all internal values.
Definition: MSExperiment.h:696
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:94
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:107
static String path(const String &file)
Returns the path of the file (without the file name).
MzXML file (.mzXML)
Definition: FileTypes.h:65
void setFileType(const String &file_type)
sets the file type
File adapter for DTA files.
Definition: DTAFile.h:61
Description of a file location, used to store the origin of (meta) data.
Definition: SourceFile.h:47
void resize(Size s)
Definition: MSExperiment.h:124
File not found exception.
Definition: Exception.h:524
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
File adapter for 'XMass Analysis (fid)' files.
Definition: XMassFile.h:66
void setChecksum(const String &checksum, ChecksumType type)
sets the file's checksum
MS2 file (.ms2)
Definition: FileTypes.h:74
Read/write Mascot generic files (MGF).
Definition: MascotGenericFile.h:62
Unknown file extension.
Definition: FileTypes.h:61
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
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
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
XMass Analysis file (fid)
Definition: FileTypes.h:86
void store(const String &filename, const MapType &map) const
Stores a map in an MzML file.
Definition: MzMLFile.h:109
Mascot Generic Format (.mgf)
Definition: FileTypes.h:69
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
DTA2D file (.dta2d)
Definition: FileTypes.h:63
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).
Secure Hash Algorithm-1.
Definition: SourceFile.h:55
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:71
PeakFileOptions & getOptions()
Mutable access to the options for loading/storing.
MzData file (.mzData)
Definition: FileTypes.h:64
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:265
static String typeToMZML(Type type)
Returns the mzML name (TODO: switch to accession since they are more stable!)
DTA file (.dta)
Definition: FileTypes.h:62
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:93
void setPathToFile(const String &path_path_to_file)
sets the file path
Type
Actual file types enum.
Definition: FileTypes.h:59
PeakFileOptions & getOptions()
Mutable access to the options for loading/storing.
void load(const String &filename, MapType &map)
Loads a map from a MzData file.
Definition: MzDataFile.h:81
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:850
void setLogType(LogType type) const
Sets the progress log that should be used. The default type is NONE!
bool hasPrefix(const String &string) const
true if String begins with string, false otherwise
const std::vector< SourceFile > & getSourceFiles() const
returns a const reference to the source data file

OpenMS / TOPP release 2.0.0 Documentation generated on Tue Jan 31 2017 06:39:44 using doxygen 1.8.8