Skip to content

Commit

Permalink
csi: Move convenience functions to fs module
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Jan 15, 2025
1 parent fbec27f commit 24a6fd7
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 113 deletions.
2 changes: 1 addition & 1 deletion noodles-bam/src/io/indexed_reader/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ where
match bai::fs::read(build_index_src(src, "bai")) {
Ok(index) => Ok(Box::new(index)),
Err(e) if e.kind() == io::ErrorKind::NotFound => {
let index = csi::read(build_index_src(src, "csi"))?;
let index = csi::fs::read(build_index_src(src, "csi"))?;
Ok(Box::new(index))
}
Err(e) => Err(e),
Expand Down
2 changes: 1 addition & 1 deletion noodles-bcf/src/io/indexed_reader/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn read_associated_index<P>(src: P) -> io::Result<Box<dyn BinningIndex>>
where
P: AsRef<Path>,
{
let index = csi::read(build_index_src(src))?;
let index = csi::fs::read(build_index_src(src))?;
Ok(Box::new(index))
}

Expand Down
2 changes: 1 addition & 1 deletion noodles-bcf/src/io/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ where
/// let mut reader = File::open("sample.bcf").map(bcf::io::Reader::new)?;
/// let header = reader.read_header()?;
///
/// let index = csi::read("sample.bcf.csi")?;
/// let index = csi::fs::read("sample.bcf.csi")?;
/// let region = "sq0:8-13".parse()?;
/// let query = reader.query(&header, &index, &region)?;
///
Expand Down
8 changes: 8 additions & 0 deletions noodles-csi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

* csi: Raise minimum supported Rust version (MSRV) to 1.73.0.

* csi: Move convenience functions (`read` and `write`) to `fs` module.

### Deprecated

* csi: Deprecate `read` and `write`.

Use `csi::fs::read` and `csi::fs::write`, respectively, instead.

## 0.41.0 - 2024-12-12

### Changed
Expand Down
2 changes: 1 addition & 1 deletion noodles-csi/examples/csi_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn main() -> io::Result<()> {
let src = env::args().nth(1).expect("missing src");

let csi_src = format!("{src}.csi");
let index = csi::read(csi_src)?;
let index = csi::fs::read(csi_src)?;

let mut n = 0;

Expand Down
2 changes: 1 addition & 1 deletion noodles-csi/examples/csi_count_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async fn main() -> io::Result<()> {
let src = env::args().nth(1).expect("missing src");

let csi_src = format!("{src}.csi");
let index = csi::r#async::read(csi_src).await?;
let index = csi::r#async::fs::read(csi_src).await?;

let mut n = 0;

Expand Down
62 changes: 5 additions & 57 deletions noodles-csi/src/async.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Async CSI.
pub mod fs;
pub mod io;

use std::path::Path;
#[deprecated(since = "0.42.0", note = "Use `csi::r#async::fs::read` instead.")]
pub use self::fs::read;

use tokio::fs::File;

use super::Index;
#[deprecated(since = "0.42.0", note = "Use `csi::r#async::fs::write` instead.")]
pub use self::fs::write;

#[deprecated(
since = "0.39.0",
Expand All @@ -19,56 +20,3 @@ pub use self::io::Reader;
note = "Use `noodles_csi::r#async::io::Writer` instead."
)]
pub use self::io::Writer;

/// Reads the entire contents of a coordinate-sorted index (CSI).
///
/// This is a convenience function and is equivalent to opening the file at the given path and
/// reading the index.
///
/// # Examples
///
/// ```no_run
/// # use std::io;
/// #
/// # #[tokio::main]
/// # async fn main() -> io::Result<()> {
/// use noodles_csi as csi;
/// let index = csi::r#async::read("sample.bcf.csi").await?;
/// # Ok(())
/// # }
/// ```
pub async fn read<P>(src: P) -> tokio::io::Result<Index>
where
P: AsRef<Path>,
{
let mut reader = File::open(src).await.map(Reader::new)?;
reader.read_index().await
}

/// Writes a coordinate-sorted index (CSI) to a file.
///
/// This is a convenience function and is equivalent to creating a file at the given path and
/// writing the index.
///
/// # Examples
///
/// ```no_run
/// # use std::io;
/// #
/// # #[tokio::main]
/// # async fn main() -> io::Result<()> {
/// use noodles_csi as csi;
/// let index = csi::Index::default();
/// csi::r#async::write("sample.bcf.csi", &index).await?;
/// # Ok(())
/// # }
/// ```
pub async fn write<P>(dst: P, index: &Index) -> tokio::io::Result<()>
where
P: AsRef<Path>,
{
let mut writer = File::create(dst).await.map(Writer::new)?;
writer.write_index(index).await?;
writer.shutdown().await?;
Ok(())
}
57 changes: 57 additions & 0 deletions noodles-csi/src/async/fs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//! Async CSI filesystem operations.
use std::path::Path;

use tokio::{fs::File, io};

use super::io::{Reader, Writer};
use crate::Index;

/// Reads the entire contents of a coordinate-sorted index (CSI).
///
/// This is a convenience function and is equivalent to opening the file at the given path and
/// reading the index.
///
/// # Examples
///
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> tokio::io::Result<()> {
/// use noodles_csi as csi;
/// let index = csi::r#async::fs::read("sample.bcf.csi").await?;
/// # Ok(())
/// # }
/// ```
pub async fn read<P>(src: P) -> io::Result<Index>
where
P: AsRef<Path>,
{
let mut reader = File::open(src).await.map(Reader::new)?;
reader.read_index().await
}

/// Writes a coordinate-sorted index (CSI) to a file.
///
/// This is a convenience function and is equivalent to creating a file at the given path and
/// writing the index.
///
/// # Examples
///
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> tokio::io::Result<()> {
/// use noodles_csi as csi;
/// let index = csi::Index::default();
/// csi::r#async::fs::write("sample.bcf.csi", &index).await?;
/// # Ok(())
/// # }
/// ```
pub async fn write<P>(dst: P, index: &Index) -> io::Result<()>
where
P: AsRef<Path>,
{
let mut writer = File::create(dst).await.map(Writer::new)?;
writer.write_index(index).await?;
writer.shutdown().await?;
Ok(())
}
49 changes: 49 additions & 0 deletions noodles-csi/src/fs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//! CSI filesystem operations.
use std::{fs::File, io, path::Path};

use super::{
io::{Reader, Writer},
Index,
};

/// Reads the entire contents of a coordinate-sorted index (CSI).
///
/// This is a convenience function and is equivalent to opening the file at the given path and
/// reading the index.
///
/// # Examples
///
/// ```no_run
/// use noodles_csi as csi;
/// let index = csi::fs::read("sample.bcf.csi")?;
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn read<P>(src: P) -> io::Result<Index>
where
P: AsRef<Path>,
{
let mut reader = File::open(src).map(Reader::new)?;
reader.read_index()
}

/// Writes a coordinate-sorted index (CSI) to a file.
///
/// This is a convenience function and is equivalent to creating a file at the given path and
/// writing the index.
///
/// # Examples
///
/// ```no_run
/// use noodles_csi as csi;
/// let index = csi::Index::default();
/// csi::fs::write("sample.bcf.csi", &index)?;
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn write<P>(dst: P, index: &Index) -> io::Result<()>
where
P: AsRef<Path>,
{
let mut writer = File::create(dst).map(Writer::new)?;
writer.write_index(index)
}
52 changes: 7 additions & 45 deletions noodles-csi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
pub mod r#async;

pub mod binning_index;
pub mod fs;
pub mod io;

use std::{fs::File, path::Path};

use self::binning_index::index::reference_sequence::index::BinnedIndex;
pub use self::binning_index::BinningIndex;

#[deprecated(since = "0.42.0", note = "Use `csi::fs::read` instead.")]
pub use self::fs::read;

#[deprecated(since = "0.42.0", note = "Use `csi::fs::write` instead.")]
pub use self::fs::write;

#[deprecated(since = "0.39.0", note = "Use `noodles_csi::io::Reader` instead.")]
pub use self::io::Reader;

Expand All @@ -35,46 +40,3 @@ pub use self::r#async::io::Writer as AsyncWriter;

/// A coordinate-sorted index (CSI).
pub type Index = binning_index::Index<BinnedIndex>;

/// Reads the entire contents of a coordinate-sorted index (CSI).
///
/// This is a convenience function and is equivalent to opening the file at the given path and
/// reading the index.
///
/// # Examples
///
/// ```no_run
/// # use std::io;
/// use noodles_csi as csi;
/// let index = csi::read("sample.bcf.csi")?;
/// # Ok::<(), io::Error>(())
/// ```
pub fn read<P>(src: P) -> std::io::Result<Index>
where
P: AsRef<Path>,
{
let mut reader = File::open(src).map(Reader::new)?;
reader.read_index()
}

/// Writes a coordinate-sorted index (CSI) to a file.
///
/// This is a convenience function and is equivalent to creating a file at the given path and
/// writing the index.
///
/// # Examples
///
/// ```no_run
/// # use std::io;
/// use noodles_csi as csi;
/// let index = csi::Index::default();
/// csi::write("sample.bcf.csi", &index)?;
/// # Ok::<(), io::Error>(())
/// ```
pub fn write<P>(dst: P, index: &Index) -> std::io::Result<()>
where
P: AsRef<Path>,
{
let mut writer = File::create(dst).map(Writer::new)?;
writer.write_index(index)
}
2 changes: 1 addition & 1 deletion noodles-gff/examples/gff_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let region = args.next().expect("missing region").parse()?;

let index_src = format!("{src}.csi");
let index = csi::read(index_src)?;
let index = csi::fs::read(index_src)?;

let mut reader = File::open(src)
.map(bgzf::Reader::new)
Expand Down
2 changes: 1 addition & 1 deletion noodles-gff/src/io/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ where
/// .map(bgzf::Reader::new)
/// .map(gff::io::Reader::new)?;
///
/// let index = csi::read("annotations.gff3.gz.csi")?;
/// let index = csi::fs::read("annotations.gff3.gz.csi")?;
/// let region = "sq0:8-13".parse()?;
/// let query = reader.query(&index, &region)?;
///
Expand Down
2 changes: 1 addition & 1 deletion noodles-sam/src/io/indexed_reader/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Builder {

if self.index.is_none() {
let index_src = build_index_src(src);
let index = csi::read(index_src)?;
let index = csi::fs::read(index_src)?;
self.index = Some(Box::new(index));
}

Expand Down
4 changes: 2 additions & 2 deletions noodles-sam/src/io/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ where
///
/// let header = reader.read_header()?;
///
/// let index = csi::read("sample.sam.gz.csi")?;
/// let index = csi::fs::read("sample.sam.gz.csi")?;
/// let region = "sq0:8-13".parse()?;
/// let query = reader.query(&header, &index, &region)?;
///
Expand Down Expand Up @@ -384,7 +384,7 @@ where
///
/// reader.read_header()?;
///
/// let index = csi::read("sample.sam.gz.csi")?;
/// let index = csi::fs::read("sample.sam.gz.csi")?;
/// let query = reader.query_unmapped(&index)?;
///
/// for result in query {
Expand Down
2 changes: 1 addition & 1 deletion noodles-vcf/src/io/indexed_reader/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ where
match tabix::read(build_index_src(src, "tbi")) {
Ok(index) => Ok(Box::new(index)),
Err(e) if e.kind() == io::ErrorKind::NotFound => {
let index = csi::read(build_index_src(src, "csi"))?;
let index = csi::fs::read(build_index_src(src, "csi"))?;
Ok(Box::new(index))
}
Err(e) => Err(e),
Expand Down

0 comments on commit 24a6fd7

Please sign in to comment.