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

Not Able to Add Log Fold Changes to Pathway Diagram #6

Closed Ramirj closed 11 months ago

Ramirj commented 11 months ago

Hi @noriakis,

I was able to create a pathway diagram of the glycolysis pathway highlighting genes from my own data, but I'm not able to add custom log fold change values. When I try, it returns this error: "Error in UseMethod("filter") : no applicable method for 'filter' applied to an object of class "igraph". Do you know of a way to add custom log fold changes using the ggkegg package? Here is the code that I'm currently trying to use:

Create a map of glycolysis 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") |> mutate(ec = rntoec[reaction])

Define a vector of node names to highlight

nodes_to_highlight <- c("rn:R01786","rn:R01600","rn:R00756","rn:R00762", "rn:R01068","rn:R01015","rn:R01518","rn:R00658", "rn:R00200","rn:R02569","rn:R00754","rn:R00746", "rn:R00710","rn:R01602","rn:R02739","rn:R09532") # Replace with specific node names to highlight

Add the "highlight" column

g <- g |> mutate(highlight = ifelse(reaction %in% nodes_to_highlight, "yes", "no"))

Convert the igraph object to a data frame

df <- as_data_frame(g)

Merge the LFC data with the "df" dataframe, retaining all rows from "df"

g2 <- merge(df, LFCs_shen_data, by = "reaction", all.x = TRUE)

Create an igraph object from the data frame

g <- graph.data.frame(g2, directed = TRUE)

Visualize the pathway with EC number labels and LFC values

gg <- g |> filter(type %in% c("compound", "gene")) |> ggraph(layout = "manual", x = x, y = y) + geom_edge_link(aes(color = subtype_name)) + geom_node_point(color = "lightblue", aes(filter = type == "compound")) + geom_node_rect(aes(fill = LFC, filter = type == "gene"), size = 5) + geom_node_shadowtext(aes(label = ec, filter = type == "gene"), color = "black", bg.colour = "white", size = 2) + geom_node_text(aes(label = name, filter = type == "compound"), color = "grey50", size = 2, repel = TRUE) + scale_fill_gradient2(low = "blue", mid = "white", high = "red", limits = c(-3, 3)) + theme_void()

Error in UseMethod("filter") : no applicable method for 'filter' applied to an object of class "igraph"

If you can provide some insight into this issue I would greatly appreciate it, thank you!

noriakis commented 11 months ago

Hi @Ramirj, you should convert igraph object to tbl_graph object using the function in tidygraph in order to use dplyr verbs on graph. As I said in #4, this Issue section is not for the general discussion of how to filter or convert the data, but for reporting the issues related to ggkegg. I think you should first get familiar with graph manipulation reading here.