oarfish 0.10.0

A fast, accurate and versatile tool for long-read transcript quantification.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use clap::Parser;
use noodles_sam::Header;
use std::num::NonZeroUsize;
use util::oarfish_types::NamedDigestVec;

// Genome-projection mapping churns many small allocations across worker threads;
// glibc malloc fragments/retains that into large peak RSS. mimalloc keeps the
// peak down and returns memory to the OS. (Disable with `--no-default-features`.)
#[cfg(feature = "mimalloc")]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

use num_format::{Locale, ToFormattedString};
use std::{fs::File, io};
use tracing::info;
use tracing_subscriber::{EnvFilter, filter::LevelFilter, fmt, prelude::*};

use noodles_bam as bam;
use noodles_bgzf as bgzf;

mod alignment_parser;
mod bootstrap;
mod bulk;
mod em;
mod prog_opts;
mod single_cell;
mod util;

use crate::prog_opts::{Args, FilterGroup};
use crate::util::aligner::{get_aligner_from_args, get_aligner_from_fastas};
use crate::util::digest_utils;
use crate::util::normalize_probability::normalize_read_probs;
use crate::util::oarfish_types::{AlignmentFilters, TranscriptInfo};
use crate::util::projection;
use crate::util::{
    binomial_probability::binomial_continuous_prob, kde_utils, logistic_probability::logistic_prob,
};

fn get_txp_info_from_header(
    header: &Header,
    args: &Args,
) -> anyhow::Result<(Vec<TranscriptInfo>, Vec<String>)> {
    let num_ref_seqs = header.reference_sequences().len();
    let mut txps: Vec<TranscriptInfo> = Vec::with_capacity(num_ref_seqs);
    let mut txps_name: Vec<String> = Vec::with_capacity(num_ref_seqs);

    // loop over the transcripts in the header and fill in the relevant
    // information here.
    if args.model_coverage {
        for (rseq, rmap) in header.reference_sequences().iter() {
            txps.push(TranscriptInfo::with_len_and_bin_width(
                rmap.length(),
                args.bin_width,
            ));
            txps_name.push(rseq.to_string());
        }
    } else {
        for (rseq, rmap) in header.reference_sequences().iter() {
            txps.push(TranscriptInfo::with_len(rmap.length()));
            txps_name.push(rseq.to_string());
        }
    }
    info!(
        "parsed reference information for {} transcripts.",
        txps.len().to_formatted_string(&Locale::en)
    );
    Ok((txps, txps_name))
}

fn get_filter_opts(args: &Args) -> anyhow::Result<AlignmentFilters> {
    // `--score-prob-denom` tunes the transcriptome-mode score→probability
    // conversion. In genome (projection) mode a read's projected alignments are
    // weighted by bramble's exonic-coverage similarity (see `--projected-prob-source`),
    // not that conversion, so the flag has no effect there — reject it explicitly
    // rather than silently ignoring it.
    if args.score_prob_denom.is_some()
        && (args.genome.is_some() || args.genome_alignments.is_some())
    {
        anyhow::bail!(
            "--score-prob-denom does not apply in genome (projection) mode: a read's \
             projected alignments are weighted by bramble's exonic-coverage similarity \
             (see --projected-prob-source), not the score→probability conversion that \
             --score-prob-denom controls. Please omit --score-prob-denom in genome mode."
        );
    }

    // set all of the filter options that the user
    // wants to apply.
    match args.filter_group {
        Some(FilterGroup::NoFilters) => {
            info!("disabling alignment filters.");
            // override individual parameters if the user passed them in explicitly
            let fpc = args
                .five_prime_clip
                .provided_or_u32("overriding 5' clip with user-provided value", u32::MAX);
            let tpc = args
                .three_prime_clip
                .provided_or_i64("overriding 3' clip with user-provided value", i64::MAX);
            let st = args
                .score_threshold
                .provided_or_f32("overriding score threshold with user-provided value", 0_f32);
            let maf = args.min_aligned_fraction.provided_or_f32(
                "overriding min aligned fraction with user-provided value",
                0_f32,
            );
            let mal = args.min_aligned_len.provided_or_u32(
                "overriding min aligned length with user-provided value",
                1_u32,
            );

            Ok(AlignmentFilters::builder()
                .five_prime_clip(fpc)
                .three_prime_clip(tpc)
                .score_threshold(st)
                .min_aligned_fraction(maf)
                .min_aligned_len(mal)
                .which_strand(args.strand_filter)
                .model_coverage(args.model_coverage)
                .logistic_growth_rate(args.growth_rate)
                .write_assignment_probs(args.write_assignment_probs.is_some())
                .write_assignment_probs_type(args.write_assignment_probs.clone())
                .score_prob_denom(args.score_prob_denom.unwrap_or(5.0))
                .build())
        }
        Some(FilterGroup::NanocountFilters) => {
            info!("setting filters to nanocount defaults.");
            // override individual parameters if the user passed them in explicitly
            let fpc = args
                .five_prime_clip
                .provided_or_u32("overriding 5' clip with user-provided value", u32::MAX);
            let tpc = args
                .three_prime_clip
                .provided_or_i64("overriding 3' clip with user-provided value", 50_i64);
            let st = args.score_threshold.provided_or_f32(
                "overriding score threshold with user-provided value",
                0.95_f32,
            );
            let maf = args.min_aligned_fraction.provided_or_f32(
                "overriding min aligned fraction with user-provided value",
                0.5_f32,
            );
            let mal = args.min_aligned_len.provided_or_u32(
                "overriding min aligned length with user-provided value",
                50_u32,
            );

            Ok(AlignmentFilters::builder()
                .five_prime_clip(fpc)
                .three_prime_clip(tpc)
                .score_threshold(st)
                .min_aligned_fraction(maf)
                .min_aligned_len(mal)
                .which_strand(bio_types::strand::Strand::Forward)
                .model_coverage(args.model_coverage)
                .logistic_growth_rate(args.growth_rate)
                .write_assignment_probs(args.write_assignment_probs.is_some())
                .write_assignment_probs_type(args.write_assignment_probs.clone())
                .score_prob_denom(args.score_prob_denom.unwrap_or(5.0))
                .build())
        }
        None => {
            info!("setting user-provided filter parameters.");
            Ok(AlignmentFilters::builder()
                .five_prime_clip(args.five_prime_clip.try_as_u32()?)
                .three_prime_clip(args.three_prime_clip.try_as_i64()?)
                .score_threshold(args.score_threshold.try_as_f32()?)
                .min_aligned_fraction(args.min_aligned_fraction.try_as_f32()?)
                .min_aligned_len(args.min_aligned_len.try_as_u32()?)
                .which_strand(args.strand_filter)
                .model_coverage(args.model_coverage)
                .logistic_growth_rate(args.growth_rate)
                .write_assignment_probs(args.write_assignment_probs.is_some())
                .write_assignment_probs_type(args.write_assignment_probs.clone())
                .score_prob_denom(args.score_prob_denom.unwrap_or(5.0))
                .build())
        }
    }
}

/// Genome-BAM mode: project a spliced, genome-aligned (name-collated) BAM onto
/// the transcripts in `--annotation` and quantify.
fn run_genome_alignments(args: &Args, filter_opts: AlignmentFilters) -> anyhow::Result<()> {
    let bam_path = args
        .genome_alignments
        .clone()
        .expect("genome_alignments present");
    let annotation = args
        .annotation
        .clone()
        .expect("--annotation is required with --genome-alignments");

    info!("oarfish is operating in genome-alignment (projection) mode");

    // open the genome BAM with a multithreaded bgzf decoder
    let afile = File::open(&bam_path)?;
    let decomp_threads = 1.max(args.threads.saturating_sub(1));
    let worker_count = NonZeroUsize::new(decomp_threads).expect("decompression threads >= 1");
    let decoder = bgzf::io::MultithreadedReader::with_worker_count(worker_count, afile);
    let mut reader = bam::io::Reader::from(decoder);

    // the genome header gives us the chromosome -> RefId mapping; also enforce
    // that the BAM is name-collated (required for per-read-name projection).
    let genome_header = alignment_parser::read_and_verify_genome_header(&mut reader, &bam_path)?;
    let refnames: Vec<String> = genome_header
        .reference_sequences()
        .iter()
        .map(|(rseq, _)| rseq.to_string())
        .collect();

    // build the genome->transcriptome index and the transcriptome header/info.
    // genome-alignments (BAM) mode has no aligner index, so the rescue reference
    // comes from a FASTA (`--genome-fasta`) only.
    let rescue_db = projection::load_rescue_fasta(args)?;
    let use_fasta = rescue_db.is_some();
    let g2t = projection::load_g2t(&annotation, &refnames, rescue_db)?;
    let (txp_header, mut txps, txps_name) =
        projection::build_transcriptome_header_and_info(&g2t, args)?;
    let proj_config = projection::projection_config(args, use_fasta);

    let seqcol_digest = digest_utils::digest_from_header(&txp_header)?;
    let digest: NamedDigestVec = vec![("transcriptome_digest".to_string(), seqcol_digest)].into();

    bulk::quantify_genome_alignments_from_bam(
        &genome_header,
        &txp_header,
        &g2t,
        &proj_config,
        filter_opts,
        &mut reader,
        &mut txps,
        &txps_name,
        args,
        digest,
    )
}

/// Genome-read mode: spliced-align raw reads to the genome with the rammap
/// aligner, then project onto the transcripts in `--annotation` and quantify.
fn run_genome_reads(args: &mut Args, filter_opts: AlignmentFilters) -> anyhow::Result<()> {
    let annotation = args
        .annotation
        .clone()
        .expect("--annotation is required with --genome");
    let read_paths = args
        .reads
        .clone()
        .expect("--reads is required in genome read mode");

    // parse the annotation once; reused for both junction extraction and the
    // genome->transcriptome index.
    info!("loading annotation from {}", annotation.display());
    let transcripts = bramble_rs::annotation::load_transcripts(&annotation)?;
    info!("loaded {} transcripts from annotation", transcripts.len());

    // Determine the splice-junction BED to bias spliced alignment toward
    // annotated junctions (rammap `load_junctions_bed`).
    // Prefer a user-supplied BED; otherwise derive a BED12 of transcript models
    // from the annotation (default on). Computed before the aligner is built so
    // it can be loaded into the index by the aligner builder.
    let junc_bed: Option<std::path::PathBuf> = if let Some(b) = args.junctions.clone() {
        Some(b)
    } else if !args.ignore_annotation_junctions {
        let mut bed = args.output.clone().expect("output prefix required");
        let fname = format!(
            "{}.annot_junctions.bed",
            bed.file_name().map(|f| f.to_string_lossy().into_owned()).unwrap_or_default()
        );
        bed.set_file_name(fname);
        let n = projection::write_annotation_junction_bed(&transcripts, &bed)?;
        info!(
            "derived {} spliced transcript models from the annotation for the splice-junction BED",
            n
        );
        Some(bed)
    } else {
        info!("not using annotated splice junctions (--ignore-annotation-junctions)");
        None
    };

    // build the spliced genome aligner (loading the junction BED into the index)
    // and obtain its reference (chromosome) names in target-id order (== the g2t
    // RefId order).
    let (aligner, refnames) =
        crate::util::aligner::get_genome_aligner_from_args(args, junc_bed.as_deref())?;

    // build the genome->transcriptome index and the transcriptome header/info.
    // Rescue is on by default: source its reference from a FASTA (`--genome-fasta`,
    // or a FASTA `--genome`), else from the reference sequences resident in the
    // loaded genome index (`--genome` is a prebuilt index). `--no-rescue` / no
    // available source => off.
    let rescue_db = match projection::load_rescue_fasta(args)? {
        Some(db) => Some(db),
        None if !args.no_rescue => crate::util::aligner::rescue_db_from_genome_index(&aligner),
        None => None,
    };
    let use_fasta = rescue_db.is_some();
    let g2t = projection::build_g2t_from_transcripts(&transcripts, &refnames, rescue_db)?;
    let (txp_header, mut txps, txps_name) =
        projection::build_transcriptome_header_and_info(&g2t, args)?;
    let proj_config = projection::projection_config(args, use_fasta);

    let seqcol_digest = digest_utils::digest_from_header(&txp_header)?;
    let digest: NamedDigestVec = vec![("transcriptome_digest".to_string(), seqcol_digest)].into();

    bulk::quantify_genome_raw_reads(
        &txp_header,
        aligner,
        &g2t,
        &proj_config,
        filter_opts,
        &read_paths,
        &mut txps,
        &txps_name,
        args,
        digest,
    )
}

fn main() -> anyhow::Result<()> {
    let env_filter = EnvFilter::builder()
        .with_default_directive(LevelFilter::INFO.into())
        .from_env_lossy();
    let (filtered_layer, reload_handle) = tracing_subscriber::reload::Layer::new(env_filter);

    // set up the logging.  Here we will take the
    // logging level from the environment variable if
    // it is set.  Otherwise, we'll set the default
    tracing_subscriber::registry()
        // log level to INFO.
        .with(fmt::layer().with_writer(io::stderr))
        .with(filtered_layer)
        .init();

    let mut args = Args::parse();

    // change the logging filter if the user specified quiet or
    // verbose.
    if args.quiet {
        reload_handle.modify(|filter| *filter = EnvFilter::new("WARN"))?;
    }
    if args.verbose {
        reload_handle.modify(|filter| *filter = EnvFilter::new("TRACE"))?;
    }

    // Apply an explicit DP scratch-cache cap if the user set one; otherwise the
    // rammap mapper uses its default (128 MB) or the RAMMAP_DP_CACHE_CAP_MB env var.
    if let Some(mb) = args.dp_cache_cap_mb {
        rammap::set_dp_cache_cap_mb(mb);
    }

    // if we are just indexing, don't bother with anything else
    if args.only_index {
        let (header, _reader, _aligner, digest) = get_aligner_from_fastas(&mut args)?;
        info!(
            "indexing completed; index over {} references written to {}",
            header.reference_sequences().len(),
            &args.index_out.expect("nonempty").display()
        );
        info!(
            "reference digest = {}",
            serde_json::to_string_pretty(&digest.to_json())?
        );
        return Ok(());
    }

    // if we are quantifying, handle this below
    let filter_opts = get_filter_opts(&args)?;

    // Genome-space modes: rather than quantifying against the input BAM/aligner
    // references directly, we project genomic alignments onto the transcripts in
    // `--annotation` (via bramble) and quantify those. These paths build the
    // transcriptome header / `TranscriptInfo` from the annotation, not from the
    // input header, so they are handled separately and return early.
    if args.genome_alignments.is_some() {
        run_genome_alignments(&args, filter_opts)?;
        info!("oarfish completed successfully.");
        return Ok(());
    } else if args.genome.is_some() {
        run_genome_reads(&mut args, filter_opts)?;
        info!("oarfish completed successfully.");
        return Ok(());
    }

    let (header, reader, aligner, digest) = if args.alignments.is_none() {
        get_aligner_from_args(&mut args)?
    } else {
        let alignments = args.alignments.clone().unwrap();
        let afile = File::open(&alignments)?;

        let decomp_threads = if args.single_cell {
            // we will overlap quantification with parsing, so don't try to use too many
            // parser threads, and adjust the worker threads accordingly.

            // is there a better heuristic than this?
            // <= 6 threads, use only 1 for decompression
            // 6-8 threads, use 2 for decompression
            // > 8 threads, use 3 for decompression
            match args.threads {
                1..=6 => 1,
                7 | 8 => 2,
                _ => 3,
            }
        } else {
            // try to use all but 1 thread, and assume we have at least 2.
            1.max(args.threads.saturating_sub(1))
        };

        let worker_count = NonZeroUsize::new(decomp_threads).expect("decompression threads >= 1");
        if args.single_cell {
            args.threads = 1.max(args.threads.saturating_sub(decomp_threads));
        }

        let decoder = bgzf::io::MultithreadedReader::with_worker_count(worker_count, afile);
        let mut reader = bam::io::Reader::from(decoder);
        // parse the header, and ensure that the reads were mapped with a known long-read
        // aligner (as far as we can tell).
        let header = alignment_parser::read_and_verify_header(&mut reader, &alignments)?;
        let seqcol_digest = digest_utils::digest_from_header(&header)?;
        (
            header,
            Some(reader),
            None,
            vec![("bam_digest".to_string(), seqcol_digest)].into(),
        )
    };

    // where we'll write down the per-transcript information we need
    // to track.
    let (mut txps, txps_name) = get_txp_info_from_header(&header, &args)?;

    if args.single_cell {
        // TODO: do this better (quiet the EM during single-cell quant)
        reload_handle.modify(|filter| {
            *filter = if args.quiet {
                EnvFilter::new("WARN")
                    .add_directive("oarfish=warn".parse().unwrap())
                    .add_directive("oarfish::single_cell=warn".parse().unwrap())
            } else if args.verbose {
                EnvFilter::new("TRACE")
                    .add_directive("oarfish=info".parse().unwrap())
                    .add_directive("oarfish::single_cell=trace".parse().unwrap())
            } else {
                // be quiet about normal things in single-cell mode
                // e.g. EM iterations, and only print out info for
                // oarfish::single_cell events.
                EnvFilter::new("INFO")
                    .add_directive("oarfish=warn".parse().unwrap())
                    .add_directive("oarfish::single_cell=info".parse().unwrap())
            }
        })?;

        single_cell::quantify_single_cell_from_collated_bam(
            &header,
            &filter_opts,
            &mut reader.unwrap(),
            &mut txps,
            &args,
            digest,
        )?;
    } else if args.alignments.is_some() {
        bulk::quantify_bulk_alignments_from_bam(
            &header,
            filter_opts,
            &mut reader.unwrap(),
            &mut txps,
            &txps_name,
            &args,
            digest,
        )?;
    } else {
        bulk::quantify_bulk_alignments_raw_reads(
            &header,
            aligner.expect("need valid alinger to align reads"),
            filter_opts,
            &args.reads.clone().expect("expected read file(s)"),
            &mut txps,
            &txps_name,
            &args,
            digest,
        )?;
    }

    info!("oarfish completed successfully.");
    Ok(())
}