Skip to content

Commit f2fe044

Browse files
Merge pull request #247 from smithlabcode/minor-fixes
Fixing some issues with option parsing on macos
2 parents deaff4a + 9596b02 commit f2fe044

File tree

2 files changed

+62
-35
lines changed

2 files changed

+62
-35
lines changed

src/analysis/cpgbins.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
#include "MSite.hpp"
2121
#include "OptionParser.hpp"
2222
#include "bsutils.hpp"
23-
#include "xcounts_utils.hpp"
2423
#include "smithlab_utils.hpp"
24+
#include "xcounts_utils.hpp"
2525

2626
#include <bamxx.hpp>
2727

2828
#include <algorithm>
2929
#include <charconv>
30+
#include <cstdint>
3031
#include <filesystem>
3132
#include <fstream>
3233
#include <iostream>
@@ -45,6 +46,8 @@ using std::ostream;
4546
using std::runtime_error;
4647
using std::size;
4748
using std::string;
49+
using std::uint32_t;
50+
using std::uint64_t;
4851
using std::unordered_map;
4952
using std::vector;
5053

@@ -75,13 +78,13 @@ format_levels_counter(const LevelsCounter &lc) {
7578
return oss.str();
7679
}
7780

78-
7981
static unordered_map<string, uint64_t>
8082
get_chrom_sizes(const string &chrom_sizes_file) {
8183
unordered_map<string, uint64_t> chrom_sizes;
8284

8385
ifstream in(chrom_sizes_file);
84-
if (!in) throw runtime_error("failed to open file: " + chrom_sizes_file);
86+
if (!in)
87+
throw runtime_error("failed to open file: " + chrom_sizes_file);
8588

8689
string line;
8790
while (getline(in, line)) {
@@ -91,7 +94,8 @@ get_chrom_sizes(const string &chrom_sizes_file) {
9194
if (!(iss >> chrom_name >> chrom_size))
9295
throw runtime_error("bad line in " + chrom_sizes_file + ":\n" + line);
9396
string dummy;
94-
if (iss >> dummy) throw runtime_error("too many columns: " + line);
97+
if (iss >> dummy)
98+
throw runtime_error("too many columns: " + line);
9599

96100
if (chrom_sizes.find(chrom_name) != cend(chrom_sizes))
97101
throw runtime_error("repeated entry " + chrom_name + " in " +
@@ -105,7 +109,8 @@ get_chrom_sizes(const string &chrom_sizes_file) {
105109
static vector<string>
106110
get_chrom_names(const string &chrom_sizes_file) {
107111
ifstream in(chrom_sizes_file);
108-
if (!in) throw runtime_error("failed to open file: " + chrom_sizes_file);
112+
if (!in)
113+
throw runtime_error("failed to open file: " + chrom_sizes_file);
109114

110115
vector<string> chrom_names;
111116

@@ -121,7 +126,6 @@ get_chrom_names(const string &chrom_sizes_file) {
121126
return chrom_names;
122127
}
123128

124-
125129
static void
126130
update(LevelsCounter &lc, const xcounts_entry &xce) {
127131
const uint64_t n_reads = xce.n_meth + xce.n_unmeth;
@@ -149,7 +153,8 @@ process_chrom(const bool report_more_info, const char level_code,
149153

150154
uint64_t j = 0;
151155
for (auto i = 0ul; i < chrom_size; i += bin_size) {
152-
while (j < size(sites) && sites[j].pos < i) ++j;
156+
while (j < size(sites) && sites[j].pos < i)
157+
++j;
153158

154159
LevelsCounter lc;
155160
while (j < size(sites) && sites[j].pos < i + bin_size)
@@ -166,7 +171,8 @@ process_chrom(const bool report_more_info, const char level_code,
166171
: (level_code == 'u' ? lc.sites_covered
167172
: lc.total_called()))));
168173
out << r;
169-
if (report_more_info) out << '\t' << format_levels_counter(lc);
174+
if (report_more_info)
175+
out << '\t' << format_levels_counter(lc);
170176
out << '\n';
171177
}
172178
}
@@ -182,7 +188,8 @@ process_chrom(const bool report_more_info, const string &chrom_name,
182188
r.set_start(i);
183189
r.set_end(std::min(i + bin_size, chrom_size));
184190
out << r;
185-
if (report_more_info) out << '\t' << lc_formatted;
191+
if (report_more_info)
192+
out << '\t' << lc_formatted;
186193
out << '\n';
187194
}
188195
}
@@ -210,7 +217,8 @@ Columns (beyond the first 6) in the BED format output:
210217
bool verbose = false;
211218
bool report_more_info = false;
212219
uint32_t n_threads = 1;
213-
uint64_t bin_size = 1000;
220+
// uint64_t bin_size = 1000;
221+
size_t bin_size = 1000; // ADS: for macOS gcc-14.2.0
214222
string level_code = "w";
215223
string outfile;
216224

@@ -266,7 +274,8 @@ Columns (beyond the first 6) in the BED format output:
266274
std::ofstream of;
267275
if (!outfile.empty()) {
268276
of.open(outfile);
269-
if (!of) throw runtime_error("failed to open outfile: " + outfile);
277+
if (!of)
278+
throw runtime_error("failed to open outfile: " + outfile);
270279
}
271280
std::ostream out(outfile.empty() ? cout.rdbuf() : of.rdbuf());
272281

src/utils/clean-hairpins.cpp

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#include <algorithm>
2222
#include <array>
23+
#include <cstddef> // std::size_t
24+
#include <cstdint> // std::uint32_t and std::uint64_t
2325
#include <fstream>
2426
#include <iomanip>
2527
#include <iostream>
@@ -45,7 +47,10 @@ using std::ofstream;
4547
using std::ostream;
4648
using std::ostringstream;
4749
using std::runtime_error;
50+
using std::size_t;
4851
using std::string;
52+
using std::uint32_t;
53+
using std::uint64_t;
4954
using std::vector;
5055

5156
using bamxx::bgzf_file;
@@ -93,22 +98,28 @@ operator>>(bgzf_file &s, FASTQRecord &r) {
9398

9499
err_code ec = err_code::none;
95100

96-
if (!getline(s, r.name)) return s;
101+
if (!getline(s, r.name))
102+
return s;
97103

98-
if (r.name.empty() || r.name[0] != '@') ec = err_code::bad_name;
104+
if (r.name.empty() || r.name[0] != '@')
105+
ec = err_code::bad_name;
99106

100107
const auto nm_end = r.name.find_first_of(" \t");
101108
const auto nm_sz = (nm_end == string::npos ? r.name.size() : nm_end) - 1;
102109
r.name.erase(copy_n(cbegin(r.name) + 1, nm_sz, begin(r.name)), cend(r.name));
103110

104-
if (!getline(s, r.seq)) ec = err_code::bad_seq;
111+
if (!getline(s, r.seq))
112+
ec = err_code::bad_seq;
105113

106114
string tmp;
107-
if (!getline(s, tmp)) ec = err_code::bad_plus;
115+
if (!getline(s, tmp))
116+
ec = err_code::bad_plus;
108117

109-
if (!getline(s, r.qual)) ec = err_code::bad_qual;
118+
if (!getline(s, r.qual))
119+
ec = err_code::bad_qual;
110120

111-
if (ec != err_code::none) throw error_msg[ec];
121+
if (ec != err_code::none)
122+
throw error_msg[ec];
112123

113124
return s;
114125
}
@@ -171,15 +182,13 @@ struct hp_summary {
171182
// sum_percent_match_bad over the total hairpin reads.
172183
double mean_percent_match_hairpin{};
173184

174-
auto
175-
assign_values() -> void {
185+
auto assign_values() -> void {
176186
mean_percent_match_non_hairpin =
177187
sum_percent_match_good / (n_reads - n_hairpin_reads);
178188
mean_percent_match_hairpin = sum_percent_match_bad / n_hairpin_reads;
179189
}
180190

181-
auto
182-
tostring() const -> string {
191+
auto tostring() const -> string {
183192
ostringstream oss;
184193
oss << "total_reads_pairs: " << n_reads << '\n'
185194
<< "good_reads_pairs: " << n_good_reads << '\n'
@@ -197,7 +206,8 @@ struct hp_summary {
197206
static void
198207
write_histogram(const string &hist_outfile, vector<double> hist) {
199208
ofstream hist_out(hist_outfile);
200-
if (!hist_out) throw runtime_error("failed to open file: " + hist_outfile);
209+
if (!hist_out)
210+
throw runtime_error("failed to open file: " + hist_outfile);
201211
const auto total = accumulate(cbegin(hist), cend(hist), 0.0);
202212
transform(cbegin(hist), cend(hist), begin(hist),
203213
[&total](const double t) { return t / total; });
@@ -213,7 +223,8 @@ static void
213223
write_statistics(const string &filename, hp_summary hps) {
214224
hps.assign_values();
215225
ofstream out(filename);
216-
if (!out) throw runtime_error("failed to open file: " + filename);
226+
if (!out)
227+
throw runtime_error("failed to open file: " + filename);
217228
out << hps.tostring();
218229
}
219230

@@ -226,17 +237,18 @@ fraction_good_bases(const FASTQRecord &a, const FASTQRecord &b) {
226237

227238
struct clean_hairpin {
228239
double cutoff{0.95};
229-
uint64_t n_reads_to_check{std::numeric_limits<size_t>::max()};
240+
// ADS: this was uint64_t but g++-14.2.0 on macOS had a problem
241+
size_t n_reads_to_check{std::numeric_limits<size_t>::max()};
230242
double min_good_base_percent{0.5};
231243
uint32_t min_read_length{32};
232244
uint32_t n_hist_bins{20};
233245
bool invert_output{false};
234246

235-
hp_summary
236-
analyze_reads(const string &outfile1, const string &outfile2, bgzf_file &in1,
237-
bgzf_file &in2, vector<double> &hist) const;
238-
hp_summary
239-
analyze_reads(bgzf_file &in1, bgzf_file &in2, vector<double> &hist) const;
247+
hp_summary analyze_reads(const string &outfile1, const string &outfile2,
248+
bgzf_file &in1, bgzf_file &in2,
249+
vector<double> &hist) const;
250+
hp_summary analyze_reads(bgzf_file &in1, bgzf_file &in2,
251+
vector<double> &hist) const;
240252
};
241253

242254
hp_summary
@@ -246,9 +258,11 @@ clean_hairpin::analyze_reads(const string &outfile1, const string &outfile2,
246258

247259
// output files for read1 and read2 with hairpins removed
248260
bgzf_file out1(outfile1, "w");
249-
if (!out1) throw runtime_error("cannot open output file: " + outfile1);
261+
if (!out1)
262+
throw runtime_error("cannot open output file: " + outfile1);
250263
bgzf_file out2(outfile2, "w");
251-
if (!out2) throw runtime_error("cannot open output file: " + outfile2);
264+
if (!out2)
265+
throw runtime_error("cannot open output file: " + outfile2);
252266

253267
hp_summary hps{cutoff};
254268
FASTQRecord r1, r2;
@@ -384,17 +398,21 @@ main_clean_hairpins(int argc, const char **argv) {
384398

385399
// Input: paired-end reads with end1 and end2
386400
bgzf_file in1(reads_file1, "r");
387-
if (!in1) throw runtime_error("cannot open input file: " + reads_file1);
401+
if (!in1)
402+
throw runtime_error("cannot open input file: " + reads_file1);
388403
bgzf_file in2(reads_file2, "r");
389-
if (!in2) throw runtime_error("cannot open input file: " + reads_file2);
404+
if (!in2)
405+
throw runtime_error("cannot open input file: " + reads_file2);
390406

391407
const hp_summary hps =
392408
write_reads ? ch.analyze_reads(outfile1, outfile2, in1, in2, hist)
393409
: ch.analyze_reads(in1, in2, hist);
394410

395-
if (!stat_outfile.empty()) write_statistics(stat_outfile, hps);
411+
if (!stat_outfile.empty())
412+
write_statistics(stat_outfile, hps);
396413

397-
if (!hist_outfile.empty()) write_histogram(hist_outfile, hist);
414+
if (!hist_outfile.empty())
415+
write_histogram(hist_outfile, hist);
398416
}
399417
catch (const std::exception &e) {
400418
cerr << e.what() << endl;

0 commit comments

Comments
 (0)