我正在建立一个基因网络。我有一个两列数据框,我转换成和邻接矩阵,并在igraph中使用它。问题是我有自我循环的基因,我想摆脱自我循环,然后摆脱没有边缘(可能与网络隔离)的顶点。我尝试了一些东西,但不知怎的,它们不起作用。我的代码是
InnatedGraph <- graph.data.frame(innate,directed=FALSE)
V(InnatedGraph)$label.cex = 0.4
plot(InnatedGraph,vertex.label=V(InnatedGraph)$symbol, vertex.size=5)
先天是我的两列数据框架。我已经尝试过度函数去除0度的顶点,但我猜不幸它不起作用(也许becoz自环基因有1度)。
bad.vs<-V(InnatedGraph)[degree(InnatedGraph) == 0]
clean <-delete.vertices(InnatedGraph, bad.vs)
我尝试使用BioNet&#34; rmSelfLoops&#34;在我的帮助下,我能够移除自循环边但仍然无法移除没有边的顶点。
test<-rmSelfLoops(InnatedGraph)
答案 0 :(得分:11)
考虑这个示例图及其两个子集/修改:
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
答案 1 :(得分:0)
使用函数graph_from_adjacency_matrix
将邻接矩阵转换为图形并设置参数diag=F
。
那应该摆脱自我循环。