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

## ----install, eval=FALSE------------------------------------------------------
# if (!requireNamespace("BiocManager", quietly = TRUE))
#     install.packages("BiocManager")
# 
# BiocManager::install("igvShiny")

## ----load-package, results="hide", message=FALSE, warning=FALSE---------------
library(igvShiny)

## ----genome-spec--------------------------------------------------------------
genomeOptions <- parseAndValidateGenomeSpec(
    genomeName = "hg38",
    initialLocus = "NDUFS2"
    )
str(genomeOptions)

## ----minimal-app, eval=FALSE--------------------------------------------------
# library(shiny)
# library(igvShiny)
# 
# genomeOptions <- parseAndValidateGenomeSpec(
#     genomeName = "hg38",
#     initialLocus = "NDUFS2"
#     )
# 
# ui <- fluidPage(
#     titlePanel("igvShiny"),
#     igvShinyOutput("igv")
#     )
# 
# server <- function(input, output, session) {
#     output$igv <- renderIgvShiny({
#         igvShiny(genomeOptions)
#         })
#     }
# 
# shinyApp(ui, server)

## ----bed-track, eval=FALSE----------------------------------------------------
# tbl <- data.frame(
#     chrom = c("chr1", "chr1", "chr1"),
#     start = c(7432000, 7437000, 7443000),
#     end   = c(7436000, 7442000, 7447000),
#     value = c(0.2, 0.9, 0.4),
#     stringsAsFactors = FALSE
#     )
# 
# server <- function(input, output, session) {
#     output$igv <- renderIgvShiny({
#         igvShiny(genomeOptions)
#         })
# 
#     observeEvent(input$addTrack, {
#         loadBedTrack(
#             session,
#             id = "igv",
#             trackName = "my regions",
#             tbl = tbl,
#             color = "darkblue"
#             )
#         })
#     }

## ----url-track, eval=FALSE----------------------------------------------------
# gff3 <- paste0(
#     "https://s3.amazonaws.com/igv.org.genomes/hg38/",
#     "Homo_sapiens.GRCh38.94.chr.gff3.gz"
#     )
# 
# loadGFF3TrackFromURL(
#     session,
#     id = "igv",
#     trackName = "genes",
#     gff3URL = gff3,
#     indexURL = paste0(gff3, ".tbi"),
#     color = "darkgreen",
#     trackHeight = 100
#     )

## ----startup-tracks, eval=FALSE-----------------------------------------------
# output$igv <- renderIgvShiny({
#     igvShiny(
#         genomeOptions,
#         tracks = list(
#             list(
#                 name = "genes",
#                 type = "annotation",
#                 format = "gff3",
#                 url = paste0(
#                     "https://s3.amazonaws.com/igv.org.genomes/hg38/",
#                     "Homo_sapiens.GRCh38.94.chr.gff3.gz"
#                     ),
#                 indexed = FALSE
#                 )
#             )
#         )
#     })

## ----merged-track, eval=FALSE-------------------------------------------------
# sample <- paste0(
#     "https://raw.githubusercontent.com/igvteam/igv-data/main/data/test/",
#     "splice_junctions/splice_junction_track_test_cases_sampleA.",
#     "chr15-92835700-93031800"
#     )
# 
# output$igv <- renderIgvShiny({
#     igvShiny(
#         genomeOptions,
#         tracks = list(
#             list(
#                 name = "sampleA",
#                 type = "merged",
#                 height = 180,
#                 tracks = list(
#                     list(
#                         type = "wig",
#                         format = "bigwig",
#                         url = paste0(sample, ".bigWig"),
#                         color = "rgb(190,190,190)"
#                         ),
#                     list(
#                         type = "junction",
#                         format = "bed",
#                         url = paste0(sample, ".SJ.out.bed.gz"),
#                         indexURL = paste0(sample, ".SJ.out.bed.gz.tbi"),
#                         colorBy = "motif"
#                         )
#                     )
#                 )
#             )
#         )
#     })

## ----navigate, eval=FALSE-----------------------------------------------------
# observeEvent(input$goto, {
#     showGenomicRegion(
#         session,
#         id = "igv",
#         region = "chr5:88,700,000-88,800,000"
#         )
#     })

## ----read-region, eval=FALSE--------------------------------------------------
# observeEvent(input$whereAmI, {
#     getGenomicRegion(session, id = "igv")
#     })
# 
# observeEvent(input[["currentGenomicRegion.igv"]], {
#     message("now showing: ", input[["currentGenomicRegion.igv"]])
#     })

## ----module, eval=FALSE-------------------------------------------------------
# igvModuleUI <- function(id) {
#     ns <- NS(id)
#     tagList(
#         actionButton(ns("addTrack"), "Add track"),
#         igvShinyOutput(ns("igv"))
#         )
#     }
# 
# igvModuleServer <- function(id, genomeOptions, tbl) {
#     moduleServer(id, function(input, output, session) {
#         ns <- session$ns
# 
#         output$igv <- renderIgvShiny({
#             igvShiny(genomeOptions)
#             })
# 
#         observeEvent(input$addTrack, {
#             loadBedTrack(
#                 session,
#                 id = ns("igv"),
#                 trackName = "regions",
#                 tbl = tbl
#                 )
#             })
# 
#         # the region input keeps its unnamespaced name inside the module
#         observeEvent(input[["currentGenomicRegion.igv"]], {
#             message("now showing: ", input[["currentGenomicRegion.igv"]])
#             })
#         })
#     }

## ----custom-genome------------------------------------------------------------
data_directory <- system.file(package = "igvShiny", "extdata")

customOptions <- parseAndValidateGenomeSpec(
    genomeName = "ribosomal RNA gene",
    initialLocus = "U13369.1:7,276-8,225",
    stockGenome = FALSE,
    dataMode = "localFiles",
    fasta = file.path(data_directory, "ribosomal-RNA-gene.fasta"),
    fastaIndex = file.path(data_directory, "ribosomal-RNA-gene.fasta.fai"),
    genomeAnnotation = file.path(data_directory, "ribosomal-RNA-gene.gff3")
    )
customOptions[c("genomeName", "dataMode", "stockGenome")]

## ----custom-genome-http, eval=FALSE-------------------------------------------
# base_url <- "https://gladki.pl/igvr/testFiles"
# 
# remoteOptions <- parseAndValidateGenomeSpec(
#     genomeName = "ribosomal RNA gene",
#     initialLocus = "U13369.1:7,276-8,225",
#     stockGenome = FALSE,
#     dataMode = "http",
#     fasta = paste0(base_url, "/ribosomal-RNA-gene.fasta"),
#     fastaIndex = paste0(base_url, "/ribosomal-RNA-gene.fasta.fai"),
#     genomeAnnotation = paste0(base_url, "/ribosomal-RNA-gene.gff3")
#     )
# remoteOptions[c("genomeName", "dataMode", "stockGenome")]

## ----session-info-------------------------------------------------------------
sessionInfo()

