Skip to content

Commit 7e8e6df

Browse files
src/amrfinder/amrfinder.cpp: fixed a bug in which the chromosome conversion could use more threads than chromosomes with one thread assigned to each chromosome and some threads operating on undefined ranges of data
1 parent 8bd6d51 commit 7e8e6df

1 file changed

Lines changed: 29 additions & 31 deletions

File tree

src/amrfinder/amrfinder.cpp

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
/* amrfinder: program for resolving epialleles in a sliding window
2-
* along a chromosome.
1+
/* amrfinder: program for resolving epialleles in a sliding window along a
2+
* chromosome.
33
*
4-
* Copyright (C) 2014-2025 University of Southern California and
4+
* Copyright (C) 2014-2025 University of Southern California
55
* Andrew D. Smith and Benjamin E. Decato
66
*
77
* Authors: Fang Fang and Benjamin E. Decato and Andrew D. Smith
88
*
9-
* This program is free software: you can redistribute it and/or
10-
* modify it under the terms of the GNU General Public License as
11-
* published by the Free Software Foundation, either version 3 of the
12-
* License, or (at your option) any later version.
9+
* This program is free software: you can redistribute it and/or modify it
10+
* under the terms of the GNU General Public License as published by the Free
11+
* Software Foundation, either version 3 of the License, or (at your option)
12+
* any later version.
1313
*
14-
* This program is distributed in the hope that it will be useful, but
15-
* WITHOUT ANY WARRANTY; without even the implied warranty of
16-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17-
* General Public License for more details.
14+
* This program is distributed in the hope that it will be useful, but WITHOUT
15+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17+
* more details.
1818
*/
1919

2020
#include "EpireadStats.hpp"
@@ -34,8 +34,6 @@
3434
#include <thread>
3535
#include <vector>
3636

37-
using bamxx::bgzf_file;
38-
3937
struct amr_summary {
4038
amr_summary(const std::vector<GenomicRegion> &amrs) {
4139
amr_count = std::size(amrs);
@@ -80,7 +78,7 @@ read_epiread(bamxx::bgzf_file &f, epiread &er) {
8078
static inline bool
8179
validate_epiread_bgzf_file(const std::string &filename) {
8280
constexpr std::size_t max_lines_to_validate = 10000;
83-
bgzf_file in(filename, "r");
81+
bamxx::bgzf_file in(filename, "r");
8482
if (!in)
8583
throw std::runtime_error("failed to open file: " + filename);
8684

@@ -219,14 +217,14 @@ convert_coordinates(const std::size_t n_threads, const std::string &genome_file,
219217
std::atomic_uint32_t conv_failure = 0;
220218

221219
std::vector<std::thread> threads;
222-
for (auto i = 0ul; i < n_threads; ++i) {
223-
const auto p_beg = parts_beg + i * n_per;
220+
for (auto i = 0ul; i < std::min(n_threads, n_parts); ++i) {
221+
const auto p_beg = parts_beg + std::min(i * n_per, n_parts);
224222
const auto p_end = parts_beg + std::min((i + 1) * n_per, n_parts);
225223
threads.emplace_back([&, p_beg, p_end] {
226224
for (auto p = p_beg; p != p_end; ++p) {
227225
const std::string chrom_name = amrs[p->first].get_chrom();
228-
auto c_itr = chrom_lookup.find(chrom_name);
229-
if (c_itr == std::end(chrom_lookup))
226+
const auto c_itr = chrom_lookup.find(chrom_name);
227+
if (c_itr == std::cend(chrom_lookup))
230228
conv_failure++;
231229
else {
232230
std::vector<std::uint32_t> cpgs = collect_cpgs(c_itr->second);
@@ -335,7 +333,7 @@ process_chrom(const bool verbose, const std::uint32_t n_threads,
335333
if (verbose)
336334
std::cerr << "processing " << chrom_name << " "
337335
<< "[reads: " << std::size(epireads) << "] "
338-
<< "[cpgs: " << n_cpgs << "]" << std::endl;
336+
<< "[cpgs: " << n_cpgs << "]\n";
339337

340338
const auto n_blocks = n_threads * blocks_per_thread;
341339

@@ -462,20 +460,20 @@ main_amrfinder(int argc, char *argv[]) {
462460
std::vector<std::string> leftover_args;
463461
opt_parse.parse(argc, argv, leftover_args);
464462
if (argc == 1 || opt_parse.help_requested()) {
465-
std::cerr << opt_parse.help_message() << std::endl
466-
<< opt_parse.about_message() << std::endl;
463+
std::cerr << opt_parse.help_message() << '\n'
464+
<< opt_parse.about_message() << '\n';
467465
return EXIT_SUCCESS;
468466
}
469467
if (opt_parse.about_requested()) {
470-
std::cerr << opt_parse.about_message() << std::endl;
468+
std::cerr << opt_parse.about_message() << '\n';
471469
return EXIT_SUCCESS;
472470
}
473471
if (opt_parse.option_missing()) {
474-
std::cerr << opt_parse.option_missing_message() << std::endl;
472+
std::cerr << opt_parse.option_missing_message() << '\n';
475473
return EXIT_SUCCESS;
476474
}
477-
if (leftover_args.size() != 1) {
478-
std::cerr << opt_parse.help_message() << std::endl;
475+
if (std::size(leftover_args) != 1) {
476+
std::cerr << opt_parse.help_message() << '\n';
479477
return EXIT_SUCCESS;
480478
}
481479
const std::string reads_file(leftover_args.front());
@@ -489,13 +487,13 @@ main_amrfinder(int argc, char *argv[]) {
489487
if (verbose)
490488
std::cerr << "AMR TESTING OPTIONS: "
491489
<< "[test=" << (use_bic ? "BIC" : "LRT") << "] "
492-
<< "[iterations=" << max_itr << "]" << std::endl;
490+
<< "[iterations=" << max_itr << "]\n";
493491

494492
const EpireadStats epistat{low_prob, high_prob, critical_value,
495493
max_itr, use_bic, correct_for_read_count};
496494

497495
bamxx::bam_tpool tp(n_threads);
498-
bgzf_file in(reads_file, "r");
496+
bamxx::bgzf_file in(reads_file, "r");
499497
if (!in)
500498
throw std::runtime_error("failed to open input file: " + reads_file);
501499
if (n_threads > 1 && in.is_bgzf())
@@ -533,7 +531,7 @@ main_amrfinder(int argc, char *argv[]) {
533531
std::for_each(std::begin(amrs), std::end(amrs), rename_amr());
534532

535533
if (verbose)
536-
std::cerr << "========= POST PROCESSING =========" << std::endl;
534+
std::cerr << "========= POST PROCESSING =========\n";
537535

538536
// windows_accepted is the number of sliding windows in the
539537
// methylome that were found to have a significant signal of
@@ -592,7 +590,7 @@ main_amrfinder(int argc, char *argv[]) {
592590
const auto n_collapsed_amrs = std::size(amrs);
593591

594592
if (!convert_coordinates(n_threads, genome_file, amrs)) {
595-
std::cerr << "failed converting coordinates" << std::endl;
593+
std::cerr << "failed converting coordinates\n";
596594
return EXIT_FAILURE;
597595
}
598596

@@ -650,11 +648,11 @@ main_amrfinder(int argc, char *argv[]) {
650648
std::ofstream summary_out(summary_file);
651649
if (!summary_out)
652650
throw std::runtime_error("failed to open: " + summary_file);
653-
summary_out << amr_summary(amrs).tostring() << std::endl;
651+
summary_out << amr_summary(amrs).tostring() << '\n';
654652
}
655653
}
656654
catch (const std::exception &e) {
657-
std::cerr << e.what() << std::endl;
655+
std::cerr << e.what() << '\n';
658656
return EXIT_FAILURE;
659657
}
660658
return EXIT_SUCCESS;

0 commit comments

Comments
 (0)