forked from vgteam/vg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
2899 lines (2489 loc) · 88.8 KB
/
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <fstream>
#include <ctime>
#include <getopt.h>
#include "pb2json.h"
#include "vg.hpp"
#include "vg.pb.h"
#include "vg_set.hpp"
#include "index.hpp"
#include "mapper.hpp"
#include "Variant.h"
#include "Fasta.h"
#include "stream.hpp"
#include "alignment.hpp"
#include "convert.hpp"
#include <google/protobuf/stubs/common.h>
using namespace std;
using namespace google::protobuf;
using namespace vg;
void help_surject(char** argv) {
cerr << "usage: " << argv[0] << " surject [options] <aln.gam> >[proj.cram]" << endl
<< "Transforms alignments to be relative to particular paths." << endl
<< endl
<< "options:" << endl
<< " -d, --db-name DIR use the graph in this database" << endl
<< " -t, --threads N number of threads to use" << endl
<< " -p, --into-path NAME surject into just this path" << endl
<< " -i, --into-paths FILE surject into path names listed in FILE (one per line)" << endl
<< " -P, --into-prefix NAME surject into all paths with NAME as their prefix" << endl
//<< " -H, --header-from FILE use the header in the SAM/CRAM/BAM file for the output" << endl
// todo, reenable
// << " -c, --cram-output write CRAM to stdout (default is vg::Aligment/GAM format)" << endl
// << " -f, --reference FILE use this file when writing CRAM to rebuild sequence header" << endl
<< " -b, --bam-output write BAM to stdout" << endl
<< " -s, --sam-output write SAM to stdout" << endl
<< " -C, --compression N level for compression [0-9]" << endl
<< " -w, --window N use N nodes on either side of the alignment to surject (default 5)" << endl;
}
int main_surject(int argc, char** argv) {
if (argc == 2) {
help_surject(argv);
return 1;
}
string db_name;
string path_name;
string path_prefix;
string path_file;
string output_type = "gam";
string input_type = "gam";
string header_file;
int compress_level = 9;
int window = 5;
string fasta_filename;
int c;
optind = 2; // force optind past command positional argument
while (true) {
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{"db-name", required_argument, 0, 'd'},
{"threads", required_argument, 0, 't'},
{"into-path", required_argument, 0, 'p'},
{"into-paths", required_argument, 0, 'i'},
{"into-prefix", required_argument, 0, 'P'},
{"cram-output", no_argument, 0, 'c'},
{"reference", required_argument, 0, 'f'},
{"bam-output", no_argument, 0, 'b'},
{"sam-output", no_argument, 0, 's'},
{"header-from", required_argument, 0, 'H'},
{"compress", required_argument, 0, 'C'},
{"window", required_argument, 0, 'w'},
{0, 0, 0, 0}
};
int option_index = 0;
c = getopt_long (argc, argv, "hd:p:i:P:cbsH:C:t:w:f:",
long_options, &option_index);
// Detect the end of the options.
if (c == -1)
break;
switch (c)
{
case 'd':
db_name = optarg;
break;
case 'p':
path_name = optarg;
break;
case 'i':
path_file = optarg;
break;
case 'P':
path_prefix = optarg;
break;
case 'H':
header_file = optarg;
break;
case 'c':
output_type = "cram";
break;
case 'f':
fasta_filename = optarg;
break;
case 'b':
output_type = "bam";
break;
case 's':
compress_level = -1;
output_type = "sam";
break;
case 't':
omp_set_num_threads(atoi(optarg));
break;
case 'C':
compress_level = atoi(optarg);
break;
case 'w':
window = atoi(optarg);
break;
case 'h':
case '?':
help_surject(argv);
exit(1);
break;
default:
abort ();
}
}
string file_name = argv[optind];
Index index;
// open index
index.open_read_only(db_name);
set<string> path_names;
if (!path_file.empty()){
// open the file
ifstream in(path_file);
string line;
while (std::getline(in,line)) {
path_names.insert(line);
}
} else {
path_names.insert(path_name);
}
if (input_type == "gam") {
if (output_type == "gam") {
int thread_count = get_thread_count();
vector<vector<Alignment> > buffer;
buffer.resize(thread_count);
function<void(Alignment&)> lambda = [&index, &path_names, &buffer, &window](Alignment& src) {
int tid = omp_get_thread_num();
Alignment surj;
string path_name;
int64_t path_pos;
index.surject_alignment(src, path_names, surj, path_name, path_pos, window);
buffer[tid].push_back(surj);
stream::write_buffered(cout, buffer[tid], 1000);
};
if (file_name == "-") {
stream::for_each_parallel(std::cin, lambda);
} else {
ifstream in;
in.open(file_name.c_str());
stream::for_each_parallel(in, lambda);
}
for (int i = 0; i < thread_count; ++i) {
stream::write_buffered(cout, buffer[i], 0); // flush
}
} else {
char out_mode[5];
string out_format = "";
strcpy(out_mode, "w");
if (output_type == "bam") { out_format = "b"; }
else if (output_type == "cram") { out_format = "c"; }
else { out_format = ""; }
strcat(out_mode, out_format.c_str());
if (compress_level >= 0) {
char tmp[2];
tmp[0] = compress_level + '0'; tmp[1] = '\0';
strcat(out_mode, tmp);
}
// get the header
/*
if (header_file.empty()) {
cerr << "[vg surject] error: --header-from must be specified for SAM/BAM/CRAM output" << endl;
return 1;
}
*/
string header;
map<string, int64_t> path_by_id = index.paths_by_id();
map<string, pair<int64_t, int64_t> > path_layout;
map<string, int64_t> path_length;
index.path_layout(path_layout, path_length);
int thread_count = get_thread_count();
vector<vector<tuple<string, int64_t, Alignment> > > buffer;
buffer.resize(thread_count);
map<string, string> rg_sample;
// bam/sam/cram output
samFile* out = 0;
int buffer_limit = 100;
bam_hdr_t* hdr = NULL;
int64_t count = 0;
omp_lock_t output_lock;
omp_init_lock(&output_lock);
// handles buffers, possibly opening the output file if we're on the first record
auto handle_buffer =
[&hdr, &header, &path_length, &rg_sample, &buffer_limit,
&out_mode, &out, &output_lock, &fasta_filename](vector<tuple<string, int64_t, Alignment> >& buf) {
if (buf.size() >= buffer_limit) {
// do we have enough data to open the file?
#pragma omp critical (hts_header)
{
if (!hdr) {
hdr = hts_string_header(header, path_length, rg_sample);
if ((out = sam_open("-", out_mode)) == 0) {
/*
if (!fasta_filename.empty()) {
string fai_filename = fasta_filename + ".fai";
hts_set_fai_filename(out, fai_filename.c_str());
}
*/
cerr << "[vg surject] failed to open stdout for writing HTS output" << endl;
exit(1);
} else {
// write the header
if (sam_hdr_write(out, hdr) != 0) {
cerr << "[vg surject] error: failed to write the SAM header" << endl;
}
}
}
}
// try to get a lock, and force things if we've built up a huge buffer waiting
if (omp_test_lock(&output_lock) || buf.size() > 10*buffer_limit) {
for (auto& s : buf) {
auto& path_nom = get<0>(s);
auto& path_pos = get<1>(s);
auto& surj = get<2>(s);
string cigar = cigar_against_path(surj);
bam1_t* b = alignment_to_bam(header,
surj,
path_nom,
path_pos,
cigar,
"=",
path_pos,
0);
int r = 0;
#pragma omp critical (cout)
r = sam_write1(out, hdr, b);
if (r == 0) { cerr << "[vg surject] error: writing to stdout failed" << endl; exit(1); }
bam_destroy1(b);
}
omp_unset_lock(&output_lock);
buf.clear();
}
}
};
function<void(Alignment&)> lambda = [&index,
&path_names,
&path_length,
&window,
&rg_sample,
&header,
&out,
&buffer,
&count,
&hdr,
&out_mode,
&handle_buffer](Alignment& src) {
int tid = omp_get_thread_num();
Alignment surj;
string path_name;
int64_t path_pos;
index.surject_alignment(src, path_names, surj, path_name, path_pos, window);
if (!surj.path().mapping_size()) {
surj = src;
}
// record
if (!hdr && !surj.read_group().empty() && !surj.sample_name().empty()) {
#pragma omp critical (hts_header)
rg_sample[surj.read_group()] = surj.sample_name();
}
buffer[tid].push_back(make_tuple(path_name, path_pos, surj));
handle_buffer(buffer[tid]);
};
// now apply the alignment processor to the stream
if (file_name == "-") {
stream::for_each_parallel(std::cin, lambda);
} else {
ifstream in;
in.open(file_name.c_str());
stream::for_each_parallel(in, lambda);
}
buffer_limit = 0;
for (auto& buf : buffer) {
handle_buffer(buf);
}
bam_hdr_destroy(hdr);
sam_close(out);
omp_destroy_lock(&output_lock);
}
}
cout.flush();
return 0;
}
void help_mod(char** argv) {
cerr << "usage: " << argv[0] << " mod [options] <graph.vg> >[mod.vg]" << endl
<< "Modifies graph, outputs modified on stdout." << endl
<< endl
<< "options:" << endl
<< " -i, --include-aln FILE include the paths implied by alignments in the graph" << endl
<< " -c, --compact-ids should we sort and compact the id space? (default false)" << endl
<< " -k, --keep-path NAME keep only nodes and edges in the path" << endl
<< " -o, --remove-orphans remove orphan edges from graph (edge specified but node missing)" << endl;
}
int main_mod(int argc, char** argv) {
if (argc == 2) {
help_mod(argv);
return 1;
}
string path_name;
bool remove_orphans = false;
string aln_file;
bool compact_ids = false;
int c;
optind = 2; // force optind past command positional argument
while (true) {
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{"include-aln", required_argument, 0, 'i'},
{"compact-ids", no_argument, 0, 'c'},
{"keep-path", required_argument, 0, 'k'},
{"remove-orphans", no_argument, 0, 'o'},
{0, 0, 0, 0}
};
int option_index = 0;
c = getopt_long (argc, argv, "hk:oi:c",
long_options, &option_index);
// Detect the end of the options.
if (c == -1)
break;
switch (c)
{
case 'i':
aln_file = optarg;
break;
case 'c':
compact_ids = true;
break;
case 'k':
path_name = optarg;
break;
case 'o':
remove_orphans = true;
break;
case 'h':
case '?':
help_mod(argv);
exit(1);
break;
default:
abort ();
}
}
VG* graph;
string file_name = argv[optind];
if (file_name == "-") {
graph = new VG(std::cin);
} else {
ifstream in;
in.open(file_name.c_str());
graph = new VG(in);
}
if (!path_name.empty()) {
graph->keep_path(path_name);
}
if (remove_orphans) {
graph->remove_orphan_edges();
}
if (!aln_file.empty()) {
map<int64_t, vector<Mapping> > mappings; // keyed by id
function<void(Alignment&)> lambda = [&graph, &mappings](Alignment& aln) {
const Path& path = aln.path();
for (int i = 0; i < path.mapping_size(); ++i) {
const Mapping& mapping = path.mapping(i);
mappings[mapping.position().node_id()].push_back(mapping);
}
};
if (aln_file == "-") {
stream::for_each(std::cin, lambda);
} else {
ifstream in;
in.open(aln_file.c_str());
stream::for_each(in, lambda);
}
// now that everything is sorted, execute the edits
graph->edit(mappings);
// and optionally compact ids
if (compact_ids) {
graph->sort();
graph->compact_ids();
}
}
graph->serialize_to_ostream(std::cout);
delete graph;
return 0;
}
void help_sim(char** argv) {
cerr << "usage: " << argv[0] << " sim [options] <graph.vg>" << endl
<< "Simulates reads from the graph(s). Output is a list of reads." << endl
<< endl
<< "options:" << endl
<< " -l, --read-length N write reads of length N" << endl
<< " -n, --num-reads N simulate N reads" << endl
<< " -s, --random-seed N use this specific seed for the PRNG" << endl
<< " -e, --base-error N base substitution error rate (default 0.0)" << endl
<< " -i, --indel-error N indel error rate (default 0.0)" << endl;
}
int main_sim(int argc, char** argv) {
if (argc == 2) {
help_sim(argv);
return 1;
}
int read_length = 100;
int num_reads = 1;
int seed_val = time(NULL);
double base_error = 0;
double indel_error = 0;
int c;
optind = 2; // force optind past command positional argument
while (true) {
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{"read-length", required_argument, 0, 'l'},
{"num-reads", required_argument, 0, 'n'},
{"random-seed", required_argument, 0, 's'},
{0, 0, 0, 0}
};
int option_index = 0;
c = getopt_long (argc, argv, "hl:n:s:e:i:",
long_options, &option_index);
// Detect the end of the options.
if (c == -1)
break;
switch (c)
{
case 'l':
read_length = atoi(optarg);
break;
case 'n':
num_reads = atoi(optarg);
break;
case 's':
seed_val = atoi(optarg);
break;
case 'e':
base_error = atof(optarg);
break;
case 'i':
indel_error = atof(optarg);
break;
case 'h':
case '?':
help_sim(argv);
exit(1);
break;
default:
abort ();
}
}
VG* graph;
string file_name = argv[optind];
if (file_name == "-") {
graph = new VG(std::cin);
} else {
ifstream in;
in.open(file_name.c_str());
graph = new VG(in);
}
int64_t max_id = graph->max_node_id();
int64_t min_id = graph->min_node_id();
mt19937 rng;
rng.seed(seed_val);
string bases = "ATGC";
uniform_real_distribution<double> rprob(0, 1);
uniform_int_distribution<int> rbase(0, 3);
function<string(const string&)> introduce_read_errors
= [&rng, &rprob, &rbase, &bases, base_error, indel_error](const string& perfect_read) {
if (base_error == 0 && indel_error == 0) return perfect_read;
string read;
for (auto c : perfect_read) {
if (rprob(rng) <= base_error) {
// pick another base than what c is
char e;
do {
e = bases[rbase(rng)];
} while (e == c);
c = e;
}
if (rprob(rng) <= indel_error) {
if (rprob(rng) < 0.5) {
read.push_back(bases[rbase(rng)]);
} // else do nothing, == deletion of base
} else {
read.push_back(c);
}
}
return read;
};
for (int i = 0; i < num_reads; ++i) {
string perfect_read = graph->random_read(read_length, rng, min_id, max_id, true);
// avoid short reads at the end of the graph by retrying
int iter = 0;
while (perfect_read.size() < read_length && ++iter < 1000) {
perfect_read = graph->random_read(read_length, rng, min_id, max_id, true);
// if we can't make a suitable read in 1000 tries, then maybe the graph is too small?
}
if (iter == 1000) {
cerr << "couldn't simulate read, perhaps the chosen length is too long for this graph?" << endl;
} else {
// apply errors
string readseq = introduce_read_errors(perfect_read);
cout << readseq << endl;
}
}
delete graph;
return 0;
}
void help_kmers(char** argv) {
cerr << "usage: " << argv[0] << " kmers [options] <graph1.vg> [graph2.vg ...] >kmers.tsv" << endl
<< "Generates kmers of the graph(s). Output is: kmer id pos" << endl
<< endl
<< "options:" << endl
<< " -k, --kmer-size N print kmers of size N in the graph" << endl
<< " -e, --edge-max N cross no more than N edges when determining k-paths" << endl
<< " -j, --kmer-stride N step distance between succesive kmers in paths (default 1)" << endl
<< " -t, --threads N number of threads to use" << endl
<< " -d, --allow-dups don't filter out duplicated kmers" << endl
<< " -n, --allow-negs don't filter out relative negative positions of kmers" << endl
<< " -g, --gcsa-out output a table suitable for input to GCSA2" << endl
<< " kmer, starting position, previous characters," << endl
<< " successive characters, successive positions" << endl
<< " -p, --progress show progress" << endl;
}
int main_kmers(int argc, char** argv) {
if (argc == 2) {
help_kmers(argv);
return 1;
}
int kmer_size = 0;
int edge_max = 0;
int kmer_stride = 1;
bool show_progress = false;
bool gcsa_out = false;
bool allow_dups = false;
bool allow_negs = false;
int c;
optind = 2; // force optind past command positional argument
while (true) {
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{"kmer-size", required_argument, 0, 'k'},
{"kmer-stride", required_argument, 0, 'j'},
{"edge-max", required_argument, 0, 'e'},
{"threads", required_argument, 0, 't'},
{"gcsa-out", no_argument, 0, 'g'},
{"allow-dups", no_argument, 0, 'd'},
{"allow-negs", no_argument, 0, 'n'},
{"progress", no_argument, 0, 'p'},
{0, 0, 0, 0}
};
int option_index = 0;
c = getopt_long (argc, argv, "hk:j:pt:e:gdn",
long_options, &option_index);
// Detect the end of the options.
if (c == -1)
break;
switch (c)
{
case 'k':
kmer_size = atoi(optarg);
break;
case 'j':
kmer_stride = atoi(optarg);
break;
case 'e':
edge_max = atoi(optarg);
break;
case 't':
omp_set_num_threads(atoi(optarg));
break;
case 'g':
gcsa_out = true;
break;
case 'd':
allow_dups = true;
break;
case 'n':
allow_negs = true;
break;
case 'p':
show_progress = true;
break;
case 'h':
case '?':
help_kmers(argv);
exit(1);
break;
default:
abort ();
}
}
if (edge_max == 0) edge_max = kmer_size + 1;
vector<string> graph_file_names;
while (optind < argc) {
string file_name = argv[optind++];
graph_file_names.push_back(file_name);
}
VGset graphs(graph_file_names);
graphs.show_progress = show_progress;
if (gcsa_out) {
graphs.write_gcsa_out(cout, kmer_size, edge_max, kmer_stride);
} else {
function<void(string&, Node*, int, list<Node*>&, VG& graph)>
lambda = [](string& kmer, Node* n, int p, list<Node*>& path, VG& graph) {
#pragma omp critical (cout)
cout << kmer << '\t' << n->id() << '\t' << p << '\n';
};
graphs.for_each_kmer_parallel(lambda, kmer_size, edge_max, kmer_stride, allow_dups, allow_negs);
}
cout.flush();
return 0;
}
void help_concat(char** argv) {
cerr << "usage: " << argv[0] << " concat [options] <graph1.vg> [graph2.vg ...] >merged.vg" << endl
<< "Concatenates graphs in order by adding edges from the tail nodes of the" << endl
<< "predecessor to the head nodes of the following graph. Node IDs are" << endl
<< "compacted, so care should be taken if consistent IDs are required." << endl;
}
int main_concat(int argc, char** argv) {
if (argc == 2) {
help_concat(argv);
return 1;
}
int c;
optind = 2; // force optind past command positional argument
while (true) {
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
int option_index = 0;
c = getopt_long (argc, argv, "h",
long_options, &option_index);
// Detect the end of the options.
if (c == -1)
break;
switch (c)
{
case 'h':
case '?':
help_concat(argv);
exit(1);
break;
default:
abort ();
}
}
list<VG*> graphs;
while (optind < argc) {
VG* graph;
string file_name = argv[optind++];
if (file_name == "-") {
graph = new VG(std::cin);
} else {
ifstream in;
in.open(file_name.c_str());
graph = new VG(in);
}
graphs.push_back(graph);
}
VG merged;
for (list<VG*>::iterator g = graphs.begin(); g != graphs.end(); ++g) {
merged.append(**g);
}
// output
merged.serialize_to_ostream(std::cout);
return 0;
}
void help_ids(char** argv) {
cerr << "usage: " << argv[0] << " ids [options] <graph1.vg> [graph2.vg ...] >new.vg" << endl
<< "options:" << endl
<< " -c, --compact minimize the space of integers used by the ids" << endl
<< " -i, --increment N increase ids by N" << endl
<< " -d, --decrement N decrease ids by N" << endl
<< " -j, --join make a joint id space for all the graphs that are supplied" << endl
<< " by iterating through the supplied graphs and incrementing" << endl
<< " their ids to be non-conflicting" << endl;
}
int main_ids(int argc, char** argv) {
if (argc == 2) {
help_ids(argv);
return 1;
}
bool join = false;
bool compact = false;
int64_t increment = 0;
int64_t decrement = 0;
int c;
optind = 2; // force optind past command positional argument
while (true) {
static struct option long_options[] =
{
{"compact", no_argument, 0, 'c'},
{"increment", required_argument, 0, 'i'},
{"decrement", required_argument, 0, 'd'},
{"join", no_argument, 0, 'j'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
int option_index = 0;
c = getopt_long (argc, argv, "hci:d:j",
long_options, &option_index);
// Detect the end of the options.
if (c == -1)
break;
switch (c)
{
case 'c':
compact = true;
break;
case 'i':
increment = atoi(optarg);
break;
case 'd':
decrement = atoi(optarg);
break;
case 'j':
join = true;
break;
case 'h':
case '?':
help_ids(argv);
exit(1);
break;
default:
abort ();
}
}
if (!join) {
VG* graph;
string file_name = argv[optind];
if (file_name == "-") {
graph = new VG(std::cin);
} else {
ifstream in;
in.open(file_name.c_str());
graph = new VG(in);
}
if (compact) {
graph->compact_ids();
}
if (increment != 0) {
graph->increment_node_ids(increment);
}
if (decrement != 0) {
graph->decrement_node_ids(decrement);
}
graph->serialize_to_ostream(std::cout);
delete graph;
} else {
vector<string> graph_file_names;
while (optind < argc) {
VG* graph;
string file_name = argv[optind++];
graph_file_names.push_back(file_name);
}
VGset graphs(graph_file_names);
graphs.merge_id_space();
}
return 0;
}
void help_join(char** argv) {
cerr << "usage: " << argv[0] << " join [options] <graph1.vg> [graph2.vg ...] >joined.vg" << endl
<< "Joins graphs and sub-graphs into a single variant graph by connecting their" << endl
<< "heads to a single root node with sequence 'N'." << endl
<< "Assumes a single id namespace for all graphs to join." << endl;
}
int main_join(int argc, char** argv) {
if (argc == 2) {
help_join(argv);
return 1;
}
int c;
optind = 2; // force optind past command positional argument
while (true) {
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
int option_index = 0;
c = getopt_long (argc, argv, "h",
long_options, &option_index);
// Detect the end of the options.
if (c == -1)
break;
switch (c)
{
case 'h':
case '?':
help_join(argv);
exit(1);
break;
default:
abort ();
}
}
list<VG*> graphs;
while (optind < argc) {
VG* graph;
string file_name = argv[optind++];
if (file_name == "-") {
graph = new VG(std::cin);
} else {
ifstream in;
in.open(file_name.c_str());
graph = new VG(in);
}
graphs.push_back(graph);
}
VG joined;
for (list<VG*>::iterator g = graphs.begin(); g != graphs.end(); ++g) {
joined.extend(**g);
}
// combine all subgraphs
joined.join_heads();
// output
joined.serialize_to_ostream(std::cout);
return 0;
}
void help_stats(char** argv) {
cerr << "usage: " << argv[0] << " stats [options] <graph.vg>" << endl