forked from shpakoo/YAP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traverser.py
executable file
·336 lines (268 loc) · 11.4 KB
/
traverser.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
########################################################################################
## This file is a part of YAP package of scripts. https://github.com/shpakoo/YAP
## Distributed under the MIT license: http://www.opensource.org/licenses/mit-license.php
## Copyright (c) 2011-2013 Sebastian Szpakowski
########################################################################################
#################################################
## traverser - create a DOT (grahviz)
## representation of the performed pipeline steps.
#################################################
import sys, glob
from optparse import OptionParser
from collections import defaultdict
import re
_author="Sebastian Szpakowski"
_date="2011/01/01"
_version="Version 1"
#################################################
## Classes
##
class Node:
def __init__(self, file):
self.path = file
self.workpathid = file[1:].strip().split("_")[0]
self.label = "_".join(file[1:].strip().split("_")[1:])
self.has_manifest = False
if self.workpathid == "tep":
self.workpathid = file[1:].strip().split("_")[-1]
self.label = "_".join(file[1:].strip().split("_")[1:-1])
#### mapping type - path for files
self.inputs = defaultdict(set)
#### mapping type - name for files
self.outputs = defaultdict(set)
self.outputids = defaultdict(str)
#### mapping arg val for program's arguments
self.arguments= dict()
self.parseManifest()
def parseManifest(self):
try:
fp = open("%s/%s.manifest" % (self.path, self.workpathid), "r")
lines=fp.readlines()
fp.close()
self.has_manifest=True
except:
lines = list()
counter=0
for line in lines:
line = line.strip("\n").split("\t")
if line[0] == "output":
type = line[1]
if type not in ("e", "o", "pe", "po", "r"):
files = line[2].split(",")
if len(files)<5:
for file in files:
if not file.startswith("[var]"):
file = file.split("/")[-1]
else:
file = file[5:]
counts = len(file.split("-"))
if counts>1:
file = "%s\ \(%s\)\ %s" % (type, counts, file)
else:
file = "%s\ %s" % (type, file)
if len(file)>120:
file = "...%s" % file[-96:]
self.outputs[type].add(file)
self.outputids[file] = "<f%s>" % (counter)
counter+=1
else:
id = files[0]
id = id.split("/")[-1]
id = id.split(".")[0]
file = "%s [%s files] %s" % ( id, len(files), type )
self.outputs[type].add(file)
self.outputids[file] = "<f%s>" % (counter)
counter+=1
elif line[0] == "input":
type = line[1]
files = line[2].split(",")
if len(files)<5:
for file in files:
if file.startswith("[var]"):
file = file[5:]
counts = len(file.split("-"))
if counts>1:
file = "%s\ \(%s\)\ %s" % (type, counts, file)
else:
file = "%s\ %s" % (type, file)
elif len(file.split("~"))>1:
file = file.split("~")[0]
else:
file = file.split("/")[-1]
if len(file)>120:
file = "...%s" % file[-96:]
self.inputs[type].add(file)
else:
### group by id
ids = defaultdict(list)
for file in files:
id = file
id = id.split("/")[-1]
id = id.split(".")[0]
ids[id].append(file.split("/")[-1])
for id in ids.keys():
files = ids[id]
if len(files)<5:
for file in files:
self.inputs[type].add(file)
else:
file = "%s [%s files] %s" % ( id, len(files), type )
self.inputs[type].add(file)
#print (file)
elif line[0] == "argument":
if len(line)==2:
self.arguments[line[1]] = " "
elif line[1] in ["postprocess", "awk"]:
val = re.escape(line[2].replace("-", "_"))
#print "huh", val, line[2]
self.arguments[line[1]] = val
else:
self.arguments[line[1]] = line[2]
def getIns(self):
otpt = list()
for type, values in self.inputs.items():
for value in values :
otpt.append((value, type))
return (otpt)
def getOuts(self):
otpt = list()
types = self.outputs.keys()
types.sort()
#for type, values in self.outputs.items():
for type in types:
values = self.outputs[type]
for value in values :
id = self.outputids[value]
otpt.append((value, id, type))
return (otpt)
def getLabel(self):
otpt = "%s [%s]" % (self.label, self.workpathid)
for arg, val in self.arguments.items():
tmp = list()
for v in val.split("-"):
for vv in v.split(","):
vv = vv.strip()
if len(vv)>0:
tmp.append(vv)
otpt = "%s\\n%s -\\> %s " % (otpt, arg, "\\n-\\> ".join(tmp))
return (otpt)
def getNodeID(self):
tmp = "%s_%s" % (self.workpathid, self.label)
#print tmp
tmp = tmp.replace(".","_").replace("-", "_")
return (tmp)
def __str__(self):
otpt = "[%s]\n%s\n" % (self.workpathid, self.getLabel())
return (otpt)
#################################################
## Functions
##
#################################################
## Arguments
##
parser = OptionParser()
#parser.add_option("-f", "--file", dest="filename",
# help="write report to FILE", metavar="FILE")
#parser.add_option("-q", "--quiet",
# action="store_false", dest="verbose", default=True,
# help="don't print status messages to stdout")
(options, args) = parser.parse_args()
#################################################
## Begin
##
nodes = dict()
outs = dict()
for file in glob.glob("./S*_*"):
file = file.strip("./")
tmp = Node(file)
if tmp.has_manifest:
nodes [file] = tmp
# print """digraph Workflow {
#
# """
#
# for node in nodes.values():
# x = node.getOuts()
# if len(x)==0:
# print """node [ width=3 height=3 label="%s" shape=circle style=filled color="green" ] N_%s ;""" % (node.getLabel(), node.getNodeID())
# else:
# print """node [ width=3 height=3 label="%s" shape=box style=filled color="gray" ] N_%s ;""" % (node.getLabel(), node.getNodeID())
#
#
# for file, type in node.getOuts():
# if outs.has_key(file):
# #print node, file, type
# pass
# else:
# outs[file]=node
#
# for node in nodes.values():
# for file, type in node.getIns():
# if outs.has_key(file):
# print """edge [ label="%s [%s]" arrowhead=normal penwidth=5 color="black"] N_%s -> N_%s ;""" % (file.replace(".", "\\."), type.replace(".", "\\."), outs[file].getNodeID() , node.getNodeID())
# #else:
# # print file
#
#
# print "}"
print """digraph Workflow {
graph
[
rankdir = "LR"
fontsize = "15"
];
"""
for node in nodes.values():
x = node.getOuts()
label = "<origin> %s" % (node.getLabel())
for file, id, type in node.getOuts():
if outs.has_key(file):
#print node, file, type
pass
else:
outs[file]=node
label = """%s | %s \\"%s\\" """ % (label, id, file )
if len(x)==0:
print """
"N_%s"
[
label="%s"
shape="Mrecord"
fillcolor="black"
penwidth="5"
fontcolor="white"
fontsize=20
color="lightgray"
style="filled"
] ;""" % (node.getNodeID(), label )
else:
print """
"N_%s"
[
label="%s"
shape="Mrecord"
fillcolor="lightgray"
penwidth="2"
style="filled"
]; """ % (node.getNodeID(), label)
for node in nodes.values():
for file, type in node.getIns():
#print file, type
if outs.has_key(file):
print """edge [ label="[%s]" arrowhead=normal penwidth=3 color="lightgray"] N_%s : %s -> N_%s : <origin> ;""" % (type.replace(".", "\\."), outs[file].getNodeID(), outs[file].outputids[file], node.getNodeID() )
else:
dummynodeid =file.replace(".", "").replace("/", "").replace("-", "")
label = file.replace(".", "\.")
print """ "N_%s"
[
label="%s"
shape="folder"
fillcolor="red"
penwidth="5"
style="filled"
] ;""" % ( dummynodeid, label )
print """edge [ label="[%s]" arrowhead=normal penwidth=5 color="black"] N_%s -> N_%s : <origin> ;""" % (type.replace(".", "\\."), dummynodeid , node.getNodeID() )
print "}"
#################################################
## Finish
#################################################