Skip to content

Commit

Permalink
bam/async/io/reader: Read magic number with header
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Dec 16, 2024
1 parent 8da1688 commit c7425ba
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 55 deletions.
52 changes: 2 additions & 50 deletions noodles-bam/src/async/io/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ use noodles_csi::BinningIndex;
use noodles_sam::{self as sam, alignment::RecordBuf};
use tokio::io::{self, AsyncRead, AsyncSeek};

use self::{
header::{magic_number::read_magic_number, read_header},
query::query,
record::read_record,
record_buf::read_record_buf,
};
use crate::{io::reader::resolve_region, Record, MAGIC_NUMBER};
use self::{header::read_header, query::query, record::read_record, record_buf::read_record_buf};
use crate::{io::reader::resolve_region, Record};

/// An async BAM reader.
///
Expand Down Expand Up @@ -120,7 +115,6 @@ where
/// # }
/// ```
pub async fn read_header(&mut self) -> io::Result<sam::Header> {
read_magic(&mut self.inner).await?;
read_header(&mut self.inner).await
}

Expand Down Expand Up @@ -427,45 +421,3 @@ impl<R> From<R> for Reader<R> {
}
}
}

async fn read_magic<R>(reader: &mut R) -> io::Result<()>
where
R: AsyncRead + Unpin,
{
let magic = read_magic_number(reader).await?;

if magic == MAGIC_NUMBER {
Ok(())
} else {
Err(io::Error::new(
io::ErrorKind::InvalidData,
"invalid BAM header",
))
}
}

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

#[tokio::test]
async fn test_read_magic() {
let data = b"BAM\x01";
let mut reader = &data[..];
assert!(read_magic(&mut reader).await.is_ok());

let data = [];
let mut reader = &data[..];
assert!(matches!(
read_magic(&mut reader).await,
Err(ref e) if e.kind() == io::ErrorKind::UnexpectedEof
));

let data = b"MThd";
let mut reader = &data[..];
assert!(matches!(
read_magic(&mut reader).await,
Err(ref e) if e.kind() == io::ErrorKind::InvalidData
));
}
}
12 changes: 10 additions & 2 deletions noodles-bam/src/async/io/reader/header.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
pub(super) mod magic_number;
mod magic_number;
mod reference_sequences;

use noodles_sam as sam;
use tokio::io::{self, AsyncBufRead, AsyncBufReadExt, AsyncRead, AsyncReadExt, BufReader};

use self::reference_sequences::read_reference_sequences;
use self::{magic_number::read_magic_number, reference_sequences::read_reference_sequences};
use crate::io::reader::header::reference_sequences_eq;

pub(super) async fn read_header<R>(reader: &mut R) -> io::Result<sam::Header>
where
R: AsyncRead + Unpin,
{
use crate::io::reader::header::magic_number;

read_magic_number(reader)
.await
.and_then(magic_number::validate)?;

let mut header = read_header_inner(reader).await?;
let reference_sequences = read_reference_sequences(reader).await?;

Expand Down Expand Up @@ -106,6 +112,7 @@ mod tests {
};

use super::*;
use crate::MAGIC_NUMBER;

const SQ0_LN: NonZeroUsize = match NonZeroUsize::new(8) {
Some(length) => length,
Expand All @@ -119,6 +126,7 @@ mod tests {
#[tokio::test]
async fn test_read_header() -> io::Result<()> {
let mut src = Vec::new();
src.extend(MAGIC_NUMBER);
put_u32_le(&mut src, 27); // l_text
src.extend(b"@HD\tVN:1.6\n@SQ\tSN:sq0\tLN:8\n"); // text
put_u32_le(&mut src, 1); // n_ref
Expand Down
2 changes: 1 addition & 1 deletion noodles-bam/src/async/io/reader/header/magic_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use tokio::io::{self, AsyncRead, AsyncReadExt};

use crate::MAGIC_NUMBER;

pub(crate) async fn read_magic_number<R>(reader: &mut R) -> io::Result<[u8; MAGIC_NUMBER.len()]>
pub(super) async fn read_magic_number<R>(reader: &mut R) -> io::Result<[u8; MAGIC_NUMBER.len()]>
where
R: AsyncRead + Unpin,
{
Expand Down
2 changes: 1 addition & 1 deletion noodles-bam/src/io/reader/header.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod magic_number;
pub(crate) mod magic_number;
mod reference_sequences;
mod sam_header;

Expand Down
2 changes: 1 addition & 1 deletion noodles-bam/src/io/reader/header/magic_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ where
Ok(buf)
}

pub(super) fn validate(buf: Buf) -> io::Result<()> {
pub(crate) fn validate(buf: Buf) -> io::Result<()> {
if is_valid(buf) {
Ok(())
} else {
Expand Down

0 comments on commit c7425ba

Please sign in to comment.