| [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> | 
|---|
|  | 19 | #include <boost/serialization/vector.hpp> | 
|---|
|  | 20 |  | 
|---|
| [e15ffe] | 21 | #include <iosfwd> | 
|---|
| [4694df] | 22 | #include <map> | 
|---|
| [77b350] | 23 | #include <vector> | 
|---|
| [4694df] | 24 |  | 
|---|
| [39a07a] | 25 | #include "CodePatterns/IteratorAdaptors.hpp" | 
|---|
|  | 26 |  | 
|---|
| [77b350] | 27 | #include "Fragmentation/Homology/HomologyGraph.hpp" | 
|---|
| [79ac03] | 28 | #include "Fragmentation/SetValues/Fragment.hpp" | 
|---|
| [12a24c] | 29 |  | 
|---|
|  | 30 | class HomologyContainerTest; | 
|---|
|  | 31 |  | 
|---|
| [4694df] | 32 | /** This class takes all KeySets in a Graph, checks for those that homologues | 
|---|
|  | 33 | * of one another and places them together. | 
|---|
|  | 34 | * | 
|---|
|  | 35 | * This is meant as a storage for key, value pairs, where the key is the KeySet | 
|---|
|  | 36 | * and the value is the energy associated to the fragment this keyset | 
|---|
|  | 37 | * represents. | 
|---|
|  | 38 | * Afterwards this can then be used as training data for a high-dimensional | 
|---|
|  | 39 | * approximation to the Born-Oppenheimer-surface decomposed into lower- | 
|---|
|  | 40 | * dimensional terms in an ANOVA-like fashion. | 
|---|
|  | 41 | * | 
|---|
|  | 42 | */ | 
|---|
|  | 43 | class HomologyContainer | 
|---|
|  | 44 | { | 
|---|
| [e15ffe] | 45 | //!> grant access to output operator | 
|---|
|  | 46 | friend std::ostream& operator<<(std::ostream &out, const HomologyContainer &container); | 
|---|
| [12a24c] | 47 | //!> grant unit test access | 
|---|
|  | 48 | friend class HomologyContainerTest; | 
|---|
| [4694df] | 49 | public: | 
|---|
| [77b350] | 50 | typedef double energy_t; | 
|---|
|  | 51 | typedef std::pair<Fragment, energy_t> value_t; | 
|---|
|  | 52 | typedef std::multimap< HomologyGraph, value_t> container_t; | 
|---|
| [d058e6] | 53 | typedef container_t::const_iterator const_iterator; | 
|---|
| [39a07a] | 54 | typedef MapKeyConstIterator<container_t::const_iterator> const_key_iterator; | 
|---|
| [d058e6] | 55 | typedef std::pair< const_iterator, const_iterator> range_t; | 
|---|
| [77b350] | 56 | public: | 
|---|
|  | 57 | /** Default Constructor of class HomologyContainer. | 
|---|
|  | 58 | * | 
|---|
|  | 59 | */ | 
|---|
|  | 60 | HomologyContainer() {} | 
|---|
|  | 61 |  | 
|---|
|  | 62 | /** Constructor of class HomologyContainer. | 
|---|
|  | 63 | * | 
|---|
|  | 64 | * @param values values with with to initially fill the container | 
|---|
|  | 65 | */ | 
|---|
|  | 66 | HomologyContainer(const container_t &values) : | 
|---|
|  | 67 | container(values) | 
|---|
|  | 68 | {} | 
|---|
|  | 69 | /** Destructor of class HomologyContainer. | 
|---|
|  | 70 | * | 
|---|
|  | 71 | */ | 
|---|
| [4694df] | 72 | ~HomologyContainer() {} | 
|---|
|  | 73 |  | 
|---|
| [12a24c] | 74 | /** Equality comparator. | 
|---|
|  | 75 | * | 
|---|
|  | 76 | * Sadly, the insertion order of a std::multimap's values is not guaranteed | 
|---|
|  | 77 | * by the standard and boost::serialization does not heed the ordering of | 
|---|
|  | 78 | * the values associated to the same key. Hence, we implement a weaker | 
|---|
|  | 79 | * comparator for this class in order for the unit test to pass as we don't | 
|---|
|  | 80 | * actuallty care about the order of the homologous fragments. | 
|---|
|  | 81 | * | 
|---|
|  | 82 | * @param other instance to compare to | 
|---|
|  | 83 | * @return true - each container contains all elements of the other | 
|---|
|  | 84 | */ | 
|---|
|  | 85 | bool operator==(const HomologyContainer &other) const { | 
|---|
|  | 86 | return ((*this >= other) && (other >= *this)); | 
|---|
|  | 87 | } | 
|---|
|  | 88 | bool operator!=(const HomologyContainer& other) const { | 
|---|
|  | 89 | return !(*this == other); | 
|---|
|  | 90 | } | 
|---|
|  | 91 |  | 
|---|
|  | 92 | /** Greater equal comparator, i.e. subset comparator | 
|---|
|  | 93 | * | 
|---|
|  | 94 | * @param other container to check if it's subset | 
|---|
|  | 95 | * @return true - \a other is a subset of this | 
|---|
|  | 96 | */ | 
|---|
|  | 97 | bool operator>=(const HomologyContainer &other) const; | 
|---|
|  | 98 |  | 
|---|
| [77b350] | 99 | /** Inserter for more graphs along with values. | 
|---|
|  | 100 | * | 
|---|
|  | 101 | * @param values graph and values to insert | 
|---|
|  | 102 | */ | 
|---|
|  | 103 | void insert(const container_t &values) { | 
|---|
|  | 104 | container.insert(values.begin(), values.end()); | 
|---|
|  | 105 | } | 
|---|
|  | 106 |  | 
|---|
|  | 107 | /** Returns iterator range with all contained graphs homologous to the given \a graph. | 
|---|
|  | 108 | * | 
|---|
|  | 109 | * @param graph graph to match | 
|---|
|  | 110 | * @return iterator range with all matches | 
|---|
|  | 111 | */ | 
|---|
|  | 112 | range_t getHomologousGraphs(const HomologyGraph &graph) { | 
|---|
|  | 113 | return container.equal_range(graph); | 
|---|
|  | 114 | } | 
|---|
|  | 115 |  | 
|---|
| [d058e6] | 116 | /** Getter for constant iterator to begin of homologous graph container. | 
|---|
|  | 117 | * | 
|---|
|  | 118 | * @return begin constant iterator | 
|---|
|  | 119 | */ | 
|---|
|  | 120 | const_iterator begin() const { | 
|---|
|  | 121 | return container.begin(); | 
|---|
|  | 122 | } | 
|---|
|  | 123 |  | 
|---|
|  | 124 | /** Getter for constant iterator to past end of homologous graph container. | 
|---|
|  | 125 | * | 
|---|
|  | 126 | * @return past end constant iterator | 
|---|
|  | 127 | */ | 
|---|
|  | 128 | const_iterator end() const { | 
|---|
|  | 129 | return container.end(); | 
|---|
|  | 130 | } | 
|---|
|  | 131 |  | 
|---|
| [39a07a] | 132 | const_key_iterator key_begin() const | 
|---|
|  | 133 | { return const_key_iterator(container.begin()); } | 
|---|
|  | 134 |  | 
|---|
|  | 135 | const_key_iterator key_end() const | 
|---|
|  | 136 | { return const_key_iterator(container.end()); } | 
|---|
|  | 137 |  | 
|---|
| [4694df] | 138 | private: | 
|---|
| [77b350] | 139 | //!> multimap containing all homologous graph under same key but each with its value | 
|---|
|  | 140 | container_t container; | 
|---|
| [12a24c] | 141 |  | 
|---|
|  | 142 | private: | 
|---|
|  | 143 | friend class boost::serialization::access; | 
|---|
|  | 144 | // serialization | 
|---|
|  | 145 | template <typename Archive> | 
|---|
|  | 146 | void serialize(Archive& ar, const unsigned int version) | 
|---|
|  | 147 | { | 
|---|
|  | 148 | ar & container; | 
|---|
|  | 149 | } | 
|---|
| [4694df] | 150 | }; | 
|---|
|  | 151 |  | 
|---|
| [e15ffe] | 152 | /** Output operator for HomologyContainer. | 
|---|
|  | 153 | * | 
|---|
|  | 154 | * \param out output stream | 
|---|
|  | 155 | * \param container container to print | 
|---|
|  | 156 | * \return output stream for concatenation | 
|---|
|  | 157 | */ | 
|---|
|  | 158 | std::ostream& operator<<(std::ostream &out, const HomologyContainer &container); | 
|---|
| [4694df] | 159 |  | 
|---|
| [12a24c] | 160 | // we need to give this class a unique key for serialization | 
|---|
|  | 161 | BOOST_CLASS_EXPORT_KEY(HomologyContainer) | 
|---|
|  | 162 |  | 
|---|
|  | 163 |  | 
|---|
| [4694df] | 164 | #endif /* HOMOLOGYCONTAINER_HPP_ */ | 
|---|