| 1 | /*
 | 
|---|
| 2 |  * HomologyContainer.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Sep 22, 2012
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #ifndef HOMOLOGYCONTAINER_HPP_
 | 
|---|
| 9 | #define HOMOLOGYCONTAINER_HPP_
 | 
|---|
| 10 | 
 | 
|---|
| 11 | 
 | 
|---|
| 12 | // include config.h
 | 
|---|
| 13 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 14 | #include <config.h>
 | 
|---|
| 15 | #endif
 | 
|---|
| 16 | 
 | 
|---|
| 17 | #include <boost/serialization/export.hpp>
 | 
|---|
| 18 | #include <boost/serialization/map.hpp>
 | 
|---|
| 19 | #include <boost/serialization/vector.hpp>
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #include <iosfwd>
 | 
|---|
| 22 | #include <map>
 | 
|---|
| 23 | #include <vector>
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #include "Fragmentation/Homology/HomologyGraph.hpp"
 | 
|---|
| 26 | #include "Fragmentation/SetValues/Fragment.hpp"
 | 
|---|
| 27 | 
 | 
|---|
| 28 | class HomologyContainerTest;
 | 
|---|
| 29 | 
 | 
|---|
| 30 | /** This class takes all KeySets in a Graph, checks for those that homologues
 | 
|---|
| 31 |  * of one another and places them together.
 | 
|---|
| 32 |  *
 | 
|---|
| 33 |  * This is meant as a storage for key, value pairs, where the key is the KeySet
 | 
|---|
| 34 |  * and the value is the energy associated to the fragment this keyset
 | 
|---|
| 35 |  * represents.
 | 
|---|
| 36 |  * Afterwards this can then be used as training data for a high-dimensional
 | 
|---|
| 37 |  * approximation to the Born-Oppenheimer-surface decomposed into lower-
 | 
|---|
| 38 |  * dimensional terms in an ANOVA-like fashion.
 | 
|---|
| 39 |  *
 | 
|---|
| 40 |  */
 | 
|---|
| 41 | class HomologyContainer
 | 
|---|
| 42 | {
 | 
|---|
| 43 |   //!> grant access to output operator
 | 
|---|
| 44 |   friend std::ostream& operator<<(std::ostream &out, const HomologyContainer &container);
 | 
|---|
| 45 |   //!> grant unit test access
 | 
|---|
| 46 |   friend class HomologyContainerTest;
 | 
|---|
| 47 | public:
 | 
|---|
| 48 |   typedef double energy_t;
 | 
|---|
| 49 |   typedef std::pair<Fragment, energy_t> value_t;
 | 
|---|
| 50 |   typedef std::multimap< HomologyGraph, value_t> container_t;
 | 
|---|
| 51 |   typedef container_t::const_iterator const_iterator;
 | 
|---|
| 52 |   typedef std::pair< const_iterator, const_iterator> range_t;
 | 
|---|
| 53 | public:
 | 
|---|
| 54 |   /** Default Constructor of class HomologyContainer.
 | 
|---|
| 55 |    *
 | 
|---|
| 56 |    */
 | 
|---|
| 57 |   HomologyContainer() {}
 | 
|---|
| 58 | 
 | 
|---|
| 59 |   /** Constructor of class HomologyContainer.
 | 
|---|
| 60 |    *
 | 
|---|
| 61 |    * @param values values with with to initially fill the container
 | 
|---|
| 62 |    */
 | 
|---|
| 63 |   HomologyContainer(const container_t &values) :
 | 
|---|
| 64 |     container(values)
 | 
|---|
| 65 |   {}
 | 
|---|
| 66 |   /** Destructor of class HomologyContainer.
 | 
|---|
| 67 |    *
 | 
|---|
| 68 |    */
 | 
|---|
| 69 |   ~HomologyContainer() {}
 | 
|---|
| 70 | 
 | 
|---|
| 71 |   /** Equality comparator.
 | 
|---|
| 72 |    *
 | 
|---|
| 73 |    * Sadly, the insertion order of a std::multimap's values is not guaranteed
 | 
|---|
| 74 |    * by the standard and boost::serialization does not heed the ordering of
 | 
|---|
| 75 |    * the values associated to the same key. Hence, we implement a weaker
 | 
|---|
| 76 |    * comparator for this class in order for the unit test to pass as we don't
 | 
|---|
| 77 |    * actuallty care about the order of the homologous fragments.
 | 
|---|
| 78 |    *
 | 
|---|
| 79 |    * @param other instance to compare to
 | 
|---|
| 80 |    * @return true - each container contains all elements of the other
 | 
|---|
| 81 |    */
 | 
|---|
| 82 |   bool operator==(const HomologyContainer &other) const {
 | 
|---|
| 83 |     return ((*this >= other) && (other >= *this));
 | 
|---|
| 84 |   }
 | 
|---|
| 85 |   bool operator!=(const HomologyContainer& other) const {
 | 
|---|
| 86 |     return !(*this == other);
 | 
|---|
| 87 |   }
 | 
|---|
| 88 | 
 | 
|---|
| 89 |   /** Greater equal comparator, i.e. subset comparator
 | 
|---|
| 90 |    *
 | 
|---|
| 91 |    * @param other container to check if it's subset
 | 
|---|
| 92 |    * @return true - \a other is a subset of this
 | 
|---|
| 93 |    */
 | 
|---|
| 94 |   bool operator>=(const HomologyContainer &other) const;
 | 
|---|
| 95 | 
 | 
|---|
| 96 |   /** Inserter for more graphs along with values.
 | 
|---|
| 97 |    *
 | 
|---|
| 98 |    * @param values graph and values to insert
 | 
|---|
| 99 |    */
 | 
|---|
| 100 |   void insert(const container_t &values) {
 | 
|---|
| 101 |     container.insert(values.begin(), values.end());
 | 
|---|
| 102 |   }
 | 
|---|
| 103 | 
 | 
|---|
| 104 |   /** Returns iterator range with all contained graphs homologous to the given \a graph.
 | 
|---|
| 105 |    *
 | 
|---|
| 106 |    * @param graph graph to match
 | 
|---|
| 107 |    * @return iterator range with all matches
 | 
|---|
| 108 |    */
 | 
|---|
| 109 |   range_t getHomologousGraphs(const HomologyGraph &graph) {
 | 
|---|
| 110 |     return container.equal_range(graph);
 | 
|---|
| 111 |   }
 | 
|---|
| 112 | 
 | 
|---|
| 113 |   /** Getter for constant iterator to begin of homologous graph container.
 | 
|---|
| 114 |    *
 | 
|---|
| 115 |    * @return begin constant iterator
 | 
|---|
| 116 |    */
 | 
|---|
| 117 |   const_iterator begin() const {
 | 
|---|
| 118 |     return container.begin();
 | 
|---|
| 119 |   }
 | 
|---|
| 120 | 
 | 
|---|
| 121 |   /** Getter for constant iterator to past end of homologous graph container.
 | 
|---|
| 122 |    *
 | 
|---|
| 123 |    * @return past end constant iterator
 | 
|---|
| 124 |    */
 | 
|---|
| 125 |   const_iterator end() const {
 | 
|---|
| 126 |     return container.end();
 | 
|---|
| 127 |   }
 | 
|---|
| 128 | 
 | 
|---|
| 129 | private:
 | 
|---|
| 130 |   //!> multimap containing all homologous graph under same key but each with its value
 | 
|---|
| 131 |   container_t container;
 | 
|---|
| 132 | 
 | 
|---|
| 133 | private:
 | 
|---|
| 134 |   friend class boost::serialization::access;
 | 
|---|
| 135 |   // serialization
 | 
|---|
| 136 |   template <typename Archive>
 | 
|---|
| 137 |   void serialize(Archive& ar, const unsigned int version)
 | 
|---|
| 138 |   {
 | 
|---|
| 139 |     ar & container;
 | 
|---|
| 140 |   }
 | 
|---|
| 141 | };
 | 
|---|
| 142 | 
 | 
|---|
| 143 | /** Output operator for HomologyContainer.
 | 
|---|
| 144 |  *
 | 
|---|
| 145 |  * \param out output stream
 | 
|---|
| 146 |  * \param container container to print
 | 
|---|
| 147 |  * \return output stream for concatenation
 | 
|---|
| 148 |  */
 | 
|---|
| 149 | std::ostream& operator<<(std::ostream &out, const HomologyContainer &container);
 | 
|---|
| 150 | 
 | 
|---|
| 151 | // we need to give this class a unique key for serialization
 | 
|---|
| 152 | BOOST_CLASS_EXPORT_KEY(HomologyContainer)
 | 
|---|
| 153 | 
 | 
|---|
| 154 | 
 | 
|---|
| 155 | #endif /* HOMOLOGYCONTAINER_HPP_ */
 | 
|---|