## -----------------------------------------------------------------------------
#| label: setup
#| include: false
## Chunks that download from Synapse run only when a token is available.
have_token <- nzchar(Sys.getenv("SYNAPSE_AUTH_TOKEN"))
fmt <- function(x) format(x, big.mark = ",", scientific = FALSE)
pct <- function(x, digits = 0) paste0(round(100 * x, digits), "%")
## An analysis result quoted in the prose: `value` when the Synapse steps
## ran, `otherwise` (text that does not depend on them) when they did not.
## `value` is only evaluated when there is a token.
result <- function(value, otherwise = "") {
    if (have_token) value else otherwise
}
code_list <- function(x) paste0("`", x, "`", collapse = ", ")


## -----------------------------------------------------------------------------
#| label: packages
#| warning: false
library(SpatialExperiment)
library(scater)
library(scrapper)
library(HTAN)
library(dplyr)
library(ggplot2)
con <- htan_con("r7")


## -----------------------------------------------------------------------------
#| label: visium-candidates
visium <- htan_files(
    assay == "10xVisiumSpatialTranscriptomics-RNA-seq", level == 4,
    access == "open-synapse", con = con)
visium |>
    count(HTAN_Center, extension) |>
    arrange(desc(n)) |>
    collect()


## -----------------------------------------------------------------------------
#| label: candidates
biospecimens <- htan_tbl("biospecimen", con) |>
    select(HTAN_Assayed_Biospecimen_ID = HTAN_Biospecimen_ID,
        Tumor_Tissue_Type, Site_of_Resection_or_Biopsy)
candidates <- visium |>
    filter(extension == ".h5ad", n_participants == 1) |>
    inner_join(biospecimens, by = "HTAN_Assayed_Biospecimen_ID") |>
    filter(Tumor_Tissue_Type != "Not Otherwise Specified",
        Site_of_Resection_or_Biopsy != "Not Reported") |>
    arrange(File_Size) |>
    select(HTAN_Data_File_ID, Filename, File_Size, Tumor_Tissue_Type,
        Site_of_Resection_or_Biopsy) |>
    collect()
nrow(candidates)
candidates |>
    transmute(file = basename(Filename), MB = round(File_Size / 1e6),
        Tumor_Tissue_Type, site = Site_of_Resection_or_Biopsy) |>
    head(10)


## -----------------------------------------------------------------------------
#| label: pick
visium_id <- "HTA11_99999933430_8422134111"
stopifnot(visium_id %in% candidates$HTAN_Data_File_ID)
file <- htan_files(HTAN_Data_File_ID == visium_id, con = con) |> collect()
glimpse(select(file, HTAN_Data_File_ID, Filename, File_Size, md5,
    entityId, HTAN_Participant_ID, HTAN_Assayed_Biospecimen_ID, access))


## -----------------------------------------------------------------------------
#| label: workflow
workflow <- htan_tbl(
    "10xvisium_spatialtranscriptomics_scRNAseq_level4_metadata", con) |>
    filter(HTAN_Data_File_ID == visium_id) |>
    select(Workflow_Version, Visium_Workflow_Type,
        Visium_Workflow_Parameters_Description, Run_ID, Data_Release) |>
    collect()
glimpse(workflow)


## -----------------------------------------------------------------------------
#| label: lineage
lineage <- htan_lineage(visium_id, con = con) |>
    arrange(depth, related_file_id) |>
    collect()
lineage |>
    transmute(depth, Component = sub("10xVisiumSpatialTranscriptomics-",
        "", Component), file = basename(Filename))


## -----------------------------------------------------------------------------
#| label: biospecimen
biospecimen <- htan_tbl("biospecimen", con) |>
    filter(HTAN_Biospecimen_ID == !!file$HTAN_Assayed_Biospecimen_ID) |>
    select(HTAN_Biospecimen_ID, HTAN_Parent_ID, Acquisition_Method_Type,
        Preservation_Method, Tumor_Tissue_Type, Site_of_Resection_or_Biopsy,
        Histologic_Morphology_Code) |>
    collect()
glimpse(biospecimen)

htan_tbl("clinical_tier1_demographics", con) |>
    filter(HTAN_Participant_ID == !!file$HTAN_Participant_ID) |>
    select(HTAN_Participant_ID, Gender, Race, Vital_Status) |>
    collect()

diagnosis <- htan_tbl("clinical_tier1_diagnosis", con) |>
    filter(HTAN_Participant_ID == !!file$HTAN_Participant_ID) |>
    select(Primary_Diagnosis, Morphology, Tissue_or_Organ_of_Origin,
        Tumor_Grade) |>
    collect()
diagnosis


## -----------------------------------------------------------------------------
#| label: other-data
htan_files(participants = file$HTAN_Participant_ID, con = con) |>
    count(Component, access) |>
    arrange(Component) |>
    collect()


















































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

