Scaffolding  0.1
This program can assemble genome scaffolds using the pairing information in paired-end reads.
read_dot.hpp
1 
2 #ifndef READ_DOT_HPP
3 #define READ_DOT_HPP
4 
5 #include <iostream>
6 
7 #include <boost/graph/graph_traits.hpp>
8 #include <boost/graph/graphviz.hpp>
9 
10 #include "utils/graph_typedefs.hpp"
11 #include "utils/scaffolding_typedefs.hpp"
12 #include "utils/instance.hpp"
13 
14 namespace scaffold { namespace io {
15 
16 #warning TODO: find a better way to read sequence orientations! Currently, we just take the vertex whose name is "smaller"
17  // orient sequences using the names of the wincident vertices
18  void derive_sequence_orientations_from_vertex_names(const ScaffoldGraph& sg, SequenceMap& sequences)
19  {
20  const RawScaffoldGraph& g = sg.get_graph();
21  for(auto range = boost::vertices(g); range.first != range.second; ++range.first){
22  const ScafVertex& u = *range.first;
23  const ScafEdge uv = sg.incident_matching_edge(u);
24  const ScafVertex& v = boost::target(uv, g);
25  const VertexName& u_name = sg[u].name;
26  const VertexName& v_name = sg[v].name;
27  const std::string& contig_name = sg[uv].contig_name;
28  std::string& start_vertex = sequences[contig_name].start_vertex;
29  if(start_vertex.empty()){
30  // try to interprete vertex names as numbers
31  try{
32  const int u_num = std::stoi(u_name);
33  const int v_num = std::stoi(v_name);
34  start_vertex = (u_num < v_num) ? u_name : v_name;
35  } catch(std::exception e){
36  // if the vertex names cannot be convertex to numbers, use lexicographical compare :/
37  start_vertex = (u_name < v_name) ? u_name : v_name;
38  }// try
39  }// if the start_vertex has not been set
40  }// for each vertex of g
41  }
42 
43  // read the graph g from the instream "in"
44  Instance* read_dot(std::istream& in,
45  const unsigned number_paths,
46  const unsigned number_cycles,
47  const unsigned number_objects,
48  SequenceMap& sequences)
49  {
50  RawScaffoldGraph* g = new RawScaffoldGraph();
51  boost::dynamic_properties dp(boost::ignore_other_properties);
52  // Step 0: setup the dynamic properties
53  dp.property("node_id", boost::get(&ScafVertexProperty::index, *g));
54  dp.property("label", boost::get(&ScafVertexProperty::name, *g));
55  // attention: edges without "label" attribute are considered contig edges!
56  dp.property("label", boost::get(&ScafEdgeProperty::weight, *g));
57  dp.property("length", boost::get(&ScafEdgeProperty::length, *g));
58  dp.property("meandist", boost::get(&ScafEdgeProperty::length, *g));
59  dp.property("cname", boost::get(&ScafEdgeProperty::contig_name, *g));
60  dp.property("mult", boost::get(&ScafEdgeProperty::multiplicity, *g));
61  // Step 0a: setup property for the insert size & standard deviation of read gaps
62  typedef boost::unordered_map<RawScaffoldGraph*, unsigned> GraphIntMap;
63  GraphIntMap insert_size_map, sd_map;
64  boost::associative_property_map<GraphIntMap> insert_size_map_wrapper(insert_size_map);
65  boost::associative_property_map<GraphIntMap> sd_map_wrapper(sd_map);
66  dp.property("insert_size", insert_size_map_wrapper);
67  dp.property("sd", sd_map_wrapper);
68 
69  // Step 1: read temporary graph
70  try{
71  if(boost::read_graphviz(in, *g, dp)){
72  // put the insert_size & the std deviation into the graph
73  (*g)[graph_bundle].insert_size = insert_size_map.at(g);
74  (*g)[graph_bundle].standard_deviation = sd_map.at(g);
75  // build the instance
76  setup_vertex_names(*g);
77  Instance* outI = new Instance(g, number_paths, number_cycles, number_objects);
78  // fix multiplicities of non-matching edges
79  fix_non_matching_multiplicities(*outI);
80  // finally, derive sequence orientations
81  derive_sequence_orientations_from_vertex_names(*outI, sequences);
82  DEBUG3(std::cout << "read instance: "; outI->print_statistics(); std::cout << std::endl);
83  return outI;
84  } else {
85  DEBUG3(std::cout << "error parsing graphviz file!" << std::endl);
86  delete g;
87  return NULL;
88  }
89  } catch(boost::bad_parallel_edge ex){
90  std::cout << "Parallel-edge exception: "<< ex.what() <<std::endl;
91  delete g;
92  return NULL;
93  } catch(boost::directed_graph_error ex){
94  std::cout << "Directed graph exception: "<< ex.what() <<std::endl;
95  delete g;
96  return NULL;
97  } catch(boost::undirected_graph_error ex){
98  std::cout << "Undirected graph exception: "<<ex.what() <<std::endl;
99  delete g;
100  return NULL;
101  } catch(boost::bad_graphviz_syntax ex){
102  std::cout << "Syntax error: "<< ex.what() <<std::endl;
103  delete g;
104  return NULL;
105  }
106  } // function
107 }} // namespace
108 
109 
110 
111 
112 #endif
Definition: read_adj_list.hpp:22
Definition: read_phytree.hpp:10