Skip to content

Commit

Permalink
vcf/io/writer/record: Add reference bases writer
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Mar 12, 2024
1 parent c53639d commit 63aa580
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
10 changes: 4 additions & 6 deletions noodles-vcf/src/io/writer/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod ids;
mod info;
mod position;
mod quality_score;
mod reference_bases;
mod reference_sequence_name;
mod samples;

Expand All @@ -12,7 +13,8 @@ use std::io::{self, Write};
use self::{
alternate_bases::write_alternate_bases, filters::write_filters, ids::write_ids,
info::write_info, position::write_position, quality_score::write_quality_score,
reference_sequence_name::write_reference_sequence_name, samples::write_samples,
reference_bases::write_reference_bases, reference_sequence_name::write_reference_sequence_name,
samples::write_samples,
};
use crate::{variant::Record, Header};

Expand All @@ -36,11 +38,7 @@ where
write_ids(writer, record.ids())?;

writer.write_all(DELIMITER)?;

for result in record.reference_bases().iter() {
let base = result?;
writer.write_all(&[base])?;
}
write_reference_bases(writer, record.reference_bases())?;

writer.write_all(DELIMITER)?;
write_alternate_bases(writer, record.alternate_bases())?;
Expand Down
36 changes: 36 additions & 0 deletions noodles-vcf/src/io/writer/record/reference_bases.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::io::{self, Write};

use crate::variant::record::ReferenceBases;

pub(super) fn write_reference_bases<W, B>(writer: &mut W, reference_bases: B) -> io::Result<()>
where
W: Write,
B: ReferenceBases,
{
for result in reference_bases.iter() {
let base = result?;
writer.write_all(&[base])?;
}

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_write_reference_bases() -> io::Result<()> {
let mut buf = Vec::new();

buf.clear();
write_reference_bases(&mut buf, "A")?;
assert_eq!(buf, b"A");

buf.clear();
write_reference_bases(&mut buf, "AC")?;
assert_eq!(buf, b"AC");

Ok(())
}
}

0 comments on commit 63aa580

Please sign in to comment.