11

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)

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)

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.enter image description here

zx8754
  • 52,746
  • 12
  • 114
  • 209
Saad
  • 381
  • 2
  • 6
  • 12
  • 2
    Before converting to igraph object, remove rows from the dataframe where `fromColumn` and `toColumns` are the same value? – zx8754 Oct 17 '16 at 12:11
  • 1
    Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – zx8754 Oct 17 '16 at 12:11
  • @zx8754 Thank you so much , i would try that ! – Saad Oct 17 '16 at 12:15
  • @zx8754, can you please help me with the row removal, i searched alot and found removal of duplicated rows but not exactly what i want. – Saad Oct 17 '16 at 13:08
  • Provide example data, please. Then I am sure @lukeA can address your problem better. – zx8754 Oct 17 '16 at 13:13
  • For posterity, dropping rows that represent self-edges may inadvertently drop isolates. It is safer to leave the edgelist intact and use `simplify` as mentioned by @lukeA. – Brooks Ambrose Nov 29 '21 at 17:34

2 Answers2

19

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

enter image description here


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
lukeA
  • 53,097
  • 5
  • 97
  • 100
  • So i have a data frame like this – Saad Oct 17 '16 at 13:16
  • "53","ENSG00000175104","ENSG00000175104" "54","ENSG00000174775","ENSG00000175104" "55","ENSG00000032688","ENSG00000027164" "56","ENSG00000175104","ENSG00000140968" "57","ENSG00000027164","ENSG00000041515" "58","ENSG00000027164","ENSG00000025498" you can see the first row the same gene has an edge with it self , how can i delete this row from my data frame? – Saad Oct 17 '16 at 13:20
  • 1
    @Saad: Create the graph `g`, use `simplify(g)`. – lukeA Oct 17 '16 at 13:31
  • @lukeA you shouldn't use `simplify(g)` and `degree(g)==0` inside `delete.vertices` in one call, because it wouldn't remove vertices that had only self loop. You should either modify it to something like: `delete.vertices(simplify(g), degree(simplify(g))==0)` or do it in two steps – gawi Jun 04 '19 at 16:47
1

Use the function graph_from_adjacency_matrix to convert your adjacency matrix into a graph and set the argument diag=F.

That should get rid of the self-loops.

moondaisy
  • 4,303
  • 6
  • 41
  • 70
brawni
  • 21
  • 3