删除自循环和没有边的顶点
Remove self loops and vertex with no edges
我正在构建一个基因网络。我有一个两列数据框,我将其转换为邻接矩阵并在 igraph 中使用它。问题是我的基因有自循环,我想摆脱自循环,然后摆脱网络中没有边(可能是孤立的)的顶点。我尝试了一些东西,但不知何故它们不起作用。我的代码是
InnatedGraph <- graph.data.frame(innate,directed=FALSE)
V(InnatedGraph)$label.cex = 0.4
plot(InnatedGraph,vertex.label=V(InnatedGraph)$symbol, vertex.size=5)
innate 是我的两列数据框。我已经尝试使用 degree 函数来删除 0 度的顶点,但我想不幸的是它不起作用(也许是因为自循环基因的度数为 1)。
bad.vs<-V(InnatedGraph)[degree(InnatedGraph) == 0]
clean <-delete.vertices(InnatedGraph, bad.vs)
我尝试使用 BioNet 包 "rmSelfLoops" 中的另一个函数,借助它我能够移除自循环边,但仍然无法移除没有边的顶点。
test<-rmSelfLoops(InnatedGraph)
为了便于理解,我还会附上一张我的网络图片。
考虑这个示例图及其两个 subsets/modifications:
library(igraph)
set.seed(1)
g <- random.graph.game(10, p.or.m = 3, "gnm") + edge(7,7)
coords <- layout.auto(g)
par(mfrow = c(1,3))
plot(g, layout = coords)
plot(simplify(g), layout = coords) # remove loops and multiple edges
plot(delete.vertices(simplify(g), degree(g)==0)) # additionally delete isolated nodes
使用来自评论的 OP 示例数据:
df <- read.csv(header=F, row.names = 1, stringsAsFactors=F, text='"53","ENSG00000175104","ENSG00000175104"
"54","ENSG00000174775","ENSG00000175104"
"55","ENSG00000032688","ENSG00000027164"
"56","ENSG00000175104","ENSG00000140968"
"57","ENSG00000027164","ENSG00000041515"
"58","ENSG00000027164","ENSG00000025498"')
library(igraph)
( dfclean <- subset(df, V2!=V3) ) # remove rows where source==target
# V2 V3
# 54 ENSG00000174775 ENSG00000175104
# 55 ENSG00000032688 ENSG00000027164
# 56 ENSG00000175104 ENSG00000140968
# 57 ENSG00000027164 ENSG00000041515
# 58 ENSG00000027164 ENSG00000025498
par(mfrow = c(1,3))
plot(graph_from_data_frame(df), edge.arrow.size = .5) # orig
plot(simplify(graph_from_data_frame(df)), edge.arrow.size = .5) # same as
plot(graph_from_data_frame(dfclean), edge.arrow.size = .5) # this one
使用函数 graph_from_adjacency_matrix
将邻接矩阵转换为图形并设置参数 diag=F
。
那应该摆脱自循环。
我正在构建一个基因网络。我有一个两列数据框,我将其转换为邻接矩阵并在 igraph 中使用它。问题是我的基因有自循环,我想摆脱自循环,然后摆脱网络中没有边(可能是孤立的)的顶点。我尝试了一些东西,但不知何故它们不起作用。我的代码是
InnatedGraph <- graph.data.frame(innate,directed=FALSE)
V(InnatedGraph)$label.cex = 0.4
plot(InnatedGraph,vertex.label=V(InnatedGraph)$symbol, vertex.size=5)
innate 是我的两列数据框。我已经尝试使用 degree 函数来删除 0 度的顶点,但我想不幸的是它不起作用(也许是因为自循环基因的度数为 1)。
bad.vs<-V(InnatedGraph)[degree(InnatedGraph) == 0]
clean <-delete.vertices(InnatedGraph, bad.vs)
我尝试使用 BioNet 包 "rmSelfLoops" 中的另一个函数,借助它我能够移除自循环边,但仍然无法移除没有边的顶点。
test<-rmSelfLoops(InnatedGraph)
为了便于理解,我还会附上一张我的网络图片。
考虑这个示例图及其两个 subsets/modifications:
library(igraph)
set.seed(1)
g <- random.graph.game(10, p.or.m = 3, "gnm") + edge(7,7)
coords <- layout.auto(g)
par(mfrow = c(1,3))
plot(g, layout = coords)
plot(simplify(g), layout = coords) # remove loops and multiple edges
plot(delete.vertices(simplify(g), degree(g)==0)) # additionally delete isolated nodes
使用来自评论的 OP 示例数据:
df <- read.csv(header=F, row.names = 1, stringsAsFactors=F, text='"53","ENSG00000175104","ENSG00000175104"
"54","ENSG00000174775","ENSG00000175104"
"55","ENSG00000032688","ENSG00000027164"
"56","ENSG00000175104","ENSG00000140968"
"57","ENSG00000027164","ENSG00000041515"
"58","ENSG00000027164","ENSG00000025498"')
library(igraph)
( dfclean <- subset(df, V2!=V3) ) # remove rows where source==target
# V2 V3
# 54 ENSG00000174775 ENSG00000175104
# 55 ENSG00000032688 ENSG00000027164
# 56 ENSG00000175104 ENSG00000140968
# 57 ENSG00000027164 ENSG00000041515
# 58 ENSG00000027164 ENSG00000025498
par(mfrow = c(1,3))
plot(graph_from_data_frame(df), edge.arrow.size = .5) # orig
plot(simplify(graph_from_data_frame(df)), edge.arrow.size = .5) # same as
plot(graph_from_data_frame(dfclean), edge.arrow.size = .5) # this one
使用函数 graph_from_adjacency_matrix
将邻接矩阵转换为图形并设置参数 diag=F
。
那应该摆脱自循环。