-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathScanExitron.py
639 lines (572 loc) · 18.9 KB
/
ScanExitron.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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ===============================================================================
__version__ = "v1.3.1beta"
import sys
import os
import argparse
from pyfaidx import Fasta
import numpy as np
import subprocess
import shutil
import secrets
from collections import OrderedDict
from io import BytesIO
import configparser
from tempfile import TemporaryDirectory
def remove(infile):
if os.path.isfile(infile):
os.remove(infile)
def status_message(msg):
print(msg)
sys.stdout.flush()
def run_cmd(cmd, msg=None):
status_message(cmd)
if "," in msg:
begin, finish = msg.split(",")
status_message(begin)
else:
finish = msg
try:
result = subprocess.check_output(
cmd, shell=True, stderr=subprocess.STDOUT, stdin=subprocess.PIPE
)
except subprocess.CalledProcessError as err:
error_msg = "Error happend!: {}\n{}".format(err, err.output)
else:
error_msg = ""
if not error_msg:
status_message(finish)
return True, result
else:
status_message(error_msg)
return False, None
def config_getter(config_file="config.ini"):
this_dir = os.path.dirname(os.path.realpath(__file__))
config_default = os.path.join(this_dir, config_file)
config = configparser.ConfigParser(os.environ)
config.read(config_default)
hg38_ref = config.get("fasta", "hg38")
hg19_ref = config.get("fasta", "hg19")
hg38_anno = config.get("annotation", "hg38")
hg19_anno = config.get("annotation", "hg19")
hg38_cds = config.get("cds", "hg38")
hg19_cds = config.get("cds", "hg19")
return {
"hg38_ref": hg38_ref,
"hg19_ref": hg19_ref,
"hg38_anno": hg38_anno,
"hg19_anno": hg19_anno,
"hg38_cds": hg38_cds,
"hg19_cds": hg19_cds,
}
chrms = {
"chr1",
"chr2",
"chr3",
"chr4",
"chr5",
"chr6",
"chr7",
"chr8",
"chr9",
"chr10",
"chr11",
"chr12",
"chr13",
"chr14",
"chr15",
"chr16",
"chr17",
"chr18",
"chr19",
"chr20",
"chr21",
"chr22",
"chrX",
"chrY",
"chrM",
}
non_mito_chrms = {
"chr1",
"chr2",
"chr3",
"chr4",
"chr5",
"chr6",
"chr7",
"chr8",
"chr9",
"chr10",
"chr11",
"chr12",
"chr13",
"chr14",
"chr15",
"chr16",
"chr17",
"chr18",
"chr19",
"chr20",
"chr21",
"chr22",
"chrX",
"chrY",
}
chrms_dict = {
"1": "chr1",
"2": "chr2",
"3": "chr3",
"4": "chr4",
"5": "chr5",
"6": "chr6",
"7": "chr7",
"8": "chr8",
"9": "chr9",
"10": "chr10",
"11": "chr11",
"12": "chr12",
"13": "chr13",
"14": "chr14",
"15": "chr15",
"16": "chr16",
"17": "chr17",
"18": "chr18",
"19": "chr19",
"20": "chr20",
"21": "chr21",
"22": "chr22",
"X": "chrX",
"Y": "chrY",
"MT": "chrM",
}
reverse_chrms_dict = dict((chrms_dict[i], i) for i in chrms_dict)
def BED_handler(inbed, tmp_dir):
"""keep only canonical chromosomes and convert b37/b38 to hg19/38"""
rnd_id = secrets.token_hex(16)
tmp_file = f"{tmp_dir}/tmp.{rnd_id}.txt"
tmp = open(tmp_file, "w")
with open(inbed, "r") as f:
for line in f:
l = line.rstrip("\n").split("\t")
if l[0] in chrms:
tmp.write("\t".join(l) + "\n")
elif l[0] in chrms_dict:
l[0] = chrms_dict[l[0]]
tmp.write("\t".join(l) + "\n")
tmp.close()
shutil.move(tmp_file, inbed)
return os.path.abspath(inbed)
def junction_caller(
bam_file, ref="hg38", strand=1, out_name=None, config=None, tmp_dir=None
):
"""
Call junctions using regtools
output: out_name.janno
"""
if not config:
sys.stderr.write("No config file was found!\n")
sys.exit(1)
if ref == "hg19":
fasta = config["hg19_ref"]
gtf = config["hg19_anno"]
elif ref == "hg38":
fasta = config["hg38_ref"]
gtf = config["hg38_anno"]
prefix = os.path.splitext(os.path.basename(bam_file))[0]
if not out_name:
out_name = prefix
if os.path.exists(f"{out_name}.janno.done"):
status_message(f"{out_name}.janno found, skip junction identification.\n")
return "{}.janno".format(out_name)
cmd = f"regtools junctions extract -s {strand} -i 5 -I 10000000 {bam_file} -o {tmp_dir}/{prefix}.bed"
bed_flag, _ = run_cmd(cmd, "Calling junctions start,Calling junctions finished!")
if bed_flag:
bed = BED_handler(f"{tmp_dir}/{prefix}.bed", tmp_dir)
cmd = f"regtools junctions annotate {bed} {fasta} {gtf} -o {out_name}.janno"
janno_flag, _ = run_cmd(cmd, f"{out_name}.janno generated!")
if janno_flag:
status_message("{}.janno generated!".format(out_name))
os.remove("{}".format(bed))
done_file(f"{out_name}.janno")
return "{}.janno".format(out_name)
return False
def junction_overlap_CDS_to_position_BED(
janno, ao_cutoff=3, ref="hg38", tmp_dir=None, config=None
):
"""
intersect junctions with annotated CDS to search exitrons
"""
if not config:
sys.stderr.write("No config file was found!\n")
sys.exit(1)
if ref == "hg19":
cds = config["hg19_cds"]
elif ref == "hg38":
cds = config["hg38_cds"]
genome_seq = seq_dict(ref=ref, config=config)
print("Reading {}".format(janno))
# write all the novel junctions with canonical splicing sites to file (junction.bed)
rnd_id = secrets.token_hex(16)
junction_bed = f"{tmp_dir}/{rnd_id}.junction.bed"
total_junctions = 0
with open(janno, "r") as f, open(junction_bed, "w") as out:
f.readline()
for line in f:
l = line.rstrip().split("\t")
total_junctions += int(l[4])
chrm = l[0]
start = int(l[1])
end = int(l[2])
stats = l[10]
strand = l[5]
if stats == "N" and strand != "?":
if strand == "+":
left_site = genome_seq[chrm][start : start + 2].seq
right_site = genome_seq[chrm][end - 3 : end - 1].seq
elif strand == "-":
left_site = genome_seq[chrm][
end - 3 : end - 1
].reverse.complement.seq
right_site = genome_seq[chrm][
start : start + 2
].reverse.complement.seq
l[6] = "{}-{}".format(left_site, right_site)
splice_site = l[6].upper()
if splice_site in {"GT-AG", "GC-AG", "AT-AC"}:
out.write("{}\n".format("\t".join(l[:7])))
overlap_file = f"{tmp_dir}/{rnd_id}.overlap.bed"
cmd = f"bedtools intersect -s -wo -a {junction_bed} -b {cds} > {overlap_file}"
run_cmd(cmd, "Junctions intersect with CDS,Junctions intersect with CDS finished!")
# no overlap in CDS and junctions file
if os.path.isfile(overlap_file) and os.path.getsize(overlap_file) == 0:
remove(overlap_file)
remove(junction_bed)
print("No overlaps found in {} and gencode CDS".format(janno))
return None, None
# overlaps found in CDS and junctions file
elif os.path.isfile(overlap_file) and os.path.getsize(overlap_file) > 0:
tmp_dict = OrderedDict()
with open(overlap_file) as f:
for line in f:
l = line.rstrip().split("\t")
chrm = l[0]
length = int(l[-1])
junc_start = int(l[1])
junc_end = int(l[2])
junc_id = l[3]
junc_read_no = l[4]
strand = l[5]
splice_site = l[6]
ref_start = int(l[8])
ref_end = int(l[9])
gene_name = l[11]
gene_id = l[10]
pos_key = "{}:{}-{}".format(chrm, junc_start, junc_end)
if (
length == junc_end - junc_start
and junc_start > ref_start
and junc_end < ref_end
and chrm in non_mito_chrms
and int(junc_read_no) >= ao_cutoff
):
if pos_key not in tmp_dict:
info = "{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}".format(
chrm,
junc_start,
junc_end,
junc_id,
junc_read_no,
strand,
gene_name,
length - 1,
splice_site,
gene_id,
total_junctions,
)
tmp_dict[pos_key] = info
remove(overlap_file)
remove(junction_bed)
# exitrons found
if len(tmp_dict) > 0:
position_bed_file = (
os.path.splitext(os.path.basename(janno))[0] + ".position.bed"
)
out = open(position_bed_file, "w")
src_exitron_file = os.path.splitext(os.path.basename(janno))[0] + ".src"
output = open(src_exitron_file, "w")
position_set = set([])
for i in tmp_dict:
(
chrm,
junc_start,
junc_end,
junc_id,
junc_read_no,
strand,
gene_name,
junc_len,
splice_site,
gene_id,
total_junctions,
) = tmp_dict[i].split("\t")
# chrm = reverse_chrms_dict[chrm]
output.write(
"{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n".format(
chrm,
junc_start,
junc_end,
junc_id,
junc_read_no,
strand,
gene_name,
junc_len,
splice_site,
gene_id,
total_junctions,
)
)
start = int(junc_start)
end = int(junc_end)
# ao = int(junc_read_no)
middle_point = int(np.median([start, end]))
if "{}\t{}".format(chrm, start) not in position_set:
out.write("{}\t{}\t{}\n".format(chrm, start - 1, start))
position_set.add("{}\t{}".format(chrm, start))
if "{}\t{}".format(chrm, end) not in position_set:
out.write("{}\t{}\t{}\n".format(chrm, end - 1, end))
position_set.add("{}\t{}".format(chrm, end))
if "{}\t{}".format(chrm, middle_point) not in position_set:
out.write(
"{}\t{}\t{}\n".format(chrm, middle_point - 1, middle_point)
)
position_set.add("{}\t{}".format(chrm, middle_point))
output.close()
out.close()
else:
print("No exitron found in {}".format(janno))
return None, None
return src_exitron_file, position_bed_file
def percent_spliced_out(
bam_file, src_exitron_file, position_bed_file, ao_cutoff, pso_cutoff, mapq, out
):
print("Reading BAM file: {}".format(bam_file))
depth_dict = {}
cmd = "samtools bedcov {0} {1} -Q {2}".format(position_bed_file, bam_file, mapq)
depth_flag, result = run_cmd(cmd, "Calculate PSO and PSI.")
if depth_flag:
result_file = BytesIO(result)
result_string = result_file.getvalue().decode("utf-8")
for line in result_string.split("\n"):
if line:
chrm, _, pos, depth = line.rstrip().split()
depth_dict["{}\t{}".format(chrm, pos)] = int(depth)
with open(src_exitron_file) as f:
for line in f:
l = line.rstrip("\n").split("\t")
chrm = l[0]
start = int(l[1])
end = int(l[2])
ao = int(l[4])
strand = l[5]
middle_point = int(np.median([start, end]))
if strand == "+":
five_prime_reads = depth_dict["{}\t{}".format(chrm, start)] - ao
three_prime_reads = depth_dict["{}\t{}".format(chrm, end)] - ao
middle_reads = depth_dict["{}\t{}".format(chrm, middle_point)] - ao
elif strand == "-":
five_prime_reads = depth_dict["{}\t{}".format(chrm, end)] - ao
three_prime_reads = depth_dict["{}\t{}".format(chrm, start)] - ao
middle_reads = depth_dict["{}\t{}".format(chrm, middle_point)] - ao
ave_dp = (five_prime_reads + three_prime_reads + middle_reads) / 3.0
if five_prime_reads < 0 or three_prime_reads < 0:
continue
try:
pso = float(ao) / (ave_dp + ao)
except ZeroDivisionError:
print(f"Error in {chrm} {start} {end}")
pso = 0
psi = 1.0 - float("{:.3g}".format(pso))
dp = int(ao / pso)
if ao >= ao_cutoff and pso >= pso_cutoff:
out.write(
"{}\t{:.3g}\t{}\t{}\t{}\n".format(
"\t".join(l[:-1]), pso, psi, dp, l[-1]
)
)
os.remove(src_exitron_file)
os.remove(position_bed_file)
out.close()
print("Finished reading BAM file: {}".format(bam_file))
def external_tool_checking():
"""checking dependencies are installed"""
software = ["regtools", "bedtools", "samtools"]
cmd = "which"
for each in software:
try:
path = subprocess.check_output([cmd, each], stderr=subprocess.STDOUT)
path = str(path, "utf-8")
except subprocess.CalledProcessError:
print(
"Checking for '" + each + "': ERROR - could not find '" + each + "'",
file=sys.stderr,
)
print("Exiting.", file=sys.stderr)
sys.exit(0)
print("Checking for '" + each + "': found " + path)
def done_file(name):
out = open(name + ".done", "w")
out.write("done!")
out.close()
def MAPQ_filter(in_bam, threads=6, mapq=50):
prefix = os.path.splitext(os.path.basename(in_bam))[0]
if os.path.exists(f"{prefix}.hq.bam.done"):
status_message(f"{prefix}.hq.bam found, skip MAPQ filtering!\n")
return "{}.hq.bam".format(prefix)
cmd = "samtools view -q {0} -@ {1} -O BAM -o {2}.hq.bam {3} && samtools index {2}.hq.bam".format(
mapq, threads, prefix, in_bam
)
filter_flag, _ = run_cmd(cmd, "BAM filtering begins, BAM filtering finished.")
if filter_flag:
done_file("{}.hq.bam".format(prefix))
return "{}.hq.bam".format(prefix)
else:
return False
def seq_dict(ref="hg38", config=None):
if not config:
sys.stderr.write("No config file was found!\n")
sys.exit(1)
if ref == "hg19":
fasta = config["hg19_ref"]
elif ref == "hg38":
fasta = config["hg38_ref"]
genome_dict = Fasta(fasta, sequence_always_upper=True)
return genome_dict
def parse_args():
parser = argparse.ArgumentParser(
description="%(prog)s -i input_rna_seq_bam_file -r [hg38/hg19] -m mapping_quality",
epilog="ScanExitron: detecting exitron splicing events using RNA-Seq data",
)
parser.add_argument(
"-i",
"--input",
action="store",
dest="input",
help="Input BAM/CRAM file along with BAI/CRAI file",
required=True,
)
parser.add_argument(
"-a",
"--ao",
action="store",
dest="ao",
type=int,
help="AO cutoff (default: %(default)s)",
default=3,
)
parser.add_argument(
"-p",
"--pso",
action="store",
dest="pso",
type=float,
help="PSO cutoff (default: %(default)s)",
default=0.05,
)
parser.add_argument(
"-m",
"--mapq",
action="store",
dest="mapq",
type=int,
help="consider reads with MAPQ >= cutoff (default: %(default)s)",
default=50,
)
parser.add_argument(
"-s",
"--strand",
action="store",
dest="strand",
type=int,
help="Strand specificity of RNA library preparation (0 = unstranded, 1 = first-strand/RF, 2, = second-strand/FR) (default: %(default)s)",
default=1,
)
parser.add_argument(
"-t",
"--threads",
action="store",
dest="threads",
type=int,
help="number of threads (default: %(default)s)",
default=1,
)
parser.add_argument(
"-c",
"--config",
action="store",
dest="config",
type=str,
help="config file (default: %(default)s)",
default="config.ini",
)
parser.add_argument(
"-r",
"--ref",
action="store",
dest="ref",
help="reference (default: %(default)s)",
choices=["hg19", "hg38"],
default="hg38",
)
parser.add_argument(
"-v", "--version", action="version", version="%(prog)s {}".format(__version__)
)
args = parser.parse_args()
return args
def main():
external_tool_checking()
args = parse_args()
config = config_getter(args.config)
out_bam = MAPQ_filter(in_bam=args.input, threads=args.threads, mapq=args.mapq)
tmp_dir = TemporaryDirectory()
tmp_dir_name = tmp_dir.name
prefix = os.path.splitext(os.path.basename(args.input))[0]
outfile = prefix + ".exitron"
outstream = open(outfile, "w")
outstream.write(
"chrom\tstart\tend\tname\tao\tstrand\tgene_symbol\tlength\tsplice_site\tgene_id\tpso\tpsi\tdp\ttotal_junctions\n"
)
if out_bam:
janno_file = junction_caller(
bam_file=out_bam,
ref=args.ref,
strand=args.strand,
config=config,
tmp_dir=tmp_dir_name,
)
src_exitron_file, position_bed_file = junction_overlap_CDS_to_position_BED(
janno_file,
ao_cutoff=args.ao,
ref=args.ref,
tmp_dir=tmp_dir_name,
config=config,
)
if src_exitron_file is not None and position_bed_file is not None:
percent_spliced_out(
bam_file=args.input,
src_exitron_file=src_exitron_file,
position_bed_file=position_bed_file,
ao_cutoff=args.ao,
pso_cutoff=args.pso,
mapq=args.mapq,
out=outstream,
)
tmp_dir.cleanup()
# remove(janno_file)
outstream.close()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
sys.stderr.write("User interrupt me ^_^ \n")
sys.exit(1)