【问题标题】:Creating Subgraph using igraph in R在 R 中使用 igraph 创建子图
【发布时间】:2014-07-04 03:06:24
【问题描述】:

我需要使用 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.n​​col:

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

这是一个简单的文本文件,每行一个边。一条边由两个用制表符分隔的符号顶点名称定义:

文件.txt

ALDOA
APLP1
GRB2
RPA2
FLNC
BCL3
APP
RALGDS
PRRC2A
NARS
LSM1
GGA3
FNTA

【问题讨论】:

  • 实际上,您的示例中的节点之间没有边,因此生成的子图对我来说似乎是正确的......您的预期输出是什么?
  • @digEmAll::这只是一个示例文件(file.txt)哥们。我有一长串种子节点,其中包含来自 DATABASE.n​​col 两列的条目。有什么想法吗??
  • 在我的测试中,induced.subgraph 返回一个包含边的子图,那么您能否提供一个重现该问题的小示例?您发布的内容非常接近;只需更改节点的名称以获得预期的错误行为并发布您想要的输出......
  • @digEmAll:根据您的建议,我现在重新表述了我的问题和示例数据。请检查。
  • 我还是不太确定,但我已经尽力给你答案了;)

标签: r igraph subgraph


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    有一个内置的 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"))
    

    【讨论】:

    • 感谢您的帮助,make_ego_graph 在您的subgraph 中创建一个列表。你是如何绘制subgraph 的图表的,它是一个包含三个节点的列表?
    猜你喜欢
    • 2018-11-25
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 2016-01-04
    相关资源
    最近更新 更多