diff --git a/data/test_data/md5sum.txt b/data/test_data/md5sum.txt index 0a062fb..49b53d9 100644 --- a/data/test_data/md5sum.txt +++ b/data/test_data/md5sum.txt @@ -7,7 +7,7 @@ bcf8363f788e54d38a5ec758042959ce ./fastq_plain_out/fastq_1/fastqc_data.txt 39a5df25471d6f250de7b31a03325254 ./fastq_bgzf_mult_out/fastq_bgzip_2/fastqc_data.txt 55d98514eda63c2cf4b900a0fb7b074b ./fastq_bgzf_out/fastq_bgzip_1/fastqc_data.txt 41a799d28bdc2e35e17546fe2af146cb ./bam_mult_out/bam_1/fastqc_data.txt -fbbf6f4d0607abf18425699197fb9e7f ./bam_mult_out/bam_2/fastqc_data.txt +953dddfc5bf8d0f045ddc04419c604f8 ./bam_mult_out/bam_2/fastqc_data.txt 0e0d6a2d5523f50cb2374ef49e498036 ./fastq_stdin_out/fastq_stdin/fastqc_data.txt bab943eb8b778badeccb51045c8365b0 ./fastq_gz_out/fastq_gz_1/fastqc_data.txt 41a799d28bdc2e35e17546fe2af146cb ./bam_orig_dups_out/bam_1/fastqc_data.txt diff --git a/src/adapter_matcher.cpp b/src/adapter_matcher.cpp index bc0ea1e..e798e8a 100644 --- a/src/adapter_matcher.cpp +++ b/src/adapter_matcher.cpp @@ -13,6 +13,7 @@ #include "fmt/ranges.h" // IWYU pragma: keep #include +#include #include #include #include @@ -26,11 +27,20 @@ #include adapter_matcher::adapter_matcher() { + [[maybe_unused]] // ADS: false positive compiler warning + static constexpr auto base_to_idx = [](const auto c) -> char { + static constexpr auto mask = 31; + static constexpr auto to_acgtn = std::array{ + 4, 0, 4, 1, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + }; + return to_acgtn[c & mask]; // NOLINT + }; static constexpr auto shift_plus = [&](const auto r, const auto c) { - return (r << nibble_size) + encode_nibble(c); + return (r << nibble_size) + encode_nibble(base_to_idx(c)); }; static constexpr auto do_encoding = [&](const auto &a) { - return std::accumulate(std::cbegin(a), std::cend(a), 0ul, shift_plus); + return std::accumulate(std::cbegin(a), std::cend(a), 0LU, shift_plus); }; if (const auto &as = adapter_set::instance(); !as.adapters.empty()) { n_adapters = adapter_set::n_adapters(); diff --git a/src/bamrec.cpp b/src/bamrec.cpp index 8a7f164..292f4b9 100644 --- a/src/bamrec.cpp +++ b/src/bamrec.cpp @@ -3,6 +3,7 @@ #include "bamrec.hpp" #include +#include #include #include #include @@ -37,25 +38,32 @@ bamrec::to_string() const -> std::string { template static inline constexpr output_itr_t assign_sequence_revcomp(bidir_itr_t first, auto last, output_itr_t d_first) { - static constexpr std::span seq_nt16_str_complem = "NTGNCNNNANNNNNNN"; - constexpr auto bam_seqi = [](const auto s, const auto i) -> int { + static constexpr auto seq_nt16_str_cmpl = std::array{ + 4, 2, 3, 4, 1, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 4, + }; + static constexpr auto bam_seqi = [](const auto s, const auto i) { constexpr auto low_nibble_on = 0xf; return s[i >> 1] >> ((~i & 1) << 2) & low_nibble_on; }; - for (auto j = last; j != 0; ++d_first) - *d_first = seq_nt16_str_complem[bam_seqi(first, --j)]; + auto j = last; + while (j != 0) + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-constant-array-index) + *d_first++ = seq_nt16_str_cmpl[bam_seqi(first, --j)]; return d_first; } template static inline constexpr output_itr_t assign_sequence(bidir_itr_t first, auto last, output_itr_t d_first) { - static constexpr std::span seq_nt16_str = "NACNGNNNTNNNNNNN"; - constexpr auto bam_seqi = [](const auto s, const auto i) -> int { + static constexpr auto seq_nt16_str = std::array{ + 4, 0, 1, 4, 3, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, + }; + static constexpr auto bam_seqi = [](const auto s, const auto i) { constexpr auto low_nibble_on = 0xf; - return s[i >> 1] >> ((~i & 1) << 2) & low_nibble_on; + return (s[i >> 1] >> ((~i & 1) << 2)) & low_nibble_on; }; for (auto j = 0U; j != last; ++j) + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-constant-array-index) *d_first++ = seq_nt16_str[bam_seqi(first, j)]; return d_first; } diff --git a/src/falco_utils.hpp b/src/falco_utils.hpp index b19b673..b920997 100644 --- a/src/falco_utils.hpp +++ b/src/falco_utils.hpp @@ -30,6 +30,12 @@ // G (71)10 = (1000111)2 => 11 => 3 // T (84)10 = (1010100)2 => 10 => 2 +// N => 100 => 4 +// A => 000 => 0 +// C => 001 => 1 +// G => 011 => 3 +// T => 010 => 2 + static constexpr auto adenine_index = 0; static constexpr auto cytosine_index = 1; static constexpr auto thymine_index = 2; @@ -37,10 +43,56 @@ static constexpr auto guanine_index = 3; static constexpr std::span bases = "ACTG"; // ADS: index this with vars above static constexpr auto alphabet_size = 4; -static constexpr auto unknown_base_index = 3; +static constexpr auto unknown_base_index = 0; +static constexpr auto N_encoding = 4; static constexpr auto nibble_size = 4; +[[nodiscard]] inline constexpr auto +encode(const char c) -> char { + static constexpr auto two_bit_mask = 3; + return c & two_bit_mask; // Ns are counted as G so must be subtracted +} + +[[nodiscard]] inline constexpr auto +is_gc(const char c) -> bool { + return c & 1; +} + +[[nodiscard]] inline constexpr auto +encode_nibble(const char c) -> char { + static constexpr auto four_bit_mask = 15; // ADS: 15 is to keep 4 bits + return c & four_bit_mask; // N gets separate encoding +} + +inline constexpr auto +count_nucs(auto seq_itr, + const auto seq_end, + auto &tab) { // cppcheck-suppress constParameterReference + auto out_itr = std::begin(tab); + while (seq_itr != seq_end) + ++(*out_itr++)[encode(*seq_itr++)]; +} + +inline constexpr auto +count_ns(auto seq_itr, + const auto seq_end, + auto &tab) { // cppcheck-suppress constParameterReference + auto out_itr = std::begin(tab); + while (seq_itr != seq_end) + *out_itr++ += (*seq_itr++ == N_encoding); +} + +[[nodiscard]] inline auto +count_gc(auto seq_itr, const auto seq_end) { + auto gc = 0; + while (seq_itr != seq_end) { + gc += (*seq_itr != N_encoding) && is_gc(*seq_itr); + ++seq_itr; + } + return gc; +} + namespace falco { static constexpr auto base_permutation_for_report_impl = { guanine_index, @@ -141,23 +193,6 @@ get_run_duration(const auto start_time) { inline constexpr auto end_module_tag = ">>END_MODULE\n"; -[[nodiscard]] inline constexpr auto -encode(const char c) { - static constexpr auto two_bit_mask = 3; - return (c >> 1) & two_bit_mask; // Ns are counted as G so must be subtracted -} - -[[nodiscard]] inline constexpr auto -is_gc(const char c) { - return (c >> 1) & 1; -} - -[[nodiscard]] inline constexpr auto -encode_nibble(const char c) { - static constexpr auto four_bit_mask = 15; // ADS: 15 is to keep 4 bits - return (c >> 1) & four_bit_mask; // N gets separate encoding -} - [[nodiscard]] inline constexpr auto pct(const double a) { return 100.0 * a; @@ -252,34 +287,6 @@ ipow(const auto b, const auto e) -> std::remove_cvref_t { return e == 0 ? 1 : (e & 1 ? b : 1) * ipow(b * b, e >> 1); } -inline constexpr auto -count_nucs(auto seq_itr, - const auto seq_end, - auto &tab) { // cppcheck-suppress constParameterReference - auto out_itr = std::begin(tab); - while (seq_itr != seq_end) - ++(*out_itr++)[encode(*seq_itr++)]; -} - -inline constexpr auto -count_ns(auto seq_itr, - const auto seq_end, - auto &tab) { // cppcheck-suppress constParameterReference - auto out_itr = std::begin(tab); - while (seq_itr != seq_end) - *out_itr++ += (*seq_itr++ == 'N'); -} - -[[nodiscard]] inline auto -count_gc(auto seq_itr, const auto seq_end) { - auto gc = 0; - while (seq_itr != seq_end) { - gc += (*seq_itr != 'N' && is_gc(*seq_itr)); - ++seq_itr; - } - return gc; -} - [[nodiscard]] inline auto tabular_dot(const auto &a) { typename std::remove_cvref_t::value_type total{}; diff --git a/src/falco_word.hpp b/src/falco_word.hpp index f4218fa..157fc38 100644 --- a/src/falco_word.hpp +++ b/src/falco_word.hpp @@ -19,7 +19,7 @@ // T (84)10 = (1010100)2 => 001 struct falco_word { - static constexpr std::span extended_bases = "GTCAN"; + static constexpr std::span extended_bases = "ACTGN"; static constexpr auto extended_alpha_size = 5; static constexpr auto shift_for_width_bits = 56u; static constexpr auto width_bits_removal_mask = 0xffffffffffffff; @@ -32,12 +32,9 @@ struct falco_word { operator<=>(const falco_word &) const = default; falco_word(auto b, std::uint64_t w) { - static constexpr auto fw_encode = [](const auto c) { - return ((c >> 1) & 7) ^ 3; // NOLINT(*-avoid-magic-numbers) - }; static const auto enc_shift = [&](auto &x, auto &c) { // NOLINTNEXTLINE(clang-analyzer-security.ArrayBound) - x = (x * extended_alpha_size) + fw_encode(*c++); + x = (x * extended_alpha_size) + *c++; }; w = w < max_hi_lim ? w : max_hi_lim; const auto lo_lim = b + (w > max_lo_lim ? max_lo_lim : w); diff --git a/src/fastq_file.cpp b/src/fastq_file.cpp index c634a1e..ca71791 100644 --- a/src/fastq_file.cpp +++ b/src/fastq_file.cpp @@ -19,15 +19,15 @@ #include #include #include +#include #include #include #include +#include +#include #ifndef USE_MMAP_FOR_PLAIN_FASTQ #include -#include #include -#include -#include #else // USE_MMAP_FOR_PLAIN_FASTQ #include #include diff --git a/src/fqrec.hpp b/src/fqrec.hpp index 7018539..859d1ac 100644 --- a/src/fqrec.hpp +++ b/src/fqrec.hpp @@ -74,8 +74,13 @@ struct fq_gz_task_t { [[nodiscard]] inline auto get_next(fqrec::pos_t &cursor, const fqrec::pos_t end_itr) -> fqrec { - const auto iupac = [](auto &c) { - c = "NANCNNNGNNNNNNNNNNNNTNNNNNNNNNNN"[c & 31]; // NOLINT + const auto to_acgtn = [](auto &c) { + static constexpr auto mask = 31; + static constexpr auto x = std::array{ + 4, 0, 4, 1, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + }; + c = x[c & mask]; // NOLINT }; // ADS: need to make sure cursor < end_itr or we will move past const auto n = cursor; @@ -92,7 +97,7 @@ get_next(fqrec::pos_t &cursor, const fqrec::pos_t end_itr) -> fqrec { const auto r = itr; while (itr != end_itr && *itr != '\n') - iupac(*itr++); + to_acgtn(*itr++); if (itr++ == end_itr) return {}; const auto o = itr; diff --git a/src/original_duplicates.cpp b/src/original_duplicates.cpp index 44629d5..967174c 100644 --- a/src/original_duplicates.cpp +++ b/src/original_duplicates.cpp @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -35,12 +36,19 @@ init_orig_dups_bam(const std::string &filename, const std::uint64_t n_unique) // the target, so try to overshoot. static constexpr auto n_unique_multiplier = 10; static constexpr auto complem = [](char &x) { - assert(x >= 'A' && x <= 'T'); - x = "TNGNNNCNNNNNNNNNNNNA"[x - 'A']; + static constexpr auto complement_mapping = std::array{ + 2, 3, 0, 1, 4, // (A, C, T, G) -> (T, G, A, C) + }; + assert(x >= 0 && x <= 4); + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-constant-array-index) + x = complement_mapping[x]; }; static constexpr auto to_acgtn = [](const char x) { - static constexpr char mask = 15; - static constexpr std::span seq_nt16_str_acgtn = "NACNGNNNTNNNNNNN"; + static constexpr auto mask = 15; + static constexpr auto seq_nt16_str_acgtn = std::array{ + 4, 0, 1, 4, 3, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, + }; + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-constant-array-index) return seq_nt16_str_acgtn[x & mask]; }; const std::unique_ptr in( @@ -88,10 +96,16 @@ init_orig_dups_fq(const std::string &filename, const std::uint64_t n_unique) // the n_unique that is the target; impossible to know how many needed to hit // the target, so try to overshoot. static constexpr auto n_unique_multiplier = 10; + static constexpr auto n_lines_per_record = 4; + static constexpr auto record_line_index = 1; static constexpr auto to_acgtn = [](const auto c) { static constexpr auto mask = 31; - static constexpr std::span to_acgtn_sp = "NANCNNNGNNNNNNNNNNNNTNNNNNNNNNNN"; - return to_acgtn_sp[c & mask]; + static constexpr auto to_acgtn_a = std::array{ + 4, 0, 4, 1, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + }; + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-constant-array-index) + return to_acgtn_a[c & mask]; }; const std::unique_ptr in( bgzf_open(std::data(filename), "r"), &bgzf_close); @@ -106,7 +120,7 @@ init_orig_dups_fq(const std::string &filename, const std::uint64_t n_unique) const auto max_n_reads = n_unique_multiplier * n_unique; while (n_reads < max_n_reads && std::size(dups) < n_unique && (r = bgzf_getline(in.get(), '\n', &s)) >= 0) { - if (n_lines % 4 == 1) { + if (n_lines % n_lines_per_record == record_line_index) { const auto l_qseq = ks_len(&s); std::ranges::transform(std::span(s.s, l_qseq), s.s, to_acgtn); const auto seq = ks_c_str(&s); diff --git a/src/samrec.cpp b/src/samrec.cpp index a79ac55..90afe46 100644 --- a/src/samrec.cpp +++ b/src/samrec.cpp @@ -4,6 +4,7 @@ #include "quality_score.hpp" #include +#include #include #include #include @@ -38,12 +39,24 @@ samrec::to_string() const -> std::string { samrec::get_next(samrec::pos_t &cursor, const samrec::pos_t end_itr, samrec &rec) -> bool { + // ADS: number of SAM fields to skip after the flag column and before the read + // sequence column static constexpr auto n_fields_to_skip = 7; - const auto iupac = [](const auto c) -> char { - return "NANCNNNGNNNNNNNNNNNNTNNNNNNNNNNN"[c & 31]; // NOLINT + static constexpr auto base_to_idx = [](const auto c) -> char { + static constexpr auto mask = 31; + static constexpr auto to_acgtn = std::array{ + 4, 0, 4, 1, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + }; + return to_acgtn[c & mask]; // NOLINT }; - const auto iupac_cmpl = [](const auto c) -> char { - return "NTNGNNNCNNNNNNNNNNNNANNNNNNNNNNN"[c & 31]; // NOLINT + static constexpr auto base_to_idx_cmpl = [](const auto c) -> char { + static constexpr auto mask = 31; + static constexpr auto to_acgtn_cmpl = std::array{ + 4, 2, 4, 3, 4, 4, 4, 1, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + }; + return to_acgtn_cmpl[c & mask]; // NOLINT }; static constexpr auto bam_is_rev = [](const auto flag) -> bool { static constexpr auto BAM_FREVERSE = 16; @@ -110,14 +123,15 @@ samrec::get_next(samrec::pos_t &cursor, if (bam_is_rev(flag)) { std::reverse_copy(seq_itr, seq_itr + seq_len, out_seq_itr); - std::transform(out_seq_itr, out_seq_itr + seq_len, out_seq_itr, iupac_cmpl); + std::transform(out_seq_itr, out_seq_itr + seq_len, out_seq_itr, + base_to_idx_cmpl); if (*qual_itr != qual_missing_code) std::reverse_copy(qual_itr, qual_itr + seq_len, out_qual_itr); else *out_qual_itr = qual_missing_code; } else { - std::transform(seq_itr, seq_itr + seq_len, out_seq_itr, iupac); + std::transform(seq_itr, seq_itr + seq_len, out_seq_itr, base_to_idx); if (*qual_itr != qual_missing_code) std::copy(qual_itr, qual_itr + seq_len, out_qual_itr); else