| [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 | 
 | 
|---|
 | 23 | #include "Fragmentation/Homology/FragmentEdge.hpp"
 | 
|---|
 | 24 | #include "Fragmentation/Homology/FragmentNode.hpp"
 | 
|---|
 | 25 | 
 | 
|---|
| [372c912] | 26 | class IndexSet;
 | 
|---|
| [77b350] | 27 | class KeySet;
 | 
|---|
 | 28 | 
 | 
|---|
| [7b6b21f] | 29 | /** This class contains the representation of a molecular fragment as a graph.
 | 
|---|
 | 30 |  *
 | 
|---|
 | 31 |  * Only, we do not store the full graph in here. We have to include symmetries
 | 
|---|
 | 32 |  * such that two hydrogens may switch places. Eventually, we only look for the
 | 
|---|
 | 33 |  * set of distances of a fragment. If two hydrogens switch places, then also in
 | 
|---|
 | 34 |  * the set of distances some distances are interchanged but the whole fragment
 | 
|---|
 | 35 |  * remains the same. Hence, we have to store the bond graph representation in
 | 
|---|
 | 36 |  * such a way as to automatically include these symmetries.
 | 
|---|
 | 37 |  *
 | 
|---|
 | 38 |  * To this end, we use FragmentNode and FragmentEdge to store the vital
 | 
|---|
 | 39 |  * information.
 | 
|---|
 | 40 |  *
 | 
|---|
 | 41 |  */
 | 
|---|
 | 42 | class HomologyGraph
 | 
|---|
 | 43 | {
 | 
|---|
| [54a561] | 44 |   //!> grant output operator access to internals
 | 
|---|
 | 45 |   friend std::ostream& operator<<(std::ostream& ost, const HomologyGraph &graph);
 | 
|---|
| [7b6b21f] | 46 | public:
 | 
|---|
 | 47 |   //!> typedef for a set of nodes representing node information
 | 
|---|
| [28e203] | 48 |   typedef std::map<FragmentNode, size_t> nodes_t;
 | 
|---|
| [7b6b21f] | 49 |   //!> typedef for a set of nodes representing edge information
 | 
|---|
| [28e203] | 50 |   typedef std::map<FragmentEdge, size_t> edges_t;
 | 
|---|
| [7b6b21f] | 51 | public:
 | 
|---|
 | 52 |   /** Default constructor for class HomologyGraph.
 | 
|---|
 | 53 |    *
 | 
|---|
 | 54 |    * This is required to allow placement in STL containers
 | 
|---|
 | 55 |    *
 | 
|---|
 | 56 |    */
 | 
|---|
 | 57 |   HomologyGraph() {}
 | 
|---|
 | 58 | 
 | 
|---|
 | 59 |   /** Constructor for class HomologyGraph.
 | 
|---|
 | 60 |    *
 | 
|---|
 | 61 |    * @param _nodes information on nodes of this graph
 | 
|---|
 | 62 |    * @param _edges information on edges of this graph
 | 
|---|
 | 63 |    */
 | 
|---|
 | 64 |   HomologyGraph(const nodes_t &_nodes, const edges_t &_edges) :
 | 
|---|
 | 65 |     nodes(_nodes),
 | 
|---|
 | 66 |     edges(_edges)
 | 
|---|
 | 67 |   {}
 | 
|---|
 | 68 | 
 | 
|---|
| [77b350] | 69 |   /** Constructor for class HomologyGraph from a keyset (i.e. from atoms in the World).
 | 
|---|
 | 70 |    *
 | 
|---|
 | 71 |    * @param keyset global ids of atoms to pick
 | 
|---|
 | 72 |    */
 | 
|---|
 | 73 |   HomologyGraph(const KeySet &keyset);
 | 
|---|
 | 74 | 
 | 
|---|
| [372c912] | 75 |   /** Constructor for class HomologyGraph from a IndexSet (i.e. from atoms in the World).
 | 
|---|
 | 76 |    *
 | 
|---|
 | 77 |    * @param index global ids of atoms to pick
 | 
|---|
 | 78 |    */
 | 
|---|
 | 79 |   HomologyGraph(const IndexSet &index);
 | 
|---|
 | 80 | 
 | 
|---|
| [7b6b21f] | 81 |   /** Destructor for class HomologyGraph.
 | 
|---|
 | 82 |    *
 | 
|---|
 | 83 |    */
 | 
|---|
 | 84 |   ~HomologyGraph() {}
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 |   // comparators (allows sorting and hence quicker finding in STL containers)
 | 
|---|
 | 87 |   bool operator<(const HomologyGraph &graph) const;
 | 
|---|
 | 88 |   bool operator>(const HomologyGraph &graph) const;
 | 
|---|
 | 89 |   bool operator==(const HomologyGraph &graph) const;
 | 
|---|
 | 90 |   bool operator!=(const HomologyGraph &graph) const {
 | 
|---|
 | 91 |     return (!(*this == graph));
 | 
|---|
 | 92 |   }
 | 
|---|
 | 93 | 
 | 
|---|
| [28e203] | 94 |   /** Checks whether this graph contains a specific \a node.
 | 
|---|
 | 95 |    *
 | 
|---|
 | 96 |    * @param node node to look for
 | 
|---|
 | 97 |    * @param count how often this node must occur
 | 
|---|
 | 98 |    * @return true - graph contains this node at least once, false - else
 | 
|---|
 | 99 |    */
 | 
|---|
 | 100 |   bool hasNode(const FragmentNode &node, const size_t count = 1) const {
 | 
|---|
 | 101 |     nodes_t::const_iterator iter = nodes.find(node);
 | 
|---|
 | 102 |     if (iter == nodes.end())
 | 
|---|
 | 103 |       return count == 0;
 | 
|---|
 | 104 |     else
 | 
|---|
 | 105 |       return (iter->second == count);
 | 
|---|
 | 106 |   }
 | 
|---|
 | 107 | 
 | 
|---|
 | 108 |   /** Checks whether this graph contains a specific \a edge.
 | 
|---|
 | 109 |    *
 | 
|---|
 | 110 |    * @param edge edge to look for
 | 
|---|
 | 111 |    * @param count how often this edge must occur
 | 
|---|
 | 112 |    * @return true - graph contains this edge at least once, false - else
 | 
|---|
 | 113 |    */
 | 
|---|
 | 114 |   bool hasEdge(const FragmentEdge &edge, const size_t count = 1) const {
 | 
|---|
 | 115 |     edges_t::const_iterator iter = edges.find(edge);
 | 
|---|
 | 116 |     if (iter == edges.end())
 | 
|---|
 | 117 |       return count == 0;
 | 
|---|
 | 118 |     else
 | 
|---|
 | 119 |       return (iter->second == count);
 | 
|---|
 | 120 |   }
 | 
|---|
 | 121 | 
 | 
|---|
| [7b6b21f] | 122 |   /** Assignment operator for class HomologyGraph.
 | 
|---|
 | 123 |    *
 | 
|---|
 | 124 |    * This is required to allow placement in STL container as we need to
 | 
|---|
 | 125 |    * const_cast override our const member variables.
 | 
|---|
 | 126 |    *
 | 
|---|
 | 127 |    */
 | 
|---|
 | 128 |   HomologyGraph& operator=(const HomologyGraph &graph);
 | 
|---|
 | 129 | 
 | 
|---|
 | 130 | private:
 | 
|---|
 | 131 |   //!> information on the nodes of the graph
 | 
|---|
 | 132 |   const nodes_t nodes;
 | 
|---|
 | 133 |   //!> information on the edges of the graph
 | 
|---|
 | 134 |   const edges_t edges;
 | 
|---|
| [67db80] | 135 | 
 | 
|---|
 | 136 | private:
 | 
|---|
 | 137 |   friend class boost::serialization::access;
 | 
|---|
 | 138 |   // serialization
 | 
|---|
 | 139 |   template <typename Archive>
 | 
|---|
 | 140 |   void serialize(Archive& ar, const unsigned int version)
 | 
|---|
 | 141 |   {
 | 
|---|
 | 142 |     ar & const_cast<nodes_t &>(nodes);
 | 
|---|
 | 143 |     ar & const_cast<edges_t &>(edges);
 | 
|---|
 | 144 |   }
 | 
|---|
| [7b6b21f] | 145 | };
 | 
|---|
 | 146 | 
 | 
|---|
| [54a561] | 147 | std::ostream& operator<<(std::ostream& ost, const HomologyGraph &graph);
 | 
|---|
 | 148 | 
 | 
|---|
| [67db80] | 149 | // we need to give this class a unique key for serialization
 | 
|---|
 | 150 | BOOST_CLASS_EXPORT_KEY(HomologyGraph)
 | 
|---|
 | 151 | 
 | 
|---|
| [77b350] | 152 | // define some helpers outside to allow for light-weight unit testing
 | 
|---|
 | 153 | namespace detail {
 | 
|---|
 | 154 |   const HomologyGraph::nodes_t getNodesFromKeySet(const KeySet &keyset);
 | 
|---|
 | 155 |   const HomologyGraph::edges_t getEdgesFromKeySet(const KeySet &keyset);
 | 
|---|
| [372c912] | 156 |   const HomologyGraph::nodes_t getNodesFromIndexSet(const IndexSet &keyset);
 | 
|---|
 | 157 |   const HomologyGraph::edges_t getEdgesFromIndexSet(const IndexSet &keyset);
 | 
|---|
| [77b350] | 158 | };
 | 
|---|
 | 159 | 
 | 
|---|
| [7b6b21f] | 160 | 
 | 
|---|
 | 161 | #endif /* HOMOLOGYGRAPH_HPP_ */
 | 
|---|