OpenMS
FileHandler Class Reference

Facilitates file handling by file type recognition. More...

#include <OpenMS/FORMAT/FileHandler.h>

Collaboration diagram for FileHandler:
[legend]

Public Member Functions

PeakFileOptionsgetOptions ()
 Mutable access to the options for loading/storing. More...
 
const PeakFileOptionsgetOptions () const
 Non-mutable access to the options for loading/storing. More...
 
FeatureFileOptionsgetFeatOptions ()
 Mutable access to the feature file options for loading/storing. More...
 
const FeatureFileOptionsgetFeatOptions () const
 Non-mutable access to the feature file options for loading/storing. More...
 
void setOptions (const PeakFileOptions &)
 set options for loading/storing More...
 
void setFeatOptions (const FeatureFileOptions &)
 set feature file options for loading/storing More...
 
void loadExperiment (const String &filename, PeakMap &exp, const std::vector< FileTypes::Type > allowed_types=std::vector< FileTypes::Type >(), ProgressLogger::LogType log=ProgressLogger::NONE, const bool rewrite_source_file=false, const bool compute_hash=false)
 Loads a file into an MSExperiment. More...
 
void storeExperiment (const String &filename, const PeakMap &exp, const std::vector< FileTypes::Type > allowed_types={}, ProgressLogger::LogType log=ProgressLogger::NONE)
 Stores an MSExperiment to a file. More...
 
void loadSpectrum (const String &filename, MSSpectrum &spec, const std::vector< FileTypes::Type > allowed_types={})
 Loads a single MSSpectrum from a file. More...
 
void storeSpectrum (const String &filename, MSSpectrum &spec, const std::vector< FileTypes::Type > allowed_types={})
 Stores a single MSSpectrum to a file. More...
 
void loadFeatures (const String &filename, FeatureMap &map, const std::vector< FileTypes::Type > allowed_types={}, ProgressLogger::LogType log=ProgressLogger::NONE)
 Loads a file into a FeatureMap. More...
 
void storeFeatures (const String &filename, const FeatureMap &map, const std::vector< FileTypes::Type > allowed_types={}, ProgressLogger::LogType log=ProgressLogger::NONE)
 Store a FeatureMap. More...
 
void loadConsensusFeatures (const String &filename, ConsensusMap &map, const std::vector< FileTypes::Type > allowed_types={}, ProgressLogger::LogType log=ProgressLogger::NONE)
 Loads a file into a ConsensusMap. More...
 
void storeConsensusFeatures (const String &filename, const ConsensusMap &map, const std::vector< FileTypes::Type > allowed_types={}, ProgressLogger::LogType log=ProgressLogger::NONE)
 Store a ConsensusFeatureMap. More...
 
void loadIdentifications (const String &filename, std::vector< ProteinIdentification > &additional_proteins, std::vector< PeptideIdentification > &additional_peptides, const std::vector< FileTypes::Type > allowed_types={}, ProgressLogger::LogType log=ProgressLogger::NONE)
 Loads an identification file into a proteinIdentifications and peptideIdentifications. More...
 
void storeIdentifications (const String &filename, const std::vector< ProteinIdentification > &additional_proteins, const std::vector< PeptideIdentification > &additional_peptides, const std::vector< FileTypes::Type > allowed_types={}, ProgressLogger::LogType log=ProgressLogger::NONE)
 Stores proteins and peptides into an Identification File. More...
 
void loadTransitions (const String &filename, TargetedExperiment &library, const std::vector< FileTypes::Type > allowed_types={}, ProgressLogger::LogType log=ProgressLogger::NONE)
 Load transitions of a spectral library. More...
 
void storeTransitions (const String &filename, const TargetedExperiment &library, const std::vector< FileTypes::Type > allowed_types={}, ProgressLogger::LogType log=ProgressLogger::NONE)
 Store transitions of a spectral library. More...
 
void loadTransformations (const String &filename, TransformationDescription &map, bool fit_model=true, const std::vector< FileTypes::Type > allowed_types={})
 Loads a file into Transformations. More...
 
void storeTransformations (const String &filename, const TransformationDescription &map, const std::vector< FileTypes::Type > allowed_types={})
 Store Transformations. More...
 
void storeQC (const String &input_file, const String &filename, const MSExperiment &exp, const FeatureMap &feature_map, std::vector< ProteinIdentification > &prot_ids, std::vector< PeptideIdentification > &pep_ids, const ConsensusMap &consensus_map=ConsensusMap(), const String &contact_name="", const String &contact_address="", const String &description="", const String &label="label", const bool remove_duplicate_features=false, const std::vector< FileTypes::Type > allowed_types={})
 Store QC info. More...
 

Static Public Member Functions

static FileTypes::Type getType (const String &filename)
 Tries to determine the file type (by name or content) More...
 
static FileTypes::Type getTypeByFileName (const String &filename)
 Try to get the file type from the filename. More...
 
static bool hasValidExtension (const String &filename, const FileTypes::Type type)
 Check if filename has the extension type. More...
 
static String stripExtension (const String &filename)
 If filename contains an extension, it will be removed (including the '.'). Special extensions, known to OpenMS, e.g. '.mzML.gz' will be recognized as well. More...
 
static String swapExtension (const String &filename, const FileTypes::Type new_type)
 Tries to find and remove a known file extension, and append the new one. More...
 
static FileTypes::Type getConsistentOutputfileType (const String &output_filename, const String &requested_type)
 Useful function for TOPP tools which have an 'out_type' parameter and want to know what output format to write. This function makes sure that the type derived from output_filename and requested_type are consistent, i.e. are either identical or one of them is UNKNOWN. Upon conflict, an error message is printed and the UNKNOWN type is returned. More...
 
static FileTypes::Type getTypeByContent (const String &filename)
 Determines the file type of a file by parsing the first few lines. More...
 
static bool isSupported (FileTypes::Type type)
 Returns if the file type is supported in this build of the library. More...
 
static String computeFileHash (const String &filename)
 Computes a SHA-1 hash value for the content of the given file. More...
 

Private Attributes

PeakFileOptions options_
 
FeatureFileOptions f_options_
 

Detailed Description

Facilitates file handling by file type recognition.

This class provides file type recognition from the file name and from the file content.

It also offer a common interface to load MSExperiment data and allows querying for supported file types.

See also
FileTypes

Member Function Documentation

◆ computeFileHash()

static String computeFileHash ( const String filename)
static

Computes a SHA-1 hash value for the content of the given file.

Returns
The SHA-1 hash of the given file.

◆ getConsistentOutputfileType()

static FileTypes::Type getConsistentOutputfileType ( const String output_filename,
const String requested_type 
)
static

Useful function for TOPP tools which have an 'out_type' parameter and want to know what output format to write. This function makes sure that the type derived from output_filename and requested_type are consistent, i.e. are either identical or one of them is UNKNOWN. Upon conflict, an error message is printed and the UNKNOWN type is returned.

Parameters
output_filenameA full filename (with none, absolute or relative paths) whose type is determined using FileHandler::getTypeByFileName() internally
requested_typeA type as string, usually obtained from '-out_type', e.g. "FASTA" (case insensitive). The string can be empty (yields UNKNOWN for this type)
Returns
A consistent file type or UNKNOWN upon conflict

◆ getFeatOptions() [1/2]

FeatureFileOptions& getFeatOptions ( )

Mutable access to the feature file options for loading/storing.

◆ getFeatOptions() [2/2]

const FeatureFileOptions& getFeatOptions ( ) const

Non-mutable access to the feature file options for loading/storing.

◆ getOptions() [1/2]

PeakFileOptions& getOptions ( )

Mutable access to the options for loading/storing.

Referenced by TOPPFLASHDeconv::main_(), and TOPPViewBase::metadataFileDialog().

◆ getOptions() [2/2]

const PeakFileOptions& getOptions ( ) const

Non-mutable access to the options for loading/storing.

◆ getType()

static FileTypes::Type getType ( const String filename)
static

Tries to determine the file type (by name or content)

First tries to determine the type from the file name. If this fails, the type is determined from the file content.

Parameters
filenamethe name of the file to check
Returns
A FileTypes::Type corresponding to the extension, or FileTypes::UNKNOWN if not determinable
Exceptions
Exception::FileNotFoundis thrown if the file is not present

Referenced by TOPPViewBase::addDataFile(), and TOPPOpenSwathBase::performCalibration().

◆ getTypeByContent()

static FileTypes::Type getTypeByContent ( const String filename)
static

Determines the file type of a file by parsing the first few lines.

Exceptions
Exception::FileNotFoundis thrown if the file is not present

Referenced by InspectOutfile::getExperiment().

◆ getTypeByFileName()

static FileTypes::Type getTypeByFileName ( const String filename)
static

Try to get the file type from the filename.

Parameters
filenamethe name of the file to check
Returns
A FileTypes::Type corresponding to the extension, or FileTypes::UNKNOWN if not determinable
Exceptions
Exception::FileNotFoundis thrown if the file is not present

Referenced by TOPPOpenSwathBase::loadSwathFiles_().

◆ hasValidExtension()

static bool hasValidExtension ( const String filename,
const FileTypes::Type  type 
)
static

Check if filename has the extension type.

If the extension is not known (e.g. '.tmp') this is also allowed. However, if the extension is another one (neither type nor unknown), false is returned.

◆ isSupported()

static bool isSupported ( FileTypes::Type  type)
static

Returns if the file type is supported in this build of the library.

◆ loadConsensusFeatures()

void loadConsensusFeatures ( const String filename,
ConsensusMap map,
const std::vector< FileTypes::Type allowed_types = {},
ProgressLogger::LogType  log = ProgressLogger::NONE 
)

Loads a file into a ConsensusMap.

Parameters
filenamethe file name of the file to load.
mapThe ConsensusMap to load the data into.
allowed_typesA vector of supported filetypes. If the vector is empty, load from any type that we have a handler for. Otherwise getType() is called internally to check the type
logProgress logging mode
Exceptions
Exception::FileNotFoundis thrown if the file could not be opened
Exception::ParseErroris thrown if an error occurs during parsing

Referenced by TOPPViewBase::addDataFile(), TOPPViewBase::fileChanged_(), and TOPPGNPSExport::main_().

◆ loadExperiment()

void loadExperiment ( const String filename,
PeakMap exp,
const std::vector< FileTypes::Type allowed_types = std::vector< FileTypes::Type >(),
ProgressLogger::LogType  log = ProgressLogger::NONE,
const bool  rewrite_source_file = false,
const bool  compute_hash = false 
)

Loads a file into an MSExperiment.

Parameters
filenameThe file name of the file to load.
expThe experiment to load the data into.
allowed_typesA vector of supported filetypes. If the vector is empty, load from any type that we have a handler for. Otherwise getType() is called internally to check the type
logProgress logging mode
rewrite_source_fileSet's the SourceFile name and path to the current file. Note that this looses the link to the primary MS run the file originated from.
compute_hashIf source files are rewritten, this flag triggers a recomputation of hash values. A SHA1 string gets stored in the checksum member of SourceFile.
Exceptions
Exception::FileNotFoundis thrown if the file could not be opened
Exception::ParseErroris thrown if an error occurs during parsing

Referenced by TOPPViewBase::addDataFile(), CachedSwathFileConsumer::ensureMapsAreFilled_(), TOPPViewBase::fileChanged_(), InspectOutfile::getExperiment(), TOPPFLASHDeconv::main_(), and NucleicAcidSearchEngine::main_().

◆ loadFeatures()

void loadFeatures ( const String filename,
FeatureMap map,
const std::vector< FileTypes::Type allowed_types = {},
ProgressLogger::LogType  log = ProgressLogger::NONE 
)

Loads a file into a FeatureMap.

Parameters
filenamethe file name of the file to load.
mapThe FeatureMap to load the data into.
allowed_typesA vector of supported filetypes. If the vector is empty, load from any type that we have a handler for. Otherwise getType() is called internally to check the type
logProgress logging mode
Exceptions
Exception::FileNotFoundis thrown if the file could not be opened
Exception::ParseErroris thrown if an error occurs during parsing

Referenced by TOPPViewBase::addDataFile(), and TOPPViewBase::fileChanged_().

◆ loadIdentifications()

void loadIdentifications ( const String filename,
std::vector< ProteinIdentification > &  additional_proteins,
std::vector< PeptideIdentification > &  additional_peptides,
const std::vector< FileTypes::Type allowed_types = {},
ProgressLogger::LogType  log = ProgressLogger::NONE 
)

Loads an identification file into a proteinIdentifications and peptideIdentifications.

Parameters
filenamethe file name of the file to load.
additional_proteinsThe proteinIdentification vector to load the data into.
additional_peptidesThe peptideIdentification vector to load the data into.
allowed_typesA vector of supported filetypes. If the vector is empty, load from any type that we have a handler for. Otherwise getType() is called internally to check the type
logProgress logging mode
Exceptions
Exception::FileNotFoundis thrown if the file could not be opened
Exception::ParseErroris thrown if an error occurs during parsing

Referenced by TOPPViewBase::addDataFile().

◆ loadSpectrum()

void loadSpectrum ( const String filename,
MSSpectrum spec,
const std::vector< FileTypes::Type allowed_types = {} 
)

Loads a single MSSpectrum from a file.

Parameters
filenameThe file name of the file to load.
specThe spectrum to load the data into.
allowed_typesA vector of supported filetypes. If the vector is empty, load from any type that we have a handler for. Otherwise getType() is called internally to check the type
Exceptions
Exception::FileNotFoundis thrown if the file could not be opened
Exception::ParseErroris thrown if an error occurs during parsing

◆ loadTransformations()

void loadTransformations ( const String filename,
TransformationDescription map,
bool  fit_model = true,
const std::vector< FileTypes::Type allowed_types = {} 
)

Loads a file into Transformations.

Parameters
filenamethe file name of the file to load.
[out]mapThe Transformations to load the data into.
fit_modelCall fitModel() on the map before returning?
allowed_typesA vector of supported filetypes. If the vector is empty, load from any type that we have a handler for. Otherwise getType() is called internally to check the type
Exceptions
Exception::FileNotFoundis thrown if the file could not be opened
Exception::ParseErroris thrown if an error occurs during parsing

Referenced by TOPPOpenSwathBase::performCalibration().

◆ loadTransitions()

void loadTransitions ( const String filename,
TargetedExperiment library,
const std::vector< FileTypes::Type allowed_types = {},
ProgressLogger::LogType  log = ProgressLogger::NONE 
)

Load transitions of a spectral library.

Parameters
filenamethe file name of the file to read.
libraryThe TargetedExperiment to load.
allowed_typesA vector of supported filetypes. If the vector is empty, load from any type that we have a handler for. Otherwise getType() is called internally to check the type
logProgress logging mode
Exceptions
Exception::FileNotFoundis thrown if the file could not be opened
Exception::ParseErroris thrown if an error occurs during parsing

Referenced by TOPPOpenSwathBase::loadTransitionList().

◆ setFeatOptions()

void setFeatOptions ( const FeatureFileOptions )

set feature file options for loading/storing

◆ setOptions()

void setOptions ( const PeakFileOptions )

set options for loading/storing

Referenced by TOPPFLASHDeconv::main_(), and NucleicAcidSearchEngine::main_().

◆ storeConsensusFeatures()

void storeConsensusFeatures ( const String filename,
const ConsensusMap map,
const std::vector< FileTypes::Type allowed_types = {},
ProgressLogger::LogType  log = ProgressLogger::NONE 
)

Store a ConsensusFeatureMap.

Parameters
filenamethe file name of the file to write.
mapThe ConsensusMap to store.
allowed_typesA vector of supported filetypes. If empty we try to guess based on the filename. If that fails we throw UnableToCreateFile. If there is only one allowed type, check whether it agrees with the filename, and throw UnableToCreateFile if they disagree.
logProgress logging mode
Exceptions
Exception::UnableToCreateFileis thrown if the file could not be written

◆ storeExperiment()

void storeExperiment ( const String filename,
const PeakMap exp,
const std::vector< FileTypes::Type allowed_types = {},
ProgressLogger::LogType  log = ProgressLogger::NONE 
)

Stores an MSExperiment to a file.

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.

Parameters
filenameThe name of the file to store the data in.
expThe experiment to store.
allowed_typesA vector of supported filetypes. If empty we try to guess based on the filename. If that fails we throw UnableToCreateFile. If there is only one allowed type, check whether it agrees with the filename, and throw UnableToCreateFile if they disagree.
logProgress logging mode
Exceptions
Exception::UnableToCreateFileis thrown if the file could not be written

Referenced by TOPPFLASHDeconv::main_(), and NucleicAcidSearchEngine::main_().

◆ storeFeatures()

void storeFeatures ( const String filename,
const FeatureMap map,
const std::vector< FileTypes::Type allowed_types = {},
ProgressLogger::LogType  log = ProgressLogger::NONE 
)

Store a FeatureMap.

Parameters
filenamethe file name of the file to write.
mapThe FeatureMap to store.
allowed_typesA vector of supported filetypes. If empty we try to guess based on the filename. If that fails we throw UnableToCreateFile. If there is only one allowed type, check whether it agrees with the filename, and throw UnableToCreateFile if they disagree.
logProgress logging mode
Exceptions
Exception::UnableToCreateFileis thrown if the file could not be written

◆ storeIdentifications()

void storeIdentifications ( const String filename,
const std::vector< ProteinIdentification > &  additional_proteins,
const std::vector< PeptideIdentification > &  additional_peptides,
const std::vector< FileTypes::Type allowed_types = {},
ProgressLogger::LogType  log = ProgressLogger::NONE 
)

Stores proteins and peptides into an Identification File.

Parameters
filenamethe file name of the file to write to.
additional_proteinsThe proteinIdentification vector to load the data from.
additional_peptidesThe peptideIdentification vector to load the data from.
allowed_typesA vector of supported filetypes. If empty we try to guess based on the filename. If that fails we throw UnableToCreateFile. If there is only one allowed type, check whether it agrees with the filename, and throw UnableToCreateFile if they disagree.
logProgress logging mode
Exceptions
Exception::UnableToCreateFileis thrown if the file could not be written

Referenced by NucleicAcidSearchEngine::main_().

◆ storeQC()

void storeQC ( const String input_file,
const String filename,
const MSExperiment exp,
const FeatureMap feature_map,
std::vector< ProteinIdentification > &  prot_ids,
std::vector< PeptideIdentification > &  pep_ids,
const ConsensusMap consensus_map = ConsensusMap(),
const String contact_name = "",
const String contact_address = "",
const String description = "",
const String label = "label",
const bool  remove_duplicate_features = false,
const std::vector< FileTypes::Type allowed_types = {} 
)

Store QC info.

Stores QC data in mzQC file with JSON format

Parameters
input_filemzML input file name
filenamemzQC output file name
expMSExperiment to extract QC data from, prior sortSpectra() and updateRanges() required
feature_mapFeatureMap from feature file (featureXML)
prot_idsprotein identifications from ID file (idXML)
pep_idsprotein identifications from ID file (idXML)
consensus_mapan optional consensus map to store.
contact_namename of the person creating the mzQC file
contact_addresscontact address (mail/e-mail or phone) of the person creating the mzQC file
descriptiondescription and comments about the mzQC file contents
labelunique and informative label for the run
remove_duplicate_featureswhether to remove duplicate features only for QCML for now
allowed_typesA vector of supported filetypes. If empty we try to guess based on the filename. If that fails we throw UnableToCreateFile. If there is only one allowed type, check whether it agrees with the filename, and throw UnableToCreateFile if they disagree.
Exceptions
Exception::UnableToCreateFileis thrown if the file could not be written

◆ storeSpectrum()

void storeSpectrum ( const String filename,
MSSpectrum spec,
const std::vector< FileTypes::Type allowed_types = {} 
)

Stores a single MSSpectrum to a file.

Parameters
filenameThe file name of the file to store.
specThe spectrum to store the data from.
allowed_typesA vector of supported filetypes. If the vector is empty, load from any type that we have a handler for. Otherwise getType() is called internally to check the type
Exceptions
Exception::UnableToCreateFileis thrown if the file could not be written

◆ storeTransformations()

void storeTransformations ( const String filename,
const TransformationDescription map,
const std::vector< FileTypes::Type allowed_types = {} 
)

Store Transformations.

Parameters
filenamethe file name of the file to write.
mapThe Transformations to store.
allowed_typesA vector of supported filetypes. If empty we try to guess based on the filename. If that fails we throw UnableToCreateFile. If there is only one allowed type, check whether it agrees with the filename, and throw UnableToCreateFile if they disagree.
Exceptions
Exception::UnableToCreateFileis thrown if the file could not be written

Referenced by TOPPOpenSwathBase::performCalibration().

◆ storeTransitions()

void storeTransitions ( const String filename,
const TargetedExperiment library,
const std::vector< FileTypes::Type allowed_types = {},
ProgressLogger::LogType  log = ProgressLogger::NONE 
)

Store transitions of a spectral library.

Parameters
filenamethe file name of the file to write.
libraryThe TargetedExperiment to store.
allowed_typesA vector of supported filetypes. If empty we try to guess based on the filename. If that fails we throw UnableToCreateFile. If there is only one allowed type, check whether it agrees with the filename, and throw UnableToCreateFile if they disagree.
logProgress logging mode
Exceptions
Exception::UnableToCreateFileis thrown if the file could not be written

◆ stripExtension()

static String stripExtension ( const String filename)
static

If filename contains an extension, it will be removed (including the '.'). Special extensions, known to OpenMS, e.g. '.mzML.gz' will be recognized as well.

E.g. 'experiment.featureXML' becomes 'experiment' and 'c:\files\data.mzML.gz' becomes 'c:\files\data' If the extension is unknown, the everything in the basename of the file after the last '.' is removed. E.g. 'future.newEnding' becomes 'future' If the filename does not contain '.', but the path (if any) does, nothing is removed, e.g. '/my.dotted.dir/filename' is returned unchanged.

Parameters
filenamethe name to strip
Returns
the stripped filename

Referenced by TOPPViewBase::addDataFile().

◆ swapExtension()

static String swapExtension ( const String filename,
const FileTypes::Type  new_type 
)
static

Tries to find and remove a known file extension, and append the new one.

Internally calls 'stripExtension()' and adds the new suffix to the result. E.g. 'experiment.featureXML'+ FileTypes::TRANSFORMATIONXML becomes 'experiment.trafoXML' and 'c:\files\data.mzML.gz' + FileTypes::FEATUREXML becomes 'c:\files\data.featureXML' If the existing extension is unknown, the everything after the last '.' is removed, e.g. 'exp.tmp' + FileTypes::IDXML becomes 'exp.idXML'

Parameters
filenamethe original filename
new_typethe FileTypes::Types to use to set the new extension
Returns
the updated string

Referenced by TOPPViewBase::addDataFile().

Member Data Documentation

◆ f_options_

FeatureFileOptions f_options_
private

◆ options_

PeakFileOptions options_
private