Skip to content

Commit

Permalink
sam/alignment/record/sequence: Add checked split at
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Jul 22, 2024
1 parent dca4c90 commit 610dc9f
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 2 deletions.
15 changes: 15 additions & 0 deletions noodles-bam/src/record/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ impl<'a> sam::alignment::record::Sequence for Sequence<'a> {
self.get(i)
}

fn split_at_checked(
&self,
mid: usize,
) -> Option<(
Box<dyn sam::alignment::record::Sequence + '_>,
Box<dyn sam::alignment::record::Sequence + '_>,
)> {
self.split_at_checked(mid).map(|(left, right)| {
(
Box::new(left) as Box<dyn sam::alignment::record::Sequence + '_>,
Box::new(right) as Box<dyn sam::alignment::record::Sequence + '_>,
)
})
}

fn iter(&self) -> Box<dyn Iterator<Item = u8> + '_> {
Box::new(self.iter())
}
Expand Down
15 changes: 15 additions & 0 deletions noodles-bam/src/record/sequence/subsequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ impl<'a> sam::alignment::record::Sequence for Subsequence<'a> {
self.get(i)
}

fn split_at_checked(
&self,
mid: usize,
) -> Option<(
Box<dyn sam::alignment::record::Sequence + '_>,
Box<dyn sam::alignment::record::Sequence + '_>,
)> {
self.split_at_checked(mid).map(|(left, right)| {
(
Box::new(left) as Box<dyn sam::alignment::record::Sequence + '_>,
Box::new(right) as Box<dyn sam::alignment::record::Sequence + '_>,
)
})
}

fn iter(&self) -> Box<dyn Iterator<Item = u8> + '_> {
Box::new(self.iter())
}
Expand Down
2 changes: 1 addition & 1 deletion noodles-sam/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
### Added

* sam/alignment/record/sequence: Add get base by index (`Sequence::get`)
([#283]).
([#283]) and checked split at (`Sequence::split_at_checked`).

[#283]: https://github.com/zaeleus/noodles/issues/283

Expand Down
21 changes: 21 additions & 0 deletions noodles-sam/src/alignment/record/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ pub trait Sequence {
/// Returns the base at the given index.
fn get(&self, i: usize) -> Option<u8>;

/// Splits the subsequence into two subsequences at the given index.
fn split_at_checked(
&self,
mid: usize,
) -> Option<(Box<dyn Sequence + '_>, Box<dyn Sequence + '_>)>;

/// Returns an iterator over bases.
fn iter(&self) -> Box<dyn Iterator<Item = u8> + '_>;
}
Expand All @@ -35,6 +41,13 @@ impl Sequence for Box<dyn Sequence + '_> {
(**self).get(i)
}

fn split_at_checked(
&self,
mid: usize,
) -> Option<(Box<dyn Sequence + '_>, Box<dyn Sequence + '_>)> {
(**self).split_at_checked(mid)
}

fn iter(&self) -> Box<dyn Iterator<Item = u8> + '_> {
(**self).iter()
}
Expand All @@ -61,6 +74,14 @@ mod tests {
self.0.get(i).copied()
}

fn split_at_checked(
&self,
mid: usize,
) -> Option<(Box<dyn Sequence + '_>, Box<dyn Sequence + '_>)> {
let (left, right) = self.0.split_at(mid);
Some((Box::new(T(left.to_vec())), Box::new(T(right.to_vec()))))
}

fn iter(&self) -> Box<dyn Iterator<Item = u8> + '_> {
Box::new(self.0.iter().copied())
}
Expand Down
18 changes: 18 additions & 0 deletions noodles-sam/src/alignment/record_buf/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,24 @@ impl crate::alignment::record::Sequence for &Sequence {
self.0.get(i).copied()
}

fn split_at_checked(
&self,
mid: usize,
) -> Option<(
Box<dyn crate::alignment::record::Sequence + '_>,
Box<dyn crate::alignment::record::Sequence + '_>,
)> {
if mid > self.len() {
let (left, right) = self.0.split_at(mid);
Some((
Box::new(crate::record::Sequence::new(left)),
Box::new(crate::record::Sequence::new(right)),
))
} else {
None
}
}

fn iter(&self) -> Box<dyn Iterator<Item = u8> + '_> {
Box::new(self.0.iter().copied())
}
Expand Down
20 changes: 19 additions & 1 deletion noodles-sam/src/record/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pub struct Sequence<'a>(&'a [u8]);

impl<'a> Sequence<'a> {
pub(super) fn new(buf: &'a [u8]) -> Self {
pub(crate) fn new(buf: &'a [u8]) -> Self {
Self(buf)
}

Expand Down Expand Up @@ -42,6 +42,24 @@ impl<'a> crate::alignment::record::Sequence for Sequence<'a> {
self.get(i)
}

fn split_at_checked(
&self,
mid: usize,
) -> Option<(
Box<dyn crate::alignment::record::Sequence + '_>,
Box<dyn crate::alignment::record::Sequence + '_>,
)> {
if mid <= self.len() {
let (left, right) = self.0.split_at(mid);
Some((
Box::new(Sequence::new(left)),
Box::new(Sequence::new(right)),
))
} else {
None
}
}

fn iter(&self) -> Box<dyn Iterator<Item = u8> + '_> {
Box::new(self.as_ref().iter().copied())
}
Expand Down

0 comments on commit 610dc9f

Please sign in to comment.