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

## ----load-data----------------------------------------------------------------
library(rnaSentry)
library(SummarizedExperiment)
library(survival)

se_real <- fetch_gse20685()
using_real <- !is.null(se_real)
if (using_real) {
  se <- se_real
} else {
  message("GEOquery unavailable or download failed; using synthetic fallback.")
  se <- rnaSentry:::.brca_fallback_cohort()
}
se
table(se$subtype)
sum(se$event)

## ----naive-screen, warning = FALSE--------------------------------------------
lg <- assays(se)$exprs
vars <- apply(lg, 1, stats::var, na.rm = TRUE)
cand <- names(sort(vars, decreasing = TRUE))[seq_len(200)]
stat <- do.call(rbind, lapply(cand, function(g) {
    s <- summary(coxph(Surv(se$time, se$event) ~ lg[g, ]))
    c(beta = s$coefficients[1, 1], p = s$coefficients[1, 5])
}))
rownames(stat) <- cand
top <- stat[order(stat[, "p"]), , drop = FALSE][seq_len(5), , drop = FALSE]
naive_score <- colMeans(sign(top[, "beta"]) * lg[rownames(top), , drop = FALSE])

c_default <- as.numeric(concordance(Surv(se$time, se$event) ~
                                    naive_score)$concordance[1])
c_reverse <- as.numeric(concordance(Surv(se$time, se$event) ~ naive_score,
                                    reverse = TRUE)$concordance[1])
c(c_default = c_default, c_reverse = c_reverse, sum = c_default + c_reverse)

## ----guarded-run, warning = TRUE----------------------------------------------
run <- run_rnaSentry(se, time_col = "time", event_col = "event",
                     outcome_col = "overall_survival",
                     design_vars = c("age", "subtype"),
                     top_n = 20, repeats = 3, folds = 3, seed = 42,
                     render_report = FALSE)
sig <- run$stages$build_signature
head(sig$genes)
sig$cv_summary
sig$flags

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

## ----risk-groups--------------------------------------------------------------
km <- run$stages$km_curve
km$log_rank_p

## ----brca-km, fig.width = 7, fig.height = 5, fig.align = "center", fig.cap = "Kaplan-Meier survival curves for the low- and high-risk groups at the discovery cutpoint, with the log-rank p-value annotated. The risk groups diverge sharply."----
plot(run$stages$km_curve)

## ----brca-forest, fig.width = 7, fig.height = 5, fig.align = "center", fig.cap = "Adjusted Cox forest plot for the signature genes: log hazard ratio with 95% confidence interval, ordered by p-value. The vertical dashed line marks no effect (log HR = 0)."----
plot(run$stages$cox_model)

## ----brca-parametric, fig.width = 7, fig.height = 5, fig.align = "center", fig.cap = "Kaplan-Meier curves with parametric survival-model overlay for the low- and high-risk groups."----
plot(run$stages$survival_parametric)

## ----design-audit-------------------------------------------------------------
da <- run$stages$design_audit
da$confounder_table
da$formula_text

## ----brca-pca-subtype, fig.width = 7, fig.height = 5, fig.align = "center", fig.cap = "PC1-PC2 scatter of the GSE20685 subset colored by breast-cancer subtype. The subtypes separate along the leading PCs, mirroring the design audit's surrogate-association flag for `subtype`."----
plot_pca_audit(run$stages$pca_audit, color_by = "subtype")

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

