Skip to content

Commit 6cad075

Browse files
sym: adding an option to compress output in gzip format
1 parent 279ec86 commit 6cad075

1 file changed

Lines changed: 15 additions & 15 deletions

File tree

src/utils/symmetric-cpgs.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ process_sites(bgzf_file &in, T &out) {
9090
int
9191
main_symmetric_cpgs(int argc, const char **argv) {
9292
try {
93-
string outfile;
93+
// file types from HTSlib use "-" for the filename to go to stdout
94+
string outfile("-");
9495
// (not used) bool VERBOSE = false;
96+
bool compress_output = false;
9597

9698
const string description =
9799
"Get CpG sites and make methylation levels symmetric.";
@@ -101,6 +103,7 @@ main_symmetric_cpgs(int argc, const char **argv) {
101103
"<methcounts-file>");
102104
opt_parse.add_opt("output", 'o', "output file (default: stdout)", false,
103105
outfile);
106+
opt_parse.add_opt("zip", 'z', "output gzip format", false, compress_output);
104107
// opt_parse.add_opt("verbose", 'v', "print more run info", false, VERBOSE);
105108
std::vector<string> leftover_args;
106109
opt_parse.parse(argc, argv, leftover_args);
@@ -124,27 +127,24 @@ main_symmetric_cpgs(int argc, const char **argv) {
124127
const string filename(leftover_args.front());
125128
/****************** END COMMAND LINE OPTIONS *****************/
126129

127-
// const bool show_progress = VERBOSE && isatty(fileno(stderr));
130+
const bool show_progress = VERBOSE && isatty(fileno(stderr));
128131
bgzf_file in(filename, "r");
129132
if (!in) throw std::runtime_error("could not open file: " + filename);
130133

131134
bool sites_are_sorted = true;
132-
if (outfile.empty() || !has_gz_ext(outfile)) {
133-
std::ofstream of;
134-
if (!outfile.empty()) of.open(outfile.c_str());
135-
std::ostream out(outfile.empty() ? std::cout.rdbuf() : of.rdbuf());
136-
sites_are_sorted = process_sites(in, out);
137-
}
138-
else {
139-
bgzf_file out(outfile, "w");
140-
sites_are_sorted = process_sites(in, out);
141-
}
135+
136+
// open the output file
137+
const string output_mode = compress_output ? "w" : "wu";
138+
bamxx::bgzf_file out(outfile, output_mode);
139+
if (!out) throw std::runtime_error("error opening output file: " + outfile);
140+
141+
sites_are_sorted = process_sites(in, out);
142142

143143
if (!sites_are_sorted) {
144144
cerr << "sites are not sorted in: " << filename << endl;
145-
namespace fs = std::filesystem;
146-
const fs::path outpath{outfile};
147-
if (fs::exists(outpath)) fs::remove(outpath);
145+
const std::filesystem::path outpath{outfile};
146+
if (std::filesystem::exists(outpath))
147+
std::filesystem::remove(outpath);
148148
return EXIT_FAILURE;
149149
}
150150
}

0 commit comments

Comments
 (0)