-
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.
- Loading branch information
Showing
9 changed files
with
105 additions
and
8 deletions.
There are no files selected for viewing
File renamed without changes.
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,65 @@ | ||
use std::io::{BufRead, BufReader, Read}; | ||
|
||
#[derive(Debug)] | ||
pub struct FastqReader<R: Read> { | ||
reader: BufReader<R>, | ||
} | ||
|
||
impl<R: Read + std::fmt::Debug> FastqReader<R> { | ||
pub fn new(reader: R) -> Self { | ||
Self { | ||
reader: BufReader::new(reader), | ||
} | ||
} | ||
} | ||
|
||
impl<R: Read + std::fmt::Debug> Iterator for FastqReader<R> { | ||
type Item = FastqRecord; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
let mut id = String::new(); | ||
let mut sequence = String::new(); | ||
let mut qualities = String::new(); | ||
|
||
for _ in 0..4 { | ||
let mut line = String::new(); | ||
self.reader.read_line(&mut line).ok()?; | ||
match line.chars().next()? { | ||
'@' => id = line[1..].trim().to_string(), | ||
'+' => (), | ||
_ => { | ||
if sequence.is_empty() { | ||
sequence.push_str(&line.trim()); | ||
} else { | ||
qualities.push_str(&line.trim()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
Some(FastqRecord::new( | ||
id, | ||
Dna::from_ascii(&sequence), | ||
qualities.into_bytes(), | ||
)) | ||
} | ||
} | ||
|
||
use crate::dna::Dna; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub struct FastqRecord { | ||
pub id: String, | ||
pub sequence: Dna, | ||
pub qualities: Vec<u8>, | ||
} | ||
|
||
impl FastqRecord { | ||
pub fn new(id: String, sequence: Dna, qualities: Vec<u8>) -> Self { | ||
Self { | ||
id, | ||
sequence, | ||
qualities, | ||
} | ||
} | ||
} |
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 @@ | ||
pub mod fasta; | ||
pub mod fastq; |
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 |
---|---|---|
|
@@ -43,8 +43,6 @@ fn can_be_sorted() { | |
]; | ||
sequences.sort(); | ||
|
||
println!("{:?}", sequences); | ||
|
||
assert_eq!( | ||
sequences, | ||
vec![ | ||
|
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,30 @@ | ||
use nuc::{ | ||
dna::Dna, | ||
io::fastq::{FastqReader, FastqRecord}, | ||
}; | ||
|
||
#[test] | ||
fn can_read_an_example_fasta_file() { | ||
let reader = FastqReader::new( | ||
r#"@SEQ_ID | ||
GATTTGGGGTTCAAAGCAGTATCGATCAAATAGTAAATCCATTTGTTCAACTCACAGTTT | ||
+ | ||
!''*((((***+))%%%++)(%%%%).1***-+*''))**55CCF>>>>>>CCCCCCC65"# | ||
.as_bytes(), | ||
); | ||
|
||
assert_eq!( | ||
reader.into_iter().collect::<Vec<_>>(), | ||
vec![FastqRecord { | ||
id: "SEQ_ID".to_string(), | ||
sequence: Dna::from_ascii( | ||
"GATTTGGGGTTCAAAGCAGTATCGATCAAATAGTAAATCCATTTGTTCAACTCACAGTTT" | ||
), | ||
qualities: vec![ | ||
33, 39, 39, 42, 40, 40, 40, 40, 42, 42, 42, 43, 41, 41, 37, 37, 37, 43, 43, 41, 40, | ||
37, 37, 37, 37, 41, 46, 49, 42, 42, 42, 45, 43, 42, 39, 39, 41, 41, 42, 42, 53, 53, | ||
67, 67, 70, 62, 62, 62, 62, 62, 62, 67, 67, 67, 67, 67, 67, 67, 54, 53 | ||
] | ||
},] | ||
); | ||
} |
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 @@ | ||
pub mod fasta_test; | ||
pub mod fastq_test; |
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,3 @@ | ||
pub mod hash_test; | ||
pub mod dna_test; | ||
pub mod io; |