Creating Subgraph using igraph in R
Asked Answered
D

2

5

I need to obtain a subgraph of the seed nodes (the input list of nodes; file.txt) and their first interactors (neighbours) from a graph (g) using igraph. Unfortunately, I am ending up with only a single node in the subgraph, and not all the rest of the nodes and edges (vertices) which interlink them.

g<-read.graph("DATABASE.ncol",format="ncol",directed=FALSE) #load the data
g2<-simplify(g, remove.multiple=TRUE, remove.loops=TRUE) # Remove the self-loops in the data
DAT1 <- readLines("file.txt")   #It provides a character vector right away
list_nodes_1 = neighbors(g2, DAT1) #list of nodes to be fetched in subnetwork
list_nodes_1 # 16
g3 <- induced.subgraph(graph=g2,vids=DAT1) #subnetwork construction
g3 # GRAPH UN-- 1 0 --; indicating only one node
plot (g3)

Any suggestions for obtaining the whole subnetwork (including nodes and vertices)? or is there any other function available for creating sub-graphs?

DATABASE.ncol:

MAP2K4  FLNC
MYPN    ACTN2
ACVR1   FNTA
GATA2   PML
RPA2    STAT3
ARF1    GGA3
ARF3    ARFIP2
ARF3    ARFIP1
XRN1    ALDOA
APP     APPBP2
APLP1   DAB1
CITED2  TFAP2A
EP300   TFAP2A
APOB    MTTP
ARRB2   RALGDS
CSF1R   GRB2
PRRC2A  GRB2
LSM1    NARS
SLC4A1  SLC4A1AP
BCL3    BARD1

It is a simple text file with one edge per line. An edge is defined by two symbolic vertex names delimited by tab:

file.txt

ALDOA
APLP1
GRB2
RPA2
FLNC
BCL3
APP
RALGDS
PRRC2A
NARS
LSM1
GGA3
FNTA
Donndonna answered 15/5, 2014 at 15:6 Comment(5)
Actually, there's no edge between the nodes in your example, so the resulting subgraph seems correct to me... what's your expected output ?Endocranium
@digEmAll::It is just a sample file (file.txt) buddy. I have a long list of the seed nodes comprising of entries from both the columns of DATABASE.ncol. Any ideas??Donndonna
in my tests induce.subgraph returns a subgraph with edges included, so could you provide a small example that reproduces the problem? What you have posted is really close to that; just change the names of the nodes in order to obtain the expected mis-behaviour and post what is your desired output...Endocranium
@digEmAll: With your kind suggestions, I have rephrased my question and sample data now. Please check.Donndonna
I'm still not really sure, but I have tried to give you an answer ;)Endocranium
E
6

I'm not sure to have completely understood your problem, so I have created a (hopefully) self-explanatory example:

# for example reproducibility
set.seed(123)

# create a fake undirected graph
D <- read.table(
sep=',',
header=T,
text=
'from,to
A,B
A,C
D,E
F,G
H,I')

g1 <- graph.data.frame(D,directed=F)
plot(g1)

# we want a sub-network containing the floowing nodes:
subv <- c('A','B','H')

# first method: 
# create a sub-network composed by ONLY the nodes in subv and the edges 
# between them
g2 <- induced.subgraph(graph=g1,vids=subv)
plot(g2)

# second method: 
# create a sub-network composed by the nodes in subv and, if some of them is
# connected to other nodes (even if not in subv), take also them 
# (and of course include all the edges among this bunch of nodes). 
sg1 <- decompose.graph(g1,mode="weak")
neighverts <- unique(unlist(sapply(sg1,FUN=function(s){if(any(V(s)$name %in% subv)) V(s)$name else NULL})))
g3 <- induced.subgraph(graph=g1,vids=neighverts)
plot(g3)

Graph g1 :

g1

Graph g2 :

g2

Graph g3 :

g3

Endocranium answered 16/5, 2014 at 14:56 Comment(0)
T
5

There is a built-in igraph function for that. Try make_ego_graph():

library(igraph)
graph <- make_ring(7)
V(graph)$name <- c("A", "B", "C", "D", "E", "F", "G")

# Get the list of induced subgraphs
subgraph <- make_ego_graph(graph, order=1, c("A", "D", "F"))

Graph and it's three subgraphs

Teriteria answered 1/11, 2016 at 18:51 Comment(1)
Thanks for the help, make_ego_graph creates a list into your subgraph. How did you graph subgraph which is a list of three nodes?Berylberyle

© 2022 - 2024 — McMap. All rights reserved.