-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpafcoverage_main.cpp
129 lines (108 loc) · 3.25 KB
/
pafcoverage_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
#include <unistd.h>
#include <getopt.h>
#include <fstream>
#include <iostream>
#include "pafcoverage.hpp"
using namespace std;
void help(char** argv) {
cerr << "usage: " << argv[0] << " [options] <paf> [paf2] [paf3] [...]" << endl
<< "Print some PAF coverages statistics for query sequences" << endl
<< endl
<< "options: " << endl
<< " -p, --query-prefix PREFIX Only look at query sequences with given prefix" << endl
<< " -g, --print-gaps Print gaps in coverage in BED format" << endl
<< " -m, --min-gap-length N Only print gaps that are >= Nbp [default: 1]" << endl;
}
int main(int argc, char** argv) {
string query_prefix;
bool print_gaps = false;
int64_t min_gap_length = 1;
int c;
optind = 1;
while (true) {
static const struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"query-prefix", required_argument, 0, 'q'},
{"print-gaps", no_argument, 0, 'g'},
{"min-gap-length", required_argument, 0, 'm'},
{0, 0, 0, 0}
};
int option_index = 0;
c = getopt_long (argc, argv, "hp:gm:",
long_options, &option_index);
// Detect the end of the options.
if (c == -1)
break;
switch (c)
{
case 'p':
query_prefix = optarg;
break;
case 'g':
print_gaps = true;
break;
case 'm':
min_gap_length = stol(optarg);
break;
case 'h':
case '?':
/* getopt_long already printed an error message. */
help(argv);
exit(1);
break;
default:
abort ();
}
}
if (argc <= 1) {
help(argv);
return 1;
}
// Parse the positional argument
if (optind >= argc) {
cerr << "[pafcoverage] error: too few arguments" << endl;
help(argv);
return 1;
}
vector<string> in_paths;
int stdin_count = 0;
while (optind < argc) {
in_paths.push_back(argv[optind++]);
if (in_paths.back() == "-") {
++stdin_count;
}
}
if (stdin_count > 1) {
cerr << "mzgaf2paf] error: only one input can be piped with -" << endl;
return 1;
}
// coverage stats go here
CoverageMap coverage_map;
for (const string& in_path : in_paths) {
ifstream in_file;
istream* in_stream;
if (in_path == "-") {
in_stream = &cin;
} else {
in_file.open(in_path);
if (!in_file) {
cerr << "[pafcoverage] error: unable to open input: " << in_path << endl;
return 1;
}
in_stream = &in_file;
}
string buffer;
while (getline(*in_stream, buffer)) {
if (buffer.substr(0, query_prefix.length()) == query_prefix) {
update_coverage_map(buffer, coverage_map);
}
}
}
// print the bed
if (print_gaps) {
print_coverage_gaps_as_bed(coverage_map, cout, min_gap_length);
} else {
print_coverage_summary(coverage_map, cout);
}
return 0;
}