Skip to content

Commit

Permalink
Add appropriate extension to -o files if not present
Browse files Browse the repository at this point in the history
  • Loading branch information
Roderick Bovee committed Dec 15, 2017
1 parent fd7e52f commit f5a9858
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 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 = "finch"
version = "0.1.4"
version = "0.1.5"
authors = ["Roderick Bovee & One Codex <[email protected]>"]
description = "An implementation of min-wise independent permutation locality sensitive hashing ('MinHashing') for genomic data and command-line utility for manipulation."
keywords = ["minhash", "bioinformatics", "sketches"]
Expand Down
25 changes: 18 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ macro_rules! add_output_options {
};
}

// got a little macro-happy here; this could maybe just be a function
// and we could be smarter about how we pass borrows in here?
macro_rules! output_to {
($object: ident, $matches: ident) => {
($object: ident, $matches: ident, $default_ext: expr) => {
let output = $matches.value_of("output_file");
match output {
None => {
Expand All @@ -42,8 +44,17 @@ macro_rules! output_to {
})?;
println!("{}", text);
},
Some(out_filename) => {
let mut out = File::create(out_filename).map_err(|_| {
Some(o) => {
// if the filename doesn't have the right extension
// add it on
let filename = String::from(o);
let out_filename = if filename.ends_with($default_ext) {
filename
} else {
filename + $default_ext
};

let mut out = File::create(&out_filename).map_err(|_| {
format!("Could not create {}", out_filename)
})?;
let _ = out.write_all(&serde_json::to_vec(&$object).map_err(|_| {
Expand Down Expand Up @@ -193,7 +204,7 @@ fn run() -> Result<(), String> {
if let Some(matches) = matches.subcommand_matches("sketch") {
if matches.is_present("output_file") || matches.is_present("std_out") {
let sketches = parse_all_mash_files(matches)?;
output_to!(sketches, matches);
output_to!(sketches, matches, FINCH_EXT);
} else {
// "sketch in place"
parse_mash_files(matches, |multisketch, filename| {
Expand Down Expand Up @@ -222,7 +233,7 @@ fn run() -> Result<(), String> {
let all_sketches = parse_all_mash_files(matches)?;
distances.extend(calc_sketch_distances(&all_sketches.sketches, &all_sketches.sketches, mash_mode, max_dist));

output_to!(distances, matches);
output_to!(distances, matches, ".json");
return Ok(());
}

Expand Down Expand Up @@ -256,7 +267,7 @@ fn run() -> Result<(), String> {
let sketches = open_mash_file(filename, matches, Some(&first_sketch))?;
distances.extend(calc_sketch_distances(&query_sketches, &sketches.sketches, mash_mode, max_dist));
}
output_to!(distances, matches);
output_to!(distances, matches, ".json");
} else if let Some(matches) = matches.subcommand_matches("hist") {
let mut hist_map: HashMap<String, Vec<u64>> = HashMap::new();
parse_mash_files(matches, |multisketch, _| {
Expand All @@ -265,7 +276,7 @@ fn run() -> Result<(), String> {
}
})?;

output_to!(hist_map, matches);
output_to!(hist_map, matches, ".json");
} else if let Some(matches) = matches.subcommand_matches("info") {
parse_mash_files(matches, |multisketch, _| {
for sketch in multisketch.sketches.iter() {
Expand Down

0 comments on commit f5a9858

Please sign in to comment.