-
Notifications
You must be signed in to change notification settings - Fork 0
/
edges2abc.py
executable file
·43 lines (35 loc) · 1.16 KB
/
edges2abc.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
# coding=utf-8
# Convert edge files into simple abc graph format.
# format: node1 node2 [weight]
import sys
if(len(sys.argv) < 1):
print("Please, supply an edge file.")
exit()
edge_filename = sys.argv[1]
graph_name = ".".join(edge_filename.split(".")[:-1]) + ".abc"
eq_list = ".".join(edge_filename.split(".")[:-1]) + ".eq_list"
nodes = set()
edges = []
# Parsing edgefile
with open(edge_filename) as edgeFile:
for i, line in enumerate(edgeFile):
if(i >= 2):
data = line.rstrip("\n").split("\t")
nodes.add(data[0])
nodes.add(data[2])
weight = len(data[6:])
edges.append((data[0], data[2], weight))
# converting node set to indexable node list
node_list = sorted(list(nodes))
# exporting equivalence list
with open(eq_list, "w") as eq_out:
for i, node in enumerate(node_list):
eq_out.write(node + " " + str(i) + "\n")
# exporting graph in abc format
with open(graph_name, "w") as graph:
for edge in edges:
n1 = node_list.index(edge[0])
n2 = node_list.index(edge[1])
w = edge[2]
graph.write(" ".join(str(el) for el in [n1, n2, w]))
graph.write("\n")