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

DSpectrum.h (Maintainer: Marc Sturm)

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // --------------------------------------------------------------------------
00005 //                   OpenMS Mass Spectrometry Framework
00006 // --------------------------------------------------------------------------
00007 //  Copyright (C) 2003-2008 -- Oliver Kohlbacher, Knut Reinert
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Lesser General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2.1 of the License, or (at your option) any later version.
00013 //
00014 //  This library is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //  Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with this library; if not, write to the Free Software
00021 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // --------------------------------------------------------------------------
00024 // $Maintainer: Marc Sturm $
00025 // --------------------------------------------------------------------------
00026 
00027 #ifndef OPENMS_KERNEL_DSPECTRUM_H
00028 #define OPENMS_KERNEL_DSPECTRUM_H
00029 
00030 #include <OpenMS/KERNEL/DPeakArray.h>
00031 #include <OpenMS/KERNEL/DPeak.h>
00032 #include <OpenMS/METADATA/MetaInfoInterface.h>
00033 #include <OpenMS/KERNEL/RangeManager.h>
00034 
00035 #include <gsl/gsl_randist.h>
00036 
00037 
00038 #include <list>
00039 #include <cmath>
00040 
00041 namespace OpenMS
00042 {
00043 
00044   class Peak1D;
00045 
00046   namespace Internal
00047   {
00068     template < UInt D >
00069     class PrecursorPeak : public DPeak<D>
00070     {
00071 
00073       typedef DPeak<D> Base;
00074 
00075      public:
00076 
00078       typedef Int ChargeType;
00079 
00081       enum
00082         {
00083           DIMENSION = D
00084         };
00085 
00087       PrecursorPeak()
00088         : Base(),
00089           charge_(0)
00090       {
00091       }
00092 
00094       PrecursorPeak(const PrecursorPeak& rhs)
00095         : Base(rhs),
00096           charge_(rhs.charge_)
00097       {
00098       }
00099 
00101       PrecursorPeak & operator=(const PrecursorPeak& rhs)
00102       {
00103         Base::operator=(rhs);
00104         charge_=rhs.charge_;
00105         return *this;
00106       }
00107 
00109       ~PrecursorPeak()
00110       {
00111       }
00112 
00114       ChargeType const & getCharge() const
00115       {
00116         return charge_;
00117       }
00118 
00120       void setCharge( ChargeType charge )
00121       {
00122         charge_ = charge;
00123         return;
00124       }
00125 
00126      protected:
00127 
00128       ChargeType charge_;
00129 
00130     };
00131 
00132   } // namespace Internal
00133 
00152   template < typename ContainerT = DPeakArray<Peak1D> >
00153   class DSpectrum :
00154     public MetaInfoInterface,
00155     public RangeManager<ContainerT::value_type::DIMENSION>
00156   {
00157    public:
00158 
00161 
00163     typedef ContainerT ContainerType;
00164 
00166     typedef typename ContainerType::value_type PeakType;
00167 
00169     enum
00170       {
00171         DIMENSION = PeakType::DIMENSION
00172       };
00173 
00175     typedef typename PeakType::CoordinateType CoordinateType;
00176 
00178     typedef Internal::PrecursorPeak<DIMENSION> PrecursorPeakType;
00179 
00181     typedef RangeManager<DIMENSION> RangeManagerType;
00182 
00183     // allow c'tors of other template instances to access private members
00184     template < typename ContainerT_ > friend class DSpectrum;
00185     
00187 
00190     typedef typename ContainerType::iterator iterator;
00191     typedef typename ContainerType::const_iterator const_iterator;
00192     typedef typename ContainerType::reverse_iterator reverse_iterator;
00193     typedef typename ContainerType::const_reverse_iterator const_reverse_iterator;
00194     typedef typename ContainerType::value_type value_type;
00195     typedef typename ContainerType::reference reference;
00196     typedef typename ContainerType::const_reference const_reference;
00197     typedef typename ContainerType::pointer pointer;
00198     typedef typename ContainerType::difference_type difference_type;
00199     typedef typename ContainerType::size_type size_type;
00201 
00204 
00205     typedef typename ContainerType::iterator Iterator;
00207     typedef typename ContainerType::const_iterator ConstIterator;
00209     typedef typename ContainerType::reverse_iterator ReverseIterator;
00211     typedef typename ContainerType::const_reverse_iterator ConstReverseIterator;
00213 
00216 
00218     DSpectrum()
00219       : MetaInfoInterface(),
00220         RangeManagerType(),
00221         container_(),
00222         precursor_peak_(),
00223         retention_time_(-1), // warning: don't change this !! Otherwise MSExperimentExtern might not behave as expected !!
00224         ms_level_(1),
00225         name_()
00226     {
00227     }
00228 
00230     DSpectrum(const typename ContainerType::AllocType& alloc)
00231       : MetaInfoInterface(),
00232         RangeManagerType(),
00233         container_(alloc),
00234         precursor_peak_(),
00235         retention_time_(-1), // warning: don't change this !! Otherwise MSExperimentExtern might not behave as expected !!
00236         ms_level_(1),
00237         name_()
00238     {
00239     }    
00240     
00242     DSpectrum(const DSpectrum& rhs)
00243       : MetaInfoInterface(rhs),
00244         RangeManagerType(rhs),
00245         container_(rhs.container_),
00246         precursor_peak_(rhs.precursor_peak_),
00247         retention_time_(rhs.retention_time_),
00248         ms_level_(rhs.ms_level_),
00249         name_(rhs.name_)
00250     {
00251     }
00252 
00254     template < template < typename, typename > class ContainerT2, typename AllocT2>
00255     DSpectrum(const DSpectrum< ContainerT2 <PeakType, AllocT2 > >& rhs)
00256       : MetaInfoInterface(rhs),
00257         RangeManagerType(rhs),
00258         container_(rhs.container_),
00259         precursor_peak_(rhs.precursor_peak_),
00260         retention_time_(rhs.retention_time_),
00261         ms_level_(rhs.ms_level_),
00262         name_(rhs.name_)
00263     {
00264     }    
00265 
00267     template < template < typename, typename > class ContainerT2, typename AllocT2, typename AllocT>
00268     DSpectrum(const DSpectrum< ContainerT2 <PeakType, AllocT2 > >& rhs, const AllocT& alloc)
00269       : MetaInfoInterface(rhs),
00270         RangeManagerType(rhs),
00271         container_(rhs.container_, alloc),
00272         precursor_peak_(rhs.precursor_peak_),
00273         retention_time_(rhs.retention_time_),
00274         ms_level_(rhs.ms_level_),
00275         name_(rhs.name_)
00276     {
00277     }       
00278         
00280     inline ~DSpectrum()
00281     {
00282     }
00284 
00286     DSpectrum& operator = (const DSpectrum& rhs)
00287     {
00288       if (this==&rhs) return *this;
00289 
00290       MetaInfoInterface::operator=(rhs);
00291       RangeManagerType::operator=(rhs);
00292       container_ = rhs.container_;
00293       precursor_peak_ = rhs.precursor_peak_;
00294       retention_time_ = rhs.retention_time_;
00295       ms_level_ = rhs.ms_level_;
00296       name_ = rhs.name_;
00297       return *this;
00298     }
00299 
00301     template < template < typename, typename > class ContainerT2, typename AllocT>
00302     DSpectrum& operator = (const DSpectrum< ContainerT2 <PeakType, AllocT > >& rhs)
00303     {
00304       //if (this==&rhs) return *this;
00305 
00306       MetaInfoInterface::operator=(rhs);
00307       RangeManagerType::operator=(rhs);
00308       container_ = rhs.container_;
00309       precursor_peak_ = rhs.precursor_peak_;
00310       retention_time_ = rhs.retention_time_;
00311       ms_level_ = rhs.ms_level_;
00312       name_ = rhs.name_;
00313       return *this;
00314     }
00315     
00317     bool operator == (const DSpectrum& rhs) const
00318     {
00319       return
00320         MetaInfoInterface::operator==(rhs) &&
00321         RangeManagerType::operator==(rhs) &&
00322         container_ == rhs.container_ &&
00323         precursor_peak_ == rhs.precursor_peak_ &&
00324         retention_time_ == rhs.retention_time_ &&
00325         ms_level_ == rhs.ms_level_
00326         ;
00327       //name_ == rhs.name_  // the name can differ => do not test it
00328     }
00329 
00331     bool operator != (const DSpectrum& rhs) const
00332     {
00333       return !(operator==(rhs));
00334     }
00335 
00338 
00339     inline const ContainerType& getContainer() const
00340     {
00341       return container_;
00342     }
00344     inline ContainerType& getContainer()
00345     {
00346       return container_;
00347     }
00349     inline void setContainer(const ContainerType& container)
00350     {
00351       container_ = container;
00352     }
00353 
00355     inline ConstIterator begin() const
00356     {
00357       return container_.begin();
00358     }
00360     inline ConstIterator end() const
00361     {
00362       return container_.end();
00363     }
00364 
00366     inline Iterator begin()
00367     {
00368       return container_.begin();
00369     }
00371     inline Iterator end()
00372     {
00373       return container_.end();
00374     }
00375 
00377     reference operator[] (size_type n)
00378     {
00379       return container_[n];
00380     }
00381 
00383     const_reference operator[] (size_type n) const
00384     {
00385       return container_[n];
00386     }
00387 
00389     inline size_type max_size() const
00390     {
00391       return container_.max_size();
00392     }
00394     inline UInt size() const
00395     {
00396       return container_.size();
00397     }
00399     inline bool empty() const
00400     {
00401       return container_.empty();
00402     }
00403 
00405     inline void swap(ContainerType& rhs)
00406     {
00407       container_.swap(rhs);
00408     }
00409 
00411     inline bool operator<(const DSpectrum& rhs)
00412     {
00413       return container_<rhs.getContainer();
00414     }
00415 
00417     inline bool operator>(const DSpectrum& rhs)
00418     {
00419       return container_>rhs.getContainer();
00420     }
00421 
00423     inline bool operator<=(const DSpectrum& rhs)
00424     {
00425       return container_<=rhs.getContainer();
00426     }
00427 
00429     inline bool operator>=(const DSpectrum& rhs)
00430     {
00431       return container_>=rhs.getContainer();
00432     }
00433 
00435     inline ReverseIterator rbegin()
00436     {
00437       return container_.rbegin();
00438     }
00439 
00441     inline ConstReverseIterator rbegin() const
00442     {
00443       return container_.rbegin();
00444     }
00445 
00447     inline ReverseIterator rend()
00448     {
00449       return container_.rend();
00450     }
00451 
00453     inline ConstReverseIterator rend() const
00454     {
00455       return container_.rend();
00456     }
00457 
00459     inline Iterator insert( Iterator loc, const value_type& val )
00460     {
00461       return container_.insert(loc, val);
00462     }
00463 
00465     inline void insert( iterator loc, size_type num, const value_type& val )
00466     {
00467       container_.insert(loc, num, val);
00468     }
00469 
00471     template<class InputIterator> void insert( iterator loc, InputIterator start, InputIterator end )
00472     {
00473       container_.insert(loc, start, end);
00474     }
00475 
00477     inline Iterator erase( iterator loc )
00478     {
00479       return container_.erase(loc);
00480     }
00481 
00483     inline Iterator erase( iterator start, iterator end )
00484     {
00485       return container_.erase(start, end);
00486     }
00487 
00489     inline value_type& front()
00490     {
00491       return container_.front();
00492     }
00493 
00495     inline const value_type& front() const
00496     {
00497       return container_.front();
00498     }
00499 
00501     inline value_type& back()
00502     {
00503       return container_.back();
00504     }
00505 
00507     inline const value_type& back() const
00508     {
00509       return container_.back();
00510     }
00511 
00513     inline void pop_back()
00514     {
00515       container_.pop_back();
00516     }
00517 
00519     inline void push_back( const value_type& val )
00520     {
00521       container_.push_back(val);
00522     }
00523 
00525     inline void assign( size_type num, const value_type& val )
00526     {
00527       container_.assign(num, val);
00528     }
00529 
00531     template<class InputIterator> void assign( InputIterator start, InputIterator end )
00532     {
00533       container_.assign(start, end);
00534     }
00535 
00537     inline void clear()
00538     {
00539       container_.clear();
00540     }
00541 
00543     inline void resize( size_type num, const value_type& val = value_type() )
00544     {
00545       container_.resize(num, val);
00546     }
00547 
00549 
00550     // Docu in base class
00551     virtual void updateRanges()
00552     {
00553       this->clearRanges();
00554       updateRanges_(container_.begin(), container_.end());
00555     }
00556 
00559 
00560     const PrecursorPeakType& getPrecursorPeak() const
00561     {
00562       return precursor_peak_;
00563     }
00564 
00566     PrecursorPeakType& getPrecursorPeak()
00567     {
00568       return precursor_peak_;
00569     }
00570 
00572     void setPrecursorPeak(const PrecursorPeakType& peak)
00573     {
00574       precursor_peak_ = peak;
00575     }
00576 
00578     CoordinateType getRT() const
00579     {
00580       return retention_time_;
00581     }
00582 
00583 
00588     void setRT(CoordinateType rt)
00589     {
00590       retention_time_= rt;
00591     }
00592 
00598     UInt getMSLevel() const
00599     {
00600       return ms_level_;
00601     }
00602 
00604     void setMSLevel(UInt ms_level)
00605     {
00606       ms_level_ = ms_level;
00607     }
00608 
00610     String getName() const
00611     {
00612       return name_;
00613     }
00614 
00616     void setName(const String& name)
00617     {
00618       name_ = name;
00619     }
00620 
00622 
00623 
00625 
00626 
00634     UInt findNearest(CoordinateType mz) const throw (Exception::Precondition)
00635     {
00636       //no peak => no search
00637       if (size()==0) throw Exception::Precondition(__FILE__,__LINE__,__PRETTY_FUNCTION__,"There must be at least one peak to determine the nearest peak!");
00638       
00639       //searh for position for inserting
00640       ConstIterator it = MZBegin(mz);
00641       //border cases
00642       if (it==begin()) return 0;
00643       if (it==end()) return size()-1;
00644       //the peak before or the current peak are closest
00645       ConstIterator it2 = it;
00646       --it2;
00647       if (std::fabs(it->getMZ()-mz)<std::fabs(it2->getMZ()-mz))
00648       {
00649         return it - begin();
00650       }
00651       else
00652       {
00653         return it2 - begin();
00654       }
00655     }
00661     Iterator MZBegin(CoordinateType mz)
00662     {
00663       PeakType p;
00664       p.setPosition(mz);
00665       return lower_bound(container_.begin(), container_.end(), p, typename PeakType::PositionLess());
00666     }
00672     Iterator MZEnd(CoordinateType mz)
00673     {
00674       PeakType p;
00675       p.setPosition(mz);
00676       return upper_bound(container_.begin(), container_.end(), p, typename PeakType::PositionLess());
00677     }
00683     ConstIterator MZBegin(CoordinateType mz) const
00684     {
00685       PeakType p;
00686       p.setPosition(mz);
00687       return lower_bound(container_.begin(), container_.end(), p, typename PeakType::PositionLess());
00688     }
00694     ConstIterator MZEnd(CoordinateType mz) const
00695     {
00696       PeakType p;
00697       p.setPosition(mz);
00698       return upper_bound(container_.begin(), container_.end(), p, typename PeakType::PositionLess());
00699     }
00701   
00702   protected:
00703 
00705     ContainerType   container_;
00706 
00708     PrecursorPeakType precursor_peak_;
00709 
00711     CoordinateType retention_time_;
00712 
00714     UInt ms_level_;
00715 
00717     String name_;
00718   };
00719 
00721   template <typename Container>
00722   std::ostream& operator << (std::ostream& os, const DSpectrum<Container>& rhs)
00723   {
00724     os << "-- DSpectrum BEGIN --"<<std::endl;
00725     os << "MS-LEVEL:" <<rhs.getMSLevel() << std::endl;
00726     os << "RT:" <<rhs.getRT() << std::endl;
00727     os << "NAME:" <<rhs.getName() << std::endl;
00728     os << "\n" << rhs.getContainer() << std::endl;
00729     os << "-- DSpectrum END --"<<std::endl;
00730 
00731     return os;
00732   }
00733 
00734 } // namespace OpenMS
00735 
00736 
00737 
00738 #endif // OPENMS_KERNEL_SPECTRUM_H

Generated Tue Apr 1 15:36:33 2008 -- using doxygen 1.5.4 OpenMS / TOPP 1.1