-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_script_discordant_mates_dense.py
executable file
·448 lines (362 loc) · 14.3 KB
/
create_script_discordant_mates_dense.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#!/usr/bin/env python
"""
Create a cluster job file to create a discordant-mates density discriminator
track.
"""
from sys import argv,stdin,stdout,stderr,exit
def usage(s=None):
message = """
usage: create_script_discordant_mates_dense [options] > discordant_mates_dense.sh
<sub>_<samp>_<type> (required) run descriptor; for example, CS_NORM_PE
means subject "CS", sample "NORM", and type "PE";
other filenames can use "{run}" to refer to this
string
--control=<filename> read control values from a file (see list below)
--base=<path> path prefix; other filenames can use "{base}" to
refer to this path
--chromosomes=<filename> read chromosome names and lengths from a file
(default is {base}/data/hg19.chrom_lengths)
--input=<filename> (required) track file to process
--track=<filename> (required) track file to create
(default is {base}/tracks/{run}.discordant_mates.dense)
--tempinput=<filename> temporary file to hold input track file, if
needed; only needed if the input track was gzipped
(default is {base}/tracks/{run}.discordant_mates.dense.scratch)
--temp=<filename> temporary file to hold track file, if needed; only
needed if --gzip and --bigwig are both used
(default is {base}/tracks/{run}.discordant_mates.dense.temp)
--gzip compress track file
--bigwig[=<filename>] create bigwig file in addition to track file
--bigwigchroms=<filename> chromosomes file for bedGraphToBigWig
(default is {base}/temp/ucsc.hg19.chrom_lengths)
--bigwigurl=<url> url for the bigwig file; this can use {bigwig}
for the bigwig filename
--bigwiglink=<filename> path at which to create a symbolic link to the
bigwig and info files; this can use {bigwig}
for the bigwig filename
--bigwigposition=<interval> intitial UCSC browser interval for bigwig track
--initialize=<text> (cumulative) shell command to add to job beginning
"shebang:bash" is mapped "#!/usr/bin/env bash"
other commands are copied "as is"
values read from control file:
discordant_mates_dense.minLength.{run}
discordant_mates_dense.density
discordant_mates_dense.samplingStep"""
if (s == None): exit (message)
else: exit ("%s%s" % (s,message))
def main():
global basePath,runName
global debug
bashShebang = "#!/usr/bin/env bash"
# parse args
runName = None
controlFilename = None
basePath = None
inputFilename = None
chromsFilename = None
trackName = None
scratchFilename = None
tempFilename = None
gzipOutput = False
bigWigFilename = None
bigWigChromsFilename = None
bigWigUrl = None
bigWigLink = None
bigWigPosition = None
bashInitializers = ["set -eu"]
debug = []
for arg in argv[1:]:
if ("=" in arg):
argVal = arg.split("=",1)[1].strip()
if (arg.startswith("--control=")):
controlFilename = argVal
elif (arg.startswith("--base=")) or (arg.startswith("--basepath=")) or (arg.startswith("--path=")):
basePath = argVal
elif (arg.startswith("--input=")):
inputFilename = argVal
elif (arg.startswith("--chromosomes=")) or (arg.startswith("--chroms=")):
chromsFilename = argVal
elif (arg.startswith("--track=")):
trackName = argVal
elif (arg.startswith("--tempinput=")):
tempInputFilename = argVal
elif (arg.startswith("--temp=")):
tempFilename = argVal
elif (arg == "--gzip"):
gzipOutput = True
elif (arg == "--bigwig"):
bigWigFilename = "{track}.bw"
elif (arg.startswith("--bigwig=")):
bigWigFilename = argVal
elif (arg.startswith("--bigwigchromosomes=")) or (arg.startswith("--bigwigchroms=")):
bigWigChromsFilename = argVal
elif (arg.startswith("--bigwigurl=")) or (arg.startswith("--url=")):
bigWigUrl = argVal
elif (arg.startswith("--bigwiglink=")) or (arg.startswith("--link=")):
bigWigLink = argVal
elif (arg.startswith("--bigwigposition=")) or (arg.startswith("--bigwigpos=")):
bigWigPosition = argVal
elif (arg.startswith("--initialize=")) or (arg.startswith("--init=")):
if (argVal == "shebang:bash"):
argVal = bashShebang
if (argVal == "set -eu"):
bashInitializers = [x for x in bashInitializers if (x != "set -eu")]
bashInitializers += [argVal]
elif (arg == "--debug"):
debug += ["debug"]
elif (arg.startswith("--debug=")):
debug += argVal.split(",")
elif (arg.startswith("--")):
usage("unrecognized option: %s" % arg)
elif (runName == None):
fields = arg.split(":",2)
if (len(fields) != 3):
fields = arg.split("_")
if (len(fields) < 3) or (fields[-1] not in ["PE","MP"]):
usage("\"%s\" is not a valid run descriptor" % arg)
runName = "_".join(fields)
else:
usage("unrecognized option: %s" % arg)
if (runName == None):
usage("you have to give me a run descriptor")
if (controlFilename == None):
usage("you have to give me a control filename")
if (inputFilename == None):
usage("you have to give me an input track filename")
if (chromsFilename == None):
chromsFilename = "{base}/data/hg19.chrom_lengths"
if (trackName == None):
trackName = "{base}/tracks/{run}.discordant_mates.dense"
if (scratchFilename == None):
scratchFilename = trackName + ".scratch"
if (tempFilename == None) and (bigWigFilename != None) and (gzipOutput):
tempFilename = trackName + ".temp"
if (bigWigFilename != None):
if (bigWigChromsFilename == None):
bigWigChromsFilename = "{base}/temp/ucsc.hg19.chrom_lengths"
if (bigWigUrl == None):
usage("you have to give me a url for the bigwig file")
trackId = "%s.discordant_mates.dense" % runName
##########
# perform filename substitution
##########
if (basePath == None): basePath = "."
elif (basePath.endswith("/")): basePath = basePath[:-1]
controlFilename = do_filename_substitutition(controlFilename)
chromsFilename = do_filename_substitutition(chromsFilename)
# input track name
inputFilename = do_filename_substitutition(inputFilename)
gzipInput = False
if (inputFilename.endswith(".gz")): gzipInput = True
elif (inputFilename.endswith(".gzip")): gzipInput = True
elif (inputFilename.endswith(".bedgraph")): pass
elif (not inputFilename.endswith(".dat")): inputFilename += ".dat"
# track name
trackName = do_filename_substitutition(trackName)
trackFilename = trackName
if (gzipOutput):
if (not trackFilename.endswith(".gz")): trackFilename += ".gz"
else:
if (not trackFilename.endswith(".dat")): trackFilename += ".dat"
if (scratchFilename != None):
scratchFilename = do_filename_substitutition(scratchFilename)
if (tempFilename != None):
tempFilename = do_filename_substitutition(tempFilename)
# big wig name
if (bigWigFilename != None):
bigWigFilename = do_filename_substitutition(bigWigFilename)
if ("{track}" in bigWigFilename):
trackTemp = trackName
if (trackTemp.endswith(".gz")): trackTemp = trackTemp[:-3]
elif (trackTemp.endswith(".dat")): trackTemp = trackTemp[:-4]
bigWigFilename = bigWigFilename.replace("{track}",trackTemp)
if (bigWigFilename.endswith(".bw")): infoFilename = bigWigFilename[:-3] + ".info"
else: infoFilename = bigWigFilename + ".info"
if (bigWigChromsFilename != None):
bigWigChromsFilename = do_filename_substitutition(bigWigChromsFilename)
if (bigWigUrl != None):
bigWigTemp = bigWigFilename
slashIx = bigWigTemp.rfind("/")
if (slashIx >= 0): bigWigTemp = bigWigTemp[slashIx+1:]
bigWigUrl = bigWigUrl.replace("{bigwig}",bigWigTemp)
if (bigWigLink != None):
bigWigSave = bigWigLink
bigWigTemp = bigWigFilename
slashIx = bigWigTemp.rfind("/")
if (slashIx >= 0): bigWigTemp = bigWigTemp[slashIx+1:]
bigWigLink = bigWigLink.replace("{bigwig}",bigWigTemp)
infoTemp = infoFilename
slashIx = infoTemp.rfind("/")
if (slashIx >= 0): infoTemp = infoTemp[slashIx+1:]
infoLink = bigWigSave.replace("{bigwig}",infoTemp)
##########
# get values from the control file
##########
minLength = None
densityThreshold = None
samplingStep = None
f = file(controlFilename,"rt")
lineNumber = 0
for line in f:
lineNumber += 1
line = line.strip()
if (line == ""): continue
if (line.startswith("#")): continue
fields = line.split()
assert (len(fields) >= 3), \
"not enough fields at control file line %d (%d, expected at least 3)" \
% (lineNumber,len(fields))
assert (fields[1] == "="), \
"can't understand control file line %d:\n%s" \
% (lineNumber,line)
(name,_,val) = fields[:3]
if (name == "discordant_mates_dense.minLength." + runName): minLength = int(val)
if (name == "discordant_mates_dense.density"): densityThreshold = val
if (name == "discordant_mates_dense.samplingStep"): samplingStep = int(val)
f.close()
if (minLength == None): assert(False), "control file lacks minLength"
if (samplingStep == None): assert(False), "control file lacks samplingStep"
if (densityThreshold == None): assert(False), "control file lacks density"
if ("." in densityThreshold):
while (densityThreshold.endswith("0")):
densityThreshold = densityThreshold[:-1]
if (densityThreshold.endswith(".")):
densityThreshold = densityThreshold[:-1]
##########
# create the job's shell script
##########
# write bash intitializers
if (bashInitializers != None):
for (ix,bashInitializer) in enumerate(bashInitializers):
if (bashInitializer != bashShebang): continue
print bashInitializer
bashInitializers[ix] = None
for (ix,bashInitializer) in enumerate(bashInitializers):
if (bashInitializer != "set -eu"): continue
print bashInitializer
bashInitializers[ix] = None
for bashInitializer in bashInitializers:
if (bashInitializer == None): continue
print do_filename_substitutition(bashInitializer)
print
print "today=`today {mmm}/{d}/{yyyy}`"
# write commands describing the files the script will create
if (scratchFilename != None):
print "echo \"will use %s as a scratch file\"" % scratchFilename
if (tempFilename != None):
print "echo \"will write temporary files to %s\"" % tempFilename
print "echo \"will write track file to %s\"" % trackFilename
if (bigWigFilename != None):
print "echo \"will write bigwig file to %s\"" % bigWigFilename
# write command(s) to create track file
print
print "echo \"=== creating track %s ===\"" % trackId
if (gzipInput):
commands = []
command = ["time gzip -dc %s" % inputFilename]
commands += [command]
command = ["> %s" % scratchFilename]
commands += [command]
print
print commands_to_pipeline(commands)
trackSourceFilename = scratchFilename
else:
trackSourceFilename = inputFilename
commands = []
command = ["time genodsp"]
command += ["--chromosomes=%s" % chromsFilename]
command += ["--show:uncovered"]
command += ["= input %s" % trackSourceFilename]
command += ["= binarize"]
command += ["= slidingsum W=%d D=W" % minLength]
command += ["= percentile %s W=%d --min=1/inf --quiet" \
% (densityThreshold,samplingStep)]
if (gzipInput): command += ["= input %s --destroy" % trackSourceFilename]
else: command += ["= input %s" % trackSourceFilename]
command += ["= binarize"]
command += ["= clump --average=percentile%s L=%d" \
% (densityThreshold,minLength)]
commands += [command]
if (gzipOutput):
if (tempFilename != None):
command = ["tee %s" % tempFilename]
commands += [command]
command = ["gzip"]
commands += [command]
command = ["> %s" % trackFilename]
commands += [command]
print
print commands_to_pipeline(commands)
# write command(s) to convert track file to bigwig
if (bigWigFilename != None):
print
print "echo \"=== converting track %s to bigwig ===\"" % trackId
if (tempFilename != None): trackInput = tempFilename
else: trackInput = trackFilename
commands = []
command = ["time bedGraphToBigWig"]
command += [trackInput]
command += [bigWigChromsFilename]
command += [bigWigFilename]
commands += [command]
print
print commands_to_pipeline(commands)
if (tempFilename != None):
commands = []
command = ["rm %s" % tempFilename]
commands += [command]
print
print commands_to_pipeline(commands)
commands = []
command = ["make_bigwig_info"]
command += ["--url=%s" % bigWigUrl]
command += ["--name=\"%s discordant mates dense\"" % runName]
command += ["--desc=\"%s dense intervals in discordant mates mapped occupancy (${today})\"" \
% runName]
command += ["--autoscale=\"on\""]
command += ["--alwayszero=\"on\""]
command += ["--maxheight=\"10:10:10\""]
command += ["--color=250,30,100"]
if (bigWigPosition != None): command += ["--pos=\"%s\"" % bigWigPosition]
command += ["> %s" % infoFilename]
commands += [command]
print
print commands_to_pipeline(commands)
if (bigWigLink != None):
print
print "rm -f %s" % infoLink
print "ln -s %s %s" % (infoFilename,infoLink)
print "rm -f %s" % bigWigLink
print "ln -s %s %s" % (bigWigFilename,bigWigLink)
infoUrl = bigWigUrl
slashIx = infoUrl.rfind("/")
if (slashIx >= 0): infoUrl = infoUrl[:slashIx]
infoTemp = infoFilename
slashIx = infoTemp.rfind("/")
if (slashIx >= 0): infoTemp = infoTemp[slashIx+1:]
infoUrl = infoUrl + "/" + infoTemp
print >>stderr, infoUrl
print
print "echo \"track URL is %s\"" % (infoUrl)
def commands_to_pipeline(commands):
pipeline = []
for (cmdNum,cmd) in enumerate(commands):
if (cmdNum == 0): prefix = ""
else: prefix = " | "
if (cmd[0].startswith(">")):
assert (cmdNum != 0)
assert (len(cmd) == 1)
prefix = " "
pipeline += [prefix + cmd[0]]
for line in cmd[1:]:
pipeline += [" " + line]
return " \\\n".join(pipeline)
def do_filename_substitutition(s):
if ("{base}" in s):
assert (basePath != None)
s = s.replace("{base}",basePath)
if ("{run}" in s):
assert (runName != None)
s = s.replace("{run}",runName)
return s
if __name__ == "__main__": main()