igraph的顶点序列

时间:2014-06-30 08:04:13

标签: list graph igraph

我使用igraph提取了所有最短路径,并将其保存在列表文件中(命名为best)。首先,所有节点都是数字,我向它们添加了基因符号。结果是:

library(igraph)
adjacency <- structure(list(source = structure(c(3L, 5L, 1L, 3L, 2L, 4L), 
  .Label = c("ANXA7", "CAV1", "FLNA", "GRB7", "PRKCA"), class = "factor"), 
  Target = structure(c(5L, 2L, 1L, 3L, 4L, 1L), 
  .Label = c("A1BG", "ANXA7", "CAV1", "GRB7", "PRKCA"), class = "factor")), 
  .Names = c("source", "Target"), 
  class = "data.frame",row.names = c(NA, -6L)) 

graph <- graph.data.frame(adjacency, directed = FALSE)

graph
# IGRAPH UN-- 6 6 -- 
# + attr: name (v/c)
plot(graph)

然后实现最短的路径:

rest <- best <- list()
rest[[1]] <- get.all.shortest.paths(graph, 1, 3, mode = c("all"))$res
names <- V(graph)
best <- lapply(rest[[1]], function(rest) { names[rest]})

然后

[[1]]
Vertex sequence:
[1] "FLNA"  "PRKCA" "ANXA7"

您也可能需要输入和取消列表输出:

dput(best)
list(list(structure(c(1, 2, 3), class = "igraph.es", env = <environment>)))

unlist(best[[1]])
[1] 1 2 3

但我需要的只是提取/计数节点名称。订单也很重要。

names(best[[1]])
NULL     # I need to just list the names
length(best)
[1] 1   # number of shortest paths
> length(best[[1]])
[1] 1 # I need to count nodes in each shortest path

由于

1 个答案:

答案 0 :(得分:1)

只需将此代码(名称&lt; -V(图表))替换为:

names<-V(graph)$name

这样可行。