rsomics-count 0.1.0

Genomic feature counting and count-matrix normalization
Documentation
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
use std::ffi::OsString;
use std::num::NonZeroU64;
use std::path::{Path, PathBuf};

use clap::{Args, Parser, Subcommand, ValueEnum};
use rsomics_common::{OutputArgs, Result, RsomicsError, ThreadArgs, ToolMeta};
use serde::Serialize;

use rsomics_count::{
    AnnotationFormat, AnnotationOptions, Contribution, CountLevel, CountOptions, CountReport,
    NormalizeMode, NormalizeOptions, NormalizeReport, PairOptions, SplitMode, StrandMode, count,
    normalize,
};

pub const META: ToolMeta = ToolMeta {
    name: env!("CARGO_PKG_NAME"),
    version: env!("CARGO_PKG_VERSION"),
};

#[derive(Debug, Parser)]
#[command(
    name = "rsomics-count",
    version,
    about = "Assign alignments to genomic features and normalize count matrices",
    arg_required_else_help = true,
    subcommand_negates_reqs = true
)]
pub struct Cli {
    #[command(subcommand)]
    command: Option<Command>,

    #[command(flatten)]
    count: CountArgs,

    #[command(flatten)]
    threads: ThreadArgs,

    #[command(flatten)]
    pub output: OutputArgs,
}

#[derive(Debug, Subcommand)]
enum Command {
    /// Normalize a strict gene-by-sample count matrix.
    Normalize(NormalizeArgs),
}

#[derive(Debug, Args)]
struct CountArgs {
    /// SAM or BAM libraries; use - for a single standard-input library.
    #[arg(required = true, num_args = 1.., value_name = "ALIGNMENT")]
    inputs: Vec<PathBuf>,

    /// GTF, GFF3, or SAF annotation; gzip is detected by content.
    #[arg(short = 'a', long, required = true, value_name = "ANNOTATION")]
    annotation: Option<PathBuf>,

    /// Transactional featureCounts-compatible count matrix.
    #[arg(short, long, required = true, value_name = "COUNTS")]
    output: Option<PathBuf>,

    /// Transactional assignment summary; defaults to COUNTS.summary.
    #[arg(long, value_name = "SUMMARY")]
    summary: Option<PathBuf>,

    /// Annotation grammar.
    #[arg(short = 'F', long, value_enum, default_value_t = AnnotationFormatArg::Gtf)]
    annotation_format: AnnotationFormatArg,

    /// Selected annotation feature types.
    #[arg(long, value_delimiter = ',', default_value = "exon")]
    feature_type: Vec<String>,

    /// Attribute that groups features into meta-features.
    #[arg(short = 'g', long, default_value = "gene_id")]
    group_by: String,

    /// Count grouped meta-features or individual annotation records.
    #[arg(long, value_enum, default_value_t = LevelArg::Meta)]
    level: LevelArg,

    /// One strand mode for all libraries or a comma-separated mode per library.
    #[arg(
        short = 's',
        long,
        value_enum,
        value_delimiter = ',',
        default_value = "unstranded"
    )]
    strand: Vec<StrandArg>,

    /// Assignment of reads overlapping multiple targets.
    #[arg(long, value_enum, default_value_t = ContributionArg::Exclude)]
    multi_overlap: ContributionArg,

    /// Assignment of alignments with NH greater than one.
    #[arg(short = 'M', long, value_enum, default_value_t = ContributionArg::Exclude)]
    multi_map: ContributionArg,

    /// Minimum overlapping reference bases.
    #[arg(long, default_value_t = NonZeroU64::new(1).unwrap())]
    min_overlap: NonZeroU64,

    /// Minimum fraction of aligned reference bases overlapping a target.
    #[arg(long, default_value_t = 0.0, value_parser = ratio)]
    frac_overlap: f64,

    /// Minimum fraction of target bases overlapped.
    #[arg(long, default_value_t = 0.0, value_parser = ratio)]
    frac_overlap_feature: f64,

    /// Minimum mapping quality.
    #[arg(short = 'Q', long, default_value_t = 0)]
    min_mapq: u8,

    /// Exclude alignments carrying the secondary flag.
    #[arg(long)]
    primary_only: bool,

    /// Exclude PCR and optical duplicate alignments.
    #[arg(long)]
    ignore_duplicates: bool,

    /// Include any, only split, or only nonsplit alignments.
    #[arg(long, value_enum, default_value_t = SplitArg::Any)]
    split: SplitArg,

    /// Count read pairs as fragments.
    #[arg(short = 'p', long)]
    paired: bool,

    /// Require both mates to be mapped.
    #[arg(short = 'B', long, requires = "paired")]
    require_both_mapped: bool,

    /// Exclude fragments whose mapped mates use different references or orientations.
    #[arg(short = 'C', long, requires = "paired")]
    exclude_chimeric: bool,

    /// Enforce the configured fragment-length interval.
    #[arg(short = 'P', long, requires = "require_both_mapped")]
    check_fragment_length: bool,

    /// Minimum absolute template length.
    #[arg(
        short = 'd',
        long,
        default_value_t = 50,
        requires = "check_fragment_length"
    )]
    min_fragment_length: u32,

    /// Maximum absolute template length.
    #[arg(
        short = 'D',
        long,
        default_value_t = 600,
        requires = "check_fragment_length"
    )]
    max_fragment_length: u32,

    /// CSV pairs mapping annotation chromosome names to alignment names.
    #[arg(short = 'A', long, value_name = "ALIASES")]
    chromosome_aliases: Option<PathBuf>,
}

#[derive(Debug, Args)]
struct NormalizeArgs {
    /// Gene-by-sample count matrix.
    counts: PathBuf,

    /// Transactional normalized matrix.
    #[arg(short, long)]
    output: PathBuf,

    /// Separate gene and positive-length table when counts has no Length column.
    #[arg(short = 'l', long)]
    lengths: Option<PathBuf>,

    /// Normalization arithmetic.
    #[arg(short, long, value_enum, default_value_t = NormalizeModeArg::Tpm)]
    mode: NormalizeModeArg,
}

#[derive(Clone, Copy, Debug, ValueEnum)]
enum AnnotationFormatArg {
    Gtf,
    Gff3,
    Saf,
}

#[derive(Clone, Copy, Debug, ValueEnum)]
enum LevelArg {
    Meta,
    Feature,
}

#[derive(Clone, Copy, Debug, ValueEnum)]
enum StrandArg {
    Unstranded,
    Sense,
    Antisense,
}

#[derive(Clone, Copy, Debug, ValueEnum)]
enum ContributionArg {
    Exclude,
    Full,
    Fractional,
}

#[derive(Clone, Copy, Debug, ValueEnum)]
enum SplitArg {
    Any,
    Only,
    Exclude,
}

#[derive(Clone, Copy, Debug, ValueEnum)]
enum NormalizeModeArg {
    Tpm,
    Fpkm,
    FpkmUq,
}

#[derive(Debug, Serialize)]
#[serde(tag = "operation", rename_all = "kebab-case")]
pub enum CommandReport {
    Count(CountReport),
    Normalize(NormalizeReport),
}

impl Cli {
    pub fn execute(self) -> Result<CommandReport> {
        match self.command {
            Some(Command::Normalize(args)) => execute_normalize(args).map(CommandReport::Normalize),
            None => self.execute_count().map(CommandReport::Count),
        }
    }

    fn execute_count(self) -> Result<CountReport> {
        let annotation = self.count.annotation.ok_or_else(|| {
            RsomicsError::ConfigError("--annotation is required for counting".to_owned())
        })?;
        let output = self.count.output.ok_or_else(|| {
            RsomicsError::ConfigError("--output is required for counting".to_owned())
        })?;
        let summary = self.count.summary.unwrap_or_else(|| summary_path(&output));
        let mut protected: Vec<&Path> = self.count.inputs.iter().map(PathBuf::as_path).collect();
        protected.push(annotation.as_path());
        if let Some(aliases) = self.count.chromosome_aliases.as_deref() {
            protected.push(aliases);
        }
        protected.push(summary.as_path());
        rsomics_common::reject_output_alias(&output, protected)?;
        rsomics_common::reject_output_alias(
            &summary,
            self.count
                .inputs
                .iter()
                .map(PathBuf::as_path)
                .chain(std::iter::once(annotation.as_path()))
                .chain(self.count.chromosome_aliases.as_deref()),
        )?;

        count(
            &self.count.inputs,
            &annotation,
            &output,
            &summary,
            &AnnotationOptions {
                format: self.count.annotation_format.into(),
                feature_types: self.count.feature_type,
                group_by: self.count.group_by,
                level: self.count.level.into(),
            },
            &CountOptions {
                strands: self.count.strand.into_iter().map(Into::into).collect(),
                multi_overlap: self.count.multi_overlap.into(),
                multi_map: self.count.multi_map.into(),
                min_overlap: self.count.min_overlap.get(),
                frac_overlap: self.count.frac_overlap,
                frac_overlap_feature: self.count.frac_overlap_feature,
                min_mapq: self.count.min_mapq,
                primary_only: self.count.primary_only,
                ignore_duplicates: self.count.ignore_duplicates,
                split: self.count.split.into(),
                pair: PairOptions {
                    enabled: self.count.paired,
                    require_both_mapped: self.count.require_both_mapped,
                    exclude_chimeric: self.count.exclude_chimeric,
                    check_fragment_length: self.count.check_fragment_length,
                    min_fragment_length: self.count.min_fragment_length,
                    max_fragment_length: self.count.max_fragment_length,
                },
                aliases: self.count.chromosome_aliases,
                threads: self.threads.requested(),
            },
        )
    }
}

fn execute_normalize(args: NormalizeArgs) -> Result<NormalizeReport> {
    rsomics_common::reject_output_alias(
        &args.output,
        std::iter::once(args.counts.as_path()).chain(args.lengths.as_deref()),
    )?;
    normalize(
        &args.counts,
        &args.output,
        &NormalizeOptions {
            mode: args.mode.into(),
            lengths: args.lengths,
        },
    )
}

fn summary_path(output: &Path) -> PathBuf {
    let mut path = OsString::from(output.as_os_str());
    path.push(".summary");
    PathBuf::from(path)
}

fn ratio(value: &str) -> std::result::Result<f64, String> {
    let ratio: f64 = value
        .parse()
        .map_err(|_| format!("expected a ratio from 0 through 1, found {value:?}"))?;
    if ratio.is_finite() && (0.0..=1.0).contains(&ratio) {
        Ok(ratio)
    } else {
        Err(format!(
            "expected a ratio from 0 through 1, found {value:?}"
        ))
    }
}

impl From<AnnotationFormatArg> for AnnotationFormat {
    fn from(value: AnnotationFormatArg) -> Self {
        match value {
            AnnotationFormatArg::Gtf => Self::Gtf,
            AnnotationFormatArg::Gff3 => Self::Gff3,
            AnnotationFormatArg::Saf => Self::Saf,
        }
    }
}

impl From<LevelArg> for CountLevel {
    fn from(value: LevelArg) -> Self {
        match value {
            LevelArg::Meta => Self::MetaFeature,
            LevelArg::Feature => Self::Feature,
        }
    }
}

impl From<StrandArg> for StrandMode {
    fn from(value: StrandArg) -> Self {
        match value {
            StrandArg::Unstranded => Self::Unstranded,
            StrandArg::Sense => Self::Sense,
            StrandArg::Antisense => Self::Antisense,
        }
    }
}

impl From<ContributionArg> for Contribution {
    fn from(value: ContributionArg) -> Self {
        match value {
            ContributionArg::Exclude => Self::Exclude,
            ContributionArg::Full => Self::Full,
            ContributionArg::Fractional => Self::Fractional,
        }
    }
}

impl From<SplitArg> for SplitMode {
    fn from(value: SplitArg) -> Self {
        match value {
            SplitArg::Any => Self::Any,
            SplitArg::Only => Self::Only,
            SplitArg::Exclude => Self::Exclude,
        }
    }
}

impl From<NormalizeModeArg> for NormalizeMode {
    fn from(value: NormalizeModeArg) -> Self {
        match value {
            NormalizeModeArg::Tpm => Self::Tpm,
            NormalizeModeArg::Fpkm => Self::Fpkm,
            NormalizeModeArg::FpkmUq => Self::FpkmUq,
        }
    }
}

#[cfg(test)]
mod tests {
    use clap::CommandFactory;

    use super::*;

    #[test]
    fn command_tree_is_valid() {
        rsomics_help::command::<Cli>().debug_assert();
        Cli::command().debug_assert();
    }
}