-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_script_depth.py
executable file
·379 lines (308 loc) · 11.9 KB
/
create_script_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
#!/usr/bin/env python
"""
Create a cluster job file to create a depth-of-coverage track.
"""
from sys import argv,stdin,stdout,stderr,exit
def usage(s=None):
message = """
usage: create_script_depth [options] > 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
--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}.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}.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"""
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
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("--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 (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}.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.depth" % runName
##########
# perform filename substitution
##########
if (basePath == None): basePath = "."
elif (basePath.endswith("/")): basePath = basePath[:-1]
bamFilename = do_filename_substitutition(bamFilename)
chromsFilename = do_filename_substitutition(chromsFilename)
# 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 (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)
##########
# create the job's shell script
##########
if (sequencingType == "PE"): pOrient = "H2H"
elif (sequencingType == "MP"): pOrient = "T2T"
# 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):
print "echo \"will write temporary file 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
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 += ["--requiremates"]
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 (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)
description = "mapped coverage depth"
if (dateInTrackname): description += " (${today})"
commands = []
command = ["make_bigwig_info"]
command += ["--url=%s" % bigWigUrl]
command += ["--name=\"%s depth\"" % runName]
command += ["--desc=\"%s %s\"" % (runName,description)]
command += ["--autoscale=\"on\""]
command += ["--alwayszero=\"on\""]
command += ["--maxheight=\"128:30:20\""]
command += ["--color=0,0,0"]
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()