1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
|
#ifndef CONTIGGRAPH_H
#define CONTIGGRAPH_H 1
#include "Common/ContigID.h"
#include "Graph/Properties.h"
#include <boost/graph/graph_traits.hpp>
#include <cassert>
#include <utility>
using boost::graph_traits;
/** A contig graph is a directed graph with the property that
* the edge (u,v) implies the existence of the edge (~v,~u).
*/
template <typename G>
class ContigGraph : public G {
public:
/** Copy constructors */
ContigGraph(const ContigGraph&) = default;
ContigGraph(ContigGraph&&) = default;
ContigGraph& operator=(const ContigGraph&) = default;
ContigGraph& operator=(ContigGraph&&) = default;
typedef G base_type;
// Graph
typedef typename graph_traits<G>::vertex_descriptor
vertex_descriptor;
typedef typename graph_traits<G>::directed_category
directed_category;
typedef typename graph_traits<G>::traversal_category
traversal_category;
typedef typename graph_traits<G>::edge_parallel_category
edge_parallel_category;
// IncidenceGraph
typedef typename graph_traits<G>::edge_descriptor
edge_descriptor;
typedef typename graph_traits<G>::out_edge_iterator
out_edge_iterator;
typedef typename graph_traits<G>::degree_size_type
degree_size_type;
// AdjacencyGraph
typedef typename graph_traits<G>::adjacency_iterator
adjacency_iterator;
// VertexListGraph
typedef typename graph_traits<G>::vertex_iterator
vertex_iterator;
typedef typename graph_traits<G>::vertices_size_type
vertices_size_type;
// EdgeListGraph
typedef typename graph_traits<G>::edge_iterator
edge_iterator;
typedef typename graph_traits<G>::edges_size_type
edges_size_type;
// VertexMutablePropertyGraph
typedef typename vertex_property<G>::type vertex_property_type;
// EdgeMutablePropertyGraph
typedef typename edge_property<G>::type edge_property_type;
// BidirectionalGraph
/** Iterate through the in-edges. */
class in_edge_iterator
: public std::iterator<std::input_iterator_tag, edge_descriptor>
{
/** Return the complement (~v, ~u) of the edge (u, v). */
static edge_descriptor complement(const edge_descriptor& e)
{
return std::pair<vertex_descriptor, vertex_descriptor>(
e.second ^ 1, e.first ^ 1);
}
public:
in_edge_iterator() { }
in_edge_iterator(typename graph_traits<G>::out_edge_iterator it)
: m_it(it) { }
edge_descriptor operator*() const
{
return complement(*m_it);
}
bool operator==(const in_edge_iterator& it) const
{
return m_it == it.m_it;
}
bool operator!=(const in_edge_iterator& it) const
{
return m_it != it.m_it;
}
in_edge_iterator& operator++() { ++m_it; return *this; }
in_edge_iterator operator++(int)
{
in_edge_iterator it = *this;
++*this;
return it;
}
private:
out_edge_iterator m_it;
};
public:
/** Construct an empty contig graph. */
ContigGraph() { }
/** Construct a contig graph with n vertices. The underlying
* directed graph has two vertices for each contig. */
ContigGraph(vertices_size_type n) : G(2 * n) { }
/** Return the in degree of vertex v. */
degree_size_type in_degree(vertex_descriptor v) const
{
return G::out_degree(get(vertex_complement, *this, v));
}
/** Remove all out edges from vertex u. */
void clear_out_edges(vertex_descriptor u)
{
std::pair<adjacency_iterator, adjacency_iterator>
adj = G::adjacent_vertices(u);
for (adjacency_iterator v = adj.first; v != adj.second; ++v) {
vertex_descriptor uc = get(vertex_complement, *this, u);
vertex_descriptor vc = get(vertex_complement, *this, *v);
if (vc == u) {
// When ~v == u, removing (~v,~u), which is (u,~u),
// would invalidate our iterator. This edge will be
// removed by clear_out_edges.
} else
G::remove_edge(vc, uc);
}
G::clear_out_edges(u);
}
/** Remove all in edges from vertex v. */
void clear_in_edges(vertex_descriptor v)
{
clear_out_edges(get(vertex_complement, *this, v));
}
/** Remove all edges to and from vertex v. */
void clear_vertex(vertex_descriptor v)
{
clear_out_edges(v);
clear_in_edges(v);
}
/** Add a vertex to this graph. */
vertex_descriptor add_vertex(
const vertex_property_type& data = vertex_property_type())
{
vertex_descriptor v = G::add_vertex(data);
G::add_vertex(data);
return v;
}
/** Remove vertex v from this graph. It is assumed that there
* are no edges to or from vertex v. It is best to call
* clear_vertex before remove_vertex.
*/
void remove_vertex(vertex_descriptor v)
{
G::remove_vertex(v);
G::remove_vertex(get(vertex_complement, *this, v));
}
/** Add edge (u,v) to this graph. */
std::pair<edge_descriptor, bool>
add_edge(vertex_descriptor u, vertex_descriptor v)
{
vertex_descriptor uc = get(vertex_complement, *this, u);
vertex_descriptor vc = get(vertex_complement, *this, v);
std::pair<edge_descriptor, bool> e = G::add_edge(u, v);
if (u != vc)
G::add_edge(vc, uc);
return e;
}
/** Add edge (u,v) to this graph. */
std::pair<edge_descriptor, bool>
add_edge(vertex_descriptor u, vertex_descriptor v,
const edge_property_type& ep)
{
vertex_descriptor uc = get(vertex_complement, *this, u);
vertex_descriptor vc = get(vertex_complement, *this, v);
std::pair<edge_descriptor, bool> e = G::add_edge(u, v, ep);
if (u != vc)
G::add_edge(vc, uc, ep);
return e;
}
/** Remove the edge (u,v) from this graph. */
void remove_edge(vertex_descriptor u, vertex_descriptor v)
{
vertex_descriptor uc = get(vertex_complement, *this, u);
vertex_descriptor vc = get(vertex_complement, *this, v);
G::remove_edge(u, v);
if (u != vc)
G::remove_edge(vc, uc);
}
/** Remove the edge e from this graph. */
void remove_edge(edge_descriptor e)
{
remove_edge(source(e, *this), target(e, *this));
}
};
namespace std {
template <typename G>
inline void swap(ContigGraph<G>& a, ContigGraph<G>& b)
{
a.swap(b);
}
}
// IncidenceGraph
template <typename G>
std::pair<
typename ContigGraph<G>::out_edge_iterator,
typename ContigGraph<G>::out_edge_iterator>
out_edges(
typename ContigGraph<G>::vertex_descriptor u,
const ContigGraph<G>& g)
{
return g.out_edges(u);
}
template <typename G>
typename ContigGraph<G>::degree_size_type
out_degree(
typename ContigGraph<G>::vertex_descriptor u,
const ContigGraph<G>& g)
{
return g.out_degree(u);
}
// BidirectionalGraph
template <typename G>
std::pair<
typename ContigGraph<G>::in_edge_iterator,
typename ContigGraph<G>::in_edge_iterator>
in_edges(
typename ContigGraph<G>::vertex_descriptor u,
const ContigGraph<G>& g)
{
typedef typename ContigGraph<G>::in_edge_iterator
in_edge_iterator;
typedef typename ContigGraph<G>::out_edge_iterator
out_edge_iterator;
std::pair<out_edge_iterator, out_edge_iterator> it
= out_edges(get(vertex_complement, g, u), g);
return std::pair<in_edge_iterator, in_edge_iterator>(
it.first, it.second);
}
template <typename G>
typename ContigGraph<G>::degree_size_type
in_degree(
typename ContigGraph<G>::vertex_descriptor u,
const ContigGraph<G>& g)
{
return g.in_degree(u);
}
// AdjacencyGraph
template <typename G>
std::pair<
typename ContigGraph<G>::adjacency_iterator,
typename ContigGraph<G>::adjacency_iterator>
adjacent_vertices(
typename ContigGraph<G>::vertex_descriptor u,
const ContigGraph<G>& g)
{
return g.adjacent_vertices(u);
}
// VertexListGraph
template <typename G>
typename ContigGraph<G>::vertices_size_type
num_vertices(const ContigGraph<G>& g)
{
return g.num_vertices();
}
template <typename G>
std::pair<typename ContigGraph<G>::vertex_iterator,
typename ContigGraph<G>::vertex_iterator>
vertices(const ContigGraph<G>& g)
{
return g.vertices();
}
// EdgeListGraph
template <typename G>
typename ContigGraph<G>::edges_size_type
num_edges(const ContigGraph<G>& g)
{
return g.num_edges();
}
template <typename G>
std::pair<typename ContigGraph<G>::edge_iterator,
typename ContigGraph<G>::edge_iterator>
edges(const ContigGraph<G>& g)
{
return g.edges();
}
// AdjacencyMatrix
template <typename G>
std::pair<typename ContigGraph<G>::edge_descriptor, bool>
edge(
typename ContigGraph<G>::vertex_descriptor u,
typename ContigGraph<G>::vertex_descriptor v,
const ContigGraph<G>& g)
{
return g.edge(u, v);
}
// VertexMutableGraph
template <typename G>
typename ContigGraph<G>::vertex_descriptor
add_vertex(ContigGraph<G>& g)
{
return g.add_vertex();
}
template <typename G>
void
remove_vertex(
typename ContigGraph<G>::vertex_descriptor u,
ContigGraph<G>& g)
{
g.remove_vertex(u);
}
// EdgeMutableGraph
template <typename G>
void
clear_vertex(
typename ContigGraph<G>::vertex_descriptor u,
ContigGraph<G>& g)
{
g.clear_vertex(u);
}
template <typename G>
std::pair<typename ContigGraph<G>::edge_descriptor, bool>
add_edge(
typename ContigGraph<G>::vertex_descriptor u,
typename ContigGraph<G>::vertex_descriptor v,
ContigGraph<G>& g)
{
return g.add_edge(u, v);
}
template <typename G>
void
remove_edge(
typename ContigGraph<G>::vertex_descriptor u,
typename ContigGraph<G>::vertex_descriptor v,
ContigGraph<G>& g)
{
return g.remove_edge(u, v);
}
template <typename G>
void
remove_edge(
typename ContigGraph<G>::edge_descriptor e,
ContigGraph<G>& g)
{
g.remove_edge(e);
}
// MutableIncidenceGraph
template <typename G>
void
clear_out_edges(
typename ContigGraph<G>::vertex_descriptor u,
ContigGraph<G>& g)
{
g.clear_out_edges(u);
}
// MutableBidirectionalGraph
template <typename G>
void
clear_in_edges(
typename ContigGraph<G>::vertex_descriptor u,
ContigGraph<G>& g)
{
g.clear_in_edges(u);
}
// PropertyGraph
/** Return true if this vertex has been removed. */
template <typename G>
bool get(vertex_removed_t tag, const ContigGraph<G>& g,
typename ContigGraph<G>::vertex_descriptor u)
{
return get(tag, static_cast<const G&>(g), u);
}
template <typename G>
void put(vertex_removed_t tag, ContigGraph<G>& g,
typename ContigGraph<G>::vertex_descriptor u,
bool flag)
{
put(tag, static_cast<G&>(g), u, flag);
put(tag, static_cast<G&>(g), get(vertex_complement, g, u), flag);
}
/** Return the properties of the edge of iterator eit. */
template <typename G>
const typename ContigGraph<G>::edge_property_type&
get(edge_bundle_t, const ContigGraph<G>&,
typename ContigGraph<G>::out_edge_iterator eit)
{
return eit.get_property();
}
// PropertyGraph
template <typename G>
typename vertex_bundle_type<G>::type
get(vertex_bundle_t, const ContigGraph<G>& g,
typename ContigGraph<G>::vertex_descriptor u)
{
return g[u];
}
template <typename G>
typename edge_bundle_type<G>::type
get(edge_bundle_t, const ContigGraph<G>& g,
typename ContigGraph<G>::edge_descriptor e)
{
return g[e];
}
// PropertyGraph
namespace boost {
template <typename G>
struct property_map<ContigGraph<G>, vertex_index_t>
{
typedef typename property_map<G, vertex_index_t>::type type;
typedef type const_type;
};
}
/** Return the complement of the specified vertex. */
template <typename G>
typename graph_traits<G>::vertex_descriptor
get(vertex_complement_t, const ContigGraph<G>&,
typename graph_traits<G>::vertex_descriptor u)
{
return u ^ 1;
}
/** Return the contig index of the specified vertex. */
template <typename G>
ContigID
get(vertex_contig_index_t, const ContigGraph<G>&,
typename graph_traits<G>::vertex_descriptor u)
{
return u.contigIndex();
}
/** Return the sense of the specified vertex. */
template <typename G>
bool
get(vertex_sense_t, const ContigGraph<G>&,
typename graph_traits<G>::vertex_descriptor u)
{
return u.sense();
}
// VertexMutablePropertyGraph
template <typename G>
typename ContigGraph<G>::vertex_descriptor
add_vertex(
const typename vertex_property<G>::type& vp,
ContigGraph<G>& g)
{
return g.add_vertex(vp);
}
// EdgeMutablePropertyGraph
template <typename G>
std::pair<typename ContigGraph<G>::edge_descriptor, bool>
add_edge(
typename ContigGraph<G>::vertex_descriptor u,
typename ContigGraph<G>::vertex_descriptor v,
const typename ContigGraph<G>::edge_property_type& ep,
ContigGraph<G>& g)
{
return g.add_edge(u, v, ep);
}
#endif
|