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

## ----build-csv----------------------------------------------------------------
library(rnaSentry)
library(SummarizedExperiment)

set.seed(42)
n_genes <- 300
n_samples <- 30
genes <- paste0("GENE", seq_len(n_genes))
samples <- paste0("S", seq_len(n_samples))

# Raw integer counts (bulk RNA-seq style), genes x samples.
cnt <- matrix(rpois(n_genes * n_samples, lambda = 200),
              nrow = n_genes, ncol = n_samples,
              dimnames = list(genes, samples))

# Clinical metadata, one row per sample.
clin <- S4Vectors::DataFrame(
    time  = round(rexp(n_samples, rate = 0.08), 1),
    event = rbinom(n_samples, size = 1, prob = 0.5),
    batch = factor(rep(c("B1", "B2", "B3"), length.out = n_samples)),
    stage = factor(sample(c("I", "II", "III"), n_samples, replace = TRUE)),
    row.names = samples
)

# --- Deliberately introduce the two classic file problems -------------------
# A sample present in the counts file but not the clinical file,
cnt <- cbind(cnt, S31 = cnt[, "S2"])
# and a clinical row with no matching counts column.
clin <- rbind(clin, S32 = clin["S1", ])
# Plus two intake issues load_counts() should catch: a non-integer count...
cnt["GENE1", "S1"] <- cnt["GENE1", "S1"] + 0.5
# ...and a gene with no counts anywhere.
cnt["GENE300", ] <- 0L

td <- tempfile("csvdemo")
dir.create(td)
write.csv(cnt, file.path(td, "counts.csv"))
write.csv(clin, file.path(td, "clinical.csv"))
list.files(td)

## ----read-csv-----------------------------------------------------------------
counts   <- as.matrix(read.csv(file.path(td, "counts.csv"),   row.names = 1))
clinical <- read.csv(file.path(td, "clinical.csv"), row.names = 1)

dim(counts)
dim(clinical)

## ----intersect-samples--------------------------------------------------------
n_counts_before <- ncol(counts)
n_clin_before   <- nrow(clinical)

common <- intersect(colnames(counts), rownames(clinical))
counts   <- counts[,  common, drop = FALSE]
clinical <- clinical[common, ,   drop = FALSE]

data.frame(
    "counts columns" = c(before = n_counts_before, after = ncol(counts)),
    "clinical rows"  = c(before = n_clin_before,   after = nrow(clinical)),
    row.names = c("before intersect", "after intersect")
)

## ----load-counts--------------------------------------------------------------
se <- load_counts(counts, clinical)
se

## ----load-flags---------------------------------------------------------------
S4Vectors::metadata(se)$flags

## ----design-audit-------------------------------------------------------------
da <- design_audit(se, design_vars = c("batch", "stage"))
da

## ----run-pipeline, warning = TRUE---------------------------------------------
run <- run_rnaSentry(se, time_col = "time", event_col = "event",
                     top_n = 5, repeats = 1, folds = 2, seed = 42,
                     design_vars = c("batch", "stage"),
                     render_report = FALSE)
run

## ----unlock-signature---------------------------------------------------------
run$stages$build_signature <-
    lock_signature(run$stages$build_signature, lock = FALSE)

## ----assay-naming-------------------------------------------------------------
se_norm <- se
SummarizedExperiment::assayNames(se_norm) <- "logcounts"

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

