Skip to content

Commit

Permalink
refactor: moving toward program options structs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Patro committed Jun 2, 2022
1 parent b1da266 commit efde741
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 115 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ bincode = "1.3.3"
bstr = "0.2.17"
crossbeam-channel = "0.5.4"
crossbeam-queue = "0.3.5"
derive_builder = "0.11.2"
typed-builder = "0.10.0"
indicatif = "0.16.2"
needletail = "0.4.1"
petgraph = "0.6.0"
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod em;
pub mod eq_class;
pub mod infer;
pub mod io_utils;
pub mod prog_opts;
pub mod pugutils;
pub mod quant;
pub mod utils;
68 changes: 28 additions & 40 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use slog::{crit, o, warn, Drain};
use std::path::Path;

use alevin_fry::cellfilter::{generate_permit_list, CellFilterMethod};
use alevin_fry::prog_opts::QuantOpts;
use alevin_fry::quant::{ResolutionStrategy, SplicedAmbiguityModel};

#[global_allocator]
Expand Down Expand Up @@ -403,8 +404,8 @@ fn main() -> anyhow::Result<()> {
let dump_eq = t.is_present("dump-eqclasses");
let use_mtx = !t.is_present("use-eds");
let input_dir: String = t.value_of_t("input-dir").unwrap();
let output_dir = t.value_of_t("output-dir").unwrap();
let tg_map = t.value_of_t("tg-map").unwrap();
let output_dir: String = t.value_of_t("output-dir").unwrap();
let tg_map: String = t.value_of_t("tg-map").unwrap();
let resolution: ResolutionStrategy = t.value_of_t("resolution").unwrap();
let sa_model: SplicedAmbiguityModel = t.value_of_t("sa-model").unwrap();
let small_thresh = t.value_of_t("small-thresh").unwrap();
Expand Down Expand Up @@ -488,6 +489,29 @@ fn main() -> anyhow::Result<()> {
}
}

let quant_opts = QuantOpts::builder()
.input_dir(input_dir.clone())
.tg_map(tg_map.clone())
.output_dir(output_dir.clone())
.num_threads(num_threads)
.num_bootstraps(num_bootstraps)
.init_uniform(init_uniform)
.summary_stat(summary_stat)
.dump_eq(dump_eq)
.use_mtx(use_mtx)
.resolution(resolution)
.sa_model(sa_model)
.small_thresh(small_thresh)
.large_graph_thresh(large_graph_thresh)
.filter_list(filter_list)
.pug_exact_umi(pug_exact_umi)
.cmdline(&cmdline)
.version(VERSION)
.log(&log)
.build();

println!("PO : {:?}", quant_opts);

// first make sure that the input direcory passed in has the
// appropriate json file in it.
// we should take care to document this workflow explicitly.
Expand All @@ -500,24 +524,7 @@ fn main() -> anyhow::Result<()> {
if json_path.exists() {
let velo_mode = alevin_fry::utils::is_velo_mode(input_dir.to_string());
if velo_mode {
match alevin_fry::quant::velo_quantify(
input_dir,
tg_map,
output_dir,
num_threads,
num_bootstraps,
init_uniform,
summary_stat,
dump_eq,
use_mtx,
resolution,
sa_model,
small_thresh,
filter_list,
&cmdline,
VERSION,
&log,
) {
match alevin_fry::quant::velo_quantify(quant_opts) {
// if we're all good; then great!
Ok(_) => {}
// if we have an error, see if it's an error parsing
Expand All @@ -542,26 +549,7 @@ fn main() -> anyhow::Result<()> {
},
}; // end match if
} else {
match alevin_fry::quant::quantify(
input_dir,
tg_map,
output_dir,
num_threads,
num_bootstraps,
init_uniform,
summary_stat,
dump_eq,
use_mtx,
resolution,
pug_exact_umi,
sa_model,
small_thresh,
large_graph_thresh,
filter_list,
&cmdline,
VERSION,
&log,
) {
match alevin_fry::quant::quantify(quant_opts) {
// if we're all good; then great!
Ok(_) => {}
// if we have an error, see if it's an error parsing
Expand Down
26 changes: 26 additions & 0 deletions src/prog_opts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::quant::{ResolutionStrategy, SplicedAmbiguityModel};
//use derive_builder::Builder;
use typed_builder::TypedBuilder;

#[derive(TypedBuilder, Debug)]
//#[builder(name = "QuantOptsBuilder")]
pub struct QuantOpts<'a, 'b, 'c, 'd> {
pub input_dir: String,
pub tg_map: String,
pub output_dir: String,
pub num_threads: u32,
pub num_bootstraps: u32,
pub init_uniform: bool,
pub summary_stat: bool,
pub dump_eq: bool,
pub use_mtx: bool,
pub resolution: ResolutionStrategy,
pub pug_exact_umi: bool,
pub sa_model: SplicedAmbiguityModel,
pub small_thresh: usize,
pub large_graph_thresh: usize,
pub filter_list: Option<&'a str>,
pub cmdline: &'b str,
pub version: &'c str,
pub log: &'d slog::Logger,
}
128 changes: 53 additions & 75 deletions src/quant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use flate2::Compression;
use crate::em::{em_optimize, em_optimize_subset, run_bootstrap, EmInitType};
use crate::eq_class::{EqMap, EqMapType, IndexedEqList};
use crate::io_utils;
use crate::prog_opts::QuantOpts;
use crate::pugutils;
use crate::utils as afutils;
use libradicl::rad_types;
Expand All @@ -51,6 +52,12 @@ pub enum SplicedAmbiguityModel {
WinnerTakeAll,
}

impl Default for SplicedAmbiguityModel {
fn default() -> Self {
SplicedAmbiguityModel::WinnerTakeAll
}
}

// Implement the trait
impl FromStr for SplicedAmbiguityModel {
type Err = &'static str;
Expand All @@ -74,6 +81,12 @@ pub enum ResolutionStrategy {
ParsimonyGene,
}

impl Default for ResolutionStrategy {
fn default() -> Self {
ResolutionStrategy::CellRangerLike
}
}

impl fmt::Display for ResolutionStrategy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
Expand Down Expand Up @@ -298,27 +311,9 @@ fn write_eqc_counts(
// TODO: see if we'd rather pass an structure
// with these options
#[allow(clippy::too_many_arguments)]
pub fn quantify(
input_dir: String,
tg_map: String,
output_dir: String,
num_threads: u32,
num_bootstraps: u32,
init_uniform: bool,
summary_stat: bool,
dump_eq: bool,
use_mtx: bool,
resolution: ResolutionStrategy,
pug_exact_umi: bool,
sa_model: SplicedAmbiguityModel,
small_thresh: usize,
large_graph_thresh: usize,
filter_list: Option<&str>,
cmdline: &str,
version: &str,
log: &slog::Logger,
) -> anyhow::Result<()> {
let parent = std::path::Path::new(&input_dir);
pub fn quantify(quant_opts: QuantOpts) -> anyhow::Result<()> {
let parent = std::path::Path::new(&quant_opts.input_dir);
let log = quant_opts.log;

// read the collate metadata
let collate_md_file =
Expand All @@ -341,25 +336,25 @@ pub fn quantify(
);

do_quantify(
input_dir,
quant_opts.input_dir,
br,
tg_map,
output_dir,
num_threads,
num_bootstraps,
init_uniform,
summary_stat,
dump_eq,
use_mtx,
resolution,
pug_exact_umi,
sa_model,
small_thresh,
large_graph_thresh,
filter_list,
cmdline,
version,
log,
quant_opts.tg_map,
quant_opts.output_dir,
quant_opts.num_threads,
quant_opts.num_bootstraps,
quant_opts.init_uniform,
quant_opts.summary_stat,
quant_opts.dump_eq,
quant_opts.use_mtx,
quant_opts.resolution,
quant_opts.pug_exact_umi,
quant_opts.sa_model,
quant_opts.small_thresh,
quant_opts.large_graph_thresh,
quant_opts.filter_list,
quant_opts.cmdline,
quant_opts.version,
&log,
)
} else {
let i_file =
Expand All @@ -372,25 +367,25 @@ pub fn quantify(
);

do_quantify(
input_dir,
quant_opts.input_dir,
br,
tg_map,
output_dir,
num_threads,
num_bootstraps,
init_uniform,
summary_stat,
dump_eq,
use_mtx,
resolution,
pug_exact_umi,
sa_model,
small_thresh,
large_graph_thresh,
filter_list,
cmdline,
version,
log,
quant_opts.tg_map,
quant_opts.output_dir,
quant_opts.num_threads,
quant_opts.num_bootstraps,
quant_opts.init_uniform,
quant_opts.summary_stat,
quant_opts.dump_eq,
quant_opts.use_mtx,
quant_opts.resolution,
quant_opts.pug_exact_umi,
quant_opts.sa_model,
quant_opts.small_thresh,
quant_opts.large_graph_thresh,
quant_opts.filter_list,
quant_opts.cmdline,
quant_opts.version,
&log,
)
}
}
Expand Down Expand Up @@ -1375,24 +1370,7 @@ pub fn do_quantify<T: Read>(
// TODO: see if we'd rather pass an structure
// with these options
#[allow(clippy::too_many_arguments)]
pub fn velo_quantify(
_input_dir: String,
_tg_map: String,
_output_dir: String,
_num_threads: u32,
_num_bootstraps: u32,
_init_uniform: bool,
_summary_stat: bool,
_dump_eq: bool,
_use_mtx: bool,
_resolution: ResolutionStrategy,
mut _sa_model: SplicedAmbiguityModel,
_small_thresh: usize,
_filter_list: Option<&str>,
_cmdline: &str,
_version: &str,
_log: &slog::Logger,
) -> anyhow::Result<()> {
pub fn velo_quantify(_quant_opts: QuantOpts) -> anyhow::Result<()> {
unimplemented!("not implemented on this branch yet");
//Ok(())
}

0 comments on commit efde741

Please sign in to comment.