-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_script_insert_depth.py
executable file
·491 lines (404 loc) · 16.7 KB
/
create_script_insert_depth.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!/usr/bin/env python
"""
Create a cluster job file to create several insert depth-of-coverage tracks.
"""
from sys import argv,stdin,stdout,stderr,exit
def usage(s=None):
message = """
usage: create_script_insert_depth [options] > insert_depth.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
--class[es]=<kinds> which class(es) of inserts to handle; <kinds> is
a comma-separated list
(default is short,normal,long)
--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
--bam=<filename> (required) bam file to process
--namesorted the bam file has been sorted by read names
--chromosomes=<filename> read chromosome names and lengths from a file
(default is {base}/data/hg19.chrom_lengths)
--track=<filename> (required) track file to create
(default is {base}/tracks/{run}.{kind}_inserts.depth)
--temp=<filename> temporary file to hold track file, if needed; only
needed if --gzip and --bigwig are both used
(default is {base}/tracks/{run}.{kind}_inserts.depth.temp)
--gzip compress track file
--undated don't include today's date in the track name
--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"
--head=<number> limit the number of bam records
values read from control file:
avgInsertLen.{run}
minInsertLen.{run} (optional)
maxInsertLen.{run} (optional)
stdevInsert.{run}"""
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
sequencingType = None
classesOfInterest = ["short","normal","long"]
controlFilename = None
basePath = None
bamFilename = None
isNameSorted = False
chromsFilename = None
trackName = None
tempFilename = None
gzipOutput = False
dateInTrackname = True
bigWigFilename = None
bigWigChromsFilename = None
bigWigUrl = None
bigWigLink = None
bigWigPosition = None
bashInitializers = ["set -eu"]
headLimit = None
debug = []
for arg in argv[1:]:
if ("=" in arg):
argVal = arg.split("=",1)[1].strip()
if (arg.startswith("--class=")) or (arg.startswith("--classes=")):
classesOfInterest = argVal.split(",")
for kind in classesOfInterest:
if (kind not in ["short","normal","long"]):
usage ("unknown class \"%s\" in \"%s\"" % (kind,arg))
elif (arg.startswith("--control=")):
controlFilename = argVal
elif (arg.startswith("--base=")) or (arg.startswith("--basepath=")) or (arg.startswith("--path=")):
basePath = argVal
elif (arg.startswith("--bam=")):
bamFilename = argVal
elif (arg == "--namesorted"):
isNameSorted = True
elif (arg.startswith("--chromosomes=")) or (arg.startswith("--chroms=")):
chromsFilename = argVal
elif (arg.startswith("--track=")):
trackName = argVal
elif (arg.startswith("--temp=")):
tempFilename = argVal
elif (arg == "--gzip"):
gzipOutput = True
elif (arg == "--undated"):
dateInTrackname = False
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.startswith("--head=")):
headLimit = 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)
sequencingType = fields[-1]
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 (bamFilename == None):
usage("you have to give me a bam filename")
if (chromsFilename == None):
chromsFilename = "{base}/data/hg19.chrom_lengths"
if (trackName == None):
trackName = "{base}/tracks/{run}.{kind}_inserts.depth"
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.{kind}_inserts.depth" % runName
##########
# perform filename substitution
##########
if (basePath == None): basePath = "."
elif (basePath.endswith("/")): basePath = basePath[:-1]
controlFilename = do_filename_substitutition(controlFilename)
bamFilename = do_filename_substitutition(bamFilename)
chromsFilename = do_filename_substitutition(chromsFilename)
# track name
trackName = do_filename_substitutition(trackName)
assert ("{kind}" in trackName)
trackFilename = trackName
if (gzipOutput):
if (not trackFilename.endswith(".gz")): trackFilename += ".gz"
else:
if (not trackFilename.endswith(".dat")): trackFilename += ".dat"
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)
assert ("{kind}" in bigWigUrl)
if (bigWigLink != None):
bigWigSave = bigWigLink
bigWigTemp = bigWigFilename
slashIx = bigWigTemp.rfind("/")
if (slashIx >= 0): bigWigTemp = bigWigTemp[slashIx+1:]
bigWigLink = bigWigLink.replace("{bigwig}",bigWigTemp)
assert ("{kind}" in bigWigLink)
infoTemp = infoFilename
slashIx = infoTemp.rfind("/")
if (slashIx >= 0): infoTemp = infoTemp[slashIx+1:]
infoLink = bigWigSave.replace("{bigwig}",infoTemp)
assert ("{kind}" in infoLink)
##########
# get values from the control file
##########
avgInsertLen = None
stdevInsert = None
minInsertLen = None
maxInsertLen = 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 == "avgInsertLen." + runName): avgInsertLen = int(val)
if (name == "avgInsertLen.*") and (avgInsertLen == None): avgInsertLen = int(val)
if (name == "stdevInsert." + runName): stdevInsert = int(val)
if (name == "stdevInsert.*") and (stdevInsert == None): stdevInsert = int(val)
if (name == "minInsertLen." + runName): minInsertLen = int(val)
if (name == "minInsertLen.*") and (minInsertLen == None): minInsertLen = int(val)
if (name == "maxInsertLen." + runName): maxInsertLen = int(val)
if (name == "maxInsertLen.*") and (maxInsertLen == None): maxInsertLen = int(val)
f.close()
if (avgInsertLen == None): assert(False), "control file lacks avgInsertLen"
if (stdevInsert == None): assert(False), "control file lacks stdevInsert"
#if minInsertLen == None, not a problem, min is optional
#if maxInsertLen == None, not a problem, max is optional
##########
# create the job's shell script
##########
if (sequencingType == "PE"): pOrient = "H2H"
elif (sequencingType == "MP"): pOrient = "T2T"
shortInsertLen = avgInsertLen - 2*stdevInsert
longInsertLen = avgInsertLen + 2*stdevInsert
insertClasses = []
if ("short" in classesOfInterest):
insertClasses += [("short" ,minInsertLen, shortInsertLen)]
if ("long" in classesOfInterest):
insertClasses += [("long" ,longInsertLen ,maxInsertLen )]
if ("normal" in classesOfInterest):
insertClasses += [("normal",shortInsertLen,longInsertLen )]
# 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
if (dateInTrackname):
print "today=`today {mmm}/{d}/{yyyy}`"
# write commands describing the files the script will create
if (tempFilename != None):
if ("{kind}" not in tempFilename):
print "echo \"will write temporary file to %s\"" % tempFilename
else:
for (insertClass,_,_) in insertClasses:
print "echo \"will write temporary file to %s\"" \
% tempFilename.replace("{kind}",insertClass)
for (insertClass,_,_) in insertClasses:
trackClassFilename = trackFilename.replace("{kind}",insertClass)
print "echo \"will write track file to %s\"" % trackClassFilename
if (bigWigFilename != None):
bigWigClassFilename = bigWigFilename.replace("{kind}",insertClass)
print "echo \"will write bigwig file to %s\"" % bigWigClassFilename
# loop over insert classes
for (insertClass,shortLength,longLength) in insertClasses:
trackClassId = trackId.replace("{kind}",insertClass)
trackClassFilename = trackFilename.replace("{kind}",insertClass)
if (tempFilename != None): tempClassFilename = tempFilename.replace("{kind}",insertClass)
else: tempClassFilename = None
if (shortLength != None): classRange = "%d.." % shortLength
else: classRange = ".."
if (longLength != None): classRange += "%d" % longLength
if (longLength == None): classRangeText = ">%d" % shortLength
elif (shortLength == None): classRangeText = "<%d" % longLength
else: classRangeText = "%d..%d" % (shortLength,longLength)
# write command(s) to create track files
print
print "echo \"=== creating track %s (range %s) ===\"" % (trackClassId,classRange)
commands = []
command = ["time samtools view %s" % bamFilename]
commands += [command]
command = ["filtered_sam_to_intervals"]
if (isNameSorted): command += ["--namesorted"]
if (headLimit != None): command += ["--head=%s" % headLimit]
command += ["--mergemates=%s" % classRange]
command += ["--prohibit:\"(CIGAR == *)\""]
command += ["--require:\" (RNEXT == =)\""]
command += ["--require:\" (PORIENT==%s)\"" % pOrient]
command += ["--nonames"]
command += ["--progress=2M"]
commands += [command]
command = ["genodsp"]
command += ["--chromosomes=%s" % chromsFilename]
command += ["--novalue --show:uncovered"]
commands += [command]
if (gzipOutput):
if (tempClassFilename != None):
command = ["tee %s" % tempClassFilename]
commands += [command]
command = ["gzip"]
commands += [command]
command = ["> %s" % trackClassFilename]
commands += [command]
print
print commands_to_pipeline(commands)
# write command(s) to convert track file to bigwig
if (bigWigFilename != None):
bigWigClassFilename = bigWigFilename.replace("{kind}",insertClass)
bigWigClassUrl = bigWigUrl.replace("{kind}",insertClass)
if (bigWigLink != None):
bigWigClassLink = bigWigLink.replace("{kind}",insertClass)
infoClassLink = infoLink.replace("{kind}",insertClass)
infoClassFilename = infoFilename.replace("{kind}",insertClass)
print
print "echo \"=== converting track %s to bigwig ===\"" % trackClassId
if (tempClassFilename != None): trackInput = tempClassFilename
else: trackInput = trackClassFilename
commands = []
command = ["time bedGraphToBigWig"]
command += [trackInput]
command += [bigWigChromsFilename]
command += [bigWigClassFilename]
commands += [command]
print
print commands_to_pipeline(commands)
if (tempClassFilename != None):
commands = []
command = ["rm %s" % tempClassFilename]
commands += [command]
print
print commands_to_pipeline(commands)
description = "%s inserts mapped coverage depth (%s bp)"\
% (insertClass,classRangeText)
if (dateInTrackname): description += " (${today})"
commands = []
command = ["make_bigwig_info"]
command += ["--url=%s" % bigWigClassUrl]
command += ["--name=\"%s %s inserts depth\"" % (runName,insertClass)]
command += ["--desc=\"%s %s\"" % (runName,description)]
command += ["--autoscale=\"on\""]
command += ["--alwayszero=\"on\""]
command += ["--maxheight=\"128:30:20\""]
command += ["--color=60,140,170"]
if (bigWigPosition != None): command += ["--pos=\"%s\"" % bigWigPosition]
command += ["> %s" % infoClassFilename]
commands += [command]
print
print commands_to_pipeline(commands)
if (bigWigLink != None):
print
print "rm -f %s" % infoClassLink
print "ln -s %s %s" % (infoClassFilename,infoClassLink)
print "rm -f %s" % bigWigClassLink
print "ln -s %s %s" % (bigWigClassFilename,bigWigClassLink)
infoUrl = bigWigClassUrl
slashIx = infoUrl.rfind("/")
if (slashIx >= 0): infoUrl = infoUrl[:slashIx]
infoTemp = infoClassFilename
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()