Skip to content

Commit

Permalink
Update as param to read_bigwig()
Browse files Browse the repository at this point in the history
Now matches `rtracklayer::import.bw()` for easier migration.
  • Loading branch information
jayhesselberth committed Dec 29, 2024
1 parent dc13c4b commit c50715d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 20 deletions.
Empty file removed .gitmodules
Empty file.
21 changes: 9 additions & 12 deletions R/read.r
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@
#' @seealso \url{https://github.com/brentp/bw-python}
#'
#' @examples
#' bw <- system.file('extdata', 'test.bw', package = 'cpp11bigwig')
#' bw <- system.file("extdata", "test.bw", package = "cpp11bigwig")
#'
#' read_bigwig(bw)
#'
#' read_bigwig(bw, chrom = "10")
#'
#' read_bigwig(bw, chrom = "1", start = 100, end = 130)
#'
#' read_bigwig(bw, as = 'gr')
#' read_bigwig(bw, as = "GRanges")
#'
#' @export
read_bigwig <- function(bwfile, chrom = NULL, start = NULL, end = NULL, as = NULL) {

if (!file.exists(bwfile)) {
stop("File does not exist: ", bwfile)
}
Expand All @@ -34,22 +33,22 @@ read_bigwig <- function(bwfile, chrom = NULL, start = NULL, end = NULL, as = NUL
stop("`start` and `end` must both be >= 0")
}

if (!is.null(as) && !as %in% c("gr", "tbl")) {
stop("`as` must be one of 'gr' or 'tbl' (the default)")
if (!is.null(as) && !as %in% c("GRanges", "tbl")) {
stop("`as` must be one of 'GRanges' or 'tbl' (the default)")
}

res <- read_bigwig_cpp(bwfile, chrom, start, end)

if (!is.null(as) && as == 'gr') {
return(as_gr(res))
if (!is.null(as) && as == "GRanges") {
return(as_granges(res))
} else {
return(as_tibble(res))
}
}

#' convert to GRanges
#' @noRd
as_gr <- function(x) {
as_granges <- function(x) {
GRanges(
seqnames = x$chrom,
ranges = IRanges(start = x$start, end = x$end),
Expand All @@ -71,15 +70,14 @@ as_gr <- function(x) {
#' @seealso \url{https://github.com/brentp/bw-python}
#'
#' @examples
#' bb <- system.file('extdata', 'test.bb', package = 'cpp11bigwig')
#' bb <- system.file("extdata", "test.bb", package = "cpp11bigwig")
#'
#' read_bigbed(bb)
#'
#' read_bigbed(bb, chrom = "chr10")
#'
#' @export
read_bigbed <- function(bbfile, chrom = NULL, start = NULL, end = NULL, convert = TRUE) {

if (!file.exists(bbfile)) {
stop("File does not exist: ", bbfile)
}
Expand Down Expand Up @@ -107,12 +105,11 @@ read_bigbed <- function(bbfile, chrom = NULL, start = NULL, end = NULL, convert
}

#' @examples
#' bb <- system.file('extdata', 'test.bb', package = 'cpp11bigwig')
#' bb <- system.file("extdata", "test.bb", package = "cpp11bigwig")
#' bigbed_sql_fields(bb)
#'
#' @noRd
bigbed_sql_fields <- function(bbfile) {

res <- bigbed_sql_cpp(bbfile)

# parse the autoSql
Expand Down
4 changes: 2 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ pak::pak("rnabioco/cpp11bigwig")
```{r}
library(cpp11bigwig)
bw <- system.file('extdata', 'test.bw', package = 'cpp11bigwig')
bw <- system.file("extdata", "test.bw", package = "cpp11bigwig")
read_bigwig(bw)
bb <- system.file('extdata', 'test.bb', package = 'cpp11bigwig')
bb <- system.file("extdata", "test.bb", package = "cpp11bigwig")
read_bigbed(bb)
```
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pak::pak("rnabioco/cpp11bigwig")
``` r
library(cpp11bigwig)

bw <- system.file('extdata', 'test.bw', package = 'cpp11bigwig')
bw <- system.file("extdata", "test.bw", package = "cpp11bigwig")
read_bigwig(bw)
#> # A tibble: 6 × 4
#> chrom start end value
Expand All @@ -47,7 +47,7 @@ read_bigwig(bw)
#> 5 1 150 151 1.5
#> 6 10 200 300 2

bb <- system.file('extdata', 'test.bb', package = 'cpp11bigwig')
bb <- system.file("extdata", "test.bb", package = "cpp11bigwig")
read_bigbed(bb)
#> # A tibble: 3 × 12
#> chrom start end name score strand thickStart thickEnd reserved blockCount
Expand Down
6 changes: 2 additions & 4 deletions tests/testthat/test-read.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

test_that("results have expected shape", {
bw <- test_path("data/test.bw")

Expand All @@ -8,7 +7,7 @@ test_that("results have expected shape", {
expect_true("tbl_df" %in% class(res))

# interval sizes
expect_true(all(res$end - res$start == c(1,1,1,50,1,100)))
expect_true(all(res$end - res$start == c(1, 1, 1, 50, 1, 100)))
# interval values
expect_equal(sum(res$value), 5.5)

Expand All @@ -18,7 +17,7 @@ test_that("results have expected shape", {
expect_equal(nrow(read_bigwig(bw, end = 3)), 3)

# GRanges
res <- read_bigwig(bw, as = 'gr')
res <- read_bigwig(bw, as = "GRanges")
expect_true("GRanges" %in% class(res))

# bigbed
Expand All @@ -27,7 +26,6 @@ test_that("results have expected shape", {

expect_equal(ncol(res), 12)
expect_equal(nrow(res), 3)

})

test_that("missing file causes error", {
Expand Down

0 comments on commit c50715d

Please sign in to comment.