## ----knitr-setup, include=FALSE-----------------------------------------------
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)

## ----load-packages------------------------------------------------------------
library(HumanRetinaLRSData)
library(SummarizedExperiment)
library(ggplot2)
library(ggrepel)

## ----load-data----------------------------------------------------------------
se_gene     <- ROGeneLevelData()
se_isoform  <- ROIsoformLevelData()

## ----plot-gene-pca, fig.width=8, fig.height=6---------------------------------
stage_colors <- c(
  "stage 1" = "orange",
  "stage 2" = "seagreen",
  "stage 3" = "purple"
)

expr_gene <- assay(se_gene, "cpm")
pca_gene  <- prcomp(t(log2(expr_gene + 1)), scale. = TRUE)
var_gene  <- round(
  pca_gene$sdev^2 / sum(pca_gene$sdev^2) * 100, 1
)

pca_df_gene <- data.frame(
  PC1    = pca_gene$x[, 1],
  PC2    = pca_gene$x[, 2],
  sample = colnames(expr_gene),
  stage  = colData(se_gene)[["stage"]]
)

ggplot(pca_df_gene, aes(x = PC1, y = PC2, color = stage)) +
  geom_point(size = 3) +
  geom_label_repel(
    aes(label = sample),
    size = 2.5, show.legend = FALSE
  ) +
  scale_color_manual(values = stage_colors, name = "Stage") +
  labs(
    title = "PCA of Gene-Level Expression",
    x     = paste0("PC1 (", var_gene[1], "%)"),
    y     = paste0("PC2 (", var_gene[2], "%)")
  ) +
  theme_bw(base_size = 12)

## ----plot-isoform-pca, fig.width=8, fig.height=6------------------------------
expr_iso <- assay(se_isoform, "cpm")
pca_iso  <- prcomp(t(log2(expr_iso + 1)), scale. = TRUE)
var_iso  <- round(
  pca_iso$sdev^2 / sum(pca_iso$sdev^2) * 100, 1
)

pca_df_iso <- data.frame(
  PC1    = pca_iso$x[, 1],
  PC2    = pca_iso$x[, 2],
  sample = colnames(expr_iso),
  stage  = colData(se_isoform)[["stage"]]
)

ggplot(pca_df_iso, aes(x = PC1, y = PC2, color = stage)) +
  geom_point(size = 3) +
  geom_label_repel(
    aes(label = sample),
    size = 2.5, show.legend = FALSE
  ) +
  scale_color_manual(values = stage_colors, name = "Stage") +
  labs(
    title = "PCA of Isoform-Level Expression",
    x     = paste0("PC1 (", var_iso[1], "%)"),
    y     = paste0("PC2 (", var_iso[2], "%)")
  ) +
  theme_bw(base_size = 12)

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

