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

MS2File.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: Andreas Bertsch $
00033 // --------------------------------------------------------------------------
00034 
00035 #ifndef OPENMS_FORMAT_MS2FILE_H
00036 #define OPENMS_FORMAT_MS2FILE_H
00037 
00038 #include <OpenMS/DATASTRUCTURES/String.h>
00039 #include <OpenMS/CONCEPT/Exception.h>
00040 #include <OpenMS/SYSTEM/File.h>
00041 #include <OpenMS/CONCEPT/ProgressLogger.h>
00042 #include <OpenMS/METADATA/DocumentIdentifier.h>
00043 
00044 #include <vector>
00045 #include <fstream>
00046 
00047 namespace OpenMS
00048 {
00065   class OPENMS_DLLAPI MS2File :
00066     public ProgressLogger
00067   {
00068 public:
00069 
00071     MS2File();
00072 
00074     virtual ~MS2File();
00075 
00076     template <typename MapType>
00077     void load(const String & filename, MapType & exp)
00078     {
00079       //startProgress(0,0,"loading DTA2D file");
00080 
00081       if (!File::exists(filename))
00082       {
00083         throw Exception::FileNotFound(__FILE__, __LINE__, __PRETTY_FUNCTION__, filename);
00084       }
00085       if (!File::readable(filename))
00086       {
00087         throw Exception::FileNotReadable(__FILE__, __LINE__, __PRETTY_FUNCTION__, filename);
00088       }
00089 
00090       exp.reset();
00091 
00092       //set DocumentIdentifier
00093       exp.setLoadedFileType(filename);
00094       exp.setLoadedFilePath(filename);
00095 
00096       std::ifstream in(filename.c_str());
00097 
00098       UInt spectrum_number = 0;
00099       typename MapType::SpectrumType spec;
00100       typename MapType::SpectrumType::PeakType p;
00101 
00102       String line;
00103       bool first_spec(true);
00104 
00105       // line number counter
00106       Size line_number = 0;
00107 
00108       while (getline(in, line, '\n'))
00109       {
00110         ++line_number;
00111 
00112         line.trim();
00113         if (line.empty()) continue;
00114 
00115         // header
00116         if (line[0] == 'H')
00117         {
00118           continue;
00119         }
00120 
00121         // scan
00122         if (line[0] == 'S')
00123         {
00124           if (!first_spec)
00125           {
00126             spec.setMSLevel(2);
00127             spec.setNativeID(String("index=") + (spectrum_number++));
00128             exp.push_back(spec);
00129           }
00130           else
00131           {
00132             first_spec = false;
00133           }
00134           spec.clear(true);
00135           line.simplify();
00136           std::vector<String> split;
00137           line.split(' ', split);
00138           if (split.size() != 4)
00139           {
00140             throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "line (" + String(line_number) + ") '" + line  + "' should contain four values, got " + String(split.size()) + "!", "");
00141           }
00142           spec.getPrecursors().resize(1);
00143           spec.getPrecursors()[0].setMZ(split[3].toDouble());
00144           continue;
00145         }
00146 
00147         // charge-independent analysis
00148         if (line[0] == 'I')
00149         {
00150           continue;
00151         }
00152 
00153         // charge specification
00154         if (line[0] == 'Z')
00155         {
00156           continue;
00157         }
00158 
00159         // charge-dependent analysis
00160         if (line[0] == 'D')
00161         {
00162           continue;
00163         }
00164 
00165         // yet another peak, hopefully
00166         line.simplify();
00167         std::vector<String> split;
00168         line.split(' ', split);
00169         if (split.size() != 2)
00170         {
00171           throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "line (" + String(line_number) + ") '" + line  + "' should contain two values, got " + String(split.size()) + "!", "");
00172         }
00173 
00174         try
00175         {
00176           p.setPosition(split[0].toDouble());
00177           p.setIntensity(split[1].toFloat());
00178         }
00179         catch (Exception::ConversionError /*&e*/)
00180         {
00181           throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "ConversionError: line (" + String(line_number) + ") '" + line  + "' does not contain two numbers!", "");
00182         }
00183         spec.push_back(p);
00184       }
00185 
00186       if (!first_spec)
00187       {
00188         spec.setMSLevel(2);
00189         spec.setNativeID(String("index=") + (spectrum_number++));
00190         exp.push_back(spec);
00191       }
00192     }
00193 
00194     /*
00195     template <typename MapType> void store(const String& filename, MapType& map)
00196     {
00197 
00198     }
00199     */
00200 
00201 protected:
00202 
00203   };
00204 
00205 } // namespace OpenMS
00206 
00207 #endif // OPENMS_FORMAT_MS2FILE_H

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