[英]Creating Subgraph using igraph in R
我需要使用igraph從圖(g)獲取種子節點(節點的輸入列表; file.txt)及其第一個交互器(鄰居)的子圖。 不幸的是,我最終僅在子圖中得到一個節點,而不是所有其他將它們互連的節點和邊(頂點)。
g<-read.graph("DATABASE.ncol",format="ncol",directed=FALSE) #load the data
g2<-simplify(g, remove.multiple=TRUE, remove.loops=TRUE) # Remove the self-loops in the data
DAT1 <- readLines("file.txt") #It provides a character vector right away
list_nodes_1 = neighbors(g2, DAT1) #list of nodes to be fetched in subnetwork
list_nodes_1 # 16
g3 <- induced.subgraph(graph=g2,vids=DAT1) #subnetwork construction
g3 # GRAPH UN-- 1 0 --; indicating only one node
plot (g3)
對於獲取整個子網(包括節點和頂點)有什么建議嗎? 還是有其他功能可用於創建子圖?
DATABASE.ncol:
MAP2K4 FLNC
MYPN ACTN2
ACVR1 FNTA
GATA2 PML
RPA2 STAT3
ARF1 GGA3
ARF3 ARFIP2
ARF3 ARFIP1
XRN1 ALDOA
APP APPBP2
APLP1 DAB1
CITED2 TFAP2A
EP300 TFAP2A
APOB MTTP
ARRB2 RALGDS
CSF1R GRB2
PRRC2A GRB2
LSM1 NARS
SLC4A1 SLC4A1AP
BCL3 BARD1
這是一個簡單的文本文件,每行一條邊。 一條邊由兩個用制表符分隔的符號頂點名稱定義:
file.txt
ALDOA
APLP1
GRB2
RPA2
FLNC
BCL3
APP
RALGDS
PRRC2A
NARS
LSM1
GGA3
FNTA
我不確定是否已完全理解您的問題,所以我創建了一個(希望)不言自明的示例:
# for example reproducibility
set.seed(123)
# create a fake undirected graph
D <- read.table(
sep=',',
header=T,
text=
'from,to
A,B
A,C
D,E
F,G
H,I')
g1 <- graph.data.frame(D,directed=F)
plot(g1)
# we want a sub-network containing the floowing nodes:
subv <- c('A','B','H')
# first method:
# create a sub-network composed by ONLY the nodes in subv and the edges
# between them
g2 <- induced.subgraph(graph=g1,vids=subv)
plot(g2)
# second method:
# create a sub-network composed by the nodes in subv and, if some of them is
# connected to other nodes (even if not in subv), take also them
# (and of course include all the edges among this bunch of nodes).
sg1 <- decompose.graph(g1,mode="weak")
neighverts <- unique(unlist(sapply(sg1,FUN=function(s){if(any(V(s)$name %in% subv)) V(s)$name else NULL})))
g3 <- induced.subgraph(graph=g1,vids=neighverts)
plot(g3)
圖g1:
圖g2:
圖表g3:
我們如何基於某些頂點可能為NA的屬性使用一組選定的頂點? 想象在您的示例中V(graph)$ att1 <-c(1,2,NA,1,2,3,NA)並想要選擇att1 == 1的頂點。我嘗試通過選擇這些頂點的名稱V(graph)[att1 == 1,na_ok = TRUE] $ name,但它不起作用。 給我一個錯誤“ if(is.numeric(v)&& any(v <0))中的錯誤{:缺少值,需要TRUE / FALSE”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.