Skip to content

Commit

Permalink
improved error handling with an error type emitting a specific error …
Browse files Browse the repository at this point in the history
…code and a message and chainable
  • Loading branch information
jeffersonfparil committed May 14, 2024
1 parent a388735 commit 79b03af
Show file tree
Hide file tree
Showing 6 changed files with 720 additions and 303 deletions.
6 changes: 3 additions & 3 deletions src/aldknni.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ impl GenotypesAndPhenotypes {
// Instantiate output file
let mae_intermediate = match sensible_round(sum_mae / n_missing, 4) {
Ok(x) => x,
Err(e) => {
Err(_e) => {
return Err(ImputefError {
code: 114,
message: "Error printing the MAE for the intermediate file: ".to_owned()
Expand Down Expand Up @@ -931,7 +931,7 @@ pub fn impute_aldknni(
let end = std::time::SystemTime::now();
let duration = match end.duration_since(start) {
Ok(x) => x,
Err(e) => return Err(ImputefError{
Err(_e) => return Err(ImputefError{
code: 123,
message: "Error measuring the duration of running adaptive_ld_knn_imputation() within impute_aldknni().".to_owned()
})
Expand All @@ -956,7 +956,7 @@ pub fn impute_aldknni(
})
}
};
match remove_file(fname_imputed) {
match remove_file(&fname_imputed) {
Ok(x) => x,
Err(_) => {
return Err(ImputefError {
Expand Down
15 changes: 2 additions & 13 deletions src/geno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,7 @@ impl LoadAll for FileGeno {
// Parse the geno line
let allele_freqs: LocusFrequencies = match line.lparse() {
Ok(x) => *x,
Err(x) => match x {
ImputefError => continue,
_ => {
return Err(ImputefError {
code: 308,
message: "T_T Input sync file error, i.e. '".to_owned()
+ &fname
+ "' at line with the first 20 characters as: "
+ &line,
})
}
},
Err(_x) => continue,
};
freq.push(allele_freqs);
}
Expand Down Expand Up @@ -500,7 +489,7 @@ pub fn load_geno<'a, 'b>(
n_threads,
) {
Ok(x) => x,
Err(e) => return Err(ImputefError {
Err(_e) => return Err(ImputefError {
code: 320,
message:
"Error parsing the genotype data (extracted from allele frequency table text file: "
Expand Down
154 changes: 95 additions & 59 deletions src/mvi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ impl GenotypesAndPhenotypes {
pub fn mean_imputation(&mut self) -> Result<&mut Self, ImputefError> {
match self.check() {
Ok(x) => x,
Err(e) => return Err(ImputefError{
code: 501,
message: "Error checking GenotypesAndPhenotypes type in method mean_imputation() | ".to_owned() + &e.message
})
Err(e) => {
return Err(ImputefError {
code: 501,
message:
"Error checking GenotypesAndPhenotypes type in method mean_imputation() | "
.to_owned()
+ &e.message,
})
}
};
// We are assuming that all non-zero alleles across pools are kept, i.e. biallelic loci have 2 columns, triallelic have 3, and so on.
let (n, p) = self.intercept_and_allele_frequencies.dim();
Expand Down Expand Up @@ -90,10 +95,15 @@ impl GenotypesAndPhenotypes {
) -> Result<(&mut Self, SparsitySimulationOutput), ImputefError> {
match self.check() {
Ok(x) => x,
Err(e) => return Err(ImputefError{
code: 504,
message: "Error checking GenotypesAndPhenotypes type in method simulate_missing() | ".to_owned() + &e.message
})
Err(e) => {
return Err(ImputefError {
code: 504,
message:
"Error checking GenotypesAndPhenotypes type in method simulate_missing() | "
.to_owned()
+ &e.message,
})
}
};
let (n, l) = self.coverages.dim();
// let (loci_idx, _loci_chr, _loci_pos) = self.count_loci().expect("Error defining loci indexes and identities via count_loci() method within simulate_missing() method for GenotypesAndPhenotypes struct.");
Expand Down Expand Up @@ -145,10 +155,15 @@ impl GenotypesAndPhenotypes {
}
match self.check() {
Ok(x) => x,
Err(e) => return Err(ImputefError{
code: 505,
message: "Error checking GenotypesAndPhenotypes type in method simulate_missing() | ".to_owned() + &e.message
})
Err(e) => {
return Err(ImputefError {
code: 505,
message:
"Error checking GenotypesAndPhenotypes type in method simulate_missing() | "
.to_owned()
+ &e.message,
})
}
};
Ok((
self,
Expand Down Expand Up @@ -228,10 +243,14 @@ impl GenotypesAndPhenotypes {
};
match max_sparsity == missing_rate_sim {
true => (),
false => return Err(ImputefError{
code: 508,
message: "Error: the simulated sparsity does not match the sparsity level requested.".to_owned()
})
false => {
return Err(ImputefError {
code: 508,
message:
"Error: the simulated sparsity does not match the sparsity level requested."
.to_owned(),
})
}
};
let _ = genotype_data_for_optimisation.mean_imputation().expect("Error calling mean_imputation() within estimate_expected_mae_in_mvi() method for GenotypesAndPhenotypes struct.");
let vec_vec_imputed_mask = match genotype_data_for_optimisation
Expand Down Expand Up @@ -297,14 +316,16 @@ pub fn impute_mean(
out: &str,
) -> Result<String, ImputefError> {
// Estimate predicted imputation accuracy
let mae = match genotypes_and_phenotypes
.estimate_expected_mae_in_mvi() {
let mae =
match genotypes_and_phenotypes.estimate_expected_mae_in_mvi() {
Ok(x) => x,
Err(e) => return Err(ImputefError{
Err(e) => return Err(ImputefError {
code: 510,
message: "Error calling estimate_expected_mae_in_mvi() method within impute_mean() | ".to_owned() +
&e.message
})
message:
"Error calling estimate_expected_mae_in_mvi() method within impute_mean() | "
.to_owned()
+ &e.message,
}),
};
println!("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
println!(
Expand All @@ -313,57 +334,71 @@ pub fn impute_mean(
);
println!("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
let start = std::time::SystemTime::now();
match genotypes_and_phenotypes
.mean_imputation() {
Ok(x) => x,
Err(e) => return Err(ImputefError{
match genotypes_and_phenotypes.mean_imputation() {
Ok(x) => x,
Err(e) => {
return Err(ImputefError {
code: 511,
message: "Error calling mean_imputation() method within impute_mean() | ".to_owned() +
&e.message
message: "Error calling mean_imputation() method within impute_mean() | "
.to_owned()
+ &e.message,
})
};
}
};
let end = std::time::SystemTime::now();
let duration = match end
.duration_since(start) {
Ok(x) => x,
Err(_) => return Err(ImputefError{
let duration = match end.duration_since(start) {
Ok(x) => x,
Err(_) => {
return Err(ImputefError {
code: 512,
message: "Error measuring the duration of mean value imputation within impute_mean()".to_owned()
message:
"Error measuring the duration of mean value imputation within impute_mean()"
.to_owned(),
})
};
}
};
println!(
"Mean value imputation: {} pools x {} loci | Missingness: {}% | Duration: {} seconds",
genotypes_and_phenotypes.coverages.nrows(),
genotypes_and_phenotypes.coverages.ncols(),
match genotypes_and_phenotypes.missing_rate() {
Ok(x) => x,
Err(_) => return Err(ImputefError{
code: 513,
message: "Error measuring sparsity after mean value imputation within impute_mean().".to_owned()
})
Err(_) =>
return Err(ImputefError {
code: 513,
message:
"Error measuring sparsity after mean value imputation within impute_mean()."
.to_owned()
}),
},
duration.as_secs_f64()
);
// Remove 100% of the loci with missing data
let start = std::time::SystemTime::now();
match genotypes_and_phenotypes
.filter_out_top_missing_loci(&1.00) {
Ok(x) => x,
Err(e) => return Err(ImputefError{
match genotypes_and_phenotypes.filter_out_top_missing_loci(&1.00) {
Ok(x) => x,
Err(e) => {
return Err(ImputefError {
code: 514,
message: "Error calling filter_out_top_missing_loci() method within impute_mean() | ".to_owned() +
&e.message
message:
"Error calling filter_out_top_missing_loci() method within impute_mean() | "
.to_owned()
+ &e.message,
})
};
}
};
let end = std::time::SystemTime::now();
let duration = match end
.duration_since(start) {
Ok(x) => x,
Err(_) => return Err(ImputefError{
let duration = match end.duration_since(start) {
Ok(x) => x,
Err(_) => {
return Err(ImputefError {
code: 515,
message: "Error measuring the duration of mean value imputation within impute_mean()".to_owned()
message:
"Error measuring the duration of mean value imputation within impute_mean()"
.to_owned(),
})
};
}
};
println!(
"Missing data removed, i.e. loci which cannot be imputed because of extreme sparsity: {} pools x {} loci | Missingness: {}% | Duration: {} seconds",
genotypes_and_phenotypes.coverages.nrows(),
Expand All @@ -378,15 +413,16 @@ pub fn impute_mean(
duration.as_secs_f64()
);
// Output
let out = match genotypes_and_phenotypes
.write_tsv(filter_stats, false, out, n_threads) {
Ok(x) => x,
Err(e) => return Err(ImputefError{
let out = match genotypes_and_phenotypes.write_tsv(filter_stats, false, out, n_threads) {
Ok(x) => x,
Err(e) => {
return Err(ImputefError {
code: 517,
message: "Error writing the output of mean value imputation within impute_mean() | ".to_owned() +
&e.message
message: "Error writing the output of mean value imputation within impute_mean() | "
.to_owned() + &e.message,
})
};
}
};

Ok(out)
}
Expand Down
34 changes: 20 additions & 14 deletions src/phen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use ndarray::prelude::*;
use std::fs::File;
use std::io::{self, prelude::*, BufReader, Error, ErrorKind};
use std::io::{prelude::*, BufReader};
use std::{str, vec};

use crate::structs_and_traits::*;
Expand Down Expand Up @@ -36,10 +36,12 @@ impl Parse<Phen> for FilePhen {
let mut phen_vec: Vec<f64> = vec![];
let file = match File::open(filename_phen.clone()) {
Ok(x) => x,
Err(_) => return Err(ImputefError{
code: 601,
message: "Input phenotype file not found: ".to_owned() + &filename_phen
})
Err(_) => {
return Err(ImputefError {
code: 601,
message: "Input phenotype file not found: ".to_owned() + &filename_phen,
})
}
};
let reader = BufReader::new(file);
for l in reader.lines() {
Expand Down Expand Up @@ -118,10 +120,12 @@ impl Parse<Phen> for FilePhen {
let filename_phen = self.filename.clone();
let file = match File::open(filename_phen.clone()) {
Ok(x) => x,
Err(_) => return Err(ImputefError{
code: 604,
message: "Input phenotype file not found: ".to_owned() + &filename_phen
})
Err(_) => {
return Err(ImputefError {
code: 604,
message: "Input phenotype file not found: ".to_owned() + &filename_phen,
})
}
};
let reader = BufReader::new(file);
let mut all_lines: Vec<String> = vec![];
Expand Down Expand Up @@ -188,7 +192,7 @@ impl Parse<Phen> for FilePhen {
return Err(ImputefError{
code: 606,
message: "Error: invalid phenotype format. Please select: 'default' or 'gwalpha_fmt'. File: ".to_owned()
})
});
}
}
}
Expand All @@ -206,7 +210,7 @@ impl Parse<FileSyncPhen> for (FileSync, FilePhen) {
////////////////////
let phen = match self.1.lparse() {
Ok(x) => x,
Err(e) => return Err(ImputefError{
Err(_e) => return Err(ImputefError{
code: 607,
message: "Error calling lparse() within the lparse() method for the (FileSync, FilePhen) struct. File: ".to_owned() +
&filename_sync
Expand Down Expand Up @@ -239,10 +243,12 @@ impl Parse<FileSyncPhen> for (FileSync, FilePhen) {
test,
}));
} else {
return Err(ImputefError{
return Err(ImputefError {
code: 610,
message: "Erro: invalid phenotype format. Please select: 'default' or 'gwalpha_fmt'".to_owned()
})
message:
"Erro: invalid phenotype format. Please select: 'default' or 'gwalpha_fmt'"
.to_owned(),
});
}
}
}
Expand Down
Loading

0 comments on commit 79b03af

Please sign in to comment.