navinlabcode / copykat

Other
193 stars 53 forks source link

Heatmap cannot be plotted #95

Open Yijia-Jiang opened 1 year ago

Yijia-Jiang commented 1 year ago

Hi, I am using the code provided in the tutorial and cannot to generate both heatmaps using the following code

heatmap.3(t(as.matrix(CNA.test[,4:ncol(CNA.test)])),dendrogram="r", distfun = function(x) parallelDist::parDist(x,threads =4, method = "euclidean"), hclustfun = function(x) hclust(x, method="ward.D2"),
            ColSideColors=chr1,RowSideColors=cells,Colv=NA, Rowv=TRUE,
            notecol="black",col=my_palette,breaks=col_breaks, key=TRUE,
            keysize=1, density.info="none", trace="none",
            cexRow=0.1,cexCol=0.1,cex.main=1,cex.lab=0.1,
            symm=F,symkey=F,symbreaks=T,cex=1, cex.main=4, margins=c(10,10))

  legend("topright", paste("pred.",names(table(com.preN)),sep=""), pch=15,col=RColorBrewer::brewer.pal(n = 8, name = "Dark2")[2:1], cex=0.6, bty="n")

The error shows:

Error in heatmap.3(t(as.matrix(CNA.test[, 4:ncol(CNA.test)])), dendrogram = "r", : `x' must be a numeric matrix

I also tried code to plot tumor heatmap:

heatmap.3(t(tumor.mat),dendrogram="r", distfun = function(x) parallelDist::parDist(x,threads =4, method = "euclidean"), hclustfun = function(x) hclust(x, method="ward.D2"),
            ColSideColors=chr1,RowSideColors=cells,Colv=NA, Rowv=TRUE,
            notecol="black",col=my_palette,breaks=col_breaks, key=TRUE,
            keysize=1, density.info="none", trace="none",
            cexRow=0.1,cexCol=0.1,cex.main=1,cex.lab=0.1,
            symm=F,symkey=F,symbreaks=T,cex=1, cex.main=4, margins=c(10,10))

  legend("topright", c("c1","c2"), pch=15,col=RColorBrewer::brewer.pal(n = 8, name = "Dark2")[3:4], cex=0.9, bty='n')

Another error occurs: Error in plot.new() : figure margins too large

Can you help?

decodebiology commented 9 months ago

Hi,

I am not a developer of this tool. But I used following code to reproduce heatmap using ComplexHeatmap package. it is easier to work with ComplexHeatmap than working with heatmap.3. It would be nice if developers consider incorporating ComplexHeatmap instead.

library(copykat)
library(ComplexHeatmap)
library(circlize)

pid <- "PatientX"
copykat.test <- copykat(rawmat=exp.rawdata, id.type="S", ngene.chr=5, win.size=25, KS.cut=0.1, sam.name=(paste0(pid)), distance="euclidean", norm.cell.names="",output.seg="TRUE", plot.genes="FALSE", genome="hg20",n.cores=16)

pred.test <- data.frame(copykat.test$prediction)
pred.test <- pred.test[-which(pred.test$copykat.pred=="not.defined"),]  ##remove undefined cells
CNA.test <- data.frame(copykat.test$CNAmat)

mat <- t(CNA.test[,4:ncol(CNA.test)])
com.preN <- pred.test$copykat.pred
hcc <- hclust(parallelDist::parDist(t(mat),threads =8, method = "euclidean"), method = "ward.D2")
hc.umap <- cutree(hcc,2)
roworder = hcc$order
hc.cut <- cutree(hcc,2)

CNA.testX <- CNA.test
CNA.testX$chrom <- paste0("chr",CNA.testX$chrom)

chr_cols <- c("chr1" = "black", "chr2" = "grey", "chr3" = "black", "chr4" = "grey", "chr5" = "black", "chr6" = "grey", "chr7" = "black", "chr8" = "grey", "chr9" = "black", "chr10" = "grey", "chr11" = "black", "chr12" = "grey", "chr13" = "black", "chr14" = "grey", "chr15" = "black", "chr16" = "grey", "chr17" = "black", "chr18" = "grey", "chr19" = "black", "chr20" = "grey", "chr21" = "black", "chr22" = "grey", "chr23" = "black", "chrX" = "grey", "chrY" = "black", "chrMT" = "grey")

topAnnotation <- HeatmapAnnotation(chromosomes = CNA.testX$chrom, col = list(chromosomes = chr_cols))
leftAnnotation <- rowAnnotation(cluster = paste0("c",hc.cut), pred=com.preN, col = list(cluster = c("c1" = "#088A85", "c2" = "#DBA901"), pred = c("diploid" = "#088A68", "aneuploid" = "#DF013A") ) )

col_fun = colorRamp2(c(min(mat), 0, max(mat)), c("#08088A", "white", "#B40431"))

ht1 <- Heatmap(mat, left_annotation = leftAnnotation, row_order = roworder, top_annotation=topAnnotation, col=col_fun, show_row_names=F, show_column_names=F, cluster_columns=F,clustering_distance_columns = "euclidean",clustering_method_columns = "ward.D2")

pdf(paste0(pid,"_CNA.test_heatmap_complexHeatmap_ward.D2.pdf"))
draw(ht1)
dev.off()
recervictory commented 2 months ago

Thanks for sharing the code @decodebiology But col_fun is missing

decodebiology commented 2 months ago

Thanks for sharing the code @decodebiology But col_fun is missing

Now added col_fun