Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

FileHandler.h

Go to the documentation of this file.
00001 // --------------------------------------------------------------------------
00002 //                   OpenMS -- Open-Source Mass Spectrometry
00003 // --------------------------------------------------------------------------
00004 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
00005 // ETH Zurich, and Freie Universitaet Berlin 2002-2012.
00006 //
00007 // This software is released under a three-clause BSD license:
00008 //  * Redistributions of source code must retain the above copyright
00009 //    notice, this list of conditions and the following disclaimer.
00010 //  * Redistributions in binary form must reproduce the above copyright
00011 //    notice, this list of conditions and the following disclaimer in the
00012 //    documentation and/or other materials provided with the distribution.
00013 //  * Neither the name of any author or any participating institution
00014 //    may be used to endorse or promote products derived from this software
00015 //    without specific prior written permission.
00016 // For a full list of authors, refer to the file AUTHORS.
00017 // --------------------------------------------------------------------------
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
00022 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00025 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00027 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00028 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // --------------------------------------------------------------------------
00031 // $Maintainer: Andreas Bertsch $
00032 // $Authors: Marc Sturm $
00033 // --------------------------------------------------------------------------
00034 
00035 #ifndef OPENMS_FORMAT_FILEHANDLER_H
00036 #define OPENMS_FORMAT_FILEHANDLER_H
00037 
00038 #include <OpenMS/config.h>
00039 #include <OpenMS/FORMAT/FileTypes.h>
00040 
00041 #include <OpenMS/FORMAT/DTAFile.h>
00042 #include <OpenMS/FORMAT/DTA2DFile.h>
00043 #include <OpenMS/FORMAT/MzXMLFile.h>
00044 #include <OpenMS/FORMAT/MzMLFile.h>
00045 #include <OpenMS/FORMAT/FeatureXMLFile.h>
00046 #include <OpenMS/FORMAT/MzDataFile.h>
00047 #include <OpenMS/FORMAT/MascotGenericFile.h>
00048 #include <OpenMS/FORMAT/MS2File.h>
00049 #include <OpenMS/FORMAT/XMassFile.h>
00050 
00051 #include <OpenMS/FORMAT/MsInspectFile.h>
00052 #include <OpenMS/FORMAT/SpecArrayFile.h>
00053 #include <OpenMS/FORMAT/KroenikFile.h>
00054 
00055 #include <OpenMS/KERNEL/ChromatogramTools.h>
00056 #include <OpenMS/CONCEPT/ProgressLogger.h>
00057 
00058 namespace OpenMS
00059 {
00073   class OPENMS_DLLAPI FileHandler
00074   {
00075 public:
00084     static FileTypes::Type getType(const String & filename);
00085 
00086 
00088     static FileTypes::Type getTypeByFileName(const String & filename);
00089 
00095     static FileTypes::Type getTypeByContent(const String & filename);
00096 
00098     static bool isSupported(FileTypes::Type type);
00099 
00101     PeakFileOptions & getOptions();
00102 
00104     const PeakFileOptions & getOptions() const;
00105 
00119     template <class PeakType>
00120     bool loadExperiment(const String & filename, MSExperiment<PeakType> & exp, FileTypes::Type force_type = FileTypes::UNKNOWN, ProgressLogger::LogType log = ProgressLogger::NONE)
00121     {
00122       //determine file type
00123       FileTypes::Type type;
00124       if (force_type != FileTypes::UNKNOWN)
00125       {
00126         type = force_type;
00127       }
00128       else
00129       {
00130         try
00131         {
00132           type = getType(filename);
00133         }
00134         catch (Exception::FileNotFound)
00135         {
00136           return false;
00137         }
00138       }
00139 
00140       //load right file
00141       switch (type)
00142       {
00143       case FileTypes::DTA:
00144         exp.reset();
00145         exp.resize(1);
00146         DTAFile().load(filename, exp[0]);
00147         return true;
00148 
00149         break;
00150 
00151       case FileTypes::DTA2D:
00152       {
00153         DTA2DFile f;
00154         f.getOptions() = options_;
00155         f.setLogType(log);
00156         f.load(filename, exp);
00157         return true;
00158       }
00159       break;
00160 
00161       case FileTypes::MZXML:
00162       {
00163         MzXMLFile f;
00164         f.getOptions() = options_;
00165         f.setLogType(log);
00166         f.load(filename, exp);
00167         return true;
00168       }
00169       break;
00170 
00171       case FileTypes::MZDATA:
00172       {
00173         MzDataFile f;
00174         f.getOptions() = options_;
00175         f.setLogType(log);
00176         f.load(filename, exp);
00177         return true;
00178       }
00179       break;
00180 
00181       case FileTypes::MZML:
00182       {
00183         MzMLFile f;
00184         f.getOptions() = options_;
00185         f.setLogType(log);
00186         f.load(filename, exp);
00187         ChromatogramTools().convertSpectraToChromatograms<MSExperiment<PeakType> >(exp, true);
00188         return true;
00189       }
00190       break;
00191 
00192       case FileTypes::MGF:
00193       {
00194         MascotGenericFile f;
00195         f.setLogType(log);
00196         f.load(filename, exp);
00197         return true;
00198       }
00199 
00200       case FileTypes::MS2:
00201       {
00202         MS2File f;
00203         f.setLogType(log);
00204         f.load(filename, exp);
00205         return true;
00206       }
00207 
00208       case FileTypes::XMASS:
00209       {
00210         exp.reset();
00211         exp.resize(1);
00212         XMassFile().load(filename, exp[0]);
00213         XMassFile().importExperimentalSettings(filename, exp);
00214         return true;
00215       }
00216 
00217       default:
00218         break;
00219       }
00220 
00221       return false;
00222     }
00223 
00235     template <class PeakType>
00236     void storeExperiment(const String & filename, const MSExperiment<PeakType> & exp, ProgressLogger::LogType log = ProgressLogger::NONE)
00237     {
00238       //load right file
00239       switch (getTypeByFileName(filename))
00240       {
00241       case FileTypes::DTA2D:
00242       {
00243         DTA2DFile f;
00244         f.getOptions() = options_;
00245         f.setLogType(log);
00246         f.store(filename, exp);
00247       }
00248       break;
00249 
00250       case FileTypes::MZXML:
00251       {
00252         MzXMLFile f;
00253         f.getOptions() = options_;
00254         f.setLogType(log);
00255         if (!exp.getChromatograms().empty())
00256         {
00257           MSExperiment<PeakType> exp2 = exp;
00258           ChromatogramTools().convertChromatogramsToSpectra<MSExperiment<PeakType> >(exp2);
00259           f.store(filename, exp2);
00260         }
00261         else
00262         {
00263           f.store(filename, exp);
00264         }
00265       }
00266       break;
00267 
00268       case FileTypes::MZDATA:
00269       {
00270         MzDataFile f;
00271         f.getOptions() = options_;
00272         f.setLogType(log);
00273         if (!exp.getChromatograms().empty())
00274         {
00275           MSExperiment<PeakType> exp2 = exp;
00276           ChromatogramTools().convertChromatogramsToSpectra<MSExperiment<PeakType> >(exp2);
00277           f.store(filename, exp2);
00278         }
00279         else
00280         {
00281           f.store(filename, exp);
00282         }
00283       }
00284       break;
00285 
00286       default:
00287       {
00288         MzMLFile f;
00289         f.getOptions() = options_;
00290         f.setLogType(log);
00291         f.store(filename, exp);
00292       }
00293       break;
00294       }
00295     }
00296 
00309     template <class FeatureType>
00310     bool loadFeatures(const String & filename, FeatureMap<FeatureType> & map, FileTypes::Type force_type = FileTypes::UNKNOWN)
00311     {
00312       //determine file type
00313       FileTypes::Type type;
00314       if (force_type != FileTypes::UNKNOWN)
00315       {
00316         type = force_type;
00317       }
00318       else
00319       {
00320         try
00321         {
00322           type = getType(filename);
00323         }
00324         catch (Exception::FileNotFound)
00325         {
00326           return false;
00327         }
00328       }
00329 
00330       //load right file
00331       if (type == FileTypes::FEATUREXML)
00332       {
00333         FeatureXMLFile().load(filename, map);
00334       }
00335       else if (type == FileTypes::TSV)
00336       {
00337         MsInspectFile().load(filename, map);
00338       }
00339       else if (type == FileTypes::PEPLIST)
00340       {
00341         SpecArrayFile().load(filename, map);
00342       }
00343       else if (type == FileTypes::KROENIK)
00344       {
00345         KroenikFile().load(filename, map);
00346       }
00347       else
00348       {
00349         return false;
00350       }
00351 
00352       return true;
00353     }
00354 
00355 private:
00356     PeakFileOptions options_;
00357   };
00358 
00359 } //namespace
00360 
00361 #endif //OPENMS_FORMAT_FILEHANDLER_H

OpenMS / TOPP release 1.10.0 Documentation generated on Thu Mar 7 2013 09:42:39 using doxygen 1.7.1