## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>",
                      error = FALSE, warning = FALSE, message = FALSE)

## ----load---------------------------------------------------------------------
library(DuckDBGRanges)
library(GenomicRanges)

## ----classes------------------------------------------------------------------
showClass("DuckDBGRanges")

## ----construct----------------------------------------------------------------
gr <- GRanges(c("chr1:100-150:+", "chr1:200-275:-", "chr2:300-360:+"),
              gene_id = paste0("GENE", 1:3))
gr_df <- as.data.frame(gr)
gr_df$range_id <- paste0("range", seq_len(nrow(gr_df)))
gr_path <- tempfile(fileext = ".parquet"); arrow::write_parquet(gr_df, gr_path)

gr_ddb <- DuckDBGRanges(gr_path,
                        seqnames = "seqnames", start = "start",
                        end = "end", strand = "strand",
                        keycol = list(range_id = gr_df$range_id),
                        mcols = "gene_id")
gr_ddb

## ----grl----------------------------------------------------------------------
grl_data <- data.frame(
    transcript_id = c("ENST001", "ENST002"),
    seqnames = I(list(rep("chr1", 3), rep("chr2", 2))),
    start  = I(list(c(100L, 200L, 300L), c(150L, 250L))),
    width  = I(list(c(50L, 75L, 60L),   c(80L, 45L))),
    strand = I(list(c("+", "-", "+"),   c("-", "+"))))
grl_path <- tempfile(fileext = ".parquet"); arrow::write_parquet(grl_data, grl_path)

grl_ddb <- DuckDBGRangesList(grl_path,
                             seqnames = "seqnames", start = "start",
                             width = "width", strand = "strand",
                             keycol = list(transcript_id = grl_data$transcript_id))
elementNROWS(grl_ddb)

## ----materialize--------------------------------------------------------------
as(gr_ddb[seqnames(gr_ddb) == "chr1"], "GRanges")

## ----sessioninfo--------------------------------------------------------------
sessionInfo()

