| 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/EdgesPerFragment.hpp" | 
|---|
| 31 | #include "Fragmentation/Homology/HomologyGraph.hpp" | 
|---|
| 32 | #include "Fragmentation/Summation/SetValues/Fragment.hpp" | 
|---|
| 33 | #include "Fragmentation/Summation/SetValues/SamplingGrid.hpp" | 
|---|
| 34 |  | 
|---|
| 35 | class HomologyContainerTest; | 
|---|
| 36 |  | 
|---|
| 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 | */ | 
|---|
| 48 | class HomologyContainer : public Observable | 
|---|
| 49 | { | 
|---|
| 50 | //!> grant access to output operator | 
|---|
| 51 | friend std::ostream& operator<<(std::ostream &out, const HomologyContainer &container); | 
|---|
| 52 | //!> grant unit test access | 
|---|
| 53 | friend class HomologyContainerTest; | 
|---|
| 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; | 
|---|
| 61 | FragmentationEdges::edges_t edges; | 
|---|
| 62 | double fragmentenergy; | 
|---|
| 63 | double contribution; | 
|---|
| 64 | bool containsGrids; | 
|---|
| 65 | SamplingGrid charge_distribution; | 
|---|
| 66 | SamplingGrid potential_distribution; | 
|---|
| 67 |  | 
|---|
| 68 | value_t() : | 
|---|
| 69 | fragmentenergy(0.), | 
|---|
| 70 | contribution(0.), | 
|---|
| 71 | containsGrids(false) | 
|---|
| 72 | {} | 
|---|
| 73 |  | 
|---|
| 74 | bool operator==(const value_t &othervalue) const; | 
|---|
| 75 |  | 
|---|
| 76 | private: | 
|---|
| 77 | friend class boost::serialization::access; | 
|---|
| 78 | // serialization | 
|---|
| 79 | template <typename Archive> | 
|---|
| 80 | void serialize(Archive& ar, const unsigned int version) | 
|---|
| 81 | { | 
|---|
| 82 | ar & fragment; | 
|---|
| 83 | if (version > 1) | 
|---|
| 84 | ar & edges; | 
|---|
| 85 | if (version <= 2) | 
|---|
| 86 | ar & contribution; | 
|---|
| 87 | if (version > 2) { | 
|---|
| 88 | ar & fragmentenergy; | 
|---|
| 89 | ar & contribution; | 
|---|
| 90 | } | 
|---|
| 91 | if (version > 0) { | 
|---|
| 92 | ar & containsGrids; | 
|---|
| 93 | if (containsGrids) { | 
|---|
| 94 | ar & charge_distribution; | 
|---|
| 95 | ar & potential_distribution; | 
|---|
| 96 | } | 
|---|
| 97 | } | 
|---|
| 98 | } | 
|---|
| 99 | }; | 
|---|
| 100 |  | 
|---|
| 101 | static bool compareEnergyContribution( | 
|---|
| 102 | const std::pair<const HomologyGraph, HomologyContainer::value_t> &a, | 
|---|
| 103 | const std::pair<const HomologyGraph, HomologyContainer::value_t> &b); | 
|---|
| 104 |  | 
|---|
| 105 | static bool compareEnergy( | 
|---|
| 106 | const std::pair<const HomologyGraph, HomologyContainer::value_t> &a, | 
|---|
| 107 | const std::pair<const HomologyGraph, HomologyContainer::value_t> &b); | 
|---|
| 108 |  | 
|---|
| 109 | public: | 
|---|
| 110 | typedef std::multimap< HomologyGraph, value_t> container_t; | 
|---|
| 111 | typedef container_t::const_iterator const_iterator; | 
|---|
| 112 | typedef MapKeyConstIterator<container_t::const_iterator> const_key_iterator; | 
|---|
| 113 | typedef std::pair< const_iterator, const_iterator> range_t; | 
|---|
| 114 |  | 
|---|
| 115 | public: | 
|---|
| 116 | /** Default Constructor of class HomologyContainer. | 
|---|
| 117 | * | 
|---|
| 118 | */ | 
|---|
| 119 | HomologyContainer(); | 
|---|
| 120 |  | 
|---|
| 121 | /** Constructor of class HomologyContainer. | 
|---|
| 122 | * | 
|---|
| 123 | * @param values values with with to initially fill the container | 
|---|
| 124 | */ | 
|---|
| 125 | HomologyContainer(const container_t &values); | 
|---|
| 126 |  | 
|---|
| 127 | /** Destructor of class HomologyContainer. | 
|---|
| 128 | * | 
|---|
| 129 | */ | 
|---|
| 130 | ~HomologyContainer() {} | 
|---|
| 131 |  | 
|---|
| 132 | /** Equality comparator. | 
|---|
| 133 | * | 
|---|
| 134 | * Sadly, the insertion order of a std::multimap's values is not guaranteed | 
|---|
| 135 | * by the standard and boost::serialization does not heed the ordering of | 
|---|
| 136 | * the values associated to the same key. Hence, we implement a weaker | 
|---|
| 137 | * comparator for this class in order for the unit test to pass as we don't | 
|---|
| 138 | * actuallty care about the order of the homologous fragments. | 
|---|
| 139 | * | 
|---|
| 140 | * @param other instance to compare to | 
|---|
| 141 | * @return true - each container contains all elements of the other | 
|---|
| 142 | */ | 
|---|
| 143 | bool operator==(const HomologyContainer &other) const { | 
|---|
| 144 | return ((*this >= other) && (other >= *this)); | 
|---|
| 145 | } | 
|---|
| 146 | bool operator!=(const HomologyContainer& other) const { | 
|---|
| 147 | return !(*this == other); | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | /** Greater equal comparator, i.e. subset comparator | 
|---|
| 151 | * | 
|---|
| 152 | * @param other container to check if it's subset | 
|---|
| 153 | * @return true - \a other is a subset of this | 
|---|
| 154 | */ | 
|---|
| 155 | bool operator>=(const HomologyContainer &other) const; | 
|---|
| 156 |  | 
|---|
| 157 | /** Inserter for more graphs along with values. | 
|---|
| 158 | * | 
|---|
| 159 | * @param values graph and values to insert | 
|---|
| 160 | */ | 
|---|
| 161 | void insert(const container_t &values); | 
|---|
| 162 |  | 
|---|
| 163 | /** Returns iterator range with all contained graphs homologous to the given \a graph. | 
|---|
| 164 | * | 
|---|
| 165 | * @param graph graph to match | 
|---|
| 166 | * @return iterator range with all matches | 
|---|
| 167 | */ | 
|---|
| 168 | range_t getHomologousGraphs(const HomologyGraph &graph) const { | 
|---|
| 169 | return container.equal_range(graph); | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | /** Getter for constant iterator to begin of homologous graph container. | 
|---|
| 173 | * | 
|---|
| 174 | * @return begin constant iterator | 
|---|
| 175 | */ | 
|---|
| 176 | const_iterator begin() const { | 
|---|
| 177 | return container.begin(); | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | /** Getter for constant iterator to past end of homologous graph container. | 
|---|
| 181 | * | 
|---|
| 182 | * @return past end constant iterator | 
|---|
| 183 | */ | 
|---|
| 184 | const_iterator end() const { | 
|---|
| 185 | return container.end(); | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | const_key_iterator key_begin() const | 
|---|
| 189 | { return const_key_iterator(container.begin()); } | 
|---|
| 190 |  | 
|---|
| 191 | const_key_iterator key_end() const | 
|---|
| 192 | { return const_key_iterator(container.end()); } | 
|---|
| 193 |  | 
|---|
| 194 | const_key_iterator getNextKey(const_key_iterator &iter) const | 
|---|
| 195 | { return const_key_iterator(container.upper_bound(*iter)); } | 
|---|
| 196 |  | 
|---|
| 197 | /** Clears all homologies from container. | 
|---|
| 198 | * | 
|---|
| 199 | */ | 
|---|
| 200 | void clear(); | 
|---|
| 201 |  | 
|---|
| 202 | /** Returns the number of keys in the container. | 
|---|
| 203 | * | 
|---|
| 204 | * \return size of internal container | 
|---|
| 205 | */ | 
|---|
| 206 | const size_t size() | 
|---|
| 207 | { return container.size(); } | 
|---|
| 208 |  | 
|---|
| 209 | private: | 
|---|
| 210 | //!> multimap containing all homologous graph under same key but each with its value | 
|---|
| 211 | container_t container; | 
|---|
| 212 |  | 
|---|
| 213 | private: | 
|---|
| 214 | friend class boost::serialization::access; | 
|---|
| 215 | // serialization | 
|---|
| 216 | template <typename Archive> | 
|---|
| 217 | void load(Archive& ar, const unsigned int version) | 
|---|
| 218 | { | 
|---|
| 219 | OBSERVE; | 
|---|
| 220 | ar & container; | 
|---|
| 221 | } | 
|---|
| 222 | template <typename Archive> | 
|---|
| 223 | void save(Archive& ar, const unsigned int version) const | 
|---|
| 224 | { | 
|---|
| 225 | ar & container; | 
|---|
| 226 | } | 
|---|
| 227 | BOOST_SERIALIZATION_SPLIT_MEMBER() | 
|---|
| 228 | }; | 
|---|
| 229 |  | 
|---|
| 230 | /** Output operator for HomologyContainer. | 
|---|
| 231 | * | 
|---|
| 232 | * \param out output stream | 
|---|
| 233 | * \param container container to print | 
|---|
| 234 | * \return output stream for concatenation | 
|---|
| 235 | */ | 
|---|
| 236 | std::ostream& operator<<(std::ostream &out, const HomologyContainer &container); | 
|---|
| 237 |  | 
|---|
| 238 | // we need to give this class a unique key for serialization | 
|---|
| 239 | BOOST_CLASS_EXPORT_KEY(HomologyContainer) | 
|---|
| 240 |  | 
|---|
| 241 | // version for serialized information associated to HomologyGraph | 
|---|
| 242 | BOOST_CLASS_VERSION(HomologyContainer::value_t, 3) | 
|---|
| 243 |  | 
|---|
| 244 | #endif /* HOMOLOGYCONTAINER_HPP_ */ | 
|---|