Question: pheatmap: Error in hclust(d, method = method) : NA/NaN/Inf in foreign function call (arg 10)
0
gravatar for camillab.
12 days ago by
camillab.10
London
camillab.10 wrote:

Hi! I am trying to do an heatmap with pheatmap package but I keep getting this error :

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

I have tried with na.omit() and looking at the dataset there is not NA. here my dataset:

# A tibble: 5 x 8
  `Gene descripti~ `Gene symbol`  mu_p0 `mu_ p2_`
  <chr>            <chr>          <dbl>     <dbl>
1 RIKEN cDNA 0610~ 0610005C13RIK  0.797      1.04
2 RIKEN cDNA 0610~ 0610007C21RIK 99.9      129.  
3 RIKEN cDNA 0610~ 0610007L01RIK 28.4       32.7 
4 RIKEN cDNA 0610~ 0610007P08RIK  6.13       2.61
5 RIKEN cDNA 0610~ 0610007P14RIK 37.9       37.7

and here my code:

library(gplots) 
library(pheatmap)
library(RColorBrewer)
library(tidyr)

mouse <- Mousebaseline %>% drop_na() #remove rows with NA from the merged filed
rnames <- mouse$`Gene symbol`#select name
mouse <- mouse[-c(1:2)]# remove gene symbol
mouse.matrix <-(as.matrix(mouse))
rownames(mouse.matrix) <- rnames # assign row names
mouse.matrix <- t(mouse.matrix) #transpose
mouseUT <- scale(mouse.matrix)

pheatmap(mouseUT, scale = "none",cluster_rows = T, cluster_cols = T, show_rownames = T, show_colnames = F, clustering_method = "ward.D2",border_color= NA, main = "Mouse baseline (Ward.D2)")

it gives me the same error even if I do not scale prior the heatmap like:

pheatmap(mouse.matrix, scale = "column",cluster_rows = T, cluster_cols = T, show_rownames = T, show_colnames = F, clustering_method = "ward.D2",border_color= NA, main = "Mouse baseline (Ward.D2)")

also if I do na.omit() as follow:

library(gplots) 
library(pheatmap)
library(RColorBrewer)
library(tidyr)

mouse <- Mousebaseline %>% drop_na() #remove rows with NA from the merged filed
rnames <- mouse$`Gene symbol`#select name
mouse <- mouse[-c(1:2)]# remove gene symbol
mouse.matrix <-(as.matrix(mouse))
rownames(mouse.matrix) <- rnames # assign row names
mouse.matrix <- t(mouse.matrix) #transpose
mouseUT <- scale(mouse.matrix)
mouseUT<- na.omit(mouseUT)

pheatmap(mouseUT, scale = "none",cluster_rows = T, cluster_cols = T, show_rownames = T, show_colnames = F, clustering_method = "ward.D2",border_color= NA, main = "Mouse baseline (Ward.D2)")

I got this error:

Error in hclust(d, method = method) : must have n >= 2 objects to cluster

thank you for you help!

camilla

arg10 rna-seq pheatmap R • 141 views
ADD COMMENTlink written 12 days ago by camillab.10
1
gravatar for antonioggsousa
12 days ago by
antonioggsousa480 wrote:

Hi,

Before running pheatmap() function, please do and post here the result of the two following lines of code:

is.na(mouseUT) %>% table()

dim(mouseUT)

António

ADD COMMENTlink written 12 days ago by antonioggsousa480

hi! thank you. is this means that there are NAs? if yes, why my code doesn't get rid of them?

is.na(mouseUT) %>% table()

 FALSE   TRUE 
168102  37566 

dim(mouseUT)
[1]     6 34278
ADD REPLYlink written 12 days ago by camillab.10

Yes, it means that you still have NAs in our data. This is quite strange, because I tried to exclude NAs with the functions that you have and it works with mine example.

Still I think pheatmap supports/deals with NAs; however, it does not handle infinite values. Did you transformed your current data using logaritm?

ADD REPLYlink written 12 days ago by antonioggsousa480

no the values are RPKMs

ADD REPLYlink written 12 days ago by camillab.10

Can you do and post the result here:

is.infinite(mouseUT) %>% table()

António

ADD REPLYlink modified 12 days ago • written 12 days ago by antonioggsousa480

no inf values:

.
 FALSE 
205668
ADD REPLYlink written 12 days ago by camillab.10

...but the output of this command indicates that your data has thousands of NA values:

is.na(mouseUT) %>% table()

pheatmap() cannot calculate distances using NA values if, for example, an entire gene or sample only has NA values; so, you will have to filter out genes and/or samples that only have NA values.

ADD REPLYlink modified 12 days ago • written 12 days ago by Kevin Blighe61k

yes but why mouse <- Mousebaseline %>% drop_na() doesn't work?

ADD REPLYlink written 12 days ago by camillab.10

You will have to trace back through your code and check the contents of each object. The mere fact that there was a variable called Gene symbol in mouse and Mousebaseline is likely where you need to first look.

Indeed (I looked up further in this thread), you need to remove those first 2 columns from the data and ensure that all other columns are encoded numerically. drop_na() will only work on a data-frame (or matrix) that is only numerical.

I can basically reproduce the same error (in Portuguese) if I impute NAs row- and column-wise:

data.frame(A = c(1,2,3),B = c(5,4,3))
  A B
1 1 5
2 2 4
3 3 3
pheatmap(data.frame(A = c(1,2,3),B = c(5,4,3)))


data.frame(A = c(1,NA,3),B = c(5,NA,3))
   A  B
1  1  5
2 NA NA
3  3  3
pheatmap(data.frame(A = c(1,NA,3),B = c(5,NA,3)))
Error in hclust(d, method = method) : 
  NA/NaN/Inf em chamada de função externa (argumento 10)


data.frame(A = c(1,2,3),B = c(NA,NA,NA))
  A  B
1 1 NA
2 2 NA
3 3 NA
pheatmap(data.frame(A = c(1,2,3),B = c(NA,NA,NA)))
Error in hclust(d, method = method) : 
  NA/NaN/Inf em chamada de função externa (argumento 10)
ADD REPLYlink modified 12 days ago • written 12 days ago by Kevin Blighe61k

I have tried removing the character columns and it still did not work. but this morning I thought that maybe there were rows = 0 that once scaled give rise to NA and removing them with the following code, works:

mouse <-filter_if(Mousebaseline, is.numeric, all_vars((.) != 0)) #remove all rows with n= 0
rnames <- mouse$`Gene symbol`#select name
mouse <- mouse[-c(1:2)]# remove gene symbol
mouse.matrix <-(as.matrix(mouse))
rownames(mouse.matrix) <- rnames # assign row names
mouse.matrix <- t(mouse.matrix) #transpose
mouseUT <- scale(mouse.matrix)
ADD REPLYlink written 12 days ago by camillab.10

Yes, if one column contains only zeros, after scaling the whole column will be set to NaN.

So, may be this was the issue with your data since the begining.

António

ADD REPLYlink written 12 days ago by antonioggsousa480
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1719 users visited in the last hour