I'm trying to create a heatmap using heatmap.2 function from a binary matrix, and extract the dendrogram from the heatmap, and save dendrogram into newick file format. The matrix has genomes on the row and genes on the column.
To do this I'm running following code.
library(ggdendro)
library(ape)
library(gplots)
library(vegan)
profile <-as.data.frame(read.delim("profile_file.txt",row.names=1,sep="\t", as.is=T))
dist_func<-function(x) vegdist(x,"jaccard",binary=TRUE)
hclust_func<-function(x) hclust(x,method="ward.D2")
heat<-heatmap.2(as.matrix(profile),Rowv=TRUE,Colv=TRUE,distfun=dist_func,hclustfun=hclust_func,trace="none")
row.dendro<-heat$rowDendrogram
row.hcclust<-as.hclust(row.dendro)
row.phylo<-as.phylo(row.hcclust)
write.tree(phy=row.phylo, file="tree_file.nwk")
This code works well when I try to run it will full profile. But when I reduce the number of gene columns I get an error at the step where I try to convert dendrogram in to hclust object.
row.hcclust<-as.hclust(row.dendro)
Error: all(vapply(s, is.integer, NA)) is not TRUE
I tried looking for any "NA" values in my dataset but there were none or else it should not have worked for full dataset as well.
Can any body please help me resolving this error? or suggest what might be the reason for this error to occur?