lucaz88 / genome_comparison_code

Companion repository to the article: "Comparative whole-genome approach to identify bacterial traits for microbial interactions"
GNU General Public License v3.0
2 stars 0 forks source link

Enhancement: R code for Figure 1? #1

Open morgansobol opened 1 year ago

morgansobol commented 1 year ago

Hi Luca Zoccarato,

First, I would like to say thank you very much for making your R code so easy to follow. I was able to easily adapt it to my data. I was wondering if you happen to have the code you used to produce the final combined plot of the trait presence/absence, LTC, and GTC (Figure 1)? If so, would you be willing to share that here in this repository or with me? Of course, I will be certain to give credit to your work/Github when my work is published :)

Thanks again! Morgan S.

lucaz88 commented 1 year ago

Ciao Morgan, sorry for the late reply. Below you can find the code snippet for the plotting with some explanation. I hope it will be helpful for you. Keep in mind that the figure has been finalized with a graphic editor for minor text/position adjustments.

library(iheatmapr)
library(DescTools)

# required objects:
# gnm_profile -> matrix of genome by trait presence/absence
# LTC_table -> auxiliary matrix with info on annotate traits: trait ids, ann. tool and trait cluster id
# GFC_table -> auxiliary matrix with info on genomes: genome ids, taxonomy, genome cluster id
# trait_hc and gnm_hc are the precomputed dendrograms 

p1_vert = main_heatmap(gnm_profile, name = " ") %>%
  add_row_title("") %>%
  add_row_annotation(data.frame("LTCs" = factor(LTC_table$LTC, levels = LTC_table$LTC[!duplicated(LTC_table$LTC)])),
                     colors = list("LTCs" = LTC_table$LTC_col[!duplicated(LTC_table$LTC)]),
                     size = 0.04, buffer = 0.05, side = "right") %>%
  add_row_dendro(trait_hc, side = "right", size = 0.16, reorder = T) %>%
  add_row_annotation(data.frame("Ann.tool" = factor(LTC_table$Annotation, levels = unique(LTC_table$Annotation))),
                     colors = list("Ann.tool" = brewer.pal(length(unique(LTC_table$Annotation)), "Set3")),
                     size = 0.02, side = "left") %>%
  add_row_summary(layout = list(zeroline = F, title = "Gnm.frac."), size = 0.2, side = "left") %>%
  add_col_title("")  %>%
  add_col_annotation(data.frame("GFCs" = factor(GFC_table$GFC, levels = unique(GFC_table$GFC))),
                     colors = list("GFCs" = DescTools::ColToGray(unique(GFC_table$GFC_col_bw))),
                     size = 0.03, side = "top") %>%
  add_col_dendro(gnm_hc, buffer = 0.02, size = 0.16, side = "top", reorder = T) %>%
  add_col_annotation(data.frame("Taxa" = factor(GFC_table$Taxon_RA, levels = rev(unique(GFC_table$Taxon_RA[gnm_hc$order])))),
                     colors = list("Taxa" = rev(unique(GFC_table$Taxon_col[gnm_hc$order]))),
                     size = 0.03, side = "top")
p1_vert %>% save_iheatmap("hetamap.pdf", vwidth = 1350, vheight = 1000)