删除无边的自环和顶点 [英] Remove self loops and vertex with no edges

查看:172
本文介绍了删除无边的自环和顶点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个基因网络.我有一个两列数据框,将其转换为邻接矩阵,并在igraph中使用它.问题是我有带有自环的基因,我想摆脱自环,然后摆脱网络中没有边(也许是孤立的)的顶点.我已经尝试了一些方法,但是以某种方式它们不起作用.我的代码是

I am constructing a gene network. I have a two column data frame which i converted into and adjacency matrix and use it in igraph. The problem is i have genes that have self loops, I want to get rid of self loops and then get rid of the vertices that have no edge (isolated maybe) from the network. I have tried a few things but somehow they do not work. My code is

 InnatedGraph <- graph.data.frame(innate,directed=FALSE)
 V(InnatedGraph)$label.cex = 0.4
 plot(InnatedGraph,vertex.label=V(InnatedGraph)$symbol, vertex.size=5)

先天是我的两列数据框.我已经尝试过度函数来删除0度的顶点,但不幸的是我想它不起作用(也许因为自环基因的度为1).

innate is my two column data frame. I have tried the degree function to remove vertices with 0 degree but i guess unfortunately it does not work (maybe becoz the self loop genes have degree 1).

bad.vs<-V(InnatedGraph)[degree(InnatedGraph) == 0] 
clean <-delete.vertices(InnatedGraph, bad.vs)

我尝试使用BioNet软件包"rmSelfLoops"中的另一个功能,借助该功能,我可以删除自环边,但仍然无法删除没有边的顶点.

I tried using another function from package BioNet "rmSelfLoops" with the help of which i was able to remove the self loop edges but then still unable to remove the vertices with no edges.

test<-rmSelfLoops(InnatedGraph)

我还将附上一张我的网络图片,以使其更易于理解.

I will also include a picture of my network just to make it easier to understand.

推荐答案

请考虑以下示例图及其两个子集/修改:

Consider this example graph and its two 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的示例数据:

Using OP's sample data from the comment:

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

这篇关于删除无边的自环和顶点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆