noriakis / ggkegg

Analyzing and visualizing KEGG information using the grammar of graphics
https://noriakis.github.io/software/ggkegg
MIT License
210 stars 15 forks source link

LFCs on Raw KEGG Map #12

Closed Ramirj closed 10 months ago

Ramirj commented 10 months ago

Hi @noriakis,

Is it possible to use this package to visualize LFCs for target genes on raw KEGG Maps? For example, this image: image

I already have a list of reaction numbers and corresponding LFCs for the genes I'm interested in, I just don't know how to write a code that can achieve this

noriakis commented 10 months ago

Hi @Ramirj, I have made highlight_entities accept named numeric vector in devel and main branch. So you can do something like:

vecs <- c(-1, 1) |> setNames(c("CDKN2A","CDC45"))
highlight_entities("hsa04110", vecs, legend_name="interesting")

Make sure you specify which column to search for by name argument. In you case I think it should be reaction.

Ramirj commented 10 months ago

Hi @noriakis,

Thanks for the help, I read through the documentation and was able to figure out a way to visualize LFCs on raw KEGG maps. I just had to add the overlay_raw_map function to my original code. Here's the code I made in case anyone else has a similar issue:

Make pathway diagram with LFCs for genes present in lab data

Create a map of pathway for Oryza Sativa + map EC number labels

library(ggkegg) library(BiocFileCache)

Fetch and cache RN to EC map

url <- paste0("https://rest.kegg.jp/link/reaction/ec") bfc <- BiocFileCache() path <- bfcrpath(bfc, url) convert <- data.frame(data.table::fread(path, header = FALSE, sep = "\t")) rntoec <- convert$V1 |> strsplit(":") |> vapply("[", 2, FUN.VALUE = "a") |> setNames(convert$V2)

Map the EC number to gene nodes

g <- pathway("dosa00010") |> #change depending on pathway you want to map mutate(ec = rntoec[reaction])

Define a vector of node names to highlight

nodes_to_highlight <- reaction

Add LFCs

Create a vector containing log fold changes

LFC_vector <- LFCs

Add log fold changes to the graph

g <- g %>% mutate(LFC = ifelse(reaction %in% nodes_to_highlight, LFC_vector, NA))

Add color gradient to visualize LFCs

gg <- g |> filter(type %in% c("compound", "gene")) |> ggraph(layout = "manual", x = x, y = y) + geom_node_rect(aes(fill = LFC), size = 5) + scale_fill_viridis_c(option = "viridis", direction = 1, begin = 0.60, end = 1) + # Customize color scale overlay_raw_map("dosa00010")+ theme_void()

gg

Save diagram as png file

ggsave("whatever.png", gg, width = 10, height = 6, units = "in", dpi = 300)