Skip to content

Commit ed14ccd

Browse files
Merge pull request #206 from smithlabcode/xcounts-header-append
xcounts header prepend
2 parents 7d5d684 + 436037b commit ed14ccd

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

src/common/counts_header.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#include "counts_header.hpp"
2020

21+
#include <iostream>
22+
#include <fstream>
2123
#include <string>
2224
#include <vector>
2325
#include <cassert>
@@ -30,6 +32,7 @@
3032
#include <config.h>
3133

3234
#include "bamxx.hpp"
35+
#include "dnmt_error.hpp"
3336

3437
using std::vector;
3538
using std::string;
@@ -52,6 +55,20 @@ write_counts_header_from_chrom_sizes(const vector<string> &chrom_names,
5255
}
5356

5457

58+
void
59+
write_counts_header_from_file(const string &header_file, bgzf_file &out) {
60+
std::ifstream in(header_file);
61+
if (!in.is_open()) {
62+
throw dnmt_error("failed to open header file: " + header_file);
63+
}
64+
string line;
65+
while(getline(in, line)) {
66+
out.write(line + '\n');
67+
}
68+
in.close();
69+
}
70+
71+
5572
inline bgzf_file &
5673
getline(bgzf_file &file, kstring_t &line) {
5774
if (file.f == nullptr) return file;

src/common/counts_header.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ write_counts_header_from_chrom_sizes(const std::vector<std::string> &chrom_names
3030
const std::vector<uint64_t> &chrom_sizes,
3131
bamxx::bgzf_file &out);
3232

33+
void
34+
write_counts_header_from_file(const std::string &header_file,
35+
bamxx::bgzf_file &out);
36+
3337
// returns -1 on failure, 0 on success
3438
int
3539
get_chrom_sizes_for_counts_header(const uint32_t n_threads,

src/utils/xcounts.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ main_xcounts(int argc, const char **argv) {
8585
bool require_coverage = false;
8686
size_t n_threads = 1;
8787
string genome_file;
88+
string header_file;
8889

8990
string outfile{"-"};
9091
const string description =
@@ -99,6 +100,8 @@ main_xcounts(int argc, const char **argv) {
99100
false, genome_file);
100101
opt_parse.add_opt("reads", 'r', "ouput only sites with reads",
101102
false, require_coverage);
103+
opt_parse.add_opt("header", 'h', "use this file to generate header",
104+
false, header_file);
102105
opt_parse.add_opt("threads", 't', "threads for compression (use few)",
103106
false, n_threads);
104107
std::vector<string> leftover_args;
@@ -150,7 +153,9 @@ main_xcounts(int argc, const char **argv) {
150153
tpool.set_io(out);
151154
}
152155

153-
if (!genome_file.empty())
156+
if (!header_file.empty())
157+
write_counts_header_from_file(header_file, out);
158+
else if (!genome_file.empty())
154159
write_counts_header_from_chrom_sizes(chrom_names, chrom_sizes, out);
155160

156161
// use the kstring_t type to more directly use the BGZF file
@@ -163,17 +168,20 @@ main_xcounts(int argc, const char **argv) {
163168
uint32_t offset = 0;
164169
string prev_chrom;
165170
bool status_ok = true;
171+
bool found_header = (!genome_file.empty() || !header_file.empty());
166172

167173
MSite site;
168174
while (status_ok && getline(in, line)) {
169175
if (is_counts_header_line(line.s)) {
170-
if (!genome_file.empty()) continue;
176+
if (!genome_file.empty() || !header_file.empty()) continue;
177+
found_header = true;
171178
const string header_line{line.s};
172179
write_counts_header_line(header_line, out);
173180
continue;
174181
}
182+
175183
status_ok = site.initialize(line.s, line.s + line.l);
176-
if (!status_ok) break;
184+
if (!status_ok || !found_header) break;
177185

178186
if (site.chrom != prev_chrom) {
179187
prev_chrom = site.chrom;
@@ -196,6 +204,10 @@ main_xcounts(int argc, const char **argv) {
196204
<< filename << " to " << outfile << endl;
197205
return EXIT_FAILURE;
198206
}
207+
if (!found_header) {
208+
cerr << "no header provided or found" << endl;
209+
return EXIT_FAILURE;
210+
}
199211
}
200212
catch (const std::exception &e) {
201213
cerr << e.what() << endl;

0 commit comments

Comments
 (0)