| [d24ef58] | 1 | /* | 
|---|
|  | 2 | * BoostGraphCreator.hpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: May 17, 2017 | 
|---|
|  | 5 | *      Author: heber | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 |  | 
|---|
|  | 9 | #ifndef GRAPH_BOOSTGRAPHCREATOR_HPP_ | 
|---|
|  | 10 | #define GRAPH_BOOSTGRAPHCREATOR_HPP_ | 
|---|
|  | 11 |  | 
|---|
|  | 12 | // include config.h | 
|---|
|  | 13 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 14 | #include <config.h> | 
|---|
|  | 15 | #endif | 
|---|
|  | 16 |  | 
|---|
| [bccbe9] | 17 | #include <map> | 
|---|
| [d24ef58] | 18 | #include <vector> | 
|---|
|  | 19 |  | 
|---|
|  | 20 | #include <boost/function.hpp> | 
|---|
|  | 21 | #include <boost/graph/adjacency_list.hpp> | 
|---|
|  | 22 |  | 
|---|
| [bccbe9] | 23 | #include "types.hpp" | 
|---|
|  | 24 |  | 
|---|
| [d24ef58] | 25 | class atom; | 
|---|
|  | 26 | class bond; | 
|---|
|  | 27 | class molecule; | 
|---|
|  | 28 |  | 
|---|
| [0dc8bf2] | 29 | class BoostGraphCreatorTest; | 
|---|
| [6e5b8d] | 30 | class BreadthFirstSearchGathererTest; | 
|---|
|  | 31 |  | 
|---|
| [d24ef58] | 32 | /** This is a helper class that contains functions to create a boost::graph | 
|---|
|  | 33 | * from the present bond graph of molecules. | 
|---|
|  | 34 | */ | 
|---|
|  | 35 | struct BoostGraphCreator | 
|---|
|  | 36 | { | 
|---|
| [0dc8bf2] | 37 | //!> grant unit test access to private parts | 
|---|
|  | 38 | friend class BoostGraphCreatorTest; | 
|---|
| [6e5b8d] | 39 | //!> grant unit test access to private parts that use BoostGraphCreator's internal graph | 
|---|
|  | 40 | friend class BreadthFirstSearchGathererTest; | 
|---|
|  | 41 |  | 
|---|
| [d24ef58] | 42 | public: | 
|---|
| [e3ec8a8] | 43 |  | 
|---|
| [d24ef58] | 44 | //!> typedef for an undirected graph using boost::graph | 
|---|
|  | 45 | typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS, | 
|---|
| [e3ec8a8] | 46 | boost::property<boost::vertex_name_t, atomId_t>, | 
|---|
|  | 47 | boost::property<boost::vertex_color_t, boost::default_color_type> /* needed for limited-depth DFS, | 
|---|
|  | 48 | otherwise the property_map gets full size of graph */ | 
|---|
|  | 49 | > UndirectedGraph; | 
|---|
|  | 50 |  | 
|---|
| [bccbe9] | 51 | //!> typedef for a map of graph node indices | 
|---|
|  | 52 | typedef boost::property_map < UndirectedGraph, boost::vertex_index_t >::type index_map_t; | 
|---|
|  | 53 | typedef boost::property_map < UndirectedGraph, boost::vertex_index_t >::const_type const_index_map_t; | 
|---|
| [d24ef58] | 54 | //!> typedef for a map of graph node indices | 
|---|
| [bccbe9] | 55 | typedef boost::property_map < UndirectedGraph, boost::vertex_name_t >::type name_map_t; | 
|---|
|  | 56 | typedef boost::property_map < UndirectedGraph, boost::vertex_name_t >::const_type const_name_map_t; | 
|---|
| [d24ef58] | 57 | //!> typedef for the  predicate to evaluate for adding the current edge or not | 
|---|
|  | 58 | typedef boost::function<bool (const bond &)> predicate_t; | 
|---|
| [bccbe9] | 59 | //!> typedef for a Vertex | 
|---|
|  | 60 | typedef boost::graph_traits<UndirectedGraph>::vertex_descriptor Vertex; | 
|---|
|  | 61 | //!> typedef for vertex iterator | 
|---|
|  | 62 | typedef boost::graph_traits<UndirectedGraph>::vertex_iterator vertex_iter; | 
|---|
| [e0b960] | 63 | //!> typedef for a Edge | 
|---|
|  | 64 | typedef boost::graph_traits<UndirectedGraph>::edge_descriptor Edge; | 
|---|
|  | 65 | //!> typedef for edge iterator | 
|---|
|  | 66 | typedef boost::graph_traits<UndirectedGraph>::edge_iterator edge_iter; | 
|---|
| [d24ef58] | 67 |  | 
|---|
| [bccbe9] | 68 | //!> typedef for a node id | 
|---|
|  | 69 | typedef size_t nodeId_t; | 
|---|
|  | 70 | //!> typedef for map converting between node id in graph and the associated atomic id | 
|---|
|  | 71 | typedef std::map<atomId_t, nodeId_t> atomids_nodeids_t; | 
|---|
| [d24ef58] | 72 |  | 
|---|
|  | 73 | /** Creates the boost::graph using all atoms and bonds in the given \a _mol. | 
|---|
|  | 74 | * | 
|---|
|  | 75 | * \param _mol molecule whose bond graph to construct | 
|---|
|  | 76 | * \param _pred predicate to evaluate on adding each edge/bond | 
|---|
|  | 77 | */ | 
|---|
|  | 78 | void createFromMolecule( | 
|---|
|  | 79 | const molecule &_mol, | 
|---|
|  | 80 | const predicate_t &_pred); | 
|---|
|  | 81 |  | 
|---|
|  | 82 | /** Creates the boost::graph using all atoms and bonds in the given vector | 
|---|
|  | 83 | * of \a _atoms | 
|---|
|  | 84 | * | 
|---|
|  | 85 | * \param _atoms vector of _atoms whose bond graph to construct | 
|---|
|  | 86 | * \param _pred predicate to evaluate on adding each edge/bond | 
|---|
|  | 87 | */ | 
|---|
|  | 88 | void createFromAtoms( | 
|---|
| [1c0b0b] | 89 | const std::vector<const atom *> &_atoms, | 
|---|
| [d24ef58] | 90 | const predicate_t &_pred); | 
|---|
|  | 91 |  | 
|---|
| [f5ea10] | 92 | /** Creates the boost::graph from the defining graph6 string where the atom | 
|---|
|  | 93 | * nodes map is simply the identity. | 
|---|
|  | 94 | * | 
|---|
|  | 95 | * \param _graph_string graph6 string defining the graph | 
|---|
|  | 96 | */ | 
|---|
|  | 97 | void createFromGraph6String( | 
|---|
|  | 98 | const std::string &_graph_string); | 
|---|
|  | 99 |  | 
|---|
| [d24ef58] | 100 | /** Getter for the created graph. | 
|---|
|  | 101 | * | 
|---|
|  | 102 | * \return graph | 
|---|
|  | 103 | */ | 
|---|
|  | 104 | UndirectedGraph get() const | 
|---|
|  | 105 | { return graph; } | 
|---|
|  | 106 |  | 
|---|
|  | 107 | /** Getter for the index map  of the created graph. | 
|---|
|  | 108 | * | 
|---|
|  | 109 | * \return indexmap | 
|---|
|  | 110 | */ | 
|---|
|  | 111 | index_map_t getIndexMap() const { | 
|---|
|  | 112 | return boost::get(boost::vertex_index, graph); | 
|---|
|  | 113 | } | 
|---|
|  | 114 |  | 
|---|
|  | 115 | /** Return the number of vertices contained in the created graph. | 
|---|
|  | 116 | * | 
|---|
|  | 117 | * \return number of vertices | 
|---|
|  | 118 | */ | 
|---|
|  | 119 | size_t getNumVertices() const { | 
|---|
|  | 120 | return boost::num_vertices(graph); | 
|---|
|  | 121 | } | 
|---|
|  | 122 |  | 
|---|
|  | 123 | /** Return the number of edges contained in the created graph. | 
|---|
|  | 124 | * | 
|---|
|  | 125 | * \return number of edges | 
|---|
|  | 126 | */ | 
|---|
|  | 127 | size_t getNumEdges() const { | 
|---|
|  | 128 | return boost::num_edges(graph); | 
|---|
|  | 129 | } | 
|---|
|  | 130 |  | 
|---|
| [bccbe9] | 131 | /** Returns the node id to a given atom id \a _atomid. | 
|---|
|  | 132 | * | 
|---|
|  | 133 | * \param _atomid atom id | 
|---|
|  | 134 | * \return node id | 
|---|
|  | 135 | */ | 
|---|
|  | 136 | nodeId_t getNodeId(const atomId_t &_atomid) const; | 
|---|
|  | 137 |  | 
|---|
| [d24ef58] | 138 | /** General purpose function that contains the internal logic of walking the | 
|---|
|  | 139 | * bonds of a set of atoms given by \a _begin and \a _end iterators and | 
|---|
|  | 140 | * adding its edges to a graph based on the evaluation of a given predicate | 
|---|
|  | 141 | * \a _pred. | 
|---|
|  | 142 | * | 
|---|
|  | 143 | * \note We need \a _no_nodes because molecule::iterator does not work with | 
|---|
|  | 144 | * std::distance. | 
|---|
|  | 145 | * | 
|---|
|  | 146 | * \param _begin begin iterator | 
|---|
|  | 147 | * \param _end end iterator | 
|---|
|  | 148 | * \param _no_nodes number of nodes | 
|---|
|  | 149 | * \param _pred predicate | 
|---|
|  | 150 | */ | 
|---|
|  | 151 | template <typename iterator> | 
|---|
|  | 152 | void createFromRange( | 
|---|
|  | 153 | const iterator &_begin, | 
|---|
|  | 154 | const iterator &_end, | 
|---|
|  | 155 | const size_t &_no_nodes, | 
|---|
|  | 156 | const predicate_t &_pred | 
|---|
|  | 157 | ); | 
|---|
|  | 158 |  | 
|---|
| [e0b960] | 159 | /** Finds a given edge by its two atomic indices. | 
|---|
|  | 160 | * | 
|---|
|  | 161 | * \param _firstid first atomic id of edge | 
|---|
|  | 162 | * \param _secondid second atomic id of edge | 
|---|
|  | 163 | * \return edge descriptor in graph or empty descriptor | 
|---|
|  | 164 | */ | 
|---|
|  | 165 | Edge findEdge(const atomId_t &_firstid, const atomId_t &_secondid); | 
|---|
|  | 166 |  | 
|---|
|  | 167 | /** Allows to remove a present edge in the graph. | 
|---|
|  | 168 | * | 
|---|
|  | 169 | * \param _firstid first atomic id of edge | 
|---|
|  | 170 | * \param _secondid second atomic id of edge | 
|---|
|  | 171 | * \return true - edge found and removed, false - else | 
|---|
|  | 172 | */ | 
|---|
|  | 173 | bool removeEdge(const atomId_t &_firstid, const atomId_t &_secondid); | 
|---|
|  | 174 |  | 
|---|
| [83956e] | 175 | /** Allows to remove a present edge in the graph. | 
|---|
|  | 176 | * | 
|---|
|  | 177 | * \param _bondids pair of bond ids | 
|---|
|  | 178 | * \return true - edge found and removed, false - else | 
|---|
|  | 179 | */ | 
|---|
|  | 180 | bool removeEdge(const std::pair<atomId_t, atomId_t> &_bondids) | 
|---|
|  | 181 | { return removeEdge(_bondids.first, _bondids.second); } | 
|---|
|  | 182 |  | 
|---|
| [e0b960] | 183 | /** Adds an edge to the graph if not already present. | 
|---|
|  | 184 | * | 
|---|
|  | 185 | * \param _firstid first atomic id of edge | 
|---|
|  | 186 | * \param _secondid second atomic id of edge | 
|---|
|  | 187 | * \return true - edge not found thus added, false - else | 
|---|
|  | 188 | */ | 
|---|
|  | 189 | bool addEdge(const atomId_t &_firstid, const atomId_t &_secondid); | 
|---|
|  | 190 |  | 
|---|
| [d24ef58] | 191 | private: | 
|---|
|  | 192 | //!> internal graph that is created by creator functions | 
|---|
|  | 193 | UndirectedGraph graph; | 
|---|
| [bccbe9] | 194 | //!> external property map for all the atomic ids of each graph node | 
|---|
|  | 195 | atomids_nodeids_t atomids_nodeids; | 
|---|
| [d24ef58] | 196 | }; | 
|---|
|  | 197 |  | 
|---|
| [1356af] | 198 | #include "BoostGraphCreator_impl.hpp" | 
|---|
|  | 199 |  | 
|---|
| [d24ef58] | 200 |  | 
|---|
|  | 201 | #endif /* GRAPH_BOOSTGRAPHCREATOR_HPP_ */ | 
|---|