利用R语言作PPI(免String网页和安装cytoscape法)

原创来自https://mp.weixin.qq.com/s/xw-x_r9yq6Iiw-4LwyKvWw,本人稍作修改,原文是针对小鼠,现改为人类
当得到差异基因后,很多时候需要作PPI蛋白互作网络,一般需要登陆STRING网站,然后用cytoscape软件作图,本文将通过R包STRINGdb(记住是大写,这个包是bioconductor里的,默认的CRAN里有个stringdb,名字一模一样,但是完全不全,前往不要搞错)来进行string蛋白互作分析,同时会利用igraph和ggraph对互作网络进行可视化。
首先安装包,缺啥补啥,没啥好说的

if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")

BiocManager::install("STRINGdb")

进行分析前,先要创建一个STRINGdb对象:

library(tidyverse)
library(clusterProfiler) # Y叔的包有没有,这个其实只是为了ID转换
library(org.Hs.eg.db)  小鼠的话,把Hs改成Mm
 
library(STRINGdb)
library(igraph)
library(ggraph)
# 创建STRINGdb对象
string_db <- STRINGdb$new( version="11", species=9606, 
                           score_threshold=400, input_directory="")

目前version最新版是11,9606是人类,小鼠是10090
score_threshold是蛋白互作的得分,此值会用于筛选互作结果,400是默认分值,如果要求严格可以调高此值。自己调即可

  • 构建基因列表,可以自己定义值,也可以导入表格,需要表头
    可以是symbol,可以是ENTREZID,这个不详细说
# Y叔的clusterProfiler将Gene Symbol转换为Entrez ID
gene <- gene %>% bitr(fromType = "SYMBOL", 
                      toType = "ENTREZID", 
                      OrgDb = "org.Hs.eg.db", 
                      drop = T)

转换后是这样的


image.png

使用map函数用于将基因匹配到STRING数据库的ID,map函数的帮助信息可以查看STRINGdb$help("map")。然后plot_network绘图即可。中途需要在线下载数据,慢慢等吧。

data_mapped <- gene %>% string_db$map(my_data_frame_id_col_names = "ENTREZID", 
                removeUnmappedRows = TRUE)
string_db$plot_network( data_mapped$STRING_id )
网址画图

R语言画图

可以发现和官网的出图是一样的。这里不是重点,只是为了说明STRINGdb的基础用法,就不再展开了。

使用get_interactions获取蛋白互作信息,以用于后续可视化。

需在线下载数据,等着。。。

hit<-data_mapped$STRING_id
info <- string_db$get_interactions(hit)

info包含了蛋白互作的信息,比较重要的是前两列和最后一列:from、to、combined_score,前两列指定了蛋白互作关系的基因对,最后一列是此蛋白互作关系的得分。
info数据将用于后续分析。
info的结果是这样的

image.png

使用igraph和ggraph可视化蛋白互作网络图
先使用igraph创建网络数据,并进行必要的处理,然后转到ggraph绘图。

# 转换stringID为Symbol,只取前两列和最后一列
links <- info %>%
  mutate(from = data_mapped[match(from, data_mapped$STRING_id), "SYMBOL"]) %>% 
  mutate(to = data_mapped[match(to, data_mapped$STRING_id), "SYMBOL"]) %>%  
  dplyr::select(from, to , last_col()) %>% 
  dplyr::rename(weight = combined_score)
# 节点数据
nodes <- links %>% { data.frame(gene = c(.$from, .$to)) } %>% distinct()
# 创建网络图
# 根据links和nodes创建
net <- igraph::graph_from_data_frame(d=links,vertices=nodes,directed = F)
# 添加一些参数信息用于后续绘图
# V和E是igraph包的函数,分别用于修改网络图的节点(nodes)和连线(links)
igraph::V(net)$deg <- igraph::degree(net) # 每个节点连接的节点数
igraph::V(net)$size <- igraph::degree(net)/5 #
igraph::E(net)$width <- igraph::E(net)$weight/10
# 使用ggraph绘图
# ggraph是基于ggplot2的包,语法和常规ggplot2类似
ggraph(net,layout = "kk")+
  geom_edge_fan(aes(edge_width=width), color = "lightblue", show.legend = F)+
  geom_node_point(aes(size=size), color="orange", alpha=0.7)+
  geom_node_text(aes(filter=deg>5, label=name), size = 5, repel = T)+
  scale_edge_width(range = c(0.2,1))+
  scale_size_continuous(range = c(1,10) )+
  guides(size=F)+
  theme_graph()

这里的参数设置是节点的大小和其连接的线的数量有关,线数量越多则点越大;线的宽度和其蛋白互作的得分有关,得分越高则越宽。只显示节点数大于5的基因名称。

KK法作图
  • stress布局作图:
ggraph(net,layout = "stress")+ #不同的地方
  geom_edge_fan(aes(edge_width=width), color = "lightblue", show.legend = F)+
  geom_node_point(aes(size=size), color="orange", alpha=0.7)+
  geom_node_text(aes(filter=deg>5, label=name), size = 5, repel = T)+
  scale_edge_width(range = c(0.2,1))+
  scale_size_continuous(range = c(1,10) )+
  guides(size=F)+
  theme_graph()
stree法作图
  • 环形布局
ggraph(net,layout = "linear", circular = TRUE)+
  geom_edge_fan(aes(edge_width=width), color = "lightblue", show.legend = F)+
  geom_node_point(aes(size=size), color="orange", alpha=0.7)+
  geom_node_text(aes(filter=deg>5, label=name), size = 5, repel = F)+
  scale_edge_width(range = c(0.2,1))+
  scale_size_continuous(range = c(1,10) )+
  guides(size=F)+
  theme_graph()
image.png

上面的几张图可以看到有几个单一的互作关系,其和主网络并不相连,像这种互作关系可以去掉,此时出来的图就会更加美观。

# 去除游离的互作关系
# 如果links数据框的一个link的from只出现过一次,同时to也只出现一次,则将其去除
links_2 <- links %>% mutate(from_c = count(., from)$n[match(from, count(., from)$from)]) %>%
  mutate(to_c = count(., to)$n[match(to, count(., to)$to)]) %>%
  filter(!(from_c == 1 & to_c == 1)) %>%
  dplyr::select(1,2,3)
# 新的节点数据
nodes_2 <- links_2 %>% { data.frame(gene = c(.$from, .$to)) } %>% distinct()
# 创建网络图
net_2 <- igraph::graph_from_data_frame(d=links_2,vertices=nodes_2,directed = F)
# 添加必要的参数
igraph::V(net_2)$deg <- igraph::degree(net_2)
igraph::V(net_2)$size <- igraph::degree(net_2)/5
igraph::E(net_2)$width <- igraph::E(net_2)$weight/10

如果去除了游离的互作关系,那么可以使用一种中心布局的方式,它是根据一个节点的连接数而排列其位置,连接数越大,节点越倾向于在中间位置排列,会更容易看得出重要节点。

另外环形布局的线使用弧形线(geom_edge_arc)会更美观:

ggraph(net,layout = "linear", circular = TRUE)+
  geom_edge_arc(aes(edge_width=width), color = "lightblue", show.legend = F)+
  geom_node_point(aes(size=size), color="orange", alpha=0.7)+
  geom_node_text(aes(filter=deg>5, label=name), size = 5, repel = F)+
  scale_edge_width(range = c(0.2,1))+
  scale_size_continuous(range = c(1,10) )+
  guides(size=F)+
  theme_graph()
image.png
  • 除了使用igraph创建网络图外,也可以使用tidygraph的as_tbl_graph函数处理数据,然后使用ggraph绘图:
links_2 %>% tidygraph::as_tbl_graph() %>%
  ggraph(layout = "kk")+
  geom_edge_fan(color = "grey")+
  geom_node_point(size=5, color="blue", alpha=0.8)+
  geom_node_text(aes(label=name), repel = T)+
  theme_void()
image.png
links_2 %>% tidygraph::as_tbl_graph() %>%
  ggraph(layout = "stress")+
  geom_edge_fan(color = "grey")+
  geom_node_point(size=5, color="blue", alpha=0.8)+
  geom_node_text(aes(label=name), repel = T)+
  theme_void()
image.png
links_2 %>% tidygraph::as_tbl_graph() %>%
    ggraph(layout = "linear", circular = TRUE)+
    geom_edge_fan(color = "grey")+
    geom_node_point(size=5, color="blue", alpha=0.8)+
    geom_node_text(aes(label=name), repel = T)+
    theme_void()
image.png

你可能感兴趣的:(利用R语言作PPI(免String网页和安装cytoscape法))