Skip to content

Commit

Permalink
Update to clap 3.2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Jun 18, 2022
1 parent fa8e618 commit 4c24e8b
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 46 deletions.
60 changes: 55 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ codegen-units = 1

[dependencies]
anyhow = "1.0.32"
clap = { version = "3.1.0", features = ["cargo"] }
clap = { version = "3.2.5", features = ["cargo", "deprecated"] }
flate2 = "1.0.14"
futures = { version = "0.3.17", default-features = false, features = ["std"] }
git-testament = "0.2.0"
Expand Down
17 changes: 17 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ pub enum StrandSpecificationOption {
Auto,
}

impl clap::ValueEnum for StrandSpecificationOption {
fn value_variants<'a>() -> &'a [Self] {
&[Self::None, Self::Forward, Self::Reverse, Self::Auto]
}

fn to_possible_value<'a>(&self) -> Option<clap::PossibleValue<'a>> {
use clap::PossibleValue;

match self {
Self::None => Some(PossibleValue::new("none")),
Self::Forward => Some(PossibleValue::new("forward")),
Self::Reverse => Some(PossibleValue::new("reverse")),
Self::Auto => Some(PossibleValue::new("auto")),
}
}
}

impl FromStr for StrandSpecificationOption {
type Err = String;

Expand Down
88 changes: 48 additions & 40 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use mimalloc::MiMalloc;
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;

use std::thread;
use std::{path::PathBuf, thread};

use clap::{crate_name, Arg, ArgMatches, Command};
use clap::{crate_name, value_parser, Arg, ArgMatches, Command};
use git_testament::{git_testament, render_testament};
use squab::{commands, count::Filter};
use squab::{commands, count::Filter, normalization::Method, StrandSpecificationOption};
use tracing::{info, warn};

git_testament!(TESTAMENT);
Expand All @@ -18,25 +18,28 @@ fn match_args_from_env() -> clap::ArgMatches {
.arg(
Arg::new("with-secondary-records")
.long("with-secondary-records")
.help("Count secondary records (BAM flag 0x100)"),
.help("Count secondary records (BAM flag 0x100)")
.action(clap::ArgAction::SetTrue),
)
.arg(
Arg::new("with-supplementary-records")
.long("with-supplementary-records")
.help("Count supplementary records (BAM flag 0x800)"),
.help("Count supplementary records (BAM flag 0x800)")
.action(clap::ArgAction::SetTrue),
)
.arg(
Arg::new("with-nonunique-records")
.long("with-nonunique-records")
.help("Count nonunique records (BAM data tag NH > 1)"),
.help("Count nonunique records (BAM data tag NH > 1)")
.action(clap::ArgAction::SetTrue),
)
.arg(
Arg::new("strand-specification")
.long("strand-specification")
.value_name("str")
.help("Strand specification")
.possible_values(&["none", "forward", "reverse", "auto"])
.default_value("auto"),
.default_value("auto")
.value_parser(value_parser!(StrandSpecificationOption)),
)
.arg(
Arg::new("feature-type")
Expand All @@ -59,35 +62,40 @@ fn match_args_from_env() -> clap::ArgMatches {
.long("min-mapping-quality")
.value_name("u8")
.help("Minimum mapping quality to consider an alignment")
.default_value("10"),
.default_value("10")
.value_parser(value_parser!(u8)),
)
.arg(
Arg::new("output")
.short('o')
.long("output")
.value_name("file")
.value_name("path")
.help("Output destination for feature counts")
.value_parser(value_parser!(PathBuf))
.required(true),
)
.arg(
Arg::new("annotations")
.short('a')
.long("annotations")
.value_name("file")
.value_name("path")
.help("Input annotations file (GFF3)")
.value_parser(value_parser!(PathBuf))
.required(true),
)
.arg(
Arg::new("threads")
.long("threads")
.value_name("uint")
.help("Force a specific number of threads"),
.help("Force a specific number of threads")
.value_parser(value_parser!(usize)),
)
.arg(
Arg::new("bam")
.index(1)
.help("Input alignment file")
.required(true)
.index(1),
.value_parser(value_parser!(PathBuf))
.required(true),
);

let normalize_cmd = Command::new("normalize")
Expand All @@ -114,21 +122,23 @@ fn match_args_from_env() -> clap::ArgMatches {
.long("annotations")
.value_name("file")
.help("Input annotations file (GFF3)")
.value_parser(value_parser!(PathBuf))
.required(true),
)
.arg(
Arg::new("method")
.long("method")
.value_name("str")
.help("Quantification normalization method")
.possible_values(&["fpkm", "tpm"])
.default_value("tpm"),
.default_value("tpm")
.value_parser(value_parser!(Method)),
)
.arg(
Arg::new("counts")
.index(1)
.help("Input counts file")
.required(true)
.index(1),
.value_parser(value_parser!(PathBuf)),
);

Command::new(crate_name!())
Expand All @@ -140,6 +150,7 @@ fn match_args_from_env() -> clap::ArgMatches {
.short('v')
.long("verbose")
.help("Use verbose logging")
.action(clap::ArgAction::SetTrue)
.hide(true),
)
.subcommand(quantify_cmd)
Expand All @@ -148,28 +159,25 @@ fn match_args_from_env() -> clap::ArgMatches {
}

fn quantify(matches: &ArgMatches) -> anyhow::Result<()> {
let bam_src = matches.value_of("bam").unwrap();
let annotations_src = matches.value_of("annotations").unwrap();
let bam_src: &PathBuf = matches.get_one("bam").unwrap();
let annotations_src: &PathBuf = matches.get_one("annotations").unwrap();

let results_dst = matches.value_of("output").unwrap();
let results_dst: &PathBuf = matches.get_one("output").unwrap();

let feature_type = matches.value_of("feature-type").unwrap();
let id = matches.value_of("id").unwrap();
let min_mapping_quality = matches
.value_of_t("min-mapping-quality")
.unwrap_or_else(|e| e.exit());
let feature_type: &String = matches.get_one("feature-type").unwrap();
let id: &String = matches.get_one("id").unwrap();
let min_mapping_quality = *matches.get_one("min-mapping-quality").unwrap();

let with_secondary_records = matches.is_present("with-secondary-records");
let with_supplementary_records = matches.is_present("with-supplementary-records");
let with_nonunique_records = matches.is_present("with-nonunique-records");
let with_secondary_records = *matches.get_one("with-secondary-records").unwrap();
let with_supplementary_records = *matches.get_one("with-supplementary-records").unwrap();
let with_nonunique_records = *matches.get_one("with-nonunique-records").unwrap();

let threads = matches
.value_of_t("threads")
.or_else(|_| thread::available_parallelism().map(usize::from))?;
let threads = match matches.get_one("threads") {
Some(n) => *n,
None => thread::available_parallelism().map(usize::from)?,
};

let strand_specification_option = matches
.value_of_t("strand-specification")
.unwrap_or_else(|e| e.exit());
let strand_specification_option = *matches.get_one("strand-specification").unwrap();

let filter = Filter::new(
min_mapping_quality,
Expand Down Expand Up @@ -197,13 +205,13 @@ fn quantify(matches: &ArgMatches) -> anyhow::Result<()> {
}

fn normalize(matches: &ArgMatches) -> anyhow::Result<()> {
let counts_src = matches.value_of("counts").unwrap();
let annotations_src = matches.value_of("annotations").unwrap();
let counts_src: &PathBuf = matches.get_one("counts").unwrap();
let annotations_src: &PathBuf = matches.get_one("annotations").unwrap();

let feature_type = matches.value_of("feature-type").unwrap();
let id = matches.value_of("id").unwrap();
let feature_type: &String = matches.get_one("feature-type").unwrap();
let id: &String = matches.get_one("id").unwrap();

let method = matches.value_of_t("method").unwrap_or_else(|e| e.exit());
let method = *matches.get_one("method").unwrap();

commands::normalize(counts_src, annotations_src, feature_type, id, method)
}
Expand All @@ -213,7 +221,7 @@ fn main() -> anyhow::Result<()> {

tracing_subscriber::fmt::init();

if matches.is_present("verbose") {
if let Some(true) = matches.get_one("verbose").copied() {
warn!("`-v`/`--verbose` is deprecated and will be removed in a future version. Logging is now always enabled.");
}

Expand Down
15 changes: 15 additions & 0 deletions src/normalization/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ pub enum Method {
Tpm,
}

impl clap::ValueEnum for Method {
fn value_variants<'a>() -> &'a [Self] {
&[Self::Fpkm, Self::Tpm]
}

fn to_possible_value<'a>(&self) -> Option<clap::PossibleValue<'a>> {
use clap::PossibleValue;

match self {
Self::Fpkm => Some(PossibleValue::new("fpkm")),
Self::Tpm => Some(PossibleValue::new("tpm")),
}
}
}

#[derive(Debug, Eq, PartialEq)]
pub struct ParseError(String);

Expand Down

0 comments on commit 4c24e8b

Please sign in to comment.