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

FeatureMap.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: Chris Bielow $
00032 // $Authors: Marc Sturm, Chris Bielow, Clemens Groepl $
00033 // --------------------------------------------------------------------------
00034 
00035 #ifndef OPENMS_KERNEL_FEATUREMAP_H
00036 #define OPENMS_KERNEL_FEATUREMAP_H
00037 
00038 #include <OpenMS/KERNEL/Feature.h>
00039 #include <OpenMS/METADATA/DocumentIdentifier.h>
00040 #include <OpenMS/METADATA/ProteinIdentification.h>
00041 #include <OpenMS/METADATA/DataProcessing.h>
00042 #include <OpenMS/KERNEL/RangeManager.h>
00043 #include <OpenMS/KERNEL/ComparatorUtils.h>
00044 #include <OpenMS/CONCEPT/UniqueIdIndexer.h>
00045 
00046 #include <algorithm>
00047 #include <vector>
00048 #include <exception>
00049 
00050 namespace OpenMS
00051 {
00052 
00067   template <typename FeatureT = Feature>
00068   class FeatureMap :
00069     public std::vector<FeatureT>,
00070     public RangeManager<2>,
00071     public DocumentIdentifier,
00072     public UniqueIdInterface,
00073     public UniqueIdIndexer<FeatureMap<FeatureT> >
00074   {
00075 public:
00080     typedef FeatureT FeatureType;
00081     typedef RangeManager<2> RangeManagerType;
00082     typedef std::vector<FeatureType> Base;
00083     typedef typename Base::iterator Iterator;
00084     typedef typename Base::const_iterator ConstIterator;
00085     typedef typename Base::reverse_iterator ReverseIterator;
00086     typedef typename Base::const_reverse_iterator ConstReverseIterator;
00087     typedef FeatureType & Reference;
00088     typedef const FeatureType & ConstReference;
00090 
00095 
00097     FeatureMap() :
00098       Base(),
00099       RangeManagerType(),
00100       DocumentIdentifier(),
00101       UniqueIdInterface(),
00102       UniqueIdIndexer<FeatureMap<FeatureT> >(),
00103       protein_identifications_(),
00104       unassigned_peptide_identifications_(),
00105       data_processing_()
00106     {}
00107 
00109     FeatureMap(const FeatureMap & source) :
00110       Base(source),
00111       RangeManagerType(source),
00112       DocumentIdentifier(source),
00113       UniqueIdInterface(source),
00114       UniqueIdIndexer<FeatureMap<FeatureT> >(source),
00115       protein_identifications_(source.protein_identifications_),
00116       unassigned_peptide_identifications_(source.unassigned_peptide_identifications_),
00117       data_processing_(source.data_processing_)
00118     {}
00119 
00121     virtual ~FeatureMap()
00122     {}
00124 
00126     FeatureMap & operator=(const FeatureMap & rhs)
00127     {
00128       if (&rhs == this) return *this;
00129 
00130       Base::operator=(rhs);
00131       RangeManagerType::operator=(rhs);
00132       DocumentIdentifier::operator=(rhs);
00133       UniqueIdInterface::operator=(rhs);
00134       protein_identifications_ = rhs.protein_identifications_;
00135       unassigned_peptide_identifications_ = rhs.unassigned_peptide_identifications_;
00136       data_processing_ = rhs.data_processing_;
00137 
00138       return *this;
00139     }
00140 
00142     bool operator==(const FeatureMap & rhs) const
00143     {
00144       return std::operator==(*this, rhs) &&
00145              RangeManagerType::operator==(rhs) &&
00146              DocumentIdentifier::operator==(rhs) &&
00147              UniqueIdInterface::operator==(rhs) &&
00148              protein_identifications_ == rhs.protein_identifications_ &&
00149              unassigned_peptide_identifications_ == rhs.unassigned_peptide_identifications_ &&
00150              data_processing_ == rhs.data_processing_;
00151     }
00152 
00154     bool operator!=(const FeatureMap & rhs) const
00155     {
00156       return !(operator==(rhs));
00157     }
00158 
00164     FeatureMap operator+(const FeatureMap & rhs) const
00165     {
00166       FeatureMap tmp(*this);
00167       tmp += rhs;
00168       return tmp;
00169     }
00170 
00183     FeatureMap & operator+=(const FeatureMap & rhs)
00184     {
00185       FeatureMap empty_map;
00186       // reset these:
00187       RangeManagerType::operator=(empty_map);
00188 
00189       if (!this->getIdentifier().empty() || !rhs.getIdentifier().empty()) LOG_INFO << "DocumentIdentifiers are lost during merge of FeatureMaps\n";
00190       DocumentIdentifier::operator=(empty_map);
00191 
00192       UniqueIdInterface::operator=(empty_map);
00193 
00194       // merge these:
00195       protein_identifications_.insert(protein_identifications_.end(), rhs.protein_identifications_.begin(), rhs.protein_identifications_.end());
00196       unassigned_peptide_identifications_.insert(unassigned_peptide_identifications_.end(), rhs.unassigned_peptide_identifications_.begin(), rhs.unassigned_peptide_identifications_.end());
00197       data_processing_.insert(data_processing_.end(), rhs.data_processing_.begin(), rhs.data_processing_.end());
00198 
00199       // append features:
00200       this->insert(this->end(), rhs.begin(), rhs.end());
00201 
00202       // todo: check for double entries
00203       // features, unassignedpeptides, proteins...
00204 
00205       // consistency
00206       try
00207       {
00208         UniqueIdIndexer<FeatureMap<FeatureT> >::updateUniqueIdToIndex();
00209       }
00210       catch (Exception::Postcondition /*&e*/) // assign new UID's for conflicting entries
00211       {
00212         Size replaced_uids =  UniqueIdIndexer<FeatureMap<FeatureT> >::resolveUniqueIdConflicts();
00213         LOG_INFO << "Replaced " << replaced_uids << " invalid uniqueID's\n";
00214       }
00215 
00216       return *this;
00217     }
00218 
00225 
00226     void sortByIntensity(bool reverse = false)
00227     {
00228       if (reverse)
00229       {
00230         std::sort(this->begin(), this->end(), reverseComparator(typename FeatureType::IntensityLess()));
00231       }
00232       else
00233       {
00234         std::sort(this->begin(), this->end(), typename FeatureType::IntensityLess());
00235       }
00236     }
00237 
00239     void sortByPosition()
00240     {
00241       std::sort(this->begin(), this->end(), typename FeatureType::PositionLess());
00242     }
00243 
00245     void sortByRT()
00246     {
00247       std::sort(this->begin(), this->end(), typename FeatureType::RTLess());
00248     }
00249 
00251     void sortByMZ()
00252     {
00253       std::sort(this->begin(), this->end(), typename FeatureType::MZLess());
00254     }
00255 
00257     void sortByOverallQuality(bool reverse = false)
00258     {
00259       if (reverse)
00260       {
00261         std::sort(this->begin(), this->end(), reverseComparator(typename FeatureType::OverallQualityLess()));
00262       }
00263       else
00264       {
00265         std::sort(this->begin(), this->end(), typename FeatureType::OverallQualityLess());
00266       }
00267     }
00268 
00270 
00271     // Docu in base class
00272     void updateRanges()
00273     {
00274       this->clearRanges();
00275       updateRanges_(this->begin(), this->end());
00276 
00277       //enlarge the range by the convex hull points
00278       for (Size i = 0; i < this->size(); ++i)
00279       {
00280         DBoundingBox<2> box = this->operator[](i).getConvexHull().getBoundingBox();
00281         if (!box.isEmpty())
00282         {
00283           //update RT
00284           if (box.minPosition()[Peak2D::RT] < this->pos_range_.minPosition()[Peak2D::RT])
00285           {
00286             this->pos_range_.setMinX(box.minPosition()[Peak2D::RT]);
00287           }
00288           if (box.maxPosition()[Peak2D::RT] > this->pos_range_.maxPosition()[Peak2D::RT])
00289           {
00290             this->pos_range_.setMaxX(box.maxPosition()[Peak2D::RT]);
00291           }
00292           //update m/z
00293           if (box.minPosition()[Peak2D::MZ] < this->pos_range_.minPosition()[Peak2D::MZ])
00294           {
00295             this->pos_range_.setMinY(box.minPosition()[Peak2D::MZ]);
00296           }
00297           if (box.maxPosition()[Peak2D::MZ] > this->pos_range_.maxPosition()[Peak2D::MZ])
00298           {
00299             this->pos_range_.setMaxY(box.maxPosition()[Peak2D::MZ]);
00300           }
00301         }
00302       }
00303     }
00304 
00306     void swap(FeatureMap & from)
00307     {
00308       FeatureMap tmp;
00309 
00310       // swap the actual features
00311       Base::swap(from);
00312 
00313       // swap range information
00314       tmp.RangeManagerType::operator=(* this);
00315       this->RangeManagerType::operator=(from);
00316       from.RangeManagerType::operator=(tmp);
00317 
00318       // swap DocumentIdentifier
00319       DocumentIdentifier::swap(from);
00320 
00321       // swap unique id
00322       UniqueIdInterface::swap(from);
00323 
00324       // swap unique id index
00325       UniqueIdIndexer<FeatureMap<FeatureT> >::swap(from);
00326 
00327       // swap the remaining members
00328       protein_identifications_.swap(from.protein_identifications_);
00329       unassigned_peptide_identifications_.swap(from.unassigned_peptide_identifications_);
00330       data_processing_.swap(from.data_processing_);
00331     }
00332 
00334     const std::vector<ProteinIdentification> & getProteinIdentifications() const
00335     {
00336       return protein_identifications_;
00337     }
00338 
00340     std::vector<ProteinIdentification> & getProteinIdentifications()
00341     {
00342       return protein_identifications_;
00343     }
00344 
00346     void setProteinIdentifications(const std::vector<ProteinIdentification> & protein_identifications)
00347     {
00348       protein_identifications_ = protein_identifications;
00349     }
00350 
00352     const std::vector<PeptideIdentification> & getUnassignedPeptideIdentifications() const
00353     {
00354       return unassigned_peptide_identifications_;
00355     }
00356 
00358     std::vector<PeptideIdentification> & getUnassignedPeptideIdentifications()
00359     {
00360       return unassigned_peptide_identifications_;
00361     }
00362 
00364     void setUnassignedPeptideIdentifications(const std::vector<PeptideIdentification> & unassigned_peptide_identifications)
00365     {
00366       unassigned_peptide_identifications_ = unassigned_peptide_identifications;
00367     }
00368 
00370     const std::vector<DataProcessing> & getDataProcessing() const
00371     {
00372       return data_processing_;
00373     }
00374 
00376     std::vector<DataProcessing> & getDataProcessing()
00377     {
00378       return data_processing_;
00379     }
00380 
00382     void setDataProcessing(const std::vector<DataProcessing> & processing_method)
00383     {
00384       data_processing_ = processing_method;
00385     }
00386 
00392     void clear(bool clear_meta_data = true)
00393     {
00394       Base::clear();
00395 
00396       if (clear_meta_data)
00397       {
00398         clearRanges();
00399         this->DocumentIdentifier::operator=(DocumentIdentifier());             // no "clear" method
00400         clearUniqueId();
00401         protein_identifications_.clear();
00402         unassigned_peptide_identifications_.clear();
00403         data_processing_.clear();
00404       }
00405     }
00406 
00419     template <typename Type>
00420     Size applyMemberFunction(Size (Type::* member_function)())
00421     {
00422       Size assignments = 0;
00423       assignments += ((*this).*member_function)();
00424       for (Iterator iter = this->begin(); iter != this->end(); ++iter)
00425       {
00426         assignments += iter->applyMemberFunction(member_function);
00427       }
00428       return assignments;
00429     }
00430 
00432     template <typename Type>
00433     Size applyMemberFunction(Size (Type::* member_function)() const) const
00434     {
00435       Size assignments = 0;
00436       assignments += ((*this).*member_function)();
00437       for (ConstIterator iter = this->begin(); iter != this->end(); ++iter)
00438       {
00439         assignments += iter->applyMemberFunction(member_function);
00440       }
00441       return assignments;
00442     }
00443 
00444 protected:
00445 
00447     std::vector<ProteinIdentification> protein_identifications_;
00448 
00450     std::vector<PeptideIdentification> unassigned_peptide_identifications_;
00451 
00453     std::vector<DataProcessing> data_processing_;
00454   };
00455 
00457   template <typename FeatureType>
00458   std::ostream & operator<<(std::ostream & os, const FeatureMap<FeatureType> & map)
00459   {
00460     os << "# -- DFEATUREMAP BEGIN --" << "\n";
00461     os << "# POS \tINTENS\tOVALLQ\tCHARGE\tUniqueID" << "\n";
00462     for (typename FeatureMap<FeatureType>::const_iterator iter = map.begin(); iter != map.end(); iter++)
00463     {
00464       os << iter->getPosition() << '\t'
00465       << iter->getIntensity() << '\t'
00466       << iter->getOverallQuality() << '\t'
00467       << iter->getCharge() << '\t'
00468       << iter->getUniqueId() << "\n";
00469     }
00470     os << "# -- DFEATUREMAP END --" << std::endl;
00471     return os;
00472   }
00473 
00474 } // namespace OpenMS
00475 
00476 #endif // OPENMS_KERNEL_DFEATUREMAP_H

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