Loading libraries

library(Matrix)
library(pheatmap)
library(RColorBrewer)
library(ggplot2)
set.seed(43)

Functions

#' Function to return object for ggplot2 plotting
#' @param size_text Size of the text to use for plotting
#' @param leg_pos Position of legend
#' @return ggplot theme object
theme_plot <- function(size_text=15,
                           leg_pos="right",
                           size_text_titles=NULL,
                           classic=FALSE,
                           line_size=0.85,
                           line_size_color="black",
                           axis_ticks_length=0.25,
                           axis_ticks_color="black",
                           rotate_x_axis=TRUE
                           ){
    require(ggplot2)
    if( is.null(size_text_titles) ){
        size_text_titles<- size_text + 2
    }
    if( classic ){
        gg.plots<- theme_classic()
    }else{
        gg.plots<- theme_bw()
    }
    gg.plots <- gg.plots +
        theme(axis.line=element_line(linewidth=line_size, colour=line_size_color),
              axis.ticks=element_line(colour=axis_ticks_color, linewidth=line_size),
              axis.ticks.length=unit(axis_ticks_length, "cm"),
              axis.title.x=element_text(size=size_text_titles),
              legend.position=leg_pos,
              plot.title=element_text(hjust=0.5, size=size_text),
              axis.text.y=element_text(size=size_text, color="black"),
              axis.title.y=element_text(size=size_text_titles) )
    if( rotate_x_axis) {
        gg.plots<- gg.plots +
            theme(axis.text.x=element_text(size=size_text, angle=45, hjust=1, color=line_size_color) )
    }else{
        gg.plots<- gg.plots +
            theme(axis.text.x = element_text(size=size_text, angle = 90, vjust = 0.5, hjust=1, color=line_size_color))
    }
}

pheatmap.colorsymmetric <- function(x,lim=NULL,...)
{
    require(pheatmap)
    if(is.null(lim)){
        lim <- max(abs(x), na.rm=TRUE)
        if( min(x, na.rm=TRUE) < 0 ){
            lim_down<- -lim
            col_palette<- colorRampPalette(c("blue","white","red"))(256)
        }else{        
            lim_down<- 0
            col_palette<- colorRampPalette(c("white","red"))(256)
        }
    }else{
        lim <- 1
        lim_down<- -1
        col_palette<- colorRampPalette(c("blue","white","red"))(256)        
    }
    pheatmap(
        x, color = col_palette,
        breaks=seq(lim_down,lim,length.out=255),
        #clustering_method="ward.D2",
        ... )
}

Loading data

The TCGA data can be found at Zenodo.

# Reading SCE object
lis_tcga<- readRDS("lis_tcga.rds")

meta<- lis_tcga$meta
norm_data<- lis_tcga$norm_data

Heatmap of clusters

meta$RNA_snn_res.4<- as.character(meta$RNA_snn_res.4)
snn_clusters <- unique(meta$RNA_snn_res.4)
cluster_means <- sapply( snn_clusters, function(x) {
  apply(
      norm_data[,which(meta$RNA_snn_res.4==x)], 1, mean )
} )
colnames(cluster_means) <- snn_clusters
cluster_means <- cluster_means[,order(snn_clusters)]

# annotation
sm_annot<- unique(meta[,c("RNA_snn_res.4", "Molecular_clusters")])
# Set cluster 18 as benign-like
sm_annot<- sm_annot[!(sm_annot$RNA_snn_res.4 == 18 & sm_annot$Molecular_clusters == "Normal"),]
rownames(sm_annot)<- sm_annot$RNA_snn_res.4
sm_annot<- sm_annot[,"Molecular_clusters", drop=F]
pheatmap.colorsymmetric(
    cor(t(scale(t(cluster_means),center=T,scale=F))),
    clustering_method = "mcquitty",
    clustering_distance_rows = "correlation",
    annotation_row = sm_annot,    
    clustering_distance_cols = "correlation")

Barplot

freq_mat<- table(meta$RNA_snn_res.4, meta$Subtype)
freq_per<-  apply( freq_mat, 1, function(X) X*100/sum(X) )

df<- reshape2::melt(freq_per)
df$Var2<- factor(as.character(df$Var2), levels=rev(c("22", "10", "16", "17", "13", "1", "3", "15", "4", "21", "7", "9", "6", "19", "5", "8", "18", "20", "14", "0", "12", "2", "11")))

vec_colors<- c("Normal"="#CCA975", "ccRCC"= "#cc79a7", "pRCC" = "#f0e442",  "chRCC"= "#009e73")

ggplot(df, aes(Var2, value, fill=Var1) )+
    geom_bar(position="stack", stat="identity") +
    coord_flip() +
    scale_fill_manual(values=vec_colors) +
    theme_plot(classic=T, rotate_x_axis=F)

sessionInfo()
## R version 4.3.1 (2023-06-16)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: AlmaLinux 9.6 (Sage Margay)
## 
## Matrix products: default
## BLAS/LAPACK: FlexiBLAS IMKL;  LAPACK version 3.11.0
## 
## locale:
##  [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8    
##  [5] LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8   
##  [7] LC_PAPER=en_CA.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C       
## 
## time zone: Canada/Eastern
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] ggplot2_3.5.2      RColorBrewer_1.1-3 pheatmap_1.0.12    Matrix_1.6-4      
## 
## loaded via a namespace (and not attached):
##  [1] gtable_0.3.4     jsonlite_1.8.8   dplyr_1.1.4      compiler_4.3.1  
##  [5] highr_0.10       Rcpp_1.0.12      tidyselect_1.2.0 stringr_1.5.1   
##  [9] jquerylib_0.1.4  scales_1.3.0     yaml_2.3.8       fastmap_1.1.1   
## [13] lattice_0.22-5   R6_2.5.1         plyr_1.8.9       labeling_0.4.3  
## [17] generics_0.1.3   knitr_1.45       tibble_3.2.1     munsell_0.5.0   
## [21] bslib_0.6.1      pillar_1.9.0     rlang_1.1.6      utf8_1.2.4      
## [25] stringi_1.8.3    cachem_1.0.8     xfun_0.41        sass_0.4.8      
## [29] cli_3.6.2        withr_2.5.2      magrittr_2.0.3   digest_0.6.33   
## [33] grid_4.3.1       lifecycle_1.0.4  vctrs_0.6.5      evaluate_0.23   
## [37] glue_1.7.0       farver_2.1.1     fansi_1.0.6      colorspace_2.1-0
## [41] reshape2_1.4.4   rmarkdown_2.29   tools_4.3.1      pkgconfig_2.0.3 
## [45] htmltools_0.5.7