前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >🤩 ggkegg | 用这个神包玩转kegg数据库吧!~(一)

🤩 ggkegg | 用这个神包玩转kegg数据库吧!~(一)

作者头像
生信漫卷
发布2023-10-08 15:06:33
4170
发布2023-10-08 15:06:33
举报

1写在前面

好久没更了,实在是太忙了,值班真的是根本不不睡觉啊,一忙一整天,忙到怀疑人生。😭

最近看到比较🔥的就是ggkegg包,感觉使用起来还是有一定难度的。🫠

和大家分享一下使用教程吧,还有一些小坑。💪

2用到的包

代码语言:javascript
复制
rm(list = ls())
library(ggkegg)
library(ggfx)
library(ggraph)
library(igraph)
library(clusterProfiler)
library(dplyr)
library(tidygraph)

3小试牛刀

首先以eco00270为例,获取后转换路径和eco标识符,删除无连接节点并返回igraph对象。🤪

代码语言:javascript
复制
g <- ggkegg(pid="eco00270",
            convert_org = c("pathway","eco"),
            delete_zero_degree = T,
            return_igraph = T)

gg <- ggraph(g, layout="stress") 
gg$data$type  %>%  unique()

gg + geom_edge_diagonal(
  aes(color=subtype_name,
      filter=type!="maplink"))+
  geom_node_point(
  aes(filter= !type%in%c("map","compound")),
    fill=gg$data[!gg$data$type%in%c("map","compound"),]$bgcolor,
    color="black",
    shape=21, size=4
  )+
  geom_node_point(
    aes(filter= !type%in%c("map","gene")),
    fill=gg$data[!gg$data$type%in%c("map","gene"),]$bgcolor,
    color="black",
    shape=21, size=6
  )+
  geom_node_text(
    aes(label=converted_name,
        filter=type=="gene"),
    repel=T,
    bg.colour="white")+
  theme_void()

4改变nodes颜色

颜狗必备技能,修改配色。😘

代码语言:javascript
复制
g <- pathway("ko00520")
V(g)$color_one <- colorRampPalette(RColorBrewer::brewer.pal(5,"Set1"))(length(V(g)))
V(g)$color_two <- colorRampPalette(RColorBrewer::brewer.pal(5,"Set2"))(length(V(g)))

ggraph(g, x=x, y=y) +
  geom_node_rect(aes(xmin=xmin, xmax=x, fill=I(color_one)), alpha=0.5)+
  geom_node_rect(aes(xmin=x, xmax=xmax, fill=I(color_two)), alpha=0.5)+
  ggfx::with_outer_glow(geom_node_text(aes(label=name %>% 
                       strsplit(":") %>% 
                       sapply("[", 2) %>%
                       strsplit(" ") %>%
                       sapply("[", 1),
                     filter=type=="ortholog"),
                     size=2), colour="white", expand=1)

代码语言:javascript
复制
V(g)$color_one <- colorRampPalette(RColorBrewer::brewer.pal(5,"Set1"))(length(V(g)))
V(g)$color_two <- colorRampPalette(RColorBrewer::brewer.pal(5,"Set2"))(length(V(g)))
V(g)$color_three <- colorRampPalette(RColorBrewer::brewer.pal(5,"PuOr"))(length(V(g)))
V(g)$color_four <- colorRampPalette(RColorBrewer::brewer.pal(5,"Paired"))(length(V(g)))

V(g)$space <- V(g)$width/4

ggraph(g, x=x, y=y) +
  geom_node_rect(aes(xmin=xmin, xmax=xmin+space, fill=I(color_one), filter=type=="ortholog"))+
  geom_node_rect(aes(xmin=xmin+space, xmax=xmin+2*space, fill=I(color_two), filter=type=="ortholog"))+
  geom_node_rect(aes(xmin=xmin+2*space, xmax=xmin+3*space, fill=I(color_three), filter=type=="ortholog"))+
  geom_node_rect(aes(xmin=xmin+3*space, xmax=xmin+4*space, fill=I(color_four), filter=type=="ortholog"))+
  ggfx::with_outer_glow(geom_node_text(aes(label=name %>% 
                                             strsplit(":") %>% 
                                             sapply("[", 2) %>%
                                             strsplit(" ") %>%
                                             sapply("[", 1),
                                           filter=type=="ortholog"),
                                       size=2), colour="white", expand=1)+
  theme_void()

5Global maps展示

更宏观的展示结果。🤩

代码语言:javascript
复制
pathway("ko01200") %>% 
    process_reaction() %>%
    activate(nodes) %>% 
    mutate(x=NULL, y=NULL,
       comp=convert_id("compound")) %>%
    mutate(degree=centrality_degree(mode="all")) %>%
ggraph(layout="kk")+
    geom_node_point(aes(color=degree,
                        filter=type=="compound"))+
    geom_edge_parallel(
        color="grey",
        end_cap=circle(1,"mm"),
        start_cap=circle(1,"mm"),
        arrow=arrow(length=unit(1,"mm"),type="closed"))+
    geom_node_text(aes(label=comp,filter=degree>15),
                   repel=TRUE, bg.colour="white")+
    theme_graph()

6highlight指定nodes和edges

可以使用highligh_set_edgeshighlight_set_nodes函数突出你想要突出的nodesedges。😘

代码语言:javascript
复制
pathway("ko01230") %>% 
  process_line() %>%
  activate(nodes) %>%
  mutate(
    compound=convert_id("compound"),
    M00002=highlight_set_nodes(module("M00002")@reaction_components)) %>%
  activate(edges) %>%
  mutate(M00002=highlight_set_edges(module("M00002")@definition_components)) %>%
  ggraph(x=x, y=y)+
  geom_edge_link()+
  with_outer_glow(geom_edge_link(aes(color=M00002, filter=M00002)),
                  colour="pink")+
  geom_node_point(shape=21,aes(filter=type!="line"))+
  with_outer_glow(geom_node_point(shape=21, aes(filter=M00002, color=M00002)),
                  colour="pink")+
  geom_node_text(aes(label=compound, filter=M00002), repel=TRUE,
                 bg.colour="white", size=2)+
  theme_void()

我们试着突出一下代谢相关pathwayko01100),需要用到M00021module。😋

代码语言:javascript
复制
g <- pathway("ko01100") %>% 
  process_line() %>%
  highlight_module(module("M00021")) %>%
  mutate(compound=convert_id("compound"))

g %>% ggraph(x=x, y=y) +
  geom_node_point(size=1, aes(color=I(fgcolor),
    filter=fgcolor!="none" & type!="line"))+
  geom_edge_link(width=0.1, aes(color=I(fgcolor),
                                filter=type=="line"& fgcolor!="none"))+
  with_outer_glow(
    geom_edge_link(width=1,
                   aes(color=I(fgcolor),
                       filter=fgcolor!="none" & M00021)),
    colour="red", expand=3
  )+
  with_outer_glow(
    geom_node_point(size=2,
                   aes(color=I(fgcolor),
                       filter=fgcolor!="none" & M00021)),
    colour="red", expand=3
  )+
  theme_void()

可以看到多个module涉及Cysteinemethionine的代谢,我们可以通过使用ggforce来高亮M00017。😘

代码语言:javascript
复制
list_of_modules <- c("M00021","M00338","M00609","M00017","M00034","M00035","M00368")
for (mm in list_of_modules) {
  g <- g %>% highlight_module(module(mm))
}

ggraph(g,x=x,y=y,layout="manual") +
  geom_edge_link0(width=0.5, color="grey")+
  geom_edge_link(color="red",aes(filter=M00017|M00021|M00338|M00609|M00034|M00035|M00368))+
geom_node_point(size=2, color="red",aes(filter=M00017|M00021|M00338|M00609|M00034|M00035|M00368))+
  ggforce::geom_mark_rect(aes(fill=M00017,
                              label=module("M00017")@name,
                              x=x, y=y,
                              group=M00017,
                              filter=M00017),
                          label.fill = "transparent",
                          label.fontsize = 10,
                          expand=unit(1,"mm"))+
  theme_void()

可视化一下compounds,可以用geom_node_text,ggrepel, 和shadowtext。🤪

代码语言:javascript
复制
Rg %>% ggraph(x=x, y=y) +
  geom_node_point(size=1, aes(color=I(fgcolor),
    filter=fgcolor!="none" & type!="line"))+
  geom_edge_link(width=0.1, aes(color=I(fgcolor),
                                filter=type=="line"& fgcolor!="none"))+
  with_outer_glow(
    geom_edge_link(width=1,
                   aes(color=I(fgcolor),
                       filter=fgcolor!="none" & M00021)),
    colour="red", expand=3
  )+
  with_outer_glow(
    geom_node_point(size=2,
                   aes(color=I(fgcolor),
                       filter=fgcolor!="none" & M00021)),
    colour="red", expand=3
  )+
    geom_node_text(aes(label=compound, filter=M00021),
                   repel=T, bg.colour="white", size=5)+
  theme_void()

局部放大一下!~😘

代码语言:javascript
复制
annot <- g %>%  ggraph(x=x, y=y)+
  with_outer_glow(
    geom_edge_link(width=1,
                   aes(color=I(fgcolor),
                       filter=fgcolor!="none" & M00021)),
    colour="red", expand=3
  )+
  with_outer_glow(
    geom_node_point(size=2,
                    aes(color=I(fgcolor),
                        filter=fgcolor!="none" & M00021)),
    colour="red", expand=3
  )+
    geom_node_text(aes(label=compound, filter=M00021),
                   repel=TRUE, bg.colour="white", size=5)
g %>%
  ggraph(x=x, y=y) +
  geom_node_point(size=1, aes(color=I(fgcolor),
                              filter=fgcolor!="none" & type!="line"))+
  geom_edge_link(width=0.1, aes(color=I(fgcolor),
                                filter=type=="line"& fgcolor!="none"))+
  with_outer_glow(
    geom_edge_link(width=1,
                   aes(color=I(fgcolor),
                       filter=fgcolor!="none" & M00021)),
    colour="red", expand=3
  )+
  with_outer_glow(
    geom_node_point(size=2,
                    aes(color=I(fgcolor),
                        filter=fgcolor!="none" & M00021)),
    colour="red", expand=3
  )+
  annotation_custom(ggplotify::as.grob(annot),
    ymin=-1500, ymax=0, xmin=0, xmax=1500)+
  theme_void()

最后祝大家早日不卷!~

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-10-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 生信漫卷 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1写在前面
  • 2用到的包
  • 3小试牛刀
  • 4改变nodes颜色
  • 5Global maps展示
  • 6highlight指定nodes和edges
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档