---
title: "iCluster analysis of glioblastoma multiforme multi-omics data"
author: 
  name: "Qianxing Mo"
  affiliation: "Department of Biostatistics and Bioinformatics <br> 
  H. Lee Moffitt Cancer Center and Research Institute, Tampa, FL 33612"
output:
  html_document:
    theme: united
    df_print: kable
date: 'Compiled: `r format(Sys.Date(), "%B %d, %Y")`'
---

```{r setup, include=FALSE}
all_times <- list()  # store the time for each chunk
knitr::knit_hooks$set(time_it = local({
  now <- NULL
  function(before, options) {
    if (before) {
      now <<- Sys.time()
    } else {
      res <- difftime(Sys.time(), now)
      all_times[[options$label]] <<- res
    }
  }
}))
knitr::opts_chunk$set(
  message = FALSE,
  warning = FALSE,
  time_it = TRUE,
  error = TRUE
)
```
This tutorial aims to illustrate iClusterBayes2, an updated [iClusterBayes](https://academic.oup.com/biostatistics/article/19/1/71/3852318) function for identifying cancer molecular subtypes based on multi-omics data. We use the TCGA glioblastoma (GBM) multi-omics data generated by The Cancer Genome Atlas (TCGA) project ([TCGA 2018](https://pubmed.ncbi.nlm.nih.gov/18772890/)) as an example.  The level-3 GBM multi-omics data are available at the [TCGA Public Portal](http://firebrowse.org) and the [cBio Cancer Genomics Portal](http://cbioportal.org/).  The GBM dataset consists of somatic mutation, DNA copy number, and microarray gene expression data for 84 GBM primary samples.  The somatic mutation data are available for 306 selected genes in which 1 and 0 are used to indicate a gene with mutation and no mutation, respectively.  The level-3 DNA copy number data are made of 16295 chromosome segment means (output of R package DNAcopy) for the 84 samples.  The mRNA expression data are made of 1740 unified most variable genes across three microarray platforms (Affymetrix Human Exon 1.0 ST GeneChips, Affymetrix HT-HG-U133A GeneChips, and custom designed Agilent 244K array), which are detailed in [Verhaak et al. 2010](https://pubmed.ncbi.nlm.nih.gov/20129251/). 

In general, the iCluster analysis of multi-omics data can be carried out in the following steps.  
```{r packages, cache=FALSE}
library(iClusterPlus)
library(patchwork)
library(dplyr)
library(lattice)
library(umap)
library(ggplot2)
library(gplots)
library(knitr)
```
# Step 0: Prepare multi-omics data for iCluster analysis 
For the somatic mutation data (1: mutation, 0: no mutation), genes with low-frequency mutations contribute little to sample clustering.  Therefore, we recommend filtering out genes with too few mutations for iCluster analysis (e.g., < 2%). 
```{r mutation data filtering}
data(gbm)
gbm.mut2 <- gbm.mut[,apply(gbm.mut,2,mean) > 0.02]
```

For copy number data, we use function CNregions to merge the copy number regions with similar neighboring segment means.  We suggest reducing the GBM copy number regions to 5,000-6,000 regions. 
```{r compressing CNV segments}
gbm.cn <- CNregions(seg=gbm.seg,epsilon=0,frac.overlap=0.5, rmSmallseg=TRUE,nProbes=5)
dim(gbm.cn)

# order the samples 
gbm.cn <- gbm.cn[order(rownames(gbm.cn)),]
# the sample names for the three data sets should be in the same order 
all(rownames(gbm.cn)==rownames(gbm.exp))
all(rownames(gbm.cn)==rownames(gbm.mut2))
```

For gene expression data, we recommend performing variance (or coefficient of variation) filtering to select the top most variable genes (e.g. 1000 - 5000 genes) for iCluster analysis.  For example, we use the top 1740 unified most variable genes as described above for our iCluster analysis.  

# Step 1: Make PCA variance plot to determine the dimension of latent factor used for iCluster modeling.
The iCluster methods are based on latent factor model.  We suggest using PCA variance plot to determine the number of latent factors used for iCluster modeling.  

```{r data procession, fig.height=5,fig.width=10}
# PCA variance plot for continuous multi-omics data 
pcaVarPlot(xList=list(gbm.cn,gbm.exp),K=50)
```

From the PCA variance plot, it can be seen that the rate of decrease of the explained variance starts to slow down significantly around the 5th PC.  An optimal number of latent factors used for iCluster modeling can be chosen around the change point where the rate of decrease of the explained variance of the PCs starts to slow down significantly.  As an example, here, we choose K=5 as the number of latent factors for iCluster modeling of the GBM multi-omics data.  It can be found that a few latent variables (e.g., K=3-7) are usually sufficient for identification of cancer subtypes.  In addition, the [iClusterBayes](https://pubmed.ncbi.nlm.nih.gov/28541380/) method is a Bayesian integrative clustering method.  A Markov chain Monte Carlo (MCMC) algorithm is used for the model parameter estimation and feature selection, which are computationally intensive.  To reduce computation time and make the MCMC sampling more efficient, it is optimal to use a small number of factors (e.g., K < 8) for modeling.  Otherwise, the user will need to tune the iClusterBayes parameters (e.g., sdev and beta.var.scale) to make the MCMC chains mix well. 

# Step 2: Perform iCluster modeling 
The iCluster modeling generates a latent factor matrix (meanZ), which capture the major sources of variation of the multi-omics data.  As a result, the latent factor matrix can be used for sample clustering and visualization.
```{r iClusterBayes analysis}
set.seed(321)
date()
icfit <- iClusterBayes2(xList=list(gbm.mut2,gbm.cn,gbm.exp),type=c("binomial","gaussian","gaussian"),
                       K=5,n.burnin=2000,n.draw=12000,prior.gamma=c(0.5,0.5,0.5),sdev=0.05,thin=2)
date()
```

After the modeling, we need to check the distributions of the posterior probabilities (PPs) of the model parameters being selected.  In general, if the PPs spread in the region of [0, 1], it indicates that the MCMC sampling work.  if the PPs concentrate around 0, it indicates that the MCMC sampling may not work. The user will need to tune the sampling parameters sdev and beta.var.scale for the MCMC sampling.  
```{r check PPs, fig.width=10,fig.height=5}
par(mfrow=c(1,3))
hist(icfit$beta.pp[[1]], col="yellow")
hist(icfit$beta.pp[[2]], col="blue")
hist(icfit$beta.pp[[3]], col="green")
```

# Step 3: Visualize sample clusters on a 2-dimensional space
Before clustering analysis, we project the samples to a 2-dimensional space to estimate the number of clusters. 
```{r UMAP, fig.width=6,fig.height=6}
## clustering of cells using Louvain method and the latent factor matrix generated by iCluster
gbm.umap <- umap(icfit$meanZ)
colnames(gbm.umap$layout) <- c("UMAP1","UMAP2")
plot(gbm.umap$layout)
```

# Step 4: Perform sample clustering
From the UMAP plot, it seems that there are 3-4 major clusters.  Therefore, we perform k-means clustering for the GBM samples using the latent factor matrix meanZ and let the number of clusters be 3-4. The user can also try other clustering methods (e.g., k-medoids) on the latent factor matrix meanZ.  
```{r kmeans clustering, fig.width=10,fig.height=5}
km3 <- kmeans(icfit$meanZ, 3, nstart = 100)
km4 <- kmeans(icfit$meanZ, 4, nstart = 100)
gbm.clusters <- data.frame(gbm.umap$layout,ic3=as.factor(km3$cluster), ic4=as.factor(km4$cluster)) 
p1 <- ggplot(gbm.clusters, aes(x=UMAP1, y=UMAP2, col=ic3)) + geom_point(size = 3) + theme_bw()
p2 <- ggplot(gbm.clusters, aes(x=UMAP1, y=UMAP2, col=ic4)) + geom_point(size = 3) + theme_bw()
p1 + p2
```

# Step 5: Draw the heatmaps of the sample clusters 
It is necessary to make heatmaps of the multi-omics data to check if the clusters are biologically meaningful. We will make the heatmaps for the 3 and 4 clusters separately.  On the following heatmaps, the columns are the samples ordered by the clusters and the rows are the features ordered by the clustering methods specified by the plotHMBayes function.    

Figure A: Heatmap of the 3 iClusters. 
```{r heatmap for 3 clusters, fig.height=8, fig.width = 8}
##### plot heatmaps #######
chr <- unlist(strsplit(colnames(gbm.cn),"\\."))
chr <- chr[seq(1,length(chr),by=2)]
chr <- gsub("chr","",chr)
chr <- as.numeric(chr)

bw.col <- colorpanel(2,low="white",high="black")
col.scheme <- alist()
col.scheme[[1]] <- bw.col
col.scheme[[2]] <- bluered(256)
col.scheme[[3]] <- bluered(256)

icfit$clusters <- gbm.clusters$ic3
hm3c <- plotHMBayes(fit=icfit, xList=list(gbm.mut2,gbm.cn,gbm.exp), type=c("binomial","gaussian","gaussian"),
                   sample.order=NULL, feature.order=c(F,F,T),dist.method="euclidean",hclust.method="ward.D",
                   sparse=c(T,T,T), threshold=c(0.5,0.5,0.5), feature.scale=c(F,F,T), 
                   col.scheme=col.scheme,chr=chr,plot.chr=c(F,T,F),cap=c(0,0.99,0.99))
```

Figure B: Heatmap of the 4 iClusters. 
```{r heatmap for 4 clusters, fig.height=8, fig.width = 8}
icfit$clusters <- gbm.clusters$ic4
hm4c <- plotHMBayes(fit=icfit, xList=list(gbm.mut2,gbm.cn,gbm.exp), type=c("binomial","gaussian","gaussian"),
                   sample.order=NULL, feature.order=c(F,F,T), dist.method="euclidean", hclust.method="ward.D",
                   sparse=c(T,T,T), threshold=c(0.5,0.5,0.5), feature.scale=c(F,F,T), 
                   col.scheme=col.scheme,chr=chr,plot.chr=c(F,T,F),cap=c(0,0.99,0.99))
```

Last, we compare the 3 and 4 iClusters.
```{r survival analysis, fig.width=8}
table(gbm.clusters$ic3, gbm.clusters$ic4)
```
We can see that the samples characterized by normal chromosome 7 are grouped into the same cluster in the 3-cluster solution (Figure A) and the 4-cluster solution (Figure B).  The samples characterized by amplified chromosome 7 are divided into 2 clusters (Figure A) and 3 clusters in Figure B, respectively.

<details>
  <summary>**Session Info**</summary>
```{r}
sessionInfo()
```
</details>


