| [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 |
|
|---|
| [ff2c52] | 94 | static bool compareEnergyContribution(
|
|---|
| 95 | const std::pair<const HomologyGraph, HomologyContainer::value_t> &a,
|
|---|
| 96 | const std::pair<const HomologyGraph, HomologyContainer::value_t> &b);
|
|---|
| 97 |
|
|---|
| [4694df] | 98 | public:
|
|---|
| [77b350] | 99 | typedef std::multimap< HomologyGraph, value_t> container_t;
|
|---|
| [d058e6] | 100 | typedef container_t::const_iterator const_iterator;
|
|---|
| [39a07a] | 101 | typedef MapKeyConstIterator<container_t::const_iterator> const_key_iterator;
|
|---|
| [d058e6] | 102 | typedef std::pair< const_iterator, const_iterator> range_t;
|
|---|
| [bcc24f3] | 103 |
|
|---|
| [77b350] | 104 | public:
|
|---|
| 105 | /** Default Constructor of class HomologyContainer.
|
|---|
| 106 | *
|
|---|
| 107 | */
|
|---|
| [bcc24f3] | 108 | HomologyContainer();
|
|---|
| [77b350] | 109 |
|
|---|
| 110 | /** Constructor of class HomologyContainer.
|
|---|
| 111 | *
|
|---|
| 112 | * @param values values with with to initially fill the container
|
|---|
| 113 | */
|
|---|
| [bcc24f3] | 114 | HomologyContainer(const container_t &values);
|
|---|
| 115 |
|
|---|
| [77b350] | 116 | /** Destructor of class HomologyContainer.
|
|---|
| 117 | *
|
|---|
| 118 | */
|
|---|
| [4694df] | 119 | ~HomologyContainer() {}
|
|---|
| 120 |
|
|---|
| [12a24c] | 121 | /** Equality comparator.
|
|---|
| 122 | *
|
|---|
| 123 | * Sadly, the insertion order of a std::multimap's values is not guaranteed
|
|---|
| 124 | * by the standard and boost::serialization does not heed the ordering of
|
|---|
| 125 | * the values associated to the same key. Hence, we implement a weaker
|
|---|
| 126 | * comparator for this class in order for the unit test to pass as we don't
|
|---|
| 127 | * actuallty care about the order of the homologous fragments.
|
|---|
| 128 | *
|
|---|
| 129 | * @param other instance to compare to
|
|---|
| 130 | * @return true - each container contains all elements of the other
|
|---|
| 131 | */
|
|---|
| 132 | bool operator==(const HomologyContainer &other) const {
|
|---|
| 133 | return ((*this >= other) && (other >= *this));
|
|---|
| 134 | }
|
|---|
| 135 | bool operator!=(const HomologyContainer& other) const {
|
|---|
| 136 | return !(*this == other);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /** Greater equal comparator, i.e. subset comparator
|
|---|
| 140 | *
|
|---|
| 141 | * @param other container to check if it's subset
|
|---|
| 142 | * @return true - \a other is a subset of this
|
|---|
| 143 | */
|
|---|
| 144 | bool operator>=(const HomologyContainer &other) const;
|
|---|
| 145 |
|
|---|
| [77b350] | 146 | /** Inserter for more graphs along with values.
|
|---|
| 147 | *
|
|---|
| 148 | * @param values graph and values to insert
|
|---|
| 149 | */
|
|---|
| [bcc24f3] | 150 | void insert(const container_t &values);
|
|---|
| [77b350] | 151 |
|
|---|
| 152 | /** Returns iterator range with all contained graphs homologous to the given \a graph.
|
|---|
| 153 | *
|
|---|
| 154 | * @param graph graph to match
|
|---|
| 155 | * @return iterator range with all matches
|
|---|
| 156 | */
|
|---|
| [a8bc27a] | 157 | range_t getHomologousGraphs(const HomologyGraph &graph) const {
|
|---|
| [77b350] | 158 | return container.equal_range(graph);
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| [d058e6] | 161 | /** Getter for constant iterator to begin of homologous graph container.
|
|---|
| 162 | *
|
|---|
| 163 | * @return begin constant iterator
|
|---|
| 164 | */
|
|---|
| 165 | const_iterator begin() const {
|
|---|
| 166 | return container.begin();
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | /** Getter for constant iterator to past end of homologous graph container.
|
|---|
| 170 | *
|
|---|
| 171 | * @return past end constant iterator
|
|---|
| 172 | */
|
|---|
| 173 | const_iterator end() const {
|
|---|
| 174 | return container.end();
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| [39a07a] | 177 | const_key_iterator key_begin() const
|
|---|
| 178 | { return const_key_iterator(container.begin()); }
|
|---|
| 179 |
|
|---|
| 180 | const_key_iterator key_end() const
|
|---|
| 181 | { return const_key_iterator(container.end()); }
|
|---|
| 182 |
|
|---|
| [bcc24f3] | 183 | const_key_iterator getNextKey(const_key_iterator &iter) const
|
|---|
| 184 | { return const_key_iterator(container.upper_bound(*iter)); }
|
|---|
| 185 |
|
|---|
| 186 | /** Clears all homologies from container.
|
|---|
| 187 | *
|
|---|
| 188 | */
|
|---|
| 189 | void clear();
|
|---|
| 190 |
|
|---|
| [707a2b] | 191 | /** Returns the number of keys in the container.
|
|---|
| 192 | *
|
|---|
| 193 | * \return size of internal container
|
|---|
| 194 | */
|
|---|
| 195 | const size_t size()
|
|---|
| 196 | { return container.size(); }
|
|---|
| 197 |
|
|---|
| [4694df] | 198 | private:
|
|---|
| [77b350] | 199 | //!> multimap containing all homologous graph under same key but each with its value
|
|---|
| 200 | container_t container;
|
|---|
| [12a24c] | 201 |
|
|---|
| 202 | private:
|
|---|
| 203 | friend class boost::serialization::access;
|
|---|
| 204 | // serialization
|
|---|
| 205 | template <typename Archive>
|
|---|
| [bcc24f3] | 206 | void load(Archive& ar, const unsigned int version)
|
|---|
| 207 | {
|
|---|
| 208 | OBSERVE;
|
|---|
| 209 | ar & container;
|
|---|
| 210 | }
|
|---|
| 211 | template <typename Archive>
|
|---|
| 212 | void save(Archive& ar, const unsigned int version) const
|
|---|
| [12a24c] | 213 | {
|
|---|
| 214 | ar & container;
|
|---|
| 215 | }
|
|---|
| [bcc24f3] | 216 | BOOST_SERIALIZATION_SPLIT_MEMBER()
|
|---|
| [4694df] | 217 | };
|
|---|
| 218 |
|
|---|
| [e15ffe] | 219 | /** Output operator for HomologyContainer.
|
|---|
| 220 | *
|
|---|
| 221 | * \param out output stream
|
|---|
| 222 | * \param container container to print
|
|---|
| 223 | * \return output stream for concatenation
|
|---|
| 224 | */
|
|---|
| 225 | std::ostream& operator<<(std::ostream &out, const HomologyContainer &container);
|
|---|
| [4694df] | 226 |
|
|---|
| [12a24c] | 227 | // we need to give this class a unique key for serialization
|
|---|
| 228 | BOOST_CLASS_EXPORT_KEY(HomologyContainer)
|
|---|
| 229 |
|
|---|
| [bf1d1b] | 230 | // version for serialized information associated to HomologyGraph
|
|---|
| [814b8c] | 231 | BOOST_CLASS_VERSION(HomologyContainer::value_t, 2)
|
|---|
| [12a24c] | 232 |
|
|---|
| [4694df] | 233 | #endif /* HOMOLOGYCONTAINER_HPP_ */
|
|---|