noriakis / ggkegg

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

output_overlay_image #20

Closed Ramirj closed 4 months ago

Ramirj commented 4 months ago

Hi @noriakis,

I tried implementing the output_overlay_image function to my code, and while it did seem to improve the resolution, it also came produced two overlapping images. Do you know what the issue could be? Here is my code:

Load required libraries

library(ggkegg) library(BiocFileCache) library(viridis)

Define the pathway ID

pathway_id <- "dosa00270" # Change depending on the pathway you want to map

Fetch and cache RN to EC map

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

Create a graph of the pathway

g <- pathway(pathway_id) |> mutate(ec = rntoec[reaction])

Define a vector of node names to highlight

nodes_to_highlight <- reaction

Add log fold changes (LFCs) to the graph

LFC_vector <- LFCs # Make sure LFCs is a vector with values for each corresponding reaction number

Ensure that LFC column is created in the graph

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

Create a pathway diagram with gradient colors to visualize LFCs

gg <- g |> filter(type %in% c("compound", "gene")) |> ggraph(layout = "manual", x = x 2, y = y 2) + geom_node_rect(aes(fill = LFC), size = 5) + # Remove redundant aesthetic mappings scale_fill_gradient2(low = "red", high = "green", breaks = c(-8, -4, -1, 1, 4, 8), limits = c(-8, 8)) + overlay_raw_map("dosa00270") + theme_minimal() + theme_void() + ggtitle("Induced (Green) and Repressed (Red) Genes in Cysteine and Methionine Met Pathway - 8 hr Timepoint")

Output the high-resolution image with adjusted legend space

output_overlay_image(gg, high_res = TRUE, use_cache = TRUE, with_legend_image = TRUE, res = 200, legend_space = 150, out = "pathway_image.png")

gg

pathway_image

noriakis commented 4 months ago

Hi, could you please provide a reproducible example? You can use a convenient library like reprex.

Ramirj commented 4 months ago
# Load required libraries
library(ggkegg)
#> Loading required package: ggplot2
#> Loading required package: ggraph
#> Loading required package: XML
#> Loading required package: igraph
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
#> Loading required package: tidygraph
#> 
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:igraph':
#> 
#>     groups
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> 
library(BiocFileCache)
#> Loading required package: dbplyr
library(viridis)
#> Loading required package: viridisLite

# Define the pathway ID
pathway_id <- "dosa00270"  # Change depending on the pathway you want to map

# Fetch and cache RN to EC map
url <- "https://rest.kegg.jp/link/rn/ec"
bfc <- BiocFileCache()
path <- bfcrpath(bfc, url)
convert <- data.frame(data.table::fread(path, header = FALSE, sep = "\t"))
kotoec <- convert$V1 |>
  strsplit(":") |>
  vapply("[", 2, FUN.VALUE = "a") |>
  setNames(convert$V2)

# Create a graph of the pathway
g <- pathway(pathway_id) |>
  mutate(ec = rntoec[reaction])
#> Error in `mutate()`:
#> ℹ In argument: `ec = rntoec[reaction]`.
#> Caused by error:
#> ! object 'rntoec' not found

# Define a vector of node names to highlight
nodes_to_highlight <- reaction
#> Error in eval(expr, envir, enclos): object 'reaction' not found

# Add log fold changes (LFCs) to the graph
LFC_vector <- LFCs  # Make sure LFCs is a vector with values for each corresponding reaction number
#> Error in eval(expr, envir, enclos): object 'LFCs' not found

# Ensure that LFC column is created in the graph
g <- g %>%
  mutate(LFC = ifelse(reaction %in% nodes_to_highlight, LFC_vector, NA))
#> Error in eval(expr, envir, enclos): object 'g' not found

# Create a pathway diagram with gradient colors to visualize LFCs
gg <- g |>
  filter(type %in% c("compound", "gene")) |>
  ggraph(layout = "manual", x = x * 2, y = y * 2) +
  geom_node_rect(aes(fill = LFC), size = 5) +  # Remove redundant aesthetic mappings
  scale_fill_gradient2(low = "red", high = "green",
                       breaks = c(-8, -4, -1, 1, 4, 8),
                       limits = c(-8, 8)) +
  overlay_raw_map("dosa00270") +
  theme_minimal() +
  theme_void() +
  ggtitle("Induced (Green) and Repressed (Red) Genes in Cysteine and Methionine Met Pathway - 8 hr Timepoint")
#> Error in eval(expr, envir, enclos): object 'g' not found

# Output the high-resolution image with adjusted legend space
output_overlay_image(gg, high_res = TRUE, use_cache = TRUE, with_legend_image = TRUE,
                     res = 200, legend_space = 150,
                     out = "pathway_image.png")
#> Error in eval(expr, envir, enclos): object 'gg' not found

gg
#> Error in eval(expr, envir, enclos): object 'gg' not found

Created on 2024-02-15 with reprex v2.1.0

Ramirj commented 4 months ago

By the way, for the "reaction" vector, you could set it to something like "rn:R01514" and it should work. Then you can just set the "LFCs" vector to whatever number.

noriakis commented 4 months ago

Hi, please consult here for what the reproducible example is. Your code makes the error here in my environment, as in your pasted reprex.

# Create a graph of the pathway
g <- pathway(pathway_id) |>
  mutate(ec = rntoec[reaction])
#> Error in `mutate()`:
#> ℹ In argument: `ec = rntoec[reaction]`.
#> Caused by error:
#> ! object 'rntoec' not found
Ramirj commented 4 months ago

Sorry, there was a typo in the code. It should work now:

# Load required libraries
library(ggkegg)
#> Loading required package: ggplot2
#> Loading required package: ggraph
#> Loading required package: XML
#> Loading required package: igraph
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
#> Loading required package: tidygraph
#> 
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:igraph':
#> 
#>     groups
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> 
library(BiocFileCache)
#> Loading required package: dbplyr
library(viridis)
#> Loading required package: viridisLite

# Define the pathway ID
pathway_id <- "dosa00270"  # Change depending on the pathway you want to map

# Fetch and cache RN to EC map
url <- "https://rest.kegg.jp/link/rn/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)

# Create a graph of the pathway
g <- pathway(pathway_id) |>
  mutate(ec = rntoec[reaction])

# Define a vector of node names to highlight
nodes_to_highlight <- reaction
#> Error in eval(expr, envir, enclos): object 'reaction' not found

# Add log fold changes (LFCs) to the graph
LFC_vector <- LFCs  # Make sure LFCs is a vector with values for each corresponding reaction number
#> Error in eval(expr, envir, enclos): object 'LFCs' not found

# Ensure that LFC column is created in the graph
g <- g %>%
  mutate(LFC = ifelse(reaction %in% nodes_to_highlight, LFC_vector, NA))
#> Error in `mutate()`:
#> ℹ In argument: `LFC = ifelse(reaction %in% nodes_to_highlight,
#>   LFC_vector, NA)`.
#> Caused by error:
#> ! object 'nodes_to_highlight' not found

# Create a pathway diagram with gradient colors to visualize LFCs
gg <- g |>
  filter(type %in% c("compound", "gene")) |>
  ggraph(layout = "manual", x = x * 2, y = y * 2) +
  geom_node_rect(aes(fill = LFC), size = 5) +  # Remove redundant aesthetic mappings
  scale_fill_gradient2(low = "red", high = "green",
                       breaks = c(-8, -4, -1, 1, 4, 8),
                       limits = c(-8, 8)) +
  overlay_raw_map("dosa00270") +
  theme_minimal() +
  theme_void() +
  ggtitle("Induced (Green) and Repressed (Red) Genes in Cysteine and Methionine Met Pathway - 8 hr Timepoint")
#> Warning: Existing variables `x` and `y` overwritten by layout variables
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.

# Output the high-resolution image with adjusted legend space
output_overlay_image(gg, high_res = TRUE, use_cache = TRUE, with_legend_image = TRUE,
                     res = 200, legend_space = 150,
                     out = "pathway_image.png")
#> Scale for x is already present.
#> Adding another scale for x, which will replace the existing scale.
#> Scale for y is already present.
#> Adding another scale for y, which will replace the existing scale.
#> Error in `geom_node_rect()`:
#> ! Problem while computing aesthetics.
#> ℹ Error occurred in the 1st layer.
#> Caused by error:
#> ! object 'LFC' not found

gg
#> Error in `geom_node_rect()`:
#> ! Problem while computing aesthetics.
#> ℹ Error occurred in the 1st layer.
#> Caused by error:
#> ! object 'LFC' not found

Created on 2024-02-15 with reprex v2.1.0

noriakis commented 4 months ago

Hi, as you can see, your codes still produce many errors, not related to your original problem. Please read and understand what the reproducible example is. You just copied and pasted your original code to reprex package, which is clearly showing errors.

# Define a vector of node names to highlight
nodes_to_highlight <- reaction
#> Error in eval(expr, envir, enclos): object 'reaction' not found
Ramirj commented 4 months ago

Okay, I got rid of the extra stuff causing the errors. This should work for you now:

# Load required libraries
library(ggkegg)
#> Loading required package: ggplot2
#> Loading required package: ggraph
#> Loading required package: XML
#> Loading required package: igraph
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
#> Loading required package: tidygraph
#> 
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:igraph':
#> 
#>     groups
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> 
library(BiocFileCache)
#> Loading required package: dbplyr
library(viridis)
#> Loading required package: viridisLite

# Define the pathway ID
pathway_id <- "dosa00270"  # Change depending on the pathway you want to map

# Create a graph of the pathway
g <- pathway(pathway_id)

# Create a pathway diagram with gradient colors to visualize LFCs
gg <- g |>
  filter(type %in% c("compound", "gene")) |>
  ggraph(layout = "manual", x = x * 2, y = y * 2) +
  geom_node_rect(aes(fill = "red"), size = 5) +  # Remove redundant aesthetic mappings
  overlay_raw_map("dosa00270") +
  theme_minimal() +
  theme_void() +
  ggtitle("Induced (Green) and Repressed (Red) Genes in Cysteine and Methionine Met Pathway - 8 hr Timepoint")
#> Warning: Existing variables `x` and `y` overwritten by layout variables
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.

# Output the high-resolution image with adjusted legend space
output_overlay_image(gg, high_res = TRUE, use_cache = TRUE, with_legend_image = TRUE,
                     res = 200, legend_space = 150,
                     out = "pathway_image.png")
#> Scale for x is already present.
#> Adding another scale for x, which will replace the existing scale.
#> Scale for y is already present.
#> Adding another scale for y, which will replace the existing scale.
#> [1] "pathway_image.png"

gg

Created on 2024-02-15 with reprex v2.1.0

Ramirj commented 4 months ago

Hi @noriakis, I found out how to fix the problem. I needed to get rid of the overlay_raw_map function from my original code. It works great now, thanks!