This is a test version of Biostars. For the public version, visit https://www.biostars.org.
heatmap with plink genome file

Hi all!

I'm trying to do a heatmap with plink's output for IBD calculation (.genomefile). This file has several columns, but only three are important to me, so my parsed input format looks like this:

IID1    IID2    PI_HAT
ID1     ID2     0.0163
ID1     ID3     0
ID1     ID4     0.0155
ID2     ID1     0.0096
ID2     ID3     0.0125
ID2     ID4     0.475
...

I would like to do a heatmap with the PIHAT values for all my IDs (I have hundreds). To my understanding, Rasks for a matrix as input for the plot, but I'm not able to parse this input into the correct format (I actually get the heatmap, but all the values are wrong). Could someone please give me advice or if there's another way to do the plot, I'd be as well to try it. Thank you very much in advance!

r plink ibd

1 answer

Input data for heatmap should be numeric matrix, your data is long format data, we need to reshape it to wide, then convert to matrix for plotting, see example:

library(reshape2)

# example data
df1 <- read.table(text = "IID1    IID2    PI_HAT
ID1     ID2     0.0163
ID1     ID3     0
ID1     ID4     0.0155
ID2     ID1     0.0096
ID2     ID3     0.0125
ID2     ID4     0.475", header = TRUE, stringsAsFactors = FALSE)


#convert long-to-wide
x <- dcast(df1, IID1 ~ IID2, value.var = "PI_HAT")

# convert to matrix with column AND rownames
myM <- as.matrix(x[ , -1 ])
row.names(myM) <- x$IID1

# I am converting all NAs to 0, reconsider if this is suitable in your case.
myM[ is.na(myM) ] <- 0

#then plot
heatmap(myM)

Woooow, thank you very much, it's working! Just for me to understand... what is the difference between long and wide format?? I hadn't heard before about this before.

Log in to answer this question.