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

## ----load-package, results="hide", message=FALSE, warning=FALSE---------------
library(igvShiny)
futile.logger::flog.threshold(futile.logger::WARN)

## ----stub-session, include = FALSE--------------------------------------------
# The loaders are one-way messages to a browser: they take a shiny session and
# send to it, and there is no browser here. A session that prints instead of
# sending lets every example below run, and shows what igv.js receives.
session <- list(sendCustomMessage = function(type, message) {
    flatten <- function(v) paste(v, collapse = ",")
    fields <- vapply(message, flatten, character(1))
    cat(type, "\n", paste0("  ", names(fields), ": ", fields, collapse = "\n"),
        "\n", sep = "")
    })

## ----trackconfig--------------------------------------------------------------
tbl <- data.frame(
    chrom = "chr1",
    start = 7432000,
    end = 7436000
    )

loadBedTrack(
    session,
    id = "igv",
    trackName = "my regions",
    tbl = tbl,
    trackConfig = list(displayMode = "EXPANDED", maxRows = 20)
    )

## ----rejected-key-------------------------------------------------------------
genomeOptions <- parseAndValidateGenomeSpec(
    genomeName = "hg38",
    initialLocus = "NDUFS2"
    )

widget <- igvShiny(
    genomeOptions,
    tracks = list(
        list(
            name = "genes",
            type = "annotation",
            url = "https://example.org/genes.bed",
            onClick = "alert('hi')"
            )
        )
    )

names(widget$x$tracks[[1]])

## ----wig----------------------------------------------------------------------
coverage <- data.frame(
    chrom = "chr1",
    start = c(7432000, 7437000),
    end = c(7436000, 7442000),
    value = c(0.2, 0.9)
    )

loadBedGraphTrack(
    session, id = "igv", trackName = "coverage", tbl = coverage,
    autoscale = TRUE,
    trackConfig = list(
        graphType = "points",
        autoscaleGroup = "sampleA",
        logScale = FALSE
        )
    )

## ----gff3---------------------------------------------------------------------
gff3.url <- paste0(
    "https://s3.amazonaws.com/igv.org.genomes/hg38/",
    "Homo_sapiens.GRCh38.94.chr.gff3.gz"
    )

loadGFF3TrackFromURL(
    session, id = "igv", trackName = "genes",
    gff3URL = gff3.url, indexURL = paste0(gff3.url, ".tbi"),
    colorByAttribute = "biotype",
    displayMode = "EXPANDED",
    visibilityWindow = 1000000,
    colorTable = list(
        protein_coding = "darkgreen",
        processed_transcript = "blue",
        default = "black"
        )
    )

## ----bam-sort-----------------------------------------------------------------
bam.url <- "https://1000genomes.s3.amazonaws.com/phase3/NA12878.bam"

loadBamTrackFromURL(
    session, id = "igv", trackName = "reads",
    bamURL = bam.url, indexURL = paste0(bam.url, ".bai"),
    trackConfig = list(
        sort = list(
            option = "TAG", tag = "HP",
            chr = "chr8", position = 128750000
            )
        )
    )

## ----gwas---------------------------------------------------------------------
gwas <- data.frame(
    CHR = "chr1",
    BP = c(1000, 2000),
    SNP = c("rs1", "rs2"),
    P = c(1e-8, 1e-5)
    )

loadGwasTrack(
    session, id = "igv", trackName = "gwas", tbl.gwas = gwas,
    trackConfig = list(
        columns = list(chromosome = 12, position = 13, value = 28)
        )
    )

## ----junctions----------------------------------------------------------------
junctions.url <- paste0(
    "https://raw.githubusercontent.com/igvteam/igv-data/main/data/test/",
    "splice_junctions/sampleA.SJ.out.bed.gz"
    )

loadSpliceJunctionTrackFromURL(
    session, id = "igv", trackName = "sampleA junctions",
    url = junctions.url, indexURL = paste0(junctions.url, ".tbi"),
    trackHeight = 150,
    trackConfig = list(
        colorBy = "motif",
        labelWith = "uniquelyMapped",
        minUniquelyMappedReads = 5,
        hideAnnotatedJunctions = FALSE
        )
    )

## ----startup------------------------------------------------------------------
gff3.url <- paste0(
    "https://s3.amazonaws.com/igv.org.genomes/hg38/",
    "Homo_sapiens.GRCh38.94.chr.gff3.gz"
    )

startupTrack <- list(
    name = "genes",
    type = "annotation",
    format = "gff3",
    url = gff3.url,
    indexed = FALSE,
    displayMode = "EXPANDED"
    )

widget <- igvShiny(genomeOptions, tracks = list(startupTrack))
str(widget$x$tracks[[1]])

## ----startup-app, eval=FALSE--------------------------------------------------
# output$igv <- renderIgvShiny({
#     igvShiny(genomeOptions, tracks = list(startupTrack))
#     })

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

