Skip to content

Commit

Permalink
working on improving error-handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffersonfparil committed May 13, 2024
1 parent 61e0e5b commit a388735
Show file tree
Hide file tree
Showing 7 changed files with 539 additions and 258 deletions.
28 changes: 15 additions & 13 deletions src/aldknni.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,17 +685,17 @@ impl GenotypesAndPhenotypes {
// Instantiate output file
let mae_intermediate = match sensible_round(sum_mae / n_missing, 4) {
Ok(x) => x,
Err(e) => return Err(ImputefError {
code: 114,
message: "Error printing the MAE for the intermediate file: ".to_owned() +
&fname_intermediate_file
})
Err(e) => {
return Err(ImputefError {
code: 114,
message: "Error printing the MAE for the intermediate file: ".to_owned()
+ &fname_intermediate_file,
})
}
};
println!(
"--> {}: Writing out intermediate file with expected MAE of {}: {}",
i,
mae_intermediate,
&fname_intermediate_file
i, mae_intermediate, &fname_intermediate_file
);
let mut file_out = match OpenOptions::new()
.create_new(true)
Expand Down Expand Up @@ -820,11 +820,13 @@ impl GenotypesAndPhenotypes {
}
let mae = match sensible_round(sum_mae / n_missing, 4) {
Ok(x) => x,
Err(e) => return Err(ImputefError {
code: 122,
message: "Error in adaptive_ld_knn_imputation() method | ".to_owned() +
&e.message
})
Err(e) => {
return Err(ImputefError {
code: 122,
message: "Error in adaptive_ld_knn_imputation() method | ".to_owned()
+ &e.message,
})
}
};
Ok((vec_fname_intermediate_files_and_mae[0].0.to_owned(), mae))
}
Expand Down
28 changes: 17 additions & 11 deletions src/filter_missing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ impl GenotypesAndPhenotypes {
) -> Result<&mut Self, ImputefError> {
match self.check() {
Ok(x) => x,
Err(e) => return Err(ImputefError{
Err(e) => return Err(ImputefError {
code: 201,
message: "Error checking GenotypesAndPhenotypes in the method set_missing_by_depth() | ".to_owned() +
&e.message
})
message:
"Error checking GenotypesAndPhenotypes in the method set_missing_by_depth() | "
.to_owned()
+ &e.message,
}),
};
let (n, _p) = self.intercept_and_allele_frequencies.dim();
let (_n, l) = self.coverages.dim();
Expand All @@ -37,11 +39,13 @@ impl GenotypesAndPhenotypes {
}
match self.check() {
Ok(x) => x,
Err(e) => return Err(ImputefError{
Err(e) => return Err(ImputefError {
code: 202,
message: "Error checking GenotypesAndPhenotypes in the method set_missing_by_depth() | ".to_owned() +
&e.message
})
message:
"Error checking GenotypesAndPhenotypes in the method set_missing_by_depth() | "
.to_owned()
+ &e.message,
}),
};
Ok(self)
}
Expand Down Expand Up @@ -87,9 +91,10 @@ impl GenotypesAndPhenotypes {
missingness_per_pool.fold(0.0, |sum, &x| if x > 0.0 { sum + 1.0 } else { sum });
let n_after_filtering = n - (n_missing * frac_top_missing_pools).ceil() as usize;
if n_after_filtering == 0 {
return Err(ImputefError{
return Err(ImputefError {
code: 204,
message: "No pools left after filtering, please reduce 'frac_top_missing_pools'".to_owned(),
message: "No pools left after filtering, please reduce 'frac_top_missing_pools'"
.to_owned(),
});
}
// Sort by increasing missingness
Expand Down Expand Up @@ -187,7 +192,8 @@ impl GenotypesAndPhenotypes {
if l_after_filtering == 0 {
return Err(ImputefError {
code: 207,
message: "No loci left after filtering, please reduce 'frac_top_missing_loci'".to_owned(),
message: "No loci left after filtering, please reduce 'frac_top_missing_loci'"
.to_owned(),
});
}
// Sort by increasing missingness
Expand Down
80 changes: 49 additions & 31 deletions src/geno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ impl Parse<LocusFrequencies> for String {
if self.as_bytes()[0] == 35_u8 {
return Err(ImputefError {
code: 301,
message: "Commented out line: ".to_owned() + &self
})
message: "Commented out line: ".to_owned() + &self,
});
}
let vec_line: Vec<&str> = self.split('\t').collect();
let vec_line: Vec<&str> = if vec_line.len() == 1 {
Expand All @@ -36,7 +36,9 @@ impl Parse<LocusFrequencies> for String {
Err(_) => {
return Err(ImputefError {
code: 302,
message: "Please check format of the file: position is not and integer: ".to_owned() + &self
message: "Please check format of the file: position is not and integer: "
.to_owned()
+ &self,
})
}
};
Expand Down Expand Up @@ -143,12 +145,12 @@ impl LoadAll for FileGeno {
Err(x) => match x {
ImputefError => continue,
_ => {
return Err(ImputefError{
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
+ &fname
+ "' at line with the first 20 characters as: "
+ &line,
})
}
},
Expand Down Expand Up @@ -213,11 +215,15 @@ impl LoadAll for FileGeno {
for thread in thread_objects {
match thread.join() {
Ok(x) => x,
Err(_) => return Err(ImputefError{
code: 310,
message: "Unknown thread error occured in load() method for FileGeno struct: ".to_owned() +
&fname
})
Err(_) => {
return Err(ImputefError {
code: 310,
message:
"Unknown thread error occured in load() method for FileGeno struct: "
.to_owned()
+ &fname,
})
}
};
}
// Extract output filenames from each thread into a vector and sort them
Expand Down Expand Up @@ -423,21 +429,25 @@ pub fn load_geno<'a, 'b>(
// Extract pool names from the txt file
let file: File = File::open(fname).expect("Error reading the allele frequency table file.");
let reader = io::BufReader::new(file);
let mut header: String = match reader
.lines()
.next() {
Some(x) => match x {
Ok(y) => y,
Err(_) => return Err(ImputefError {
let mut header: String = match reader.lines().next() {
Some(x) => match x {
Ok(y) => y,
Err(_) => {
return Err(ImputefError {
code: 316,
message: "Error reading the allele frequency table file: ".to_owned() + &fname
message: "Error reading the allele frequency table file: ".to_owned() + &fname,
})
},
None => return Err(ImputefError{
}
},
None => {
return Err(ImputefError {
code: 317,
message: "Please check the format of the allele frequency table text file: ".to_owned() + &fname
message: "Please check the format of the allele frequency table text file: "
.to_owned()
+ &fname,
})
};
}
};
if header.ends_with('\n') {
header.pop();
if header.ends_with('\r') {
Expand Down Expand Up @@ -475,23 +485,31 @@ pub fn load_geno<'a, 'b>(
true => (),
false => return Err(ImputefError {
code: 319,
message: "Error in the number of pools and the pool sizes do not match in the input file: ".to_owned() +
&fname
message:
"Error in the number of pools and the pool sizes do not match in the input file: "
.to_owned()
+ &fname,
}),
};
let file_geno = FileGeno {
filename: fname.to_owned(),
};
let genotypes_and_phenotypes = match file_geno
.convert_into_genotypes_and_phenotypes(filter_stats, false, n_threads) {
let genotypes_and_phenotypes = match file_geno.convert_into_genotypes_and_phenotypes(
filter_stats,
false,
n_threads,
) {
Ok(x) => x,
Err(e) => return Err(ImputefError {
code: 320,
message: "Error parsing the genotype data (extracted from allele frequency table text file: ".to_owned() + &fname + ") via convert_into_genotypes_and_phenotypes() method within impute()."
})
message:
"Error parsing the genotype data (extracted from allele frequency table text file: "
.to_owned()
+ &fname
+ ") via convert_into_genotypes_and_phenotypes() method within impute().",
}),
};
Ok((genotypes_and_phenotypes,
filter_stats))
Ok((genotypes_and_phenotypes, filter_stats))
}

#[cfg(test)]
Expand Down
53 changes: 31 additions & 22 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ pub fn define_chunks(
let vec_idx_loci_idx_fin: Vec<usize> = vec_idx_all[1..n_chunks].to_owned();
match vec_idx_loci_idx_ini.len() == vec_idx_loci_idx_fin.len() {
true => (),
false => return Err(ImputefError {
code: 401,
message: "Error defining chunks: the number of initial and final indices are different.".to_owned()
})
false => {
return Err(ImputefError {
code: 401,
message:
"Error defining chunks: the number of initial and final indices are different."
.to_owned(),
})
}
};
Ok((vec_idx_loci_idx_ini, vec_idx_loci_idx_fin))
}
Expand All @@ -73,10 +77,12 @@ pub fn find_file_splits(fname: &str, n_threads: &usize) -> Result<Vec<u64>, Impu
let mut reader = BufReader::new(file);
let end = match reader.stream_position() {
Ok(x) => x,
Err(_) => return Err(ImputefError {
code: 403,
message: "Error navigating file: ".to_owned() + fname
})
Err(_) => {
return Err(ImputefError {
code: 403,
message: "Error navigating file: ".to_owned() + fname,
})
}
};
let mut out = (0..end)
.step_by((end as usize) / n_threads)
Expand All @@ -91,14 +97,15 @@ pub fn find_file_splits(fname: &str, n_threads: &usize) -> Result<Vec<u64>, Impu

/// Round-up an `f64` to `n_digits` decimal points
pub fn sensible_round(x: f64, n_digits: usize) -> Result<f64, ImputefError> {
let factor = match ("1e".to_owned() + &n_digits.to_string())
.parse::<f64>() {
Ok(x) => x,
Err(_) => return Err(ImputefError{
let factor = match ("1e".to_owned() + &n_digits.to_string()).parse::<f64>() {
Ok(x) => x,
Err(_) => {
return Err(ImputefError {
code: 404,
message: "Error parsing String into f64: ".to_owned() + &x.to_string()
message: "Error parsing String into f64: ".to_owned() + &x.to_string(),
})
};
}
};
Ok((x * factor).round() / factor)
}

Expand All @@ -110,10 +117,10 @@ pub fn parse_f64_roundup_and_own(x: f64, n_digits: usize) -> Result<String, Impu
}
match sensible_round(x, n_digits) {
Ok(x) => Ok(x.to_string()),
Err(e) => Err(ImputefError{
Err(e) => Err(ImputefError {
code: 405,
message: "Error in parse_f64_roundup_and_own() | ".to_owned() + &e.message
})
message: "Error in parse_f64_roundup_and_own() | ".to_owned() + &e.message,
}),
}
}

Expand Down Expand Up @@ -246,11 +253,13 @@ pub fn pearsons_correlation_pairwise_complete(
};
let r = match sensible_round(r, 7) {
Ok(x) => x,
Err(e) => return Err(ImputefError{
code: 410,
message: "Error in pearsons_correlation_pairwise_complete() | ".to_owned() +
&e.message
})
Err(e) => {
return Err(ImputefError {
code: 410,
message: "Error in pearsons_correlation_pairwise_complete() | ".to_owned()
+ &e.message,
})
}
};
Ok((r, pval))
}
Expand Down
Loading

0 comments on commit a388735

Please sign in to comment.