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 de52799
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
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 de52799

Please sign in to comment.