From ea786af2882601b347b9750f3491bd6568cf64c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rayan=20Hassa=C3=AFne?= Date: Wed, 25 Nov 2026 12:43:24 +0100 Subject: [PATCH] fix: make outputs independent of hash order and --threads Several outputs changed between runs, or between thread counts, because they depended on HashMap iteration order or on how chromosomes were split across workers. Thread count (per-chromosome workers merged in a different order): - inner_distance: pairs were concatenated in worker order and cut at the sample size, and the distance list was cut at the same length as the pair list although it has no entries for cross-chromosome pairs. Pairs are now merged in chromosome order, cut, and the distances rebuilt from the kept pairs. Each worker's chromosome batch is now in ascending order so its own cut keeps the right pairs. - dupRadar/featureCounts mate pairing: the merge took the duplicate and multi-mapper flags of whichever mate the first worker held, and a repeated mate key (no HI tag, duplicated records) overwrote the waiting mate, so reads were dropped differently depending on the split. Mates now wait in a per-key FIFO queue that pairs read1 with read2 in arrival order, giving the same pairs in one pass or after a merge, and a fragment counts as duplicate / multi-mapped if either mate is, as featureCounts does (Rsubread readSummary.c, loop over both ends in process_pairer_output). Hash order: - dupRadar density scatter: pixels with equal density are now drawn in pixel order, so the SVG/PNG no longer change from run to run. - chromosome_mapping: two GTF names mapped to one alignment name made the reverse lookup pick one at random. Such a config is now rejected at load time with a clear error. - The chromosome-mismatch error listed 5 random GTF chromosomes; it now lists the first 5 in GTF order. - Qualimap COLLISION debug lines are printed in ascending mean order. - MergedGeneCoverage::iter yields genes in index order. The Qualimap unmapped-segment reads are now looked up by their GTF chromosome name, like every other read. Single-threaded output is unchanged on the benchmark datasets; the parallel path now reproduces it byte for byte. Co-Authored-By: Claude Opus 5.5 --- docs/src/content/docs/usage/configuration.md | 4 + src/config.rs | 96 +++- src/rna/dupradar/counting.rs | 483 +++++++++++-------- src/rna/dupradar/plots.rs | 12 +- src/rna/qualimap/coverage.rs | 10 +- src/rna/qualimap/output.rs | 6 +- src/rna/rseqc/accumulators.rs | 150 +++++- 7 files changed, 533 insertions(+), 228 deletions(-) diff --git a/docs/src/content/docs/usage/configuration.md b/docs/src/content/docs/usage/configuration.md index 85dd9ed3..3c0799ef 100644 --- a/docs/src/content/docs/usage/configuration.md +++ b/docs/src/content/docs/usage/configuration.md @@ -335,6 +335,10 @@ An explicit mapping from GTF chromosome names (keys) to alignment file chromosom names (values). Applied after the prefix, so explicit entries can override the prefix for specific chromosomes. +Each alignment chromosome name may appear only once as a value. A mapping that +sends two GTF chromosomes to the same alignment chromosome is rejected when the +configuration is loaded. + ```yaml rna: # After adding "chr" prefix, override the mitochondrial chromosome diff --git a/src/config.rs b/src/config.rs index b826348f..c178458f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -912,20 +912,62 @@ pub fn load_merged_config( let config: Config = serde_yaml_ng::from_value(merged).context("Failed to deserialize merged configuration")?; + config.rna.validate().with_context(|| { + let files: Vec = paths.iter().map(|(p, _)| p.display().to_string()).collect(); + format!("Invalid configuration (from {})", files.join(", ")) + })?; Ok((config, paths)) } impl RnaConfig { + /// Check settings that deserialization alone cannot catch. + /// + /// Currently: `chromosome_mapping` must not map two GTF chromosomes to the + /// same alignment chromosome. Reads are looked up by their alignment + /// name, so such a mapping has no single answer. + pub fn validate(&self) -> Result<()> { + let mut by_aln: HashMap<&str, Vec<&str>> = HashMap::new(); + for (gtf_name, aln_name) in &self.chromosome_mapping { + by_aln.entry(aln_name).or_default().push(gtf_name); + } + let mut clashes: Vec = by_aln + .into_iter() + .filter(|(_, gtf_names)| gtf_names.len() > 1) + .map(|(aln_name, mut gtf_names)| { + gtf_names.sort_unstable(); + format!("'{}' <- {}", aln_name, gtf_names.join(", ")) + }) + .collect(); + if !clashes.is_empty() { + clashes.sort_unstable(); + anyhow::bail!( + "chromosome_mapping maps several GTF chromosomes to the same alignment \ + chromosome: {}. Each alignment chromosome may appear only once as a value.", + clashes.join("; ") + ); + } + Ok(()) + } + /// Build a reverse mapping: alignment chromosome name -> GTF chromosome name. /// /// The config file maps GTF -> alignment, but at lookup time we need /// alignment -> GTF (since the index is keyed by GTF names). /// Explicit chromosome_mapping entries take priority over the prefix. + /// + /// The mapping should have passed [`RnaConfig::validate`]. If it maps two + /// GTF names to one alignment name anyway, the GTF name that sorts first + /// wins, so the result never depends on hash order. pub fn alignment_to_gtf_mapping(&self) -> HashMap { - self.chromosome_mapping - .iter() - .map(|(gtf_name, aln_name)| (aln_name.clone(), gtf_name.clone())) - .collect() + let mut pairs: Vec<(&String, &String)> = self.chromosome_mapping.iter().collect(); + pairs.sort_unstable(); + let mut reverse = HashMap::with_capacity(pairs.len()); + for (gtf_name, aln_name) in pairs { + reverse + .entry(aln_name.clone()) + .or_insert_with(|| gtf_name.clone()); + } + reverse } /// Returns the chromosome prefix if configured. @@ -1006,6 +1048,52 @@ mod tests { assert_eq!(config.rna.split_bam.active_bed(), None); } + #[test] + fn test_chromosome_mapping_rejects_shared_alignment_name() { + let yaml = r#" +chromosome_mapping: + chrM: "MT" + chrMT: "MT" + chr1: "1" +"#; + let config: RnaConfig = serde_yaml_ng::from_str(yaml).unwrap(); + let err = config.validate().unwrap_err().to_string(); + assert!(err.contains("'MT' <- chrM, chrMT"), "{err}"); + + // The reverse mapping is still deterministic: the GTF name that sorts first wins. + let reverse = config.alignment_to_gtf_mapping(); + assert_eq!(reverse.get("MT").map(String::as_str), Some("chrM")); + assert_eq!(reverse.get("1").map(String::as_str), Some("chr1")); + } + + #[test] + fn test_chromosome_mapping_one_to_one_is_valid() { + let yaml = r#" +chromosome_mapping: + chrM: "MT" + chr1: "1" +"#; + let config: RnaConfig = serde_yaml_ng::from_str(yaml).unwrap(); + config.validate().unwrap(); + assert_eq!(config.alignment_to_gtf_mapping().len(), 2); + } + + #[test] + fn test_load_merged_config_rejects_shared_alignment_name() { + let dir = std::env::temp_dir().join(format!("rustqc_cfg_dup_{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("config.yaml"); + std::fs::write( + &path, + "rna:\n chromosome_mapping:\n chrM: MT\n chrMT: MT\n", + ) + .unwrap(); + let err = load_merged_config(Some(path.to_str().unwrap())).unwrap_err(); + let msg = format!("{err:#}"); + std::fs::remove_dir_all(&dir).ok(); + assert!(msg.contains("'MT' <- chrM, chrMT"), "{msg}"); + } + #[test] fn test_top_level_rna_wrapper() { let yaml = r#" diff --git a/src/rna/dupradar/counting.rs b/src/rna/dupradar/counting.rs index 68bedf9b..ed06f542 100644 --- a/src/rna/dupradar/counting.rs +++ b/src/rna/dupradar/counting.rs @@ -21,7 +21,7 @@ use indicatif::ProgressBar; use log::{debug, warn}; use rayon::prelude::*; use rust_htslib::bam::{self, FetchDefinition, Read as BamRead}; -use std::collections::HashMap; +use std::collections::{HashMap, VecDeque}; use std::sync::atomic::{AtomicU64, Ordering}; // =================================================================== @@ -500,6 +500,72 @@ fn hash_qname(qname: &[u8]) -> u64 { crate::io::fnv1a(qname) } +/// Paired-end mates waiting for their partner, keyed by [`MateBufferKey`]. +/// +/// A key normally holds one waiting mate. Unusual input (duplicated records, +/// secondary alignments without an HI tag) can give several mates of the same +/// direction (read1 or read2) one key. Those queue up in arrival order, and +/// each read of the other direction pairs with the oldest one. This gives the +/// same pairs whether all of a key's mates go through one buffer (the +/// single-threaded path) or through two per-chromosome workers whose buffers +/// are merged afterwards, so the counts don't depend on `--threads`. A read +/// is never paired with a mate of its own direction, and none is dropped. +#[derive(Debug, Default)] +struct MateBuffer { + /// Oldest waiting mate per key. + first: HashMap, + /// Further waiting mates per key (same direction as `first`), oldest first. + extra: HashMap>, +} + +impl MateBuffer { + /// Remove and return the oldest waiting mate under `key` that can pair + /// with a read of direction `is_read1`, if any. + fn take_mate(&mut self, key: &MateBufferKey, is_read1: bool) -> Option { + if self.first.get(key)?.is_read1 == is_read1 { + return None; + } + let mate = self.first.remove(key)?; + if let Some(queue) = self.extra.get_mut(key) { + if let Some(next) = queue.pop_front() { + self.first.insert(*key, next); + } + if queue.is_empty() { + self.extra.remove(key); + } + } + Some(mate) + } + + /// Add a mate that found no partner (call after `take_mate` returned None). + fn push(&mut self, key: MateBufferKey, info: MateInfo) { + match self.first.entry(key) { + std::collections::hash_map::Entry::Vacant(e) => { + e.insert(info); + } + std::collections::hash_map::Entry::Occupied(_) => { + self.extra.entry(key).or_default().push_back(info); + } + } + } + + fn is_empty(&self) -> bool { + self.first.is_empty() + } + + /// All waiting mates, each key's mates in arrival order. + fn into_entries(mut self) -> Vec<(MateBufferKey, MateInfo)> { + let mut out = Vec::with_capacity(self.first.len()); + for (key, info) in self.first { + out.push((key, info)); + if let Some(queue) = self.extra.remove(&key) { + out.extend(queue.into_iter().map(|m| (key, m))); + } + } + out + } +} + /// Accumulated results from processing a batch of chromosomes. /// Each parallel worker produces one of these, which are then merged. #[derive(Debug)] @@ -507,7 +573,7 @@ struct ChromResult { /// Per-gene counts (flat vec indexed by GeneIdx) gene_counts: Vec, /// Unmatched mates left over from this chromosome batch - unmatched_mates: HashMap, + unmatched_mates: MateBuffer, /// Total reads seen (all reads, including skipped) total_reads: u64, /// Total mapped reads (after filtering) @@ -566,7 +632,7 @@ impl ChromResult { fn new(num_genes: usize, num_biotypes: usize) -> Self { ChromResult { gene_counts: vec![GeneCounts::default(); num_genes], - unmatched_mates: HashMap::new(), + unmatched_mates: MateBuffer::default(), total_reads: 0, total_mapped: 0, total_dup: 0, @@ -595,6 +661,47 @@ impl ChromResult { } } + /// Count a paired-end fragment whose two mates have been matched: + /// the buffered `mate` and a read with the given gene hits and flags. + /// + /// featureCounts skips the whole fragment as a duplicate (`ignoreDup`) + /// or a multi-mapper (no `countMultiMappingReads`) if *either* end has + /// the 0x400 flag or NH > 1 (Rsubread `readSummary.c`, + /// `process_pairer_output`, loop over both ends). So the fragment flags + /// are the OR of the two mates. This is symmetric, so it does not matter + /// which mate was buffered. + fn count_pair(&mut self, mate: &MateInfo, gene_hits: &[GeneIdx], is_dup: bool, is_multi: bool) { + let frag_is_dup = mate.is_dup || is_dup; + let frag_is_multi = mate.is_multi || is_multi; + + self.total_fragments += 1; + self.n_multi_dup += 1; + self.n_unique_dup += 1; + if !frag_is_dup { + self.n_multi_nodup += 1; + self.n_unique_nodup += 1; + } + + // featureCounts union-with-both-end-preference voting: each gene + // gets +1 per mate that overlaps it (max 2). Genes hit by both + // mates (score 2) beat genes hit by only one (score 1). If + // multiple genes tie at the highest score the fragment is + // ambiguous. This matches Rsubread::featureCounts behaviour. + let combined_genes = merge_gene_hits(&mate.gene_hits, gene_hits); + if combined_genes.is_empty() { + self.stat_no_features += 1; + } else if combined_genes.len() > 1 { + self.stat_ambiguous += 1; + } else if assign_fragment_to_gene( + &combined_genes, + &mut self.gene_counts, + frag_is_dup, + frag_is_multi, + ) { + self.stat_assigned += 1; + } + } + /// Merge another ChromResult into this one (additive). fn merge(&mut self, other: ChromResult) { for (i, counts) in other.gene_counts.into_iter().enumerate() { @@ -604,48 +711,18 @@ impl ChromResult { self.gene_counts[i].nodup_unique += counts.nodup_unique; self.gene_counts[i].fc_reads += counts.fc_reads; } - // Merge unmatched mates — pair any cross-chromosome mates immediately - // when both workers have buffered reads from the same pair (same key). - // Using HashMap::extend would silently overwrite one mate, losing it. - for (key, other_info) in other.unmatched_mates { - if let Some(self_info) = self.unmatched_mates.remove(&key) { - if self_info.is_read1 != other_info.is_read1 { - // Genuine cross-worker pair: read1 ↔ read2 - self.total_fragments += 1; - let frag_is_dup = self_info.is_dup; - let frag_is_multi = self_info.is_multi; - self.n_multi_dup += 1; - self.n_unique_dup += 1; - if !frag_is_dup { - self.n_multi_nodup += 1; - self.n_unique_nodup += 1; - } - let combined_genes = - merge_gene_hits(&self_info.gene_hits, &other_info.gene_hits); - if combined_genes.is_empty() { - self.stat_no_features += 1; - } else if combined_genes.len() > 1 { - self.stat_ambiguous += 1; - } else if assign_fragment_to_gene( - &combined_genes, - &mut self.gene_counts, - frag_is_dup, - frag_is_multi, - ) { - self.stat_assigned += 1; - } - } else { - // False match: same read direction (e.g. secondary alignment - // of a singleton in different workers). Put self_info back. - self.unmatched_mates.insert(key, self_info); - // other_info has same key — can't insert into HashMap without - // overwriting. It will be counted as a singleton at reconciliation - // since the first entry remains in the map. - // We lose this entry, but R featureCounts also handles secondary - // duplicates this way (only one alignment per QNAME at same pos). - } - } else { - self.unmatched_mates.insert(key, other_info); + // Merge unmatched mates: a mate of a pair whose other mate was + // buffered by the other worker (mates on different chromosomes) is + // paired now, exactly as if both had gone through one buffer. + for (key, other_info) in other.unmatched_mates.into_entries() { + match self.unmatched_mates.take_mate(&key, other_info.is_read1) { + Some(self_info) => self.count_pair( + &self_info, + &other_info.gene_hits, + other_info.is_dup, + other_info.is_multi, + ), + None => self.unmatched_mates.push(key, other_info), } } self.total_reads += other.total_reads; @@ -831,7 +908,7 @@ fn process_counting_record( aligned_blocks_buf: &mut Vec<(u64, u64)>, gene_hits: &mut Vec, biotype_hits_buf: &mut Vec, - mate_buffer: &mut HashMap, + mate_buffer: &mut MateBuffer, ) { let flags = record.flags(); @@ -939,69 +1016,23 @@ fn process_counting_record( (qname_hash, mate_tid, mate_pos_val, own_tid, own_pos, hi_tag) }; - // Check if the mate is already in the buffer. We must verify the match - // is a genuine read1↔read2 pair, not a secondary alignment of the *same* - // read (which produces an identical key when the mate is unmapped, because - // both primary and secondary share qname_hash, position, and mate=-1/-1). - let matched = mate_buffer.remove(&buffer_key).and_then(|mate_info| { - if mate_info.is_read1 != is_read1 { - // Genuine pair: read1 matched read2 (or vice versa) - Some(mate_info) - } else { - // False match: same read direction (e.g. secondary alignment of a - // singleton). Put the original entry back and treat this read as - // a new buffer insert. - mate_buffer.insert(buffer_key, mate_info); - None - } - }); - - if let Some(mate_info) = matched { - result.total_fragments += 1; - - let frag_is_dup = if is_read1 { is_dup } else { mate_info.is_dup }; - let frag_is_multi = if is_read1 { - is_multi - } else { - mate_info.is_multi - }; - result.n_multi_dup += 1; - result.n_unique_dup += 1; - if !frag_is_dup { - result.n_multi_nodup += 1; - result.n_unique_nodup += 1; - } - - // featureCounts union-with-both-end-preference voting: each gene - // gets +1 per mate that overlaps it (max 2). Genes hit by both - // mates (score 2) beat genes hit by only one (score 1). If - // multiple genes tie at the highest score the fragment is - // ambiguous. This matches Rsubread::featureCounts behaviour. - let combined_genes: Vec = merge_gene_hits(&mate_info.gene_hits, gene_hits); - - if combined_genes.is_empty() { - result.stat_no_features += 1; - } else if combined_genes.len() > 1 { - result.stat_ambiguous += 1; - } else if assign_fragment_to_gene( - &combined_genes, - &mut result.gene_counts, - frag_is_dup, - frag_is_multi, - ) { - result.stat_assigned += 1; + // Pair with the oldest buffered mate of the other direction. A read is + // never paired with one of its own direction: a secondary alignment of a + // singleton has the same key as its primary when there is no HI tag. + match mate_buffer.take_mate(&buffer_key, is_read1) { + Some(mate_info) => result.count_pair(&mate_info, gene_hits, is_dup, is_multi), + None => { + // Take ownership of gene_hits instead of cloning (it's cleared at loop start) + mate_buffer.push( + buffer_key, + MateInfo { + gene_hits: std::mem::take(gene_hits), + is_dup, + is_multi, + is_read1, + }, + ); } - } else { - // Take ownership of gene_hits instead of cloning (it's cleared at loop start) - mate_buffer.insert( - buffer_key, - MateInfo { - gene_hits: std::mem::take(gene_hits), - is_dup, - is_multi, - is_read1, - }, - ); } } @@ -1040,18 +1071,7 @@ fn process_chromosome_batch( // Pre-compute resolved chromosome names per TID (apply prefix/mapping once, // not on every read). Used by both featureCounts counting and RSeQC tools. - let tid_to_gtf_chrom: Vec = tid_to_name - .iter() - .map(|bam_name| { - if let Some(mapped) = chrom_mapping.get(bam_name.as_str()) { - mapped.clone() - } else if let Some(prefix) = chrom_prefix { - format!("{}{}", prefix, bam_name) - } else { - bam_name.clone() - } - }) - .collect(); + let tid_to_gtf_chrom = gtf_chrom_names(tid_to_name, chrom_mapping, chrom_prefix); let tid_to_rseqc_chrom = &tid_to_gtf_chrom; let tid_to_rseqc_chrom_upper: Vec = tid_to_gtf_chrom.iter().map(|s| s.to_uppercase()).collect(); @@ -1072,7 +1092,7 @@ fn process_chromosome_batch( let mut aligned_blocks_buf: Vec<(u64, u64)> = Vec::new(); let mut gene_hits: Vec = Vec::new(); let mut biotype_hits_buf: Vec = Vec::new(); - let mut mate_buffer: HashMap = HashMap::new(); + let mut mate_buffer = MateBuffer::default(); let mut record = bam::Record::new(); for &tid in tids { @@ -1153,10 +1173,35 @@ fn process_chromosome_batch( Ok((result, rseqc_accums)) } +/// Resolve each alignment chromosome (by tid) to its GTF name: an explicit +/// `chromosome_mapping` entry first, else the configured prefix, else as is. +fn gtf_chrom_names( + tid_to_name: &[String], + chrom_mapping: &HashMap, + chrom_prefix: Option<&str>, +) -> Vec { + tid_to_name + .iter() + .map(|bam_name| { + if let Some(mapped) = chrom_mapping.get(bam_name.as_str()) { + mapped.clone() + } else if let Some(prefix) = chrom_prefix { + format!("{}{}", prefix, bam_name) + } else { + bam_name.clone() + } + }) + .collect() +} + /// Partition chromosome indices across workers using greedy bin-packing /// (largest-first scheduling). Assigns each chromosome to the worker with the /// smallest current total length, producing a more balanced distribution than /// round-robin when chromosome sizes vary widely. +/// +/// Each batch lists its chromosomes in ascending tid order (file order), so a +/// worker meets them in the same order as a single pass over the file. The +/// accumulators that keep only the first N items of the file rely on this. fn partition_chromosomes(lengths: &[u64], num_workers: usize) -> Vec> { let mut batches: Vec> = vec![Vec::new(); num_workers]; let mut loads: Vec = vec![0; num_workers]; @@ -1176,6 +1221,9 @@ fn partition_chromosomes(lengths: &[u64], num_workers: usize) -> Vec> { batches[min_worker].push(tid); loads[min_worker] += lengths[tid as usize]; } + for batch in &mut batches { + batch.sort_unstable(); + } batches } @@ -1387,18 +1435,7 @@ pub fn count_reads( // Pre-compute resolved chromosome names for RSeQC tools // (apply chromosome prefix and mapping, same as parallel path) - let tid_to_rseqc_chrom: Vec = tid_to_name - .iter() - .map(|bam_name| { - if let Some(mapped) = chrom_mapping.get(bam_name.as_str()) { - mapped.clone() - } else if let Some(prefix) = chrom_prefix { - format!("{}{}", prefix, bam_name) - } else { - bam_name.clone() - } - }) - .collect(); + let tid_to_rseqc_chrom = gtf_chrom_names(&tid_to_name, chrom_mapping, chrom_prefix); let tid_to_rseqc_chrom_upper: Vec = tid_to_rseqc_chrom .iter() .map(|s| s.to_uppercase()) @@ -1420,7 +1457,7 @@ pub fn count_reads( let mut aligned_blocks_buf: Vec<(u64, u64)> = Vec::new(); let mut gene_hits: Vec = Vec::new(); let mut biotype_hits_buf: Vec = Vec::new(); - let mut mate_buffer: HashMap = HashMap::new(); + let mut mate_buffer = MateBuffer::default(); let mut record = bam::Record::new(); while let Some(read_result) = bam.read(&mut record) { @@ -1504,6 +1541,8 @@ pub fn count_reads( // never visits reads in the unmapped segment (tid=-1) at the end of the // BAM file. We sweep them here so that bam_stat totals, fc_unmapped, // and RSeQC accumulators see every record. + // GTF names per tid, as the workers resolved them. + let gtf_chroms = gtf_chrom_names(&tid_to_name, chrom_mapping, chrom_prefix); if use_parallel { let mut bam = bam::IndexedReader::from_path(bam_path).with_context(|| { format!( @@ -1540,8 +1579,8 @@ pub fn count_reads( // Mapped read in the unmapped segment (placed at mate's position). // Pass to process_read() for full Qualimap processing. if let Some(qm_index) = qualimap_index { - if (tid as usize) < tid_to_name.len() { - qm.process_read(&record, &tid_to_name[tid as usize], qm_index); + if (tid as usize) < gtf_chroms.len() { + qm.process_read(&record, >f_chroms[tid as usize], qm_index); } } } else if record.flags() & BAM_FUNMAP != 0 { @@ -1569,69 +1608,12 @@ pub fn count_reads( } } - // --- Cross-chromosome mate reconciliation --- + // --- Remaining unmatched mates --- // - // In parallel mode, mates on different chromosomes end up in different - // workers' mate buffers. Reconcile them here: try to match each unmatched - // mate against the others. Any that still don't match are treated as - // singletons (same as the sequential path). + // Mates on different chromosomes were already paired while merging the + // workers' buffers, so what is left never found its partner. if paired && !merged.unmatched_mates.is_empty() { - // Drain all unmatched mates and try to pair them - let unmatched: Vec<(MateBufferKey, MateInfo)> = merged.unmatched_mates.drain().collect(); - let mut still_unmatched: HashMap = HashMap::new(); - - for (key, info) in unmatched { - if let Some(mate_info) = still_unmatched.remove(&key) { - if info.is_read1 == mate_info.is_read1 { - // False match: same read direction (secondary alignment of a - // singleton). Put the first entry back, drop the second - // (HashMap only holds one entry per key). - still_unmatched.insert(key, mate_info); - continue; - } - merged.total_fragments += 1; - - // Use read1's dup/multi status for the fragment. - let frag_is_dup = if info.is_read1 { - info.is_dup - } else { - mate_info.is_dup - }; - let frag_is_multi = if info.is_read1 { - info.is_multi - } else { - mate_info.is_multi - }; - - merged.n_multi_dup += 1; - merged.n_unique_dup += 1; - if !frag_is_dup { - merged.n_multi_nodup += 1; - merged.n_unique_nodup += 1; - } - - // Combine gene hits with featureCounts scoring - let combined_genes: Vec = - merge_gene_hits(&mate_info.gene_hits, &info.gene_hits); - - if combined_genes.is_empty() { - merged.stat_no_features += 1; - } else if combined_genes.len() > 1 { - merged.stat_ambiguous += 1; - } else if assign_fragment_to_gene( - &combined_genes, - &mut merged.gene_counts, - frag_is_dup, - frag_is_multi, - ) { - merged.stat_assigned += 1; - // Coverage recording skipped — no aligned blocks available for - // cross-chromosome mate reconciliation. - } - } else { - still_unmatched.insert(key, info); - } - } + let still_unmatched = std::mem::take(&mut merged.unmatched_mates).into_entries(); // Handle remaining singletons (mates whose partner was never seen). // featureCounts defaults to requireBothEndsMapped=FALSE, meaning @@ -1643,7 +1625,7 @@ pub fn count_reads( // These reads were already classified individually through // classify_read_fc() when first encountered, so we do NOT // increment fc_singleton here (that would double-count). - for (_key, mate_info) in still_unmatched.drain() { + for (_key, mate_info) in still_unmatched { merged.total_fragments += 1; // Include singletons in library size counts for RPKM calculation. // R dupRadar's analyzeDuprates() uses N = sum(stat[,2]) - Unmapped @@ -1704,7 +1686,16 @@ pub fn count_reads( .count(); if merged.total_mapped > 0 && genes_with_reads == 0 { let bam_chroms: Vec<&str> = tid_to_name.iter().take(5).map(|s| s.as_str()).collect(); - let gtf_chroms: Vec<&str> = index.keys().take(5).map(|s| s.as_str()).collect(); + // First 5 annotated chromosomes in GTF order (not the index's hash order). + let mut gtf_chroms: Vec<&str> = Vec::with_capacity(5); + for exon in genes.values().flat_map(|g| g.exons.iter()) { + if gtf_chroms.len() == 5 { + break; + } + if !gtf_chroms.contains(&exon.chrom.as_str()) { + gtf_chroms.push(exon.chrom.as_str()); + } + } anyhow::bail!( "Chromosome name mismatch: no reads could be assigned to any gene.\n\ \n\ @@ -2086,6 +2077,100 @@ mod tests { } } + fn mate(gene: GeneIdx, is_read1: bool, is_dup: bool) -> MateInfo { + MateInfo { + gene_hits: vec![gene], + is_dup, + is_multi: false, + is_read1, + } + } + + /// Feed one read through a mate buffer the way `process_counting_record` does. + fn feed(result: &mut ChromResult, key: MateBufferKey, info: MateInfo) { + match result.unmatched_mates.take_mate(&key, info.is_read1) { + Some(m) => result.count_pair(&m, &info.gene_hits, info.is_dup, info.is_multi), + None => result.unmatched_mates.push(key, info), + } + } + + fn summary(r: &ChromResult) -> (u64, u64, u64, Vec<(u64, u64, u64, u64)>) { + let genes = r + .gene_counts + .iter() + .map(|g| (g.all_multi, g.nodup_multi, g.all_unique, g.nodup_unique)) + .collect(); + (r.total_fragments, r.stat_assigned, r.stat_ambiguous, genes) + } + + #[test] + fn test_mate_buffer_same_pairs_single_pass_and_merged() { + // One key, mates on two chromosomes: two read1 records (chrom A, read + // first) and two read2 records (chrom B). FIFO pairing gives + // (r1a, r2a) and (r1b, r2b): both assigned to one gene. + let key: MateBufferKey = (42, 0, 100, 1, 200, -1); + let reads_a = || vec![mate(0, true, false), mate(1, true, false)]; + let reads_b = || vec![mate(0, false, false), mate(1, false, false)]; + + let mut single = ChromResult::new(2, 0); + for info in reads_a().into_iter().chain(reads_b()) { + feed(&mut single, key, info); + } + assert!(single.unmatched_mates.is_empty()); + + for a_first in [true, false] { + let mut a = ChromResult::new(2, 0); + reads_a().into_iter().for_each(|i| feed(&mut a, key, i)); + let mut b = ChromResult::new(2, 0); + reads_b().into_iter().for_each(|i| feed(&mut b, key, i)); + let merged = if a_first { + a.merge(b); + a + } else { + b.merge(a); + b + }; + assert!(merged.unmatched_mates.is_empty()); + assert_eq!(summary(&merged), summary(&single)); + } + assert_eq!(single.stat_assigned, 2); + assert_eq!(single.stat_ambiguous, 0); + } + + #[test] + fn test_mate_buffer_fragment_is_duplicate_if_either_mate_is() { + let key: MateBufferKey = (7, 0, 10, 0, 50, -1); + for (r1_dup, r2_dup) in [(true, false), (false, true)] { + let mut r = ChromResult::new(1, 0); + feed(&mut r, key, mate(0, true, r1_dup)); + feed(&mut r, key, mate(0, false, r2_dup)); + assert_eq!(summary(&r).3, vec![(1, 0, 1, 0)], "r1_dup={r1_dup}"); + assert_eq!(r.n_multi_nodup, 0); + } + } + + #[test] + fn test_mate_buffer_never_pairs_same_direction() { + let key: MateBufferKey = (9, 0, 10, 0, 10, -1); + let mut r = ChromResult::new(1, 0); + feed(&mut r, key, mate(0, true, false)); + feed(&mut r, key, mate(0, true, false)); + assert_eq!(r.total_fragments, 0); + assert_eq!(r.unmatched_mates.into_entries().len(), 2); + } + + #[test] + fn test_partition_chromosomes_batches_in_file_order() { + let lengths = vec![ + 250, 243, 198, 191, 182, 171, 159, 146, 138, 133, 135, 130, 57, + ]; + for workers in 1..=6 { + for batch in partition_chromosomes(&lengths, workers) { + assert!(batch.windows(2).all(|w| w[0] < w[1]), "{batch:?}"); + } + } + } + #[test] fn test_partition_chromosomes_single_worker() { let lengths = vec![100, 200, 300]; diff --git a/src/rna/dupradar/plots.rs b/src/rna/dupradar/plots.rs index e08c251b..45b46af5 100644 --- a/src/rna/dupradar/plots.rs +++ b/src/rna/dupradar/plots.rs @@ -597,13 +597,15 @@ where .or_insert((x, y, d)); } - // Sort by density ascending so high-density points draw on top - let mut deduped: Vec<(f64, f64, f64)> = pixel_map.into_values().collect(); - deduped.sort_by(|a, b| a.2.partial_cmp(&b.2).unwrap_or(std::cmp::Ordering::Equal)); + // Sort by density ascending so high-density points draw on top. Equal + // densities are ordered by pixel (x, then y): the map's iteration order is + // random, and the draw order is written into the SVG. + let mut deduped: Vec<_> = pixel_map.into_iter().collect(); + deduped.sort_by(|(pa, a), (pb, b)| a.2.total_cmp(&b.2).then(pa.cmp(pb))); - let max_dens = deduped.iter().map(|d| d.2).fold(0.0f64, f64::max); + let max_dens = deduped.iter().map(|(_, d)| d.2).fold(0.0f64, f64::max); - for (x, y, d) in deduped { + for (_, (x, y, d)) in deduped { let t = if max_dens > 0.0 { d / max_dens } else { 0.0 }; let c = density_color(t); chart.draw_series(std::iter::once(Circle::new( diff --git a/src/rna/qualimap/coverage.rs b/src/rna/qualimap/coverage.rs index 515bd4ac..70773b2b 100644 --- a/src/rna/qualimap/coverage.rs +++ b/src/rna/qualimap/coverage.rs @@ -268,10 +268,16 @@ impl MergedGeneCoverage { self.coverage.get(&gene_idx).map(|v| v.as_slice()) } - /// Iterate over all genes with coverage data. + /// Iterate over all genes with coverage data, in ascending gene index order. #[allow(dead_code)] pub fn iter(&self) -> impl Iterator { - self.coverage.iter().map(|(&idx, v)| (idx, v.as_slice())) + let mut genes: Vec<(u32, &[i32])> = self + .coverage + .iter() + .map(|(&idx, v)| (idx, v.as_slice())) + .collect(); + genes.sort_unstable_by_key(|&(idx, _)| idx); + genes.into_iter() } /// Number of genes that have any coverage recorded. diff --git a/src/rna/qualimap/output.rs b/src/rna/qualimap/output.rs index a4f9a18e..09ae9f08 100644 --- a/src/rna/qualimap/output.rs +++ b/src/rna/qualimap/output.rs @@ -358,9 +358,9 @@ pub fn write_qualimap_results( num_collisions ); if num_collisions > 0 { - // Report the mean values that collided - let mut mean_counts: std::collections::HashMap> = - std::collections::HashMap::new(); + // Report the mean values that collided, in ascending mean order + // (positive f64 bit patterns sort like the values). + let mut mean_counts: BTreeMap> = BTreeMap::new(); for (idx, entry) in entries.iter().enumerate() { if entry.mean_coverage > 0.0 { mean_counts diff --git a/src/rna/rseqc/accumulators.rs b/src/rna/rseqc/accumulators.rs index f990dc1e..1b64c8e8 100644 --- a/src/rna/rseqc/accumulators.rs +++ b/src/rna/rseqc/accumulators.rs @@ -1757,6 +1757,8 @@ pub struct InnerDistAccum { pub pair_num: u64, /// Maximum pairs to sample. pub sample_size: u64, + /// Chromosome runs of `pairs` (see [`merge_chrom_runs`]). + pair_runs: Vec<(i32, usize)>, } impl InnerDistAccum { @@ -1822,6 +1824,7 @@ impl InnerDistAccum { distance: None, classification: "sameChrom=No", }); + push_chrom_run(&mut self.pair_runs, record.tid()); return; } @@ -1898,6 +1901,7 @@ impl InnerDistAccum { distance: Some(mrna_dist), classification: "sameTranscript=Yes,sameExon=No,dist=mRNA", }); + push_chrom_run(&mut self.pair_runs, record.tid()); self.distances.push(mrna_dist); return; } else { @@ -1913,27 +1917,30 @@ impl InnerDistAccum { distance: Some(inner_dist), classification, }); + push_chrom_run(&mut self.pair_runs, record.tid()); self.distances.push(inner_dist); } /// Merge another accumulator into this one, respecting the sample size limit. /// - /// In parallel mode each worker accumulates pairs independently. After - /// merging all workers the total may exceed `sample_size`, so we truncate - /// to the limit. This matches the upstream RSeQC behaviour of sampling - /// at most `sample_size` pairs regardless of processing order. + /// Upstream RSeQC keeps the first `sample_size` pairs of the file. Each + /// worker keeps the first `sample_size` pairs of its own chromosomes + /// (processed in ascending order), so putting the pairs back in + /// chromosome order and cutting at `sample_size` gives the same pairs as + /// a single pass, whatever the thread count. pub fn merge(&mut self, other: InnerDistAccum) { - self.pairs.extend(other.pairs); - self.distances.extend(other.distances); - self.pair_num += other.pair_num; - - // Enforce the sampling limit after merging - if self.pair_num > self.sample_size { - let limit = self.sample_size as usize; - self.pairs.truncate(limit); - self.distances.truncate(limit); - self.pair_num = self.sample_size; - } + merge_chrom_runs( + &mut self.pairs, + &mut self.pair_runs, + other.pairs, + other.pair_runs, + ); + let limit = self.sample_size.min(self.pairs.len() as u64) as usize; + truncate_chrom_runs(&mut self.pairs, &mut self.pair_runs, limit); + self.pair_num = self.pairs.len() as u64; + // Every pair except those with mates on different chromosomes has a + // distance, in the same order. + self.distances = self.pairs.iter().filter_map(|p| p.distance).collect(); } } @@ -2237,6 +2244,83 @@ fn point_in(region_map: &HashMap, chrom: &str, point: u6 region_map.get(chrom).is_some_and(|ci| ci.contains(point)) } +// =================================================================== +// Chromosome-ordered merging +// =================================================================== + +/// Record that one more item was appended for chromosome `tid`. +/// +/// `runs` describes a vector of items as consecutive `(tid, count)` runs. +/// Accumulators whose output depends on the order of the items they collect +/// (inner_distance's first-N sample) keep such runs so that +/// [`merge_chrom_runs`] can restore file order. +#[inline] +fn push_chrom_run(runs: &mut Vec<(i32, usize)>, tid: i32) { + match runs.last_mut() { + Some((last_tid, count)) if *last_tid == tid => *count += 1, + _ => runs.push((tid, 1)), + } +} + +/// Append `other_items` to `items` and reorder the result by chromosome. +/// +/// Each parallel worker reads whole chromosomes, but which chromosomes a +/// worker gets depends on the thread count. Sorting the runs by tid (a stable +/// sort, so the order within a chromosome is kept) gives the order of a +/// single pass over a coordinate-sorted file, so the merged result is the +/// same for any `--threads` and any merge order. +fn merge_chrom_runs( + items: &mut Vec, + runs: &mut Vec<(i32, usize)>, + other_items: Vec, + other_runs: Vec<(i32, usize)>, +) { + debug_assert_eq!(runs.iter().map(|r| r.1).sum::(), items.len()); + debug_assert_eq!( + other_runs.iter().map(|r| r.1).sum::(), + other_items.len() + ); + if other_items.is_empty() { + return; + } + let mut chunks: Vec<(i32, Vec)> = Vec::with_capacity(runs.len() + other_runs.len()); + let mut mine = std::mem::take(items).into_iter(); + for &(tid, n) in runs.iter() { + chunks.push((tid, mine.by_ref().take(n).collect())); + } + let mut theirs = other_items.into_iter(); + for &(tid, n) in &other_runs { + chunks.push((tid, theirs.by_ref().take(n).collect())); + } + chunks.sort_by_key(|c| c.0); + + runs.clear(); + for (tid, chunk) in chunks { + match runs.last_mut() { + Some((last_tid, count)) if *last_tid == tid => *count += chunk.len(), + _ => runs.push((tid, chunk.len())), + } + items.extend(chunk); + } +} + +/// Keep the first `len` items, adjusting the chromosome runs to match. +fn truncate_chrom_runs(items: &mut Vec, runs: &mut Vec<(i32, usize)>, len: usize) { + if items.len() <= len { + return; + } + items.truncate(len); + let mut left = len; + runs.retain_mut(|(_, count)| { + if left == 0 { + return false; + } + *count = (*count).min(left); + left -= *count; + true + }); +} + // =================================================================== // Merge helpers for Vec<[u64; N]> per-cycle arrays // =================================================================== @@ -2815,3 +2899,39 @@ mod tests { } } } + +#[cfg(test)] +mod chrom_run_tests { + use super::*; + + fn runs_of(items: &[(i32, u32)]) -> (Vec, Vec<(i32, usize)>) { + let mut runs = Vec::new(); + for &(tid, _) in items { + push_chrom_run(&mut runs, tid); + } + (items.iter().map(|x| x.1).collect(), runs) + } + + #[test] + fn test_merge_chrom_runs_restores_file_order() { + // Worker 1 read chromosomes 1 and 3, worker 2 read chromosomes 0 and 2. + let w1 = [(1, 10), (1, 11), (3, 30)]; + let w2 = [(0, 0), (2, 20), (2, 21)]; + let expected = vec![0, 10, 11, 20, 21, 30]; + for (first, second) in [(&w1, &w2), (&w2, &w1)] { + let (mut items, mut runs) = runs_of(first); + let (other, other_runs) = runs_of(second); + merge_chrom_runs(&mut items, &mut runs, other, other_runs); + assert_eq!(items, expected); + assert_eq!(runs, vec![(0, 1), (1, 2), (2, 2), (3, 1)]); + } + } + + #[test] + fn test_truncate_chrom_runs() { + let (mut items, mut runs) = runs_of(&[(0, 0), (1, 10), (1, 11), (2, 20)]); + truncate_chrom_runs(&mut items, &mut runs, 2); + assert_eq!(items, vec![0, 10]); + assert_eq!(runs, vec![(0, 1), (1, 1)]); + } +}