use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
use crate::filter::FilterConfig;
use crate::io::Format;
use crate::trim::TrimPlan;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FastqTags {
All,
None,
Only(BTreeSet<[u8; 2]>),
}
impl FastqTags {
pub fn parse(s: &str) -> anyhow::Result<Self> {
match s {
"all" => Ok(FastqTags::All),
"none" => Ok(FastqTags::None),
_ => {
let mut set = BTreeSet::new();
for tok in s.split(',') {
if tok.len() != 2 || !tok.bytes().all(|c| c.is_ascii_alphanumeric()) {
anyhow::bail!(
"--fastq-tags: invalid tag {tok:?} (SAM tags are exactly 2 \
characters); use `all`, `none`, or a comma list like `MM,ML,RG`"
);
}
let b = tok.as_bytes();
set.insert([b[0], b[1]]);
}
Ok(FastqTags::Only(set))
},
}
}
pub fn carries(&self, tag: &[u8; 2]) -> bool {
match self {
FastqTags::All => true,
FastqTags::None => false,
FastqTags::Only(s) => s.contains(tag),
}
}
pub fn carries_mods(&self) -> bool {
match self {
FastqTags::All => true,
FastqTags::None => false,
FastqTags::Only(s) => s.contains(b"MM") || s.contains(b"ML"),
}
}
}
pub fn kinetics_tags() -> impl Iterator<Item = [u8; 2]> {
[
*b"ip", *b"pw", *b"fi", *b"fp", *b"ri", *b"rp", *b"sa", *b"sm", *b"sx",
]
.into_iter()
}
pub const SIGNAL_TAGS: [[u8; 2]; 5] = [*b"mv", *b"ts", *b"ns", *b"sp", *b"pi"];
pub const MOD_TAGS: [[u8; 2]; 3] = [*b"MM", *b"ML", *b"MN"];
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct TagRemoval {
tags: BTreeSet<[u8; 2]>,
}
impl TagRemoval {
pub fn parse(values: &[String]) -> anyhow::Result<Self> {
let mut tags = BTreeSet::new();
for item in values.iter().flat_map(|v| v.split(',')) {
let item = item.trim();
match item {
"kinetics" => tags.extend(kinetics_tags()),
"mods" => tags.extend(MOD_TAGS),
"signal" => tags.extend(SIGNAL_TAGS),
_ if item.len() == 2 && item.bytes().all(|c| c.is_ascii_alphanumeric()) => {
let b = item.as_bytes();
tags.insert([b[0], b[1]]);
},
_ => anyhow::bail!(
"--remove-tag: invalid item {item:?} (a SAM tag is exactly 2 alphanumeric \
characters, such as `ML` or `RG`; the groups are kinetics, mods and signal)"
),
}
}
Ok(TagRemoval { tags })
}
pub fn is_empty(&self) -> bool {
self.tags.is_empty()
}
pub fn contains(&self, tag: &[u8; 2]) -> bool {
!self.tags.is_empty() && self.tags.contains(tag)
}
pub fn tags(&self) -> impl Iterator<Item = &[u8; 2]> {
self.tags.iter()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AdapterInfer {
Off,
Discover,
Report,
}
impl AdapterInfer {
pub fn is_report(self) -> bool {
matches!(self, Self::Report)
}
pub fn label(self) -> &'static str {
match self {
Self::Off => "off",
Self::Discover => "discover",
Self::Report => "report",
}
}
}
#[derive(Debug, Clone)]
pub struct Advisory {
pub warn: bool,
pub message: String,
}
impl Advisory {
pub fn warn(message: impl Into<String>) -> Self {
Advisory {
warn: true,
message: message.into(),
}
}
pub fn info(message: impl Into<String>) -> Self {
Advisory {
warn: false,
message: message.into(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum ProgressMode {
Auto,
Bar,
Plain,
None,
}
#[derive(Debug, Clone, Default)]
pub struct IoConfig {
pub input: Option<PathBuf>,
pub output: Option<PathBuf>,
pub in_format: Option<Format>,
pub out_format: Option<Format>,
}
#[derive(Debug, Clone)]
pub struct Config {
pub io: IoConfig,
pub filter: FilterConfig,
pub trim: TrimPlan,
pub adapters: Option<crate::adapter::AdapterConfig>,
pub adapter_infer: AdapterInfer,
pub threads: usize,
pub fastq_tags: FastqTags,
pub render_workers: usize,
pub adapter_sample: usize,
pub compression_level: u8,
pub update_moves: bool,
pub remove_tags: TagRemoval,
pub tag_filters: crate::tagfilter::TagFilters,
pub ordered: bool,
pub verbosity: u8,
pub quiet: bool,
pub threads_clamped: Option<(usize, usize)>,
pub summary_json: Option<PathBuf>,
pub rejected_output: Option<PathBuf>,
pub advisories: Vec<Advisory>,
pub progress: ProgressMode,
pub adapter_fasta: Option<PathBuf>,
pub adapters_configured: Option<usize>,
pub trim_classes: [bool; 3],
}
impl Default for Config {
fn default() -> Self {
Config {
io: IoConfig::default(),
filter: crate::filter::FilterConfig::default(),
trim: crate::trim::TrimPlan::default(),
adapters: None,
adapter_infer: AdapterInfer::Off,
threads: 1,
fastq_tags: FastqTags::All,
render_workers: 0,
adapter_sample: 0,
compression_level: 6,
update_moves: false,
remove_tags: TagRemoval::default(),
tag_filters: crate::tagfilter::TagFilters::default(),
ordered: false,
verbosity: 0,
quiet: false,
threads_clamped: None,
summary_json: None,
rejected_output: None,
advisories: Vec::new(),
progress: ProgressMode::Auto,
adapter_fasta: None,
adapters_configured: None,
trim_classes: [false; 3],
}
}
}
impl Config {
pub fn write_targets(&self) -> impl Iterator<Item = (&'static str, &Path)> {
[
("-o/--output", self.io.output.as_deref()),
("--rejected-output", self.rejected_output.as_deref()),
("--summary-json", self.summary_json.as_deref()),
]
.into_iter()
.filter_map(|(flag, path)| path.map(|p| (flag, p)))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ThreadBudget {
pub decode: usize,
pub render: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Decode {
Inline,
Stream,
Blocks,
}
pub fn thread_budget(total: usize, decode: Decode) -> ThreadBudget {
let total = total.max(1);
let taken = match decode {
Decode::Inline => 0,
_ if total == 1 => 0,
Decode::Stream => 1,
Decode::Blocks => (total / 4).max(1),
};
ThreadBudget {
decode: taken.max(1),
render: total - taken,
}
}
pub fn resolve_threads(requested: Option<usize>, ncpu: usize) -> usize {
let ncpu = ncpu.max(1);
match requested {
None => ncpu,
Some(n) => n.clamp(1, ncpu),
}
}
#[cfg(test)]
mod resolve_threads_tests {
use super::resolve_threads;
#[test]
fn auto_uses_all_cpus() {
assert_eq!(resolve_threads(None, 8), 8);
}
#[test]
fn in_range_is_unchanged() {
assert_eq!(resolve_threads(Some(4), 8), 4);
}
#[test]
fn over_spec_clamps_to_ncpu() {
assert_eq!(resolve_threads(Some(32), 8), 8);
}
#[test]
fn zero_floors_to_one() {
assert_eq!(resolve_threads(Some(0), 8), 1);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn thread_budget_shares_the_workers_between_stages() {
let budget = |decode, render| ThreadBudget { decode, render };
assert_eq!(thread_budget(8, Decode::Blocks), budget(2, 6));
assert_eq!(thread_budget(32, Decode::Blocks), budget(8, 24));
assert_eq!(thread_budget(2, Decode::Blocks), budget(1, 1));
assert_eq!(thread_budget(8, Decode::Stream), budget(1, 7));
assert_eq!(thread_budget(2, Decode::Stream), budget(1, 1));
assert_eq!(thread_budget(8, Decode::Inline), budget(1, 8));
for decode in [Decode::Inline, Decode::Stream, Decode::Blocks] {
assert_eq!(thread_budget(1, decode), budget(1, 1));
}
}
#[test]
fn kinetics_group_folds_in_the_nine_per_base_arrays() {
let r = TagRemoval::parse(&["kinetics".to_string()]).unwrap();
let names: Vec<String> = r
.tags()
.map(|t| String::from_utf8_lossy(t).into_owned())
.collect();
assert_eq!(
names,
["fi", "fp", "ip", "pw", "ri", "rp", "sa", "sm", "sx"]
);
for tag in [
b"ip", b"pw", b"fi", b"fp", b"ri", b"rp", b"sa", b"sm", b"sx",
] {
assert!(r.contains(tag), "{}", String::from_utf8_lossy(tag));
}
assert!(!r.contains(b"MM"));
}
#[test]
fn comma_lists_and_groups_share_one_set() {
let r = TagRemoval::parse(&["MM,RG".to_string(), "kinetics".to_string()]).unwrap();
assert!(r.contains(b"MM") && r.contains(b"RG") && r.contains(b"ip"));
assert_eq!(r.tags().count(), 11);
let r = TagRemoval::parse(&["mods,signal".to_string()]).unwrap();
assert_eq!(r.tags().count(), 8);
assert!(r.contains(b"ML") && r.contains(b"mv") && r.contains(b"pi"));
assert!(TagRemoval::parse(&[]).unwrap().is_empty());
}
#[test]
fn group_names_are_lowercase_only() {
for item in ["Kinetics", "KINETICS", "kinetic", "MODS", "xyz"] {
let err = TagRemoval::parse(&[item.to_string()]).unwrap_err();
assert!(err.to_string().contains("invalid item"), "{item}: {err}");
}
}
#[test]
fn no_flag_removes_nothing() {
let r = TagRemoval::parse(&[]).unwrap();
assert!(r.is_empty());
assert!(!r.contains(b"MM"));
assert_eq!(r, TagRemoval::default());
}
#[test]
fn remove_tag_rejects_a_malformed_value() {
for bad in ["M", "MMM", "M_", "", "\u{e9}"] {
let err = TagRemoval::parse(&[bad.to_string()])
.unwrap_err()
.to_string();
assert!(err.starts_with("--remove-tag:"), "{bad:?}: {err}");
}
assert!(TagRemoval::parse(&["M1".to_string()]).is_ok());
}
#[test]
fn parse_all_none() {
assert_eq!(FastqTags::parse("all").unwrap(), FastqTags::All);
assert_eq!(FastqTags::parse("none").unwrap(), FastqTags::None);
}
#[test]
fn parse_list_collects_tags() {
let t = FastqTags::parse("MM,ML,RG").unwrap();
match t {
FastqTags::Only(ref s) => {
assert!(s.contains(b"MM") && s.contains(b"ML") && s.contains(b"RG"));
assert_eq!(s.len(), 3);
},
other => panic!("Expected Only, got {other:?}"),
}
}
#[test]
fn parse_rejects_bad_token() {
assert!(FastqTags::parse("MM,ABC").is_err()); assert!(FastqTags::parse("").is_err()); assert!(FastqTags::parse("MM,").is_err()); }
#[test]
fn parse_rejects_non_ascii_two_byte_token() {
assert!(FastqTags::parse("é").is_err());
assert!(FastqTags::parse("RG").is_ok());
}
#[test]
fn carries_rules() {
assert!(FastqTags::All.carries(b"RG"));
assert!(FastqTags::All.carries_mods());
assert!(!FastqTags::None.carries(b"RG"));
assert!(!FastqTags::None.carries_mods());
let only = FastqTags::parse("ML,RG").unwrap();
assert!(only.carries(b"RG"));
assert!(!only.carries(b"XY"));
assert!(only.carries_mods());
let mn_only = FastqTags::parse("MN").unwrap();
assert!(!mn_only.carries_mods());
}
}