-
Notifications
You must be signed in to change notification settings - Fork 1
/
ftemplate.py
308 lines (235 loc) · 8.23 KB
/
ftemplate.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
#!/usr/bin/python
'''
fill out a template file
save as a new file
call qsub on the file
'''
import inspect,os,re,time,random,subprocess
qsub_log_dir = '/home/vicker/qsub_logs'
def next_line(fname,sep='guess',comment='#',skip_blank=True):
'''
return next line of file split by separator guessed from file extension
or given as option
sep = None => do not split into tokens
sep = '' => whitespace as separator
'''
if type(fname) == str:
#treat fname as file name
f = open(fname)
if sep == 'guess':
#guess separator from filename extension
exts = {'tsv':'\t', 'tab':'\t', 'csv':',', 'ssv':''}
ext = fname.split('.')[-1]
assert ext.lower() in exts.keys()
sep = exts[ext.lower()]
else:
#treat fname as file handle
f = fname
assert sep != 'guess'
for line in f:
line = line.strip()
#remove any comment
if comment != None:
if line.find(comment) != -1:
line = line[:line.index(comment)].strip()
#skip blank lines
if line == '': continue
if sep != None:
#split using separator, yield token list
tok = line.split(sep)
yield tok
else:
#yield whole line
yield line
f.close()
def next_sample(fname):
'''
get next non commented sample name
skip blank lines
'''
f = open(fname)
for line in f:
if line == '': break
line = line.strip()
if line.find('#') != -1:
line = line[:line.index('#')].strip()
if line == '': continue
yield line
f.close()
def myqsub(inp,out=None,dic=None,sge=True,wait=False,makelog=True,templatedir='job_templates/'):
'''
inp - template file
out - output file
dic - key:value dictionary
replace @{key} with value for all placeholders in the input file
write to output file
run qsub
if dic == None, get key value pairs from name space of calling function
sge == True => fix commented shell variables that confuse sun grid engine
out == None => automatically generate a name, in tmp/ if it exists
wait ==> wait for job to complete before returning
makelog ==> create a directory '../logs' if it does not exist
'''
#create logs directory if not already present
if makelog == True:
if not os.path.exists('logs'):
os.makedirs('logs')
#get local variables from calling function
if dic == None: dic = get_locals(inspect.currentframe().f_back.f_locals)
#create random name for output file
if out == None:
out = 'tmp/' + str(random.random())[-6:] + '.' + ('%.9f'%time.time()).split('.')[1] + '.sge'
if not os.path.exists('tmp'): os.makedirs('tmp')
if not os.path.exists(inp):
if templatedir != None:
inp = os.path.join(templatedir,inp)
#fill out the template file, save to new file
ftemplate(inp,out,dic,sge)
#call qsub on the new file
#capture qsub output to a file
#output should contain the jobnumber
if wait:
os.system('qsub -sync y '+out+' | tee '+out+'.qsub')
else:
os.system('qsub '+out+' | tee '+out+'.qsub')
#get job number from the file
f = open(out+'.qsub')
data = f.read()
f.close()
jobnumber = int(data.strip().split()[2])
#store a copy of the script file to a global log directory
#rename the script file to be the job number
os.system('cp '+out+' '+qsub_log_dir+'/%d.sge'%jobnumber)
return jobnumber
def ftemplate(inp,out,dic=None,sge=True):
'''
inp - template file
out - output file
dic - key:value dictionary
replace @{key} with value for all placeholders in the input file
write to output file
if dic == None, get key value pairs from name space of calling function
sge == True => fix commented shell variables that confuse sun grid engine
'''
#get local variables from calling function
if dic == None: dic = get_locals(inspect.currentframe().f_back.f_locals)
#read template, fill out place holders
f = open(inp)
data = sub_templates(f.read(),dic,sge)
f.close()
#write out final file
fout = open(out,'wb')
fout.write(data)
fout.close()
def get_locals(tvar):
'''
extract only simple local variables from the frame
'''
dic = {}
allowed = [str,int,float,bool]
for k,v in tvar.iteritems():
if type(k) != str: continue
if not type(v) in allowed: continue
dic[k] = v
del tvar
return dic
def sub_templates(data,dic,sge=False):
'''
substitute values for place holders
'''
#replace @{key} with value
for key in dic.iterkeys():
if not '@{%s}'%key in data: continue
data = data.replace('@{%s}'%key, str(dic[key]))
#check all placeholders were replaced
if '@{' in data:
for m in re.finditer('@{.*}',data):
print m.group(0), 'not matched'
raise Exception
#stop commented shell variables from looking like
#sun grid engine parameters
if sge == True: data = data.replace('#${','# ${')
return data
def T(data,dic=None):
'''
fill out a template string
replaces @{key} with value
must replace all placeholders
not all keys need be present in the template
non string keys are ignored
'''
if dic == None:
#get local variables from calling function
dic = get_locals(inspect.currentframe().f_back.f_locals)
return sub_templates(data,dic)
def run(cmd,dic=None):
'''
fill out a template string
run as shell command then return stdout and stderr
replaces @{key} with value
must replace all placeholders
not all keys need be present in the template
non string keys are ignored
'''
if dic == None:
#get local variables from calling function
dic = get_locals(inspect.currentframe().f_back.f_locals)
cmd = sub_templates(cmd,dic)
return subprocess.check_output(cmd,shell=True)
def myqsub2(inp,out,dic=None,sge=True,bintype=None,arrayjobs=0,mem=1.0,rt=2,name='myjob',ops=None):
'''
like qsub but automatically generates the SGE boiler plate
inp - template file
out - output file
dic - key:value dictionary
replace @{key} with value for all placeholders in the input file
write to output file
run qsub
if dic == None, get key value pairs from name space of calling function
sge == True => fix commented shell variables that confuse sun grid engine
automatically generate the SGE boiler plate options
bintype guessed from extension of inp file, or set manually eg '/usr/bin/perl'
arrayjobs = number of array jobs
mem is gigabytes
rt is run time in hours
ops is list of options, minus the leading #$
eg ['-pe smp @{threads}']
'''
if dic == None:
#get local variables from calling function
dic = get_locals(inspect.currentframe().f_back.f_locals)
sge_header = []
if bintype == None:
#auto detect binary type
if inp.endswith('.py'):
sge_header.append('#$ -S /usr/bin/python')
elif inp.endswith('.sh'):
sge_header.append('#$ -S /bin/sh')
else:
raise Exception
else:
sge_header.append('#$ -S '+bintype)
sge_header.append('#$ -N '+name)
sge_header.append('#$ -cwd')
sge_header.append('#$ -V')
if arrayjobs == 0:
sge_header.append('#$ -o ../logs/$JOB_NAME.$JOB_ID.out')
sge_header.append('#$ -e ../logs/$JOB_NAME.$JOB_ID.err')
else:
sge_header.append('#$ -o ../logs/$JOB_NAME.$JOB_ID.$TASK_ID.out')
sge_header.append('#$ -e ../logs/$JOB_NAME.$JOB_ID.$TASK_ID.err')
sge_header.append('#$ -t 1:%d'%arrayjobs)
sge_header.append('#$ -l h_vmem=%.1fG'%mem)
sge_header.append('#$ -l h_rt=%d:00:00'%rt)
if ops != None:
for x in ops:
sge_header.append('#$ '+x)
data = '\n'.join(sge_header) + '\n\n'
f = open(inp)
data += f.read()
f.close()
data = sub_templates(data,dic,sge)
fout = open(out,'wb')
fout.write(data)
fout.close()
os.system('qsub %s'%out)