Scaffolding  0.1
This program can assemble genome scaffolds using the pairing information in paired-end reads.
graph_typedefs.hpp
1 
2 
3 #ifndef GRAPH_TYPEDEFS_HPP
4 #define GRAPH_TYPEDEFS_HPP
5 
6 #include <boost/graph/graph_traits.hpp>
7 #include <boost/graph/adjacency_list.hpp>
8 
9 #include <iostream>
10 #include <queue>
11 
12 #include "utils/utils.hpp"
13 
14 // translate an edge given: the old edge e, the old graph g, the new graph h, and a translate map t from old to new
15 #define translate_edge(e,g,h,t) (boost::edge(t.at(boost::source(e, g)), t.at(boost::target(e, g)), h).first)
16 
17 
18 typedef enum {dir_rev, dir_fwd, dir_fwdrev} Direction;
19 
20 using VertexName = std::string;
21 using EdgeName = std::pair<VertexName, VertexName>;
22 
23 #define NONAME "%"
24 
25 
27 {
28  unsigned p;
29  unsigned c;
30 
31  PathsAndCycles(const unsigned _p = 0, const unsigned _c = 0): p(_p), c(_c) {}
32 
33  operator size_t() const
34  {
35  size_t hash = p;
36  boost::hash_combine(hash, c);
37  return hash;
38  }
39 
40  PathsAndCycles operator+(const PathsAndCycles& pc) const
41  {
42  return {p + pc.p, c + pc.c};
43  }
44  PathsAndCycles& operator+=(const PathsAndCycles& pc)
45  {
46  p += pc.p;
47  c += pc.c;
48  return *this;
49  }
50  PathsAndCycles operator-(const PathsAndCycles& pc) const
51  {
52  return {p - pc.p, c - pc.c};
53  }
54  PathsAndCycles& operator-=(const PathsAndCycles& pc)
55  {
56  p -= pc.p;
57  c -= pc.c;
58  return *this;
59  }
60 
61  bool operator<=(const PathsAndCycles& pc) const
62  {
63  return (p <= pc.p) && (c <= pc.c);
64  }
65  bool operator<(const PathsAndCycles& pc) const
66  {
67  return (p < pc.p) && (c < pc.c);
68  }
69  bool operator==(const PathsAndCycles& pc) const
70  {
71  return (p == pc.p) && (c == pc.c);
72  }
73  bool operator!=(const PathsAndCycles& pc) const
74  {
75  return !operator==(pc);
76  }
77 
78  friend std::ostream& operator<<(std::ostream& os, const PathsAndCycles& pc)
79  {
80  return os << "("<<pc.p<<","<<pc.c<<")";
81  }
82 };
83 // provide hashing
84 size_t hash_value(const PathsAndCycles& pc)
85 {
86  return pc;
87 }
88 
89 
90 template<class Graph>
91 using Vertex = typename boost::graph_traits<Graph>::vertex_descriptor;
92 template<class Graph>
93 using VertexProperty = typename Graph::vertex_bundled;
94 template<class Graph>
95 using VertexPair = typename std::pair<Vertex<Graph>, Vertex<Graph> >;
96 template<class Graph>
97 using VertexPairSet = typename boost::unordered_set<VertexPair<Graph> >;
98 template<class Graph>
99 using VertexIter = typename boost::graph_traits<Graph>::vertex_iterator;
100 template<class Graph>
101 using VertexIterRange = typename std::pair<VertexIter<Graph>, VertexIter<Graph> >;
102 template<class Graph>
103 using AdjIter = typename boost::graph_traits<Graph>::adjacency_iterator;
104 template<class Graph>
105 using AdjIterRange = typename std::pair<AdjIter<Graph>, AdjIter<Graph> >;
106 template<class Graph>
107 using cVertexIndexMap = typename boost::property_map<Graph, unsigned VertexProperty<Graph>::*>::const_type;
108 template<class Graph>
109 using VertexIndexMap = typename boost::property_map<Graph, unsigned VertexProperty<Graph>::*>::type;
110 
111 template<class Graph>
112 using VPairDependencyMap = typename boost::unordered_map<VertexPair<Graph>, std::list<VertexPair<Graph> > >;
113 
114 template<class Graph>
115 using Edge = typename boost::graph_traits<Graph>::edge_descriptor;
116 template<class Graph>
117 using EdgeProperty = typename Graph::edge_bundled;
118 template<class Graph>
119 using EdgeIter = typename boost::graph_traits<Graph>::edge_iterator;
120 template<class Graph>
121 using EdgeIterRange = typename std::pair<EdgeIter<Graph>, EdgeIter<Graph> >;
122 template<class Graph>
123 using OEdgeIter = typename boost::graph_traits<Graph>::out_edge_iterator;
124 template<class Graph>
125 using OEdgeIterRange = typename std::pair<OEdgeIter<Graph>, OEdgeIter<Graph> >;
126 template<class Graph>
127 using VertexAndOEIterRange = std::pair<Vertex<Graph>, OEdgeIterRange<Graph> >;
128 template<class Graph>
129 using OEIterList = std::list<VertexAndOEIterRange<Graph> >;
130 template<class Graph>
131 using IEdgeIter = typename boost::graph_traits<Graph>::in_edge_iterator;
132 template<class Graph>
133 using IEdgeIterRange = typename std::pair<IEdgeIter<Graph>, IEdgeIter<Graph> >;
134 
135 
136 template<class Graph1, class Graph2>
137 using VTranslateMap = typename boost::unordered_map<Vertex<Graph1>, Vertex<Graph2> >;
138 //template<class Graph>
139 //using VTranslateMap = typename boost::unordered_map<Vertex<Graph>, Vertex<Graph> >;
140 template<class Graph>
141 using Matching = VTranslateMap<Graph, Graph>;
142 
143 template<class Graph>
144 using EWeightMap = typename boost::property_map<Graph, boost::edge_weight_t>::type;
145 template<class Graph>
146 using cEWeightMap = typename boost::property_map<Graph, boost::edge_weight_t>::const_type;
147 
148 // a structure to return a vertex with its degree
149 template<class Graph>
150 using VertexAndNum = typename std::pair<Vertex<Graph>, unsigned>;
151 template<class Graph>
152 using VertexAndDegree = VertexAndNum<Graph>;
153 
154 
155 // set up edge hashing
156 // for undirected edges, that is, uv and vu get the same hash value, pass undirected_pair_hash
157 template <class Graph, class Pair = std::unordered_pair<VertexName, VertexName> >
159 {
160  const Graph& g;
161 
162  EdgeHasher(const Graph& _g) : g(_g) {}
163 
164  size_t operator()(const Edge<Graph>& e) const {
165  const Vertex<Graph>& u = boost::source(e, g);
166  const Vertex<Graph>& v = boost::target(e, g);
167  const std::unordered_pair<VertexName, VertexName> name_pair(g[u].name, g[v].name);
168  // use the pair hasher in utils/utils.hpp
169  return hash_value(name_pair);
170  }
171 };
172 
173 template <class Graph>
175 
176 
177 
178 // a comparer based on vertex degree, to find the min degree vertex
179 template <class Graph>
180 struct degree_cmp{
181  const Graph& g;
182  degree_cmp(const Graph& _g) : g(_g) {}
183 
184  bool operator()(const Vertex<Graph>& u, const Vertex<Graph>& v){
185  return degree(u, g) < degree(v, g);
186  }
187 };
188 
189 
190 template<class Graph>
191 using EdgeToInt = boost::unordered_map<Edge<Graph>, size_t, EdgeHasher<Graph> >;
192 template<class Graph>
193 using DirectedEdgeToInt = boost::unordered_map<Edge<Graph>, size_t, DirectedEdgeHasher<Graph> >;
194 template<class Graph>
195 using EdgeList = std::list<Edge<Graph> >;
196 template<class Graph>
197 using EdgeSet = boost::unordered_set<Edge<Graph>, EdgeHasher<Graph> >;
198 template<class Graph>
199 using DirectedEdgeSet = boost::unordered_set<Edge<Graph>, DirectedEdgeHasher<Graph> >;
200 //using BiConnComponentMap = boost::iterator_property_map<size_t*, EIndexMap<Graph>, size_t, size_t& >;
201 template<class Graph>
202 using VertexToInt = boost::unordered_map<Vertex<Graph>, unsigned>;
203 template<class Graph>
204 using VertexSet = boost::unordered_set<Vertex<Graph> >;
205 template<class Graph>
206 using VertexList = std::list<Vertex<Graph> >;
207 template<class Graph>
208 using VertexPairList = std::list<VertexPair<Graph> >;
209 template<class Graph>
210 using VertexQueue = std::queue<Vertex<Graph> >;
211 //using ComponentMap = boost::iterator_property_map<size_t*, VIndexMap<Graph>, size_t, size_t& >;
212 template<class Graph>
213 using VertexPairToInt = boost::unordered_map<VertexPair<Graph>, unsigned>;
214 #warning TODO: use unordered_pair_hash for BridgeMaps, so the bridge (u,v) can be found by looking for (v,u)!!!
215 template<class Graph>
216 using ComponentMap = VertexToInt<Graph>;
217 template<class Graph>
218 using BiConnComponentMap = EdgeToInt<Graph>;
219 template<class Graph>
220 using WeightedBridge = std::pair<VertexPair<Graph>, unsigned>;
221 template<class Graph>
222 using BridgeMap = VertexPairToInt<Graph>;
223 template<class Graph>
224 using BridgeIter = typename BridgeMap<Graph>::iterator;
225 template<class Graph>
226 using cBridgeIter = typename BridgeMap<Graph>::const_iterator;
227 
228 namespace scaffold {
229 
230  // output a vertex in a graph
231  template<class Graph>
232  using VertexAndGraph = std::pair<const Vertex<Graph>&, const Graph&>;
233  template<class Graph>
234  std::ostream& operator<<(std::ostream& os, const VertexAndGraph<Graph>& vg){
235  const Vertex<Graph>& v = vg.first;
236  const Graph& g = vg.second;
237  return os << g[v].name;
238  }
239 
240 
241  // output pair of vertices
242  template<typename Graph>
243  using VertexPairAndGraph = std::pair<const VertexPair<Graph>&, const Graph&>;
244  template<typename Graph>
245  std::ostream& operator<<(std::ostream& os, const VertexPairAndGraph<Graph>& uvg){
246  const Graph& g = uvg.second;
247  return os << VertexAndGraph<Graph>(uvg.first.first, g) << "->" << VertexAndGraph<Graph>(uvg.first.second, g);
248  }
249 
250  // output an edge
251  template<typename Graph>
252  using EdgeAndGraph = std::pair<const Edge<Graph>&, const Graph&>;
253  template<typename Graph>
254  std::ostream& operator<<(std::ostream& os, const EdgeAndGraph<Graph>& eg){
255  const Edge<Graph>& e = eg.first;
256  const Graph& g = eg.second;
257  return os << VertexAndGraph<Graph>(boost::source(e, g), g) << "->" << VertexAndGraph<Graph>(boost::target(e, g), g);
258  }
259 
260  // output list of vertices
261  template<typename Graph>
262  using VertexListAndGraph = std::pair<const VertexList<Graph>&, const Graph&>;
263  template<typename Graph>
264  std::ostream& operator<<(std::ostream& os, const VertexListAndGraph<Graph>& lg){
265  const Graph& g = lg.second;
266  for(const auto& u: lg.first) os << VertexAndGraph<Graph>(u, g) << " ";
267  return os;
268  }
269  // output list of vertices
270  template<typename Graph>
271  using VertexPairListAndGraph = std::pair<const std::list<VertexPair<Graph> >&, const Graph&>;
272  template<typename Graph>
273  std::ostream& operator<<(std::ostream& os, const VertexPairListAndGraph<Graph>& lg){
274  const Graph& g = lg.second;
275  for(const auto& u: lg.first) os << VertexPairAndGraph<Graph>(u, g) << " ";
276  return os;
277  }
278 
279  // output set of vertices
280  template<typename Graph>
281  using VertexSetAndGraph = std::pair<const VertexSet<Graph>&, const Graph&>;
282  template<typename Graph>
283  std::ostream& operator<<(std::ostream& os, const VertexSetAndGraph<Graph>& lg){
284  const Graph& g = lg.second;
285  for(const auto& u: lg.first) os << VertexPairAndGraph<Graph>(u, g) << " ";
286  return os;
287  }
288 
289 
290  template<class Graph>
291  const EdgeName get_edge_name(const Edge<Graph>& e, const Graph& g)
292  {
293  const Vertex<Graph> u = boost::source(e, g);
294  const Vertex<Graph> v = boost::target(e, g);
295  return EdgeName(g[u].name, g[v].name);
296  }
297 
298  std::ostream& operator<<(std::ostream& os, const EdgeName& en){
299  return os << "("<<en.first <<","<<en.second<<")";
300  }
301 
302 
303 } // namespace
304 
305 #endif
Definition: graph_typedefs.hpp:158
Definition: read_adj_list.hpp:22
an unordered pair
Definition: unordered_pair.hpp:17
size_t hash_value(const boost::unordered_set< T > &S)
a hash computation for an unordered set, XORing its members
Definition: utils.hpp:178
Definition: graph_typedefs.hpp:180
Definition: graph_typedefs.hpp:26