This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Generating a heatmap in DEG analysis

After performing DESEQ2 on my data, I could able to plot MA, PCA, and EnhancedVolcano plots including a (.xlsx) file consisting of log fold change ratios, base mean values, and p- values adjusted, and normal p-values were obtained. I would like to now generate a heatmap.

This is my R-script until now:

countData <- read.table("gene_count_matrix.csv", header = TRUE, sep = ",", row.names = 1)
head(countData)
metaData <- read.table("phenodata.csv", header = TRUE, sep = ",")
head(metaData)

#Deseq2
library(DESeq2)

dds <- DESeqDataSetFromMatrix(countData=countData, colData=metaData, design=~stage_condition)
dds <- DESeq(dds)
dds <- dds[rowSums(counts(dds)) > 0]
res <- results(dds)

head(res)
summary(res)

res <- res[order(res$padj),]
head(res)

resSig <- res[ which(res$pvalue < 0.05)]
res_lfc <- subset(resSig, abs(log2FoldChange) > 2) 
head(res_lfc) 

#MA plot

plotMA(res, ylim=c(-2,2))

#plot for PCA
log_dds<-rlog(dds)
plotPCAWithSampleNames(log_dds, intgroup="treatment", ntop=40000)

#volcano plot
library("EnhancedVolcano")

EnhancedVolcano(res,lab = rownames(res),x = 'log2FoldChange',y = 'pvalue',labSize = 0, pCutoff = 0.05,FCcutoff = 1,xlim=c(-30,12))
plot(EnhancedVolcano)
differential-gene-expression rna-seq

Please format the code in your posts appropriately in the future using the code formatting button (the one with 1s and 0s) for clarity. I have done it for you this time.

2 answers

There are many ways to create heatmaps. The most common packages are probably pheatmap and ComplexHeatmap. The latter is much more flexible, but requires slightly more knowledge/reading to use effectively. Regardless, it's an excellent package and worth learning how to use.

devarora shows how to create sample distance heatmaps in their answer, which are a useful QC measure. The DESeq2 vignette shows how to create heatmaps for the count matrix itself via pheatmap.

Heatmap

library("RColorBrewer")

library('pheatmap')

Perform Variance transformation or rlog. say

variance_dds<- vst(dds, blind=FALSE)

sampleDists<-dist(t(assay(variance_dds)))

sampleDistMatrix <- as.matrix(sampleDists)

rownames(sampleDistMatrix) <- paste(variance_dds$Condition, variance_dds$sample, sep="-")

colnames(sampleDistMatrix) <- NULL

colors <- colorRampPalette( rev(brewer.pal(9, "Blues")) )(255)

pheatmap(sampleDistMatrix, clustering_distance_rows=sampleDists,clustering_distance_cols=sampleDists, col=colors)

Hopefully you will get a heatmap. With little tweaks you can change colors too.

Log in to answer this question.