原創(chuàng)來自https://mp.weixin.qq.com/s/xw-x_r9yq6Iiw-4LwyKvWw,本人稍作修改缩歪,原文是針對小鼠莺奸,現(xiàn)改為人類
當?shù)玫讲町惢蚝螅芏鄷r候需要作PPI蛋白互作網(wǎng)絡叠骑,一般需要登陸STRING網(wǎng)站,然后用cytoscape軟件作圖削茁,本文將通過R包STRINGdb
(記住是大寫座云,這個包是bioconductor里的,默認的CRAN里有個stringdb付材,名字一模一樣朦拖,但是完全不全,前往不要搞錯)來進行string蛋白互作分析厌衔,同時會利用igraph和ggraph對互作網(wǎng)絡進行可視化璧帝。
首先安裝包,缺啥補啥富寿,沒啥好說的
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("STRINGdb")
進行分析前睬隶,先要創(chuàng)建一個STRINGdb對象:
library(tidyverse)
library(clusterProfiler) # Y叔的包有沒有,這個其實只是為了ID轉換
library(org.Hs.eg.db) 小鼠的話页徐,把Hs改成Mm
library(STRINGdb)
library(igraph)
library(ggraph)
# 創(chuàng)建STRINGdb對象
string_db <- STRINGdb$new( version="11", species=9606,
score_threshold=400, input_directory="")
目前version
最新版是11,9606是人類苏潜,小鼠是10090
score_threshold
是蛋白互作的得分,此值會用于篩選互作結果变勇,400是默認分值恤左,如果要求嚴格可以調(diào)高此值。自己調(diào)即可
- 構建基因列表搀绣,可以自己定義值飞袋,也可以導入表格,需要表頭
可以是symbol链患,可以是ENTREZID巧鸭,這個不詳細說
# Y叔的clusterProfiler將Gene Symbol轉換為Entrez ID
gene <- gene %>% bitr(fromType = "SYMBOL",
toType = "ENTREZID",
OrgDb = "org.Hs.eg.db",
drop = T)
轉換后是這樣的
使用map函數(shù)用于將基因匹配到STRING數(shù)據(jù)庫的ID,map函數(shù)的幫助信息可以查看STRINGdb$help("map")麻捻。然后plot_network繪圖即可纲仍。中途需要在線下載數(shù)據(jù)呀袱,慢慢等吧。
data_mapped <- gene %>% string_db$map(my_data_frame_id_col_names = "ENTREZID",
removeUnmappedRows = TRUE)
string_db$plot_network( data_mapped$STRING_id )
可以發(fā)現(xiàn)和官網(wǎng)的出圖是一樣的郑叠。這里不是重點夜赵,只是為了說明STRINGdb的基礎用法,就不再展開了锻拘。
使用get_interactions獲取蛋白互作信息,以用于后續(xù)可視化击蹲。
需在線下載數(shù)據(jù)署拟,等著。歌豺。推穷。
hit<-data_mapped$STRING_id
info <- string_db$get_interactions(hit)
info
包含了蛋白互作的信息,比較重要的是前兩列和最后一列:from类咧、to馒铃、combined_score
,前兩列指定了蛋白互作關系的基因?qū)弁铮詈笠涣惺谴说鞍谆プ麝P系的得分区宇。
info
數(shù)據(jù)將用于后續(xù)分析。
info的結果是這樣的
使用igraph和ggraph可視化蛋白互作網(wǎng)絡圖
先使用igraph創(chuàng)建網(wǎng)絡數(shù)據(jù)值戳,并進行必要的處理议谷,然后轉到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)
# 節(jié)點數(shù)據(jù)
nodes <- links %>% { data.frame(gene = c(.$from, .$to)) } %>% distinct()
# 創(chuàng)建網(wǎng)絡圖
# 根據(jù)links和nodes創(chuàng)建
net <- igraph::graph_from_data_frame(d=links,vertices=nodes,directed = F)
# 添加一些參數(shù)信息用于后續(xù)繪圖
# V和E是igraph包的函數(shù)卧晓,分別用于修改網(wǎng)絡圖的節(jié)點(nodes)和連線(links)
igraph::V(net)$deg <- igraph::degree(net) # 每個節(jié)點連接的節(jié)點數(shù)
igraph::V(net)$size <- igraph::degree(net)/5 #
igraph::E(net)$width <- igraph::E(net)$weight/10
# 使用ggraph繪圖
# ggraph是基于ggplot2的包,語法和常規(guī)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()
這里的參數(shù)設置是節(jié)點的大小和其連接的線的數(shù)量有關赴捞,線數(shù)量越多則點越大逼裆;線的寬度和其蛋白互作的得分有關,得分越高則越寬赦政。只顯示節(jié)點數(shù)大于5的基因名稱胜宇。
- 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()
- 環(huán)形布局
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()
上面的幾張圖可以看到有幾個單一的互作關系,其和主網(wǎng)絡并不相連恢着,像這種互作關系可以去掉掸屡,此時出來的圖就會更加美觀。
# 去除游離的互作關系
# 如果links數(shù)據(jù)框的一個link的from只出現(xiàn)過一次然评,同時to也只出現(xiàn)一次仅财,則將其去除
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)
# 新的節(jié)點數(shù)據(jù)
nodes_2 <- links_2 %>% { data.frame(gene = c(.$from, .$to)) } %>% distinct()
# 創(chuàng)建網(wǎng)絡圖
net_2 <- igraph::graph_from_data_frame(d=links_2,vertices=nodes_2,directed = F)
# 添加必要的參數(shù)
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
如果去除了游離的互作關系,那么可以使用一種中心布局的方式碗淌,它是根據(jù)一個節(jié)點的連接數(shù)而排列其位置盏求,連接數(shù)越大抖锥,節(jié)點越傾向于在中間位置排列,會更容易看得出重要節(jié)點碎罚。
另外環(huán)形布局的線使用弧形線(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()
- 除了使用igraph創(chuàng)建網(wǎng)絡圖外磅废,也可以使用tidygraph的as_tbl_graph函數(shù)處理數(shù)據(jù),然后使用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()
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()
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()