Repository navigation
Expand file tree
/
Copy pathxcounts.cpp
More file actions
298 lines (263 loc) · 9.77 KB
/
Copy pathxcounts.cpp
File metadata and controls
298 lines (263 loc) · 9.77 KB
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
/* Copyright (C) 2023 Andrew D. Smith
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*/
[[maybe_unused]] static constexpr auto about = R"(
xcounts: reformat counts so they only give the m and u counts in a dynamic
step wig format
)";
#include "MSite.hpp"
#include "OptionParser.hpp"
#include "counts_header.hpp"
#include "dnmt_error.hpp"
#include <bamxx.hpp>
#include <htslib/bgzf.h>
#include <htslib/sam.h>
#include <charconv>
#include <cstdint>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <iterator>
#include <string>
#include <system_error>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// NOLINTBEGIN(*-narrowing-conversions)
enum class xcounts_err : std::uint8_t {
// clang-format off
ok = 0,
open_failure = 1,
header_failure = 2,
chromosome_not_found = 3,
inconsistent_chromosome_order = 4,
incorrect_chromosome_size = 5,
failed_to_write_file = 6,
failed_to_parse_site = 7,
// clang-format on
};
// clang-format off
struct xcounts_err_cat : std::error_category {
auto name() const noexcept -> const char * override {
return "xcounts error";
}
auto message(const int condition) const -> std::string override {
// NOLINTBEGIN(*-avoid-magic-numbers)
using std::string_literals::operator""s;
switch (condition) {
case 0: return "ok"s;
case 1: return "failed to open methylome file"s;
case 2: return "failed to parse xcounts header"s;
case 3: return "failed to find chromosome in xcounts header"s;
case 4: return "inconsistent chromosome order"s;
case 5: return "incorrect chromosome size"s;
case 6: return "failed to write to file"s;
case 7: return "failed to parse site"s;
}
std::abort(); // unreacheable
// NOLINTEND(*-avoid-magic-numbers)
}
};
// clang-format on
template <>
struct std::is_error_code_enum<xcounts_err> : public std::true_type {};
std::error_code
make_error_code(xcounts_err e) {
static auto category = xcounts_err_cat{};
return std::error_code(static_cast<std::underlying_type_t<xcounts_err>>(e),
category);
}
template <typename T>
static inline std::uint32_t
fill_output_buffer(const std::uint32_t offset, const MSite &s, T &buf) {
#pragma GCC diagnostic push
#pragma GCC diagnostic error "-Wstringop-overflow=0"
// NOLINTBEGIN(*-pointer-arithmetic)
auto buf_end = std::data(buf) + std::size(buf);
auto res = std::to_chars(std::data(buf), buf_end, s.pos - offset);
*res.ptr++ = '\t';
res = std::to_chars(res.ptr, buf_end, s.n_meth());
*res.ptr++ = '\t';
res = std::to_chars(res.ptr, buf_end, s.n_unmeth());
*res.ptr++ = '\n';
return std::distance(std::data(buf), res.ptr);
// NOLINTEND(*-pointer-arithmetic)
#pragma GCC diagnostic push
}
int
main_xcounts(int argc, char *argv[]) { // NOLINT(*-avoid-c-arrays)
try {
static constexpr auto output_buffer_size = 128;
static constexpr auto input_buffer_size = 1024;
// ADS: It might happen that a "chromosome" has no CpG sites (like
// Scaffold113377 in strPur2). Therefore, we can't assume each chrom will
// appear in the input when attempting to check consistency.
bool verbose = false;
bool gzip_output = false;
bool keep_all_sites = false;
bool require_coverage = false;
std::size_t n_threads = 1;
std::string genome_file;
std::string header_file;
std::string outfile{"-"};
const std::string description =
"compress counts files by removing context information";
/****************** COMMAND LINE OPTIONS ********************/
OptionParser opt_parse(argv[0], // NOLINT(*-pointer-arithmetic)
description,
"<counts-file> (\"-\" for standard input)", 1);
opt_parse.add_opt("output", 'o', "output file (default is standard out)",
false, outfile);
opt_parse.add_opt("chroms", 'c', "make header from this reference", false,
genome_file);
opt_parse.add_opt("reads", 'r',
"output only sites with reads (on by default)", false,
require_coverage);
opt_parse.add_opt("keep", 'k', "keep sites that are not covered", false,
keep_all_sites);
opt_parse.add_opt("header", 'h', "use this file to generate header", false,
header_file);
opt_parse.add_opt("threads", 't', "threads for compression (use few)",
false, n_threads);
opt_parse.add_opt("zip", 'z',
"gzip compress output (automatic if input is gzip)",
false, gzip_output);
opt_parse.add_opt("verbose", 'v', "print more run info", false, verbose);
std::vector<std::string> leftover_args;
opt_parse.parse(argc, argv, leftover_args);
if (argc == 1 || opt_parse.help_requested()) {
std::cerr << opt_parse.help_message() << '\n'
<< opt_parse.about_message() << '\n';
return EXIT_SUCCESS;
}
if (opt_parse.about_requested()) {
std::cerr << opt_parse.about_message() << '\n';
return EXIT_SUCCESS;
}
if (opt_parse.option_missing()) {
std::cerr << opt_parse.option_missing_message() << '\n';
return EXIT_SUCCESS;
}
if (std::size(leftover_args) != 1) {
std::cerr << opt_parse.help_message() << '\n';
return EXIT_SUCCESS;
}
const std::string filename(leftover_args.front());
/****************** END COMMAND LINE OPTIONS *****************/
std::vector<std::string> chrom_names;
std::vector<std::uint64_t> chrom_sizes;
if (!genome_file.empty()) {
const int ret = get_chrom_sizes_for_counts_header(
n_threads, genome_file, chrom_names, chrom_sizes);
if (ret)
throw dnmt_error{"failed to get chrom sizes from: " + genome_file};
}
bamxx::bam_tpool tpool(n_threads);
bamxx::bgzf_file in(filename, "r");
if (!in)
throw dnmt_error{"could not open file: " + filename};
const auto outfile_mode = (gzip_output || in.is_compressed()) ? "w" : "wu";
bamxx::bgzf_file out(outfile, outfile_mode);
if (!out)
throw dnmt_error{"error opening output file: " + outfile};
if (n_threads > 1) {
if (in.is_bgzf())
tpool.set_io(in);
tpool.set_io(out);
}
std::unordered_map<std::string, std::uint32_t> chrom_order;
std::unordered_set<std::uint32_t> chroms_seen;
if (!header_file.empty())
chrom_order = write_counts_header_from_file(header_file, out);
else if (!genome_file.empty())
chrom_order =
write_counts_header_from_chrom_sizes(chrom_names, chrom_sizes, out);
// use the kstring_t type to more directly use the BGZF file
kstring_t line{0, 0, nullptr};
const int ret = ks_resize(&line, input_buffer_size);
if (ret)
throw dnmt_error("failed to acquire buffer");
std::vector<char> buf(output_buffer_size);
std::uint32_t offset = 0;
std::string prev_chrom;
bool found_header = (!genome_file.empty() || !header_file.empty());
std::error_code ec{};
std::uint32_t chrom_counter = 0;
MSite site;
while (ec == std::errc{} && bamxx::getline(in, line)) {
if (is_counts_header_line(line.s)) {
if (!genome_file.empty() || !header_file.empty())
continue;
found_header = true;
const std::string header_line{line.s};
write_counts_header_line(header_line, out);
continue;
}
// NOLINTNEXTLINE(*-pointer-arithmetic)
if (!site.initialize(line.s, line.s + line.l)) {
ec = xcounts_err::failed_to_parse_site;
break;
}
if (!found_header) {
ec = xcounts_err::header_failure;
break;
}
if (site.chrom != prev_chrom) {
if (verbose)
std::cerr << "processing: " << site.chrom << '\n';
if (!chrom_order.empty()) {
const auto expected_chrom_order = chrom_order.find(site.chrom);
if (expected_chrom_order == std::cend(chrom_order)) {
ec = xcounts_err::chromosome_not_found;
break;
}
if (expected_chrom_order->second < chrom_counter) {
ec = xcounts_err::inconsistent_chromosome_order;
break;
}
if (chroms_seen.count(expected_chrom_order->second) > 0) {
ec = xcounts_err::inconsistent_chromosome_order;
break;
}
chroms_seen.emplace(expected_chrom_order->second);
}
++chrom_counter;
prev_chrom = site.chrom;
offset = 0;
site.chrom += '\n';
const std::int64_t sz = size(site.chrom);
if (bgzf_write(out.f, std::data(site.chrom), sz) != sz)
ec = xcounts_err::failed_to_write_file;
}
if (keep_all_sites || site.n_reads > 0) {
const std::int64_t sz = fill_output_buffer(offset, site, buf);
if (bgzf_write(out.f, std::data(buf), sz) != sz)
ec = xcounts_err::failed_to_write_file;
offset = site.pos;
}
}
ks_free(&line);
if (ec) {
std::cerr << "failed converting " << filename << " to " << outfile << '\n'
<< ec.message() << '\n';
return EXIT_FAILURE;
}
}
catch (const std::exception &e) {
std::cerr << e.what() << '\n';
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
// NOLINTEND(*-narrowing-conversions)