-
Notifications
You must be signed in to change notification settings - Fork 4
/
n50.cc
152 lines (133 loc) · 4.29 KB
/
n50.cc
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
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <string>
#include <fstream>
#include <algorithm>
#include <numeric>
#include "common.hpp"
#include <n50_cmdline.hpp>
static std::vector<uint32_t> get_nsizes(const std::vector<uint32_t>& in_sizes, bool others) {
std::vector<uint32_t> res;
if(in_sizes.empty() && !others) {
res.push_back(50);
} else {
for(auto x : in_sizes) {
if(x == 0 || x >= 100)
n50_cmdline::error() << "Invalid Nx size '" << x << "': x must be in (0, 100)";
res.push_back(x);
}
}
std::sort(res.begin(), res.end());
return res;
}
void update_stats(size_t size, std::vector<size_t>& sizes, size_t& total_size, double& E) {
sizes.push_back(size);
size_t o_total_size = total_size;
total_size += size;
E = ((double)o_total_size / (double)total_size) * E + (double)(size * size) / total_size;
}
size_t read_from_fasta(std::istream& is, std::vector<size_t>& sizes, size_t& total_size, double& E) {
size_t contig_i = 0;
int c;
std::string line;
// Skip up to first header
for(c = is.peek(); c != '>' && c != EOF; c = is.peek())
skip_line(is);
while(c != EOF) {
skip_line(is); // Ignore header
size_t size = 0;
for(c = is.peek(); c != '>' && c != EOF; c = is.peek()) {
std::getline(is, line);
size += line.size();
}
++contig_i;
update_stats(size, sizes, total_size, E);
}
return contig_i;
}
size_t read_from_sizes(std::istream& is, std::vector<size_t>& sizes, size_t& total_size, double& E) {
size_t size;
size_t contig_i = 0;
std::string line;
while(is.peek() != EOF) {
std::getline(is, line);
size = std::stoul(line);
++contig_i;
update_stats(size, sizes, total_size, E);
}
return contig_i;
}
int n50_main(int argc, char *argv[]) {
n50_cmdline args(argc, argv);
if(args.file_arg.empty()) {
args.file_arg.push_back("/dev/stdin");
if(isatty(0))
std::cerr << "Warning: reading from terminal" << std::endl;
}
const std::vector<uint32_t> nsizes = get_nsizes(args.N_arg, !args.all_flag && (args.Esize_flag || args.sum_flag));
std::vector<size_t> sizes;
size_t total_size = 0;
double E = 0.0;
size_t contig_i = 0;
for(auto file : args.file_arg) {
try {
std::ifstream is;
is.exceptions(std::ios::failbit|std::ios::badbit);
is.open(file);
if(args.from_sizes_flag)
contig_i += read_from_sizes(is, sizes, total_size, E);
else
contig_i += read_from_fasta(is, sizes, total_size, E);
} catch(std::ios::failure&) {
std::cerr << "Error with file '" << file << '\'' << std::endl;
return EXIT_FAILURE;
} catch(std::runtime_error& e) {
std::cerr << "Error with file '" << file << "': " << e.what() << std::endl;
}
}
// Compute statistics
std::sort(sizes.begin(), sizes.end(), [](size_t x, size_t y) { return x > y; });
const size_t sum_size = total_size;
if(args.size_given)
total_size = args.size_arg;
std::vector<size_t> sizes_for_n;
for(auto s : nsizes) sizes_for_n.push_back(std::ceil((double)s * total_size / 100.0));
assert(sizes_for_n.size() == nsizes.size());
size_t csize = 0;
size_t nsize = 0;
for(auto s : sizes) {
csize += s;
for( ; nsize < nsizes.size() && csize >= sizes_for_n[nsize]; ++nsize) {
if(!args.no_header_flag)
std::cout << 'N' << nsizes[nsize] << ' ';
std::cout << s << '\n';
}
}
for( ; nsize < nsizes.size(); ++nsize) {
if(!args.no_header_flag)
std::cout << 'N' << nsizes[nsize] << ' ';
std::cout << "-\n";
}
if(args.sum_flag || args.all_flag) {
if(!args.no_header_flag)
std::cout << (args.terse_flag ? "S" :"Sequence") << ' ';
std::cout << sum_size << '\n';
}
if(args.average_flag || args.all_flag) {
if(!args.no_header_flag)
std::cout << (args.terse_flag ? "A" : "Average") << ' ';
std::cout << ((double)sum_size / sizes.size()) << "\n";
}
if(args.Esize_flag || args.all_flag) {
if(!args.no_header_flag)
std::cout << (args.terse_flag ? "E" : "E-size") << ' ';
std::cout << E << '\n';
}
if(args.count_flag || args.all_flag) {
if(!args.no_header_flag)
std::cout << (args.terse_flag ? "C" : "Count") << ' ';
std::cout << contig_i << '\n';
}
return EXIT_SUCCESS;
}