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

DTAFile.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_DTAFILE_H
00036 #define OPENMS_FORMAT_DTAFILE_H
00037 
00038 #include <OpenMS/DATASTRUCTURES/String.h>
00039 #include <OpenMS/CONCEPT/Constants.h>
00040 #include <OpenMS/METADATA/Precursor.h>
00041 #include <OpenMS/SYSTEM/File.h>
00042 
00043 #include <fstream>
00044 #include <vector>
00045 
00046 namespace OpenMS
00047 {
00058   class OPENMS_DLLAPI DTAFile
00059   {
00060 public:
00062     DTAFile();
00064     virtual ~DTAFile();
00065 
00075     template <typename SpectrumType>
00076     void load(const String & filename, SpectrumType & spectrum)
00077     {
00078       std::ifstream is(filename.c_str());
00079       if (!is)
00080       {
00081         throw Exception::FileNotFound(__FILE__, __LINE__, __PRETTY_FUNCTION__, filename);
00082       }
00083 
00084       //  Delete old spectrum
00085       spectrum.clear(true);
00086 
00087       //temporary variables
00088       String line;
00089       std::vector<String> strings(2);
00090       typename SpectrumType::PeakType p;
00091       char delimiter;
00092 
00093       // line number counter
00094       Size line_number = 1;
00095 
00096       //read first line and store precursor m/z and charge
00097       getline(is, line, '\n');
00098       line.trim();
00099 
00100       //test which delimiter is used in the line
00101       if (line.has('\t'))
00102       {
00103         delimiter = '\t';
00104       }
00105       else
00106       {
00107         delimiter = ' ';
00108       }
00109 
00110       line.split(delimiter, strings);
00111       if (strings.size() != 2)
00112       {
00113         throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\" (got  " + String(strings.size()) + ", expected 2 entries)", filename);
00114       }
00115       Precursor precursor;
00116       DoubleReal mh_mass;
00117       Int charge;
00118       try
00119       {
00120         // by convention the first line holds: singly protonated peptide mass, charge state
00121         mh_mass = strings[0].toDouble();
00122         charge = strings[1].toInt();
00123       }
00124       catch (...)
00125       {
00126         throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\": not a float number.", filename);
00127       }
00128       if (charge != 0)
00129       {
00130         precursor.setMZ((mh_mass - Constants::PROTON_MASS_U) / charge + Constants::PROTON_MASS_U);
00131       }
00132       else
00133       {
00134         precursor.setMZ(mh_mass);
00135       }
00136       precursor.setCharge(charge);
00137       spectrum.getPrecursors().push_back(precursor);
00138 
00139       while (getline(is, line, '\n'))
00140       {
00141         ++line_number;
00142         line.trim();
00143         if (line.empty()) continue;
00144 
00145         //test which delimiter is used in the line
00146         if (line.has('\t'))
00147         {
00148           delimiter = '\t';
00149         }
00150         else
00151         {
00152           delimiter = ' ';
00153         }
00154 
00155         line.split(delimiter, strings);
00156         if (strings.size() != 2)
00157         {
00158           throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\" (got  " + String(strings.size()) + ", expected 2 entries)", filename);
00159         }
00160         try
00161         {
00162           //fill peak
00163           p.setPosition((typename SpectrumType::PeakType::PositionType)strings[0].toDouble());
00164           p.setIntensity((typename SpectrumType::PeakType::IntensityType)strings[1].toDouble());
00165         }
00166         catch (Exception::BaseException & /*e*/)
00167         {
00168           throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string("Bad data line (" + String(line_number) + "): \"") + line + "\": not a float number.", filename);
00169         }
00170         spectrum.push_back(p);
00171       }
00172 
00173       spectrum.setName(File::basename(filename));
00174       is.close();
00175     }
00176 
00185     template <typename SpectrumType>
00186     void store(const String & filename, const SpectrumType & spectrum) const
00187     {
00188       std::ofstream os(filename.c_str());
00189       if (!os)
00190       {
00191         throw Exception::UnableToCreateFile(__FILE__, __LINE__, __PRETTY_FUNCTION__, filename);
00192       }
00193       os.precision(writtenDigits<DoubleReal>());
00194 
00195       //write precursor information
00196       Precursor precursor;
00197       if (spectrum.getPrecursors().size() > 0)
00198       {
00199         precursor = spectrum.getPrecursors()[0];
00200       }
00201       if (spectrum.getPrecursors().size() > 1)
00202       {
00203         std::cerr << "Warning: The spectrum written to the DTA file '" << filename << "' has more than one precursor. The first precursor is used!" << "\n";
00204       }
00205       //unknown charge
00206       if (precursor.getCharge() == 0)
00207       {
00208         os << precursor.getMZ();
00209       }
00210       //known charge
00211       else
00212       {
00213         os << ((precursor.getMZ() - 1.0) * precursor.getCharge() + 1.0);
00214       }
00215       //charge
00216       os << " " << precursor.getCharge() << "\n";
00217 
00218       // Iterate over all peaks of the spectrum and
00219       // write one line for each peak of the spectrum.
00220       typename SpectrumType::ConstIterator it(spectrum.begin());
00221       for (; it != spectrum.end(); ++it)
00222       {
00223         // Write m/z and intensity.
00224         os << it->getPosition() << " " << it->getIntensity() << "\n";
00225       }
00226 
00227       // Done.
00228       os.close();
00229     }
00230 
00231   };
00232 } // namespace OpenMS
00233 
00234 #endif // OPENMS_FORMAT_DTAFILE_H

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