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