-
|
Hello! I'm trying to create Record objects for unit testing on some code I'm trying to develop, but I've been unable to do so. I'm using Noodles to read BAM files and compute some simple statistics of them. In order to check that I'm counting what I need to count, the unit test would ideally get a particular Record and run it thru the stats collectors to check their behavior. So far, when trying to generate them on the fly, I hit some required private function in Noodles that stopped my efforts. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
You can build a Here's an example that counts the number of unmapped records in a BAM using a function that's generic over an alignment record. // cargo add noodles@0.101.0 --features bam,sam
use std::{env, fs::File, io};
use noodles::{bam, sam};
fn main() -> io::Result<()> {
let src = env::args().nth(1).expect("missing src");
let mut reader = File::open(src).map(bam::io::Reader::new)?;
let _header = reader.read_header()?;
let mut record = bam::Record::default();
let mut n = 0;
while reader.read_record(&mut record)? != 0 {
if record_is_unmapped(&record)? {
n += 1;
}
}
println!("{n}");
Ok(())
}
fn record_is_unmapped<R>(record: &R) -> io::Result<bool>
where
R: sam::alignment::Record,
{
record.flags().map(|flags| flags.is_unmapped())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_record_is_unmapped() -> io::Result<()> {
use noodles::sam::alignment::record::Flags;
let record = sam::alignment::RecordBuf::builder()
.set_flags(Flags::UNMAPPED)
.build();
assert!(record_is_unmapped(&record)?);
let record = sam::alignment::RecordBuf::builder()
.set_flags(Flags::empty())
.build();
assert!(!record_is_unmapped(&record)?);
Ok(())
}
} |
Beta Was this translation helpful? Give feedback.
You can build a
sam::alignment::RecordBufusing its associatedBuilder. Alignment records implementsam::alignment::Record, which you would want to use between your implementation and tests.Here's an example that counts the number of unmapped records in a BAM using a function that's generic over an alignment record.