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

ChromatogramTools.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_KERNEL_CHROMATOGRAMTOOLS_H
00036 #define OPENMS_KERNEL_CHROMATOGRAMTOOLS_H
00037 
00038 #include <OpenMS/DATASTRUCTURES/Map.h>
00039 #include <OpenMS/CONCEPT/Types.h>
00040 #include <OpenMS/KERNEL/MSChromatogram.h>
00041 #include <OpenMS/KERNEL/MSExperiment.h>
00042 #include <OpenMS/KERNEL/RangeUtils.h>
00043 #include <OpenMS/CONCEPT/LogStream.h>
00044 
00045 namespace OpenMS
00046 {
00055   class ChromatogramTools
00056   {
00057 public:
00059 
00060 
00061     ChromatogramTools()
00062     {}
00063 
00065     ChromatogramTools(const ChromatogramTools & /*rhs*/)
00066     {}
00067 
00069     virtual ~ChromatogramTools()
00070     {}
00071 
00073 
00075 
00076 
00085     template <typename ExperimentType>
00086     void convertChromatogramsToSpectra(ExperimentType & exp)
00087     {
00088       for (std::vector<MSChromatogram<> >::const_iterator it = exp.getChromatograms().begin(); it != exp.getChromatograms().end(); ++it)
00089       {
00090         // for each peak add a new spectrum
00091         for (typename ExperimentType::ChromatogramType::const_iterator pit = it->begin(); pit != it->end(); ++pit)
00092         {
00093           typename ExperimentType::SpectrumType spec;
00094 
00095           // add precursor and product peaks to spectrum settings
00096           spec.getPrecursors().push_back(it->getPrecursor());
00097           spec.getProducts().push_back(it->getProduct());
00098           spec.setRT(pit->getRT());
00099           spec.setMSLevel(2);
00100           spec.setInstrumentSettings(it->getInstrumentSettings());
00101           spec.setAcquisitionInfo(it->getAcquisitionInfo());
00102           spec.setSourceFile(it->getSourceFile());
00103 
00104           // TODO implement others
00105           if (it->getChromatogramType() == ChromatogramSettings::SELECTED_REACTION_MONITORING_CHROMATOGRAM)
00106           {
00107             spec.getInstrumentSettings().setScanMode(InstrumentSettings::SRM);
00108           }
00109           if (it->getChromatogramType() == ChromatogramSettings::SELECTED_ION_MONITORING_CHROMATOGRAM)
00110           {
00111             spec.getInstrumentSettings().setScanMode(InstrumentSettings::SIM);
00112           }
00113 
00114           // new spec contains one peak, with product m/z and intensity
00115           typename ExperimentType::PeakType peak;
00116           peak.setMZ(it->getMZ());
00117           peak.setIntensity(pit->getIntensity());
00118           spec.push_back(peak);
00119           exp.push_back(spec);
00120         }
00121       }
00122 
00123       exp.setChromatograms(std::vector<MSChromatogram<> >());
00124     }
00125 
00137     template <typename ExperimentType>
00138     void convertSpectraToChromatograms(ExperimentType & exp, bool remove_spectra = false)
00139     {
00140       typedef typename ExperimentType::SpectrumType SpectrumType;
00141       Map<DoubleReal, Map<DoubleReal, std::vector<SpectrumType> > > chroms;
00142       for (typename ExperimentType::ConstIterator it = exp.begin(); it != exp.end(); ++it)
00143       {
00144         // TODO other types
00145         if (it->getInstrumentSettings().getScanMode() == InstrumentSettings::SRM)
00146         {
00147           // exactly one precursor and one product ion
00148           if (it->getPrecursors().size() == 1 && it->size() == 1)
00149           {
00150             chroms[it->getPrecursors().begin()->getMZ()][it->begin()->getMZ()].push_back(*it);
00151           }
00152           // Exactly one precursor and more than one product ion.
00153           // This is how some converters (e.g. ReAdW 4.0.2) store SRM data,
00154           // collecting all information from one precursor in a single
00155           // pseudo-spectrum
00156           else if (it->getPrecursors().size() == 1 && it->size() > 0)
00157           {
00158             for (Size peak_idx = 0; peak_idx < it->size(); peak_idx++)
00159             {
00160               // copy spectrum and delete all data, but keep metadata, then add single peak
00161               SpectrumType dummy = *it;
00162               dummy.clear(false);
00163               dummy.push_back((*it)[peak_idx]);
00164               chroms[it->getPrecursors().begin()->getMZ()][(*it)[peak_idx].getMZ()].push_back(dummy);
00165             }
00166           }
00167           else
00168           {
00169             LOG_WARN << "ChromatogramTools: need exactly one precursor (given " << it->getPrecursors().size() <<
00170             ") and one or more product ions (" << it->size() << "), skipping conversion of this spectrum to chromatogram." << std::endl;
00171           }
00172         }
00173         else
00174         {
00175           // This does not makes sense to warn here, because it would also warn on simple mass spectra...
00176           // TODO think what to to here
00177           //LOG_WARN << "ChromatogramTools: cannot convert other chromatrogram spectra types than 'Selected Reaction Monitoring', skipping conversion." << std::endl;
00178           //
00179         }
00180       }
00181 
00182       typename Map<DoubleReal, Map<DoubleReal, std::vector<SpectrumType> > >::const_iterator it1 = chroms.begin();
00183       for (; it1 != chroms.end(); ++it1)
00184       {
00185         typename Map<DoubleReal, std::vector<SpectrumType> >::const_iterator it2 = it1->second.begin();
00186         for (; it2 != it1->second.end(); ++it2)
00187         {
00188           typename ExperimentType::ChromatogramType chrom;
00189           chrom.setPrecursor(*it2->second.begin()->getPrecursors().begin());
00190           Product prod;
00191           prod.setMZ(it2->first);
00192           chrom.setProduct(prod);
00193           chrom.setInstrumentSettings(it2->second.begin()->getInstrumentSettings());
00194           chrom.setAcquisitionInfo(it2->second.begin()->getAcquisitionInfo());
00195           chrom.setSourceFile(it2->second.begin()->getSourceFile());
00196 
00197           typename std::vector<SpectrumType>::const_iterator it3 = it2->second.begin();
00198           for (; it3 != it2->second.end(); ++it3)
00199           {
00200             typename ExperimentType::ChromatogramType::PeakType p;
00201             p.setRT(it3->getRT());
00202             p.setIntensity(it3->begin()->getIntensity());
00203             chrom.push_back(p);
00204           }
00205 
00206           chrom.setNativeID("chromatogram=" + it2->second.begin()->getNativeID());               // TODO native id?
00207           chrom.setChromatogramType(ChromatogramSettings::SELECTED_REACTION_MONITORING_CHROMATOGRAM);
00208           exp.addChromatogram(chrom);
00209         }
00210       }
00211 
00212       if (remove_spectra)
00213       {
00214         exp.erase(remove_if(exp.begin(), exp.end(), HasScanMode<SpectrumType>(InstrumentSettings::SRM)), exp.end());
00215       }
00216     }
00217 
00219   };
00220 } // namespace OpenMS
00221 
00222 #endif // OPENMS_KERNEL_CHROMATOGRAMTOOLS_H

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