snapshot of 4.0a development. initial git repo commit
[erange.git] / makeGraphs.py
1 import sys, os
2
3
4 def getEdges(nodeList, shorten=False):
5     edgeDict = {}
6
7     for nodeEntry in nodeList:
8         try:
9             (node1, node2, count) = nodeEntry.strip().split("\t")
10         except ValueError:
11             continue
12
13         if shorten:
14             try:
15                 node1 = node1.split("_")[1]
16             except IndexError:
17                 pass
18
19             try:
20                 node2 = node2.split("_")[1]
21             except IndexError:
22                 pass
23
24         node1Detail = (node1, int(count))
25         node2Detail = (node2, int(count))
26         try:
27             if node2Detail not in edgeDict[node1]:
28                 edgeDict[node1].append(node2Detail)
29         except KeyError:
30             edgeDict[node1] = [node2Detail]
31
32         try:
33             if node1Detail not in edgeDict[node2]:
34                 edgeDict[node2].append(node1Detail)
35         except KeyError:
36             edgeDict[node2] = [node1Detail]
37
38     return edgeDict
39
40
41 def getEdgesFromFile(inFileName, shorten=False):
42
43     infile = open(inFileName)
44     edgeDict = getEdges(infile, shorten)
45     infile.close()
46
47     return edgeDict
48
49
50 def getOutputLine(currentNode, node, nodeCount):
51     if nodeCount > 2:
52         outputLine = '\t"%s" -- "%s" [ label = "%d", penwidth=%d, color="red", constraint=false] ; \n' % (currentNode, node, nodeCount, nodeCount)
53     else:
54         outputLine = '\t"%s" -- "%s" [ label = "%d", color="red", constraint=false] ; \n' % (currentNode, node, nodeCount)
55
56     return outputLine
57
58
59 infilename = sys.argv[1]
60 outprefix = sys.argv[2]
61
62 shorten = False
63 if "-shorten" in sys.argv:
64     shorten = True
65
66 edgeDict = getEdgesFromFile(infilename, shorten)
67
68 nodeList = edgeDict.keys()
69 seenNodeDict = {}
70 seenEdgeDict = {}
71 currentNodeList = []
72 currentEdgeList = []
73 treeList = []
74 localCount = []
75
76 outstat = open("%s.stats" % outprefix,"w")
77 outstat.write("#gID\tnodes\tedges\tweight\n")
78
79 def visitNodes(currentNode):
80     if currentNode in seenNodeDict:
81         return
82
83     seenNodeDict[currentNode] = []
84     for (node, nodeCount) in edgeDict[currentNode]:
85         nodePair = [node, currentNode]
86         nodePair.sort()
87         if str(nodePair) not in seenEdgeDict:
88             if node not in currentNodeList:
89                 currentNodeList.append(node)
90
91             outputLine = getOutputLine(currentNode, node, nodeCount)
92             currentEdgeList.append(outputLine)
93             seenEdgeDict[str(nodePair)] = 0
94             localCount[0] += nodeCount
95             try:
96                 visitNodes(node)
97             except:
98                 pass
99
100 print "getting trees"
101 for node in nodeList:
102     if node not in seenNodeDict:
103         currentNodeList = [node]
104         currentEdgeList = []
105         localCount = [0]
106         outfile = open("%s.%s.gv" % (outprefix, node), "w")
107         treeList.append(node)
108         outfile.write("graph g%s {\n" % node)
109         visitNodes(node)
110         currentNodeList.sort()
111         outfile.write('subgraph G0 {\n\t"%s" ' % currentNodeList[0])
112         for anode in currentNodeList[1:]:
113             outfile.write('-- "%s" ' % anode)
114
115         outfile.write(" [ weight = 100 ] ;\n\tordering = out ;\n}\n")
116         for line in currentEdgeList:
117             outfile.write(line)
118
119         outfile.write("}\n")
120         outfile.close()
121         outstat.write("%s\t%d\t%d\t%d\n" % (node, len(currentNodeList), len(currentEdgeList), localCount[0]))
122
123 print "generating pngs"
124 for node in treeList:
125     output = os.popen("dot -Tpng %s.%s.gv > %s.%s.png" % (outprefix, node, outprefix, node))
126
127 outstat.close()