Scaffolding  0.1
This program can assemble genome scaffolds using the pairing information in paired-end reads.
ilp_contig_jumps.hpp
1 
2 
3 #ifndef ILP_CONTIG_JUMPS_HPP
4 #define ILP_CONTIG_JUMPS_HPP
5 
6 #include "solv/ilp_common.hpp"
7 #include "utils/set_queue.hpp"
8 
9 namespace scaffold { namespace solv { namespace ilp {
10 
11  // introduce constraints to respect the dependencies of new edges added by the jump preprocessing
12  void respect_jump_dependencies(IloModel& model, const IloEnv& env, var_collection_multi& vars, const ScafVPairDependencyMap& dependencies)
13  {
14  // the dependency map maps each VertexPair p to a list of VertexPairs on which they depend, that is,
15  // p cannot be taken more times than the sum of its dependencies
16  IloRangeArray c(env);
17  DEBUG5(std::cout << "adding contig-jump-shortcut constraints: if we take an added shortcut, we should also take its dependency"<<std::endl);
18  for(const auto& dlist: dependencies){
19  const ScafVertexPair& uv = dlist.first;
20  const auto& uv_dep_list = dlist.second;
21  // build the constraint
22  IloExpr expr(env);
23  IloExpr rev_expr(env);
24  for(const auto& uv_dep_edge: uv_dep_list){
25  expr += vars.times_used.at(uv_dep_edge);
26  rev_expr += vars.times_used.at(reverse(uv_dep_edge));
27  }
28 
29  expr -= vars.times_used.at(uv);
30  rev_expr -= vars.times_used.at(reverse(uv));
31  c.add(expr >= 0);
32  c.add(rev_expr >= 0);
33  }
34  model.add(c);
35  }
36 
37  // new contig jump handling: each edge u---v can jump contigs u--x===y--v provided the lengths fit
38  void construct_jumps_for_VPair(IloModel& model,
39  const IloEnv& env,
40  var_collection_multi& vars,
41  const ScafVertexPair& uv,
42  const int uv_length,
43  const ScafVertexPairSet& target_set)
44  {
45  const Instance& I = vars.I;
46  assert(uv_length != NO_LENGTH);
47  assert(!target_set.empty());
48  assert(!I.is_matching_edge(uv));
49 
50  IloRangeArray c(env);
51  const ScafVertex& u = uv.first;
52  const ScafVertex& v = uv.second;
53  const unsigned uv_multi = I[uv].multiplicity;
54  DEBUG5(std::cout << "constructing jump for "<<I[u].name<<"->"<<I[v].name<<std::endl;)
55 
56  // for each small contig xy that we can place in the jump, create a variable that is the minimum of ux & yv
57  // then, we can take the jump as many times as the sum of all these variables
58  IloExpr overall_uv_jumps(env);
59  for(const ScafVertexPair& xy: target_set){
60  const ScafVertex& x = xy.first;
61  const ScafVertex& y = xy.second;
62  const unsigned xy_multi = I[xy].multiplicity;
63 
64  // step 1: register a variable for the number of times that uv can jump over xy
65  IloNumVar uv_jumps_xy(env, 0, xy_multi, IloNumVar::Int);
66  // step 2: constrain the number of jumps by how often ux & yv are taken
67  IloExpr used_ux(env);
68  used_ux += vars.times_used.at(ScafVertexPair(u, x));
69  used_ux -= uv_jumps_xy;
70  c.add(used_ux >= 0);
71 
72  IloExpr used_yv(env);
73  used_yv += vars.times_used.at(ScafVertexPair(y, v));
74  used_yv -= uv_jumps_xy;
75  c.add(used_yv >= 0);
76 
77  // step 3: add the number of times uv can jump over xy to the overall number of times that uv can jump
78  overall_uv_jumps += uv_jumps_xy;
79  }
80 
81  const std::string uv_jump_name((std::string)"jump_" + I[u].name + "," + I[v].name);
82  const auto uv_jump_var_iter = vars.contig_jumps.emplace(std::piecewise_construct, std::make_tuple(u,v), std::make_tuple(env, 0, uv_multi, IloNumVar::Int, uv_jump_name.c_str())).first;
83  overall_uv_jumps -= uv_jump_var_iter->second;
84  c.add(overall_uv_jumps >= 0);
85 
86  model.add(c);
87  }// function
88 
89 }}}// namespace
90 
91 #endif
Definition: read_adj_list.hpp:22
std::pair< B, A > reverse(const std::pair< A, B > &p)
reverse a pair of things, that is, turn (x,y) into (y,x)
Definition: utils.hpp:213