Debian Codegraph @

ext/bifrost/doc/snippets/test.cpp

raw symbols SCIP
1 #include <iostream>
2 #include <stack>
3 #include <queue>
4 #include <unordered_set>
5
6 #include <bifrost/ColoredCDBG.hpp>
7
8 using namespace std;
9
10 // Boolean class indicating if its associated unitig was "visited", "seen" or none of those two.
11 // The class inherits from CCDBG_Data_t<MyBool> to be used with ColoredCDBG and from
12 // CDBG_Data_t<MyBool> to be used with CompactedDBG.
13
14 class MyBool : public CCDBG_Data_t<MyBool>, CDBG_Data_t<MyBool> {
15
16 public:
17
18 MyBool() : b(NOT_VISITED_SEEN) {} // Initiate the boolean to "not visited"
19
20 // Clear method for CompactedDBG
21 void clear(const UnitigMap<MyBool>& um_dest){
22
23 set_not_seen_visited(); // Set the new unitig to "not seen nor visited"
24 }
25
26 // Clear method for CompactedDBG
27 void clear(const UnitigColorMap<MyBool>& um_dest){
28
29 set_not_seen_visited(); // Set the new unitig to "not seen nor visited"
30 }
31
32 // Concatenation method for ColoredCDBG
33 void concat(const UnitigColorMap<MyBool>& um_dest, const UnitigColorMap<MyBool>& um_src){
34
35 // When concatenating the reference unitig of um_src to the reference unitig of um_dest,
36 // we set the boolean of the new unitig to "not visited" because it will be a new unitig.
37
38 set_not_seen_visited(); // Set the new unitig to "not seen nor visited"
39 }
40
41 // Concatenation method for ColoredCDBG
42 void concat(const UnitigMap<MyBool>& um_dest, const UnitigMap<MyBool>& um_src){
43
44 // When concatenating the reference unitig of um_src to the reference unitig of um_dest,
45 // we set the boolean of the new unitig to "not visited" because it will be a new unitig.
46
47 set_not_seen_visited(); // Set the new unitig to "not seen nor visited"
48 }
49
50 // Extraction method for ColoredCDBG
51 void extract(const UnitigColors* uc_dest, const UnitigColorMap<MyBool>& um_src, const bool last_extraction) {
52
53 // This function creates a new unitig which is a sub-unitig from the reference unitig of um_src.
54 // The new unitig created is set to "not seen nor visited" as a measure of precaution (it is already
55 // initiated by default to "not visited" in the constructor)
56
57 set_not_seen_visited(); // Set the new unitig to "not seen nor visited"
58 }
59
60 // Extraction method for ColoredCDBG
61 void extract(const UnitigMap<MyBool>& um_src, bool last_extraction) {
62
63 // This function creates a new unitig which is a sub-unitig from the reference unitig of um_src.
64 // The new unitig created is set to "not seen nor visited" as a measure of precaution (it is already
65 // initiated by default to "not visited" in the constructor)
66
67 set_not_seen_visited(); // Set the new unitig to "not seen nor visited"
68 }
69
70 // Methods MyBool::merge() and MyBool
71
72 string toString() const {
73
74 if (is_visited()) return string("visited");
75 if (is_seen()) return string("seen");
76
77 return string("Not seen nor visited");
78 }
79
80 inline void set_visited() { b = VISITED; } // Set the boolean to "visited"
81 inline void set_seen() { b = SEEN; } // Set the boolean to "seen"
82 inline void set_not_seen_visited() { b = NOT_VISITED_SEEN; } // Set the boolean to "not seen and not visited"
83
84 inline bool is_visited() const { return (b == VISITED); } // return if the boolean is "visited"
85 inline bool is_not_visited() const { return !is_visited(); } // return if the boolean is "not visited"
86
87 inline bool is_seen() const { return (b == SEEN); } // return if the boolean is "seen"
88 inline bool is_not_seen() const { return !is_seen(); } // return if the boolean is "not seen"
89
90 private:
91
92 const static uint8_t NOT_VISITED_SEEN = 0x0;
93 const static uint8_t VISITED = 0x1;
94 const static uint8_t SEEN = 0x2;
95
96 uint8_t b;
97 };
98
99 void clearMarking(const unordered_set<UnitigColorMap<MyBool>, UnitigMapHash<DataAccessor<MyBool>, DataStorage<MyBool>>>& set_km_seen){
100
101 for (const auto& ucm : set_km_seen){
102
103 DataAccessor<MyBool>* da_ucm = ucm.getData();
104 MyBool* data_ucm = da_ucm->getData(ucm);
105
106 data_ucm->clear(ucm);
107 }
108 }
109
110 void clearMarking(ColoredCDBG<MyBool>& ccdbg){
111
112 for (const auto& unitig : ccdbg){
113
114 DataAccessor<MyBool>* da_ucm = unitig.getData();
115 MyBool* data_ucm = da_ucm->getData(unitig);
116
117 data_ucm->clear(unitig);
118 }
119 }
120
121 void BFS_Recursive(const UnitigColorMap<MyBool>& ucm){
122
123 DataAccessor<MyBool>* da = ucm.getData(); // Get DataAccessor from unitig
124 MyBool* data = da->getData(ucm); // Get boolean from DataAccessor
125
126 if (data->is_not_visited() && data->is_not_seen()){
127
128 data->set_visited(); // Set boolean to indicate unitig was visited
129
130 for (auto& successor : ucm.getSuccessors()){ // Iterate over successors of a unitig
131
132 DataAccessor<MyBool>* da_succ = successor.getData(); // Get DataAccessor from unitig successor
133 MyBool* data_succ = da_succ->getData(successor); // Get boolean from DataAccessor
134
135 if (data_succ->is_not_visited()) data_succ->set_seen(); // Set boolean to indicate unitig was seen (visited but not traversed yet)
136 }
137
138 for (auto& predecessor : ucm.getPredecessors()){ // Iterate over predecessors of a unitig
139
140 DataAccessor<MyBool>* da_pred = predecessor.getData(); // Get DataAccessor from unitig predecessor
141 MyBool* data_pred = da_pred->getData(predecessor); // Get boolean from DataAccessor
142
143 if (data_pred->is_not_visited()) data_pred->set_seen(); // Set boolean to indicate unitig was visited (visited but not traversed yet)
144 }
145
146 // Traverse successors
147 for (auto& successor : ucm.getSuccessors()){
148
149 DataAccessor<MyBool>* da_succ = successor.getData(); // Get DataAccessor from unitig successor
150 MyBool* data_succ = da_succ->getData(successor); // Get boolean from DataAccessor
151
152 if (data_succ->is_seen()){
153
154 data_succ->set_not_seen_visited();
155
156 BFS_Recursive(successor);
157 }
158 }
159
160 // Traverse predecessors
161 for (auto& predecessor : ucm.getSuccessors()){
162
163 DataAccessor<MyBool>* da_pred = predecessor.getData(); // Get DataAccessor from unitig predecessor
164 MyBool* data_pred = da_pred->getData(predecessor); // Get boolean from DataAccessor
165
166 if (data_pred->is_seen()){
167
168 data_pred->set_not_seen_visited();
169
170 BFS_Recursive(predecessor);
171 }
172 }
173 }
174 }
175
176 void BFS_Iterative(const UnitigColorMap<MyBool>& ucm){
177
178 queue<UnitigColorMap<MyBool>> q; // Create queue of unitig to traverse
179 UnitigColorMap<MyBool> ucm_tmp(ucm); // Create a non-const local copy of unitig given in parameter
180
181 DataAccessor<MyBool>* da = ucm_tmp.getData(); // Get DataAccessor from unitig
182 MyBool* data = da->getData(ucm_tmp); // Get boolean from DataAccessor
183
184 data->set_visited(); // Set boolean to indicate unitig was visited
185
186 q.push(ucm_tmp); // Push unitig to traverse on the stack
187
188 while (!q.empty()){ // While they are unitigs to traverse in the stack
189
190 ucm_tmp = q.front(); // Get unitig at the front of the queue
191
192 q.pop(); // Delete unitig at the front of the queue
193
194 for (auto& successor : ucm_tmp.getSuccessors()){ // Traverse successors
195
196 DataAccessor<MyBool>* da_succ = successor.getData(); // Get DataAccessor from successor
197 MyBool* data_succ = da_succ->getData(successor); // Get boolean from DataAccessor
198
199 if (data_succ->is_not_visited()){ // If boolean indicates the successor was not visited
200
201 data_succ->set_visited(); // Set boolean to indicate successor was visited
202
203 q.push(successor); // Traverse neighbors of successor
204 }
205 }
206
207 // Traverse predecessors
208 for (auto& predecessor : ucm_tmp.getPredecessors()){
209
210 DataAccessor<MyBool>* da_pred = predecessor.getData(); // Get DataAccessor from predecessor
211 MyBool* data_pred = da_pred->getData(predecessor); // Get boolean from DataAccessor
212
213 if (data_pred->is_not_visited()){ // If boolean indicates the predecessor was not visited
214
215 data_pred->set_visited(); // Set boolean to indicate predecessor was visited
216
217 q.push(predecessor); // Traverse neighbors of predecessor
218 }
219 }
220 }
221 }
222
223 void DFS_Recursive(const UnitigColorMap<MyBool>& ucm){
224
225 DataAccessor<MyBool>* da = ucm.getData(); // Get DataAccessor from unitig
226 MyBool* data = da->getData(ucm); // Get boolean from DataAccessor
227
228 if (data->is_not_visited()){
229
230 data->set_visited(); // Set boolean to indicate unitig was visited
231
232 for (auto& successor : ucm.getSuccessors()) DFS_Recursive(successor); // Traverse neighbors of successor
233 for (auto& predecessor : ucm.getPredecessors()) DFS_Recursive(predecessor); // Traverse neighbors of predecessor
234 }
235 }
236
237 void DFS_Iterative(const UnitigColorMap<MyBool>& ucm){
238
239 stack<UnitigColorMap<MyBool>> stck; // Create stack of unitig to traverse
240 UnitigColorMap<MyBool> ucm_tmp(ucm); // Create a non-const local copy of unitig given in parameter
241
242 stck.push(ucm_tmp); // Push first unitig to traverse on the stack
243
244 while (!stck.empty()){ // While they are unitigs to traverse in the stack
245
246 ucm_tmp = stck.top(); // Get the unitig on top of the stack
247
248 stck.pop(); // Delete unitig on the top of the stack
249
250 DataAccessor<MyBool>* da = ucm_tmp.getData(); // Get DataAccessor from unitig
251 MyBool* data = da->getData(ucm_tmp); // Get boolean from DataAccessor
252
253 if (data->is_not_visited()){ // If boolean indicates the unitig was not visited
254
255 data->set_visited(); // Set boolean to indicate unitig was visited
256
257 // Add successors to stack of unitigs to traverse
258 for (auto& successor : ucm_tmp.getSuccessors()) stck.push(successor);
259 // Add predecessors to stack of unitigs to traverse
260 for (auto& predecessor : ucm_tmp.getPredecessors()) stck.push(predecessor);
261 }
262 }
263 }
264
265 // - Parameter "iterative" indicates if you want to traverse the graph in a iterative (true)
266 // or recursive (false) way. Recursive version is limited to small components because of
267 // the stack size (recursive calls are piling up variables on your stack) which is not
268 // the case of the iterative version. Default is iterative.
269 void traverse(ColoredCDBG<MyBool>& ccdbg, const bool DFS = true, const bool iterative = true){
270
271 for (auto& unitig : ccdbg){ // Iterate over unitigs of a colored de Bruijn graph
272
273 if (iterative) DFS ? DFS_Iterative(unitig) : BFS_Iterative(unitig); // Traverse neighbors of unitig in an iterative manner
274 else DFS ? DFS_Recursive(unitig) : BFS_Recursive(unitig); // Traverse neighbors of unitig in a recursive manner
275 }
276
277 clearMarking(ccdbg);
278 }
279
280 size_t getNbConnectedComponent(ColoredCDBG<MyBool>& ccdbg){
281
282 size_t nb_cc = 0; // Number of connected components
283
284 for (auto& unitig : ccdbg){ // Iterate over unitigs of a colored de Bruijn graph
285
286 DataAccessor<MyBool>* da = unitig.getData(); // Get DataAccessor from unitig
287 MyBool* data = da->getData(unitig); // Get boolean from DataAccessor
288
289 if (data->is_not_visited()){ // If boolean indicates the unitig was not visited
290
291 ++nb_cc; // It's a new connected components
292
293 DFS_Iterative(unitig); // Traverse neighbors of unitig in DFS
294 }
295 }
296
297 clearMarking(ccdbg);
298
299 return nb_cc;
300 }
301
302 pair<UnitigColorMap<MyBool>, UnitigColorMap<MyBool>> extractSuperBubble(const UnitigColorMap<MyBool>& s){
303
304 vector<UnitigColorMap<MyBool>> vertices_visit;
305
306 pair<UnitigColorMap<MyBool>, UnitigColorMap<MyBool>> p;
307
308 unordered_set<UnitigColorMap<MyBool>, UnitigMapHash<DataAccessor<MyBool>, DataStorage<MyBool>>> set_km_seen;
309
310 UnitigColorMap<MyBool> v(s);
311
312 vertices_visit.push_back(v);
313
314 while (!vertices_visit.empty()){ // While there are vertices to visit
315
316 v = vertices_visit.back(); // Pick arbitrary vertex in the set
317
318 vertices_visit.pop_back(); // Delete that vertex from the set
319
320 DataAccessor<MyBool>* da = v.getData(); // Get DataAccessor from unitig
321 MyBool* data = da->getData(v); // Get boolean from DataAccessor
322
323 //cout << v.getUnitigHead().toString() << " - " << data->toString() << endl;
324
325 data->set_visited(); // Set boolean to indicate unitig was visited
326 set_km_seen.insert(v);
327
328 if (!v.getSuccessors().hasSuccessors()){ // Unitig is no successor, it is a tip
329
330 clearMarking(set_km_seen);
331 return p;
332 }
333
334 for (auto& u : v.getSuccessors()){
335
336 if (u == s){ // Cycle including s
337
338 clearMarking(set_km_seen);
339 return p;
340 }
341
342 DataAccessor<MyBool>* da_u = u.getData(); // Get DataAccessor from successor
343 MyBool* bool_u = da_u->getData(u); // Get boolean from DataAccessor
344
345 if (bool_u->is_not_visited()){
346
347 bool_u->set_seen(); // Set boolean to indicate unitig was seen
348 set_km_seen.insert(u);
349
350 bool all_predecessor_visited = true;
351
352 for (const auto& predecessor : u.getPredecessors()){
353
354 const DataAccessor<MyBool>* da_pred = predecessor.getData();
355 const MyBool* data_pred = da_pred->getData(predecessor);
356
357 if (data_pred->is_not_visited()){
358
359 all_predecessor_visited = false;
360 break;
361 }
362 }
363
364 if (all_predecessor_visited) vertices_visit.push_back(u);
365 }
366 else {
367
368 clearMarking(set_km_seen);
369 return p;
370 }
371 }
372
373 if (vertices_visit.size() == 1){
374
375 bool not_seen = true;
376
377 for (const auto& cucm : set_km_seen){
378
379 if (cucm != vertices_visit[0]){
380
381 const DataAccessor<MyBool>* da_cucm = cucm.getData();
382 const MyBool* data_cucm = da_cucm->getData(cucm);
383
384 if (data_cucm->is_seen()){
385
386 not_seen = false;
387 break;
388 }
389 }
390 }
391
392 if (not_seen){
393
394 for (const auto& successor : vertices_visit[0].getSuccessors()){
395
396 if (successor == s){ // cycle
397
398 clearMarking(set_km_seen);
399 return p;
400 }
401 }
402
403 p.first = s;
404 p.second = vertices_visit[0];
405
406 clearMarking(set_km_seen);
407 return p;
408 }
409 }
410 }
411
412 clearMarking(set_km_seen);
413 return p;
414 }
415
416 int main(int argc, char *argv[])
417 {
418
419 if (argc != 1){
420
421 ColoredCDBG<MyBool> ccdbg;
422
423 // Read input filenames
424 const string filename_prefix(argv[1]);
425
426 cout << "=== Reading graph ===" << endl;
427
428 if (ccdbg.read(filename_prefix, 4)){
429
430 cout << "=== Traversing graph: Depth First Search ===" << endl;
431
432 traverse(ccdbg, true);
433
434 cout << "=== Traversing graph: Breadth First Search ===" << endl;
435
436 traverse(ccdbg, false);
437
438 cout << "=== Computing number of connected components ===" << endl;
439
440 const size_t nb_cc = getNbConnectedComponent(ccdbg);
441
442 cout << nb_cc << " connected components found" << endl;
443
444 cout << "=== Computing super bubbles ===" << endl;
445
446 size_t nb_super_bubble = 0;
447 size_t nb_unitig_processed = 0;
448
449 for (const auto& unitig : ccdbg){
450
451 const pair<UnitigColorMap<MyBool>, UnitigColorMap<MyBool>> p = extractSuperBubble(unitig);
452
453 if (!p.first.isEmpty && !p.second.isEmpty) ++nb_super_bubble;
454
455 ++nb_unitig_processed;
456
457 if (nb_unitig_processed % 100000 == 0){
458
459 cout << "Processed " << nb_unitig_processed << " unitigs: " << nb_super_bubble << " super bubbles so far" << endl;
460 }
461 }
462
463 cout << nb_super_bubble << " super bubbles found" << endl;
464 }
465 else cerr << "Invalid graph filename prefix provided" << endl;
466 }
467 else cerr << "No input graph filename prefix provided" << endl;
468 }