Scaffolding  0.1
This program can assemble genome scaffolds using the pairing information in paired-end reads.
utils.hpp
Go to the documentation of this file.
1 
6 #ifndef UTILS_HPP
7 #define UTILS_HPP
8 
9 #include <iostream>
10 #include <string>
11 #include <cassert>
12 #include <bitset>
13 #include <boost/unordered_set.hpp>
14 #include <boost/unordered_map.hpp>
15 
16 #define default_buckets boost::unordered::detail::default_bucket_count
17 
18 
19 
20 #define byte unsigned char
21 #define SIZE_T_BITS (unsigned)(8*sizeof(size_t))
22 // avoid the clutter for map-emplaces where both constructors take just one argument
23 #define DEEP_EMPLACE(x,y) emplace(std::piecewise_construct, std::make_tuple(x), std::make_tuple(y))
24 // avoid the clutter for map-emplaces where the target is default constructed
25 #define DEFAULT_EMPLACE(x) emplace(std::piecewise_construct, std::make_tuple(x), std::make_tuple())
26 
27 
28 #ifdef LONG_WIDTH
29  #define MAX_TW (unsigned char)LONG_WIDTH
30 #else
31  #define MAX_TW (unsigned char)64
32 #endif
33 
34 // on debuglevel 3 all DEBUG1, DEBUG2, and DEBUG3 statements are evaluated
35 #ifndef NDEBUG
36  #ifndef debuglevel
37  #define debuglevel 5
38  #endif
39 #else
40  #define debuglevel 0
41 #endif
42 
43 #if debuglevel > 0
44 #define DEBUG1(x) x
45 #else
46 #define DEBUG1(x)
47 #endif
48 
49 #if debuglevel > 1
50 #define DEBUG2(x) x
51 #else
52 #define DEBUG2(x)
53 #endif
54 
55 #if debuglevel > 2
56 #define DEBUG3(x) x
57 #else
58 #define DEBUG3(x)
59 #endif
60 
61 #if debuglevel > 3
62 #define DEBUG4(x) x
63 #else
64 #define DEBUG4(x)
65 #endif
66 
67 #if debuglevel > 4
68 #define DEBUG5(x) x
69 #else
70 #define DEBUG5(x)
71 #endif
72 
73 #if debuglevel > 5
74 #define DEBUG6(x) x
75 #else
76 #define DEBUG6(x)
77 #endif
78 
79 #include "unordered_pair.hpp"
80 
82 template <typename ElementA, typename ElementB>
83 bool pareto_le(const std::pair<ElementA,ElementB>& p1, const std::pair<ElementA,ElementB>& p2)
84 {
85  return (p1.first <= p2.first) && (p1.second <= p2.second);
86 }
87 
96 {
97  const size_t max_x;
98  const size_t max_y;
99  size_t x;
100  size_t y;
101 
102  // construct
103  diagonal_counter(const size_t _max_x, const size_t _max_y): max_x(_max_x), max_y(_max_y), x(0), y(0) {}
104  diagonal_counter(const size_t _max_x, const size_t _max_y, const size_t _x, const size_t _y): max_x(_max_x), max_y(_max_y), x(_x), y(_y) {}
105 
107  bool step_forward(){
108  if(at_end()) return false;
109  // if we are at the border...
110  if((x == max_x) || (y == 0)){
111  // then increase the sum and reset x & y to the other side of the matrix
112  const size_t new_sum = x + y + 1;
113  x = (max_y <= new_sum) ? new_sum - max_y : 0;
114  y = new_sum - x;
115  } else {
116  // otherwise just go through the diagonal we're on
117  x++;
118  y--;
119  }
120  return true;
121  }
122 
124  bool step_back(){
125  if(at_beginning()) return false;
126  // if we are at the border...
127  if((y == max_y) || (x == 0)){
128  // then decrease the sum and reset x & y to the other side of the matrix
129  const size_t new_sum = x + y - 1;
130  y = (max_x <= new_sum) ? new_sum - max_x : 0;
131  x = new_sum - y;
132  } else {
133  // otherwise just go through the diagonal we're on
134  x--;
135  y++;
136  }
137  return true;
138  }
139 
141  bool at_beginning(){
142  return (x == 0) && (y == 0);
143  }
145  bool at_end(){
146  return (x == max_x) && (y == max_y);
147  }
148 };
149 
150 
152 template<typename Element>
153 std::ostream& operator<<(std::ostream& os, const std::list<Element>& lst)
154 {
155  for(auto i : lst) os << i << " ";
156  return os;
157 }
159 template<typename Element1, typename Element2>
160 std::ostream& operator<<(std::ostream& os, const boost::unordered_map<Element1, Element2>& map)
161 {
162  for(auto i : map) os << i << " ";
163  return os;
164 }
165 
167 
170 template <class Set, typename Element>
171 inline bool contains(const Set& s, const Element& el)
172 {
173  return s.find(el) != s.cend();
174 }
175 
177 template<typename T>
178 size_t hash_value(const boost::unordered_set<T>& S)
179 {
180  size_t result = 0;
181  for(const auto& i : S)
182  result = (result << 1) ^ hash_value(i);
183  return result;
184 }
185 
187 
188 template<typename T>
189 void operator&=(boost::unordered_set<T>& S1, const boost::unordered_set<T>& S2)
190 {
191  for(typename boost::unordered_set<T>::iterator i = S1.begin(); i != S1.end();)
192  if(!contains(S2, *i)) i = S1.erase(i); else ++i;
193 }
194 
196 
197 template<typename T>
198 void operator^=(boost::unordered_set<T>& S1, const boost::unordered_set<T>& S2)
199 {
200  for(typename boost::unordered_set<T>::iterator i = S1.begin(); i != S1.end();)
201  if(contains(S2, *i)) i = S1.erase(i); else ++i;
202  for(typename boost::unordered_set<T>::iterator i = S2.begin(); i != S2.end(); ++i) S1.emplace(*i);
203 }
204 
206 inline bool file_exists(const std::string& filename)
207 {
208  return std::ifstream(filename.c_str()).good();
209 }
210 
212 template<typename A, typename B>
213 inline std::pair<B,A> reverse(const std::pair<A,B>& p)
214 {
215  return std::pair<B,A>(p.second, p.first);
216 }
217 
218 
219 
220 #endif
bool pareto_le(const std::pair< ElementA, ElementB > &p1, const std::pair< ElementA, ElementB > &p2)
return whether a pair is pareto-smaller than another pair
Definition: utils.hpp:83
void operator^=(boost::unordered_set< T > &S1, const boost::unordered_set< T > &S2)
symmetric set difference for unordered sets
Definition: utils.hpp:198
size_t x
current x coordinate
Definition: utils.hpp:99
bool step_back()
advance backwrd (SW) through the matrix
Definition: utils.hpp:124
bool file_exists(const std::string &filename)
testing whether a file exists by trying to open it
Definition: utils.hpp:206
bool at_end()
check if we're at the end, that is (#columns, #rows)
Definition: utils.hpp:145
const size_t max_y
number of rows in the matrix
Definition: utils.hpp:98
const size_t max_x
number of columns in the matrix
Definition: utils.hpp:97
size_t hash_value(const boost::unordered_set< T > &S)
a hash computation for an unordered set, XORing its members
Definition: utils.hpp:178
bool step_forward()
advance forward (NE) through the matrix
Definition: utils.hpp:107
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
void operator&=(boost::unordered_set< T > &S1, const boost::unordered_set< T > &S2)
set intersection for unordered sets
Definition: utils.hpp:189
Definition: utils.hpp:95
bool at_beginning()
check if we're at the beginning, that is (0,0)
Definition: utils.hpp:141
bool contains(const Set &s, const Element &el)
a more readable containment check
Definition: utils.hpp:171
size_t y
current y coordinate
Definition: utils.hpp:100