Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
^CRAN-SUBMISSION$
^[\.]?air\.toml$
^\.vscode$
^\.clang-format$
6 changes: 6 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
BasedOnStyle: Google
IndentWidth: 2
ColumnLimit: 100
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
49 changes: 8 additions & 41 deletions R/read.r
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,16 @@ as_granges <- function(x) {

#' Read data from bigBed files.
#'
#' Columns are automatically typed based on the autoSql schema embedded
#' in the bigBed file. Integer types (`uint`, `int`) become R integers,
#' floating point types (`float`, `double`) become R doubles, and all
#' other types (including array types like `int[blockCount]`) remain
#' as character strings.
#'
#' @param bbfile filename for bigBed file
#' @param chrom read data for specific chromosome
#' @param start start position for data
#' @param end end position for data
#' @param convert convert bigBed values to individual columns
#'
#' @return \code{tibble}
#'
Expand All @@ -87,8 +92,7 @@ read_bigbed <- function(
bbfile,
chrom = NULL,
start = NULL,
end = NULL,
convert = TRUE
end = NULL
) {
if (!file.exists(bbfile)) {
stop("File does not exist: ", bbfile)
Expand All @@ -99,42 +103,5 @@ read_bigbed <- function(
}

res <- read_bigbed_cpp(bbfile, chrom, start, end)

if (!convert) {
return(as_tibble(res))
}

vals <- do.call(rbind, strsplit(res[["value"]], "\t"))
# merge chrom, start, end with new values
res_new <- cbind(res[, 1:3], vals)

fnames <- bigbed_sql_fields(bbfile)
# drop chrom, start, end
fnames <- fnames[-(1:3)]

colnames(res_new) <- c("chrom", "start", "end", fnames)
return(as_tibble(res_new))
}

#' @examples
#' 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
lines <- unlist(strsplit(res, "\n"))
fields <- lines[grep(";", lines)]

unlist(
lapply(
fields,
function(line) {
field <- sub("^\\s*\\S+\\s+(\\S+);.*", "\\1", line)
field
}
)
)
as_tibble(res)
}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ 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
#> <chr> <int> <int> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 chr1 4.80e6 4.84e6 test… 1 + 4797973 4836816 1 9
#> 2 chr10 4.85e6 4.88e6 diff… 1 + 4848118 4880877 1 6
#> 3 chr20 5.07e6 5.15e6 negs… 1 - 5073253 5152630 1 14
#> <chr> <int> <int> <chr> <int> <chr> <int> <int> <int> <int>
#> 1 chr1 4.80e6 4.84e6 test… 1 + 4797973 4836816 1 9
#> 2 chr10 4.85e6 4.88e6 diff… 1 + 4848118 4880877 1 6
#> 3 chr20 5.07e6 5.15e6 negs… 1 - 5073253 5152630 1 14
#> # ℹ 2 more variables: blockSizes <chr>, chromStarts <chr>
```
21 changes: 0 additions & 21 deletions cpp11bigwig.Rproj

This file was deleted.

10 changes: 6 additions & 4 deletions man/read_bigbed.Rd

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

Loading
Loading