-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
164 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
use clap::*; | ||
use intspan::*; | ||
use std::collections::BTreeMap; | ||
|
||
// Create clap subcommand arguments | ||
pub fn make_subcommand() -> Command { | ||
Command::new("concat") | ||
.about("Concatenate sequence pieces of same species") | ||
.after_help( | ||
r###" | ||
* <infile> is path to block fasta file, .fas.gz is supported | ||
* infile == stdin means reading from STDIN | ||
* <name.lst> is a file with a list of names to keep, one per line | ||
* Orders in the output file will following the ones in <name.lst> | ||
"###, | ||
) | ||
.arg( | ||
Arg::new("infile") | ||
.required(true) | ||
.num_args(1) | ||
.index(1) | ||
.help("Sets the input file to use"), | ||
) | ||
.arg( | ||
Arg::new("name.lst") | ||
.required(true) | ||
.num_args(1) | ||
.index(2) | ||
.help("Path to name.lst"), | ||
) | ||
.arg( | ||
Arg::new("phylip") | ||
.long("phylip") | ||
.action(ArgAction::SetTrue) | ||
.help("Output relaxed phylip instead of fasta"), | ||
) | ||
.arg( | ||
Arg::new("outfile") | ||
.long("outfile") | ||
.short('o') | ||
.num_args(1) | ||
.default_value("stdout") | ||
.help("Output filename. [stdout] for screen"), | ||
) | ||
} | ||
|
||
// command implementation | ||
pub fn execute(args: &ArgMatches) -> anyhow::Result<()> { | ||
//---------------------------- | ||
// Loading | ||
//---------------------------- | ||
let mut writer = writer(args.get_one::<String>("outfile").unwrap()); | ||
let mut reader = reader(args.get_one::<String>("infile").unwrap()); | ||
let is_phylip = args.get_flag("phylip"); | ||
|
||
//---------------------------- | ||
// Load names | ||
//---------------------------- | ||
let names = read_first_column(args.get_one::<String>("name.lst").unwrap()); | ||
|
||
let mut seq_of: BTreeMap<String, String> = BTreeMap::new(); | ||
for name in &names { | ||
// default value | ||
seq_of.insert(name.to_string(), "".to_string()); | ||
} | ||
|
||
while let Ok(block) = next_fas_block(&mut reader) { | ||
let block_names = block.names; | ||
let length = block.entries.first().unwrap().seq().len(); | ||
|
||
for name in &names { | ||
if block_names.contains(name) { | ||
for entry in &block.entries { | ||
let entry_name = entry.range().name(); | ||
if entry_name == name { | ||
let seq = std::str::from_utf8(entry.seq()).unwrap(); | ||
seq_of.entry(name.to_string()).and_modify(|e| *e += seq); | ||
} | ||
} | ||
} else { | ||
// fill absent names with ------ | ||
seq_of | ||
.entry(name.to_string()) | ||
.and_modify(|e| *e += "-".repeat(length).as_str()); | ||
} | ||
} | ||
} | ||
|
||
//---------------------------- | ||
// Output | ||
//---------------------------- | ||
if is_phylip { | ||
let count = names.len(); | ||
let length = seq_of.first_key_value().unwrap().1.len(); | ||
writer.write_all(format!("{} {}\n", count, length).as_ref())?; | ||
for (k, v) in &seq_of { | ||
writer.write_all(format!("{} {}\n", k, v).as_ref())?; | ||
} | ||
} else { | ||
for (k, v) in &seq_of { | ||
writer.write_all(format!(">{}\n{}\n", k, v).as_ref())?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
//! Subcommand modules for the `fasr` binary. | ||
|
||
pub mod concat; | ||
pub mod maf2fas; | ||
pub mod name; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Spar | ||
YJM789 |