From cf7116d7773951c67b593411b003b596c6d0637f Mon Sep 17 00:00:00 2001 From: Cj Jiang Date: Sun, 14 Jul 2024 12:46:57 +1000 Subject: [PATCH] feat: argument bounds checking --- src/base/filter_stats.rs | 57 ++++++++++++++++++++++++++++++++++++++++ src/base/mod.rs | 1 + src/main.rs | 3 ++- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/base/filter_stats.rs diff --git a/src/base/filter_stats.rs b/src/base/filter_stats.rs new file mode 100644 index 0000000..14f91e6 --- /dev/null +++ b/src/base/filter_stats.rs @@ -0,0 +1,57 @@ +use std::io::{self, Error, ErrorKind}; + +use crate::base::*; + +impl Parse for FilterStats { + fn lparse(&self) -> io::Result> { + let remove_ns = self.remove_ns.clone(); + let remove_monoallelic = self.remove_monoallelic.clone(); + let keep_lowercase_reference = self.keep_lowercase_reference.clone(); + let max_base_error_rate = if self.max_base_error_rate <= 1.0 && self.max_base_error_rate >= 0.0 { + self.max_base_error_rate.clone() + } else { + return Err(Error::new( + ErrorKind::Other, + "Invalid range. max_base_error_rate must be between 0.0 and 1.0", + )); + }; + let min_coverage_depth = self.min_coverage_depth; + let min_coverage_breadth = if self.min_coverage_breadth <= 1.0 && self.max_base_error_rate >= 0.0 { + self.min_coverage_breadth.clone() + } else { + return Err(Error::new( + ErrorKind::Other, + "Invalid range. min_coverage_breadth must be between 0.0 and 1.0", + )); + }; + let min_allele_frequency = if self.min_allele_frequency <= 1.0 && self.min_allele_frequency >= 0.0 { + self.min_allele_frequency.clone() + } else { + return Err(Error::new( + ErrorKind::Other, + "Invalid range. min_allele_frequency must be between 0.0 and 1.0", + )); + }; + let max_missingness_rate = if self.max_missingness_rate <= 1.0 && self.max_missingness_rate >= 0.0 { + self.max_missingness_rate.clone() + } else { + return Err(Error::new( + ErrorKind::Other, + "Invalid range. max_missingness_rate must be between 0.0 and 1.0", + )); + }; + let pool_sizes = self.pool_sizes.clone(); + return Ok(Box::new(FilterStats { + remove_ns, + remove_monoallelic, + keep_lowercase_reference, + max_base_error_rate, + min_coverage_depth, + min_coverage_breadth, + min_allele_frequency, + max_missingness_rate, + pool_sizes, + })); + } +} + diff --git a/src/base/mod.rs b/src/base/mod.rs index 069ad76..8c99ed7 100644 --- a/src/base/mod.rs +++ b/src/base/mod.rs @@ -6,3 +6,4 @@ mod pileup; mod structs_and_traits; mod sync; mod vcf; +mod filter_stats; diff --git a/src/main.rs b/src/main.rs index f0a4058..5674432 100644 --- a/src/main.rs +++ b/src/main.rs @@ -193,7 +193,7 @@ fn main() { format: phen_format, }; let phen = file_phen.lparse().unwrap(); - let filter_stats = base::FilterStats { + let file_filter_stats = base::FilterStats { remove_ns: !args.keep_ns, remove_monoallelic: args.remove_monoallelic, keep_lowercase_reference: args.keep_lowercase_reference, @@ -204,6 +204,7 @@ fn main() { max_missingness_rate: args.max_missingness_rate, pool_sizes: phen.pool_sizes.clone(), }; + let filter_stats = file_filter_stats.lparse().unwrap(); if args.analysis == String::from("pileup2sync") { // PILEUP INPUT let file_pileup = base::FilePileup {