使用R中的igraph创建子图

5

我需要使用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

实际上,在您的示例中节点之间没有边,因此得到的子图对我来说似乎是正确的...您期望的输出是什么? - digEmAll
@digEmAll:这只是一个样本文件(file.txt),朋友。我有一个长列表的种子节点,包括DATABASE.ncol的两列条目。有什么想法吗? - Vinay
在我的测试中,induce.subgraph 返回一个包含边的子图,所以你能提供一个能够重现问题的小例子吗?你发布的内容已经非常接近了;只需更改节点名称以获得期望的错误行为,并发布你想要的输出即可... - digEmAll
@digEmAll:感谢您的建议,我已经重新表述了我的问题和示例数据。请检查。 - Vinay
我还不是很确定,但我已经尝试给你一个答案 ;) - digEmAll
2个回答

6

我不确定我是否完全理解了您的问题,因此我创建了一个(希望)自我说明的示例:

# 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:

g1

图形g2:

g2

图形g3:

g3


5
有一个内置的igraph函数可以实现这个功能。尝试使用make_ego_graph()函数:
library(igraph)
graph <- make_ring(7)
V(graph)$name <- c("A", "B", "C", "D", "E", "F", "G")

# Get the list of induced subgraphs
subgraph <- make_ego_graph(graph, order=1, c("A", "D", "F"))

Graph and it's three subgraphs


1
感谢您的帮助,make_ego_graph将创建一个列表到您的subgraph中。您是如何绘制由三个节点组成的列表subgraph的图形的? - Alex

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接