Skip to content

Commit

Permalink
vcf/record: Add alternate bases wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Feb 13, 2024
1 parent 6455bf1 commit f77a543
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
7 changes: 5 additions & 2 deletions noodles-vcf/src/record.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Lazily-evaluated VCF record and fields.

mod alternate_bases;
pub(crate) mod fields;
mod filters;
mod ids;
Expand All @@ -11,7 +12,9 @@ use std::{fmt, io};
use noodles_core::Position;

use self::fields::Fields;
pub use self::{filters::Filters, ids::Ids, info::Info, samples::Samples};
pub use self::{
alternate_bases::AlternateBases, filters::Filters, ids::Ids, info::Info, samples::Samples,
};

/// An immutable, lazily-evaluated VCF record.
#[derive(Clone, Default, Eq, PartialEq)]
Expand Down Expand Up @@ -48,7 +51,7 @@ impl Record {
}

/// Returns the alternate bases.
pub fn alternate_bases(&self) -> &str {
pub fn alternate_bases(&self) -> AlternateBases<'_> {
self.0.alternate_bases()
}

Expand Down
43 changes: 43 additions & 0 deletions noodles-vcf/src/record/alternate_bases.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use std::fmt;

/// VCF record alternate bases.
pub struct AlternateBases<'a>(&'a str);

const DELIMITER: char = ',';

impl<'a> AlternateBases<'a> {
pub(super) fn new(src: &'a str) -> Self {
Self(src)
}

/// Returns whether there are any alternate bases.
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}

/// Returns the number of alternate bases.
pub fn len(&self) -> usize {
if self.is_empty() {
0
} else {
self.iter().count()
}
}

/// Returns an iterator over alternate bases.
pub fn iter(&self) -> impl Iterator<Item = &'a str> + '_ {
self.0.split(DELIMITER)
}
}

impl<'a> fmt::Debug for AlternateBases<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.iter()).finish()
}
}

impl<'a> AsRef<str> for AlternateBases<'a> {
fn as_ref(&self) -> &str {
self.0
}
}
11 changes: 8 additions & 3 deletions noodles-vcf/src/record/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::io;
use noodles_core::Position;

pub(crate) use self::bounds::Bounds;
use super::{Filters, Ids, Info, Samples};
use super::{AlternateBases, Filters, Ids, Info, Samples};

#[derive(Clone, Eq, PartialEq)]
pub(crate) struct Fields {
Expand Down Expand Up @@ -45,8 +45,13 @@ impl Fields {
&self.buf[self.bounds.reference_bases_range()]
}

pub(super) fn alternate_bases(&self) -> &str {
&self.buf[self.bounds.alternate_bases_range()]
pub(super) fn alternate_bases(&self) -> AlternateBases {
let src = match &self.buf[self.bounds.alternate_bases_range()] {
MISSING => "",
buf => buf,
};

AlternateBases::new(src)
}

pub(super) fn quality_score(&self) -> Option<&str> {
Expand Down

0 comments on commit f77a543

Please sign in to comment.