## -----------------------------------------------------------------------------
#| label: setup
#| include: false
library(HTAN)
library(dplyr)
fmt <- function(x) format(x, big.mark = ",", scientific = FALSE)


## -----------------------------------------------------------------------------
#| label: install
#| eval: false
# if (!requireNamespace("BiocManager", quietly = TRUE)) {
#     install.packages("BiocManager")
# }
# BiocManager::install("HTAN")


## -----------------------------------------------------------------------------
#| label: install-github
#| eval: false
# BiocManager::install("seandavi/HTAN")


## -----------------------------------------------------------------------------
#| label: load
library(HTAN)
library(dplyr)


## -----------------------------------------------------------------------------
#| label: con
con <- htan_con("r7")


## -----------------------------------------------------------------------------
#| label: first-query
htan_files(assay == "ScRNA-seq", level == 4, con = con) |>
    count(HTAN_Center, extension) |>
    arrange(desc(n)) |>
    collect()


## -----------------------------------------------------------------------------
#| label: first-lineage
htan_files(assay == "ScRNA-seq", level == 4, participants = "HTA12_251",
    con = con) |>
    head(1) |>
    htan_lineage(con = con) |>
    select(related_file_id, depth, Component, HTAN_Assayed_Biospecimen_ID) |>
    arrange(depth) |>
    collect()


## -----------------------------------------------------------------------------
#| label: snapshot-info
info <- htan_snapshot_info(con)
info[c("release", "phase", "source", "n_tables", "total_rows", "build_date")]


## -----------------------------------------------------------------------------
#| label: table-groups
htan_tables(con) |>
    count(category)


## -----------------------------------------------------------------------------
#| label: old-tables
htan_tables(con) |>
    filter(source_version != "r7") |>
    select(name, source_version, rows)


## -----------------------------------------------------------------------------
#| label: fixture
fixture <- system.file("extdata", "snapshot", package = "HTAN")
con_small <- htan_con("r7", base_url = fixture)
htan_snapshot_info(con_small)[c("release", "n_tables", "total_rows")]
htan_disconnect(con_small)


## -----------------------------------------------------------------------------
#| label: demographics
htan_tbl("clinical_tier1_demographics", con) |>
    count(Gender, sort = TRUE) |>
    collect()


## -----------------------------------------------------------------------------
#| label: demographics-clean
htan_tbl("clinical_tier1_demographics", con) |>
    mutate(Gender = tolower(Gender)) |>
    count(Gender, sort = TRUE) |>
    collect()


## -----------------------------------------------------------------------------
#| label: columns
htan_columns("id_provenance", con) |>
    select(column, description)


## -----------------------------------------------------------------------------
#| label: files
files <- htan_files(con = con)
files |>
    count(access) |>
    arrange(desc(n)) |>
    collect()


## -----------------------------------------------------------------------------
#| label: files-h5ad
htan_files(assay == "ScRNA-seq", level == 4, extension == ".h5ad",
    con = con) |>
    select(HTAN_Data_File_ID, HTAN_Center, File_Size, n_participants) |>
    arrange(File_Size) |>
    collect()


## -----------------------------------------------------------------------------
#| label: files-participant
htan_files(participants = "HTA11_7862", con = con) |>
    count(Component, access) |>
    arrange(Component) |>
    collect()


## -----------------------------------------------------------------------------
#| label: lineage
htan_lineage("HTA11_7862_200000101134111", con = con) |>
    select(related_file_id, depth, Filename) |>
    arrange(depth, related_file_id) |>
    collect() |>
    mutate(Filename = basename(Filename))


## -----------------------------------------------------------------------------
#| label: download-gc
tiff <- htan_files(
    drs_uri == "dg.4DFC/6231a7d4-7e68-11ee-b51d-e7db3b9207b8", con = con)
tiff |>
    select(HTAN_Data_File_ID, Component, File_Size, access) |>
    collect()
dl <- htan_download(tiff, con = con)
dl$status
read_htan(dl) |> basename()


## -----------------------------------------------------------------------------
#| label: download-synapse
#| eval: false
# visium <- htan_files(HTAN_Data_File_ID == "HTA11_99999933430_8422134111",
#     con = con)
# dl <- htan_download(visium, con = con)
# spe <- read_htan(dl)


## -----------------------------------------------------------------------------
#| label: download-controlled
fastq <- htan_files(access == "controlled", con = con) |>
    arrange(HTAN_Data_File_ID) |>
    head(1)
tryCatch(
    htan_download(fastq, con = con),
    htan_error_controlled = function(e) cat(conditionMessage(e))
)


## -----------------------------------------------------------------------------
#| label: session-info
sessionInfo()

