Skip to content

Commit

Permalink
When opening a bigBed with a file-like, use GenericBBIFile, otherwise…
Browse files Browse the repository at this point in the history
… we read initial bytes twice
  • Loading branch information
jackh726 committed Aug 13, 2024
1 parent eb9e1e8 commit f10b42d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bigtools/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bigtools"
version = "0.5.2-dev"
version = "0.5.2"
authors = ["Jack Huey <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion pybigtools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ name = "pybigtools"
crate-type = ["cdylib"]

[dependencies]
bigtools = { version = "0.5.2-dev", path = "../bigtools", default_features = false, features = ["read", "write"] }
bigtools = { version = "0.5.2", path = "../bigtools", default_features = false, features = ["read", "write"] }
url = "2.4.0"
tokio = { version = "1.34.0", features = ["rt", "rt-multi-thread"] }
futures = { version = "0.3.1", features = ["thread-pool"] }
Expand Down
28 changes: 13 additions & 15 deletions pybigtools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use bigtools::utils::misc::{
use bigtools::{
BBIFileRead, BBIReadError as _BBIReadError, BedEntry, BigBedRead as BigBedReadRaw,
BigBedWrite as BigBedWriteRaw, BigWigRead as BigWigReadRaw, BigWigWrite as BigWigWriteRaw,
CachedBBIFileRead, Value, ZoomRecord,
CachedBBIFileRead, GenericBBIRead, Value, ZoomRecord,
};

use bigtools::utils::reopen::Reopen;
Expand Down Expand Up @@ -2385,22 +2385,20 @@ fn open(py: Python, path_url_or_file_like: PyObject, mode: Option<String>) -> Py
"Unknown argument for `path_url_or_file_like`. Not a file path string or url, and not a file-like object.",
))),
};
let read = match BigWigReadRaw::open(file_like.clone()) {
Ok(bwr) => BBIRead {
bbi: BBIReadRaw::BigWigFileLike(bwr.cached()),
let read = match GenericBBIRead::open(file_like.clone()) {
Ok(GenericBBIRead::BigWig(bigwig)) => BBIRead {
bbi: BBIReadRaw::BigWigFileLike(bigwig.cached()),
}
.into_py(py),
Err(_) => match BigBedReadRaw::open(file_like) {
Ok(bbr) => BBIRead {
bbi: BBIReadRaw::BigBedFileLike(bbr.cached()),
}
.into_py(py),
Err(e) => {
return Err(PyErr::new::<BBIReadError, _>(format!(
"File-like object is not a bigWig or bigBed. Or there was just a problem reading: {e}",
)))
}
},
Ok(GenericBBIRead::BigBed(bigbed)) => BBIRead {
bbi: BBIReadRaw::BigBedFileLike(bigbed.cached()),
}
.into_py(py),
Err(e) => {
return Err(PyErr::new::<BBIReadError, _>(format!(
"File-like object is not a bigWig or bigBed. Or there was just a problem reading: {e}",
)))
}
};
Ok(read)
}
Expand Down
4 changes: 4 additions & 0 deletions pybigtools/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def test_open_filelike():
with pybigtools.open(f, "r") as b:
assert b.chroms() == {"chr17": 83_257_441}

with open(TEST_DIR / "data/bigBedExample.bb", "rb") as f:
with pybigtools.open(f, "r") as b:
assert b.chroms("chr21") == 48_129_895

# BytesIO
with open(REPO_ROOT / "bigtools/resources/test/valid.bigWig", "rb") as f:
bw_bytes = f.read()
Expand Down

0 comments on commit f10b42d

Please sign in to comment.