Extraction of a sub-network from a parent network

129 views
Skip to first unread message

ashwani sharma

unread,
Nov 24, 2009, 12:29:17 AM11/24/09
to networkx...@googlegroups.com
Hi everyone


I am very new with graph and I am facing a problem in dealing with it.

I need to extract a sub network or graph from a parent one.

my parent input file is having 2 column in it. (basically these 2 columns are genes interacting with each other, but this is not necessary here I guess).


other input file let say "sub_network.txt"is having a single column. (this column also contains list genes which are from parent file only.)

I hope I am clear here in explaining my problem.

Any help will be highly appreciated.
Thanks in advance


Regards,

Ashwani


Sudarshan Iyengar

unread,
Nov 24, 2009, 8:32:10 AM11/24/09
to networkx...@googlegroups.com
use
>>>import networkx as nx
>>>G=nx.read_adjlist('filename')
This will create a graph G. Then use
>>>H=nx.subgraph(G,list)
where list contains the list of the vertices on which you want the
induced subgraph.

I suppose this is what you want.

Sudarshan
> --
>
> You received this message because you are subscribed to the Google Groups
> "networkx-discuss" group.
> To post to this group, send email to networkx...@googlegroups.com.
> To unsubscribe from this group, send email to
> networkx-discu...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/networkx-discuss?hl=en.
>

ashwani sharma

unread,
Nov 25, 2009, 3:20:26 AM11/25/09
to networkx...@googlegroups.com
Hi Sudarshan,

Thanks a lot for helping me out. I did as you suggested.

Here is my code snippet :
import networkx as nx

G=nx.read_adjlist('sailu_core.txt')

#This will create a graph G. Then use

f1 = open('ess_new.txt','r')

i=0

ess = []
for line in f1:
        ess.append(line.rstrip())
        i=i+1;
len = len(ess)
print len       

H=nx.subgraph(G,ess)

Now its not showing any error while running. But how can I get to now that sub graph has generated.

Thanks once again

 Regards,
 Ashwani

Dan Schult

unread,
Nov 25, 2009, 1:34:38 PM11/25/09
to networkx...@googlegroups.com

On Nov 25, 2009, at 3:20 AM, ashwani sharma wrote:
> [ ... ]

> H=nx.subgraph(G,ess)
>
> Now its not showing any error while running. But how can I get to
> now that sub graph has generated.


The subgraph is now in H.

What do you want to do with the subgraph?
You can use e.g. H.nodes(), H.edges(),
nx.write_adjlist(H,"newgraph.txt")
see: http://networkx.lanl.gov/ for more info.
The sky is the limit.... :)
Dan

Sudarshan Iyengar

unread,
Nov 25, 2009, 11:20:41 PM11/25/09
to networkx...@googlegroups.com
On Wed, Nov 25, 2009 at 1:50 PM, ashwani sharma <ashwan...@gmail.com> wrote:
> Hi Sudarshan,
>
> Thanks a lot for helping me out. I did as you suggested.
>
> Here is my code snippet :
> import networkx as nx
>
> G=nx.read_adjlist('sailu_core.txt')
>
> #This will create a graph G. Then use
>
> f1 = open('ess_new.txt','r')
>
> i=0
>
> ess = []
> for line in f1:
>         ess.append(line.rstrip())
>         i=i+1;
> len = len(ess)
> print len
>
> H=nx.subgraph(G,ess)
>
> Now its not showing any error while running. But how can I get to now that
> sub graph has generated.
>
> Thanks once again
>
>  Regards,
>  Ashwani
>
Hi ashwani,

A better way to write the graph is to use nx.write_adjlist(G,'output.txt')
This will create an adjacency list of the graph G in 'output.txt'

best,
sudarshan

ashwani sharma

unread,
Nov 26, 2009, 1:12:44 AM11/26/09
to networkx...@googlegroups.com
Hi

this is what I tried and I am explaining below :

import networkx as nx
import matplotlib.pyplot as plt
G=nx.read_adjlist('sailu_core.txt')


f1 = open('ess_new.txt','r')

i=0

ess = []
for line in f1:
        ess.append(line.rstrip())
        i=i+1;
len = len(ess)
print len                           #its printing length (which is 280) fine.



H=nx.subgraph(G,ess)


nx.write_adjlist(H,'output.txt')    # no output is coming in file "output.txt"
nx.write_adjlist(G,'output1.txt')   # output is coming in file "output1.txt"



print H.number_of_nodes()           # output is 0   i.e; grapgh is not generated
print G.number_of_nodes()           # output is 78031   i.e; grapgh is generated


#nx.draw(G)
#plt.show()


Sorry guys, I am bugging u again and again.....but its not working for me :( . Kindly tell me what changes should be in the above code to make it work properly.

Regards,
Ashwani

Dan Schult

unread,
Nov 26, 2009, 8:30:25 AM11/26/09
to networkx...@googlegroups.com
Most likely the strings you are loading from the ess_new.txt file
don't match the strings loaded from sailu_core.txt.  They may have
extra white space so that they look the same, but they have to be
exactly the same strings or they won't match nodes of G.

You might need .strip() instead of .rstrip()

ashwani sharma

unread,
Nov 27, 2009, 5:03:40 AM11/27/09
to networkx...@googlegroups.com
Hi

sailu_core.txt contains :
aaea    aaeb
aaea    aaer
aaea    accd
aaea    acre
aaea    acrf
aaea    cvpa
aaea    emrb
aaea    exbb
aaea    exbd
aaea    fece
aaea    feci
aaea    fecr
aaea    fhla
aaea    flid
aaea    folc
aaea    glf
aaea    gutq
aaea    heme
aaea    hisp
aaea    kefb
aaea    lrha
 .              .
.               .
.               .            
.               .


and ess_new.txt contains :
ribf
lspa
isph
dapb
fola
imp
ftsl
ftsi
mure
murf
mray
murd
ftsw
murg
murc
ftsq
ftsa
ftsz
lpxc
seca
can
folk
heml
yadr
dapd
map
rpsb
.
.
 
I mean to say that I have run other many programs on the same files...like shortest path, path length etc........they all are running fine but this one.

Guys, plz Help me out here  :(

thanks n regards,
Ashwani

Aric Hagberg

unread,
Nov 27, 2009, 9:16:12 AM11/27/09
to networkx...@googlegroups.com
import networkx as nx

G=nx.read_adjlist('k4.adjlist')
print G.nodes()

nodes= [l.rstrip() for l in open('nodes.txt')]

H=nx.subgraph(G,nodes)
print H.nodes()
print H.edges()

---------------

# k4.adjlist
#/usr/bin/ipython
# GMT Fri Nov 27 13:58:49 2009
# complete_graph(4)
0 1 2 3
1 2 3
2 3
3

# nodes.txt
1
2

Aric

franck kalala

unread,
Nov 27, 2009, 9:51:15 AM11/27/09
to networkx...@googlegroups.com
Hey all;

How to create an Erdos Renyi random graph with n=1000 nodes, m=2000 edges and average connectivity <k>=4.

Average connectivity= sum_of_node_degree/n.

Cheers


De : Aric Hagberg <ahag...@gmail.com>
À : networkx...@googlegroups.com
Envoyé le : Ven 27 Novembre 2009, 14 h 16 min 12 s
Objet : Re: [networkx-discuss] Extraction of a sub-network from a parent network
--

You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To post to this group, send email to networkx...@googlegroups.com.
To unsubscribe from this group, send email to networkx-discuss+unsub...@googlegroups.com.

Aric Hagberg

unread,
Nov 27, 2009, 9:56:09 AM11/27/09
to networkx...@googlegroups.com
On Fri, Nov 27, 2009 at 7:51 AM, franck kalala <franck...@yahoo.fr> wrote:
> Hey all;
>
> How to create an Erdos Renyi random graph with n=1000 nodes, m=2000 edges
> and average connectivity <k>=4.
>
> Average connectivity= sum_of_node_degree/n.
>

>>> import networkx as nx
>>> G=nx.gnm_random_graph(1000,2000)

http://networkx.lanl.gov/reference/generated/networkx.gnm_random_graph.html#networkx.gnm_random_graph

Aric

franck kalala

unread,
Nov 29, 2009, 7:49:29 PM11/29/09
to networkx...@googlegroups.com
Is there any routine that implement the OREGON network graph?

The reference is here:
http://topology.eecs.umich.edu/data.html

Cheers

Envoyé le : Ven 27 Novembre 2009, 14 h 56 min 09 s
Objet : Re: [networkx-discuss] Erdos renyi graph with a given average connectivity

Aric Hagberg

unread,
Nov 29, 2009, 8:00:47 PM11/29/09
to networkx...@googlegroups.com
On Sun, Nov 29, 2009 at 5:49 PM, franck kalala <franck...@yahoo.fr> wrote:
> Is there any routine that implement the OREGON network graph?
>
> The reference is here:
> http://topology.eecs.umich.edu/data.html
>

It looks like those files are colon delimited edge lists of integer
nodes. In that case try:

>>> import networkx
>>> G=networkx.read_edgelist("peer.oregon.010331",delimiter=":",nodetype=int)

Aric

franck kalala

unread,
Nov 29, 2009, 8:22:55 PM11/29/09
to networkx...@googlegroups.com
Thanks Aric.

The command works.
It great that ,those file are incorporated in networkx.

How can  we modify those file, for example if I want to add some nodes or edges.

Franck
Envoyé le : Lun 30 Novembre 2009, 1 h 00 min 47 s
Objet : Re: Re : [networkx-discuss] OREGON network graph

franck kalala

unread,
Nov 30, 2009, 4:38:46 PM11/30/09
to networkx...@googlegroups.com
Hey all,

Sorry this is not networkx quaestion:
how to use multi legend in matplotlib?
for example if i want to plot the function
y=ax for a=1,2,3,5
and I want in figure to have the plot for each a and the legend for each a.

Cheers

Envoyé le : Lun 30 Novembre 2009, 1 h 00 min 47 s
Objet : Re: Re : [networkx-discuss] OREGON network graph

franck kalala

unread,
Nov 30, 2009, 8:19:02 PM11/30/09
to networkx...@googlegroups.com
hey all

how to create a 1000 nodes Barabasi_Alert power law graph, with average connectivity <k>=4?

Franck

Envoyé le : Lun 30 Novembre 2009, 1 h 00 min 47 s
Objet : Re: Re : [networkx-discuss] OREGON network graph

Sudarshan Iyengar

unread,
Nov 30, 2009, 10:05:21 PM11/30/09
to networkx...@googlegroups.com
>>>import networkx as nx
>>>G=nx.barabasi_albert_graph(1000,4)

G is now a BA graph with 1000 nodes and k=4.

>>>G.nodes()
will display all the nodes.

>>>G.edges()
Will print all the edges.

Sudarshan
> networkx-discu...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/networkx-discuss?hl=en.
>
>
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "networkx-discuss" group.
> To post to this group, send email to networkx...@googlegroups.com.
> To unsubscribe from this group, send email to
> networkx-discu...@googlegroups.com.

franck kalala

unread,
Dec 1, 2009, 4:58:55 AM12/1/09
to networkx...@googlegroups.com
The average connectivity is defined to be:

<k>=sum_of_degrees/number_of_node

if i check:

>> import networkx as nx
>> G=nx.barabasi_albert_graph(1000,4)
>> sum(G.degree())/1000.
>> 7.858

which is different to the connectivity I want, <k>=4.
I think the connectivity cannot play the role of m as defined here


http://networkx.lanl.gov/reference/generated/networkx.barabasi_albert_graph.html#networkx.barabasi_albert_graph

Franck


De : Sudarshan Iyengar <sudars...@gmail.com>
À : networkx...@googlegroups.com
Envoyé le : Mar 1 Décembre 2009, 3 h 05 min 21 s
Objet : Re: [networkx-discuss] 1000 nodes BA power law graph
> networkx-discuss+unsub...@googlegroups.com.

> For more options, visit this group at
> http://groups.google.com/group/networkx-discuss?hl=en.
>
>
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "networkx-discuss" group.
> To post to this group, send email to networkx...@googlegroups.com.
> To unsubscribe from this group, send email to
> networkx-discuss+unsub...@googlegroups.com.

> For more options, visit this group at
> http://groups.google.com/group/networkx-discuss?hl=en.
>

--

You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To post to this group, send email to networkx...@googlegroups.com.
To unsubscribe from this group, send email to networkx-discuss+unsub...@googlegroups.com.

Sudarshan Iyengar

unread,
Dec 1, 2009, 5:07:46 AM12/1/09
to networkx...@googlegroups.com
On Tue, Dec 1, 2009 at 3:28 PM, franck kalala <franck...@yahoo.fr> wrote:
> The average connectivity is defined to be:
>
> <k>=sum_of_degrees/number_of_node
>
> if i check:
>>> import networkx as nx
>>> G=nx.barabasi_albert_graph(1000,4)
>>> sum(G.degree())/1000.
>>> 7.858
>

it is quite right!

7.858 is approximately twice 4.

k=number of edges /number of nodes

number of edges = 2* sum_of_degrees

Sudarshan
>> networkx-discu...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/networkx-discuss?hl=en.
>>
>>
>>
>> --
>>
>> You received this message because you are subscribed to the Google Groups
>> "networkx-discuss" group.
>> To post to this group, send email to networkx...@googlegroups.com.
>> To unsubscribe from this group, send email to
>> networkx-discu...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/networkx-discuss?hl=en.
>>
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "networkx-discuss" group.
> To post to this group, send email to networkx...@googlegroups.com.
> To unsubscribe from this group, send email to
> networkx-discu...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/networkx-discuss?hl=en.
>
>
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "networkx-discuss" group.
> To post to this group, send email to networkx...@googlegroups.com.
> To unsubscribe from this group, send email to
> networkx-discu...@googlegroups.com.

ashwani sharma

unread,
Dec 2, 2009, 12:21:26 AM12/2/09
to networkx...@googlegroups.com
Hi guys,

Few days back I had a problem and I am still having it :(. I tried all suggestion given by Aric, Sudarshan, Dan. Thanks to them :).

Here again my code  and attachments are my input files.

I am wondering if any body can help me with given code and files.


import networkx as nx
import matplotlib.pyplot as plt

G=nx.read_adjlist('parent_file.txt')

f1 = open('Sub_graph_file.txt','r')


i=0

ess = []
for line in f1:
        ess.append(line.rstrip())
        i=i+1;
len = len(ess)
print len                           #its printing length (which is 280) fine.




H=nx.subgraph(G,ess)


nx.write_adjlist(H,'output.txt')    # no output is coming in file "output.txt"
nx.write_adjlist(G,'output1.txt')   # output is coming in file "output1.txt"



print H.number_of_nodes()           # output is 0   i.e; grapgh is not generated
print G.number_of_nodes()           # output is 78031   i.e; grapgh is generated


#nx.draw(G)
#plt.show()


thanks in advance,
Ashwani

parent_file.txt
Sub_Graph_file.txt
Reply all
Reply to author
Forward
0 new messages