Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
PARrOT/Graph_subcluster.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
150 lines (119 sloc)
5.3 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import graph_tool.all as gt | |
import csv | |
import re | |
import sys, getopt | |
import os | |
import argparse | |
from shutil import copyfile | |
#arguments | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--dir','-d',default='./', help="search dir for subgraphs [default: ./]") | |
parser.add_argument('--output','o',default='./', help="ouput directory [default: ./]") | |
args = parser.parse_args() | |
pattern = re.compile("^(subcluster_)") | |
for file in os.listdir(args.dir): | |
if pattern.search(file): | |
#find name and number | |
name = os.path.splitext(os.path.basename(file))[0] | |
number = name.split("_")[-1] | |
#load matrix | |
g=gt.load_graph_from_csv(file,skip_first=True,string_vals=True,csv_options={"delimiter":","},directed=False) | |
#g, pos = gt.triangulation(random((500, 2)) * 4, type="delaunay") | |
tree = gt.min_spanning_tree(g) | |
gt.graph_draw(g, edge_color=tree, output=args.output+"min_tree_sub_"+number+".svg") | |
#minimize blockmodel with and without overlap | |
state_sbm_overlap_ndc = gt.minimize_blockmodel_dl(g, overlap=True, deg_corr=False) | |
state_sbm_overlap_dc = gt.minimize_blockmodel_dl(g, overlap=True, deg_corr=True) | |
#equilibration | |
gt.mcmc_equilibrate(state_sbm_overlap_ndc) | |
gt.mcmc_equilibrate(state_sbm_overlap_dc) | |
if state_sbm_overlap_ndc.entropy() < state_sbm_overlap_dc.entropy(): | |
state_sbm_overlap_ndc.draw(output=args.output+"sbm_overlap_ndc_sub_"+number+".svg") | |
state_sbm_overlap=state_sbm_overlap_ndc | |
else: | |
state_sbm_overlap_dc.draw(output=args.output+"sbm_overlap_dc_sub_"+number+".svg") | |
state_sbm_overlap=state_sbm_overlap_dc | |
#minimize blockmodel | |
state_sbm_ndc = gt.minimize_blockmodel_dl(g, overlap=True, deg_corr=False) | |
state_sbm_dc = gt.minimize_blockmodel_dl(g, overlap=True, deg_corr=True) | |
#equilibration | |
gt.mcmc_equilibrate(state_sbm_ndc) | |
gt.mcmc_equilibrate(state_sbm_dc) | |
if state_sbm_ndc.entropy() < state_sbm_dc.entropy(): | |
state_sbm_ndc.draw(output=args.output+"sbm_ndc_sub_"+number+".svg") | |
state_sbm = state_sbm_ndc | |
else: | |
state_sbm_dc.draw(output=args.output+"sbm_dc_sub_"+number+".svg") | |
state_sbm = state_sbm_dc | |
#nested blockmodel with and without equilibration | |
#without equlibration | |
state_nested_ndc = gt.minimize_nested_blockmodel_dl(g, deg_corr=False) | |
state_nested_dc = gt.minimize_nested_blockmodel_dl(g, deg_corr=True) | |
if state_nested_ndc.entropy() < state_nested_dc.entropy(): | |
state_nested_ndc.draw(output=args.output+"nested_ndc_sub_"+number+".svg") | |
state_nested = state_nested_ndc | |
else: | |
state_nested_dc.draw(output=args.output+"nested_dc_sub_"+number+".svg") | |
state_nested = state_nested_dc | |
#with equilibration | |
state_nested_equil_ndc = state_nested_ndc.copy(sampling=True) | |
gt.mcmc_equilibrate(state_nested_equil_ndc) | |
state_nested_equil_dc = state_nested_dc.copy(sampling=True) | |
gt.mcmc_equilibrate(state_nested_equil_dc) | |
if state_nested_ndc.entropy() < state_nested_dc.entropy(): | |
state_nested_ndc.draw(output=args.output+"nested_equil_ndc_sub_"+number+".svg") | |
state_nested_equil = state_nested_equil_ndc | |
else: | |
state_nested_dc.draw(output=args.output+"nested_equil_dc_sub_"+number+".svg") | |
state_nested_equil = state_nested_equil_dc | |
#central edges view | |
bv, be = gt.betweenness(g) | |
u=gt.GraphView(g,directed=False) | |
gt.graph_draw(u,vertex_fill_color=bv, output=args.output+"central-edges-view_sub_"+number+".svg") | |
#write files | |
#sbm with overlap | |
b = state_sbm_overlap.get_majority_blocks() | |
d = {} | |
for i in range(0,len(list(b))): | |
d[g.vp.name[i]]=b[i] | |
with open(args.output+'block_member_sbm_overlap_sub_'+number+'.csv', 'w') as csv_file: | |
writer = csv.writer(csv_file) | |
for gene, group in d.items(): | |
writer.writerow([gene, group]) | |
#sbm | |
s = state_sbm.get_blocks() | |
e = {} | |
for i in range(0,len(list(g.vp.name))): | |
e[g.vp.name[i]]=s[i] | |
with open(args.output+'block_member_sbm_sub_'+number+'.csv', 'w') as csv_file: | |
writer = csv.writer(csv_file) | |
for gene, group in e.items(): | |
writer.writerow([gene, group]) | |
#nested sbm | |
lstate = state_nested.levels[0] | |
n=lstate.get_blocks() | |
f = {} | |
for i in range(0,len(list(n))): | |
f[g.vp.name[i]]=n[i] | |
with open(args.output+'block_member_nested_sub_'+number+'.csv', 'w') as csv_file: | |
writer = csv.writer(csv_file) | |
for gene, group in f.items(): | |
writer.writerow([gene, group]) | |
#nested sbm with equilibration | |
lstate2 = state_nested_equil.levels[0] | |
eq=lstate2.get_blocks() | |
h = {} | |
for i in range(0,len(list(eq))): | |
h[g.vp.name[i]]=eq[i] | |
with open(args.output+'block_member_nested_equil_sub_'+number+'.csv', 'w') as csv_file: | |
writer = csv.writer(csv_file) | |
for gene, group in h.items(): | |
writer.writerow([gene, group]) | |
#writing best model to file | |
states_value = [state_nested.entropy(),state_nested_equil.entropy(),state_sbm.entropy(),state_sbm_overlap.entropy()] | |
states = ['nested','nested_equil','sbm','sbm_overlap'] | |
state_best = states[states_value.index(min(states_value))] | |
copyfile('block_member_'+state_best+'_sub_'+number+".csv","./block_member.csv") | |
print(state_best+" is the best fitting algorithm and the memberships of it are saved to block_member.csv") | |
#save graph to file | |
g.save(args.output+"graph_"+number+".xml.gz") |