This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Possibility of marking genes of interest by in ComplexHeatmap

Hi,

I was wondering if there is a possibility of marking genes of interest on the heatmap via ComplexHeatmap R package. Instead of specifying row numbers inanno_mark function, can we specify the gene list and mark on the heatmap? For long gene list, it's bit challenging to identify the rownames of each gene and specify them. Thank you.

Genes of interest

genes_to_show = c('Gene_A', 'Gene_F', 'Gene_L', 'Gene_R', 'Gene_GG', 'Gene_ZZ', 'Gene_Test')

Mark rownames

ha = rowAnnotation(foo = anno_mark(at = c(1:4, 11, 28, 44,:47), 
        labels = rownames(m)) ## Mention gene list here "genes_to_show" instead of specifying row numbers

This would work, but still need to specify row numbers

ha = rowAnnotation(foo = anno_mark(at = c(1:4, 11, 28, 44,:47), 
                                   labels = genes_to_show))

Plot heatmap

    Heatmap(m, name = "mat", cluster_rows = FALSE,  right_annotation = ha,
        row_names_side = "left", row_names_gp = gpar(fontsize = 4))

Best Regards,

Mohammed

heatmap ggplot2 r complexheatmap

1 answer

Use a combination of which() and %in% to isolate the row indices that match the gene character vector:

mark_at = which(rownames(m) %in% genes_to_show)

ha = rowAnnotation(foo = anno_mark(at = mark_at, labels = genes_to_show))

Giuseppe thank you very much. This was very helpful.

Maybe another question, there are associated p-values with all the genes diff.exp between two groups in the heatmap (full matrix), is there a possibility to add another right_annotation by color code based on the strength of p-values and add a legend scale.

Yes, it is actually in the manual that you can find here.

However, it shows how to build an annotation for p-values only for columns.

I have written a working example (statistically invalid so don't mind that part!) with simulated data where I add a row annotation with -log10(FDR), which is better for visualization. You will also need the circlize package to build the color vector.

# Simulate random data from two matrices

a = matrix(rnorm(1000, mean = 0, sd = 1), ncol = 10)
b = matrix(rnorm(1000, mean = 0, sd = 1), ncol = 10)

# Add "DE" to the 10% of the data, same rows

sample_diff = sample(seq_len(100), size = 10)
a[sample_diff, ] = a[sample_diff,] + rnorm(100, 2.5, 1)
b[sample_diff, ] = b[sample_diff,] - rnorm(100, 2.5, 1)

# Bind matrices

mat = cbind(a, b)

# Test for differences in mean and get p-value

pvalues = apply(mat, 1, function(x) t.test(x = x[1:10], y = x[11:20])$p.value)
padj = p.adjust(pvalues, method = "fdr")

# Make a heatmap row annotation
# First we compute the range of the adjusted p-value in -log10(pvalue) range, 
# rounding it to the maximum integer

maxp = ceiling(max(-log10(padj)))

col_fun = circlize::colorRamp2(c(0, maxp), c("gray", "red"))
pvalue_anno = rowAnnotation(pvalue = anno_simple(-log10(padj), col = col_fun))

# Column annotation
col_sample = c("A" = "orange", "B" = "purple")
sample_anno = HeatmapAnnotation(sample = c(rep("A", 10), rep("B", 10)), 
                          col = list(sample = col_sample))

# Legend construction

lgd_at = floor(seq(0, maxp, length.out = 4))
lgd_labs = c(1, paste0("1e-", lgd_at[2:4]))
lgd_pvalue = Legend(title = "FDR", col_fun = col_fun, at = lgd_at, 
    labels = lgd_labs)

# Draw heatmap and add legend

ht = Heatmap(mat, name = "simulated", 
                   top_annotation = sample_anno, 
                   right_annotation = pvalue_anno)

draw(ht, annotation_legend_list = list(lgd_pvalue))

Log in to answer this question.