Skip to content

Reordering by custom dendrogram no longer working with heatmap.2 #8

@dr-joe-wirth

Description

@dr-joe-wirth

I have included my code below. It defines a function capable of taking in a tree in newick format alongside a matrix of values, and then plotting a heatmap with the tree on the rows and the columns.

The issue appears to be with heatmap.2

If I plot the dendrogram by itself, it is in the correct order.

Within the call to heatmap.2 ...
If I change Rowv=tree to Rowv=FALSE and Colv=tree to Colv=FALSE then I get the correct order.

The code below used to work just fine and produce the graphics I desired. However, it plots the correct dendrogram but it is reordering the matrix which makes the dendrogram no longer correspond with the data.

This last worked in mid 2020. I'm not sure what happened since then to cause it to break.

I'd be happy to provide more information and/or files. This is my first issue report. Please let me know if you'd like additional details.

meanSquareMatrix <- function(squareMat){
	# for the first row to the second-to-last row
	for(i in 1:(ncol(squareMat)-1)){
		
		# for the i+1th column to the last column
		for(j in (i+1):ncol(squareMat)){
			# extract the two values for the forward and reverse comparison
			valuesV <- c(squareMat[i,j], squareMat[j,i])
	
			# get the mean of the values and replace the original data
			squareMat[i,j] <- squareMat[j,i] <- mean(valuesV)
		}
	}
	
	return(squareMat)
}


generateHeatmap <- function(treeFN=NULL, aaiFN=NULL, pdfOutFN=NULL, numDecimals=0, numColors=16, height=32, width=32){
	# dependencies
	require(ape)
	require(DECIPHER)
	require(gplots)
	require(dendextend)
	
	# import files
	tree <- ReadDendrogram(treeFN, internalLabels=FALSE)
	aai.df <- read.delim(aaiFN)
	
	# convert all underscores to spaces in aai.df
	rownames(aai.df) <- gsub("_", " ", rownames(aai.df))
	colnames(aai.df) <- gsub("_", " ", colnames(aai.df))
	
	# remove double spaces from aai.df and tip names
	rownames(aai.df) <- gsub("  ", " ", rownames(aai.df))
	colnames(aai.df) <- gsub("  ", " ", colnames(aai.df))
	labels(tree) <- gsub("  ", " ", labels(tree))
	
	# make an AAI matrix with only the taxa present in the tree
	aai.mx <- as.matrix(aai.df[which(rownames(aai.df) %in% labels(tree)), which(colnames(aai.df) %in% labels(tree))])
	
	# order the matrix so that the rows and columns match the order of the tips in the tree
	aai.mx <- aai.mx[order(match(rownames(aai.mx), labels(tree))), order(match(colnames(aai.mx), labels(tree)))]
	
	# get the mean of all forward/reverse comparisons
	aai.mx <- meanSquareMatrix(aai.mx)
	
	# remove all self-self comparisons from the table
	for(i in 1:nrow(aai.mx)){
		aai.mx[i,i] <- NA
	}
	
	# get the cell values for the matrix (rounded)
	aai.cells <- round(aai.mx, digits=numDecimals)
	
	# get the heat map colors
	colors <- colorRampPalette(colors=c("purple", "red", "yellow", "white"))(numColors)
	
	# generate the plot and write to file
	pdf(file=pdfOutFN, height=height, width=width)
	heatmap.2(aai.mx, Rowv=tree, Colv=tree, col=colors, cellnote=aai.cells, trace="none", notecol="black", notecex=1, margins=c(12,20), cexRow=1, cexCol=1, lhei=c(1,8), lwid=c(1,8))
	dev.off()

	# note:
	# Rowv=FALSE and Colv=FALSE resolves the ordering problem, but then there is no accompanying dendrogram.
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions