Hello Sir/madam,
I m using heatmap.2 function from gplots package of R to plot the heatmap of DEGs.
I have total 22 samples (10 normal and 12 treated). I have the heatmap (master heatmap) of all these samples, but the samples of interest are treated one (12 samples), so the genes which are upregulated in those 12 samples i want to extract from the master heatmap.
I have clustered the genes and samples using hierarchical clustring (hclust).
Now i want to extract the subclusters (up regulated in 12 samples) from the hclust object and for this i have used cutree function.
I have used this link to obtain the subcluster How To Get The Subclusters From The Object Of Hclust() Using Cutree() According To The Order On The Map Produced By Heatmap.2? But i want this subcluster in the form of matrix and so then i can plot this subcluster matrix again as a hetamap.
I have refered to the heatmap.2 healp page and found the subclustering code. the code is as follows,
## plot a sub-cluster using the same color coding as for the full heatmap
full <- heatmap.2(x)
heatmap.2(x, Colv=full$colDendrogram[[2]], breaks=full$breaks) # column subset
heatmap.2(x, Rowv=full$rowDendrogram[[1]], breaks=full$breaks) # row subset
heatmap.2(x, Colv=full$colDendrogram[[2]],
Rowv=full$rowDendrogram[[1]], breaks=full$breaks) # both
My question is ,
- How to extract that subcluster matrix? using R code.Can anyone provide such sample R code.
- If i plot this subcluster matrix using heatmap.2, will the pattern i have obtained in the master heatmap change in this subcluster one? if it changes then why does it change? please someone help me with this, i'm not understanding this.
Hello Kevin Blighe,
Thank so much for the reply, Yes i have referred to the ComplexHeatmap package tutorials.
I could extract the subcluster matrix following this code given in the link, http://r.789695.n4.nabble.com/Advice-on-exploration-of-sub-clusters-in-hierarchical-dendrogram-td4414277.html And now i have the heatmap with me too for this subcluster matrix.
referring to my last Question (If i plot this subcluster matrix using heatmap.2, will the pattern i have obtained in the master heatmap change in this subcluster one?)
The heatmap I have generated is not as same as it looks in the master heatmap. for this subcluster heatmap i have used heatmap.2 function with default options.
so from the heatmap images, i can see that the observed pattern is not same in the subcluster heatmp. So is it right to have change in pattern? can you explain me this problem?
here is the code i have used to subcluster,
hr3 <- hclust(as.dist(1-cor(t(vv_E), method="pearson")),method="complete")
hc3 <- hclust(as.dist(1-cor(vv_E, method="spearman")), method="complete")
#heatmap after the column and row side colors
heatmap.2(vv_E, main="Hierarchical Cluster", Rowv=as.dendrogram(hr3), Colv=as.dendrogram(hc3), dendrogram="both", scale = "row", col=colorpanel(75,"green","black","red"), density.info="none", trace="none", RowSideColors= myClusterSideBar,ColSideColors = colSidecol,colsep=c(10), rowsep=c(292), sepcolor='yellow')
#subclustering using http://r.789695.n4.nabble.com/Advice-on-exploration-of-sub-clusters-in-hierarchical-dendrogram-td4414277.html
#Advice on exploration of sub-clusters in hierarchical dendrogram
# for column
#column clustering (sample)
x_col <- as.dendrogram(hc3)
plot(x_col, xlab="sample culster") #visualization of the dendrogram
clusters<-cutree(hc3, h=0.6) #obtain clusters at cutoff height=0.6
ord<-cmdscale(as.dist(1-cor(vv_E)), k=2) #Multidimensional scaling of the data down to 2 dimensions
#row clusteirng (gene )
x_row <- as.dendrogram(hr3)
library(dendextend)
plot(x_row, xlab="gene cluster") #visualization of the dendrogram
cluster_gene <- cutree(hr3, h=1.8)
d2=color_branches(x_row,k=2)
plot(d2)
ord_gene <- cmdscale(as.dist(1-cor(t(vv_E))),k=2)
library(cluster)
clusplot(ord,clusters, color=TRUE, shade=TRUE,labels=5, lines=0) #visualization of the clusters in 2D map
clusplot(ord_gene,cluster_gene, color = TRUE, labels=4,lines=0)
#extract cluster memberships for samples:
clids = as.data.frame(clusters)
names(clids) = c("id")
clids
clids$cdr = row.names(clids)
clids$cdr
row.names(clids)
row.names(clids) = c(1:dim(clids)[1])
row.names(clids)
clstructure = lapply(unique(clids$id), function(x){clids[clids$id == x,'cdr']})
clstructure
clstructure[[1]] #get memberships of cluster 1
clstructure[[2]] #get memberships of cluster 2
##extract cluster memberships for genes
clids_gene = as.data.frame(cluster_gene)
clids_gene
names(clids_gene) = c("id_gene")
head(clids_gene)
clids_gene$cdr = row.names(clids_gene)
clids_gene$cdr
row.names(clids_gene)
clstructure_genes = lapply(unique(clids_gene$id_gene), function(x){clids_gene[clids_gene$id_gene == x,'cdr']})
clstructure_genes
clstructure_genes[[1]] #get memberships of cluster 1
clstructure_genes[[2]] #get memberships of cluster 2
length(clstructure_genes[[2]])
#to get the expression set of only samples of WZ and GEF.
cluster_WZ_GR_samples_genes <-cluster_gene_WZ_GR[,clstructure[[2]]]
cluster_WZ_GR_samples_genes
dim(cluster_WZ_GR_samples_genes)
colnames(cluster_WZ_GR_samples_genes)
write.table(cluster_WZ_GR_samples_genes, file = "Upper_Right_side_genes_291_samples_12.txt",sep="\t")
heatmap.2(cluster_WZ_GR_samples_genes,main="Hierarchical Cluster of 291 genes and 12 samples", dendrogram="both", scale = "row", col=colorpanel(75,"green","black","red"), density.info="none", trace="none")