noodles_util/alignment/async/io/reader.rs
1//! Async alignment reader.
2
3mod builder;
4mod inner;
5
6use futures::Stream;
7use noodles_sam as sam;
8use tokio::io::{self, AsyncRead};
9
10pub use self::builder::Builder;
11use self::inner::Inner;
12
13/// An async alignment reader.
14pub struct Reader<R>(Inner<R>)
15where
16 R: AsyncRead + Unpin;
17
18impl<R> Reader<R>
19where
20 R: AsyncRead + Unpin,
21{
22 /// Creates an async alignment reader.
23 ///
24 /// This attempts to autodetect the compression method and format of the input.
25 ///
26 /// # Examples
27 ///
28 /// ```
29 /// # #[tokio::main]
30 /// # async fn main() -> tokio::io::Result<()> {
31 /// use noodles_util::alignment;
32 /// use tokio::io;
33 /// let reader = alignment::r#async::io::Reader::new(io::empty()).await?;
34 /// # Ok(())
35 /// # }
36 /// ```
37 pub async fn new(reader: R) -> io::Result<Self> {
38 Builder::default().build_from_reader(reader).await
39 }
40
41 /// Reads the SAM header.
42 ///
43 /// # Examples
44 ///
45 /// ```
46 /// # #[tokio::main]
47 /// # async fn main() -> tokio::io::Result<()> {
48 /// use noodles_util::alignment;
49 /// use tokio::io;
50 /// let mut reader = alignment::r#async::io::Reader::new(io::empty()).await?;
51 /// let header = reader.read_header().await?;
52 /// # Ok(())
53 /// # }
54 /// ```
55 pub async fn read_header(&mut self) -> io::Result<sam::Header> {
56 self.0.read_header().await
57 }
58
59 /// Returns an iterator over records starting from the current stream position.
60 ///
61 /// # Examples
62 ///
63 /// ```
64 /// # #[tokio::main]
65 /// # async fn main() -> tokio::io::Result<()> {
66 /// use futures::TryStreamExt;
67 /// use noodles_util::alignment;
68 /// use tokio::io;
69 ///
70 /// let mut reader = alignment::r#async::io::Reader::new(io::empty()).await?;
71 /// let header = reader.read_header().await?;
72 ///
73 /// let mut records = reader.records(&header);
74 ///
75 /// while let Some(record) = records.try_next().await? {
76 /// // ...
77 /// }
78 /// # Ok(())
79 /// # }
80 /// ```
81 pub fn records<'r, 'h: 'r>(
82 &'r mut self,
83 header: &'h sam::Header,
84 ) -> impl Stream<Item = io::Result<Box<dyn sam::alignment::Record>>> + 'r {
85 self.0.records(header)
86 }
87}