13  Circos Fruit

Tip

Enhanced visualization integrating differential gene analysis, pathway-level GSVA (Gene Set Variation Analysis), and ssGSEA (single-sample Gene Set Enrichment Analysis).

library(TransProR)
library(org.Hs.eg.db)
library(clusterProfiler)
library(tidyr)
library(dplyr)
library(ggtree)
library(ggplot2)

13.1 Load data

tumor <- readRDS("../test_TransProR/generated_data1/removebatch_SKCM_Skin_TCGA_exp_tumor.rds")
normal <- readRDS('../test_TransProR/generated_data1/removebatch_SKCM_Skin_Normal_TCGA_GTEX_count.rds')
# Merge the datasets, ensuring both have genes as row names
all_count_exp <- merge(tumor, normal, by = "row.names")
all_count_exp <- tibble::column_to_rownames(all_count_exp, var = "Row.names")  # Set the row names

# Drawing data
# all_count_exp <- log_transform(all_count_exp)
DEG_deseq2 <- readRDS('../test_TransProR/Select DEGs/DEG_deseq2.Rdata')
#head(all_count_exp, 1)
head(DEG_deseq2, 5)
          baseMean log2FoldChange      lfcSE      stat pvalue padj change
A1BG      134.6084      -2.549682 0.05677219 -44.91075      0    0   down
A2M     30208.9912       3.251168 0.08394645  38.72907      0    0     up
AADACL2   801.4538      -8.229956 0.18969649 -43.38486      0    0   down
AARS2    1153.5493       1.624753 0.03283522  49.48202      0    0 stable
AARSD1    567.8672      -2.082289 0.02275703 -91.50088      0    0 stable

13.2 Convert from SYMBOL to ENTREZID

# Convert from SYMBOL to ENTREZID for convenient enrichment analysis later. It's crucial to do this now as a direct conversion may result in a reduced set of genes due to non-one-to-one correspondence.

# DEG_deseq2
# Retrieve gene list
gene <- rownames(DEG_deseq2)
# Perform conversion
gene = bitr(gene, fromType="SYMBOL", toType="ENTREZID", OrgDb="org.Hs.eg.db")
'select()' returned 1:many mapping between keys and columns
Warning in bitr(gene, fromType = "SYMBOL", toType = "ENTREZID", OrgDb =
"org.Hs.eg.db"): 43.37% of input gene IDs are fail to map...
# Remove duplicates and merge
gene <- dplyr::distinct(gene, SYMBOL, .keep_all=TRUE)
# Extract the SYMBOL column as a vector from the first dataset
symbols_vector <- gene$SYMBOL
# Use the SYMBOL column to filter corresponding rows from the second dataset by row names
DEG_deseq2 <- DEG_deseq2[rownames(DEG_deseq2) %in% symbols_vector, ]
head(DEG_deseq2, 5)
          baseMean log2FoldChange      lfcSE      stat pvalue padj change
A1BG      134.6084      -2.549682 0.05677219 -44.91075      0    0   down
A2M     30208.9912       3.251168 0.08394645  38.72907      0    0     up
AADACL2   801.4538      -8.229956 0.18969649 -43.38486      0    0   down
AARS2    1153.5493       1.624753 0.03283522  49.48202      0    0 stable
AARSD1    567.8672      -2.082289 0.02275703 -91.50088      0    0 stable

13.3 Select differentially expressed genes

Diff_deseq2 <- filter_diff_genes(
  DEG_deseq2, 
  p_val_col = "pvalue", 
  log_fc_col = "log2FoldChange",
  p_val_threshold = 0.05, 
  log_fc_threshold = 3
  )
# First, obtain a list of gene names from the row names of the first dataset
gene_names <- rownames(Diff_deseq2)
# Find the matching rows in the second dataframe
matched_rows <- all_count_exp[gene_names, ]
# Calculate the mean for each row
averages <- rowMeans(matched_rows, na.rm = TRUE)
# Append the averages as a new column to the first dataframe
Diff_deseq2$average <- averages
Diff_deseq2$ID <- rownames(Diff_deseq2)
Diff_deseq2$changetype <- ifelse(Diff_deseq2$change == 'up', 1, -1)
# Define a small threshold value
small_value <- .Machine$double.xmin
# Before calculating -log10, replace zeroes with the small threshold value and assign it to a new column
Diff_deseq2$log_pvalue <- ifelse(Diff_deseq2$pvalue == 0, -log10(small_value), -log10(Diff_deseq2$pvalue))
# Extract the expression data corresponding to the differentially expressed genes
heatdata_deseq2 <- all_count_exp[rownames(Diff_deseq2), ]
#head(heatdata_deseq2, 1)
set.seed(123)
# Preparing heatdata for visualization
HeatdataDeseq2 <- TransProR::process_heatdata(heatdata_deseq2, 
                                              selection = 2, 
                                              custom_names = NULL, 
                                              num_names_per_group = 60, 
                                              prefix_length = 4)
HeatdataDeseq2 <- as.matrix(HeatdataDeseq2)

library(msigdbr)
## Using the msigdbr package to download and prepare for GSVA analysis with KEGG and GO gene sets
## KEGG
KEGG_df_all <- msigdbr(species = "Homo sapiens", # Homo sapiens or Mus musculus
                        category = "C2",
                        subcategory = "CP:KEGG") 
KEGG_df <- dplyr::select(KEGG_df_all, gs_name, gs_exact_source, gene_symbol)
kegg_list <- split(KEGG_df$gene_symbol, KEGG_df$gs_name) # Grouping gene symbols by gs_name
head(kegg_list)
$KEGG_ABC_TRANSPORTERS
 [1] "ABCA1"  "ABCA10" "ABCA12" "ABCA13" "ABCA2"  "ABCA3"  "ABCA4"  "ABCA5" 
 [9] "ABCA6"  "ABCA7"  "ABCA8"  "ABCA9"  "ABCB1"  "ABCB10" "ABCB11" "ABCB11"
[17] "ABCB4"  "ABCB5"  "ABCB6"  "ABCB7"  "ABCB8"  "ABCB9"  "ABCC1"  "ABCC1" 
[25] "ABCC10" "ABCC11" "ABCC12" "ABCC2"  "ABCC3"  "ABCC4"  "ABCC5"  "ABCC6" 
[33] "ABCC6"  "ABCC8"  "ABCC9"  "ABCD1"  "ABCD2"  "ABCD3"  "ABCD4"  "ABCG1" 
[41] "ABCG2"  "ABCG4"  "ABCG5"  "ABCG8"  "CFTR"   "TAP1"   "TAP1"   "TAP1"  
[49] "TAP1"   "TAP1"   "TAP1"   "TAP1"   "TAP1"   "TAP2"   "TAP2"   "TAP2"  
[57] "TAP2"   "TAP2"   "TAP2"   "TAP2"   "TAP2"  

$KEGG_ACUTE_MYELOID_LEUKEMIA
 [1] "AKT1"     "AKT2"     "AKT3"     "AKT3"     "ARAF"     "BAD"     
 [7] "BRAF"     "CCNA1"    "CCND1"    "CEBPA"    "CHUK"     "EIF4EBP1"
[13] "FLT3"     "GRB2"     "HRAS"     "HRAS"     "IKBKB"    "IKBKG"   
[19] "JUP"      "KIT"      "KRAS"     "LEF1"     "MAP2K1"   "MAP2K2"  
[25] "MAPK1"    "MAPK3"    "MTOR"     "MYC"      "NFKB1"    "NRAS"    
[31] "PIK3CA"   "PIK3CB"   "PIK3CD"   "PIK3CG"   "PIK3R1"   "PIK3R2"  
[37] "PIK3R3"   "PIK3R5"   "PIM1"     "PIM2"     "PML"      "PPARD"   
[43] "RAF1"     "RARA"     "RELA"     "RPS6KB1"  "RPS6KB2"  "RUNX1"   
[49] "RUNX1T1"  "SOS1"     "SOS2"     "SPI1"     "STAT3"    "STAT5A"  
[55] "STAT5B"   "TCF7"     "TCF7L1"   "TCF7L2"   "ZBTB16"  

$KEGG_ADHERENS_JUNCTION
 [1] "ACP1"    "ACTB"    "ACTG1"   "ACTN1"   "ACTN2"   "ACTN3"   "ACTN4"  
 [8] "ACTN4"   "AFDN"    "BAIAP2"  "CDC42"   "CDH1"    "CREBBP"  "CSNK2A1"
[15] "CSNK2A2" "CSNK2B"  "CSNK2B"  "CSNK2B"  "CSNK2B"  "CSNK2B"  "CSNK2B" 
[22] "CSNK2B"  "CSNK2B"  "CTNNA1"  "CTNNA2"  "CTNNA3"  "CTNNB1"  "CTNND1" 
[29] "EGFR"    "EP300"   "ERBB2"   "FARP2"   "FER"     "FGFR1"   "FYN"    
[36] "IGF1R"   "INSR"    "IQGAP1"  "LEF1"    "LMO7"    "MAP3K7"  "MAPK1"  
[43] "MAPK3"   "MET"     "NECTIN1" "NECTIN2" "NECTIN3" "NECTIN4" "NLK"    
[50] "PARD3"   "PTPN1"   "PTPN6"   "PTPRB"   "PTPRF"   "PTPRJ"   "PTPRM"  
[57] "RAC1"    "RAC2"    "RAC3"    "RHOA"    "SMAD2"   "SMAD3"   "SMAD4"  
[64] "SNAI1"   "SNAI2"   "SORBS1"  "SRC"     "SSX2IP"  "TCF7"    "TCF7L1" 
[71] "TCF7L2"  "TGFBR1"  "TGFBR2"  "TJP1"    "TJP1"    "VCL"     "WAS"    
[78] "WASF1"   "WASF2"   "WASF3"   "WASL"    "YES1"   

$KEGG_ADIPOCYTOKINE_SIGNALING_PATHWAY
 [1] "ACACB"    "ACSL1"    "ACSL3"    "ACSL4"    "ACSL5"    "ACSL6"   
 [7] "ADIPOQ"   "ADIPOR1"  "ADIPOR2"  "ADIPOR2"  "AGRP"     "AKT1"    
[13] "AKT2"     "AKT3"     "AKT3"     "CAMKK1"   "CAMKK2"   "CD36"    
[19] "CHUK"     "CPT1A"    "CPT1B"    "CPT1C"    "G6PC1"    "G6PC2"   
[25] "G6PC2"    "IKBKB"    "IKBKG"    "IRS1"     "IRS2"     "IRS4"    
[31] "JAK2"     "LEP"      "LEPR"     "MAPK10"   "MAPK8"    "MAPK9"   
[37] "MTOR"     "NFKB1"    "NFKBIA"   "NFKBIB"   "NFKBIB"   "NFKBIE"  
[43] "NPY"      "PCK1"     "PCK2"     "PCK2"     "POMC"     "PPARA"   
[49] "PPARGC1A" "PRKAA1"   "PRKAA2"   "PRKAB1"   "PRKAB2"   "PRKAG1"  
[55] "PRKAG2"   "PRKAG3"   "PRKCQ"    "PTPN11"   "RELA"     "RXRA"    
[61] "RXRB"     "RXRB"     "RXRB"     "RXRB"     "RXRB"     "RXRB"    
[67] "RXRG"     "SLC2A1"   "SLC2A4"   "SLC2A4"   "SOCS3"    "STAT3"   
[73] "STK11"    "TNF"      "TNF"      "TNF"      "TNF"      "TNF"     
[79] "TNF"      "TNF"      "TNF"      "TNFRSF1A" "TNFRSF1B" "TRADD"   
[85] "TRAF2"   

$KEGG_ALANINE_ASPARTATE_AND_GLUTAMATE_METABOLISM
 [1] "ABAT"    "ACY3"    "ADSL"    "ADSS1"   "ADSS2"   "AGXT"    "AGXT2"  
 [8] "ALDH4A1" "ALDH5A1" "ASL"     "ASNS"    "ASPA"    "ASS1"    "CAD"    
[15] "CPS1"    "DDO"     "GAD1"    "GAD2"    "GFPT1"   "GFPT2"   "GLS"    
[22] "GLS2"    "GLUD1"   "GLUD2"   "GLUD2"   "GLUL"    "GOT1"    "GOT2"   
[29] "GPT"     "GPT2"    "IL4I1"   "NIT2"    "PPAT"   

$KEGG_ALDOSTERONE_REGULATED_SODIUM_REABSORPTION
 [1] "ATP1A1"   "ATP1A2"   "ATP1A3"   "ATP1A4"   "ATP1B1"   "ATP1B2"  
 [7] "ATP1B3"   "ATP1B4"   "FXYD2"    "FXYD4"    "HSD11B1"  "HSD11B2" 
[13] "IGF1"     "INS"      "INSR"     "IRS1"     "IRS2"     "IRS4"    
[19] "KCNJ1"    "KRAS"     "MAPK1"    "MAPK3"    "NEDD4L"   "NR3C2"   
[25] "PDPK1"    "PIK3CA"   "PIK3CB"   "PIK3CD"   "PIK3CG"   "PIK3R1"  
[31] "PIK3R2"   "PIK3R3"   "PIK3R5"   "PRKCA"    "PRKCB"    "PRKCG"   
[37] "SCNN1A"   "SCNN1B"   "SCNN1G"   "SFN"      "SGK1"     "SLC9A3R2"
## GO
GO_df_all <- msigdbr(species = "Homo sapiens",
                      category = "C5")  
GO_df <- dplyr::select(GO_df_all, gs_name, gene_symbol, gs_exact_source, gs_subcat)
GO_df <- GO_df[GO_df$gs_subcat != "HPO",]
go_list <- split(GO_df$gene_symbol, GO_df$gs_name) # Grouping gene symbols by gs_name
head(go_list)
$GOBP_10_FORMYLTETRAHYDROFOLATE_METABOLIC_PROCESS
[1] "AASDHPPT" "ALDH1L1"  "ALDH1L2"  "MTHFD1"   "MTHFD1L"  "MTHFD2L" 

$GOBP_2_OXOGLUTARATE_METABOLIC_PROCESS
 [1] "AADAT"  "ADHFE1" "D2HGDH" "DLST"   "GOT1"   "GOT2"   "GPT2"   "IDH1"  
 [9] "IDH2"   "IDH3B"  "KYAT3"  "L2HGDH" "MRPS36" "MRPS36" "OGDH"   "OGDHL" 
[17] "PHYH"   "TAT"   

$GOBP_2FE_2S_CLUSTER_ASSEMBLY
[1] "BOLA2"  "BOLA2B" "GLRX3"  "GLRX5"  "HSCB"   "NFS1"  

$GOBP_3_PHOSPHOADENOSINE_5_PHOSPHOSULFATE_BIOSYNTHETIC_PROCESS
[1] "PAPSS1"  "PAPSS2"  "SLC26A1" "SLC26A2" "SLC35B2" "SLC35B3"

$GOBP_3_PHOSPHOADENOSINE_5_PHOSPHOSULFATE_METABOLIC_PROCESS
 [1] "ABHD14B" "BPNT1"   "ENPP1"   "PAPSS1"  "PAPSS2"  "SLC26A1" "SLC26A2"
 [8] "SLC35B2" "SLC35B3" "SULT1A1" "SULT1A2" "SULT1A3" "SULT1A4" "SULT1B1"
[15] "SULT1C3" "SULT1C4" "SULT1E1" "SULT2A1" "SULT2B1" "TPST1"   "TPST2"  

$GOBP_3_UTR_MEDIATED_MRNA_DESTABILIZATION
 [1] "CPEB3"   "DHX36"   "DHX36"   "DND1"    "DND1"    "HNRNPD"  "KHSRP"  
 [8] "MOV10"   "PLEKHN1" "RBM24"   "RC3H1"   "TARDBP"  "TRIM71"  "UPF1"   
[15] "ZC3H12A" "ZC3H12D" "ZFP36"   "ZFP36L1" "ZFP36L2"
library(GSVA)
Warning: package 'GSVA' was built under R version 4.3.3
gsva_kegg <- gsva(expr = HeatdataDeseq2, 
                  gset.idx.list = kegg_list, 
                  kcdf = "Poisson", #"Gaussian" for logCPM, logRPKM, logTPM, "Poisson" for counts
                  verbose = TRUE
                  #parallel.sz = parallel::detectCores() # Utilize all available cores
                  )
Warning: Calling gsva(expr=., gset.idx.list=., method=., ...) is deprecated;
use a method-specific parameter object (see '?gsva').
Warning in .gsva(expr, mapped.gset.idx.list, method, kcdf, rnaseq, abs.ranking,
: Some gene sets have size one. Consider setting 'min.sz > 1'.
Estimating GSVA scores for 171 gene sets.
Estimating ECDFs with Poisson kernels

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |==                                                                    |   4%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |=================================================================     |  94%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
#write.csv(gsva_mat, "gsva_go_matrix.csv")

gsva_go <- gsva(expr = HeatdataDeseq2, 
                gset.idx.list = go_list, 
                kcdf = "Poisson", #"Gaussian" for logCPM, logRPKM, logTPM, "Poisson" for counts
                verbose = TRUE
                #parallel.sz = parallel::detectCores() # Utilize all available cores
                )
Warning: Calling gsva(expr=., gset.idx.list=., method=., ...) is deprecated;
use a method-specific parameter object (see '?gsva').
Warning: Some gene sets have size one. Consider setting 'min.sz > 1'.
Estimating GSVA scores for 7997 gene sets.
Estimating ECDFs with Poisson kernels

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |==                                                                    |   4%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |=========                                                             |  14%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |================                                                      |  24%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |=======================                                               |  34%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |==============================                                        |  44%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |=====================================                                 |  54%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |============================================                          |  64%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |===================================================                   |  74%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |==========================================================            |  84%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |=================================================================     |  94%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
#write.csv(gsva_mat, "gsva_go_matrix.csv")

ssgsea_kegg <- gsva(expr = HeatdataDeseq2, 
                    gset.idx.list = kegg_list, 
                    kcdf = "Poisson", #"Gaussian" for logCPM, logRPKM, logTPM, "Poisson" for counts
                    method = "ssgsea",
                    verbose = TRUE
                    #parallel.sz = parallel::detectCores() # Utilize all available cores
)
Warning: Calling gsva(expr=., gset.idx.list=., method=., ...) is deprecated;
use a method-specific parameter object (see '?gsva').
Warning: Some gene sets have size one. Consider setting 'min.sz > 1'.
Estimating ssGSEA scores for 171 gene sets.
[1] "Calculating ranks..."
[1] "Calculating absolute values from ranks..."

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================| 100%

[1] "Normalizing..."
#write.csv(gsva_mat, "gsva_go_matrix.csv")

ssgsea_go <- gsva(expr = HeatdataDeseq2, 
                  gset.idx.list = go_list, 
                  kcdf = "Poisson", #"Gaussian" for logCPM, logRPKM, logTPM, "Poisson" for counts
                  method = "ssgsea",
                  verbose = TRUE
                  #parallel.sz = parallel::detectCores() # Utilize all available cores
)
Warning: Calling gsva(expr=., gset.idx.list=., method=., ...) is deprecated;
use a method-specific parameter object (see '?gsva').
Warning: Some gene sets have size one. Consider setting 'min.sz > 1'.
Estimating ssGSEA scores for 7997 gene sets.
[1] "Calculating ranks..."
[1] "Calculating absolute values from ranks..."

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================| 100%

[1] "Normalizing..."
#write.csv(gsva_mat, "gsva_go_matrix.csv")
# Data transformation for GSVA of KEGG
#### Differential analysis using limma ####

# Assigning group labels for tumor and normal samples
group <- c(rep('tumor', 60), rep('normal', 60))
group <- factor(group, levels = c("normal", "tumor"))

# Constructing the design matrix
design <- model.matrix(~0 + factor(group))
colnames(design) <- levels(factor(group))
rownames(design) <- colnames(gsva_kegg)

# Forming the contrast matrix for differential analysis
con <- paste(rev(levels(group)), collapse = "-")
cont.matrix <- limma::makeContrasts(contrasts = c(con), levels = design)

# Fitting the linear model
fit <- limma::lmFit(gsva_kegg, design)
fit2 <- limma::contrasts.fit(fit, cont.matrix)
fit2 <- limma::eBayes(fit2)

# Retrieving differential expression results
tempOutput <- limma::topTable(fit2, coef = con, n = Inf)
gsva_kegg_limma <- stats::na.omit(tempOutput)

#### Visualization of differential analysis results from GSVA ####
## Setting filtering thresholds
padj_cutoff = 0.05
log2FC_cutoff = log2(2)

# Filtering significant gene sets
keep <- rownames(gsva_kegg_limma[gsva_kegg_limma$adj.P.Val < padj_cutoff & abs(gsva_kegg_limma$logFC) > log2FC_cutoff, ])
length(keep)
[1] 14
# Extracting the top 10 elements from the limma analysis results
keep_top10 <- keep[1:10]
# Selecting rows in gsva_kegg that match the top 10 genes from keep
gsva_kegg_limma_head10 <- gsva_kegg[rownames(gsva_kegg) %in% keep_top10, ]

# Checking if row names are set, assuming the first column as row names if not set
if (is.null(rownames(gsva_kegg_limma_head10))) {
  gsva_kegg_limma_head10 <- gsva_kegg_limma_head10 %>%
    mutate(RowID = row_number()) %>%  # Adding a row number for each row
    column_to_rownames(var="RowID")  # Setting this row number as row names
}

gsva_kegg_limma_head10 <- as.data.frame(gsva_kegg_limma_head10)

# To preserve row names in the long format data, first convert row names into a column of the data frame
gsva_kegg_limma_head10 <- gsva_kegg_limma_head10 %>%
  tibble::rownames_to_column(var = "Gene")  # Convert row names to a column named Gene

# Transforming the dataframe from wide to long format, including the newly added row names column
gsva_kegg_HeatdataDeseq <- gsva_kegg_limma_head10 %>%
  pivot_longer(
    cols = -Gene,    # Transforming all columns except the Gene column
    names_to = "Sample",  # Creating a new column to store the original column names
    values_to = "value"   # Creating a new column to store the values
  )

# Displaying the transformed data
print(gsva_kegg_HeatdataDeseq)
# A tibble: 1,200 × 3
   Gene                                             Sample    value
   <chr>                                            <chr>     <dbl>
 1 KEGG_AMINO_SUGAR_AND_NUCLEOTIDE_SUGAR_METABOLISM TCGA_1   0.935 
 2 KEGG_AMINO_SUGAR_AND_NUCLEOTIDE_SUGAR_METABOLISM TCGA_2   0.782 
 3 KEGG_AMINO_SUGAR_AND_NUCLEOTIDE_SUGAR_METABOLISM TCGA_3  -0.0794
 4 KEGG_AMINO_SUGAR_AND_NUCLEOTIDE_SUGAR_METABOLISM TCGA_4   0.257 
 5 KEGG_AMINO_SUGAR_AND_NUCLEOTIDE_SUGAR_METABOLISM TCGA_5   0.565 
 6 KEGG_AMINO_SUGAR_AND_NUCLEOTIDE_SUGAR_METABOLISM TCGA_6  -0.0225
 7 KEGG_AMINO_SUGAR_AND_NUCLEOTIDE_SUGAR_METABOLISM TCGA_7   0.661 
 8 KEGG_AMINO_SUGAR_AND_NUCLEOTIDE_SUGAR_METABOLISM TCGA_8   0.867 
 9 KEGG_AMINO_SUGAR_AND_NUCLEOTIDE_SUGAR_METABOLISM TCGA_9   0.249 
10 KEGG_AMINO_SUGAR_AND_NUCLEOTIDE_SUGAR_METABOLISM TCGA_10  0.753 
# ℹ 1,190 more rows
# Subsetting the first 10 rows from HeatdataDeseq2
HeatdataDeseq3 <- HeatdataDeseq2[1:10,]

# Applying logarithmic transformation and converting to a data frame
HeatdataDeseq3 <- as.data.frame(log_transform(HeatdataDeseq3))
[1] "log2 transform finished"
# Check if row names are set, if not, assume the first column as row names
if (is.null(rownames(HeatdataDeseq3))) {
  HeatdataDeseq3 <- HeatdataDeseq3 %>%
    mutate(RowID = row_number()) %>%  # Add a row number to each row
    column_to_rownames(var="RowID")   # Set this row number as row names
}

# To retain row names in a long format, first convert row names to a column in the data frame
HeatdataDeseq3 <- HeatdataDeseq3 %>%
  tibble::rownames_to_column(var = "Gene")  # Convert row names to a column named 'Gene'

# Transforming the data frame from wide to long format, including the newly added row name column
long_format_HeatdataDeseq <- HeatdataDeseq3 %>%
  pivot_longer(
    cols = -Gene,          # Transform all columns except for 'Gene'
    names_to = "Sample",   # Create a new column to store original column names
    values_to = "value"    # Create a new column to store values
  )

# Display the transformed data
print(long_format_HeatdataDeseq)
# A tibble: 1,200 × 3
   Gene  Sample  value
   <chr> <chr>   <dbl>
 1 A2M   TCGA_1   14.4
 2 A2M   TCGA_2   15.1
 3 A2M   TCGA_3   16.1
 4 A2M   TCGA_4   18.8
 5 A2M   TCGA_5   16.0
 6 A2M   TCGA_6   18.6
 7 A2M   TCGA_7   17.4
 8 A2M   TCGA_8   13.0
 9 A2M   TCGA_9   18.0
10 A2M   TCGA_10  13.3
# ℹ 1,190 more rows
# Selecting the top 10 rows from ssgsea_kegg for display
ssgsea_kegg_head10 <- as.data.frame(ssgsea_kegg[1:10,])

# Check if row names are set, if not, assume the first column as row names
if (is.null(rownames(ssgsea_kegg_head10))) {
  ssgsea_kegg_head10 <- ssgsea_kegg_head10 %>%
    mutate(RowID = row_number()) %>%  # Add a row number to each row
    column_to_rownames(var="RowID")   # Set this row number as row names
}

ssgsea_kegg_head10 <- as.data.frame(ssgsea_kegg_head10)

# To preserve row names in the long format data, first convert row names into a column of the data frame
ssgsea_kegg_head10 <- ssgsea_kegg_head10 %>%
  tibble::rownames_to_column(var = "Gene")  # Convert row names to a column named Gene

# Transforming the data frame from wide to long format, including the newly added row name column
ssgsea_kegg_HeatdataDeseq <- ssgsea_kegg_head10 %>%
  pivot_longer(
    cols = -Gene,          # Transform all columns except for 'Gene'
    names_to = "Sample",   # Create a new column to store original column names
    values_to = "value"    # Create a new column to store values
  )

# Display the transformed data
print(ssgsea_kegg_HeatdataDeseq)
# A tibble: 1,200 × 3
   Gene                  Sample   value
   <chr>                 <chr>    <dbl>
 1 KEGG_ABC_TRANSPORTERS TCGA_1  0.181 
 2 KEGG_ABC_TRANSPORTERS TCGA_2  0.207 
 3 KEGG_ABC_TRANSPORTERS TCGA_3  0.178 
 4 KEGG_ABC_TRANSPORTERS TCGA_4  0.258 
 5 KEGG_ABC_TRANSPORTERS TCGA_5  0.218 
 6 KEGG_ABC_TRANSPORTERS TCGA_6  0.237 
 7 KEGG_ABC_TRANSPORTERS TCGA_7  0.0993
 8 KEGG_ABC_TRANSPORTERS TCGA_8  0.245 
 9 KEGG_ABC_TRANSPORTERS TCGA_9  0.178 
10 KEGG_ABC_TRANSPORTERS TCGA_10 0.212 
# ℹ 1,190 more rows
values=c("#491588", "#301b8d", "#1a237a", "#11479c", "#0a5797","#0b5f63","#074d41","#1f5e27","#366928","#827729","#f28131","#fc7226","#e3551e","#bd3a1b", "#c62828", "#ad1457", "#6a1b9a")
row.names(HeatdataDeseq3)<- HeatdataDeseq3$Gene
HeatdataDeseq3$Gene <- NULL
iindata<-as.data.frame(t(HeatdataDeseq3))
heatdata.clust_new<-hclust(dist(iindata,method = "euclidean"),method = "complete")

p1=ggtree(heatdata.clust_new,branch.length='none',layout="circular",size=0.3, xlim=c(30,NA))
#p1
p0<-p1+geom_text(aes(label=node))##watch node

p0

p1<-p1+geom_hilight(node=137, fill = "#491588", alpha = .4, extend =107)+
  geom_hilight(node=157, fill = "#1976d2", alpha = .3, extend =107)+
  geom_hilight(node=161, fill = "#1976d2", alpha = .3, extend =106)+
  geom_hilight(node=179, fill = "#388e3c", alpha = .4, extend =107)+
  geom_hilight(node=127, fill = "#f9a825", alpha = .3, extend =107)+
  geom_hilight(node=150, fill = "#491588", alpha = .4, extend =105)+
  geom_hilight(node=128, fill = "#1976d2", alpha = .3, extend =107)+
  geom_hilight(node=189, fill = "#388e3c", alpha = .4, extend =105)+
  geom_hilight(node=131, fill = "#f9a825", alpha = .3, extend =106)

p1

gene_colors <- c("#491588", "#301b8d", "#1a237a", "#11479c", "#0a5797","#0b5f63","#074d41","#1f5e27","#366928","#827729")
p2 <- create_base_plot(p1, long_format_HeatdataDeseq, gene_colors)

p2

# Add a boxplot layer to the base plot
p3 <- add_boxplot(p2, long_format_HeatdataDeseq)

p3

gene_colors <- c("#491588", "#301b8d", "#1a237a", "#11479c", "#0a5797","#0b5f63","#074d41","#1f5e27","#366928","#827729")
# Add a new tile layer for SSGSEA data
p4 <- add_new_tile_layer(p3, ssgsea_kegg_HeatdataDeseq, gene_colors, "Ssgsea Term")

p4

# Add another boxplot layer with specific aesthetic adjustments
p5 <- add_boxplot(p4, ssgsea_kegg_HeatdataDeseq, fill_color="#f28131", alpha=0.65, offset=0.32)

p5

gene_colors <- c("#491588", "#301b8d", "#1a237a", "#11479c", "#0a5797","#0b5f63","#074d41","#1f5e27","#366928","#827729")
# Add a new tile layer for GSVA data with specific alpha and offset adjustments
p6 <- add_new_tile_layer(p5, gsva_kegg_HeatdataDeseq, gene_colors, "Gsva Term", alpha_value=c(0.3, 0.9), offset=0.02)

p6

gene_colors <- c("#491588", "#301b8d", "#1a237a", "#11479c", "#0a5797","#0b5f63","#074d41","#1f5e27","#366928","#827729")
p7 <- circos_fruits(p1,
                            long_format_HeatdataDeseq,
                            ssgsea_kegg_HeatdataDeseq,
                            gsva_kegg_HeatdataDeseq,
                            gene_colors)
p_final <- p7 + geom_strip(25, 114, label="TCGA_GTEX", offset=100, barsize=2, extend=1, color="#9575cd", alpha=0.5, hjust="center", align=TRUE, offset.text=19, angle=-90)


print(p_final)

13.4 Reference

  • ggplot2:

github:ggplot2

An implementation of the Grammar of Graphics in R

  • ggtreeExtra:

S Xu, Z Dai, P Guo, X Fu, S Liu, L Zhou, W Tang, T Feng, M Chen, L Zhan, T Wu, E Hu, Y Jiang, X Bo and G Yu*. ggtreeExtra: Compact visualization of richly annotated phylogenetic data. Molecular Biology and Evolution. 2021, 38(9):4039-4042.

  • ggtree:

G Yu, DK Smith, H Zhu, Y Guan, TTY Lam*. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. Methods in Ecology and Evolution. 2017, 8(1):28-36.

  • clusterProfiler 4.0:

T Wu#, E Hu#, S Xu, M Chen, P Guo, Z Dai, T Feng, L Zhou, W Tang, L Zhan, X Fu, S Liu, X Bo, G Yu. clusterProfiler 4.0: A universal enrichment tool for interpreting omics data. The Innovation. 2021, 2(3):100141.