Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:hhandika/segul into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
hhandika committed Aug 13, 2024
2 parents 195e4ce + 0fa67bd commit bb9ef31
Show file tree
Hide file tree
Showing 17 changed files with 78 additions and 38 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ Learn more about using SEGUL API in the [documentation](https://www.segul.app/do

| Feature | Quick Link |
| ------------------------------ | ---------------------------------------------------------------------------------------------------------------------- |
| Alignment concatenation | [CLI](https://www.segul.app/docs/cli-usage/concat) / [GUI](https://www.segul.app/docs/gui-usage/align-concat) |
| Alignment concatenation | [CLI](https://www.segul.app/docs/cli-usage/alignments) / [GUI](https://www.segul.app/docs/gui-usage/align-concat) |
| Alignment conversion | [CLI](https://www.segul.app/docs/cli-usage/convert) / [GUI](https://www.segul.app/docs/gui-usage/align-convert) |
| Alignment filtering | [CLI](https://www.segul.app/docs/cli-usage/filter) / [GUI](https://www.segul.app/docs/gui-usage/align-filter) |
| Alignment splitting | [CLI](https://www.segul.app/docs/cli-usage/split) / [GUI](https://www.segul.app/docs/gui-usage/align-split) |
| Alignment partition conversion | [CLI](https://www.segul.app/docs/cli-usage/part) / [GUI](https://www.segul.app/docs/gui-usage/align-partition) |
| Alignment summary statistics | [CLI](https://www.segul.app/docs/cli-usage/summary) / [GUI](https://www.segul.app/docs/gui-usage/align-summary) |
| Genomic summary statistics | [CLI](https://www.segul.app/docs/cli-usage/genomic) / [GUI](https://www.segul.app/docs/gui-usage/genomic) |
| Sequence extraction | [CLI](https://www.segul.app/docs/cli-usage/extract) / [GUI](https://www.segul.app/docs/gui-usage/sequence-extract) |
| Sequence filtering | [CLI](https://www.segul.app/docs/cli-usage/seq_filter) / GUI feature in development |
| Sequence filtering | [CLI](https://www.segul.app/docs/cli-usage/seq_filter) / GUI feature in development |
| Sequence ID extraction | [CLI](https://www.segul.app/docs/cli-usage/id) / [GUI](https://www.segul.app/docs/gui-usage/sequence-id) |
| Sequence ID mapping | [CLI](https://www.segul.app/docs/cli-usage/map) / [GUI](https://www.segul.app/docs/gui-usage/sequence-id-map) |
| Sequence ID renaming | [CLI](https://www.segul.app/docs/cli-usage/rename) / [GUI](https://www.segul.app/docs/gui-usage/sequence-rename) |
Expand Down
6 changes: 1 addition & 5 deletions src/core/align/unalign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl<'a> UnalignAlignment<'a> {
///
/// let input_fmt = InputFmt::Nexus;
/// let datatype = DataType::Dna;
/// let input_dir = Path::new("tests/files/concat");
/// let input_dir = Path::new("tests/files/alignments");
/// // Find matching alignment files in the input directory
/// let files = SeqFileFinder::new(Path::new(input_dir)).find(&input_fmt);
/// // Replace the temp directory with your own directory.
Expand All @@ -93,10 +93,6 @@ impl<'a> UnalignAlignment<'a> {
/// );
/// handle.unalign();
pub fn unalign(&self) {
if self.output_fmt != &OutputFmt::Fasta || self.output_fmt != &OutputFmt::FastaInt {
log::warn!("Unalign feature only supports fasta or fasta-int output format");
return;
}
let spin = utils::set_spinner();
spin.set_message("Converting un-aligned sequence files...");
self.input_files.par_iter().for_each(|file| {
Expand Down
4 changes: 2 additions & 2 deletions src/core/sequence/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl<'a> SequenceFiltering<'a> {
///
/// let input_fmt = InputFmt::Nexus;
/// let datatype = DataType::Dna;
/// let input_dir = Path::new("tests/files/concat");
/// let input_dir = Path::new("tests/files/alignments");
/// let files = SeqFileFinder::new(Path::new(input_dir)).find(&input_fmt);
/// // Replace the temp directory with your own directory.
/// let output = TempDir::new("tempt").unwrap();
Expand Down Expand Up @@ -224,7 +224,7 @@ mod tests {

#[test]
fn test_filter_sequences_by_length() {
let dir = Path::new("tests/files/concat");
let dir = Path::new("tests/files/alignments");
let params = SeqFilteringParameters::MinSequenceLength(7);
setup!(dir, handle, params, output);
handle.filter();
Expand Down
4 changes: 2 additions & 2 deletions src/core/sequence/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ impl<'a> SequenceID<'a> {
/// use segul::helper::types::{DataType, InputFmt};
/// use tempdir::TempDir;
///
/// let alignment_2 = PathBuf::from("tests/files/concat/gene_2.nex");
/// let alignment_1 = PathBuf::from("tests/files/concat/gene_1.nex");
/// let alignment_2 = PathBuf::from("tests/files/alignments/gene_2.nex");
/// let alignment_1 = PathBuf::from("tests/files/alignments/gene_1.nex");
/// let files = vec![alignment_1, alignment_2];
/// let output = TempDir::new("tempt").unwrap();
/// let handle = SequenceID::new(&files, &InputFmt::Auto, &DataType::Dna, Path::new(output.path()), None);
Expand Down
14 changes: 7 additions & 7 deletions src/helper/concat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ impl ConcatParams {
/// use segul::helper::types::{DataType, InputFmt};
///
/// let mut files = vec![
/// PathBuf::from("tests/files/concat/gene_1.nex"),
/// PathBuf::from("tests/files/concat/gene_2.nex"),
/// PathBuf::from("tests/files/alignments/gene_1.nex"),
/// PathBuf::from("tests/files/alignments/gene_2.nex"),
/// ];
/// let spinner = segul::helper::utils::set_spinner();
/// let input_fmt = InputFmt::Nexus;
Expand Down Expand Up @@ -188,7 +188,7 @@ mod test {

#[test]
fn test_concat_nexus() {
let path = Path::new("tests/files/concat/");
let path = Path::new("tests/files/alignments/");
let mut files = SeqFileFinder::new(path).find(&InputFmt::Nexus);
let mut concat = Concat::new(&mut files, &InputFmt::Nexus, &DNA);
let spin = utils::set_spinner();
Expand All @@ -199,15 +199,15 @@ mod test {
#[test]
#[should_panic]
fn test_get_alignment_panic() {
let path = Path::new("tests/files/concat/");
let path = Path::new("tests/files/alignments/");
let mut files = SeqFileFinder::new(path).find(&InputFmt::Nexus);
let concat = Concat::new(&mut files, &InputFmt::Nexus, &DNA);
concat.get_alignment(Path::new("."));
}

#[test]
fn test_concat_check_result() {
let path = Path::new("tests/files/concat/");
let path = Path::new("tests/files/alignments/");
let mut files = SeqFileFinder::new(path).find(&InputFmt::Nexus);
let mut concat = Concat::new(&mut files, &InputFmt::Nexus, &DNA);
let spin = utils::set_spinner();
Expand All @@ -219,7 +219,7 @@ mod test {

#[test]
fn test_concat_partition() {
let path = Path::new("tests/files/concat/");
let path = Path::new("tests/files/alignments/");
let mut files = SeqFileFinder::new(path).find(&InputFmt::Nexus);
let mut concat = Concat::new(&mut files, &InputFmt::Nexus, &DNA);
let spin = utils::set_spinner();
Expand All @@ -246,7 +246,7 @@ mod test {

#[test]
fn test_header_datatype() {
let path = Path::new("tests/files/concat/");
let path = Path::new("tests/files/alignments/");
let mut files = SeqFileFinder::new(path).find(&InputFmt::Nexus);
let mut concat = Concat::new(&mut files, &InputFmt::Nexus, &DataType::Aa);
concat.match_header_datatype();
Expand Down
52 changes: 45 additions & 7 deletions src/helper/finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl<'a> SeqFileFinder<'a> {
/// use segul::helper::types::InputFmt;
/// use segul::helper::finder::SeqFileFinder;
///
/// let dir = Path::new("tests/files/concat");
/// let dir = Path::new("tests/files/alignments");
/// let input_fmt = InputFmt::Nexus;
/// let files = SeqFileFinder::new(&dir).find(&input_fmt);
/// assert_eq!(files.len(), 4);
Expand All @@ -250,17 +250,39 @@ impl<'a> SeqFileFinder<'a> {
/// # Example
/// ```
/// use std::path::Path;
/// use segul::helper::types::InputFmt;
/// use segul::helper::finder::SeqFileFinder;
///
/// let dir = Path::new("tests/files/concat");
/// let dir = Path::new("tests/files/alignments");
/// let files = SeqFileFinder::new(&dir).find_recursive();
/// assert_eq!(files.len(), 4);
/// ```
pub fn find_recursive(&self) -> Vec<PathBuf> {
walk_dir!(self, re_match_sequence_lazy)
}

/// Find input files for sequence and alignment, recursively.
/// Limit search to only the input format.
///
/// # Example
/// ```
/// use std::path::Path;
/// use segul::helper::types::InputFmt;
/// use segul::helper::finder::SeqFileFinder;
///
/// let dir = Path::new("tests/files/alignments");
/// let files = SeqFileFinder::new(&dir).find_recursive_only(&InputFmt::Nexus);
/// assert_eq!(files.len(), 4);
pub fn find_recursive_only(&self, input_fmt: &'a InputFmt) -> Vec<PathBuf> {
let files = match input_fmt {
InputFmt::Fasta => walk_dir!(self, re_matches_fasta_lazy),
InputFmt::Nexus => walk_dir!(self, re_match_nexus_lazy),
InputFmt::Phylip => walk_dir!(self, re_match_phylip_lazy),
_ => unreachable!(),
};

files
}

fn check_results(&self, files: &[PathBuf]) {
if files.is_empty() {
panic!(
Expand Down Expand Up @@ -305,6 +327,22 @@ fn re_match_sequence_lazy(fname: &str) -> bool {
RE.is_match(fname)
}

fn re_match_nexus_lazy(fname: &str) -> bool {
lazy_static! {
static ref RE: Regex = Regex::new(r"(?i)(.nex*|.nxs)(?:.*)").unwrap();
}

RE.is_match(fname)
}

fn re_match_phylip_lazy(fname: &str) -> bool {
lazy_static! {
static ref RE: Regex = Regex::new(r"(?i)(.phy*|.fna|.fa*)(?:.*)").unwrap();
}

RE.is_match(fname)
}

/// Parse IDs from input sequence files.
/// # Example
/// ```
Expand All @@ -314,8 +352,8 @@ fn re_match_sequence_lazy(fname: &str) -> bool {
/// use indexmap::IndexSet;
///
/// let files = vec![
/// PathBuf::from("tests/files/concat/gene_1.nex"),
/// PathBuf::from("tests/files/concat/gene_2.nex"),
/// PathBuf::from("tests/files/alignments/gene_1.nex"),
/// PathBuf::from("tests/files/alignments/gene_2.nex"),
/// ];
///
/// let input_fmt = InputFmt::Nexus;
Expand Down Expand Up @@ -404,7 +442,7 @@ mod test {

macro_rules! input {
($files: ident) => {
let path = Path::new("tests/files/concat");
let path = Path::new("tests/files/alignments");

let mut $files = SeqFileFinder::new(path);
};
Expand Down Expand Up @@ -451,7 +489,7 @@ mod test {
input!(files);
let fmt = InputFmt::Nexus;
files.pattern(&fmt);
assert_eq!("tests/files/concat/*.nex*", files.pattern);
assert_eq!("tests/files/alignments/*.nex*", files.pattern);
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions tests/concat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use segul::helper::types::InputFmt;

#[test]
fn test_concat() {
initiate_cmd!(cmd, "align", "concat", "tests/files/concat/", tmp_dir);
initiate_cmd!(cmd, "align", "concat", "tests/files/alignments/", tmp_dir);
cmd.arg("--partition-format")
.arg("raxml")
.assert()
Expand All @@ -19,7 +19,7 @@ fn test_concat() {

#[test]
fn test_concat_nexus_part() {
initiate_cmd!(cmd, "align", "concat", "tests/files/concat/", tmp_dir);
initiate_cmd!(cmd, "align", "concat", "tests/files/alignments/", tmp_dir);
cmd.arg("--partition-format")
.arg("nexus")
.assert()
Expand Down
2 changes: 1 addition & 1 deletion tests/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use segul::helper::types::InputFmt;

#[test]
fn test_convert() {
initiate_cmd!(cmd, "align", "convert", "tests/files/concat/", tmp_dir);
initiate_cmd!(cmd, "align", "convert", "tests/files/alignments/", tmp_dir);
cmd.arg("-F").arg("phylip").assert().success();
test_results!(4, tmp_dir, "Align-Convert", Phylip);
}
10 changes: 5 additions & 5 deletions tests/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ use segul::helper::types::InputFmt;

#[test]
fn test_extract() {
initiate_cmd!(cmd, "sequence", "extract", "tests/files/concat/", tmp_dir);
initiate_cmd!(cmd, "sequence", "extract", "tests/files/alignments/", tmp_dir);
cmd.arg("--id=ABCD").assert().success();
test_results!(4, tmp_dir, "Sequence-Extract", Nexus);
}

#[test]
#[should_panic]
fn test_conflicting_extract_cmd() {
initiate_cmd!(cmd, "sequence", "extract", "tests/files/concat/", tmp_dir);
initiate_cmd!(cmd, "sequence", "extract", "tests/files/alignments/", tmp_dir);
cmd.arg("--id")
.arg("ABCD")
.arg("--re=^AB")
Expand All @@ -28,18 +28,18 @@ fn test_conflicting_extract_cmd() {
#[test]
#[should_panic]
fn test_no_extract_cmd() {
initiate_cmd!(cmd, "sequence", "extract", "tests/files/concat/", tmp_dir);
initiate_cmd!(cmd, "sequence", "extract", "tests/files/alignments/", tmp_dir);
cmd.arg("--id")
.arg("ABCD")
.arg("--file")
.arg("tests/files/concat/concat.nex")
.arg("tests/files/alignments/alignments.nex")
.assert()
.success();
}

#[test]
fn test_extract_re() {
initiate_cmd!(cmd, "sequence", "extract", "tests/files/concat/", tmp_dir);
initiate_cmd!(cmd, "sequence", "extract", "tests/files/alignments/", tmp_dir);
cmd.arg("--re=^AB").assert().success();
test_results!(4, tmp_dir, "Sequence-Extract", Nexus);
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use segul::parser::txt;

#[test]
fn test_id_success() {
initiate_cmd!(cmd, "sequence", "id", "tests/files/concat/", tmp_dir);
initiate_cmd!(cmd, "sequence", "id", "tests/files/alignments/", tmp_dir);
cmd.assert().success();
let pred = predicates::path::is_file();
let res_path = tmp_dir.path().join("SEGUL-ID").join("id.txt");
Expand Down
4 changes: 2 additions & 2 deletions tests/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ use segul::helper::types::InputFmt;

#[test]
fn test_remove_id() {
initiate_cmd!(cmd, "sequence", "remove", "tests/files/concat/", tmp_dir);
initiate_cmd!(cmd, "sequence", "remove", "tests/files/alignments/", tmp_dir);
cmd.arg("--id=ABCD").assert().success();
test_results!(3, tmp_dir, "Sequence-Remove", Nexus);
}

#[test]
fn test_remove_re() {
initiate_cmd!(cmd, "sequence", "remove", "tests/files/concat/", tmp_dir);
initiate_cmd!(cmd, "sequence", "remove", "tests/files/alignments/", tmp_dir);
cmd.arg("--re=E$").assert().success();
test_results!(4, tmp_dir, "Sequence-Remove", Nexus);
}
2 changes: 1 addition & 1 deletion tests/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use segul::helper::types::InputFmt;

#[test]
fn test_rename() {
initiate_cmd!(cmd, "sequence", "rename", "tests/files/concat/", tmp_dir);
initiate_cmd!(cmd, "sequence", "rename", "tests/files/alignments/", tmp_dir);
cmd.arg("--remove=D").assert().success();
test_results!(4, tmp_dir, "Sequence-Rename", Nexus);
}
8 changes: 7 additions & 1 deletion tests/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ use segul::helper::types::InputFmt;

#[test]
fn test_dna_translation() {
initiate_cmd!(cmd, "sequence", "translate", "tests/files/concat/", tmp_dir);
initiate_cmd!(
cmd,
"sequence",
"translate",
"tests/files/alignments/",
tmp_dir
);
cmd.assert().success();
test_results!(4, tmp_dir, "Sequence-Translate", Nexus);
}

0 comments on commit bb9ef31

Please sign in to comment.