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

## ----setup--------------------------------------------------------------------
library(misha)
gdb.init_examples()

## ----eval = FALSE-------------------------------------------------------------
# # eval = FALSE: needs a genome FASTA on disk, or a multi-GB download from UCSC.
# # Create database from FASTA file
# gdb.create("mydb", "/path/to/genome.fa")
# 
# # Or download pre-built genome
# gdb.create_genome("hg38", path = "/path/to/install")

## ----eval = FALSE-------------------------------------------------------------
# # eval = FALSE: same as above, gdb.create() needs a genome FASTA.
# # Set option before creating database
# options(gmulticontig.indexed_format = FALSE)
# gdb.create("mydb", "/path/to/genome.fa")

## -----------------------------------------------------------------------------
gdb.init_examples() # the bundled examples database
info <- gdb.info()
print(info$format) # "indexed" or "per-chromosome"

## -----------------------------------------------------------------------------
gdb.info()

## -----------------------------------------------------------------------------
gdb.convert_to_indexed()
gdb.info()$format

## -----------------------------------------------------------------------------
gdb.init_examples() # start again from a per-chromosome database

# 1D tracks
gtrack.convert_to_indexed("dense_track")

# 2D tracks (rectangles and points)
gtrack.2d.convert_to_indexed("rects_track")

## -----------------------------------------------------------------------------
# Only "big" interval sets are stored per chromosome, so lower the threshold
# to get big sets out of the small example database (default: 1,000,000).
options(gbig.intervals.size = 10)
gintervals.save("myintervals", gscreen("dense_track > 0.3"))
gintervals.save("my2dintervals", gextract("rects_track", gintervals.2d.all())[, 1:6])
options(gbig.intervals.size = 1e6)

# 1D intervals
gintervals.convert_to_indexed("myintervals")

# 2D intervals
gintervals.2d.convert_to_indexed("my2dintervals")

## ----eval = FALSE-------------------------------------------------------------
# # eval = FALSE: copies a whole database outside the vignette's temp directory.
# # Create backup of important database
# system("cp -r /path/to/mydb /path/to/mydb.backup")

## -----------------------------------------------------------------------------
gdb.init_examples()
info <- gdb.info()
print(paste("Current format:", info$format))

## -----------------------------------------------------------------------------
gdb.convert_to_indexed()

## -----------------------------------------------------------------------------
# Check format changed
info <- gdb.info()
print(paste("New format:", info$format))

# Test a few operations
result <- gextract("dense_track", gintervals(1, 0, 1000))
print(head(result))

## ----eval = FALSE-------------------------------------------------------------
# # eval = FALSE: deletes a directory, and there is nothing here to delete.
# # After thorough testing
# system("rm -rf /path/to/mydb.backup")

## -----------------------------------------------------------------------------
gdb.init_examples()
source_db <- .misha$GROOT

# A second database to copy into
target_db <- file.path(tempdir(), "target_db")
unlink(target_db, recursive = TRUE)
gdb.create_linked(target_db, parent = source_db)

# One track, or many
gtrack.copy("dense_track", db = target_db)
gtrack.copy(c("sparse_track", "array_track"), db = target_db)

gsetroot(target_db)
gtrack.ls()
gtrack.info("dense_track")[c("type", "size.in.bytes")]

## -----------------------------------------------------------------------------
gdb.convert_to_indexed()
gdb.info()$format
head(gextract("dense_track", gintervals(1, 0, 300)))

## -----------------------------------------------------------------------------
gsetroot(source_db)
exported <- file.path(tempdir(), "dense_track.txt")
gextract("dense_track", gintervals.all(), iterator = "dense_track", file = exported)

gsetroot(target_db)
gtrack.import("dense_track_roundtrip", "Round-tripped", exported, binsize = 0)
gtrack.info("dense_track")[c("type", "size.in.bytes")]
gtrack.info("dense_track_roundtrip")[c("type", "size.in.bytes")]
gtrack.rm("dense_track_roundtrip", force = TRUE)

## -----------------------------------------------------------------------------
# Work with both formats in same session
gsetroot(source_db) # per-chromosome
data1 <- gextract("dense_track", gintervals(1, 0, 1000))
head(data1)

gsetroot(target_db) # indexed
data2 <- gextract("dense_track", gintervals(1, 0, 1000))
head(data2)

## -----------------------------------------------------------------------------
gdb.init_examples()
gdb.convert_to_indexed()

## -----------------------------------------------------------------------------
gdb.reload()

## -----------------------------------------------------------------------------
# Convert one track at a time
gdb.init_examples()
gtrack.convert_to_indexed("dense_track")
gtrack.convert_to_indexed("sparse_track")
# etc.

