Contents

1 Introduction

punKEGGer was created to make KEGG pathway data accessible and useful for network science workflows.

Traditional approaches to KEGG data within Bioconductor often reduce pathways to simple gene sets (e.g., for enrichment analysis), custom-saved graphNEL objects, or static visualizations with hardcoded layouts. While those are valid for many use cases, they often lose the rich topological and visual information encoded in KEGG’s own pathway representations.

punKEGGer provides a different approach: it parses the original .kgml files and converts them into clean, annotated igraph objects, with optional layouts and metadata preserved. From there, you can analyze topological features, integrate identifiers, combine multiple pathways, or run dynamic simulations — whatever your network science questions demand. It is, after all, a Pretty Useful Networks-from-KEGG Exploration Resource.

2 Example 1: Parse a single KEGG pathway

This first example shows the basic parsing workflow. We start from a KGML file, which is KEGG’s XML representation of a pathway, and convert it into a tidygraph/igraph object. The resulting graph preserves pathway relations and can be used directly with standard network analysis tools.

# usually you'll read from the official database like so
# kgml <- read_xml("https://rest.kegg.jp/get/hsa04210/kgml")
# or use the function fetch_kegg_kgml
# kgml <- fetch_kegg_kgml("hsa04210")
# but for reproducibility purposes you can use 
kgml_file <- system.file("extdata", "hsa04210.xml", package = "punKEGGer")
kgml <- xml2::read_xml(kgml_file)

g <- combine_kegg_network(kgml)
g
#> # A tbl_graph: 108 nodes and 141 edges
#> #
#> # A directed acyclic simple graph with 5 components
#> #
#> # Node Data: 108 × 2 (active)
#>    name  meta_id
#>    <chr> <chr>  
#>  1 53    53     
#>  2 47    47     
#>  3 54    54     
#>  4 48    48     
#>  5 43    43     
#>  6 152   152    
#>  7 51    51     
#>  8 46    46     
#>  9 38    295    
#> 10 156   156    
#> # ℹ 98 more rows
#> #
#> # Edge Data: 141 × 4
#>    from    to type  subtype   
#>   <int> <int> <chr> <chr>     
#> 1     1     2 PPrel activation
#> 2     2     5 PPrel activation
#> 3     3     4 PPrel activation
#> # ℹ 138 more rows

The printed object summarizes the parsed pathway as an tidygraph object. At this stage, nodes still are annotated using KEGG entry identifiers; annotation is added in the next step.

3 Example 2: Add gene annotations

KEGG entries often group several molecules (genes, etc) identifiers into a single pathway node. For molecule-level analyses, punKEGGer can expand these metanodes and then attach external identifiers, such as HGNC symbols, using a user-provided dictionary; such dictionaries can be obtained from resources such as BioMart.

node_info <- extract_kegg_nodes(kgml)
dict_path <- system.file("extdata", "example_dict.hsa04210.csv", package = "punKEGGer")
kegg_dict <- read.csv(dict_path)
# creates the dictionary for the metanodes
meta_dict <- create_meta_dict(node_info, kegg_dict)
# expands metanodes and returns a new graph
g_exp <- expand_metagraph(g, node_info, node_types = "gene")
#annotates expanded graph 
g_annot <- annotate_kegg_graph(g_exp, meta_dict, identifiers = "hgnc_symbol")

#show the expanded graph with annotated nodes
tidygraph::as_tibble(g_annot, active = "nodes") |>
  dplyr::select(name, meta_id, type, hgnc_symbol) |>
  head()
#> # A tibble: 6 × 4
#>   name     meta_id type  hgnc_symbol
#>   <chr>    <chr>   <chr> <chr>      
#> 1 hsa:8743 53      gene  TNFSF10    
#> 2 hsa:8795 47      gene  TNFRSF10B  
#> 3 hsa:8797 47      gene  TNFRSF10A  
#> 4 hsa:356  54      gene  FASLG      
#> 5 hsa:355  48      gene  FAS        
#> 6 hsa:8772 41      gene  FADD

Here, we expanded metanodes into single nodes. Then, we add identifier columns to the node table. These columns can then be used for plotting, filtering, or downstream analyses.

4 Example 3: Visualize your pathway

KEGG pathways include manually curated coordinates that reproduce the familiar KEGG pathway layout. layout_kegg() extracts those coordinates, and plot_kegg_graph() uses them to create a KEGG-like network plot.

layout_tbl <- layout_kegg(kgml)
g_layout <- add_kegg_layout(g_annot, layout_tbl)
plot_kegg_graph(g_layout, id = hgnc_symbol)

4.1 Alternative layout using ggraph:

In some analyses, the curated KEGG layout is less important than the graph topology itself. Since the output is a standard graph object, it can also be visualized with any other ggraph-compatible layouts.

library(ggraph)
#> Loading required package: ggplot2
library(tidygraph)
#> 
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#> 
#>     filter

# let's calculate some simple centrality measures
g_annot <- 
  g_annot |> 
  mutate(degree = centrality_degree())

# now we visualize the network using a force-directed layout, and show node size and label size proportional to degree

ggraph(g_annot, layout = "fr") +
  geom_edge_link() +
  geom_node_label(aes(size = degree, 
                     label = hgnc_symbol),
                  fill = "#9D4EDD",
                  color = "white",
                  label.r = unit(0.15,"lines"),
                  label.padding = unit(0.25,"lines"),
                  label.size = 0.2,
                  repel = TRUE
                 ) + 
  theme_void()

5 Example 4: Combine pathways and connect to your network science pipeline



#let's identify modules in the network using the Infomap algorithm

g_annot <- 
  as_tbl_graph(g_annot)

g_annot <- 
  g_annot |> 
  mutate(infomap = group_infomap())



#let's calculate degree centrality 
g_annot <- 
  g_annot |> 
  mutate(degree = centrality_degree())

# now, let's identify the gene(s) with highest centrality per module 

df <- 
  g_annot |> 
  as_tibble() |> 
  group_by(infomap) |> 
  filter(degree == max(degree)) |> 
  arrange(infomap)

print(df)
#> # A tibble: 18 × 6
#> # Groups:   infomap [11]
#>    name      meta_id type  hgnc_symbol degree infomap
#>    <chr>     <chr>   <chr> <chr>        <dbl>   <int>
#>  1 hsa:3002  234     gene  GZMB            22       1
#>  2 hsa:4790  23      gene  NFKB1           13       2
#>  3 hsa:5970  23      gene  RELA            13       2
#>  4 hsa:1439  44      gene  CSF2RB          10       3
#>  5 hsa:3563  44      gene  IL3RA           10       3
#>  6 hsa:4914  45      gene  NTRK1           10       3
#>  7 hsa:841   30      gene  CASP8            4       4
#>  8 hsa:5599  155     gene  MAPK8            6       5
#>  9 hsa:5601  155     gene  MAPK9            6       5
#> 10 hsa:5602  155     gene  MAPK10           6       5
#> 11 hsa:10018 170     gene  BCL2L11          3       6
#> 12 hsa:5894  278     gene  RAF1             2       7
#> 13 hsa:5604  279     gene  MAP2K1           2       7
#> 14 hsa:5605  279     gene  MAP2K2           2       7
#> 15 hsa:1649  201     gene  DDIT3            2       8
#> 16 hsa:7157  13      gene  TP53            11       9
#> 17 hsa:7124  51      gene  TNF              1      10
#> 18 hsa:835   229     gene  CASP2            1      11

In this example, we take our previous KEGG graph, find modules using a community detection method, and identify the most connected gene in each module. This is just an example; From here, you can compute centralities, detect communities, or simulate diffusion – whatever your network science needs demand.

#> R version 4.6.1 (2026-06-24)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 24.04.5 LTS
#> 
#> Matrix products: default
#> BLAS:   /home/biocbuild/bbs-3.24-bioc/R/lib/libRblas.so 
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0  LAPACK version 3.12.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_GB              LC_COLLATE=C              
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#> 
#> time zone: America/New_York
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] tidygraph_1.3.1  ggraph_2.2.2     ggplot2_4.0.4    punKEGGer_1.0.1 
#> [5] BiocStyle_2.42.0
#> 
#> loaded via a namespace (and not attached):
#>  [1] viridis_0.6.5       sass_0.5.0          utf8_1.2.6         
#>  [4] generics_0.1.4      tidyr_1.3.2         xml2_1.6.0         
#>  [7] stringi_1.8.9       digest_0.6.39       magrittr_2.0.5     
#> [10] evaluate_1.0.5      grid_4.6.1          RColorBrewer_1.1-3 
#> [13] bookdown_0.48       fastmap_1.2.0       jsonlite_2.0.0     
#> [16] ggrepel_0.9.8       tinytex_0.62        gridExtra_2.3.1    
#> [19] BiocManager_1.30.27 purrr_1.2.2         viridisLite_0.4.3  
#> [22] scales_1.4.0        tweenr_2.0.3        jquerylib_0.1.4    
#> [25] cli_3.6.6           graphlayouts_1.2.5  rlang_1.3.0        
#> [28] polyclip_1.10-7     withr_3.0.3         cachem_1.1.0       
#> [31] yaml_2.3.12         otel_0.3.0          tools_4.6.1        
#> [34] memoise_2.0.1       dplyr_1.2.1         vctrs_0.7.3        
#> [37] R6_2.6.1            magick_2.9.1        lifecycle_1.0.5    
#> [40] stringr_1.6.0       MASS_7.3-66         pkgconfig_2.0.3    
#> [43] pillar_1.11.1       bslib_0.12.0        gtable_0.3.6       
#> [46] Rcpp_1.1.2          glue_1.8.1          ggforce_0.6.0      
#> [49] xfun_0.62           tibble_3.3.1        tidyselect_1.2.1   
#> [52] knitr_1.52          dichromat_2.0-1     farver_2.1.2       
#> [55] htmltools_0.5.9     igraph_2.3.4        labeling_0.4.3     
#> [58] rmarkdown_2.32      compiler_4.6.1      S7_1.0.0