5

I am trying to create a heatmap with gene expression values with the package pheatmap in R. I have used the code numerous times and never had a problem untill today. It seems when I do the scale="row" , I end up with this error. I cannot create the z-scores. So probably some rows have no variability for which this is happening. How can I get rid of this. The matrix is having 1100 rows and 9 columns. My code:

data  <- read.table("~path/DEGs_DESeq.txt",sep="\t")
data2 <- as.matrix(data[,2:9])
data3 <- data2[-1,]
samples <- data2[1,]
genes <- data[2:length(data2[,1]),1]
vett <- as.numeric(data3)
data4 <- matrix(vett, length(genes), length(samples), dimnames=list(paste(genes),paste(samples))) 
head(data4)

pheatmap(as.matrix(data4), col=bluered(200), scale="row", key=T, keysize=1.5,
    density.info="none", trace="none",cexCol=0.6, fontsize_row=8, fontsize_col=10)

Error in hclust(d, method = method) : NA/NaN/Inf in foreign function call (arg 11)

How can I get rid of this error?

smci
  • 32,567
  • 20
  • 113
  • 146
ivivek_ngs
  • 917
  • 3
  • 10
  • 28
  • 6
    I had the same problem and your comment was very helpful in solving the problem. You should list it as an answer. Also this code is what I used: `counts_filtered_df <- counts_df[apply(counts_df, MARGIN = 1, FUN = function(x) sd(x) != 0),]` – user5359531 Oct 27 '16 at 20:58

2 Answers2

4

I infact worked it out.. i had to remove the rows for which no SD was coming up while performing the z score calculation and it reconstructed the heatmap. Thanks

ivivek_ngs
  • 917
  • 3
  • 10
  • 28
  • 3
    Please explain to the rest of us who don't have your dataset (so can't reproduce this) **why `pheatmap(..., scale="row"`) did not compute some SDs**? Was it grouping? Was it groups with a size of one, by any chance? Did your dataset actually have NAs in any rows? Otherwise this question is not a reusable resource. – smci Sep 02 '18 at 22:49
  • 1
    I'm not the OP, but was having a similar problem. In my dataset, which I'd checked for non-numeric and NA values, the issue was that there were a few rows with the same value in every column. For these, the SD was 0 and this was screwing up the row scaling. Removing these rows solved the problem. – Max F Jun 27 '20 at 16:21
  • Thanks for taking the lead Max and addressing the query of the comment. Really appreciate it. I shared the same answer as I solved my own problem. Apologies if it wasn’t clear enough, however Max did clarify it. – ivivek_ngs Jun 28 '20 at 17:04
0

You may have all the rows having 0 values in the dataframe