Skip to content

Commit 6a8772d

Browse files
Coerce bigBed columns based on autoSql types (#16)
Co-authored-by: Claude Opus 4.5 <[email protected]>
1 parent ddaed48 commit 6a8772d

File tree

8 files changed

+286
-175
lines changed

8 files changed

+286
-175
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
^CRAN-SUBMISSION$
1212
^[\.]?air\.toml$
1313
^\.vscode$
14+
^\.clang-format$

.clang-format

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
BasedOnStyle: Google
2+
IndentWidth: 2
3+
ColumnLimit: 100
4+
AllowShortFunctionsOnASingleLine: Empty
5+
AllowShortIfStatementsOnASingleLine: false
6+
AllowShortLoopsOnASingleLine: false

R/read.r

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,16 @@ as_granges <- function(x) {
6464

6565
#' Read data from bigBed files.
6666
#'
67+
#' Columns are automatically typed based on the autoSql schema embedded
68+
#' in the bigBed file. Integer types (`uint`, `int`) become R integers,
69+
#' floating point types (`float`, `double`) become R doubles, and all
70+
#' other types (including array types like `int[blockCount]`) remain
71+
#' as character strings.
72+
#'
6773
#' @param bbfile filename for bigBed file
6874
#' @param chrom read data for specific chromosome
6975
#' @param start start position for data
7076
#' @param end end position for data
71-
#' @param convert convert bigBed values to individual columns
7277
#'
7378
#' @return \code{tibble}
7479
#'
@@ -87,8 +92,7 @@ read_bigbed <- function(
8792
bbfile,
8893
chrom = NULL,
8994
start = NULL,
90-
end = NULL,
91-
convert = TRUE
95+
end = NULL
9296
) {
9397
if (!file.exists(bbfile)) {
9498
stop("File does not exist: ", bbfile)
@@ -99,42 +103,5 @@ read_bigbed <- function(
99103
}
100104

101105
res <- read_bigbed_cpp(bbfile, chrom, start, end)
102-
103-
if (!convert) {
104-
return(as_tibble(res))
105-
}
106-
107-
vals <- do.call(rbind, strsplit(res[["value"]], "\t"))
108-
# merge chrom, start, end with new values
109-
res_new <- cbind(res[, 1:3], vals)
110-
111-
fnames <- bigbed_sql_fields(bbfile)
112-
# drop chrom, start, end
113-
fnames <- fnames[-(1:3)]
114-
115-
colnames(res_new) <- c("chrom", "start", "end", fnames)
116-
return(as_tibble(res_new))
117-
}
118-
119-
#' @examples
120-
#' bb <- system.file("extdata", "test.bb", package = "cpp11bigwig")
121-
#' bigbed_sql_fields(bb)
122-
#'
123-
#' @noRd
124-
bigbed_sql_fields <- function(bbfile) {
125-
res <- bigbed_sql_cpp(bbfile)
126-
127-
# parse the autoSql
128-
lines <- unlist(strsplit(res, "\n"))
129-
fields <- lines[grep(";", lines)]
130-
131-
unlist(
132-
lapply(
133-
fields,
134-
function(line) {
135-
field <- sub("^\\s*\\S+\\s+(\\S+);.*", "\\1", line)
136-
field
137-
}
138-
)
139-
)
106+
as_tibble(res)
140107
}

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ bb <- system.file("extdata", "test.bb", package = "cpp11bigwig")
5353
read_bigbed(bb)
5454
#> # A tibble: 3 × 12
5555
#> chrom start end name score strand thickStart thickEnd reserved blockCount
56-
#> <chr> <int> <int> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
57-
#> 1 chr1 4.80e6 4.84e6 test… 1 + 4797973 4836816 1 9
58-
#> 2 chr10 4.85e6 4.88e6 diff… 1 + 4848118 4880877 1 6
59-
#> 3 chr20 5.07e6 5.15e6 negs… 1 - 5073253 5152630 1 14
56+
#> <chr> <int> <int> <chr> <int> <chr> <int> <int> <int> <int>
57+
#> 1 chr1 4.80e6 4.84e6 test… 1 + 4797973 4836816 1 9
58+
#> 2 chr10 4.85e6 4.88e6 diff… 1 + 4848118 4880877 1 6
59+
#> 3 chr20 5.07e6 5.15e6 negs… 1 - 5073253 5152630 1 14
6060
#> # ℹ 2 more variables: blockSizes <chr>, chromStarts <chr>
6161
```

cpp11bigwig.Rproj

Lines changed: 0 additions & 21 deletions
This file was deleted.

man/read_bigbed.Rd

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)