---
title: "ISG Signature Analysis"
author: "Cihan Erkut"
date: "20240325"
date-modified: "`r Sys.time()`"
format:
  html:
    theme: materia
    toc-title: Contents
    highlight-style: github
    df-print: tibble
    toc: true
    number-sections: true
    embed-resources: true
    code-line-numbers: true
    code-block-border-left: true
    code-fold: true
    code-tools: true
knitr:
  opts_chunk:
    tidy: "styler"
execute:
  cache: true
---

## Setup

Load libraries.

```{r}
#| label: setup
#| message: false

library(ComplexHeatmap)
library(tidyverse)
library(readxl)
library(here)
```

## Load data

Import a list of interferon signaling signature genes (ISG).
Import DEGs and DEPs (both for DDA and DIA)

```{r}
genes_ISG <- read_tsv(
  file = here("data/ISG_signature_genes.tsv")
) |>
  pull("ISG Signature")

table_rna <- read_tsv(
  file = here("data/table_results_rnaseq.tsv")
) |>
  filter(
    external_gene_name %in% genes_ISG
  ) |>
  distinct(Treatment, external_gene_name, log2FoldChange, padj, Significant) |>
  rename(
    Condition = Treatment,
    Genes = external_gene_name,
    logFC = log2FoldChange,
    FDR = padj
  ) |>
  mutate(
    Dataset = "RNA"
  )

table_tp1 <- read_tsv(
  file = here("data/table_results_proteomics_tp1.tsv")
) |>
  separate_longer_delim(
    cols = Genes,
    delim = ";"
  ) |>
  mutate(
    across(Genes, \(x) str_trim(x))
  ) |>
  filter(
    Genes %in% genes_ISG
  ) |>
  distinct(Condition, Genes, logFC, adj.P.Val, Significant) |>
  rename(
    FDR = adj.P.Val
  ) |>
  mutate(
    Dataset = "TP1"
  )

table_dia <- read_tsv(
  file = here("data/table_results_proteomics_DIA_single.tsv")
) |>
  separate_longer_delim(
    cols = Genes,
    delim = ";"
  ) |>
  mutate(
    across(Genes, \(x) str_trim(x))
  ) |>
  filter(
    Genes %in% genes_ISG
  ) |>
  distinct(Condition, Genes, logFC, adj.P.Val, Significant) |>
  rename(
    FDR = adj.P.Val
  ) |>
  mutate(
    Dataset = "DIA1"
  )

table_combined <- bind_rows(
  table_rna,
  table_tp1,
  table_dia
)

matrix_logFC <- table_combined |>
  select(-c(Significant, FDR)) |>
  pivot_wider(
    names_from = c(Dataset, Condition),
    values_from = logFC,
    values_fill = 0
  ) |>
  column_to_rownames("Genes") |>
  as.matrix()


matrix_FDR <- table_combined |>
  mutate(
    sig = if_else(Significant, "", "o")
  ) |>
  select(-c(Significant, logFC, FDR)) |>
  pivot_wider(
    names_from = c(Dataset, Condition),
    values_from = sig,
    values_fill = "x"
  ) |>
  column_to_rownames("Genes") |>
  as.matrix()

colnames(matrix_logFC) <- c("A2", "B1", "D4", "A2", "B1", "D4", "TBXT KO", "IGFBP3 WT", "IGFBP3 KO")
```

## Visualization

Plot as a heatmap.

```{r}
#| fig-height: 7

pdf(
  file = here("plots/20240912_plot_heatmap_ISG_genes.pdf"),
  width = 3.5,
  height = 6.7
)

Heatmap(
  name = "Fold\nchange\n(log2)",
  matrix = matrix_logFC,
  width = unit(6, "mm") * ncol(matrix_logFC),
  height = unit(4, "mm") * nrow(matrix_logFC),
  col = circlize::colorRamp2(
    breaks = c(-2.4, 0, 2.4),
    colors = c("darkblue", "transparent", "darkred")
  ),
  clustering_method_rows = "ward.D2",
  cluster_columns = FALSE,
  row_names_gp = gpar(
    fontsize = 9
  ),
  column_names_side = "top",
  column_names_rot = 45,
  column_names_centered = FALSE,
  cell_fun = \(j, i, x, y, w, h, f) {
    if (matrix_FDR[i, j] == "o") {
      grid.circle(x, y, r = 0.01)
    }
  },
  show_row_dend = FALSE
)

dev.off()
```
