-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitEdges.py
executable file
·49 lines (39 loc) · 1.43 KB
/
splitEdges.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# coding=utf-8
# Split an edge file in different communities based on a clustering.
import sys
import os
from collections import defaultdict as dd
edgeFileName = sys.argv[1]
clusterFile = sys.argv[2]
cluster_path = sys.argv[3] if len(sys.argv) >= 4 else "./tmp"
# Creating path to output clusters edge file
buff = ""
for element in os.path.split(cluster_path):
buff = os.path.join(buff, element)
if(not os.path.isdir(buff)):
os.mkdir(buff)
edges = dd(dict)
with open(edgeFileName, "r") as f:
for line in f:
data = line.rstrip("\n").split("\t")
if(len(data) > 2):
r1 = data[0]
r2 = data[2]
# Filling edge dictionnary
edges[r1][r2] = data
# making a set out of edge keys / sources
key_set = set(edges.keys())
with open(clusterFile) as f:
# each line is a cluster, line number is cluster number
for cl_nb, line in enumerate(f):
nodes = line.rstrip("\n").split("\t")
# concerting nodes in a cluster set
cluster = set(nodes)
out = open(os.path.join(cluster_path, "cluster_" + str(cl_nb) + ".edges") , 'w')
# starting by source node, find edges between elements in the cluster
for source in cluster & key_set:
for target in edges[source].keys():
if(target in nodes):
out.write("\t".join(edges[source][target]))
out.write("\n")
out.close()