| [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 | 
 | 
|---|
| [e920d3d] | 129 |   /** Checks whether this graph has \b exactly \a _times nodes with \a _number
 | 
|---|
 | 130 |    * atomic number.
 | 
|---|
 | 131 |    *
 | 
|---|
 | 132 |    * @param _number desired atomic number
 | 
|---|
 | 133 |    * @param _times number this must occur
 | 
|---|
 | 134 |    * @return true - graph has exactly \a _times nodes with \a _number, false - else
 | 
|---|
 | 135 |    */
 | 
|---|
 | 136 |   bool hasTimesAtomicNumber(const size_t _number, const size_t _times) const;
 | 
|---|
 | 137 | 
 | 
|---|
| [7c1091] | 138 |   /** Checks whether this graph has \b greater equal \a _times nodes with \a _number
 | 
|---|
 | 139 |    * atomic number.
 | 
|---|
 | 140 |    *
 | 
|---|
 | 141 |    * @param _number desired atomic number
 | 
|---|
 | 142 |    * @param _times number this must occur
 | 
|---|
 | 143 |    * @return true - graph has greater equal \a _times nodes with \a _number, false - else
 | 
|---|
 | 144 |    */
 | 
|---|
 | 145 |   bool hasGreaterEqualTimesAtomicNumber(const size_t _number, const size_t _times) const;
 | 
|---|
 | 146 | 
 | 
|---|
| [7b6b21f] | 147 |   /** Assignment operator for class HomologyGraph.
 | 
|---|
 | 148 |    *
 | 
|---|
 | 149 |    * This is required to allow placement in STL container as we need to
 | 
|---|
 | 150 |    * const_cast override our const member variables.
 | 
|---|
 | 151 |    *
 | 
|---|
 | 152 |    */
 | 
|---|
 | 153 |   HomologyGraph& operator=(const HomologyGraph &graph);
 | 
|---|
 | 154 | 
 | 
|---|
| [0afe00] | 155 |   /** Prints the nodes in the graph to stream \a ost.
 | 
|---|
 | 156 |    *
 | 
|---|
 | 157 |    * \param ost stream to print to
 | 
|---|
 | 158 |    */
 | 
|---|
 | 159 |   void printNodes(std::ostream& ost) const;
 | 
|---|
 | 160 | 
 | 
|---|
 | 161 |   /** Prints the edges in the graph to stream \a ost.
 | 
|---|
 | 162 |    *
 | 
|---|
 | 163 |    * \param ost stream to print to
 | 
|---|
 | 164 |    */
 | 
|---|
 | 165 |   void printEdges(std::ostream& ost) const;
 | 
|---|
 | 166 | 
 | 
|---|
| [4e6a60] | 167 |   /** Getter for the nodes contained in this graph.
 | 
|---|
 | 168 |    *
 | 
|---|
 | 169 |    * \return const ref to vector of FragmentNode
 | 
|---|
 | 170 |    */
 | 
|---|
 | 171 |   const nodes_t &getNodes() const
 | 
|---|
 | 172 |   { return nodes; }
 | 
|---|
 | 173 | 
 | 
|---|
 | 174 |   /** Getter for the edges contained in this graph.
 | 
|---|
 | 175 |    *
 | 
|---|
 | 176 |    * \return const ref to vector of FragmentEdge
 | 
|---|
 | 177 |    */
 | 
|---|
 | 178 |   const edges_t &getEdges() const
 | 
|---|
 | 179 |   { return edges; }
 | 
|---|
 | 180 | 
 | 
|---|
| [7b6b21f] | 181 | private:
 | 
|---|
 | 182 |   //!> information on the nodes of the graph
 | 
|---|
 | 183 |   const nodes_t nodes;
 | 
|---|
 | 184 |   //!> information on the edges of the graph
 | 
|---|
 | 185 |   const edges_t edges;
 | 
|---|
| [67db80] | 186 | 
 | 
|---|
 | 187 | private:
 | 
|---|
 | 188 |   friend class boost::serialization::access;
 | 
|---|
 | 189 |   // serialization
 | 
|---|
 | 190 |   template <typename Archive>
 | 
|---|
 | 191 |   void serialize(Archive& ar, const unsigned int version)
 | 
|---|
 | 192 |   {
 | 
|---|
 | 193 |     ar & const_cast<nodes_t &>(nodes);
 | 
|---|
 | 194 |     ar & const_cast<edges_t &>(edges);
 | 
|---|
 | 195 |   }
 | 
|---|
| [7b6b21f] | 196 | };
 | 
|---|
 | 197 | 
 | 
|---|
| [54a561] | 198 | std::ostream& operator<<(std::ostream& ost, const HomologyGraph &graph);
 | 
|---|
 | 199 | 
 | 
|---|
| [67db80] | 200 | // we need to give this class a unique key for serialization
 | 
|---|
 | 201 | BOOST_CLASS_EXPORT_KEY(HomologyGraph)
 | 
|---|
 | 202 | 
 | 
|---|
| [77b350] | 203 | // define some helpers outside to allow for light-weight unit testing
 | 
|---|
 | 204 | namespace detail {
 | 
|---|
 | 205 |   const HomologyGraph::nodes_t getNodesFromKeySet(const KeySet &keyset);
 | 
|---|
 | 206 |   const HomologyGraph::edges_t getEdgesFromKeySet(const KeySet &keyset);
 | 
|---|
| [372c912] | 207 |   const HomologyGraph::nodes_t getNodesFromIndexSet(const IndexSet &keyset);
 | 
|---|
 | 208 |   const HomologyGraph::edges_t getEdgesFromIndexSet(const IndexSet &keyset);
 | 
|---|
| [999eaf] | 209 |   const HomologyGraph::nodes_t getNodesFromAtomIds(const AtomIdSet::atomIdSet &keyset);
 | 
|---|
 | 210 |   const HomologyGraph::edges_t getEdgesFromAtomIds(const AtomIdSet::atomIdSet &keyset);
 | 
|---|
| [77b350] | 211 | };
 | 
|---|
 | 212 | 
 | 
|---|
| [7b6b21f] | 213 | 
 | 
|---|
 | 214 | #endif /* HOMOLOGYGRAPH_HPP_ */
 | 
|---|