This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How do I make a ComplexHeatmap from this dataframe?

Hey ya'll,

Struggling here.

The dataframe was generated using a new package called InterCom (https://github.com/saschajung/InterCom), which basically determines ligand receptor pairing and also together with sending and receiving cells using scRNA-seq data. Incredibly useful if you ask me.

Here is a sample of my data:

"score" "significance" "lig.rec.pop" "ligand.receptor"
25507.7307692308 0.950689655172414 "undef_acinar" "FSTL1_BMPR1A"
113286.378762542 0.994068965517241 "mesenchymal_acinar" "FSTL1_BMPR1A"
56944.7025939177 0.989103448275862 "endothelial_acinar" "FSTL1_BMPR1A"
24467.0634475014 0.94551724137931 "mesenchymal_acinar" "BMP4_BMPR1A"
45149.7188075585 0.985034482758621 "mesenchymal_acinar" "BMP4_BMPR2"
20099.7619047619 0.916 "blood_acinar" "IL1B_IL1R1"
19786.641 0.913310344827586 "alpha_acinar" "VTN_ITGAV"
56609.9870823529 0.989034482758621 "mesenchymal_acinar" "FN1_ITGAV"

The maximum score value is 6611701.91, and the score minimum value is 36320.27,

Could someone help me get started with a good way to scale the data (if I even need to?, and generate a ComplexHeatmap? My main struggle is figuring out how to get it into the format to plug it into ComplexHeatmap.

I was able to do it through ggplot2, but I want to do it through ComplexHeatmap as I think it is easier to add bells and whistles. The only reason I was able to do it using ggplot2 was because I specified the what x, y and scores to use:

  ggplot(hm, aes(x = ligand.receptor, y = lig.rec.pop, fill = score)) +
  geom_tile() + theme(axis.text.x = element_text(angle = 90))

Ideally, I would love a heatmap/plot like this:

enter image description here

I realize that adding the time points (ie. week 7 and 9) might be a matter of adding meta data. but really if someone could just help me get started - I would be grateful.

Thank you in advance!

Image Citation: Gonçalves, C.A., Larsen, M., Jung, S. et al. A 3D system to model human pancreas development and its reference single-cell transcriptome atlas identify signaling pathways required for progenitor expansion. Nat Commun 12, 3144 (2021). https://doi.org/10.1038/s41467-021-23295-6
scaling scale heatmap

Hi, Have you read the ComplexHeatmap manuals?

In all honesty, I have browsed through (I'll give another look), and have also looked through here: https://github.com/kevinblighe/E-MTAB-6141

My main struggle is getting the dataframe in the appropriate format for ComplexHeatmap? I have used the package before, simply just plugging in a gene-gene correlation matrix - that was straight-forward.

However, I am troubled here, because the dataframe isn't as simple as just plugging in the matrix with a simple function such as Heatmap(mymatrix)

Any help would be appreciated!

Can you provide the data via dput() so it is easy to copy/paste it?

Thank you for helping me out ATPoint.

Hope this is correct:

dput(head(hm, 100), file ="~/Projects/SCFPInterCom/table.txt")

Output:

structure(list(score = c(25507.7307692308, 113286.378762542, 
56944.7025939177, 24467.0634475014, 45149.7188075585, 20099.7619047619, 
19786.641, 56609.9870823529, 20740.8489473684, 37127.4633333333, 
22346.7266129032, 68718.2951162791, 18454.4553846154, 57107.785, 
51543.6245070423, 23787.9075, 21464.94, 47420.5779310345, 20391.693, 
18579.9489473684, 21711.9, 27910.0591304348, 38100.5562711864, 
20862.0802173913, 20609.3404054054, 61627.309141791, 223040.895, 
23133.6786545455, 129210.139459459, 28207.9302272727, 61495.0955134135, 
23739.3698372966, 18106.4491816694, 20509.0572867633, 20841.6063829787, 
22435.832398317, 111294.665918552, 20408.912743397, 19414.7661538462, 
254025.836174636, 19931.6435897436, 39863.2871794872, 36281.2738987508, 
46766.7871794872, 70548.9700149925, 70297.8571428571, 96201.4390448581, 
164237.412786797, 297523.296551724, 41698.1172413793, 61319.4744525547, 
42742.8909090909, 283918.87826087, 142715.093023256, 39795.9278350515, 
24200.7264957265, 63927.6, 42999.6913451512, 110959.097435897, 
45902.0961538461, 160793.934065934, 80543.0769230769, 84938.5494505494, 
47388.3076923077, 55956.9594017094, 48572.5981710597, 76779.8106508876, 
22605.1859596714, 33377.5314685315, 185820.123076923, 76495.249070632, 
28845.2884615385, 125076.153846154, 24057.0113122172, 31217.4082840237, 
24541.975708502, 25100.4615384615, 57054.2517482517, 36416.7212307692, 
32110.6923076923, 29391.2980769231, 29281.4579650179, 53206.3859128823, 
41147.9331103679, 67135.0142450142, 76821.3333333333, 27971.378952336, 
30231.3538461538, 54856.6538461538, 71593.8461538462, 50498.2272672383, 
24382.5562130178, 31260.2490384615, 115743.570303335, 35020.0118343195, 
48038.3513203215, 48219.3076923077, 25212.8111888112, 84515.9228626527, 
33226.3630769231), significance = c(0.950689655172414, 0.994068965517241, 
0.989103448275862, 0.94551724137931, 0.985034482758621, 0.916, 
0.913310344827586, 0.989034482758621, 0.921379310344828, 0.978275862068966, 
0.933103448275862, 0.991586206896552, 0.903724137931034, 0.989310344827586, 
0.987724137931035, 0.942137931034483, 0.92751724137931, 0.986275862068966, 
0.918827586206897, 0.904206896551724, 0.929034482758621, 0.959379310344828, 
0.979655172413793, 0.922413793103448, 0.920275862068966, 0.990344827586207, 
0.996689655172414, 0.937931034482759, 0.994551724137931, 0.960137931034483, 
0.990344827586207, 0.94151724137931, 0.900413793103448, 0.919586206896552, 
0.922413793103448, 0.933655172413793, 0.994, 0.918827586206897, 
0.910206896551724, 0.997103448275862, 0.914689655172414, 0.98151724137931, 
0.977034482758621, 0.985931034482759, 0.991724137931035, 0.991724137931035, 
0.993310344827586, 0.995379310344828, 0.997448275862069, 0.983241379310345, 
0.985310344827586, 0.970413793103448, 0.996344827586207, 0.994206896551724, 
0.966068965517241, 0.912, 0.986689655172414, 0.970758620689655, 
0.992965517241379, 0.974275862068966, 0.994620689655172, 0.990551724137931, 
0.991241379310345, 0.975724137931035, 0.982965517241379, 0.977310344827586, 
0.989655172413793, 0.901586206896552, 0.951862068965517, 0.995034482758621, 
0.989655172413793, 0.935172413793103, 0.993655172413793, 0.911379310344828, 
0.945379310344828, 0.913931034482759, 0.916965517241379, 0.983655172413793, 
0.959586206896552, 0.948275862068966, 0.937655172413793, 0.937034482758621, 
0.980758620689655, 0.968137931034483, 0.987310344827586, 0.989655172413793, 
0.932206896551724, 0.941103448275862, 0.982344827586207, 0.988620689655172, 
0.979172413793103, 0.913310344827586, 0.945448275862069, 0.993172413793104, 
0.956413793103448, 0.976551724137931, 0.976827586206897, 0.917586206896552, 
0.991172413793104, 0.951379310344828), lig.rec.pop = c("undef_acinar", 
"mesenchymal_acinar", "endothelial_acinar", "mesenchymal_acinar", 
"mesenchymal_acinar", "blood_acinar", "alpha_acinar", "mesenchymal_acinar", 
"undef_acinar", "endocrine.progenitor_acinar", "ductal_acinar", 
"endothelial_acinar", "ductal_acinar", "undef_acinar", "mesenchymal_acinar", 
"undef_acinar", "undef_acinar", "endothelial_acinar", "blood_acinar", 
"mesenchymal_acinar", "endothelial_acinar", "acinar_acinar", 
"mesenchymal_acinar", "blood_acinar", "mesenchymal_acinar", "mesenchymal_acinar", 
"undef_acinar", "mesenchymal_acinar", "endothelial_acinar", "endothelial_acinar", 
"endothelial_acinar", "blood_acinar", "mesenchymal_acinar", "beta_acinar", 
"undef_acinar", "endothelial_acinar", "mesenchymal_acinar", "acinar_acinar", 
"ductal_acinar", "endothelial_acinar", "delta_acinar", "undef_acinar", 
"ductal_acinar", "undef_acinar", "ductal_acinar", "blood_acinar", 
"mesenchymal_acinar", "mesenchymal_acinar", "endothelial_acinar", 
"endothelial_acinar", "mesenchymal_alpha", "mesenchymal_alpha", 
"mesenchymal_alpha", "endothelial_alpha", "ductal_alpha", "acinar_alpha", 
"undef_alpha", "mesenchymal_alpha", "blood_alpha", "ductal_alpha", 
"blood_alpha", "endocrine.progenitor_alpha", "undef_alpha", "undef_alpha", 
"mesenchymal_alpha", "ductal_alpha", "endocrine.progenitor_alpha", 
"beta_alpha", "endothelial_alpha", "undef_alpha", "mesenchymal_alpha", 
"delta_alpha", "undef_alpha", "mesenchymal_alpha", "acinar_alpha", 
"undef_alpha", "endothelial_alpha", "endothelial_alpha", "ductal_alpha", 
"undef_alpha", "delta_alpha", "mesenchymal_alpha", "mesenchymal_alpha", 
"undef_alpha", "mesenchymal_alpha", "undef_alpha", "acinar_alpha", 
"endothelial_alpha", "endothelial_alpha", "undef_alpha", "mesenchymal_alpha", 
"endothelial_alpha", "mesenchymal_alpha", "mesenchymal_alpha", 
"endocrine.progenitor_alpha", "ductal_alpha", "alpha_alpha", 
"mesenchymal_alpha", "acinar_alpha", "alpha_alpha"), ligand.receptor = c("FSTL1_BMPR1A", 
"FSTL1_BMPR1A", "FSTL1_BMPR1A", "BMP4_BMPR1A", "BMP4_BMPR2", 
"IL1B_IL1R1", "VTN_ITGAV", "FN1_ITGAV", "CALR_ITGAV", "VTN_ITGAV", 
"SPP1_ITGAV", "COL4A1_ITGAV", "FN1_ITGAV", "FGB_ITGAV", "COL4A1_ITGAV", 
"FN1_ITGAV", "ANGPTL3_ITGAV", "FBN1_ITGAV", "NID1_ITGAV", "ADAM9_ITGAV", 
"NID1_ITGAV", "SPP1_ITGAV", "FBN1_ITGAV", "CALR_ITGAV", "EDIL3_ITGAV", 
"NID1_ITGAV", "VTN_ITGAV", "CALR_ITGAV", "FN1_ITGAV", "CALR_ITGAV", 
"EGFL7_NOTCH1", "EGFL7_NOTCH1", "EGFL7_NOTCH1", "EGFL7_NOTCH1", 
"EGFL7_NOTCH1", "EGFL7_NOTCH3", "FN1_SDC2", "FN1_SDC2", "LAMA3_SDC2", 
"FN1_SDC2", "FN1_SDC2", "LAMA3_SDC2", "FN1_SDC2", "FN1_SDC2", 
"VCAN_TLR2", "VCAN_TLR2", "BGN_TLR2", "VCAN_TLR2", "VCAN_TLR2", 
"BGN_TLR2", "BMP4_BMPR1A", "BMP5_BMPR1A", "FSTL1_BMPR1A", "FSTL1_BMPR1A", 
"FSTL1_BMPR1A", "FSTL1_BMPR1A", "FSTL1_BMPR1A", "BMP4_BMPR2", 
"PSAP_LRP1", "PLAT_LRP1", "THBS1_LRP1", "MDK_LRP1", "PSAP_LRP1", 
"PLAT_LRP1", "THBS1_LRP1", "MDK_LRP1", "PSAP_LRP1", "SERPINE2_LRP1", 
"CALR_LRP1", "SERPINA1_LRP1", "PSAP_LRP1", "SERPINA1_LRP1", "APOE_LRP1", 
"TFPI_LRP1", "THBS1_LRP1", "CALR_LRP1", "PLAT_LRP1", "THBS1_LRP1", 
"PSAP_LRP1", "SERPING1_LRP1", "PSAP_LRP1", "SERPING1_LRP1", "SERPINE2_LRP1", 
"MDK_LRP1", "C3_LRP1", "A2M_LRP1", "PSAP_LRP1", "LPL_LRP1", "SERPINE2_LRP1", 
"C3_LRP1", "MDK_LRP1", "SERPING1_LRP1", "APOE_LRP1", "PLAT_LRP1", 
"SERPINA1_LRP1", "SERPINA1_LRP1", "PSAP_LRP1", "PLAU_LRP1", "SERPINA1_LRP1", 
"SERPINE2_LRP1")), row.names = c(1L, 3L, 4L, 6L, 22L, 33L, 38L, 
39L, 41L, 44L, 46L, 47L, 48L, 49L, 55L, 58L, 63L, 64L, 69L, 72L, 
81L, 83L, 86L, 87L, 88L, 90L, 93L, 96L, 99L, 100L, 110L, 111L, 
114L, 115L, 117L, 118L, 129L, 130L, 136L, 138L, 141L, 143L, 146L, 
148L, 149L, 150L, 151L, 152L, 153L, 154L, 156L, 158L, 161L, 162L, 
163L, 165L, 170L, 171L, 189L, 190L, 191L, 193L, 195L, 199L, 201L, 
203L, 205L, 207L, 208L, 210L, 211L, 212L, 213L, 220L, 221L, 223L, 
224L, 225L, 227L, 228L, 230L, 233L, 234L, 238L, 239L, 240L, 243L, 
245L, 248L, 251L, 252L, 253L, 254L, 256L, 260L, 264L, 265L, 266L, 
268L, 269L), class = "data.frame")

Start with Heatmap(df[,cell_value_col_indices]) and then add row_annotation(df$lig.rec.pop) and then column_annotation. You might have to use left_annotation, top_annotation etc, they are row and column annotations with specific side parameters.

If clustering messes things up, draw the heatmap then get the row_order and column_order; pass those to the annotation functions by reordering the df with these order vectors.

Thank you very much Ram. I am going to try this now. A 3am ComplexHeatmap adventure weeee! lol

Hi, that looks like pheatmap, not ComplexHeatmap.

Oh, I see. I was thinking that too after looking at the ComplexHeatmap manual. Let me try do this in pheatmap.

I can do it with ggplot2:

ggplot(hm, aes(x = ligand.receptor, y = lig.rec.pop, fill = score)) +
  geom_tile() + theme(axis.text.x = element_text(angle = 90))

enter image description here

But I think when the time comes to add meta data and do the hierarchical clustering it might be more complicated.

1 answer

Below chunk will convert your data to complexheatmap data format. U can then modify the heatmap.

Assuming your data is in tdf data frame.

lig = unique(tdf$lig.rec.pop)
rec = unique(tdf$ligand.receptor)

mat = matrix(0,nrow = length(lig), ncol = length(rec))
rownames(mat) = lig
colnames(mat) = rec

for(i in 1:nrow(tdf))
{
  mat[tdf$lig.rec.pop[i], tdf$ligand.receptor[i]] = tdf$score[i]
}

Heatmap(mat)

Thank you very much! This is exactly what I needed! Perfect! : )

Log in to answer this question.