Scaffolding  0.1
This program can assemble genome scaffolds using the pairing information in paired-end reads.
write_dot.hpp
1 
2 #ifndef WRITE_DOT_HPP
3 #define WRITE_DOT_HPP
4 
5 #include <iostream>
6 #include <list>
7 
8 #include <boost/graph/graph_traits.hpp>
9 #include <utils/graph_typedefs.hpp>
10 #include <boost/graph/graphviz.hpp>
11 
12 
13 
14 namespace scaffold { namespace io {
15 
16 
17  template <class WhatToWrite>
19  const RawScaffoldGraph& g;
20 
21  gviz_property_writer(const RawScaffoldGraph& _g): g(_g) {}
22 
23  void write_property(std::ostream& out, const std::string& name, const std::string& value, const std::string& middle = "") const{
24  out << name << " = \""<< middle << value <<"\"";
25  }
26  void write_property(std::ostream& out, const std::string& name, const unsigned& value, const std::string& middle = "") const{
27  out << name << " = "<< middle << value;
28  }
29 
30  };
31 
32 
33  struct gviz_simple_graph_writer: public gviz_property_writer<RawScaffoldGraph> {
37 
38  void operator()(std::ostream& out) const
39  {
40  out << "graph [";
41  this->write_property(out, "insert_size", g[graph_bundle].insert_size);
42  // strangely, the ";" & endl are taken care of by boost for vertex and edge properties, but not for graph properties :/
43  out << "];" << std::endl;
44  }
45 
46  };
47 
48  struct gviz_simple_vertex_writer: public gviz_property_writer<ScafVertex> {
51 
52  void write_properties(std::ostream& out, const ScafVertex& u) const
53  {
54  this->write_property(out, "label", g[u].name);
55  }
56  void operator()(std::ostream& out, const ScafVertex& u) const
57  {
58  out << "[";
59  write_properties(out, u);
60  out << "]";
61  }
62  };
63 
64 
65  struct gviz_simple_edge_writer: public gviz_property_writer<ScafEdge> {
68 
69  void write_properties(std::ostream& out, const ScafEdge& e) const{
70  if(g[e].is_matching_edge()){
71  this->write_property(out, "style", "bold");
72  const size_t mult = g[e].multiplicity;
73  this->write_property(out, ", mult", mult);
74  if(mult > 1)
75  this->write_property(out, ", color", "darkgreen");
76  } else {
77  this->write_property(out, "label", g[e].weight, " ");
78  }
79  }
80  void operator()(std::ostream& out, const ScafEdge& e) const{
81  out << "[";
82  write_properties(out, e);
83  out << "]";
84  }
85  };
86 
88  using gviz_simple_edge_writer::g;
89  ScafEdgeSet jump_edges;
90 
91  gviz_edge_writer_with_jumps(const RawScaffoldGraph& _g, const std::list<contig_jump>& _jumps):
93  jump_edges(default_buckets, _g)
94  {
95  for(const contig_jump& jump : _jumps){
96  jump_edges.insert(jump.contig);
97  for(const ScafEdge& e : jump.path) jump_edges.insert(e);
98  }
99  }
100  void write_properties(std::ostream& out, const ScafEdge& e) const{
101  if(contains(jump_edges, e)){
102  if(g[e].is_matching_edge())
103  this->write_property(out, "color", "purple");
104  else
105  this->write_property(out, "color", "blue");
106  out << ", ";
107  } else {
108  if(g[e].is_matching_edge() && (g[e].length < (int)g[graph_bundle].insert_size)){
109  this->write_property(out, "color", "red");
110  out << ", ";
111  }
112  }
113  gviz_simple_edge_writer::write_properties(out, e);
114  }
115  void operator()(std::ostream& out, const ScafEdge& e) const{
116  out << "[";
117  write_properties(out, e);
118  out << "]";
119  }
120  };
121 
122  // write the graph g to the outstream "out"
123  template<class EdgeWriter>
124  void _write_scaffold_as_dot(std::ostream& out, const RawScaffoldGraph& g, const EdgeWriter& ew){
125  const gviz_simple_vertex_writer vw(g);
126  const gviz_simple_graph_writer gw(g);
127  boost::write_graphviz(out, g, vw, ew, gw, boost::get(&ScafVertexProperty::index, g));
128  }
129 
130  // write the graph g to the outstream "out"
131  void write_scaffold_as_dot(std::ostream& out, const RawScaffoldGraph& g){
132  _write_scaffold_as_dot(out, g, gviz_simple_edge_writer(g));
133  } // function
134 
135  // write the graph g with the given jump set to the outstream "out"
136  void write_scaffold_as_dot(std::ostream& out, const RawScaffoldGraph& g, const std::list<contig_jump>& jumps){
137  std::cout << "writing with "<<jumps.size()<< " jump contigs"<<std::endl;
138  _write_scaffold_as_dot(out, g, gviz_edge_writer_with_jumps(g, jumps));
139  } // function
140 }}// namespace
141 
142 
143 
144 
145 #endif
Definition: write_dot.hpp:18
Definition: read_adj_list.hpp:22
Definition: read_phytree.hpp:10
Definition: write_dot.hpp:65
Definition: write_dot.hpp:33
Definition: write_dot.hpp:48
Definition: scaffolding_typedefs.hpp:155
Definition: write_dot.hpp:87
bool contains(const Set &s, const Element &el)
a more readable containment check
Definition: utils.hpp:171