| [4694df] | 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 | 
 | 
|---|
| [12a24c] | 17 | #include <boost/serialization/export.hpp>
 | 
|---|
 | 18 | #include <boost/serialization/map.hpp>
 | 
|---|
| [bcc24f3] | 19 | #include <boost/serialization/split_member.hpp>
 | 
|---|
| [12a24c] | 20 | #include <boost/serialization/vector.hpp>
 | 
|---|
 | 21 | 
 | 
|---|
| [e15ffe] | 22 | #include <iosfwd>
 | 
|---|
| [4694df] | 23 | #include <map>
 | 
|---|
| [77b350] | 24 | #include <vector>
 | 
|---|
| [4694df] | 25 | 
 | 
|---|
| [39a07a] | 26 | #include "CodePatterns/IteratorAdaptors.hpp"
 | 
|---|
| [bcc24f3] | 27 | #include "CodePatterns/Observer/Observable.hpp"
 | 
|---|
| [39a07a] | 28 | 
 | 
|---|
| [77b350] | 29 | #include "Fragmentation/Homology/HomologyGraph.hpp"
 | 
|---|
| [fbf143] | 30 | #include "Fragmentation/Summation/SetValues/Fragment.hpp"
 | 
|---|
| [bf1d1b] | 31 | #include "Fragmentation/Summation/SetValues/SamplingGrid.hpp"
 | 
|---|
| [12a24c] | 32 | 
 | 
|---|
 | 33 | class HomologyContainerTest;
 | 
|---|
 | 34 | 
 | 
|---|
| [4694df] | 35 | /** This class takes all KeySets in a Graph, checks for those that homologues
 | 
|---|
 | 36 |  * of one another and places them together.
 | 
|---|
 | 37 |  *
 | 
|---|
 | 38 |  * This is meant as a storage for key, value pairs, where the key is the KeySet
 | 
|---|
 | 39 |  * and the value is the energy associated to the fragment this keyset
 | 
|---|
 | 40 |  * represents.
 | 
|---|
 | 41 |  * Afterwards this can then be used as training data for a high-dimensional
 | 
|---|
 | 42 |  * approximation to the Born-Oppenheimer-surface decomposed into lower-
 | 
|---|
 | 43 |  * dimensional terms in an ANOVA-like fashion.
 | 
|---|
 | 44 |  *
 | 
|---|
 | 45 |  */
 | 
|---|
| [bcc24f3] | 46 | class HomologyContainer : public Observable
 | 
|---|
| [4694df] | 47 | {
 | 
|---|
| [e15ffe] | 48 |   //!> grant access to output operator
 | 
|---|
 | 49 |   friend std::ostream& operator<<(std::ostream &out, const HomologyContainer &container);
 | 
|---|
| [12a24c] | 50 |   //!> grant unit test access
 | 
|---|
 | 51 |   friend class HomologyContainerTest;
 | 
|---|
| [bf1d1b] | 52 | 
 | 
|---|
 | 53 | public:
 | 
|---|
 | 54 |   /** This structure represents all values associated to a specific homology
 | 
|---|
 | 55 |    * that we wish to store in this container for later reference.
 | 
|---|
 | 56 |    */
 | 
|---|
 | 57 |   struct value_t {
 | 
|---|
 | 58 |     Fragment fragment;
 | 
|---|
 | 59 |     double energy;
 | 
|---|
 | 60 |     bool containsGrids;
 | 
|---|
 | 61 |     SamplingGrid charge_distribution;
 | 
|---|
 | 62 |     SamplingGrid potential_distribution;
 | 
|---|
 | 63 | 
 | 
|---|
 | 64 |     value_t() :
 | 
|---|
 | 65 |       energy(0.),
 | 
|---|
 | 66 |       containsGrids(false)
 | 
|---|
 | 67 |     {}
 | 
|---|
 | 68 | 
 | 
|---|
 | 69 |     bool operator==(const value_t &othervalue) const;
 | 
|---|
 | 70 | 
 | 
|---|
 | 71 |   private:
 | 
|---|
 | 72 |     friend class boost::serialization::access;
 | 
|---|
 | 73 |     // serialization
 | 
|---|
 | 74 |     template <typename Archive>
 | 
|---|
 | 75 |     void serialize(Archive& ar, const unsigned int version)
 | 
|---|
 | 76 |     {
 | 
|---|
 | 77 |       ar & fragment;
 | 
|---|
 | 78 |       ar & energy;
 | 
|---|
 | 79 |       if (version > 0) {
 | 
|---|
 | 80 |         ar & containsGrids;
 | 
|---|
 | 81 |         if (containsGrids) {
 | 
|---|
 | 82 |           ar & charge_distribution;
 | 
|---|
 | 83 |           ar & potential_distribution;
 | 
|---|
 | 84 |         }
 | 
|---|
 | 85 |       }
 | 
|---|
 | 86 |     }
 | 
|---|
 | 87 |   };
 | 
|---|
 | 88 | 
 | 
|---|
| [4694df] | 89 | public:
 | 
|---|
| [77b350] | 90 |   typedef std::multimap< HomologyGraph, value_t> container_t;
 | 
|---|
| [d058e6] | 91 |   typedef container_t::const_iterator const_iterator;
 | 
|---|
| [39a07a] | 92 |   typedef MapKeyConstIterator<container_t::const_iterator> const_key_iterator;
 | 
|---|
| [d058e6] | 93 |   typedef std::pair< const_iterator, const_iterator> range_t;
 | 
|---|
| [bcc24f3] | 94 | 
 | 
|---|
| [77b350] | 95 | public:
 | 
|---|
 | 96 |   /** Default Constructor of class HomologyContainer.
 | 
|---|
 | 97 |    *
 | 
|---|
 | 98 |    */
 | 
|---|
| [bcc24f3] | 99 |   HomologyContainer();
 | 
|---|
| [77b350] | 100 | 
 | 
|---|
 | 101 |   /** Constructor of class HomologyContainer.
 | 
|---|
 | 102 |    *
 | 
|---|
 | 103 |    * @param values values with with to initially fill the container
 | 
|---|
 | 104 |    */
 | 
|---|
| [bcc24f3] | 105 |   HomologyContainer(const container_t &values);
 | 
|---|
 | 106 | 
 | 
|---|
| [77b350] | 107 |   /** Destructor of class HomologyContainer.
 | 
|---|
 | 108 |    *
 | 
|---|
 | 109 |    */
 | 
|---|
| [4694df] | 110 |   ~HomologyContainer() {}
 | 
|---|
 | 111 | 
 | 
|---|
| [12a24c] | 112 |   /** Equality comparator.
 | 
|---|
 | 113 |    *
 | 
|---|
 | 114 |    * Sadly, the insertion order of a std::multimap's values is not guaranteed
 | 
|---|
 | 115 |    * by the standard and boost::serialization does not heed the ordering of
 | 
|---|
 | 116 |    * the values associated to the same key. Hence, we implement a weaker
 | 
|---|
 | 117 |    * comparator for this class in order for the unit test to pass as we don't
 | 
|---|
 | 118 |    * actuallty care about the order of the homologous fragments.
 | 
|---|
 | 119 |    *
 | 
|---|
 | 120 |    * @param other instance to compare to
 | 
|---|
 | 121 |    * @return true - each container contains all elements of the other
 | 
|---|
 | 122 |    */
 | 
|---|
 | 123 |   bool operator==(const HomologyContainer &other) const {
 | 
|---|
 | 124 |     return ((*this >= other) && (other >= *this));
 | 
|---|
 | 125 |   }
 | 
|---|
 | 126 |   bool operator!=(const HomologyContainer& other) const {
 | 
|---|
 | 127 |     return !(*this == other);
 | 
|---|
 | 128 |   }
 | 
|---|
 | 129 | 
 | 
|---|
 | 130 |   /** Greater equal comparator, i.e. subset comparator
 | 
|---|
 | 131 |    *
 | 
|---|
 | 132 |    * @param other container to check if it's subset
 | 
|---|
 | 133 |    * @return true - \a other is a subset of this
 | 
|---|
 | 134 |    */
 | 
|---|
 | 135 |   bool operator>=(const HomologyContainer &other) const;
 | 
|---|
 | 136 | 
 | 
|---|
| [77b350] | 137 |   /** Inserter for more graphs along with values.
 | 
|---|
 | 138 |    *
 | 
|---|
 | 139 |    * @param values graph and values to insert
 | 
|---|
 | 140 |    */
 | 
|---|
| [bcc24f3] | 141 |   void insert(const container_t &values);
 | 
|---|
| [77b350] | 142 | 
 | 
|---|
 | 143 |   /** Returns iterator range with all contained graphs homologous to the given \a graph.
 | 
|---|
 | 144 |    *
 | 
|---|
 | 145 |    * @param graph graph to match
 | 
|---|
 | 146 |    * @return iterator range with all matches
 | 
|---|
 | 147 |    */
 | 
|---|
| [a8bc27a] | 148 |   range_t getHomologousGraphs(const HomologyGraph &graph) const {
 | 
|---|
| [77b350] | 149 |     return container.equal_range(graph);
 | 
|---|
 | 150 |   }
 | 
|---|
 | 151 | 
 | 
|---|
| [d058e6] | 152 |   /** Getter for constant iterator to begin of homologous graph container.
 | 
|---|
 | 153 |    *
 | 
|---|
 | 154 |    * @return begin constant iterator
 | 
|---|
 | 155 |    */
 | 
|---|
 | 156 |   const_iterator begin() const {
 | 
|---|
 | 157 |     return container.begin();
 | 
|---|
 | 158 |   }
 | 
|---|
 | 159 | 
 | 
|---|
 | 160 |   /** Getter for constant iterator to past end of homologous graph container.
 | 
|---|
 | 161 |    *
 | 
|---|
 | 162 |    * @return past end constant iterator
 | 
|---|
 | 163 |    */
 | 
|---|
 | 164 |   const_iterator end() const {
 | 
|---|
 | 165 |     return container.end();
 | 
|---|
 | 166 |   }
 | 
|---|
 | 167 | 
 | 
|---|
| [39a07a] | 168 |   const_key_iterator key_begin() const
 | 
|---|
 | 169 |   { return const_key_iterator(container.begin()); }
 | 
|---|
 | 170 | 
 | 
|---|
 | 171 |   const_key_iterator key_end() const
 | 
|---|
 | 172 |   { return const_key_iterator(container.end()); }
 | 
|---|
 | 173 | 
 | 
|---|
| [bcc24f3] | 174 |   const_key_iterator getNextKey(const_key_iterator &iter) const
 | 
|---|
 | 175 |   { return const_key_iterator(container.upper_bound(*iter)); }
 | 
|---|
 | 176 | 
 | 
|---|
 | 177 |   /** Clears all homologies from container.
 | 
|---|
 | 178 |    *
 | 
|---|
 | 179 |    */
 | 
|---|
 | 180 |   void clear();
 | 
|---|
 | 181 | 
 | 
|---|
| [4694df] | 182 | private:
 | 
|---|
| [77b350] | 183 |   //!> multimap containing all homologous graph under same key but each with its value
 | 
|---|
 | 184 |   container_t container;
 | 
|---|
| [12a24c] | 185 | 
 | 
|---|
 | 186 | private:
 | 
|---|
 | 187 |   friend class boost::serialization::access;
 | 
|---|
 | 188 |   // serialization
 | 
|---|
 | 189 |   template <typename Archive>
 | 
|---|
| [bcc24f3] | 190 |   void load(Archive& ar, const unsigned int version)
 | 
|---|
 | 191 |   {
 | 
|---|
 | 192 |     OBSERVE;
 | 
|---|
 | 193 |     ar & container;
 | 
|---|
 | 194 |   }
 | 
|---|
 | 195 |   template <typename Archive>
 | 
|---|
 | 196 |   void save(Archive& ar, const unsigned int version) const
 | 
|---|
| [12a24c] | 197 |   {
 | 
|---|
 | 198 |     ar & container;
 | 
|---|
 | 199 |   }
 | 
|---|
| [bcc24f3] | 200 |   BOOST_SERIALIZATION_SPLIT_MEMBER()
 | 
|---|
| [4694df] | 201 | };
 | 
|---|
 | 202 | 
 | 
|---|
| [e15ffe] | 203 | /** Output operator for HomologyContainer.
 | 
|---|
 | 204 |  *
 | 
|---|
 | 205 |  * \param out output stream
 | 
|---|
 | 206 |  * \param container container to print
 | 
|---|
 | 207 |  * \return output stream for concatenation
 | 
|---|
 | 208 |  */
 | 
|---|
 | 209 | std::ostream& operator<<(std::ostream &out, const HomologyContainer &container);
 | 
|---|
| [4694df] | 210 | 
 | 
|---|
| [12a24c] | 211 | // we need to give this class a unique key for serialization
 | 
|---|
 | 212 | BOOST_CLASS_EXPORT_KEY(HomologyContainer)
 | 
|---|
 | 213 | 
 | 
|---|
| [bf1d1b] | 214 | // version for serialized information associated to HomologyGraph
 | 
|---|
 | 215 | BOOST_CLASS_VERSION(HomologyContainer::value_t, 1)
 | 
|---|
| [12a24c] | 216 | 
 | 
|---|
| [4694df] | 217 | #endif /* HOMOLOGYCONTAINER_HPP_ */
 | 
|---|