7 #ifndef SCAFFOLD_GRAPH_HPP
8 #define SCAFFOLD_GRAPH_HPP
11 #include "utils/graph_typedefs.hpp"
12 #include "utils/scaffolding_typedefs.hpp"
18 using namespace graph_infos;
19 using namespace predicates;
28 typedef RawScaffoldGraph Graph;
36 typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;
37 typedef typename boost::graph_traits<Graph>::edge_descriptor Edge;
38 typedef typename Graph::vertex_bundled VertexProperty;
39 typedef typename Graph::edge_bundled EdgeProperty;
40 typedef typename Graph::graph_bundled GraphProperty;
41 typedef typename boost::property_map<Graph, unsigned VertexProperty::*>::const_type cVertexIndexMap;
42 typedef typename boost::property_map<Graph, unsigned VertexProperty::*>::type VertexIndexMap;
43 typedef typename boost::unordered_set<Vertex> VertexSet;
50 Matching<Graph>*
const t = (translate ? translate :
new Matching<Graph>());
53 orig_to_copy(associative_property_map<Matching<Graph> >(*t)).vertex_index_map(g.
get_index_map()));
55 DEBUG4(std::cout <<
"initializing matching of the copy"<<std::endl);
56 copy_matching<Graph>(g.
matched, matched, *t);
60 if(!translate)
delete t;
66 std::cout <<
"parsing matching"<<std::endl;
68 for(EdgeIterRange<Graph> r = boost::edges(*_the_graph); r.first != r.second; ++r.first){
69 const Edge& e = *r.first;
70 const EdgeProperty& e_info = (*_the_graph)[e];
71 if(e_info.is_matching_edge()){
72 const Vertex u = source(e);
73 const Vertex v = target(e);
74 matched.emplace(u, v);
75 matched.emplace(v, u);
83 remove_matching_pair<Graph>(u, matched);
89 _the_graph(new Graph()), infos(*_the_graph)
94 _the_graph(new Graph(g)), infos(*_the_graph)
96 update_vertex_indices();
97 parse_matching_from_graph();
104 _the_graph(g), infos(*_the_graph)
106 update_vertex_indices();
107 parse_matching_from_graph();
113 _the_graph(new Graph()), infos(*_the_graph)
115 copy_and_init_matching(g, translate, update_infos);
123 setup_vertex_indices(*_the_graph, first_index);
129 return boost::get(&VertexProperty::index, *_the_graph);
134 std::pair<Edge, bool>
find_edge(
const Vertex& u,
const Vertex& v)
const
136 return boost::edge(u, v, *_the_graph);
142 for(
auto r = get_incident_non_matching(u); r;) delete_edge(*(r++));
149 void delete_vertex(
const Vertex& u,
const bool update_indices =
true,
const bool update_infos =
true)
153 for(
auto r = boost::out_edges(u, *_the_graph); r.first != r.second;)
154 delete_edge(*(r.first++),
true);
155 else boost::clear_vertex(u, *_the_graph);
156 boost::remove_vertex(u, *_the_graph);
162 if(update_indices) update_vertex_indices();
168 return (*_the_graph)[u];
173 return (*_the_graph)[u];
178 return (*_the_graph)[e];
183 return (*_the_graph)[e];
186 const EdgeProperty&
operator[](
const VertexPair<Graph>& uv)
const
188 std::pair<Edge, bool> e = boost::edge(uv.first, uv.second, *_the_graph);
190 return (*_the_graph)[e.first];
197 std::pair<Edge, bool> e = boost::edge(uv.first, uv.second, *_the_graph);
199 return (*_the_graph)[e.first];
206 return (*_the_graph)[boost::graph_bundle];
212 return boost::source(e, *_the_graph);
217 return boost::target(e, *_the_graph);
239 return matched.at(v);
245 return (uv.first == matched_with(uv.second));
250 assert(matched.find(v) != matched.cend());
251 return boost::edge(v, matched.at(v), *_the_graph).first;
254 const unsigned degree(
const Vertex& v)
const
256 return boost::degree(v, *_the_graph);
260 bool adjacent(
const Vertex& u,
const Vertex& v)
const
262 return boost::edge(u, v, *_the_graph).second;
267 for(
auto r = boost::adjacent_vertices(u, *_the_graph); r.first != r.second; ++r.first)
268 nh.emplace(*r.first);
275 for(
auto r = boost::edges(*_the_graph); r.first != r.second; ++r.first)
276 result = std::max(result,
operator[](*r.first).multiplicity);
285 for(EdgeIterRange<Graph> r = boost::edges(*_the_graph); r.first != r.second; ++r.first){
286 const Edge& e = *r.first;
288 if(!e_info.is_matching_edge())
289 if(e_info.length > _max){
290 _max = e_info.length;
294 return std::pair<Edge, int>(_max_e, _max);
302 const Vertex& u = boost::source(e, *_the_graph);
303 const Vertex& v = boost::target(e, *_the_graph);
311 boost::remove_edge(e, *_the_graph);
312 DEBUG5(std::cout<<
"deleted edge "<<(*_the_graph)[u].name<<
"-"<<(*_the_graph)[v].name<<std::endl);
318 const std::pair<Edge, bool> e_pair = find_edge(u, v);
320 delete_edge(e_pair.first);
327 return delete_edge(uv.first, uv.second);
334 for(VertexIterRange<Graph> r = boost::vertices(*_the_graph); r.first != r.second; ++r.first)
335 if((*_the_graph)[*r.first].name == name)
345 DEBUG5(std::cout<<
"finding ("<<name.first<<
","<<name.second<<
")"<<std::endl);
346 Vertex
const * u = find_vertex_by_name(name.first);
349 for(OEdgeIterRange<Graph> er = boost::out_edges(*u, *_the_graph); er.first != er.second; ++er.first){
350 const Edge& e = *er.first;
351 const Vertex& v = boost::target(e, *_the_graph);
352 if((*_the_graph)[v].name == name.second)
361 const Vertex
add_vertex(
const VertexProperty& p,
const bool update_infos =
true)
363 const Vertex u = boost::add_vertex(p, *_the_graph);
369 const Vertex
add_vertex(
const std::string name,
const bool update_infos =
true,
const unsigned index = UINT_MAX)
372 vp.index = (index != UINT_MAX ? index : boost::num_vertices(*_the_graph));
374 return add_vertex(vp, update_infos);
378 const Vertex
add_vertex(
const Vertex& u,
const bool update_infos =
true)
380 return add_vertex(
operator[](u).name, update_infos);
387 const EdgeProperty& ep = EdgeProperty(),
388 const bool update_infos =
true)
390 std::pair<Edge, bool> e = boost::add_edge(u, v, *_the_graph);
392 (*_the_graph)[e.first] = ep;
393 if(update_infos) infos.
add_edge(u, v);
402 const EdgeProperty& ep = EdgeProperty(),
403 const bool update_infos =
true)
405 std::pair<Edge, bool> e = add_edge(u, v, ep, update_infos);
408 if(ep.weight != NO_WEIGHT) (*_the_graph)[e.first].weight = NO_WEIGHT;
410 matched.emplace(u, v);
411 matched.emplace(v, u);
413 assert(matched.emplace(u, v).second);
414 assert(matched.emplace(v, u).second);
423 return EdgeName((*_the_graph)[uv.first].name, (*_the_graph)[uv.second].name);
429 const Vertex& u = boost::source(e, *_the_graph);
430 const Vertex& v = boost::target(e, *_the_graph);
431 return EdgeName((*_the_graph)[u].name, (*_the_graph)[v].name);
435 bool is_bridge(
const ScafVertex& u,
const ScafVertex& v)
437 return infos.bridges.is_bridge(u, v);
443 return infos.bridges.is_bridge_const(u, v);
450 if(infos.bridges.is_valid())
451 if(infos.bridges.is_bridge_const(u, v))
return false;
453 return edge_on_alternating_cycle(u, v, *_the_graph, matched);
461 for(ScafEdgeIterRange er = boost::edges(*_the_graph); er.first != er.second; ++er.first)
462 accu += (*_the_graph)[*er.first].weight;
467 template<
class Predicate = TruePredicate<Edge> >
486 template<
class Predicate = TruePredicate<Edge> >
501 if(infos.comps.get_num() > 1){
503 ScafMatching translate, translate_back;
505 Graph*
const h = scaffold::split_off_component<Graph>(*_the_graph, &translate, &translate_back);
508 setup_vertex_indices(*h);
509 setup_vertex_indices(*_the_graph);
512 parse_matching_from_graph();
523 ScafMatching translate;
525 copy_and_init_matching(g, &translate,
false);
526 update_vertex_indices();
536 std::cout << boost::num_vertices(*_the_graph) <<
" vertices, "
537 << boost::num_edges(*_the_graph)<<
" edges, "
538 <<
" insert size: "<<get_graph_property().insert_size
539 <<
" std deviation: "<<get_graph_property().standard_deviation;
546 std::cout <<
"removing contig edges..."<<std::endl;
547 for(
const VertexPair<Graph>& c: matched) delete_edge(c);
std::pair< Edge, int > get_max_non_contig_length() const
return a non-contig edge of maximum length in the graph
Definition: scaffold_graph.hpp:281
Predicated_EdgeIter< Graph, MatchingPredicate< Graph > > get_incident_non_matching(const Vertex &u) const
convenience function to get all incident non-matching edges of u
Definition: scaffold_graph.hpp:493
an edge predicate that is true iff the given edge is (not) a matching edge
Definition: predicates.hpp:66
void delete_edge(const VertexPair< Graph > &uv)
react to deletion of edge uv
Definition: graph_infos.hpp:662
ScaffoldGraph()
constructor initializing an empty graph
Definition: scaffold_graph.hpp:88
const Vertex & matched_with(const Vertex &v) const
get a const reference to the vertex matched with v
Definition: scaffold_graph.hpp:237
const EdgeProperty & operator[](const VertexPair< Graph > &uv) const
get a const reference to the edge properties of uv, throwing invalid_assumption if uv does not exist ...
Definition: scaffold_graph.hpp:186
void read_from_split_off_component(const GraphInfos< Graph > &infos, const Matching< Graph > &translate)
react to splitting off this connected component g from a graph h
Definition: graph_infos.hpp:688
bool delete_edge(const VertexPair< Graph > &uv)
convenience function to delete pairs of vertices
Definition: scaffold_graph.hpp:325
Graph *const split_off_component()
if _the_graph is disconnected, split off & return a connected compoenent, otherwise return NULL ...
Definition: scaffold_graph.hpp:499
void read_from_infos(const GraphInfos< Graph > &infos, const Matching< Graph > &translate)
update the infos from the graph h of which g has just been copied
Definition: graph_infos.hpp:677
bool adjacent(const Vertex &u, const Vertex &v) const
return whether u & v are adjacent
Definition: scaffold_graph.hpp:260
Vertex const * find_vertex_by_name(const VertexName &name) const
find a vertex given its name
Definition: scaffold_graph.hpp:332
void delete_vertex(const Vertex &u, const bool update_indices=true, const bool update_infos=true)
remove a vertex and all its incident edges
Definition: scaffold_graph.hpp:149
const Edge incident_matching_edge(const Vertex &v) const
return the matching edge that is incident with v
Definition: scaffold_graph.hpp:248
unsigned weight() const
return the total weight of the graph
Definition: scaffold_graph.hpp:457
VertexProperty & operator[](const Vertex &u)
get a reference to the vertex properties of u
Definition: scaffold_graph.hpp:171
Definition: read_adj_list.hpp:22
const VertexProperty & operator[](const Vertex &u) const
get a const reference to the vertex properties of u
Definition: scaffold_graph.hpp:166
Vertex target(const Edge &e) const
get a copy of the target vertex of e
Definition: scaffold_graph.hpp:215
prototype predicate
Definition: predicates.hpp:14
void remove_contigs()
remove all contig edges from the graph
Definition: scaffold_graph.hpp:544
const bool is_matching_edge(const VertexPair< Graph > &uv) const
return whether the given VertexPair is matched
Definition: scaffold_graph.hpp:243
Graph *const _the_graph
the underlying boost graph
Definition: scaffold_graph.hpp:30
void parse_matching_from_graph()
compute the matching by parsing the properties of all edges
Definition: scaffold_graph.hpp:64
const GraphProperty & get_graph_property() const
return a const reference to the graph properties
Definition: scaffold_graph.hpp:204
const Vertex add_vertex(const std::string name, const bool update_infos=true, const unsigned index=UINT_MAX)
add a new vertex and return it; use the given name & index
Definition: scaffold_graph.hpp:369
void add_edge(const Vertex< Graph > &u, const Vertex< Graph > &v)
react to addition of edge uv
Definition: graph_infos.hpp:654
const unsigned degree(const Vertex &v) const
return the degree of v
Definition: scaffold_graph.hpp:254
void copy_and_init_matching(const ScaffoldGraph &g, Matching< Graph > *const translate, const bool update_infos)
outsource common constructor work: copy a given graph, construct a matching from the translate map...
Definition: scaffold_graph.hpp:47
ScaffoldGraph(const ScaffoldGraph &g, Matching< Graph > *const translate, const bool update_infos=true)
constructor initializing a copy of the given graph g and returning the translate map in translate; up...
Definition: scaffold_graph.hpp:112
ScaffoldGraph(const Graph &g)
constructor initializing a copy of the given graph g
Definition: scaffold_graph.hpp:93
Predicated_EdgeIter< Graph, Predicate > get_incident(const Vertex &u, const Predicate &pred=Predicate()) const
get incident edges of u satisfying a predicate
Definition: scaffold_graph.hpp:487
Predicated_EdgeIter< Graph, MatchingPredicate< Graph >, EdgeIter< Graph > > get_matching_edges() const
convenience fuction to get all matching edges
Definition: scaffold_graph.hpp:474
void clear_vertex(const Vertex &u)
remove all non-matching edges incident with u
Definition: scaffold_graph.hpp:140
const Vertex add_vertex(const VertexProperty &p, const bool update_infos=true)
add a new vertex and return it
Definition: scaffold_graph.hpp:361
std::pair< Edge, bool > find_edge(const Vertex &u, const Vertex &v) const
find an edge in _the_graph, given its endpoints
Definition: scaffold_graph.hpp:134
an exception for the case that some assumption that is made is not true
Definition: exceptions.hpp:30
EdgeName get_edge_name(const Edge &e) const
get a copy of the name of an edge
Definition: scaffold_graph.hpp:427
the main graph class
Definition: scaffold_graph.hpp:25
void disjoint_union(const ScaffoldGraph &g)
merge g into our graph
Definition: scaffold_graph.hpp:521
ScaffoldGraph(Graph *const g)
constructor initializing using the given graph pointer; no copy is performed
Definition: scaffold_graph.hpp:103
EdgeProperty & operator[](const Edge &e)
get a reference to the edge properties of e
Definition: scaffold_graph.hpp:181
an edge iterator that skips over all edges for which the predicate evaluates to false ...
Definition: adv_edge_iters.hpp:19
void delete_matching(const Vertex &u)
remove the indication that uv is a matching edge
Definition: scaffold_graph.hpp:81
~ScaffoldGraph()
destructor deleting _the_graph
Definition: scaffold_graph.hpp:118
void get_neighbors(const Vertex &u, VertexSet &nh) const
put all neighbors of u into nh
Definition: scaffold_graph.hpp:265
const VertexIndexMap get_index_map() const
get an index map to use in various boost algorithms
Definition: scaffold_graph.hpp:127
bool delete_edge(const Vertex &u, const Vertex &v)
delete an edge e and update infos if requested; return whether the edge had existed before or not ...
Definition: scaffold_graph.hpp:316
Matching< Graph > matched
a perfect matching in _the_graph
Definition: scaffold_graph.hpp:31
std::pair< Edge, bool > add_matching_edge(const Vertex &u, const Vertex &v, const EdgeProperty &ep=EdgeProperty(), const bool update_infos=true)
add a matching edge unless the edge already exists; return the edge and whether an insertion took pla...
Definition: scaffold_graph.hpp:400
const Graph & get_graph() const
get a const reference to the underlying graph
Definition: scaffold_graph.hpp:227
void update_vertex_indices(const unsigned first_index=0)
recompute vertex indices; useful after deleting vertices or uniting scaffold graphs ...
Definition: scaffold_graph.hpp:121
EdgeName get_edge_name(const VertexPair< Graph > &uv) const
get a copy of the name of the edge uv; independent of its presence
Definition: scaffold_graph.hpp:421
bool is_on_alternating_cycle(const ScafVertex &u, const ScafVertex &v) const
return whether the given edge is on an alternating cycle
Definition: scaffold_graph.hpp:447
Definition: scaffolding_typedefs.hpp:86
void invalidate_all()
invalidate all infos
Definition: graph_infos.hpp:628
Vertex source(const Edge &e) const
get a copy of the source vertex of e
Definition: scaffold_graph.hpp:210
void delete_edge(const Edge &e, const bool update_infos=true)
delete an edge e and update infos if requested
Definition: scaffold_graph.hpp:300
void add_vertex(const Vertex< Graph > &u)
react to addition of vertex u
Definition: graph_infos.hpp:647
bool is_bridge_const(const ScafVertex &u, const ScafVertex &v) const
return whether the given vertex pair constitutes a bridge without updating the infos ...
Definition: scaffold_graph.hpp:441
bool is_bridge(const ScafVertex &u, const ScafVertex &v)
return whether the given vertex pair constitutes a bridge, updating infos if necessary ...
Definition: scaffold_graph.hpp:435
std::pair< Edge, bool > add_edge(const Vertex &u, const Vertex &v, const EdgeProperty &ep=EdgeProperty(), const bool update_infos=true)
add a new edge unless it is already there; return it and whether an insertion took place ...
Definition: scaffold_graph.hpp:385
Edge const * find_edge_by_name(const EdgeName &name) const
delete edge by name
Definition: scaffold_graph.hpp:342
Predicated_EdgeIter< Graph, MatchingPredicate< Graph >, EdgeIter< Graph > > get_non_matching_edges() const
convenience fuction to get all non-matching edges
Definition: scaffold_graph.hpp:480
unsigned get_max_multiplicity() const
compute the maximum multiplicity occuring in g
Definition: scaffold_graph.hpp:272
void update_disjoint_union(const GraphInfos< Graph > &infos, const Matching< Graph > &translate)
react to forming a disjoint union with g with another graph h, given the infos of h and a translate m...
Definition: graph_infos.hpp:699
GraphInfos< Graph > infos
a set of graph properties of the underlying graph
Definition: scaffold_graph.hpp:33
EdgeProperty & operator[](const VertexPair< Graph > &uv)
get a reference to the edge properties of uv, throwing invalid_assumption if uv does not exist ...
Definition: scaffold_graph.hpp:195
const Matching< Graph > & get_matching() const
get a const reference to the perfect matching in _the_graph
Definition: scaffold_graph.hpp:232
const GraphInfos< Graph > & get_infos() const
get a const reference to the graph infos
Definition: scaffold_graph.hpp:221
an accumulator class for different graph properties
Definition: graph_infos.hpp:604
const EdgeProperty & operator[](const Edge &e) const
get a const reference to the edge properties of e
Definition: scaffold_graph.hpp:176
const Vertex add_vertex(const Vertex &u, const bool update_infos=true)
add a new vertex and return it; copy the name from vertex u
Definition: scaffold_graph.hpp:378
void print_statistics()
print various statistics
Definition: scaffold_graph.hpp:534
Predicated_EdgeIter< Graph, Predicate, EdgeIter< Graph > > get_edges(const Predicate &pred=Predicate()) const
get all edges satisfying a predicate
Definition: scaffold_graph.hpp:468