Skip to content

Commit

Permalink
Merge pull request #80 from bacpop/delete-upgrade
Browse files Browse the repository at this point in the history
ska delete upgrade
  • Loading branch information
johnlees authored Aug 19, 2024
2 parents 28bca4a + c70338f commit b9c8909
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ska"
version = "0.3.9"
version = "0.3.10"
authors = [
"John Lees <[email protected]>",
"Simon Harris <[email protected]>",
Expand Down
5 changes: 5 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,13 @@ pub enum Commands {
/// Remove samples from a split k-mer file
Delete {
/// Split-kmer (.skf) file to operate on
#[arg(short, long, required = true)]
skf_file: String,

/// Output name. If not provided, will overwrite the input file
#[arg(short)]
output: Option<String>,

/// File listing sample names to remove
#[arg(short, group = "input")]
file_list: Option<String>,
Expand Down
3 changes: 1 addition & 2 deletions src/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ where
///
/// # Panics
/// - If the fit has already been run
#[allow(clippy::map_clone)]
pub fn fit_histogram(&mut self) -> Result<usize, Error> {
if self.fitted {
panic!("Model already fitted");
Expand All @@ -169,7 +168,7 @@ where
.iter()
.rev()
.skip_while(|x| **x < MIN_FREQ)
.map(|x| *x)
.copied()
.collect();
self.counts.reverse();
let counts_f64: Vec<f64> = self.counts.iter().map(|x| *x as f64).collect();
Expand Down
10 changes: 8 additions & 2 deletions src/generic_modes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,15 @@ pub fn delete<IntT: for<'a> UInt<'a>>(
log::info!("Deleting samples");
ska_array.delete_samples(delete_names);

log::info!("Saving modified skf file");
// Conditionally add suffix
let outfile_suffix = if out_file.ends_with(".skf") {
out_file.to_string()
} else {
format!("{out_file}.skf")
};
log::info!("Saving modified skf file to {outfile_suffix}");
ska_array
.save(out_file)
.save(&outfile_suffix)
.expect("Could not save modified array");
}

Expand Down
11 changes: 7 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,12 @@
//! ## ska delete
//!
//! Remove samples by name from an `.skf` file. All sample names must exist in the
//! file, or an error will be thrown. After removing samples, the input `.skf` file will be overwritten.
//! file, or an error will be thrown. After removing samples, the input `.skf` file will be overwritten,
//! unless you provide a new one with `-o`.
//! Any split k-mers which have no observed missing bases after removal will also be deleted.
//!
//! ```bash
//! ska delete all_samples.skf sample_1 sample_3
//! ska delete --skf-file all_samples.skf sample_1 sample_3
//! ```
//! If you wish to delete many samples, you can use `-f` to provide their names
//! by line via an input file.
Expand Down Expand Up @@ -662,16 +663,18 @@ pub fn main() {
}
Commands::Delete {
skf_file,
output,
file_list,
names,
} => {
let input_files = get_input_list(file_list, names);
let input_names: Vec<&str> = input_files.iter().map(|t| &*t.0).collect();
let output_file = output.clone().unwrap_or(skf_file.to_string());
log::info!("Loading skf file");
if let Ok(mut ska_array) = MergeSkaArray::<u64>::load(skf_file) {
delete(&mut ska_array, &input_names, skf_file);
delete(&mut ska_array, &input_names, &output_file);
} else if let Ok(mut ska_array) = MergeSkaArray::<u128>::load(skf_file) {
delete(&mut ska_array, &input_names, skf_file);
delete(&mut ska_array, &input_names, &output_file);
} else {
panic!("Could not read input file: {skf_file}");
}
Expand Down
13 changes: 7 additions & 6 deletions tests/skf_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ fn merge_delete() {
Command::new(cargo_bin("ska"))
.current_dir(sandbox.get_wd())
.arg("delete")
.args(&["-s", "merge.skf"])
.arg("test_3")
.arg("merge.skf")
.assert()
.failure();

Expand All @@ -69,7 +69,7 @@ fn merge_delete() {
Command::new(cargo_bin("ska"))
.current_dir(sandbox.get_wd())
.arg("delete")
.arg("merge.skf")
.args(&["-s", "merge.skf"])
.arg("test_2")
.assert()
.success();
Expand Down Expand Up @@ -125,8 +125,8 @@ fn merge_delete_u128() {
Command::new(cargo_bin("ska"))
.current_dir(sandbox.get_wd())
.arg("delete")
.arg("test_3")
.arg("merge.skf")
.args(&["-s", "merge.skf"])
.args(&["-f", &sandbox.file_string("missing_delete.txt", TestDir::Input)])
.arg("-v")
.assert()
.failure();
Expand All @@ -142,7 +142,8 @@ fn merge_delete_u128() {
Command::new(cargo_bin("ska"))
.current_dir(sandbox.get_wd())
.arg("delete")
.arg("merge.skf")
.args(&["-s", "merge.skf"])
.args(&["-o", "merge_delete"])
.arg("test_2")
.arg("-v")
.assert()
Expand All @@ -151,7 +152,7 @@ fn merge_delete_u128() {
Command::new(cargo_bin("ska"))
.current_dir(sandbox.get_wd())
.arg("nk")
.arg("merge.skf")
.arg("merge_delete.skf")
.assert()
.stdout_eq(test1_nk.stdout);
}
Expand Down
1 change: 1 addition & 0 deletions tests/test_files_in/missing_delete.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test_3

0 comments on commit b9c8909

Please sign in to comment.