-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprep_splice_site_metaplots.py
521 lines (359 loc) · 9.26 KB
/
prep_splice_site_metaplots.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
import sys
import argparse
import uuid
import subprocess
def shell_cmd(
cmd,
calling_name):
'''
Wrapper for subprocess.check_output().
Attempts calling shell command. Prints
error if any errors are raised and exits
program if so.
Parameters
----------
cmd : str
String containing full shell command
calling_name :
Name of function (or other environment)
from which shell_cmd was called -
useful for debugging.
'''
try:
subprocess.check_output(
cmd,
shell = True,
stderr = subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print e.output
sys.exit("Subprocess command failed in " +
calling_name + ". Exiting . . . ")
def call_bedtools_sort(
input_file,
outdir,
outfile = None,
bedtools_exec = "bedtools",
remove_original = True):
'''
Sort input bed file or bg file
using bedtools sort with the following
command:
$ bedtools sort -i input_file > outdir/outfile_name
Parameters
----------
input_file : str
Full path to input file
outdir : str
Full path to output directory
outfile : str
If set, provides full path for sorted file.
If not set, function will insert ".sorted"
prior to the input_file suffix.
bedtools_exec : str
Path to bedtools binary. Default
"bedtools" assumes bedtools in user's
PATH.
remove_original : bool
Whether to remove the original (unsorted file).
Often there is no reason to keep it, and as such
default is True.
'''
if not outfile:
input_file_split = input_file.split(".")
input_file_prefix = ".".join(input_file_split[0:-1])
suffix = input_file_split[-1]
outfile = (input_file_prefix +
".sorted." +
suffix)
bedtools_sort_command = " ".join([
bedtools_exec,
"sort",
"-i",
input_file,
">",
outfile])
shell_cmd(bedtools_sort_command,
"call_bedtools_sort - sort")
if remove_original:
rm_command = " ".join([
"rm",
input_file])
shell_cmd(rm_command,
"call_bedtools_sort - rm")
return outfile
def expand_bedgraph(
path_to_bg,
outfile = None,
remove_original = True):
'''
Expands a bedgraph file such that intervals with
length > 1 become multiple intervals with
length = 1. Writes the expanded file to disk.
This expansion facilitates using single-base resolution.
Parameters
----------
path_to_bg : str
Path to bedgraph file
outfile :
Path to output file. If unset the original file
path with "expanded" inserted prior to the suffix is
used.
remove_original : bool
Whether to remove the original (unsorted file).
Often there is no reason to keep it, and as such
default is True.
'''
if not outfile:
input_file_split = path_to_bg.split(".")
input_file_prefix = ".".join(input_file_split[0:-1])
suffix = input_file_split[-1]
outfile = (input_file_prefix +
".expanded." +
suffix)
expanded_file = open(outfile, 'w')
with open(path_to_bg, 'r') as file:
for line in file:
entry = line.strip().split()
start = int(entry[1])
end = int(entry[2])
gap = end - start
if gap == 1:
expanded_file.write(line)
else:
for i in range(start, end):
expanded_file.write(
"\t".join([entry[0],
str(i),
str(i+1)] + entry[3:]) + "\n")
if remove_original:
rm_command = " ".join([
"rm",
path_to_bg])
shell_cmd(rm_command,
"expand_bedgraph - rm")
expanded_file.close()
return outfile
def call_bedtools_intersect(
path_to_region_bed,
path_to_bg,
outdir,
filename_prefix = "",
bedtools_exec = "bedtools"):
'''
Calls bedtools intersect using subprocess
module with the following effective command:
$ bedtools intersect -a path_to_region_bed \
-b path_to_bg \
-wa \
-wb \
-sorted \
> \
output_file
The idea is to retrieve bedgraph information
(e.g. phastCons scores, mapability, etc) for
features of interest (e.g. regions surrounding
splice sites). Note that input bed AND begraph
(bg) files MUST be sorted - it is easiest to sort
them using sortBed/bedtools sort. Failure to do
so will lead to unexpected behavior.
Parameters
----------
path_to_region_bed : str
Path to region bed file
path_to_bg : str
Path to bedgraph file
outdir : str
Path to output directory
filename_prefix : str
Any prefix to be added to filename - e.g.
"human" for human output if process will
be repeated for multiple species
bedtools_exec : str
Path to bedtools binary. Default
"bedtools" assumes bedtools in user's
PATH.
'''
if filename_prefix != "":
filename_prefix = "_" + filename_prefix
initial_intersect_outpath = (outdir +
"/" +
str(uuid.uuid4()) +
filename_prefix +
"_bg_overlap.bedlike")
bedtools_intersect_command = " ".join([
bedtools_exec,
"intersect",
"-a",
path_to_region_bed,
"-b",
path_to_bg,
"-wa",
"-wb",
"-sorted",
">",
initial_intersect_outpath])
shell_cmd(bedtools_intersect_command,
"call_bedtools_intersect")
return initial_intersect_outpath
def process_overlap(
initial_intersect_outpath,
outdir,
name_field_header,
region_length,
filename_prefix = "",
remove_original = True):
'''
Writes simplified output file based on
bedtools intersect.
Assumes adjacent regions appear sequentially
Parameters
----------
initial_intersect_outpath : str
Path to bedtools intersect output file
outfile_path : str
Path to output file
name_field_header : list
list if strings constituting the header
for information contained in field 3 of
the bedtools intersect output
prefix : str
Prefix to add to output filename
region_length : int
Size of the regions
remove_original : bool
Whether to remove the original (unsorted file).
Often there is no reason to keep it, and as such
default is True.
'''
if filename_prefix != "":
filename_prefix = filename_prefix + "_"
outfile_path = outdir + "/" + filename_prefix + "metaregion_scores.tsv"
output_file = open(outfile_path, 'w')
header = "\t".join([
"shared_coordinate",
"score"
] + name_field_header)
output_file.write(header + "\n")
with open(initial_intersect_outpath, 'r') as file:
full_identifier = ""
for line in file:
entry = line.split()
strand = entry[5]
score = entry[9]
identifier_split = entry[3].split(";")
host_feature_id = identifier_split[-1]
if entry[3] != full_identifier:
full_identifier = entry[3]
if strand == "+":
counter = 0
elif strand == "-":
counter = region_length - 1
output_entry = "\t".join([
str(counter),
score] +
identifier_split)
output_file.write(output_entry + "\n")
if strand == "+":
counter += 1
elif strand == "-":
counter -= 1
if remove_original:
rm_command = " ".join([
"rm",
initial_intersect_outpath])
shell_cmd(rm_command,
"process_overlap - rm")
output_file.close()
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--region_bed",
type = str,
help = "Path to region bed file",
required = True)
parser.add_argument(
"--bedgraph",
type = str,
help = "Path to begGraph file",
required = True)
parser.add_argument(
"--outdir",
type = str,
help = "Path to output directory",
required = True)
parser.add_argument(
"--name_fields",
type = str,
help = "Comma separated list of names of name field elements",
required = True)
parser.add_argument(
"--region_length",
type = int,
help = "Number of bases in each region",
required = True)
parser.add_argument(
"--sort_bed",
action = "store_true",
help = "Sort bed file if not sorted")
parser.add_argument(
"--sort_bedgraph",
action = "store_true",
help = ("Sort bedgraph file if not sorted. " +
"If used, this will likely take a while. " +
"Set if not already sorted"))
parser.add_argument(
"--expand_bedgraph",
action = "store_true",
help = ("Expand bedgraph to 1-line per base - " +
" set this if not already expanded"))
parser.add_argument(
"--bedtools_exec",
type = str,
help = "Path to bedtools binary. default = 'bedtools'",
default = "bedtools")
parser.add_argument(
"--prefix",
type = str,
help = "Prefix for output_files, default = ''",
default = "")
args = parser.parse_args()
if args.sort_bed:
region_bed = call_bedtools_sort(
args.region_bed,
args.outdir,
outfile = None,
bedtools_exec = args.bedtools_exec,
remove_original = True)
else:
region_bed = args.region_bed
if args.sort_bedgraph:
bedgraph = call_bedtools_sort(
args.bedgraph,
args.outdir,
outfile = None,
bedtools_exec = args.bedtools_exec,
remove_original = True)
else:
bedgraph = args.bedgraph
if args.expand_bedgraph:
expanded_bedgraph = expand_bedgraph(
bedgraph,
outfile = None,
remove_original = True)
else:
expanded_bedgraph = bedgraph
initial_intersect_outpath = call_bedtools_intersect(
region_bed,
expanded_bedgraph,
args.outdir,
filename_prefix = args.prefix,
bedtools_exec = args.bedtools_exec)
process_overlap(
initial_intersect_outpath,
args.outdir,
args.name_fields.split(","),
args.region_length,
filename_prefix = args.prefix,
remove_original = True)
if __name__ == "__main__":
main()