diff --git a/docs/src/content/docs/rna/rseqc.mdx b/docs/src/content/docs/rna/rseqc.mdx index ffd4ec4..cceadfa 100644 --- a/docs/src/content/docs/rna/rseqc.mdx +++ b/docs/src/content/docs/rna/rseqc.mdx @@ -429,8 +429,21 @@ At 100% sampling depth: -Intermediate sampling percentages show minor stochastic variation from random -subsampling, as expected. +RustQC follows RSeQC's sampling: the percentages are +`range(floor, ceiling, step)` followed by 100, and the window for each percentage +uses RSeQC's floating-point index arithmetic, `int(N * (p / 100.0))`. The x-axis +values are therefore identical to RSeQC's, and so is the 100% point whenever the +windows cover all observations (as with the default parameters). The +intermediate points depend on how the splice-junction observations are shuffled. +RSeQC does not seed Python's `random`, so its intermediate points change from run +to run. RustQC shuffles with its own seeded RNG (`--junction-saturation-seed`, +default 42), so its intermediate points are reproducible and identical for any +`--threads` value, but they do not match RSeQC's random draws. + +As in RSeQC, reads on chromosomes without any annotated transcript are ignored. +The `pdf()` call in the R script names the PDF by sample name only, as RSeQC does +when run with `-o `, so the script does not depend on the output +directory. ## inner_distance diff --git a/docs/src/content/docs/usage/cli-reference.mdx b/docs/src/content/docs/usage/cli-reference.mdx index d54b15e..a3b5f5c 100644 --- a/docs/src/content/docs/usage/cli-reference.mdx +++ b/docs/src/content/docs/usage/cli-reference.mdx @@ -229,12 +229,17 @@ tool runs by default as part of `rustqc rna` and can be disabled via the | Option | Default | Description | | ---------------------------------------------- | ------- | ------------------------------------------------- | -| `--junction-saturation-seed ` | `42` | Random seed for reproducible sampling | +| `--junction-saturation-seed ` | `42` | Seed for the sampling shuffle (see below) | | `--junction-saturation-min-coverage ` | `1` | Minimum reads for a junction to count as detected | | `--junction-saturation-percentile-floor ` | `5` | Sampling start percentage | | `--junction-saturation-percentile-ceiling ` | `100` | Sampling end percentage | | `--junction-saturation-percentile-step ` | `5` | Sampling step percentage | +The intermediate sampling points come from RustQC's own seeded RNG: they are +reproducible for a given seed, but do not match RSeQC's random draws. The x-axis +values and the 100% point match RSeQC. See +[junction_saturation](/rna/rseqc/#junction_saturation). + #### inner_distance | Option | Default | Description | diff --git a/docs/src/content/docs/usage/configuration.md b/docs/src/content/docs/usage/configuration.md index 0b8f16f..9ca6638 100644 --- a/docs/src/content/docs/usage/configuration.md +++ b/docs/src/content/docs/usage/configuration.md @@ -488,7 +488,7 @@ Requires annotation (`--gtf`). The `min_intron` can also be set via `--min-intro rna: junction_saturation: enabled: true - seed: 42 # Random seed for reproducible sampling (default: 42) + seed: 42 # Seed for the sampling shuffle; reproducible, but not RSeQC's draws (default: 42) min_coverage: 1 # Minimum read count to consider a junction (default: 1) percentile_floor: 5 # Sampling start percentage (default: 5) percentile_ceiling: 100 # Sampling end percentage (default: 100) diff --git a/src/rna/rseqc/accumulators.rs b/src/rna/rseqc/accumulators.rs index ced01fb..ebcfbd3 100644 --- a/src/rna/rseqc/accumulators.rs +++ b/src/rna/rseqc/accumulators.rs @@ -1775,10 +1775,20 @@ fn classify_junction( /// Uses hashed `u64` keys instead of heap-allocated Strings to reduce memory /// from ~25 bytes/observation to 8 bytes/observation. The hash function is /// the same `SipHash` used for read_duplication sequence dedup. +/// +/// The observation list is shuffled before sampling, so its order must not +/// depend on the thread count. It is kept in BAM record order (then intron +/// order within each read), which is also RSeQC's order. Observations are +/// therefore kept in per-chromosome segments, in the order the records were +/// read. A worker reads each chromosome in one contiguous `fetch`, and +/// [`merge`](Self::merge) sorts the segments by reference ID, which is file +/// order for a coordinate-sorted (indexed) BAM, whatever the thread count. +/// The sequential path never merges, so it keeps file order for any BAM. #[derive(Debug, Default)] pub struct JuncSatAccum { - /// All junction observation keys (hashed `"CHROM:start-end"`). - pub observations: Vec, + /// Junction observation keys (hashed `"CHROM:start-end"`), grouped in + /// runs of consecutive records with the same reference ID. + segments: Vec<(i32, Vec)>, /// String keys corresponding to each hash, for the known_junctions lookup /// during into_result(). Stored as `(hash, string)` only for unique junctions. unique_keys: HashMap, @@ -1825,18 +1835,30 @@ impl JuncSatAccum { if iend - istart < min_intron { continue; } - let h = hash_junction_key(chrom_upper, istart, iend); - self.observations.push(h); - // Track the string key only for the first occurrence of each hash - self.unique_keys - .entry(h) - .or_insert_with(|| format!("{}:{}-{}", chrom_upper, istart, iend)); + self.push_observation(record.tid(), chrom_upper, istart, iend); } } + /// Append one junction observation from a record on reference `tid`. + fn push_observation(&mut self, tid: i32, chrom_upper: &str, istart: u64, iend: u64) { + let h = hash_junction_key(chrom_upper, istart, iend); + match self.segments.last_mut() { + Some((seg_tid, obs)) if *seg_tid == tid => obs.push(h), + _ => self.segments.push((tid, vec![h])), + } + // Track the string key only for the first occurrence of each hash + self.unique_keys + .entry(h) + .or_insert_with(|| format!("{}:{}-{}", chrom_upper, istart, iend)); + } + /// Merge another accumulator into this one. + /// + /// Segments are stably sorted by reference ID, so the merged order is the + /// BAM file order no matter how chromosomes were split between workers. pub fn merge(&mut self, other: JuncSatAccum) { - self.observations.extend(other.observations); + self.segments.extend(other.segments); + self.segments.sort_by_key(|(tid, _)| *tid); // Merge unique key maps (first writer wins — all map to the same string) for (h, key) in other.unique_keys { self.unique_keys.entry(h).or_insert(key); @@ -2667,15 +2689,25 @@ impl JuncAnnotAccum { impl JuncSatAccum { /// Post-process accumulated observations into a `SaturationResult`. /// - /// Performs the shuffle (Phase 2) and incremental subsampling (Phase 3) - /// that were previously done in the standalone `junction_saturation()` function. + /// Follows RSeQC's `ParseBAM.saturation_junction()`, except that the shuffle + /// uses RustQC's own seeded RNG, so the intermediate points are + /// reproducible but do not match RSeQC's (unseeded) random draws. The + /// x-axis values and the 100% point are deterministic and match RSeQC: /// - /// Uses incremental known/novel counting: instead of iterating all unique - /// junctions at each percentage step (O(U*P)), maintains running counters - /// updated only when new unique junctions first appear or cross the - /// min_coverage threshold. + /// 1. Drop observations on chromosomes without an annotated transcript + /// (RSeQC skips reads whose chromosome is not in the BED gene model). + /// 2. Shuffle the observations (in BAM file order) with a seeded ChaCha8 RNG. + /// 3. For each sampling percentage `p` in + /// `range(sample_start, sample_end, sample_step) + [100]`, add the + /// observations from `int(N * ((p - sample_step) / 100.0))` (clamped + /// to 0) to `int(N * (p / 100.0))` to the running junction counts, + /// using the same floating-point index arithmetic. The windows can + /// overlap or leave gaps for some parameter combinations, as in RSeQC. + /// + /// Known/novel/all junction counts are maintained incrementally instead + /// of re-scanning all unique junctions at every sampling step. pub fn into_result( - mut self, + self, known_junctions: &KnownJunctionSet, sample_start: u32, sample_end: u32, @@ -2683,65 +2715,67 @@ impl JuncSatAccum { min_coverage: u32, seed: u64, ) -> SaturationResult { - use rand::seq::SliceRandom; - use rand::SeedableRng; - use rand_chacha::ChaCha8Rng; + // Hashes of known junctions, and of junctions on annotated chromosomes. + let mut known_hashes: HashSet = HashSet::new(); + let mut kept_hashes: HashSet = HashSet::new(); + for (&h, key) in &self.unique_keys { + if known_junctions.junctions.contains(key) { + known_hashes.insert(h); + } + let chrom = key.rsplit_once(':').map_or(key.as_str(), |(c, _)| c); + if known_junctions.chroms.contains(chrom) { + kept_hashes.insert(h); + } + } - // Pre-build a HashSet from known_junctions for O(1) hash-based lookup - let known_hashes: HashSet = self - .unique_keys - .iter() - .filter(|(_, key)| known_junctions.junctions.contains(*key)) - .map(|(&h, _)| h) + // Phase 1: the observation list in RSeQC's order (BAM file order). + let mut observations: Vec = self + .segments + .into_iter() + .flat_map(|(_, obs)| obs) + .filter(|h| kept_hashes.contains(h)) .collect(); // Phase 2: deterministic shuffle - let mut rng = ChaCha8Rng::seed_from_u64(seed); - self.observations.shuffle(&mut rng); - - // Build percentage series - let mut percentages: Vec = (sample_start..=sample_end) - .step_by(sample_step as usize) - .collect(); - if *percentages.last().unwrap_or(&0) != 100 { - percentages.push(100); - } + { + use rand::seq::SliceRandom; + use rand::SeedableRng; + observations.shuffle(&mut rand_chacha::ChaCha8Rng::seed_from_u64(seed)); + } + + // Phase 3: sampling, with RSeQC's percentage series and index arithmetic. + let percentages = sampling_percentages(sample_start, sample_end, sample_step); + let total = observations.len(); + let index_at = |pct: i64| -> usize { + // Python: int(SR_num * (pct / 100.0)), truncating toward zero. + let idx = (total as f64 * (pct as f64 / 100.0)) as i64; + idx.clamp(0, total as i64) as usize + }; + // A junction counts as known once seen `max(min_coverage, 1)` times + // (RSeQC: `uniqSpliceSites[sj] >= recur` over junctions seen at least once). + let known_threshold = min_coverage.max(1); - // Phase 3: incremental sampling with running counters - let total = self.observations.len(); let mut junction_counts: HashMap = HashMap::new(); - let mut prev_end = 0; let mut known_counts = Vec::with_capacity(percentages.len()); let mut novel_counts = Vec::with_capacity(percentages.len()); let mut all_counts = Vec::with_capacity(percentages.len()); - - // Running counters — updated incrementally as new observations arrive let mut running_known: usize = 0; let mut running_novel: usize = 0; for &pct in &percentages { - let index_end = total * pct as usize / 100; - for &obs_hash in &self.observations[prev_end..index_end] { + let index_st = index_at(i64::from(pct) - i64::from(sample_step)); + let index_end = index_at(i64::from(pct)); + for &obs_hash in observations.get(index_st..index_end).unwrap_or(&[]) { let count = junction_counts.entry(obs_hash).or_insert(0); *count += 1; - - let is_known = known_hashes.contains(&obs_hash); - if *count == 1 { - // First time seeing this junction - if is_known { - if min_coverage <= 1 { - running_known += 1; - } - // else: known but below threshold, don't count yet - } else { - running_novel += 1; + if known_hashes.contains(&obs_hash) { + if *count == known_threshold { + running_known += 1; } - } else if is_known && *count == min_coverage && min_coverage > 1 { - // Junction just crossed the min_coverage threshold - running_known += 1; + } else if *count == 1 { + running_novel += 1; } } - prev_end = index_end; known_counts.push(running_known); novel_counts.push(running_novel); @@ -2757,6 +2791,21 @@ impl JuncSatAccum { } } +/// RSeQC's sampling percentages: `list(range(start, end, step))` followed by 100. +/// +/// A zero step would make Python's `range()` raise; here it yields only `[100]`. +fn sampling_percentages(sample_start: u32, sample_end: u32, sample_step: u32) -> Vec { + let mut percentages: Vec = if sample_step == 0 { + Vec::new() + } else { + (sample_start..sample_end) + .step_by(sample_step as usize) + .collect() + }; + percentages.push(100); + percentages +} + impl InnerDistAccum { /// Convert accumulated pair data into an `InnerDistanceResult`. pub fn into_result( @@ -2782,3 +2831,106 @@ impl InnerDistAccum { }) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_sampling_percentages_match_python_range() { + // list(range(5, 100, 5)) + [100] + let default: Vec = (1..=20).map(|i| i * 5).collect(); + assert_eq!(sampling_percentages(5, 100, 5), default); + // range() excludes the end, then RSeQC appends 100. + assert_eq!(sampling_percentages(10, 50, 10), vec![10, 20, 30, 40, 100]); + assert_eq!(sampling_percentages(1, 100, 5).last(), Some(&100)); + assert_eq!(sampling_percentages(1, 100, 5)[19], 96); + assert_eq!(sampling_percentages(50, 50, 5), vec![100]); + assert_eq!(sampling_percentages(5, 100, 0), vec![100]); + } + + /// Observations for the saturation tests: 40 on CHR1 (tid 0) interleaved + /// with 4 on CHRX (tid 1), which is not annotated. + fn test_observations() -> Vec<(i32, &'static str, u64)> { + let mut out = Vec::new(); + for i in 0..40u64 { + if i % 11 == 3 { + out.push((1, "CHRX", 1)); + } + out.push((0, "CHR1", (i * 7) % 9 * 100)); + } + out + } + + fn test_known() -> KnownJunctionSet { + KnownJunctionSet { + junctions: ["CHR1:0-50", "CHR1:100-150", "CHR1:200-250", "CHR1:700-750"] + .iter() + .map(|s| s.to_string()) + .collect(), + chroms: ["CHR1".to_string()].into_iter().collect(), + } + } + + fn push(accum: &mut JuncSatAccum, tid: i32, chrom: &str, start: u64) { + let end = if chrom == "CHRX" { 100 } else { start + 50 }; + accum.push_observation(tid, chrom, start, end); + } + + fn filled(obs: &[(i32, &str, u64)]) -> JuncSatAccum { + let mut accum = JuncSatAccum::default(); + for &(tid, chrom, st) in obs { + push(&mut accum, tid, chrom, st); + } + accum + } + + #[test] + fn test_saturation_final_point_and_axis() { + // The x axis and (when the windows cover the whole list) the 100% + // point do not depend on the shuffle. + let known = test_known(); + for seed in [1, 7, 42] { + for (start, end, step) in [(5, 100, 5), (1, 100, 5), (10, 100, 10)] { + let r = filled(&test_observations()).into_result(&known, start, end, step, 1, seed); + assert_eq!(r.percentages, sampling_percentages(start, end, step)); + // All 40 CHR1 observations are used (CHRX is dropped): 9 + // distinct junctions, 4 of them known. + assert_eq!(*r.all_counts.last().unwrap(), 9); + assert_eq!(*r.known_counts.last().unwrap(), 4); + assert_eq!(*r.novel_counts.last().unwrap(), 5); + } + } + } + + #[test] + fn test_saturation_deterministic_and_seeded() { + let known = test_known(); + let run = |seed| filled(&test_observations()).into_result(&known, 5, 100, 5, 1, seed); + assert_eq!(run(3).all_counts, run(3).all_counts); + } + + #[test] + fn test_saturation_merge_order_independent() { + // A coordinate-sorted layout: tid 0 records, then tid 1, then tid 2. + let obs: Vec<(i32, &str, u64)> = (0..30u64) + .map(|i| ((i / 10) as i32, "CHR1", (i * 13) % 7 * 100)) + .collect(); + let known = test_known(); + let run = |order: &[i32]| { + let mut merged = JuncSatAccum::default(); + for &tid in order { + let part: Vec<_> = obs.iter().copied().filter(|o| o.0 == tid).collect(); + merged.merge(filled(&part)); + } + merged.into_result(&known, 5, 100, 5, 1, 3) + }; + let expected = filled(&obs).into_result(&known, 5, 100, 5, 1, 3); + for order in [[0, 1, 2], [2, 0, 1], [1, 2, 0]] { + let r = run(&order); + assert_eq!(r.all_counts, expected.all_counts); + assert_eq!(r.known_counts, expected.known_counts); + assert_eq!(r.novel_counts, expected.novel_counts); + } + } +} diff --git a/src/rna/rseqc/common.rs b/src/rna/rseqc/common.rs index bbfe0cc..b5d8902 100644 --- a/src/rna/rseqc/common.rs +++ b/src/rna/rseqc/common.rs @@ -101,6 +101,11 @@ pub fn classify_junction( pub struct KnownJunctionSet { /// Junction keys in `"CHROM:start-end"` format (uppercased chromosome). pub junctions: HashSet, + /// Uppercased names of chromosomes with at least one annotated transcript. + /// + /// RSeQC's junction_saturation ignores reads on chromosomes that are not in + /// its BED gene model, which has one line per transcript. + pub chroms: HashSet, } // =================================================================== @@ -166,11 +171,12 @@ pub fn build_known_junctions_from_genes(genes: &IndexMap) -> Known for gene in genes.values() { for tx in &gene.transcripts { - if tx.exons.len() <= 1 { + if tx.exons.is_empty() { continue; } let chrom = tx.chrom.to_uppercase(); + result.chroms.insert(chrom.clone()); // Same coordinate conversion as build_reference_junctions_from_genes for i in 0..tx.exons.len() - 1 { @@ -373,6 +379,8 @@ mod tests { let known = build_known_junctions_from_genes(&genes); assert!(known.junctions.is_empty()); + // The chromosome still counts as annotated (RSeQC's BED has the transcript). + assert!(known.chroms.contains("CHR1")); } #[test] diff --git a/src/rna/rseqc/junction_saturation.rs b/src/rna/rseqc/junction_saturation.rs index 47d33cb..5a8b0a6 100644 --- a/src/rna/rseqc/junction_saturation.rs +++ b/src/rna/rseqc/junction_saturation.rs @@ -28,12 +28,21 @@ pub struct SaturationResult { /// Write the junction saturation R script. /// /// Produces an R script matching RSeQC's `junctionSaturation_plot.r` format. +/// +/// The `pdf()` call names the PDF by the file name of `prefix` only (no +/// directory), as RSeQC writes it when run with `-o ` in the output +/// directory. The script is then byte-identical to RSeQC's and does not +/// depend on `--outdir`; running it from its own directory puts the PDF next +/// to it. pub fn write_r_script(result: &SaturationResult, prefix: &str) -> Result<()> { let r_path = format!("{prefix}.junctionSaturation_plot.r"); let mut f = std::fs::File::create(&r_path).with_context(|| format!("creating R script: {r_path}"))?; - let pdf_path = format!("{prefix}.junctionSaturation_plot.pdf"); + let stem = std::path::Path::new(prefix) + .file_name() + .map_or_else(|| prefix.into(), |s| s.to_string_lossy()); + let pdf_path = format!("{stem}.junctionSaturation_plot.pdf"); // x values let x_str: Vec = result.percentages.iter().map(|p| p.to_string()).collect(); @@ -104,3 +113,36 @@ pub fn write_summary(result: &SaturationResult, path: &str) -> Result<()> { // Note: CIGAR intron extraction tests are in the common module // (rseqc::common::tests). Tests here focus on saturation-specific logic. + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_r_script_matches_rseqc_format() { + let dir = std::env::temp_dir().join(format!("rustqc_junc_sat_r_{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let prefix = dir.join("sample").to_string_lossy().to_string(); + let result = SaturationResult { + percentages: vec![50, 100], + known_counts: vec![1500, 2944], + novel_counts: vec![200, 317], + all_counts: vec![1700, 3261], + }; + write_r_script(&result, &prefix).unwrap(); + let script = + std::fs::read_to_string(format!("{prefix}.junctionSaturation_plot.r")).unwrap(); + std::fs::remove_dir_all(&dir).unwrap(); + let lines: Vec<&str> = script.lines().collect(); + // The PDF is named without the output directory, as in RSeQC run with -o sample. + assert_eq!(lines[0], "pdf('sample.junctionSaturation_plot.pdf')"); + assert_eq!(lines[1], "x=c(50,100)"); + assert_eq!(lines[2], "y=c(1500,2944)"); + assert_eq!(lines[3], "z=c(1700,3261)"); + assert_eq!(lines[4], "w=c(200,317)"); + assert_eq!(lines[5], "m=max(2,3,0)"); + assert_eq!(lines[6], "n=min(1,1,0)"); + assert_eq!(lines.len(), 12); + assert!(script.ends_with("dev.off()\n")); + } +}