1
// -*- mode: C++; tab-width: 2; -*-
4
// --------------------------------------------------------------------------
5
// OpenMS Mass Spectrometry Framework
6
// --------------------------------------------------------------------------
7
// Copyright (C) 2003-2011 -- Oliver Kohlbacher, Knut Reinert
9
// This library is free software; you can redistribute it and/or
10
// modify it under the terms of the GNU Lesser General Public
11
// License as published by the Free Software Foundation; either
12
// version 2.1 of the License, or (at your option) any later version.
14
// This library is distributed in the hope that it will be useful,
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
// Lesser General Public License for more details.
19
// You should have received a copy of the GNU Lesser General Public
20
// License along with this library; if not, write to the Free Software
21
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
// --------------------------------------------------------------------------
24
// $Maintainer: Andreas Bertsch $
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.
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.
30
// --------------------------------------------------------------------------
31
// $Maintainer: Stephan Aiche $
25
32
// $Authors: Marc Sturm $
26
33
// --------------------------------------------------------------------------
54
@brief Facilitates file handling by file type recognition.
56
This class provides file type recognition from the file name and
57
from the file content.
59
It also offer a common interface to load MSExperiment data
60
and allows querying for supported file types.
66
class OPENMS_DLLAPI FileHandler
70
/// String representations of the file types
71
static const std::string NamesOfTypes[FileTypes::SIZE_OF_TYPE];
74
@brief Tries to determine the file type (by name or content)
76
First the type is determined from the file name.
77
If this fails, the type is determined from the file content.
79
@exception Exception::FileNotFound is thrown if the file is not present
81
static FileTypes::Type getType(const String& filename);
84
/// Determines the file type from a file name
85
static FileTypes::Type getTypeByFileName(const String& filename);
88
@brief Determines the file type of a file by parsing the first few lines
90
@exception Exception::FileNotFound is thrown if the file is not present
92
static FileTypes::Type getTypeByContent(const String& filename);
94
/// Converts a file type name into a Type
95
static FileTypes::Type nameToType(const String& name);
97
/// Converts a Type into a file type name
98
static String typeToName(FileTypes::Type type);
100
/// Returns if the file type is supported in this build of the library
101
static bool isSupported(FileTypes::Type type);
61
@brief Facilitates file handling by file type recognition.
63
This class provides file type recognition from the file name and
64
from the file content.
66
It also offer a common interface to load MSExperiment data
67
and allows querying for supported file types.
73
class OPENMS_DLLAPI FileHandler
77
@brief Tries to determine the file type (by name or content)
79
First the type is determined from the file name.
80
If this fails, the type is determined from the file content.
82
@exception Exception::FileNotFound is thrown if the file is not present
84
static FileTypes::Type getType(const String& filename);
87
/// Determines the file type from a file name
88
static FileTypes::Type getTypeByFileName(const String& filename);
91
@brief Determines the file type of a file by parsing the first few lines
93
@exception Exception::FileNotFound is thrown if the file is not present
95
static FileTypes::Type getTypeByContent(const String& filename);
97
/// Returns if the file type is supported in this build of the library
98
static bool isSupported(FileTypes::Type type);
103
100
/// Mutable access to the options for loading/storing
104
101
PeakFileOptions& getOptions();
106
103
/// Non-mutable access to the options for loading/storing
107
104
const PeakFileOptions& getOptions() const;
110
@brief Loads a file into an MSExperiment
112
@param filename The file name of the file to load.
113
@param exp The experiment to load the data into.
114
@param force_type Forces to load the file with that file type. If no type is forced, it is determined from the extention ( or from the content if that fails).
115
@param log Progress logging mode
117
@return true if the file could be loaded, false otherwise
119
@exception Exception::FileNotFound is thrown if the file could not be opened
120
@exception Exception::ParseError is thrown if an error occurs during parsing
122
template <class PeakType> bool loadExperiment(const String& filename, MSExperiment<PeakType>& exp, FileTypes::Type force_type = FileTypes::UNKNOWN, ProgressLogger::LogType log = ProgressLogger::NONE)
124
//determine file type
125
FileTypes::Type type;
126
if (force_type != FileTypes::UNKNOWN)
134
type = getType(filename);
136
catch(Exception::FileNotFound)
148
DTAFile().load(filename,exp[0]);
151
case FileTypes::DTA2D:
154
f.getOptions() = options_;
156
f.load(filename,exp);
160
case FileTypes::MZXML:
163
f.getOptions() = options_;
165
f.load(filename,exp);
169
case FileTypes::MZDATA:
172
f.getOptions() = options_;
174
f.load(filename,exp);
178
case FileTypes::MZML:
181
f.getOptions() = options_;
183
f.load(filename,exp);
184
ChromatogramTools().convertSpectraToChromatograms<MSExperiment<PeakType> >(exp, true);
192
f.load(filename, exp);
199
f.load(filename, exp);
202
case FileTypes::XMASS:
206
XMassFile().load(filename, exp[0]);
207
XMassFile().importExperimentalSettings(filename, exp);
218
@brief Stores an MSExperiment to a file
220
The file type to store the data in is determined by the file name. Supported formats for storing are mzML, mzXML, mzData and DTA2D. If the file format cannot be determined from the file name, the mzML format is used.
222
@param filename The name of the file to store the data in.
223
@param exp The experiment to store.
224
@param log Progress logging mode
226
@exception Exception::UnableToCreateFile is thrown if the file could not be written
228
template <class PeakType> void storeExperiment(const String& filename, const MSExperiment<PeakType>& exp, ProgressLogger::LogType log = ProgressLogger::NONE)
231
switch(getTypeByFileName(filename))
233
case FileTypes::DTA2D:
236
f.getOptions() = options_;
238
f.store(filename,exp);
241
case FileTypes::MZXML:
244
f.getOptions() = options_;
246
if ( !exp.getChromatograms().empty() )
248
MSExperiment<PeakType> exp2 = exp;
249
ChromatogramTools().convertChromatogramsToSpectra<MSExperiment<PeakType> >(exp2);
250
f.store(filename,exp2);
254
f.store(filename, exp);
258
case FileTypes::MZDATA:
261
f.getOptions() = options_;
263
if ( !exp.getChromatograms().empty() )
265
MSExperiment<PeakType> exp2 = exp;
266
ChromatogramTools().convertChromatogramsToSpectra<MSExperiment<PeakType> >(exp2);
267
f.store(filename,exp2);
271
f.store(filename, exp);
278
f.getOptions() = options_;
280
f.store(filename,exp);
288
@brief Loads a file into a FeatureMap
290
@param filename the file name of the file to load.
291
@param map The FeatureMap to load the data into.
292
@param force_type Forces to load the file with that file type. If no type is forced, it is determined from the extention ( or from the content if that fails).
294
@return true if the file could be loaded, false otherwise
296
@exception Exception::FileNotFound is thrown if the file could not be opened
297
@exception Exception::ParseError is thrown if an error occurs during parsing
299
template <class FeatureType> bool loadFeatures(const String& filename, FeatureMap<FeatureType>& map, FileTypes::Type force_type = FileTypes::UNKNOWN)
301
//determine file type
302
FileTypes::Type type;
303
if (force_type != FileTypes::UNKNOWN)
311
type = getType(filename);
313
catch(Exception::FileNotFound)
320
if (type==FileTypes::FEATUREXML)
322
FeatureXMLFile().load(filename,map);
324
else if (type==FileTypes::TSV)
326
MsInspectFile().load(filename,map);
328
else if (type==FileTypes::PEPLIST)
330
SpecArrayFile().load(filename,map);
332
else if (type==FileTypes::KROENIK)
334
KroenikFile().load(filename,map);
345
PeakFileOptions options_;
107
@brief Loads a file into an MSExperiment
109
@param filename The file name of the file to load.
110
@param exp The experiment to load the data into.
111
@param force_type Forces to load the file with that file type. If no type is forced, it is determined from the extention ( or from the content if that fails).
112
@param log Progress logging mode
113
@param compute_hash Computes a hash value for the loaded file and stores it in the SourceFile
115
@return true if the file could be loaded, false otherwise
117
@exception Exception::FileNotFound is thrown if the file could not be opened
118
@exception Exception::ParseError is thrown if an error occurs during parsing
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)
123
//determine file type
124
FileTypes::Type type;
125
if (force_type != FileTypes::UNKNOWN)
133
type = getType(filename);
135
catch (Exception::FileNotFound)
147
DTAFile().load(filename, exp[0]);
150
case FileTypes::DTA2D:
153
f.getOptions() = options_;
155
f.load(filename, exp);
160
case FileTypes::MZXML:
163
f.getOptions() = options_;
165
f.load(filename, exp);
170
case FileTypes::MZDATA:
173
f.getOptions() = options_;
175
f.load(filename, exp);
179
case FileTypes::MZML:
182
f.getOptions() = options_;
184
f.load(filename, exp);
185
ChromatogramTools().convertSpectraToChromatograms<MSExperiment<PeakType> >(exp, true);
193
f.load(filename, exp);
202
f.load(filename, exp);
207
case FileTypes::XMASS:
210
XMassFile().load(filename, exp[0]);
211
XMassFile().importExperimentalSettings(filename, exp);
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));
230
src_file.setChecksum(computeFileHash_(filename), SourceFile::SHA1);
233
exp.getSourceFiles().clear();
234
exp.getSourceFiles().push_back(src_file);
240
@brief Stores an MSExperiment to a file
242
The file type to store the data in is determined by the file name. Supported formats for storing are mzML, mzXML, mzData and DTA2D. If the file format cannot be determined from the file name, the mzML format is used.
244
@param filename The name of the file to store the data in.
245
@param exp The experiment to store.
246
@param log Progress logging mode
248
@exception Exception::UnableToCreateFile is thrown if the file could not be written
250
template <class PeakType>
251
void storeExperiment(const String& filename, const MSExperiment<PeakType>& exp, ProgressLogger::LogType log = ProgressLogger::NONE)
254
switch (getTypeByFileName(filename))
256
case FileTypes::DTA2D:
259
f.getOptions() = options_;
261
f.store(filename, exp);
265
case FileTypes::MZXML:
268
f.getOptions() = options_;
270
if (!exp.getChromatograms().empty())
272
MSExperiment<PeakType> exp2 = exp;
273
ChromatogramTools().convertChromatogramsToSpectra<MSExperiment<PeakType> >(exp2);
274
f.store(filename, exp2);
278
f.store(filename, exp);
283
case FileTypes::MZDATA:
286
f.getOptions() = options_;
288
if (!exp.getChromatograms().empty())
290
MSExperiment<PeakType> exp2 = exp;
291
ChromatogramTools().convertChromatogramsToSpectra<MSExperiment<PeakType> >(exp2);
292
f.store(filename, exp2);
296
f.store(filename, exp);
304
f.getOptions() = options_;
306
f.store(filename, exp);
313
@brief Loads a file into a FeatureMap
315
@param filename the file name of the file to load.
316
@param map The FeatureMap to load the data into.
317
@param force_type Forces to load the file with that file type. If no type is forced, it is determined from the extention (or from the content if that fails).
319
@return true if the file could be loaded, false otherwise
321
@exception Exception::FileNotFound is thrown if the file could not be opened
322
@exception Exception::ParseError is thrown if an error occurs during parsing
324
template <class FeatureType>
325
bool loadFeatures(const String& filename, FeatureMap<FeatureType>& map, FileTypes::Type force_type = FileTypes::UNKNOWN)
327
//determine file type
328
FileTypes::Type type;
329
if (force_type != FileTypes::UNKNOWN)
337
type = getType(filename);
339
catch (Exception::FileNotFound)
346
if (type == FileTypes::FEATUREXML)
348
FeatureXMLFile().load(filename, map);
350
else if (type == FileTypes::TSV)
352
MsInspectFile().load(filename, map);
354
else if (type == FileTypes::PEPLIST)
356
SpecArrayFile().load(filename, map);
358
else if (type == FileTypes::KROENIK)
360
KroenikFile().load(filename, map);
371
PeakFileOptions options_;
374
@brief Computes a SHA-1 hash value for the content of the given file.
376
@return The SHA-1 hash of the given file.
378
String computeFileHash_(const String& filename) const;