-
Notifications
You must be signed in to change notification settings - Fork 6
/
find_circ.py
executable file
·1610 lines (1302 loc) · 59.2 KB
/
find_circ.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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
import os,sys,re
from collections import defaultdict
from gzip import GzipFile
import numpy as np
from numpy import chararray as carray
from numpy import fromstring,byte
import mmap
import logging
import pysam
import optparse
import traceback
__version__ = "1.99"
__author__ = "Marvin Jens"
__credits__ = ["Marvin Jens","Marcel Schilling","Petar Glazar","Nikolaus Rajewsky"]
__status__ = "beta"
__licence__ = "GPL"
__email__ = "[email protected]"
COMPLEMENT = {
'a' : 't',
't' : 'a',
'c' : 'g',
'g' : 'c',
'k' : 'm',
'm' : 'k',
'r' : 'y',
'y' : 'r',
's' : 's',
'w' : 'w',
'b' : 'v',
'v' : 'b',
'h' : 'd',
'd' : 'h',
'n' : 'n',
'A' : 'T',
'T' : 'A',
'C' : 'G',
'G' : 'C',
'K' : 'M',
'M' : 'K',
'R' : 'Y',
'Y' : 'R',
'S' : 'S',
'W' : 'W',
'B' : 'V',
'V' : 'B',
'H' : 'D',
'D' : 'H',
'N' : 'N',
}
def complement(s):
return "".join([COMPLEMENT[x] for x in s])
def rev_comp(seq):
return complement(seq)[::-1]
def k_mers(k,alphabet=['A','C','G','T',]):
if k == 1:
prefix = [""]
else:
prefix = k_mers(k-1,alphabet)
for pre in prefix:
for a in alphabet:
yield pre+a
# precompute RC of all possible 4-mers for fast
# splice signal check later on.
fast_4mer_RC = {}
for mer in k_mers(4,alphabet=['A','C','G','T','N']):
fast_4mer_RC[mer] = rev_comp(mer)
class mmap_fasta(object):
def __init__(self,fname):
f = file(fname)
header = f.readline()
row = f.readline()
self.ofs = len(header)
self.lline = len(row)
self.ldata = len(row.strip())
self.skip = self.lline-self.ldata
self.skip_char = row[self.ldata:]
self.mmap = mmap.mmap(f.fileno(),0,prot=mmap.PROT_READ)
def __getslice__(self,start,end):
l_start = start / self.ldata
l_end = end / self.ldata
ofs_start = l_start * self.skip + start + self.ofs
ofs_end = l_end * self.skip + end + self.ofs
s = self.mmap[ofs_start:ofs_end].replace(self.skip_char,"")
L = end-start
if len(s) == L:
return s
else:
return s+"N"*(L-len(s))
return
class indexed_fasta(object):
def __init__(self,fname,split_chrom="",**kwargs):
self.logger = logging.getLogger("indexed_fasta")
self.fname = fname
self.chrom_stats = {}
self.split_chrom = split_chrom
# try to load index
ipath = fname + '.byo_index'
if os.access(ipath,os.R_OK):
self.load_index(ipath)
else:
self.index()
self.store_index(ipath)
f = file(fname)
self.mmap = mmap.mmap(f.fileno(),0,prot=mmap.PROT_READ)
def index(self):
self.logger.info("# indexed_fasta.index('%s')" % self.fname)
ofs = 0
f = file(self.fname)
chrom = "undef"
chrom_ofs = 0
size = 0
nl_char = 0
for line in f:
ofs += len(line)
if line.startswith('>'):
# store size of previous sequence
if size: self.chrom_stats[chrom].append(size)
chrom = line[1:].split()[0].strip()
if self.split_chrom:
# use this to strip garbage from chrom/contig name like chr1:new
# ->split_chrom=':' -> chrom=chr1
chrom = chrom.split(self.split_chrom)[0]
chrom_ofs = ofs
else:
if not chrom in self.chrom_stats:
# this is the first line of the new chrom
size = 0
lline = len(line)
ldata = len(line.strip())
nl_char = lline - ldata
self.chrom_stats[chrom] = [chrom_ofs,ldata,nl_char,line[ldata:]]
size += len(line.strip())
# store size of previous sequence
if size: self.chrom_stats[chrom].append(size)
f.close()
def store_index(self,ipath):
self.logger.info("# indexed_fasta.store_index('%s')" % ipath)
# write to tmp-file first and in the end rename in order to have this atomic
# otherwise parallel building of the same index may screw it up.
import tempfile
tmp = tempfile.NamedTemporaryFile(mode="w",dir = os.path.dirname(ipath),delete=False)
for chrom in sorted(self.chrom_stats.keys()):
ofs,ldata,skip,skipchar,size = self.chrom_stats[chrom]
tmp.write("%s\t%d\t%d\t%d\t%r\t%d\n" % (chrom,ofs,ldata,skip,skipchar,size))
# make sure everything is on disk
os.fsync(tmp)
tmp.close()
# make it accessible to everyone
import stat
os.chmod(tmp.name, stat.S_IROTH | stat.S_IRGRP | stat.S_IRUSR)
# this is atomic on POSIX as we have created tmp in the same directory,
# therefore same filesystem
os.rename(tmp.name,ipath)
def load_index(self,ipath):
self.logger.info("# indexed_fasta.load_index('%s')" % ipath)
self.chrom_stats = {}
for line in file(ipath):
chrom,ofs,ldata,skip,skipchar,size = line.rstrip().split('\t')
self.chrom_stats[chrom] = (int(ofs),int(ldata),int(skip),skipchar[1:-1].decode('string_escape'),int(size))
def get_data(self,chrom,start,end,sense):
if not self.chrom_stats:
self.index()
ofs,ldata,skip,skip_char,size = self.chrom_stats[chrom]
pad_start = 0
pad_end = 0
if start < 0:
pad_start = -start
start = 0
if end > size:
pad_end = end - size
end = size
l_start = start / ldata
l_end = end / ldata
ofs_start = l_start * skip + start + ofs
ofs_end = l_end * skip + end + ofs
s = self.mmap[ofs_start:ofs_end].replace(skip_char,"")
if pad_start or pad_end:
s = "N"*pad_start + s + "N"*pad_end
if sense == "-":
s = rev_comp(s)
return s
class Accessor(object):
supports_write = False
def __init__(self,path,chrom,sense,sense_specific=False,**kwargs):
if sense_specific:
self.covered_strands = [chrom+sense]
else:
self.covered_strands = [chrom+'+',chrom+'-']
def get_data(self,chrom,start,end,sense,**kwargs):
return []
def get_oriented(self,chrom,start,end,sense,**kwargs):
data = self.get_data(chrom,start,end,sense,**kwargs)
if sense == "-": # self.sense_specific and
return data[::-1]
else:
return data
def get_sum(self,chrom,start,end,sense,**kwargs):
return self.get_data(chrom,start,end,sense,**kwargs).sum()
def flush(self):
pass
class Track(object):
"""
Abstraction of chromosome-wide adressable data like sequences, coverage, scores etc.
Actual access to the data is delegated to accessor objects which are instantiated on-the-fly for
each chromosome (strand) upon first access and then cached.
Use of mmap for the accessors is recommended and implemented for sequences and numpy (C-type)
arrays.
See io/track_accessors.py for more examples.
"""
def __init__(self,path,accessor,sense_specific=True,description="unlabeled track",system="hg18",dim=1,auto_flush=False,mode="r",**kwargs):
self.path = path
self.mode = mode
self.acc_cache = {}
self.accessor = accessor
self.kwargs = kwargs
self.sense_specific = sense_specific
self.dim = int(dim)
self.description = description
self.auto_flush = auto_flush
self.last_chrom = ""
self.logger = logging.getLogger("Track('%s')" % path)
self.system = system
self.logger.debug("Track(auto_flush=%s)" % (str(auto_flush)))
kwargs['sense_specific'] = self.sense_specific
kwargs['mode'] = self.mode
kwargs['system'] = self.system
kwargs['description'] = self.description
kwargs['dim'] = self.dim
def load(self,chrom,sense):
# automatically flush buffers whenever a new chromosome is seen. reduces memory-footprint for sorted input data
if self.auto_flush and chrom != self.last_chrom:
self.logger.debug("Seen new chromosome %s. Flushing accessor caches." % chrom)
self.flush_all()
self.last_chrom = chrom
ID = chrom+sense
if not ID in self.acc_cache:
self.logger.debug("Cache miss for %s%s. creating new accessor" % (chrom,sense))
acc = self.accessor(self.path,chrom,sense,**(self.kwargs))
# register the accessor for as many chromosomes/strands/contigs as it feels responsible for.
self.logger.debug("Accessor covers strands '%s'" % str(sorted(set(acc.covered_strands))))
if acc.covered_strands == '*':
# hint that this accessors covers the complete genome
self.logger.debug("disabling auto_flush and registering accessor for everything")
self.acc_cache = DummyCache(acc)
self.auto_flush = False
else:
for ID in acc.covered_strands:
self.acc_cache[ID] = acc
return self.acc_cache[ID]
def flush(self,chrom,sense):
ID = self.get_identifier(chrom,sense)
if ID in self.acc_cache:
self.logger.warning("Flushing %s%s" % (chrom,sense))
del self.acc_cache[ID]
def flush_all(self):
for a in self.acc_cache.values():
a.flush()
self.acc_cache = {}
def get(self,chrom,start,end,sense,**kwargs):
acc = self.load(chrom,sense)
return acc.get_data(chrom,start,end,sense,**kwargs)
def get_oriented(self,chrom,start,end,sense,**kwargs):
acc = self.load(chrom,sense)
return acc.get_oriented(chrom,start,end,sense,**kwargs)
def get_sum(self,chrom,start,end,sense):
#print "TRACK.GETSUM"
acc = self.load(chrom,sense)
return acc.get_sum(chrom,start,end,sense)
def get_identifier(self,chrom,sense):
if self.sense_specific:
return chrom+sense
else:
return chrom
class GenomeAccessor(Accessor):
def __init__(self,path,chrom,sense,system='hg19',**kwargs):
super(GenomeAccessor,self).__init__(path,chrom,sense,system=system,**kwargs)
self.logger = logging.getLogger("GenomeAccessor")
self.logger.info("# mmap: Loading genomic sequence for chromosome %s from '%s'" % (chrom,path))
self.system = system
self.data = None
fname = os.path.join(path)
try:
self.data = indexed_fasta(fname)
except IOError:
self.logger.warning("Could not access '%s'. Switching to dummy mode (only Ns)" % fname)
self.get_data = self.get_dummy
self.get_oriented = self.get_dummy
self.covered_strands = [chrom+'+',chrom+'-']
else:
# register for all chroms/strands
self.covered_strands = [chrom+'+' for chrom in self.data.chrom_stats.keys()] + [chrom+'-' for chrom in self.data.chrom_stats.keys()]
# TODO: maybe remove this if not needed
self.get = self.get_oriented
def load_indexed(self,path):
ipath = path+'.index'
if not os.access(ipath,os.R_OK):
index = self.build_index(path,ipath)
else:
index = self.load_index(ipath)
self.chrom_ofs = index.chrom_ofs
def get_data(self,chrom,start,end,sense):
seq = self.data.get_data(chrom,start,end,"+")
if sense == "-":
seq = complement(seq)
return seq
def get_dummy(self,chrom,start,end,sense):
return "N"*int(end-start)
usage = """
bwa mem -t<threads> [-p] -A2 -B10 -k 15 -T 1 $GENOME_INDEX reads.fastq.gz | %prog [options]
OR:
%prog [options] <bwa_mem_genome_alignments.bam>
"""
parser = optparse.OptionParser(usage=usage)
parser.add_option("-v","--version",dest="version",action="store_true",default=False,help="get version information")
parser.add_option("-S","--system",dest="system",type=str,default="",help="model system database (optional! Requires byo library.)")
parser.add_option("-G","--genome",dest="genome",type=str,default="",help="path to genome (either a folder with chr*.fa or one multichromosome FASTA file)")
parser.add_option("","--known-circ",dest="known_circ",type=str,default="",help="file with known circRNA junctions (BED6)")
parser.add_option("","--known-lin",dest="known_lin",type=str,default="",help="file with known linear splice junctions (BED6)")
parser.add_option("-o","--output",dest="output",default="find_circ_run",help="where to store output")
parser.add_option("-q","--silent",dest="silent",default=False,action="store_true",help="suppress any normal output to stdout. Automatically switched on when using --stdout redirection")
parser.add_option("","--stdout",dest="stdout",default=None,choices=['circs','lins','reads','multi','test'],help="use to direct chosen type of output (circs, lins, reads, multi) to stdout instead of file")
parser.add_option("-n","--name",dest="name",default="unknown",help="tissue/sample name to use (default='unknown')")
parser.add_option("","--min-uniq-qual",dest="min_uniq_qual",type=int,default=2,help="minimal uniqness for anchor alignments to consider (default=2)")
parser.add_option("-a","--anchor",dest="asize",type=int,default=15,help="anchor size (default=15)")
parser.add_option("-m","--margin",dest="margin",type=int,default=2,help="maximum nts the BP is allowed to reside within a segment (default=2)")
parser.add_option("-d","--max-mismatch",dest="maxdist",type=int,default=2,help="maximum mismatches (no indels) allowed in segment extensions (default=2)")
parser.add_option("","--short-threshold",dest="short_threshold",type=int,default=100,help="minimal genomic span [nt] of a circRNA before it is labeled SHORT (default=100)")
parser.add_option("","--huge-threshold",dest="huge_threshold",type=int,default=100000,help="maximal genomic span [nt] of a circRNA before it is labeled HUGE (default=100000)")
parser.add_option("","--debug",dest="debug",default=False,action="store_true",help="Activate LOTS of debug output")
parser.add_option("","--profile",dest="profile",default=False,action="store_true",help="Activate performance profiling")
parser.add_option("","--non-canonical",dest="noncanonical",default=False,action="store_true",help="relax the GU/AG constraint (will produce many more ambiguous counts)")
parser.add_option("","--all-hits",dest="allhits",default=False,action="store_true",help="in case of ambiguities, report each hit")
parser.add_option("","--stranded",dest="stranded",default=False,action="store_true",help="use if the reads are stranded. By default it will be used as control only, use with --strand-pref for breakpoint disambiguation.")
parser.add_option("","--strand-pref",dest="strandpref",default=False,action="store_true",help="prefer splice sites that match annotated direction of transcription")
parser.add_option("","--half-unique",dest="halfunique",default=False,action="store_true",help="also report junctions where only one anchor aligns uniquely (less likely to be true)")
parser.add_option("","--report-nobridges",dest="report_nobridges",default=False,action="store_true",help="also report junctions lacking at least a single read where both anchors, jointly align uniquely (not recommended. Much less likely to be true.)")
parser.add_option("-B","--bam",dest="bam",default=False,action="store_true",help="store anchor alignments that were recorded as linear or circular junction candidates")
parser.add_option("-t","--throughput",dest="throughput",default=False,action="store_true",help="print information on throughput to stderr (useful for benchmarking)")
parser.add_option("","--chunk-size",dest="chunksize",type=int,default=100000,help="number of reads to be processed in one chunk (default=100000)")
parser.add_option("","--noop",dest="noop",default=False,action="store_true",help="Do not search for any junctions. Only process the alignment stream (useful for benchmarking)")
parser.add_option("","--test",dest="test",default=False,action="store_true",help="Test mode: parse read splicing from read name and compare to reconstructed splicing. Use with specially prepared test reads only! (useful for automated regression testing)")
parser.add_option("","--no-linear",dest="nolinear",default=False,action="store_true",help="Do not investigate linear junctions, unless associated with another backsplice event (saves some time)")
parser.add_option("","--no-multi",dest="multi_events",default=True,action="store_false",help="Do not record multi-events (saves some time)")
options,args = parser.parse_args()
if options.version:
print """find_circ.py version {0}\n\n(c) Marvin Jens 2012-2016.\nCheck http://www.circbase.org for more information.""".format(__version__)
sys.exit(0)
# prepare output directory
if not os.path.isdir(options.output):
os.makedirs(options.output)
# prepare logging system
FORMAT = '%(asctime)-20s\t%(levelname)s\t%(name)s\t%(message)s'
logging.basicConfig(level=logging.INFO,format=FORMAT,filename=os.path.join(options.output,"run.log"),filemode='w')
logger = logging.getLogger('find_circ')
logger.info("find_circ {0} invoked as '{1}'".format(__version__," ".join(sys.argv)))
if options.system:
import importlib
system = importlib.import_module("byo.systems.{options.system}".format(**locals()))
genome = system.genome
if options.genome:
genome = Track(options.genome,accessor=GenomeAccessor)
if not (options.genome or options.system):
print "need to specify either model system database (-S) or genome FASTA file (-G)."
sys.exit(1)
# prepare output files
circs_file = file(os.path.join(options.output,"circ_splice_sites.bed"),"w")
lins_file = file(os.path.join(options.output,"lin_splice_sites.bed"),"w")
reads_file = GzipFile(os.path.join(options.output,"spliced_reads.fastq.gz"),"w")
multi_file = file(os.path.join(options.output,"multi_events.tsv"),"w")
if options.test:
test_file = file(os.path.join(options.output,"test_results.tsv"),"w")
else:
test_file = None
# redirect output to stdout, if requested
if options.stdout:
varname = "{0}_file".format(options.stdout)
out_file = globals()[varname]
out_file.write('# redirected to stdout\n')
logger.info('redirected {0} to stdout'.format(options.stdout))
globals()[varname] = sys.stdout
if args:
logger.info('reading from {0}'.format(args[0]))
if args[0].endswith('sam'):
sam_input = pysam.Samfile(args[0],'r')
else:
sam_input = pysam.Samfile(args[0],'rb')
else:
logger.info('reading from stdin')
sam_input = pysam.Samfile('-','r')
tid_cache = {}
def fast_chrom_lookup(read):
tid = read.tid
if not tid in tid_cache:
tid_cache[tid] = sam_input.getrname(tid)
return tid_cache[tid]
if options.bam:
bam_filename = os.path.join(options.output,"spliced_alignments.bam")
bam_out = pysam.Samfile(bam_filename,'wb',template=sam_input)
else:
bam_out = None
class Hit(object):
def __init__(self,name,splice):
self.name = name
self.reads = []
self.readnames = []
self.uniq = set()
self.mapquals_A = []
self.mapquals_B = []
self.n_weighted = 0.
self.n_spanned = 0
self.n_uniq_bridges = 0.
self.edits = []
self.overlaps = []
self.n_hits = []
self.signal = "NNNN"
self.strand_plus = 0
self.strand_minus = 0
self.strandmatch = 'NA'
self.flags = defaultdict(int)
self.read_flags = defaultdict(set)
self.tissues = defaultdict(float)
self.coord = splice.coord
self.add(splice)
def add_flag(self,flag,frag_name):
self.flags[flag] += 1
self.read_flags[frag_name].add(flag)
def get_flags_counts(self):
if not self.flags:
return ["N/A"],[0]
flags = sorted(self.flags)
counts = [self.flags[f] for f in flags]
return flags,counts
def add(self,splice):
#print self.name,self.coord,splice.junc_span,self.weighted_counts
self.signal = splice.gtag
# TODO: this belongs to output, not here!
self.strandmatch = 'N/A'
if options.stranded:
if splice.strandmatch:
self.strandmatch = "MATCH"
else:
self.strandmatch = "MISMATCH"
self.edits.append(splice.dist)
self.overlaps.append(splice.ov)
self.n_hits.append(splice.n_hits)
if splice.junc_span:
self.n_spanned += 1
self.n_weighted += splice.junc_span.weight
# TODO: Move this logic into JunctionSpan, bc it is closer to the underlying alignments
# by convention have A precede B in the genome.
A = splice.junc_span.align_A
B = splice.junc_span.align_B
read = splice.junc_span.primary.seq
weight = splice.junc_span.weight
if splice.is_backsplice:
A,B = B,A
# Alignment Score - Secondbest hit score
aopt = dict(A.tags)
bopt = dict(B.tags)
qA = aopt.get('AS') - aopt.get('XS',0)
qB = bopt.get('AS') - bopt.get('XS',0)
if qA and qB:
# both anchors from the *same read* align uniquely
self.n_uniq_bridges += weight
self.mapquals_A.append(qA)
self.mapquals_B.append(qB)
self.readnames.append(splice.junc_span.primary.qname)
# record the spliced read sequence as it was before mapping
if A.is_reverse:
self.strand_minus += weight
self.reads.append(rev_comp(read))
else:
self.strand_plus += weight
self.reads.append(read)
sample_name = options.name
self.tissues[sample_name] += weight
self.uniq.add((read,sample_name))
self.uniq.add((rev_comp(read),sample_name))
@property
def n_frags(self):
return len(set(self.readnames))
@property
def n_uniq(self):
return len(self.uniq) / 2 # forward and rev_comp sequences
def get_best_anchor_quals(self):
return sorted(self.mapquals_A,reverse=True)[0], sorted(self.mapquals_B,reverse=True)[0]
def get_tissue_names_counts(self):
tissues = sorted(self.tissues.keys())
tiss_counts = [str(self.tissues[k]) for k in tissues]
return tissues, tiss_counts
@property
def categories(self):
categories = []
if self.signal != "GTAG":
categories.append("NON_CANONICAL")
#if strandmatch == "MATCH":
#categories.append("STRANDMATCH")
best_qual_A, best_qual_B = self.get_best_anchor_quals()
if best_qual_A == 0 or best_qual_B == 0:
categories.append("WARN_NON_UNIQUE_ANCHOR")
if self.n_uniq_bridges == 0:
categories.append("WARN_NO_UNIQ_BRIDGES")
if min(self.n_hits) > 1:
categories.append("WARN_AMBIGUOUS_BP")
min_anchor_ov = min(self.overlaps)
min_edit = min(self.edits)
if min_anchor_ov == 0 and min_edit == 0:
pass
elif min_anchor_ov < 2 and min_edit < 2:
categories.append("WARN_EXT_1MM")
elif min_anchor_ov >= 2 or min_edit >= 2:
categories.append("WARN_EXT_2MM+")
chrom,start,end,sense = self.coord
if end-start < options.short_threshold:
categories.append("SHORT")
elif end-start > options.huge_threshold:
categories.append("HUGE")
unbroken_circread = 0
unwarned_circread = 0
total = 0.
for frag_name in self.read_flags.keys():
total += 1.
if not 'BROKEN_SEGMENTS' in self.read_flags[frag_name]:
unbroken_circread += 1
for w in self.read_flags[frag_name]:
if not w.startswith('WARN'):
unwarned_circread += 1
if total:
warn_ratio = 1. - unwarned_circread / total
broken_ratio = 1. - unbroken_circread / total
if not unbroken_circread:
categories.append('WARN_ALWAYS_BROKEN')
if not unwarned_circread:
categories.append('WARN_ALWAYS_WARN')
return categories
class SpliceSiteStorage(object):
def __init__(self, prefix, known):
self.prefix = prefix
self.sites = {}
self.novel_count = 0
if known:
self.load_known_sites(known)
def load_known_sites(self,fname):
n = 0
for line in file(fname):
if line.startswith('#'):
continue
parts = line.rstrip().split('\t')
chrom,start,end,name,score,sense = parts[:6] # BED6
start = int(start)
end = int(end)
coord = (chrom,start,end,sense)
self.sites[coord] = Hit(name,Splice(None,chrom,start,end,sense,10,10,'NNNN'))
n += 1
logger.info("loaded {0} known splice sites from '{1}'".format(n,fname))
def add(self,splice):
coord = splice.coord
if not coord in self.sites:
self.novel_count += 1
name = "{0}_{1}_{2:06d}".format(options.name,self.prefix,self.novel_count)
self.sites[coord] = Hit(name,splice)
else:
self.sites[coord].add(splice)
return self.sites[coord]
def __getitem__(self,coord):
return self.sites[coord]
def store_list(self,output):
output.write("#" + "\t".join(['chrom','start','end','name','n_frags','strand','n_weight','n_spanned','n_uniq','uniq_bridges','best_qual_left','best_qual_right','tissues','tiss_counts','edits','anchor_overlap','breakpoints','signal','strandmatch','category','flags','flag_counts']) + "\n")
for (chrom,start,end,sense),hit in self.sites.items():
if not hit.reads:
continue # a known splice site that was not recovered here
#n_spanned,n_weight,n_uniq,best_qual_A,best_qual_B,uniq_bridges,tissues,tiss_counts,min_edit,min_anchor_ov,n_hits,signal,strandmatch = hit.scores()
#n_spanned,self.weighted_counts,n_uniq,self.best_qual_A,self.best_qual_B,self.uniq_bridges,tissues,tiss_counts,min(self.edits),min(self.overlaps),min(self.n_hits),self.signal,self.strandmatch
best_qual_A, best_qual_B = hit.get_best_anchor_quals()
if options.halfunique:
if (best_qual_A < options.min_uniq_qual) and (best_qual_B < options.min_uniq_qual):
N['anchor_not_uniq'] += 1
continue
else:
if (best_qual_A < options.min_uniq_qual) or (best_qual_B < options.min_uniq_qual):
N['anchor_not_uniq'] += 1
continue
if (hit.n_uniq_bridges == 0) and not options.report_nobridges:
N['no_uniq_bridges'] += 1
continue
tissues, tiss_counts = hit.get_tissue_names_counts()
flags, flag_counts = hit.get_flags_counts()
bed = [
chrom, start, end, hit.name, hit.n_frags, sense, # BED6 compatible
hit.n_weighted, hit.n_spanned, hit.n_uniq, hit.n_uniq_bridges, # read counts
best_qual_A, best_qual_B, # mapping reliability
",".join(tissues), ",".join(tiss_counts), # sample association
min(hit.edits), min(hit.overlaps), min(hit.n_hits), hit.signal, hit.strandmatch, ",".join(sorted(hit.categories) ), # splice site detection reliability
",".join(flags), ",".join([str(c) for c in flag_counts]), # internal structure support from fragment evidence (BWA MEM)
]
output.write("\t".join([str(b) for b in bed]) + "\n")
class MultiEventRecorder(object):
def __init__(self):
multi_file.write("#" + "\t".join(['chrom','start','end','name','score','strand','fragment_name','lin_cons','lin_incons','unspliced_cons','unspliced_incons']) + "\n")
def record(self,frag_name,circ_hit,lin_cons,lin_incons,unspliced_cons,unspliced_incons):
score = len(lin_cons) - 10 * len(lin_incons) + len(unspliced_cons) - 10 * len(unspliced_incons)
circ_chrom,circ_start,circ_end,circ_sense = circ_hit.coord
cols = [circ_chrom, str(circ_start), str(circ_end), "ME:"+circ_hit.name, str(score), circ_sense, frag_name]
if not lin_cons:
cols.append("NO_LIN_CONS")
else:
cols.append(",".join([ "{0:d}-{1:d}".format(start,end) for chrom,start,end,sense in lin_cons]))
if not lin_incons:
cols.append("NO_LIN_INCONS")
else:
cols.append(",".join([ "[{0:s}:{1:d}-{2:d}]".format(chrom,start,end) for chrom,start,end,sense in lin_incons]))
if not unspliced_cons:
cols.append("NO_UNSPLICED_CONS")
else:
cols.append(",".join([ "{0:d}-{1:d}".format(start,end) for chrom,start,end,sense in unspliced_cons]))
if not unspliced_incons:
cols.append("NO_UNSPLICED_INCONS")
else:
cols.append(",".join([ "[{0:s}:{1:d}-{2:d}]".format(chrom,start,end) for chrom,start,end,sense in unspliced_incons]))
multi_file.write('\t'.join(cols) + '\n')
class Splice(object):
def __init__(self,junc_span,chrom,start,end,strand,dist,ov,gtag):
self.junc_span = junc_span
self.chrom = chrom
self.start = start
self.end = end
self.strand = strand
self.dist = dist
self.ov = ov
self.gtag = gtag
self.n_hits = 1 # if necessary, this is changed by JunctionSpan.find_breakpoints() after breaking ties
@property
def is_canonical(self):
return self.gtag == 'GTAG'
@property
def is_backsplice(self):
# the underlying alignment segment pair knows
return self.junc_span.is_backsplice
def __str__(self):
"python magic to make nice debug output"
return "Splice({self.chrom}:{self.start}-{self.end}:{self.strand} edits={self.dist} ov={self.ov} score={self.score} backsplice={self.is_backsplice})".format(**locals())
@property
def score(self):
"Used to rank different possible splices. higher is better"
# canonical beats low extension mismatches, beats low anchor overlap
s = self.is_canonical*20 - self.dist * 10 - self.ov
if options.strandpref:
s += 100 * (self.strand == self.junc_span.strand)
return s
@property
def coord(self):
if self.start < self.end:
return (self.chrom,self.start,self.end,self.strand)
else:
return (self.chrom,self.end,self.start,self.strand)
def uniqness(align):
"""
Difference between reported alignment score (AS) and
the next best hit's score (XS).
"""
u = align.get_tag('AS')
if align.has_tag('XS'):
u -= align.get_tag('XS')
return u
class JunctionSpan(object):
def __init__(self,align_A,align_B,primary,q_start,q_end,weight):
self.primary = primary
self.align_A = align_A
self.align_B = align_B
self.q_start = q_start
self.q_end = q_end
self.weight = weight
self.uniq_A = uniqness(align_A)
self.uniq_B = uniqness(align_B)
self.uniq = min(self.uniq_A, self.uniq_B)
# TODO: fix, depending on mate orientation!
if primary.is_reverse:
self.strand = '-'
else:
self.strand = '+'
# segments are ordered by occurence in the reqd query.
# if this order is the same in the genome it is a linear (normal)
# splice site. If reversed, it is a backsplice.
self.dist = align_B.pos - align_A.aend
full_read = primary.seq
self.read_part = full_read[q_start:q_end]
@property
def is_uniq(self):
return self.uniq >= options.min_uniq_qual
@property
def is_backsplice(self):
return self.dist < 0
def find_breakpoints(self):
"""
Extends distal parts of two segment alignments such that the intervening
read sequence is completely aligned and that the endpoints of this extension,
corresponding to the breakpoint inside the read, are flanked by splice signal
dinucleotides.
"""
def mismatches(a,b):
a,b = fromstring(a,dtype=byte), fromstring(b,dtype=byte)
return (a != b).sum()
def simple_match(a,b):
return (a != b)
if options.maxdist == 0:
# faster match function if no mismatches are allowed anyway!
mismatches = simple_match
# short hands
L = len(self.read_part)
A = self.align_A
B = self.align_B
read = self.read_part
margin = options.margin
maxdist = options.maxdist
is_backsplice = self.is_backsplice
# effective anchor size is reduced by allowing the breakpoint to reside
# options.margin nt's inside the anchor
eff_a = options.asize-options.margin
# record all possible breakpoints
hits = []
if options.debug:
print "readlen",L,"q_start/end",self.q_start, self.q_end
print " "*2+read
print " "*2+A.query[:-options.margin]
print " "*(2+L-eff_a)+B.query[options.margin:]
print " "*2+A.seq[:eff_a]
print " "*(2+L-eff_a)+A.seq[-eff_a:]
# intervening sequence that needs to be exactly explained by the splicing
internal = read[eff_a:-eff_a].upper()
# this much needs to be retrieved from the genome
chrom = fast_chrom_lookup(A)
flank = L - 2*eff_a + 2
A_flank = genome.get(chrom,A.pos + eff_a,A.pos + eff_a + flank,'+').upper()
B_flank = genome.get(chrom,B.aend - eff_a - flank,B.aend - eff_a,'+').upper()
l = L - 2*eff_a
# all possible breakpoints
for x in range(l+1):
spliced = A_flank[:x] + B_flank[x+2:]
dist = mismatches(spliced,internal)
if options.debug:
bla = A_flank[:x].lower() + B_flank[x+2:]
if options.debug:
print " "*(eff_a+2)+bla,dist
if dist <= maxdist:
# is the tested breakpoint inside an anchor?
ov = 0
if margin:
if x < margin:
ov = margin-x
if l-x < margin:
ov = margin-(l-x)
gt = A_flank[x:x+2]
ag = B_flank[x:x+2]
gtag = gt+ag
rc_gtag = fast_4mer_RC[gtag]
start,end = B.aend-eff_a-l+x,A.pos+eff_a+x+1
start,end = min(start,end),max(start,end)
strand = "*"
# get strand cues from read if strand-specific sequencing was used.
# TODO: skip if gtag does not match strand!
if options.stranded:
if A.is_reverse:
strand = '-'
else:
strand = '+'
if is_backsplice:
# small correction when projecting back to genome
end -= 1
else:
start -= 1
if options.noncanonical:
hits.append( Splice(self,chrom,start,end,'+',dist,ov,gtag,) )
hits.append( Splice(self,chrom,start,end,'-',dist,ov,rc_gtag) )
else:
if gtag == 'GTAG':
hits.append( Splice(self,chrom,start,end,'+',dist,ov,gtag) )
elif gtag == 'CTAC':
hits.append( Splice(self,chrom,start,end,'-',dist,ov,rc_gtag) )
if options.debug:
print ">find_breakpoints() results:"
for splice in hits:
print splice
if len(hits) < 2:
# unambiguous, return right away
return hits
# Hits are sorted, with low edit distance beating low anchor overlap
hits = sorted(hits,key=lambda x : x.score, reverse = True)
best_score = hits[0].score
ties = [h for h in hits if (h.score == best_score)]
n_hits = len(ties)
for h in hits:
h.n_hits = n_hits
return ties
class MateSegments(object):
def __init__(self,primary):
N['total_mates'] += 1
self.primary = primary
self.full_seq = primary.seq
self.proper_segs = [primary]
self.other_chrom_segs = []
self.other_strand_segs = []
self.seg_flags = {}
@property
def matenum(self):
if self.primary.is_read2:
return 2
else:
# this also catches unpaired reads
return 1
@property
def strand(self):
if self.primary.is_reverse:
return '-'
else:
return '+'