Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion data/test_data/md5sum.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions src/adapter_matcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "fmt/ranges.h" // IWYU pragma: keep

#include <algorithm>
#include <array>
#include <cassert>
#include <cstdint>
#include <format>
Expand All @@ -26,11 +27,20 @@
#include <vector>

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<char, mask + 1>{
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();
Expand Down
22 changes: 15 additions & 7 deletions src/bamrec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "bamrec.hpp"

#include <algorithm>
#include <array>
#include <cstring>
#include <format>
#include <memory>
Expand Down Expand Up @@ -37,25 +38,32 @@ bamrec::to_string() const -> std::string {
template <class bidir_itr_t, class output_itr_t>
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<char, 16>{
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 <class bidir_itr_t, class output_itr_t>
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<char, 16>{
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;
}
Expand Down
99 changes: 53 additions & 46 deletions src/falco_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,69 @@
// 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;
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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -252,34 +287,6 @@ ipow(const auto b, const auto e) -> std::remove_cvref_t<decltype(b)> {
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<decltype(a)>::value_type total{};
Expand Down
7 changes: 2 additions & 5 deletions src/falco_word.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/fastq_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
#include <cstdio>
#include <cstdlib>
#include <iterator>
#include <memory>
#include <ranges>
#include <string>
#include <system_error>
#include <tuple>
#include <vector>
#ifndef USE_MMAP_FOR_PLAIN_FASTQ
#include <compare>
#include <memory>
#include <span>
#include <tuple>
#include <vector>
#else // USE_MMAP_FOR_PLAIN_FASTQ
#include <atomic>
#include <stdexcept>
Expand Down
11 changes: 8 additions & 3 deletions src/fqrec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char, mask + 1>{
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;
Expand All @@ -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;

Expand Down
28 changes: 21 additions & 7 deletions src/original_duplicates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <htslib/sam.h>

#include <algorithm>
#include <array>
#include <cassert>
#include <cerrno>
#include <iterator>
Expand All @@ -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<char, 5>{
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<char, mask + 1>{
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<htsFile, int (*)(htsFile *)> in(
Expand Down Expand Up @@ -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<char, mask + 1>{
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<BGZF, int (*)(BGZF *)> in(
bgzf_open(std::data(filename), "r"), &bgzf_close);
Expand All @@ -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);
Expand Down
26 changes: 20 additions & 6 deletions src/samrec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "quality_score.hpp"

#include <algorithm>
#include <array>
#include <cassert>
#include <charconv>
#include <format>
Expand Down Expand Up @@ -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<char, mask + 1>{
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<char, mask + 1>{
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;
Expand Down Expand Up @@ -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
Expand Down
Loading