-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathsnarls_main.cpp
277 lines (221 loc) · 8.59 KB
/
snarls_main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// snarl_main.cpp: define the "vg snarls" subcommand, which outputs snarls and bubbles
#include <omp.h>
#include <unistd.h>
#include <getopt.h>
#include <list>
#include <fstream>
#include <regex>
#include "subcommand.hpp"
#include "../vg.hpp"
#include "vg.pb.h"
#include "../traversal_finder.hpp"
#include "../stream.hpp"
using namespace std;
using namespace vg;
using namespace vg::subcommand;
void help_snarl(char** argv) {
cerr << "usage: " << argv[0] << " snarls [options] graph.vg > snarls.pb" << endl
<< " By default, a list of protobuf Snarls is written" << endl
<< "options:" << endl
<< " -p, --pathnames output variant paths as SnarlTraversals to STDOUT" << endl
<< " -r, --traversals FILE output SnarlTraversals for ultrabubbles." << endl
<< " -l, --leaf-only restrict traversals to leaf ultrabubbles." << endl
<< " -o, --top-level restrict traversals to top level ultrabubbles" << endl
<< " -m, --max-nodes N only compute traversals for snarls with <= N nodes [10]" << endl
<< " -t, --include-trivial report snarls that consist of a single edge" << endl
<< " -s, --sort-snarls return snarls in sorted order by node ID (for topologically ordered graphs)" << endl;
}
int main_snarl(int argc, char** argv) {
if (argc == 2) {
help_snarl(argv);
return 1;
}
static const int buffer_size = 100;
string traversal_file;
bool leaf_only = false;
bool top_level_only = false;
int max_nodes = 10;
bool filter_trivial_snarls = true;
bool sort_snarls = false;
bool fill_path_names = false;
int c;
optind = 2; // force optind past command positional argument
while (true) {
static struct option long_options[] =
{
{"traversals", required_argument, 0, 'r'},
{"pathnames", no_argument, 0, 'p'},
{"leaf-only", no_argument, 0, 'l'},
{"top-level", no_argument, 0, 'o'},
{"max-nodes", required_argument, 0, 'm'},
{"include-trivial", no_argument, 0, 't'},
{"sort-snarls", no_argument, 0, 's'},
{0, 0, 0, 0}
};
int option_index = 0;
c = getopt_long (argc, argv, "sr:ltopm:h?",
long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
break;
switch (c)
{
case 'r':
traversal_file = optarg;
break;
case 'l':
leaf_only = true;
break;
case 'o':
top_level_only = true;
break;
case 'm':
max_nodes = parse<int>(optarg);
break;
case 't':
filter_trivial_snarls = false;
break;
case 's':
sort_snarls = true;
break;
case 'p':
fill_path_names = true;
break;
case 'h':
case '?':
/* getopt_long already printed an error message. */
help_snarl(argv);
exit(1);
break;
default:
abort ();
}
}
// Prepare traversal output stream
ofstream trav_stream;
if (!traversal_file.empty()) {
trav_stream.open(traversal_file);
if (!trav_stream) {
cerr << "error:[vg snarl]: Could not open \"" << traversal_file
<< "\" for writing" << endl;
return 1;
}
}
// Read the graph
VG* graph;
get_input_file(optind, argc, argv, [&](istream& in) {
graph = new VG(in);
});
if (graph == nullptr) {
cerr << "error:[vg snarl]: Could not load graph" << endl;
exit(1);
}
// The only implemented snarl finder:
SnarlFinder* snarl_finder = new CactusSnarlFinder(*graph);
// Load up all the snarls
SnarlManager snarl_manager = snarl_finder->find_snarls();
vector<const Snarl*> snarl_roots = snarl_manager.top_level_snarls();
if (fill_path_names){
TraversalFinder* trav_finder = new PathBasedTraversalFinder(*graph, snarl_manager);
for (const Snarl* snarl : snarl_roots ){
if (filter_trivial_snarls) {
auto contents = snarl_manager.shallow_contents(snarl, *graph, false);
if (contents.first.empty()) {
// Nothing but the boundary nodes in this snarl
continue;
}
}
vector<SnarlTraversal> travs = trav_finder->find_traversals(*snarl);
stream::write_buffered(cout, travs, 0);
}
delete trav_finder;
delete snarl_finder;
delete graph;
exit(0);
}
TraversalFinder* trav_finder = new ExhaustiveTraversalFinder(*graph, snarl_manager);
// Sort the top level Snarls
if (sort_snarls) {
// Ensure that all snarls are stored in sorted order
list<const Snarl*> snarl_stack;
for (const Snarl* root : snarl_roots) {
snarl_stack.push_back(root);
while (!snarl_stack.empty()) {
const Snarl* snarl = snarl_stack.back();
snarl_stack.pop_back();
if (snarl->start().node_id() > snarl->end().node_id()) {
snarl_manager.flip(snarl);
}
for (const Snarl* child_snarl : snarl_manager.children_of(snarl)) {
snarl_stack.push_back(child_snarl);
}
}
}
// Sort the snarls by node ID
std::sort(snarl_roots.begin(), snarl_roots.end(), [](const Snarl* snarl_1, const Snarl* snarl_2) {
return snarl_1->start().node_id() < snarl_2->end().node_id();
});
}
// Protobuf output buffers
vector<Snarl> snarl_buffer;
vector<SnarlTraversal> traversal_buffer;
list<const Snarl*> stack;
for (const Snarl* root : snarl_roots) {
stack.push_back(root);
while (!stack.empty()) {
const Snarl* snarl = stack.back();
stack.pop_back();
if (filter_trivial_snarls) {
auto contents = snarl_manager.shallow_contents(snarl, *graph, false);
if (contents.first.empty()) {
// Nothing but the boundary nodes in this snarl
continue;
}
}
// Write our snarl tree
snarl_buffer.push_back(*snarl);
stream::write_buffered(cout, snarl_buffer, buffer_size);
// Optionally write our traversals
if (!traversal_file.empty() && snarl->type() == ULTRABUBBLE &&
(!leaf_only || snarl_manager.is_leaf(snarl)) &&
(!top_level_only || snarl_manager.is_root(snarl)) &&
(snarl_manager.deep_contents(snarl, *graph, true).first.size() < max_nodes)) {
#ifdef debug
cerr << "Look for traversals of " << pb2json(*snarl) << endl;
#endif
vector<SnarlTraversal> travs = trav_finder->find_traversals(*snarl);
#ifdef debug
cerr << "Found " << travs.size() << endl;
#endif
traversal_buffer.insert(traversal_buffer.end(), travs.begin(), travs.end());
stream::write_buffered(trav_stream, traversal_buffer, buffer_size);
}
// Sort the child snarls by node ID?
if (sort_snarls) {
vector<const Snarl*> children = snarl_manager.children_of(snarl);
std::sort(children.begin(), children.end(), [](const Snarl* snarl_1, const Snarl* snarl_2) {
return snarl_1->start().node_id() < snarl_2->end().node_id();
});
for (const Snarl* child_snarl : children) {
stack.push_back(child_snarl);
}
}
else {
for (const Snarl* child_snarl : snarl_manager.children_of(snarl)) {
stack.push_back(child_snarl);
}
}
}
}
// flush
stream::write_buffered(cout, snarl_buffer, 0);
if (!traversal_file.empty()) {
stream::write_buffered(trav_stream, traversal_buffer, 0);
}
delete snarl_finder;
delete trav_finder;
delete graph;
return 0;
}
// Register subcommand
static Subcommand vg_snarl("snarls", "compute snarls and their traversals", main_snarl);