Skip to content

Commit

Permalink
Address comments: move out of main.rs; fix CLI help text
Browse files Browse the repository at this point in the history
  • Loading branch information
RagnarGrootKoerkamp committed Nov 30, 2021
1 parent df58688 commit daab226
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 50 deletions.
30 changes: 15 additions & 15 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,31 +394,31 @@ pub(crate) enum Command {
/// Reconstruct a phylogenetic tree given a Phylip distance matrix input file.
#[structopt(author = "Ragnar Groot Koerkamp <ragnar.grootkoerkamp@gmail.com>")]
Phylogeny {
/// The reconstruction method to use. `UPGMA` and `NeighborJoining`.
#[structopt(long = "method")]
#[structopt(
long = "method",
help = "The reconstruction method to use. `UPGMA` or `NeighborJoining`"
)]
method: PhylogenyMethod,

/// Path of Phylip distance matrix file.
#[structopt(parse(from_os_str))]
#[structopt(parse(from_os_str), help = "Input Phylip distance matrix file.")]
input: PathBuf,

/// Path to store the phylogeny in Newick format, or stdout otherwise.
#[structopt(parse(from_os_str))]
#[structopt(
parse(from_os_str),
help = "Optional output Newick file. Stdout by default."
)]
output: Option<PathBuf>,
},

/// Compute the Robinson-Foulds distance between two phylogenetic trees.
#[structopt(author = "Ragnar Groot Koerkamp <ragnar.grootkoerkamp@gmail.com>")]
RobinsonFoulds {
/// The path of the first phylogeny in Newick format.
#[structopt(parse(from_os_str))]
#[structopt(parse(from_os_str), help = "First input Newick file.")]
newick_1: PathBuf,
/// The path of the second phylogeny in Newick format.
#[structopt(parse(from_os_str))]
#[structopt(parse(from_os_str), help = "Second input Newick file.")]
newick_2: PathBuf,

/// The path to write the distance to, or stdout otherwise.
#[structopt(parse(from_os_str))]
#[structopt(
parse(from_os_str),
help = "Optional file to write distance to. Stdout by default."
)]
output: Option<PathBuf>,
},
}
Expand Down
40 changes: 5 additions & 35 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod cli;
pub mod common;
pub mod csv;
pub mod fastq;
pub mod phylogeny;
pub mod sequences_stats;

fn main() -> Result<()> {
Expand Down Expand Up @@ -284,43 +285,12 @@ fn main() -> Result<()> {
method,
input,
output,
} => {
let dm = bio_types::distancematrix::DistanceMatrix::from_file(&input)?;
let phylogeny = (match method {
cli::PhylogenyMethod::UPGMA => bio::phylogeny::upgma,
cli::PhylogenyMethod::NeighborJoining => bio::phylogeny::neighbor_joining,
})(&dm);
match output {
Some(path) => {
let mut f = fs::File::create(path)?;
f.write_all(phylogeny.to_string().as_bytes())?;
f.write(b"\n")?;
}
None => {
println!("{}", phylogeny.to_string());
}
}
}
} => phylogeny::phylogeny(input, method, output)?,
RobinsonFoulds {
ref newick_1,
ref newick_2,
newick_1,
newick_2,
output,
} => {
let dist = bio::phylogeny::robinson_foulds_distance(
&bio::io::newick::from_file(newick_1)?,
&bio::io::newick::from_file(newick_2)?,
);
match output {
Some(path) => {
let mut f = fs::File::create(path)?;
f.write_all(dist.to_string().as_bytes())?;
f.write(b"\n")?;
}
None => {
println!("{}", dist.to_string());
}
}
}
} => phylogeny::robinson_foulds_distance(&newick_1, &newick_2, output)?,
}
Ok(())
}
56 changes: 56 additions & 0 deletions src/phylogeny.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//! Compute a phylogeny from a distance matrix, or the distance between two phylogenies.
//!
//! ## Usage:
//!
//! ```
//! $ rbt phylogeny --method {UPGMA,NeighborJoining} phylip.dist [newick.tree]
//! $ rbt robinsonfoulds newick1.tree newick2.tree [outfile]
//! ```
use anyhow::Error;
use std::fs::File;

use crate::cli::PhylogenyMethod;

pub fn phylogeny(
input: std::path::PathBuf,
method: PhylogenyMethod,
output: Option<std::path::PathBuf>,
) -> Result<(), Error> {
let dm = bio_types::distancematrix::DistanceMatrix::from_file(&input)?;
let phylogeny = (match method {
PhylogenyMethod::UPGMA => bio::phylogeny::upgma,
PhylogenyMethod::NeighborJoining => bio::phylogeny::neighbor_joining,
})(&dm);
Ok(match output {
Some(path) => {
let mut f = File::create(path)?;
f.write_all(phylogeny.to_string().as_bytes())?;
f.write(b"\n")?;
}
None => {
println!("{}", phylogeny.to_string());
}
})
}

pub fn robinson_foulds_distance(
newick_1: &std::path::PathBuf,
newick_2: &std::path::PathBuf,
output: Option<std::path::PathBuf>,
) -> Result<(), Error> {
let dist = bio::phylogeny::robinson_foulds_distance(
&bio::io::newick::from_file(newick_1)?,
&bio::io::newick::from_file(newick_2)?,
);
Ok(match output {
Some(path) => {
let mut f = File::create(path)?;
f.write_all(dist.to_string().as_bytes())?;
f.write(b"\n")?;
}
None => {
println!("{}", dist.to_string());
}
})
}

0 comments on commit daab226

Please sign in to comment.