Skip to content

Commit

Permalink
vcf/io/writer/record: Write from Record trait
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Mar 4, 2024
1 parent 8118492 commit 7970ea7
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 8 deletions.
21 changes: 14 additions & 7 deletions noodles-vcf/src/io/writer/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,23 @@ use self::{
info::write_info, position::write_position, quality_score::write_quality_score,
reference_sequence_name::write_reference_sequence_name, samples::write_samples,
};
use crate::{variant::RecordBuf, Header};
use crate::{variant::Record, Header};

const MISSING: &[u8] = b".";

pub(super) fn write_record<W>(writer: &mut W, header: &Header, record: &RecordBuf) -> io::Result<()>
pub(super) fn write_record<W, R>(writer: &mut W, header: &Header, record: &R) -> io::Result<()>
where
W: Write,
R: Record + ?Sized,
{
const DELIMITER: &[u8] = b"\t";

write_reference_sequence_name(writer, record.reference_sequence_name())?;
let reference_sequence_name = record.reference_sequence_name(header)?;
write_reference_sequence_name(writer, reference_sequence_name)?;

writer.write_all(DELIMITER)?;
write_position(writer, record.position())?;
let position = record.position().transpose()?;
write_position(writer, position)?;

writer.write_all(DELIMITER)?;
write_ids(writer, record.ids())?;
Expand All @@ -39,17 +42,20 @@ where
write_alternate_bases(writer, record.alternate_bases())?;

writer.write_all(DELIMITER)?;
write_quality_score(writer, record.quality_score())?;
let quality_score = record.quality_score().transpose()?;
write_quality_score(writer, quality_score)?;

writer.write_all(DELIMITER)?;
write_filters(writer, header, record.filters())?;

writer.write_all(DELIMITER)?;
write_info(writer, header, record.info())?;

if !record.samples().is_empty() {
let samples = record.samples()?;

if !samples.is_empty() {
writer.write_all(DELIMITER)?;
write_samples(writer, header, record.samples())?;
write_samples(writer, header, samples)?;
}

writer.write_all(b"\n")?;
Expand All @@ -62,6 +68,7 @@ mod tests {
use noodles_core::Position;

use super::*;
use crate::variant::RecordBuf;

#[test]
fn test_write_record() -> io::Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion noodles-vcf/src/io/writer/record/alternate_bases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::io::{self, Write};
use super::MISSING;
use crate::variant::record::AlternateBases;

pub(super) fn write_alternate_bases<W, B>(writer: &mut W, alternate_bases: &B) -> io::Result<()>
pub(super) fn write_alternate_bases<W, B>(writer: &mut W, alternate_bases: B) -> io::Result<()>
where
W: Write,
B: AlternateBases,
Expand Down
14 changes: 14 additions & 0 deletions noodles-vcf/src/variant/record/alternate_bases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,17 @@ pub trait AlternateBases {
/// Returns an iterator over alternate bases.
fn iter(&self) -> Box<dyn Iterator<Item = io::Result<&str>> + '_>;
}

impl AlternateBases for Box<dyn AlternateBases + '_> {
fn is_empty(&self) -> bool {
(**self).is_empty()
}

fn len(&self) -> usize {
(**self).len()
}

fn iter(&self) -> Box<dyn Iterator<Item = io::Result<&str>> + '_> {
(**self).iter()
}
}
17 changes: 17 additions & 0 deletions noodles-vcf/src/variant/record/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,20 @@ pub trait Filters {
}
}
}

impl Filters for Box<dyn Filters + '_> {
fn is_empty(&self) -> bool {
(**self).is_empty()
}

fn len(&self) -> usize {
(**self).len()
}

fn iter<'a, 'h: 'a>(
&'a self,
header: &'h Header,
) -> Box<dyn Iterator<Item = io::Result<&'a str>> + 'a> {
(**self).iter(header)
}
}
14 changes: 14 additions & 0 deletions noodles-vcf/src/variant/record/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,17 @@ pub trait Ids {
/// Returns an iterator over IDs.
fn iter(&self) -> Box<dyn Iterator<Item = &str> + '_>;
}

impl Ids for Box<dyn Ids + '_> {
fn is_empty(&self) -> bool {
(**self).is_empty()
}

fn len(&self) -> usize {
(**self).len()
}

fn iter(&self) -> Box<dyn Iterator<Item = &str> + '_> {
(**self).iter()
}
}
17 changes: 17 additions & 0 deletions noodles-vcf/src/variant/record/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,20 @@ pub trait Info {
header: &'h Header,
) -> Box<dyn Iterator<Item = io::Result<(&'a str, Option<Value<'a>>)>> + 'a>;
}

impl Info for Box<dyn Info + '_> {
fn is_empty(&self) -> bool {
(**self).is_empty()
}

fn len(&self) -> usize {
(**self).len()
}

fn iter<'a, 'h: 'a>(
&'a self,
header: &'h Header,
) -> Box<dyn Iterator<Item = io::Result<(&'a str, Option<Value<'a>>)>> + 'a> {
(**self).iter(header)
}
}
25 changes: 25 additions & 0 deletions noodles-vcf/src/variant/record/samples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,28 @@ pub trait Samples {
/// Returns an iterator over samples.
fn iter(&self) -> Box<dyn Iterator<Item = Box<dyn Sample + '_>> + '_>;
}

impl Samples for Box<dyn Samples + '_> {
fn is_empty(&self) -> bool {
(**self).is_empty()
}

fn len(&self) -> usize {
(**self).len()
}

fn column_names<'a, 'h: 'a>(
&'a self,
header: &'h Header,
) -> Box<dyn Iterator<Item = io::Result<&'a str>> + 'a> {
(**self).column_names(header)
}

fn series(&self) -> Box<dyn Iterator<Item = io::Result<Box<dyn Series + '_>>> + '_> {
(**self).series()
}

fn iter(&self) -> Box<dyn Iterator<Item = Box<dyn Sample + '_>> + '_> {
(**self).iter()
}
}

0 comments on commit 7970ea7

Please sign in to comment.