Last updated: 2025-07-03

Checks: 7 0

Knit directory: Paul_CX_2025/

This reproducible R Markdown analysis was created with workflowr (version 1.7.1). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20250129) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version 73dbcc0. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .RData
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    0.1 box.svg
    Ignored:    Rplot04.svg

Note that any generated files, e.g.Β HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/DDR_Gquad.Rmd) and HTML (docs/DDR_Gquad.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd 0585f7e sayanpaul01 2025-06-03 Commit
html 0585f7e sayanpaul01 2025-06-03 Commit
html 8d1b366 sayanpaul01 2025-06-03 Commit
Rmd 3bfb399 sayanpaul01 2025-06-03 Commit
html 3bfb399 sayanpaul01 2025-06-03 Commit
Rmd b07a3be sayanpaul01 2025-06-01 Commit
html b07a3be sayanpaul01 2025-06-01 Commit
Rmd e9c6af4 sayanpaul01 2025-06-01 Commit
html e9c6af4 sayanpaul01 2025-06-01 Commit
Rmd 69a0b92 sayanpaul01 2025-06-01 Commit
html 55b5edc sayanpaul01 2025-06-01 Commit

πŸ“Œ DNA damage response GO terms

### πŸ“¦ Load Required Libraries
library(tidyverse)
library(data.table)
library(ComplexHeatmap)
library(circlize)
library(grid)

### πŸ“ Input GO Enrichment Files (First Set)
go_files <- list(
  "CX_0.1_3"  = "data/BP/All_Terms/GO_BP_CX_0.1_3.csv",
  "CX_0.1_24" = "data/BP/All_Terms/GO_BP_CX_0.1_24.csv",
  "CX_0.1_48" = "data/BP/All_Terms/GO_BP_CX_0.1_48.csv",
  "CX_0.5_3"  = "data/BP/All_Terms/GO_BP_CX_0.5_3.csv",
  "CX_0.5_24" = "data/BP/All_Terms/GO_BP_CX_0.5_24.csv",
  "CX_0.5_48" = "data/BP/All_Terms/GO_BP_CX_0.5_48.csv",
  "DOX_0.1_3" = "data/BP/All_Terms/GO_BP_DOX_0.1_3.csv",
  "DOX_0.1_24"= "data/BP/All_Terms/GO_BP_DOX_0.1_24.csv",
  "DOX_0.1_48"= "data/BP/All_Terms/GO_BP_DOX_0.1_48.csv",
  "DOX_0.5_3" = "data/BP/All_Terms/GO_BP_DOX_0.5_3.csv",
  "DOX_0.5_24"= "data/BP/All_Terms/GO_BP_DOX_0.5_24.csv",
  "DOX_0.5_48"= "data/BP/All_Terms/GO_BP_DOX_0.5_48.csv"
)

### 🧬 Define GO parent terms of interest and map their children
parent_terms <- list(
  "GO:0006974" = "DNA damage response",
  "GO:0141112" = "broken chromosome clustering",
  "GO:0006281" = "DNA repair",
  "GO:0140861" = "DNA repair-dependent chromatin remodeling",
  "GO:0008630" = "intrinsic apoptotic signaling pathway in response to DNA damage",
  "GO:0042770" = "signal transduction in response to DNA damage",
  "GO:0009432" = "SOS response",
  "GO:0043247" = "telomere maintenance in response to DNA damage"
)

child_map <- list(
  "GO:0006281" = c("GO:0006284", "GO:0006307", "GO:0006302", "GO:0006298", "GO:0043504",
                   "GO:0006289", "GO:0006301", "GO:0006290", "GO:0000725", "GO:0000012"),
  "GO:0008630" = c("GO:0042771", "GO:1902230", "GO:1902231", "GO:1902229"),
  "GO:0042770" = c("GO:0000077", "GO:0030330", "GO:0042772", "GO:0044773",
                   "GO:2000002", "GO:2000003", "GO:2000001"),
  "GO:0043247" = c("GO:1904506", "GO:1904507", "GO:0031848", "GO:1904505", "GO:0097698")
)

ddr_descriptions <- unname(unlist(parent_terms))

### πŸ” Step 1: Retrieve best p-values across all conditions
go_matrix_df <- map_dfr(names(go_files), function(cond) {
  file <- go_files[[cond]]
  df <- tryCatch(fread(file), error = function(e) return(data.table()))

  if (nrow(df) == 0 || !all(c("ID", "Description", "pvalue", "p.adjust") %in% colnames(df))) {
    message("⚠️ Skipping or padding malformed file: ", cond)
    return(tibble(Description = ddr_descriptions, pvalue = NA, p.adjust = NA, log10p = NA, Condition = cond))
  }

  df <- as_tibble(df) %>% dplyr::select(ID, Description, pvalue, p.adjust)

  results <- lapply(names(parent_terms), function(parent_id) {
    all_ids <- c(parent_id, child_map[[parent_id]])
    df_sub <- df %>% filter(ID %in% all_ids)

    if (nrow(df_sub) == 0) {
      tibble(Description = parent_terms[[parent_id]], pvalue = NA, p.adjust = NA, log10p = NA, Condition = cond)
    } else {
      best_row <- df_sub %>% slice_min(pvalue, n = 1)
      tibble(Description = parent_terms[[parent_id]],
             pvalue = best_row$pvalue,
             p.adjust = best_row$p.adjust,
             log10p = -log10(best_row$pvalue),
             Condition = cond)
    }
  })

  bind_rows(results)
})

### 🧱 Step 2: Build heatmap matrix and p-value matrix
heatmap_data <- go_matrix_df %>%
  dplyr::select(Description, Condition, log10p) %>%
  pivot_wider(names_from = Condition, values_from = log10p) %>%
  column_to_rownames("Description") %>%
  as.matrix()

pval_matrix <- go_matrix_df %>%
  dplyr::select(Description, Condition, pvalue) %>%
  pivot_wider(names_from = Condition, values_from = pvalue) %>%
  column_to_rownames("Description") %>%
  as.matrix()

p_adj_matrix <- go_matrix_df %>%
  dplyr::select(Description, Condition, p.adjust) %>%
  pivot_wider(names_from = Condition, values_from = p.adjust) %>%
  column_to_rownames("Description") %>%
  as.matrix()

### βœ… Step 3: Ensure all conditions are included
all_conditions <- names(go_files)
missing_cols <- setdiff(all_conditions, colnames(heatmap_data))
if (length(missing_cols) > 0) {
  for (cond in missing_cols) {
    heatmap_data[, cond] <- NA
    pval_matrix[, cond] <- NA
  }
  heatmap_data <- heatmap_data[, all_conditions]
  pval_matrix <- pval_matrix[, all_conditions]
}

### 🎨 Step 4: Define color palette
breaks <- seq(0, 20, by = 2.5)
palette <- colorRampPalette(c("white", "#fde0dd", "#fa9fb5", "#f768a1", "#c51b8a", "#7a0177", "#49006a"))(length(breaks))
col_fun <- colorRamp2(breaks, palette)

### πŸ”₯ Step 5: Plot Heatmap
ht <- Heatmap(
  heatmap_data,
  name = "-log10(p)",
  col = col_fun,
  na_col = "white",
  rect_gp = gpar(col = "black", lwd = 0.5),
  cluster_rows = FALSE,
  cluster_columns = FALSE,
  row_names_gp = gpar(fontsize = 9),
  column_names_gp = gpar(fontsize = 9),
  column_names_rot = 45,
  row_names_max_width = max_text_width(rownames(heatmap_data), gp = gpar(fontsize = 9)),
  cell_fun = function(j, i, x, y, width, height, fill) {
    adj_p <- p_adj_matrix[i, j]  # Use p.adjust matrix for significance check
    if (!is.na(adj_p) && adj_p < 0.05) {
      grid.text("*", x, y, gp = gpar(fontsize = 12))
    }
  },
  heatmap_legend_param = list(
    title = "-log10(p value)",
    at = breaks,
    labels = as.character(breaks),
    legend_width = unit(5, "cm"),
    direction = "horizontal",
    title_gp = gpar(fontsize = 10, fontface = "bold"),
    labels_gp = gpar(fontsize = 9)
  )
)

### πŸ–Ό Final Draw
draw(ht, heatmap_legend_side = "top")

Version Author Date
3bfb399 sayanpaul01 2025-06-03
55b5edc sayanpaul01 2025-06-01

πŸ“Œ Ribosome biogenesis GO terms

### πŸ“¦ Load Required Libraries
library(tidyverse)
library(data.table)
library(ComplexHeatmap)
library(circlize)
library(grid)

### πŸ“ Define Input Files (All 12 Conditions)
go_files <- list(
  "CX_0.1_3"  = "data/BP/All_Terms/GO_BP_CX_0.1_3.csv",
  "CX_0.1_24" = "data/BP/All_Terms/GO_BP_CX_0.1_24.csv",
  "CX_0.1_48" = "data/BP/All_Terms/GO_BP_CX_0.1_48.csv",
  "CX_0.5_3"  = "data/BP/All_Terms/GO_BP_CX_0.5_3.csv",
  "CX_0.5_24" = "data/BP/All_Terms/GO_BP_CX_0.5_24.csv",
  "CX_0.5_48" = "data/BP/All_Terms/GO_BP_CX_0.5_48.csv",
  "DOX_0.1_3" = "data/BP/All_Terms/GO_BP_DOX_0.1_3.csv",
  "DOX_0.1_24"= "data/BP/All_Terms/GO_BP_DOX_0.1_24.csv",
  "DOX_0.1_48"= "data/BP/All_Terms/GO_BP_DOX_0.1_48.csv",
  "DOX_0.5_3" = "data/BP/All_Terms/GO_BP_DOX_0.5_3.csv",
  "DOX_0.5_24"= "data/BP/All_Terms/GO_BP_DOX_0.5_24.csv",
  "DOX_0.5_48"= "data/BP/All_Terms/GO_BP_DOX_0.5_48.csv"
)

### 🧬 Ribosome Biogenesis GO Terms
parent_terms <- list(
  "GO:0042254" = "ribosome biogenesis",
  "GO:0090071" = "negative regulation of ribosome biogenesis",
  "GO:0042273" = "ribosomal large subunit biogenesis",
  "GO:0042274" = "ribosomal small subunit biogenesis",
  "GO:0000054" = "ribosomal subunit export from nucleus",
  "GO:0042255" = "ribosome assembly",
  "GO:0006364" = "rRNA processing"
)

child_map <- list(
  "GO:0090071" = c("GO:2000201", "GO:2000233"),
  "GO:0042273" = c("GO:0000470", "GO:0000027"),
  "GO:0042274" = c("GO:0030490", "GO:0000028", "GO:0140638"),
  "GO:0000054" = c("GO:2000201", "GO:2000202", "GO:2000200", "GO:0000055", "GO:0000056"),
  "GO:0042255" = c("GO:0042256", "GO:0061668", "GO:0000027", "GO:0000028"),
  "GO:0006364" = c(
    "GO:1901259", "GO:0000450", "GO:0000483", "GO:0002103", "GO:0000479", "GO:0000449",
    "GO:0000475", "GO:0000476", "GO:0000460", "GO:0000481", "GO:0000470", "GO:0030490",
    "GO:2000233", "GO:2000234", "GO:2000232", "GO:0031125", "GO:0000967", "GO:0000154", "GO:1990417"
  )
)

### πŸ” Extract –log10(pvalue) per condition for Ribosome Biogenesis terms
go_descriptions <- unname(unlist(parent_terms))

go_matrix_df <- map_dfr(names(go_files), function(cond) {
  file <- go_files[[cond]]
  df <- tryCatch(fread(file), error = function(e) return(data.table()))

  if (nrow(df) == 0 || !all(c("ID", "Description", "pvalue", "p.adjust") %in% colnames(df))) {
    message("⚠️ Skipping or padding malformed file: ", cond)
    return(tibble(Description = go_descriptions, pvalue = NA, p.adjust = NA, log10p = NA, Condition = cond))
  }

  df <- as_tibble(df) %>% dplyr::select(ID, Description, pvalue, p.adjust)

  results <- lapply(names(parent_terms), function(parent_id) {
    all_ids <- c(parent_id, child_map[[parent_id]])
    df_sub <- df %>% filter(ID %in% all_ids)

    if (nrow(df_sub) == 0) {
      tibble(Description = parent_terms[[parent_id]], pvalue = NA, p.adjust = NA, log10p = NA, Condition = cond)
    } else {
      best_row <- df_sub %>% slice_min(pvalue, n = 1)
      tibble(Description = parent_terms[[parent_id]],
             pvalue = best_row$pvalue,
             p.adjust = best_row$p.adjust,
             log10p = -log10(best_row$pvalue),
             Condition = cond)
    }
  })

  bind_rows(results)
})

### 🧱 Build matrices
heatmap_data <- go_matrix_df %>%
  dplyr::select(Description, Condition, log10p) %>%
  pivot_wider(names_from = Condition, values_from = log10p) %>%
  column_to_rownames("Description") %>%
  as.matrix()

pval_matrix <- go_matrix_df %>%
  dplyr::select(Description, Condition, pvalue) %>%
  pivot_wider(names_from = Condition, values_from = pvalue) %>%
  column_to_rownames("Description") %>%
  as.matrix()

p_adj_matrix <- go_matrix_df %>%
  dplyr::select(Description, Condition, p.adjust) %>%
  pivot_wider(names_from = Condition, values_from = p.adjust) %>%
  column_to_rownames("Description") %>%
  as.matrix()

### πŸ”§ Ensure all conditions are included
all_conditions <- names(go_files)
for (cond in setdiff(all_conditions, colnames(heatmap_data))) {
  heatmap_data[, cond] <- NA
  pval_matrix[, cond] <- NA
}
heatmap_data <- heatmap_data[, all_conditions]
pval_matrix <- pval_matrix[, all_conditions]

### 🎨 Color Palette
breaks <- seq(0, 20, by = 2.5)
palette <- colorRampPalette(c("white", "#fde0dd", "#fa9fb5", "#f768a1", "#c51b8a", "#7a0177", "#49006a"))(length(breaks))
col_fun <- colorRamp2(breaks, palette)

### πŸ”₯ Plot Heatmap
ht <- Heatmap(
  heatmap_data,
  name = "-log10(p)",
  col = col_fun,
  na_col = "white",
  rect_gp = gpar(col = "black", lwd = 0.5),
  cluster_rows = FALSE,
  cluster_columns = FALSE,
  row_names_gp = gpar(fontsize = 9),
  column_names_gp = gpar(fontsize = 9),
  column_names_rot = 45,
  row_names_max_width = max_text_width(rownames(heatmap_data), gp = gpar(fontsize = 9)),
  cell_fun = function(j, i, x, y, width, height, fill) {
    adj_p <- p_adj_matrix[i, j]  # Use p.adjust matrix for significance check
    if (!is.na(adj_p) && adj_p < 0.05) {
      grid.text("*", x, y, gp = gpar(fontsize = 12))
    }
  },
  heatmap_legend_param = list(
    title = "-log10(p value)",
    at = breaks,
    labels = as.character(breaks),
    legend_width = unit(5, "cm"),
    direction = "horizontal",
    title_gp = gpar(fontsize = 10, fontface = "bold"),
    labels_gp = gpar(fontsize = 9)
  )
)

### πŸ–Ό Final Draw
draw(ht, heatmap_legend_side = "top")

Version Author Date
e9c6af4 sayanpaul01 2025-06-01

πŸ“Œ G-Quadruplex GO terms

library(tidyverse)
library(data.table)
library(ComplexHeatmap)
library(circlize)
library(grid)

# πŸ“ Define GO enrichment input files
go_files <- list(
  "CX_0.1_3"   = "data/BP/Combined_Terms/GO_All_CX_0.1_3.csv",
  "CX_0.1_24"  = "data/BP/Combined_Terms/GO_All_CX_0.1_24.csv",
  "CX_0.1_48"  = "data/BP/Combined_Terms/GO_All_CX_0.1_48.csv",
  "CX_0.5_3"   = "data/BP/Combined_Terms/GO_All_CX_0.5_3.csv",
  "CX_0.5_24"  = "data/BP/Combined_Terms/GO_All_CX_0.5_24.csv",
  "CX_0.5_48"  = "data/BP/Combined_Terms/GO_All_CX_0.5_48.csv",
  "DOX_0.1_3"  = "data/BP/Combined_Terms/GO_All_DOX_0.1_3.csv",
  "DOX_0.1_24" = "data/BP/Combined_Terms/GO_All_DOX_0.1_24.csv",
  "DOX_0.1_48" = "data/BP/Combined_Terms/GO_All_DOX_0.1_48.csv",
  "DOX_0.5_3"  = "data/BP/Combined_Terms/GO_All_DOX_0.5_3.csv",
  "DOX_0.5_24" = "data/BP/Combined_Terms/GO_All_DOX_0.5_24.csv",
  "DOX_0.5_48" = "data/BP/Combined_Terms/GO_All_DOX_0.5_48.csv"
)

# 🧬 Define G4-related GO terms
g4_terms <- list(
  "GO:0051880" = "G-quadruplex DNA binding",
  "GO:0002151" = "G-quadruplex RNA binding",
  "GO:0071919" = "G-quadruplex DNA formation",
  "GO:1905493" = "Regulation of G-quadruplex DNA binding",
  "GO:0160225" = "G-quadruplex unwinding activity",
  "GO:0061849" = "Telomeric G-quadruplex DNA binding"
)

# πŸ” Function to extract values
get_go_values <- function(file_path) {
  df <- fread(file_path)
  df <- df[, .(ID, p.adjust, pvalue)]
  
  log10pval <- sapply(names(g4_terms), function(go_id) {
    row <- df[ID == go_id]
    if (nrow(row) == 0) return(NA)
    return(-log10(row$pvalue))
  })
  
  adj_p <- sapply(names(g4_terms), function(go_id) {
    row <- df[ID == go_id]
    if (nrow(row) == 0) return(NA)
    return(row$p.adjust)
  })
  
  return(list(log10pval = log10pval, adj_p = adj_p))
}

# 🧊 Matrix generation
matrix_list <- map(go_files, get_go_values)

go_matrix <- sapply(matrix_list, function(x) x$log10pval)
padj_matrix <- sapply(matrix_list, function(x) x$adj_p)

rownames(go_matrix) <- unname(unlist(g4_terms))
rownames(padj_matrix) <- rownames(go_matrix)

# 🎨 Colors
breaks <- seq(0, 20, by = 2.5)
palette <- colorRampPalette(c("white", "#fde0dd", "#fa9fb5", "#f768a1", "#c51b8a", "#7a0177", "#49006a"))(length(breaks))
col_fun <- colorRamp2(breaks, palette)

# πŸ”₯ Heatmap with correct cell color and stars for adj p < 0.05
ht <- Heatmap(
  go_matrix,
  name = "-log10(p value)",
  col = col_fun,
  na_col = "white",
  rect_gp = gpar(col = "black", lwd = 0.5),
  cluster_rows = FALSE,
  cluster_columns = FALSE,
  row_names_gp = gpar(fontsize = 9),
  column_names_gp = gpar(fontsize = 9),
  column_names_rot = 45,
  row_names_max_width = max_text_width(rownames(go_matrix), gp = gpar(fontsize = 9)),
  cell_fun = function(j, i, x, y, width, height, fill) {
    adj_p <- padj_matrix[i, j]
    if (!is.na(adj_p) && adj_p < 0.05) {
      grid.text("*", x, y, gp = gpar(fontsize = 12))
    }
  },
  heatmap_legend_param = list(
    title = "-log10(p value)",
    at = breaks,
    labels = as.character(breaks),
    legend_width = unit(5, "cm"),
    direction = "horizontal",
    title_gp = gpar(fontsize = 10, fontface = "bold"),
    labels_gp = gpar(fontsize = 9)
  )
)

# πŸ–Ό Draw
draw(ht, heatmap_legend_side = "top")

Version Author Date
0585f7e sayanpaul01 2025-06-03
e9c6af4 sayanpaul01 2025-06-01

πŸ“Œ Transcription by RNA polymerase I

library(tidyverse)
library(data.table)
library(ComplexHeatmap)
library(circlize)
library(grid)

### πŸ“ Define Input GO Enrichment Files for DEG Conditions
go_files <- list(
  "CX_0.1_3"   = "data/BP/Combined_Terms/GO_All_CX_0.1_3.csv",
  "CX_0.1_24"  = "data/BP/Combined_Terms/GO_All_CX_0.1_24.csv",
  "CX_0.1_48"  = "data/BP/Combined_Terms/GO_All_CX_0.1_48.csv",
  "CX_0.5_3"   = "data/BP/Combined_Terms/GO_All_CX_0.5_3.csv",
  "CX_0.5_24"  = "data/BP/Combined_Terms/GO_All_CX_0.5_24.csv",
  "CX_0.5_48"  = "data/BP/Combined_Terms/GO_All_CX_0.5_48.csv",
  "DOX_0.1_3"  = "data/BP/Combined_Terms/GO_All_DOX_0.1_3.csv",
  "DOX_0.1_24" = "data/BP/Combined_Terms/GO_All_DOX_0.1_24.csv",
  "DOX_0.1_48" = "data/BP/Combined_Terms/GO_All_DOX_0.1_48.csv",
  "DOX_0.5_3"  = "data/BP/Combined_Terms/GO_All_DOX_0.5_3.csv",
  "DOX_0.5_24" = "data/BP/Combined_Terms/GO_All_DOX_0.5_24.csv",
  "DOX_0.5_48" = "data/BP/Combined_Terms/GO_All_DOX_0.5_48.csv"
)

### πŸ”§ Parent GO Terms and their Children
parent_terms <- list(
  "GO:0006360" = "transcription by RNA polymerase I",
  "GO:0016479" = "negative regulation of transcription by RNA polymerase I",
  "GO:0042790" = "nucleolar large rRNA transcription by RNA polymerase I",
  "GO:0045943" = "positive regulation of transcription by RNA polymerase I",
  "GO:0006356" = "regulation of transcription by RNA polymerase I",
  "GO:0006363" = "termination of RNA polymerase I transcription",
  "GO:0006361" = "transcription initiation at RNA polymerase I promoter",
  "GO:0005736" = "RNA polymerase I complex"
)

child_map <- list(
  "GO:0016479" = c("GO:2000731", "GO:2001208", "GO:1901837"),
  "GO:0042790" = c("GO:1901837", "GO:1901838", "GO:1901836", "GO:0006362"),
  "GO:0045943" = c("GO:0110016", "GO:2000732", "GO:2001209", "GO:1901838"),
  "GO:0006356" = c("GO:2000730", "GO:1903357", "GO:1901836"),
  "GO:0006363" = c("GO:2000731", "GO:2000732", "GO:2000730"),
  "GO:0006361" = c("GO:1903357", "GO:0001188")
)

### πŸ” Compile –log10(p) matrix for parent groups
go_matrix_df <- map_dfr(names(go_files), function(cond) {
  file <- go_files[[cond]]
  df <- tryCatch(fread(file), error = function(e) return(data.table()))
  
  if (nrow(df) == 0 || !all(c("ID", "Description", "pvalue", "p.adjust") %in% colnames(df))) {
    message("⚠️ Skipping malformed: ", cond)
    return(tibble(Description = unname(unlist(parent_terms)), pvalue = NA, log10p = NA, Condition = cond))
  }
  
  df <- df %>% as_tibble() %>% dplyr::select(ID, Description, pvalue, p.adjust)
  
  results <- lapply(names(parent_terms), function(pid) {
    all_ids <- c(pid, child_map[[pid]])
    df_sub <- df %>% filter(ID %in% all_ids)
    
    if (nrow(df_sub) == 0) {
      tibble(Description = parent_terms[[pid]], pvalue = NA, log10p = NA, Condition = cond)
    } else {
      best <- df_sub %>% slice_min(pvalue, n = 1)
      tibble(Description = parent_terms[[pid]],
             pvalue = best$pvalue,
             p.adjust = best$p.adjust,
             log10p = -log10(best$pvalue),
             Condition = cond)
    }
  })
  
  bind_rows(results)
})

### 🧱 Build Heatmap Matrices
heatmap_data <- go_matrix_df %>%
  dplyr::select(Description, Condition, log10p) %>%
  pivot_wider(names_from = Condition, values_from = log10p) %>%
  column_to_rownames("Description") %>%
  as.matrix()

pval_matrix <- go_matrix_df %>%
  dplyr::select(Description, Condition, pvalue) %>%
  pivot_wider(names_from = Condition, values_from = pvalue) %>%
  column_to_rownames("Description") %>%
  as.matrix()

p_adj_matrix <- go_matrix_df %>%
  dplyr::select(Description, Condition, p.adjust) %>%
  pivot_wider(names_from = Condition, values_from = p.adjust) %>%
  column_to_rownames("Description") %>%
  as.matrix()

### 🧼 Pad missing columns (if any)
all_conditions <- names(go_files)
missing_cols <- setdiff(all_conditions, colnames(heatmap_data))
if (length(missing_cols) > 0) {
  for (cond in missing_cols) {
    heatmap_data[, cond] <- NA
    pval_matrix[, cond] <- NA
  }
  heatmap_data <- heatmap_data[, all_conditions]
  pval_matrix <- pval_matrix[, all_conditions]
}

### 🎨 Define Color Palette
breaks <- seq(0, 20, by = 2.5)
palette <- colorRampPalette(c("white", "#fde0dd", "#fa9fb5", "#f768a1", "#c51b8a", "#7a0177", "#49006a"))(length(breaks))
col_fun <- colorRamp2(breaks, palette)

### πŸ”₯ Plot Heatmap with Stars for p.adjust < 0.05
ht <- Heatmap(
  heatmap_data,
  name = "-log10(p)",
  col = col_fun,
  na_col = "white",
  rect_gp = gpar(col = "black", lwd = 0.5),
  cluster_rows = FALSE,
  cluster_columns = FALSE,
  row_names_gp = gpar(fontsize = 9),
  column_names_gp = gpar(fontsize = 9),
  column_names_rot = 45,
  row_names_max_width = max_text_width(rownames(heatmap_data), gp = gpar(fontsize = 9)),
  cell_fun = function(j, i, x, y, width, height, fill) {
    adj_p <- p_adj_matrix[i, j]  # Use p.adjust matrix for significance check
    if (!is.na(adj_p) && adj_p < 0.05) {
      grid.text("*", x, y, gp = gpar(fontsize = 12))
    }
  },
  heatmap_legend_param = list(
    title = "-log10(p value)",
    at = breaks,
    labels = as.character(breaks),
    legend_width = unit(5, "cm"),
    direction = "horizontal",
    title_gp = gpar(fontsize = 10, fontface = "bold"),
    labels_gp = gpar(fontsize = 9)
  )
)

### πŸ–Ό Draw
draw(ht, heatmap_legend_side = "top")

Version Author Date
3bfb399 sayanpaul01 2025-06-03
b07a3be sayanpaul01 2025-06-01

sessionInfo()
R version 4.3.0 (2023-04-21 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 26100)

Matrix products: default


locale:
[1] LC_COLLATE=English_United States.utf8 
[2] LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

time zone: America/Chicago
tzcode source: internal

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
 [1] circlize_0.4.16       ComplexHeatmap_2.18.0 data.table_1.17.0    
 [4] lubridate_1.9.4       forcats_1.0.0         stringr_1.5.1        
 [7] dplyr_1.1.4           purrr_1.0.4           readr_2.1.5          
[10] tidyr_1.3.1           tibble_3.2.1          ggplot2_3.5.2        
[13] tidyverse_2.0.0       workflowr_1.7.1      

loaded via a namespace (and not attached):
 [1] shape_1.4.6.1       gtable_0.3.6        rjson_0.2.23       
 [4] xfun_0.52           bslib_0.9.0         GlobalOptions_0.1.2
 [7] processx_3.8.6      callr_3.7.6         tzdb_0.5.0         
[10] Cairo_1.6-2         vctrs_0.6.5         tools_4.3.0        
[13] ps_1.8.1            generics_0.1.3      stats4_4.3.0       
[16] parallel_4.3.0      cluster_2.1.8.1     pkgconfig_2.0.3    
[19] RColorBrewer_1.1-3  S4Vectors_0.40.2    lifecycle_1.0.4    
[22] compiler_4.3.0      git2r_0.36.2        munsell_0.5.1      
[25] getPass_0.2-4       codetools_0.2-20    clue_0.3-66        
[28] httpuv_1.6.15       htmltools_0.5.8.1   sass_0.4.10        
[31] yaml_2.3.10         later_1.3.2         pillar_1.10.2      
[34] crayon_1.5.3        jquerylib_0.1.4     whisker_0.4.1      
[37] cachem_1.1.0        magick_2.8.6        iterators_1.0.14   
[40] foreach_1.5.2       tidyselect_1.2.1    digest_0.6.34      
[43] stringi_1.8.3       rprojroot_2.0.4     fastmap_1.2.0      
[46] colorspace_2.1-0    cli_3.6.1           magrittr_2.0.3     
[49] withr_3.0.2         scales_1.3.0        promises_1.3.2     
[52] timechange_0.3.0    rmarkdown_2.29      httr_1.4.7         
[55] matrixStats_1.5.0   png_0.1-8           GetoptLong_1.0.5   
[58] hms_1.1.3           evaluate_1.0.3      knitr_1.50         
[61] IRanges_2.36.0      doParallel_1.0.17   rlang_1.1.3        
[64] Rcpp_1.0.12         glue_1.7.0          BiocGenerics_0.48.1
[67] rstudioapi_0.17.1   jsonlite_2.0.0      R6_2.6.1           
[70] fs_1.6.3