-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #10
- Loading branch information
Showing
7 changed files
with
188 additions
and
55 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,36 @@ | ||
use nom::{ | ||
bytes::complete::{tag, take_while}, | ||
multi::many0, | ||
IResult, | ||
}; | ||
|
||
use crate::dna::Dna; | ||
|
||
pub fn parse_records(input: &str) -> IResult<&str, Vec<FastaDna>> { | ||
many0(parse_record)(input) | ||
} | ||
|
||
fn parse_record(input: &str) -> IResult<&str, FastaDna> { | ||
let (input, id) = parse_id(input)?; | ||
let (input, sequence) = parse_sequence(input)?; | ||
|
||
Ok((input, FastaDna { id, sequence })) | ||
} | ||
|
||
fn parse_id(input: &str) -> IResult<&str, String> { | ||
let (input, _) = tag(">")(input)?; | ||
let (input, id) = take_while(|c| c != '\n')(input)?; | ||
|
||
Ok((&input[1..], id.to_string())) | ||
} | ||
|
||
fn parse_sequence(input: &str) -> IResult<&str, Dna> { | ||
let (input, sequence) = take_while(|c| c != '>')(input)?; | ||
Ok((input, Dna::from_fasta_body(sequence))) | ||
} | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub struct FastaDna { | ||
pub id: String, | ||
pub sequence: Dna, | ||
} |
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 +1,2 @@ | ||
pub mod dna; | ||
pub mod fasta; |
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,51 @@ | ||
use nuc::dna::Dna; | ||
|
||
#[test] | ||
fn can_be_created_from_a_string() { | ||
let dna = Dna::try_from("ATGCCGTA").unwrap(); | ||
|
||
assert_eq!(dna.get(0), 0); | ||
assert_eq!(dna.get(1), 3); | ||
assert_eq!(dna.get(2), 2); | ||
assert_eq!(dna.get(3), 1); | ||
assert_eq!(dna.get(4), 1); | ||
assert_eq!(dna.get(5), 2); | ||
assert_eq!(dna.get(6), 3); | ||
assert_eq!(dna.get(7), 0); | ||
assert_eq!(dna.len(), 8); | ||
} | ||
|
||
#[test] | ||
fn can_be_sorted() { | ||
let mut sequences = vec![ | ||
Dna::from_ascii("ATGCCGTA"), | ||
Dna::from_ascii("CTAACGAA"), | ||
Dna::from_ascii("ATAC"), | ||
Dna::from_ascii("ATAA"), | ||
Dna::from_ascii("GTAGGG"), | ||
]; | ||
sequences.sort(); | ||
|
||
assert_eq!( | ||
sequences, | ||
vec![ | ||
Dna::from_ascii("ATAA"), | ||
Dna::from_ascii("ATAC"), | ||
Dna::from_ascii("ATGCCGTA"), | ||
Dna::from_ascii("CTAACGAA"), | ||
Dna::from_ascii("GTAGGG") | ||
] | ||
); | ||
} | ||
|
||
#[test] | ||
fn can_be_appended() { | ||
let mut dna = Dna::from_ascii("ATGCCGTA"); | ||
dna.append(&Dna::from_ascii("AAA")); | ||
dna.append(&Dna::from_ascii("CCC")); | ||
dna.append(&Dna::from_ascii("GGG")); | ||
dna.append(&Dna::from_ascii("TTT")); | ||
dna.append(&Dna::from_ascii("")); | ||
|
||
assert_eq!(dna.to_string(), "ATGCCGTAAAACCCGGGTTT"); | ||
} |
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,40 @@ | ||
|
||
use nuc::{ | ||
dna::Dna, | ||
fasta::{self, FastaDna}, | ||
}; | ||
|
||
#[test] | ||
fn can_read_an_example_fasta_file() { | ||
let fasta_content = ">Some Identifier\n\ | ||
ATGCCGTA\n\ | ||
>Another Identifier\n\ | ||
CTAACGAA\n\ | ||
>Yet Another Identifier\n\ | ||
ATAC\n\ | ||
ATAAGTAGGG"; | ||
let records = fasta::parse_records(fasta_content).unwrap().1; | ||
assert_eq!( | ||
records, | ||
vec![ | ||
FastaDna { | ||
id: "Some Identifier".to_string(), | ||
sequence: Dna::from_ascii("ATGCCGTA") | ||
}, | ||
FastaDna { | ||
id: "Another Identifier".to_string(), | ||
sequence: Dna::from_ascii("CTAACGAA") | ||
}, | ||
FastaDna { | ||
id: "Yet Another Identifier".to_string(), | ||
sequence: Dna::from_ascii("ATACATAAGTAGGG") | ||
}, | ||
] | ||
); | ||
} | ||
|
||
#[test] | ||
fn can_read_an_empty_fasta_file() { | ||
let records = fasta::parse_records("").unwrap().1; | ||
assert_eq!(records, vec![]); | ||
} |