| [7b6b21f] | 1 | /* | 
|---|
|  | 2 | * HomologyGraph.hpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Sep 24, 2012 | 
|---|
|  | 5 | *      Author: heber | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | #ifndef HOMOLOGYGRAPH_HPP_ | 
|---|
|  | 9 | #define HOMOLOGYGRAPH_HPP_ | 
|---|
|  | 10 |  | 
|---|
|  | 11 |  | 
|---|
|  | 12 | // include config.h | 
|---|
|  | 13 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 14 | #include <config.h> | 
|---|
|  | 15 | #endif | 
|---|
|  | 16 |  | 
|---|
| [67db80] | 17 | #include <boost/serialization/export.hpp> | 
|---|
| [28e203] | 18 | #include <boost/serialization/map.hpp> | 
|---|
| [67db80] | 19 |  | 
|---|
| [28e203] | 20 | #include <map> | 
|---|
| [54a561] | 21 | #include <iosfwd> | 
|---|
| [7b6b21f] | 22 |  | 
|---|
| [999eaf] | 23 | #include "AtomIdSet.hpp" | 
|---|
| [7b6b21f] | 24 | #include "Fragmentation/Homology/FragmentEdge.hpp" | 
|---|
|  | 25 | #include "Fragmentation/Homology/FragmentNode.hpp" | 
|---|
|  | 26 |  | 
|---|
| [372c912] | 27 | class IndexSet; | 
|---|
| [77b350] | 28 | class KeySet; | 
|---|
|  | 29 |  | 
|---|
| [7b6b21f] | 30 | /** This class contains the representation of a molecular fragment as a graph. | 
|---|
|  | 31 | * | 
|---|
|  | 32 | * Only, we do not store the full graph in here. We have to include symmetries | 
|---|
|  | 33 | * such that two hydrogens may switch places. Eventually, we only look for the | 
|---|
|  | 34 | * set of distances of a fragment. If two hydrogens switch places, then also in | 
|---|
|  | 35 | * the set of distances some distances are interchanged but the whole fragment | 
|---|
|  | 36 | * remains the same. Hence, we have to store the bond graph representation in | 
|---|
|  | 37 | * such a way as to automatically include these symmetries. | 
|---|
|  | 38 | * | 
|---|
|  | 39 | * To this end, we use FragmentNode and FragmentEdge to store the vital | 
|---|
|  | 40 | * information. | 
|---|
|  | 41 | * | 
|---|
|  | 42 | */ | 
|---|
|  | 43 | class HomologyGraph | 
|---|
|  | 44 | { | 
|---|
| [54a561] | 45 | //!> grant output operator access to internals | 
|---|
|  | 46 | friend std::ostream& operator<<(std::ostream& ost, const HomologyGraph &graph); | 
|---|
| [7b6b21f] | 47 | public: | 
|---|
|  | 48 | //!> typedef for a set of nodes representing node information | 
|---|
| [28e203] | 49 | typedef std::map<FragmentNode, size_t> nodes_t; | 
|---|
| [7b6b21f] | 50 | //!> typedef for a set of nodes representing edge information | 
|---|
| [28e203] | 51 | typedef std::map<FragmentEdge, size_t> edges_t; | 
|---|
| [7b6b21f] | 52 | public: | 
|---|
|  | 53 | /** Default constructor for class HomologyGraph. | 
|---|
|  | 54 | * | 
|---|
|  | 55 | * This is required to allow placement in STL containers | 
|---|
|  | 56 | * | 
|---|
|  | 57 | */ | 
|---|
|  | 58 | HomologyGraph() {} | 
|---|
|  | 59 |  | 
|---|
|  | 60 | /** Constructor for class HomologyGraph. | 
|---|
|  | 61 | * | 
|---|
|  | 62 | * @param _nodes information on nodes of this graph | 
|---|
|  | 63 | * @param _edges information on edges of this graph | 
|---|
|  | 64 | */ | 
|---|
|  | 65 | HomologyGraph(const nodes_t &_nodes, const edges_t &_edges) : | 
|---|
|  | 66 | nodes(_nodes), | 
|---|
|  | 67 | edges(_edges) | 
|---|
|  | 68 | {} | 
|---|
|  | 69 |  | 
|---|
| [77b350] | 70 | /** Constructor for class HomologyGraph from a keyset (i.e. from atoms in the World). | 
|---|
|  | 71 | * | 
|---|
|  | 72 | * @param keyset global ids of atoms to pick | 
|---|
|  | 73 | */ | 
|---|
| [b0bc13] | 74 | explicit HomologyGraph(const KeySet &keyset); | 
|---|
| [77b350] | 75 |  | 
|---|
| [372c912] | 76 | /** Constructor for class HomologyGraph from a IndexSet (i.e. from atoms in the World). | 
|---|
|  | 77 | * | 
|---|
|  | 78 | * @param index global ids of atoms to pick | 
|---|
|  | 79 | */ | 
|---|
| [b0bc13] | 80 | explicit HomologyGraph(const IndexSet &index); | 
|---|
| [372c912] | 81 |  | 
|---|
| [999eaf] | 82 | /** Constructor for class HomologyGraph from a AtomIdSet (i.e. from atoms in the World). | 
|---|
|  | 83 | * | 
|---|
|  | 84 | * @param index global ids of atoms to pick | 
|---|
|  | 85 | */ | 
|---|
|  | 86 | explicit HomologyGraph(const AtomIdSet::atomIdSet &index); | 
|---|
|  | 87 |  | 
|---|
| [7b6b21f] | 88 | /** Destructor for class HomologyGraph. | 
|---|
|  | 89 | * | 
|---|
|  | 90 | */ | 
|---|
|  | 91 | ~HomologyGraph() {} | 
|---|
|  | 92 |  | 
|---|
|  | 93 | // comparators (allows sorting and hence quicker finding in STL containers) | 
|---|
|  | 94 | bool operator<(const HomologyGraph &graph) const; | 
|---|
|  | 95 | bool operator>(const HomologyGraph &graph) const; | 
|---|
|  | 96 | bool operator==(const HomologyGraph &graph) const; | 
|---|
|  | 97 | bool operator!=(const HomologyGraph &graph) const { | 
|---|
|  | 98 | return (!(*this == graph)); | 
|---|
|  | 99 | } | 
|---|
|  | 100 |  | 
|---|
| [28e203] | 101 | /** Checks whether this graph contains a specific \a node. | 
|---|
|  | 102 | * | 
|---|
|  | 103 | * @param node node to look for | 
|---|
|  | 104 | * @param count how often this node must occur | 
|---|
|  | 105 | * @return true - graph contains this node at least once, false - else | 
|---|
|  | 106 | */ | 
|---|
|  | 107 | bool hasNode(const FragmentNode &node, const size_t count = 1) const { | 
|---|
|  | 108 | nodes_t::const_iterator iter = nodes.find(node); | 
|---|
|  | 109 | if (iter == nodes.end()) | 
|---|
|  | 110 | return count == 0; | 
|---|
|  | 111 | else | 
|---|
|  | 112 | return (iter->second == count); | 
|---|
|  | 113 | } | 
|---|
|  | 114 |  | 
|---|
|  | 115 | /** Checks whether this graph contains a specific \a edge. | 
|---|
|  | 116 | * | 
|---|
|  | 117 | * @param edge edge to look for | 
|---|
|  | 118 | * @param count how often this edge must occur | 
|---|
|  | 119 | * @return true - graph contains this edge at least once, false - else | 
|---|
|  | 120 | */ | 
|---|
|  | 121 | bool hasEdge(const FragmentEdge &edge, const size_t count = 1) const { | 
|---|
|  | 122 | edges_t::const_iterator iter = edges.find(edge); | 
|---|
|  | 123 | if (iter == edges.end()) | 
|---|
|  | 124 | return count == 0; | 
|---|
|  | 125 | else | 
|---|
|  | 126 | return (iter->second == count); | 
|---|
|  | 127 | } | 
|---|
|  | 128 |  | 
|---|
| [c4afdf3] | 129 | /** Checks whether this graph contains the given \a graph. | 
|---|
|  | 130 | * | 
|---|
|  | 131 | * Every edge must be present in this graph. | 
|---|
|  | 132 | * | 
|---|
|  | 133 | * \return true - graph is contained, false - else | 
|---|
|  | 134 | */ | 
|---|
|  | 135 | bool contains(const HomologyGraph &graph) const { | 
|---|
|  | 136 | for (edges_t::const_iterator iter = graph.edges.begin(); iter != graph.edges.end(); ++iter) { | 
|---|
|  | 137 | edges_t::const_iterator finditer = edges.find(iter->first); | 
|---|
|  | 138 | if (finditer == edges.end()) | 
|---|
|  | 139 | return false; | 
|---|
|  | 140 | else if (finditer->second < iter->second) | 
|---|
|  | 141 | return false; | 
|---|
|  | 142 | } | 
|---|
|  | 143 | return true; | 
|---|
|  | 144 | } | 
|---|
|  | 145 |  | 
|---|
| [e920d3d] | 146 | /** Checks whether this graph has \b exactly \a _times nodes with \a _number | 
|---|
|  | 147 | * atomic number. | 
|---|
|  | 148 | * | 
|---|
|  | 149 | * @param _number desired atomic number | 
|---|
|  | 150 | * @param _times number this must occur | 
|---|
|  | 151 | * @return true - graph has exactly \a _times nodes with \a _number, false - else | 
|---|
|  | 152 | */ | 
|---|
|  | 153 | bool hasTimesAtomicNumber(const size_t _number, const size_t _times) const; | 
|---|
|  | 154 |  | 
|---|
| [7c1091] | 155 | /** Checks whether this graph has \b greater equal \a _times nodes with \a _number | 
|---|
|  | 156 | * atomic number. | 
|---|
|  | 157 | * | 
|---|
|  | 158 | * @param _number desired atomic number | 
|---|
|  | 159 | * @param _times number this must occur | 
|---|
|  | 160 | * @return true - graph has greater equal \a _times nodes with \a _number, false - else | 
|---|
|  | 161 | */ | 
|---|
|  | 162 | bool hasGreaterEqualTimesAtomicNumber(const size_t _number, const size_t _times) const; | 
|---|
|  | 163 |  | 
|---|
| [7b6b21f] | 164 | /** Assignment operator for class HomologyGraph. | 
|---|
|  | 165 | * | 
|---|
|  | 166 | * This is required to allow placement in STL container as we need to | 
|---|
|  | 167 | * const_cast override our const member variables. | 
|---|
|  | 168 | * | 
|---|
|  | 169 | */ | 
|---|
|  | 170 | HomologyGraph& operator=(const HomologyGraph &graph); | 
|---|
|  | 171 |  | 
|---|
| [0afe00] | 172 | /** Prints the nodes in the graph to stream \a ost. | 
|---|
|  | 173 | * | 
|---|
|  | 174 | * \param ost stream to print to | 
|---|
|  | 175 | */ | 
|---|
|  | 176 | void printNodes(std::ostream& ost) const; | 
|---|
|  | 177 |  | 
|---|
|  | 178 | /** Prints the edges in the graph to stream \a ost. | 
|---|
|  | 179 | * | 
|---|
|  | 180 | * \param ost stream to print to | 
|---|
|  | 181 | */ | 
|---|
|  | 182 | void printEdges(std::ostream& ost) const; | 
|---|
|  | 183 |  | 
|---|
| [4e6a60] | 184 | /** Getter for the nodes contained in this graph. | 
|---|
|  | 185 | * | 
|---|
|  | 186 | * \return const ref to vector of FragmentNode | 
|---|
|  | 187 | */ | 
|---|
|  | 188 | const nodes_t &getNodes() const | 
|---|
|  | 189 | { return nodes; } | 
|---|
|  | 190 |  | 
|---|
|  | 191 | /** Getter for the edges contained in this graph. | 
|---|
|  | 192 | * | 
|---|
|  | 193 | * \return const ref to vector of FragmentEdge | 
|---|
|  | 194 | */ | 
|---|
|  | 195 | const edges_t &getEdges() const | 
|---|
|  | 196 | { return edges; } | 
|---|
|  | 197 |  | 
|---|
| [7b6b21f] | 198 | private: | 
|---|
|  | 199 | //!> information on the nodes of the graph | 
|---|
|  | 200 | const nodes_t nodes; | 
|---|
|  | 201 | //!> information on the edges of the graph | 
|---|
|  | 202 | const edges_t edges; | 
|---|
| [67db80] | 203 |  | 
|---|
|  | 204 | private: | 
|---|
|  | 205 | friend class boost::serialization::access; | 
|---|
|  | 206 | // serialization | 
|---|
|  | 207 | template <typename Archive> | 
|---|
|  | 208 | void serialize(Archive& ar, const unsigned int version) | 
|---|
|  | 209 | { | 
|---|
|  | 210 | ar & const_cast<nodes_t &>(nodes); | 
|---|
|  | 211 | ar & const_cast<edges_t &>(edges); | 
|---|
|  | 212 | } | 
|---|
| [7b6b21f] | 213 | }; | 
|---|
|  | 214 |  | 
|---|
| [54a561] | 215 | std::ostream& operator<<(std::ostream& ost, const HomologyGraph &graph); | 
|---|
|  | 216 |  | 
|---|
| [67db80] | 217 | // we need to give this class a unique key for serialization | 
|---|
|  | 218 | BOOST_CLASS_EXPORT_KEY(HomologyGraph) | 
|---|
|  | 219 |  | 
|---|
| [77b350] | 220 | // define some helpers outside to allow for light-weight unit testing | 
|---|
|  | 221 | namespace detail { | 
|---|
|  | 222 | const HomologyGraph::nodes_t getNodesFromKeySet(const KeySet &keyset); | 
|---|
|  | 223 | const HomologyGraph::edges_t getEdgesFromKeySet(const KeySet &keyset); | 
|---|
| [372c912] | 224 | const HomologyGraph::nodes_t getNodesFromIndexSet(const IndexSet &keyset); | 
|---|
|  | 225 | const HomologyGraph::edges_t getEdgesFromIndexSet(const IndexSet &keyset); | 
|---|
| [999eaf] | 226 | const HomologyGraph::nodes_t getNodesFromAtomIds(const AtomIdSet::atomIdSet &keyset); | 
|---|
|  | 227 | const HomologyGraph::edges_t getEdgesFromAtomIds(const AtomIdSet::atomIdSet &keyset); | 
|---|
| [77b350] | 228 | }; | 
|---|
|  | 229 |  | 
|---|
| [7b6b21f] | 230 |  | 
|---|
|  | 231 | #endif /* HOMOLOGYGRAPH_HPP_ */ | 
|---|