I'm pretty new to Rust (and bio rust) and just learning so any guidance would be appreciated. I've read the small Read Example for how to read in a fasta sequence from stdin,
let mut records = fasta::Reader::new(io::stdin()).records();
but I can't figure out how to read in from a file. I've tried
let mut records = fasta::Reader::new(filename);
Where the filename is a slice and a string and I've found the from_file function trying that as well. While some of them appear to work, then when I try to parse through them with for or while loops, they always complain that they're of the wrong type. The from_file function seems to not make an iterator, but a Result reader, so I can't call the next() or collect() function on it,
let mut records = fasta::Reader::from_file(filename);
let mut nb_reads = 0;
let mut nb_bases = 0;
while let Some(Ok(record)) = records.next() {
nb_reads += 1;
nb_bases += record.seq().len();
let sa = suffix_array(record.seq());
println!("Here's the Suffix array: {:#?}", sa);
nb_reads += 1;
nb_bases += record.seq().len();
}
while the for loop seems to work, but the 'result' iterator doesn't have the right type so I can't pull sequences.
let mut reader = fasta::Reader::from_file(filename);
let mut nb_reads = 0;
let mut nb_bases = 0;
for result in reader {
nb_reads += 1;
nb_bases += result.seq().len();
let sa = suffix_array(result.seq());
println!("Here's the Suffix array: {:#?}", sa);
nb_reads += 1;
nb_bases += result.seq().len();
}
I'm stumped, but I feel like I'm close to getting it to work. Thanks!
from_file
returned an error before you can use it. Easiest is to justunwrap
it:let mut reader = fasta::Reader::from_file(filename).unwrap();
– Jmb&
but it doesn't worklet reader = fasta::Reader::from_file(filename).unwrap(); for result in reader.records() { nb_reads += 1; nb_bases += result.unwrap().seq().len(); let sa = suffix_array(result.unwrap().seq());
Gives error here. When I don't unwrap I can't call the.seq()
function on them. – Ryguy