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