forked from immunogenomics/goshifter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
executable file
·1107 lines (932 loc) · 38.6 KB
/
functions.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
"""
:File: functions.py
:Author: Gosia Trynka <[email protected]>
:Last updated: 2017-06-26
Copyright (C) 2014, 2015, 2016, 2017 Gosia Trynka, Harm-Jan Westra
This file is part of GoShifter package.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
BY USING THE SOFTWARE YOU ACKNOWLEDGE THAT YOU HAVE READ AND UNDERSTAND THE
TERMS OF USE BELOW.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THIS SOFTWARE IS TO BE USED AS A RESEARCH TOOL ONLY. THE SOFTWARE TOOL SHALL
NOT BE USED AS A DIAGNOSTIC DECISION MAKING SYSTEM AND MUST NOT BE USED TO
MAKE A CLINICAL DIAGNOSIS OR REPLACE OR OVERRULE A LICENSED HEALTH CARE
PROFESSIONAL'S JUDGMENT OR CLINICAL DIAGNOSIS. ANY ACTION THE RESEARCHER TAKES
IN RESPONSE TO THE INFORMATION CONTAINED WITHIN IS AT THE RESEARCHER'S
DISCRETION ONLY.
"""
from __future__ import division
from bisect import bisect_left
from subprocess import Popen, PIPE
from chromtree import ChromTree
from bx.intervals.cluster import ClusterTree
from collections import defaultdict
import random
import copy
import sys
import os
import numpy as np
import base64
import gzip
seed=os.urandom(8)
random.seed(seed)
## to use the same seed, have to decode it back
print "\nSeed for random set to: {}\n".format(base64.b64encode(seed))
def features(fin,sep=None):
"""
Calculates basic stats for tested annotations.
"""
sep = sep if sep is not None else "\t" #defaults to tab delimited
all = []
for line in gzip.open(fin, 'r'):
chrom, start, end = line.rstrip().split(sep)[:3]
if chrom == "Chrom": continue
all.append(int(end)-int(start))
medianSize = np.median(all)
minSize = np.min(all)
maxSize = np.max(all)
print "Annotation size: median = {}, min = {}, max = {}".format(medianSize,minSize,maxSize)
return medianSize
def intervalTree(fin):
"""
Reads gzipped bed file as interval tree
"""
i = 0
tree = ChromTree()
for line in gzip.open(fin, 'r'):
i+= 1
chrom, start, end = line.rstrip().split("\t")[:3]
if chrom == "Chrom": continue
tree.insert(chrom, int(start), int(end), (int(start),int(end)))
print "Read {i} lines from {fin}".format(**locals())
return tree
def enrichPermRandBoundryPeakShift_tabixLd(snpInfoChr,tabixDir,r2min,window,expand,peaksTree,minShift,maxShift,nPerm,fOut,nold,flatld):
"""
Calculates the enrichment using random peak shifting,
peaks flip around if after shift they fall outside LD boundries
"""
if nold:
ldInfo = mapToLdInfo(snpInfoChr)
else:
if flatld:
ldInfo = getLdInfoProxyFinderFile(snpInfoChr,flatld,window,r2min)
else:
ldInfo = getLdInfoTabix(snpInfoChr,tabixDir,window,r2min)
snpPeakInfo, ldsnpPeakInfo = permRandBoundryPeakShift(snpInfoChr,peaksTree,ldInfo,expand,minShift,maxShift,nPerm)
overlapPerm(snpPeakInfo,ldsnpPeakInfo,snpInfoChr,fOut,nPerm,ldInfo)
def mapToLdInfo(snpInfoChr):
"""
If LD is not included this function creates a dictionary with index SNPs
as its own linked SNPs.
"""
ldInfo = {}
for chrom in snpInfoChr:
for snp in snpInfoChr[chrom]:
bp = snpInfoChr[chrom][snp]['bp']
ldInfo.setdefault(snp,{})
ldInfo[snp].setdefault(snp,{})
ldInfo[snp][snp]['bp2'] = bp
ldInfo[snp][snp]['r2'] = 1
return ldInfo
def getLdInfoTabix(snpInfoChr,tabixDir,window,r2min):
"""
Calls tabix to retrieve SNPs within window around the queried SNP.
Finds all the SNPs in LD at a given r2.
"""
ldInfo = {}
allSnps = 0
for chrom in snpInfoChr:
allSnps += len(snpInfoChr[chrom])
i=0
for chrom in snpInfoChr:
for snp in snpInfoChr[chrom]:
bp = snpInfoChr[chrom][snp]['bp']
i += 1
if not i % 100 or i == allSnps:
print 'Read LD info for {i} of {allSnps} SNPs'.format(**locals())
snpLdTabix(snp,chrom,bp,tabixDir,window,r2min,ldInfo)
all_ld_snps = 0
nrSnpsWoProxies = 0
for snp in ldInfo:
if len(ldInfo[snp].keys()) == 1:
nrSnpsWoProxies += 1
all_ld_snps += len(ldInfo[snp].keys())
print "Total number of LD SNPs across {} loci is {}. Number of loci without LD SNPs {}."\
.format(allSnps,all_ld_snps, nrSnpsWoProxies)
return ldInfo
def getLdInfoProxyFinderFile(snpInfoChr,tabixDir,window,r2min):
"""
If all proxies have been precalculated with proxyfinder
Use this flat file for loading all proxies (no tabix indexing required)
"""
ldInfo = {}
allSnps = 0
for chrom in snpInfoChr:
allSnps += len(snpInfoChr[chrom])
i=0
# make a SNP list
snpList = list()
# pprint(snpInfoChr)
for chrom in snpInfoChr:
for snp in snpInfoChr[chrom]:
snpList.append(snp)
# add snp as proxy to itself as default
ldInfo.setdefault(snp,{})
ldInfo[snp].setdefault(snp,{})
ldInfo[snp][snp]['bp2'] = snpInfoChr[chrom][snp]
ldInfo[snp][snp]['r2'] = 1
# open flat proxy file
proxyFile = open(tabixDir)
firstLine = True
for line in proxyFile:
if firstLine:
# check whether the file has the proper format..
firstLine = False
else:
# ChromA PosA RsIdA ChromB PosB RsIdB Distance RSquared Dprime
fields = line.rstrip().split()
if len(fields) >= 7:
chr1 = fields[0]
chr2 = fields[3]
if chr1 == chr2:
bp1 = int(fields[1])
snp1 = fields[2]
bp2 = int(fields[4])
snp2 = fields[5]
r2 = float(fields[7])
distance = abs(int(bp1-bp2))
if r2 >= r2min and distance <= window:
if snp1 in snpList:
ldInfo[snp1].setdefault(snp2,{})
ldInfo[snp1][snp2]['bp2'] = bp2
ldInfo[snp1][snp2]['r2'] = r2
elif snp2 in snpList:
ldInfo[snp2].setdefault(snp1,{})
ldInfo[snp2][snp1]['bp2'] = bp1
ldInfo[snp2][snp1]['r2'] = r2
proxyFile.close()
all_ld_snps = 0
nrSnpsWoProxies = 0
for snp in ldInfo:
if len(ldInfo[snp].keys()) == 1:
nrSnpsWoProxies += 1
all_ld_snps += len(ldInfo[snp].keys())
print "Total number of LD SNPs across {} loci is {}. Number of loci without LD SNPs {}."\
.format(allSnps,all_ld_snps, nrSnpsWoProxies)
return ldInfo
def snpLdTabix(snp,chrom,bp,tabixDir,window,r2min,ldInfo):
"""
Retrieve LD info from the tabix file for a single SNP.
"""
file = os.path.join(tabixDir,'{}_GRCh38.EUR.ld.bgz')
tabixFile = file.format(chrom)
ldInfo.setdefault(snp,{})
st = int(bp - window)
if st < 0:
st = 0
en = (bp + window)
query = "tabix {tabixFile} {chrom}:{st}-{en} | awk '$6 >= {r2min} {{print $0}}' | grep -w {snp}".format(**locals())
proc = Popen(query,shell=True,stdout=PIPE)
infile = 0
for line in proc.stdout:
fields = line.rstrip().split()
bp1 = int(fields[1])
snp1 = fields[2]
bp2 = int(fields[3])
snp2 = fields[4]
r2 = float(fields[5])
distance = abs(bp1-bp2)
if distance <= window and snp1 == snp and r2 >= r2min:
ldInfo[snp].setdefault(snp2,{})
ldInfo[snp][snp2]['bp2'] = bp2
ldInfo[snp][snp2]['r2'] = r2
infile = 1
elif distance <= window and snp2 == snp and r2 >= r2min:
ldInfo[snp].setdefault(snp1,{})
ldInfo[snp][snp1]['bp2'] = bp1
ldInfo[snp][snp1]['r2'] = r2
infile = 1
if not infile:
print "{snp} was not found in {tabixFile}. "\
"Including with no LD info.".format(**locals())
ldInfo[snp].setdefault(snp,{})
ldInfo[snp][snp]['bp2'] = bp
ldInfo[snp][snp]['r2'] = 1
def countSnps(snpInfoChr):
allSnps = 0
for chrom in snpInfoChr:
allSnps += len(snpInfoChr[chrom])
return allSnps
def permRandBoundryPeakShift(snpInfoChr,peaksTree,ldInfo,expand,minShift,maxShift,nPerm):
"""
1) Maps SNPs to shifted annotations in n permutations.
2) Takes peaks that are mapping within the intervals by LD defined boundries.
3) Mentains the same number of annotations,
if shifted outside the boundrie the annotation flips around.
4) Returns dict, key: iteration, SNP, value:1/0 if any of the ldSNPs
overlaps peak in any of the tissues in iter n.
5) Returns dictionary with info which linked SNPs overlap annotation
"""
# define LD boundries
bounds = getLdBounds(ldInfo,snpInfoChr)
# map peaks to LD boundries based on interval tree
peaksToSnp = peaks2region(bounds,peaksTree,expand)
i = 0
snpPeakInfo = {}
ldsnpPeakInfo = {}
if minShift == "False" or maxShift == "False":
print "Shifting annotations by random value within LD boundries"
else:
print "Shifting annotations by random value within specified "\
"{} and {} range".format(minShift,maxShift)
for chrom in bounds:
for snp in bounds[chrom]:
i += 1
# if there are no peaks mapping to this SNP, append 0 for all iterations
if not peaksToSnp[snp]:
# LD SNP overlap info
ldsnpPeakInfo.setdefault(snp,{})
for ldsnp in ldInfo[snp]:
ldsnpPeakInfo[snp].setdefault(ldsnp,0)
for n in range(0,nPerm+1):
# general overlap
snpPeakInfo.setdefault(n,{})
snpPeakInfo[n].setdefault(snp,0)
continue
if isinstance(bounds[chrom][snp]['maxbp'], dict):
maxbp = bounds[chrom][snp]['maxbp']['bp'] + expand
else:
maxbp = bounds[chrom][snp]['maxbp'] + expand
minbp = bounds[chrom][snp]['minbp'] - expand
if minShift == "False" or maxShift == "False":
#shift by random value within the size of the region
min_shift = 0
max_shift = maxbp-minbp
else:
#shift by specified value
min_shift = minShift
max_shift = maxShift
for n in range(0,nPerm+1):
hit = 0
snpPeakInfo.setdefault(n,{})
snpPeakInfo[n].setdefault(snp,0)
# for locus overlap
ldsnpPeakInfo.setdefault(snp,{})
if n == 0:
shiftVal = 0
else:
shiftVal = randShift(min_shift,max_shift)
# shift ldsnps
for ldsnp in ldInfo[snp]:
if isinstance(ldInfo[snp][ldsnp]['bp2'] , dict):
ldbp = ldInfo[snp][ldsnp]['bp2']['bp'] + shiftVal
else:
ldbp = ldInfo[snp][ldsnp]['bp2'] + shiftVal
# check if shifted snp maps outside ld boundries
if ldbp > maxbp:
ldbp = ldbp - maxbp + minbp #flip around
elif ldbp < minbp:
ldbp = maxbp - (minbp - ldbp) # flip around
# SNPs overlapping peaks in observed
in_peak = binarySearch(peaksToSnp[snp],ldbp)
if n == 0:
# specific SNP overlap
ldsnpPeakInfo[snp].setdefault(ldsnp,0)
if in_peak:
hit = 1
ldsnpPeakInfo[snp][ldsnp] = 1
# overlap in permutations
else:
if in_peak:
hit = 1
break
snpPeakInfo[n][snp] = hit
return snpPeakInfo, ldsnpPeakInfo
def getLdBounds(ldInfo,mappingsByChr):
"""
For each SNP defines LD boundries based on most upstream and downstream LD SNPs
"""
print "Defining LD boundries"
bounds = {}
for chrom in mappingsByChr:
bounds.setdefault(chrom,{})
for snp in mappingsByChr[chrom]:
positions = []
bounds[chrom].setdefault(snp,{})
for ldsnp in ldInfo[snp]:
ldbp = ldInfo[snp][ldsnp]["bp2"]
positions.append(ldbp)
minbp = min(positions)
maxbp = max(positions)
bounds[chrom][snp]['minbp'] = minbp
bounds[chrom][snp]['maxbp'] = maxbp
return bounds
def peaks2region(bounds,peaksTree,expand):
"""
Assignes peaks to an interval based on bx.python interval tree.
"""
print "Mapping annotations to LD boundries extended by {expand} bp".\
format(**locals())
peaksToSnp = {}
for chrom in bounds:
for snp in bounds[chrom]:
peaksToSnp.setdefault(snp,[])
minbp = bounds[chrom][snp]['minbp'] - expand
if isinstance(bounds[chrom][snp]['maxbp'], dict):
maxbp = bounds[chrom][snp]['maxbp']['bp'] + expand
else:
maxbp = bounds[chrom][snp]['maxbp'] + expand
peaks = peaksTree.find(chrom,minbp,maxbp)
for peak in peaks:
sta = peak[0]
end = peak[1]
# only end of peak in the bounds
if end >=minbp and end <= maxbp and sta < minbp:
peaksToSnp[snp].append((minbp,end))
# only begining of peak in the bounds
elif sta <= maxbp and sta >= minbp and end > maxbp:
peaksToSnp[snp].append((sta,maxbp))
# peak larger than bounds
elif sta <= minbp and end >= maxbp:
peaksToSnp[snp].append((minbp,maxbp))
# the entire peak is in
elif sta >= minbp and end <= maxbp:
peaksToSnp[snp].append(peak)
else:
sys.exit("Annotation: {sta}:{end} in region {minbp}:{maxbp}"\
" doesn't match any condition. Stopping!"\
.format(**locals()))
peaksToSnp[snp].sort()
return peaksToSnp
def randShift(minShift,maxShift=None):
"""
Returns random pos/neg value between 0 and maxShift
"""
#min for peak shift uniform dist defaults to 0 if not specified
minShift = minShift if minShift is not None else 0
x = [-1,1]
shiftVal = random.randrange(minShift,maxShift)*random.choice(x)
return shiftVal
def binarySearch(a, x, shift=None,lo=0,hi=None):
"""
Binary search on the list of tuples.
Shift defaults to zero if not specified.
Return the position of the tuple for which x maps between its two values.
"""
shift = shift if shift is not None else 0 #if shift is not specified default to zero
hi = hi if hi is not None else len(a) # hi defaults to len(a)
x = x + shift #shift look up x if shift specified = peak shifting
pos = bisect_left(a,(x,),lo,hi) # search list of tuples a on the 1st element
if pos != hi and a[pos][0] == x: #position of the exact match
return a[pos]
elif pos-1 != hi and a[pos-1][0] < x and a[pos-1][1] >= x: # will return right index of the nearest match
return a[pos-1]
else:
return False
def overlapStratPerm(snpPeakInfo,snpInfoChr,fOut,nPerm,ldInfo):
"""
For each of n permutations, count how many SNPs overlap peaks.
Write results to an output file.
"""
perm = {}
allSnps = countSnps(snpInfoChr)
for n in snpPeakInfo:
count = 0
## observed scores
if n == 0:
## observed enrichment
for snp in snpPeakInfo[n]:
## count total enrichment
count += snpPeakInfo[n][snp]
## permuted scores
else:
for snp in snpPeakInfo[n]:
## count total enrichment
count += snpPeakInfo[n][snp]
perm[n] = count
f_enrich = fOut+".nperm"+str(nPerm)+'.enrich'
with open(f_enrich,'w') as f:
f.write('nperm\tnSnpOverlap\tallSnps\tenrichment\n')
obs = perm[0]
betterScore = 0
for n in perm:
snpsOverlap = perm[n]
enrich = round(perm[n]/allSnps,5)
f.write('%s\t%s\t%s\t%s\n' % (n,snpsOverlap,allSnps,enrich))
if n > 0:
if snpsOverlap >= obs:
betterScore += 1
# calculate p-value and write to log file
pval = betterScore/nPerm
print "p-value = {}".format(pval)
print 'Detailed enrichment results per permutation written to {}'\
.format(f_enrich)
def overlapPerm(snpPeakInfo,ldsnpPeakInfo,snpInfoChr,fOut,nPerm,ldInfo):
"""
For each of n permutations, count how many SNPs overlap peaks.
Write results to an output file.
"""
perm = {}
allSnps = countSnps(snpInfoChr)
locus_score = {}
for n in snpPeakInfo:
count = 0
## observed scores
if n == 0:
## observed enrichment
for snp in snpPeakInfo[n]:
locus_score.setdefault(snp,{})
locus_score[snp].setdefault('overlap',0)
locus_score[snp].setdefault('locus_score',0)
locus_score[snp]['overlap'] = snpPeakInfo[n][snp]
## count total enrichment
count += snpPeakInfo[n][snp]
## permuted scores
else:
for snp in snpPeakInfo[n]:
locus_score[snp]['locus_score'] += snpPeakInfo[n][snp]
## count total enrichment
count += snpPeakInfo[n][snp]
perm[n] = count
f_enrich = fOut+".nperm"+str(nPerm)+'.enrich'
f_locus_score = fOut+".nperm"+str(nPerm)+'.locusscore'
f_snp_score = fOut+".nperm"+str(nPerm)+'.snpoverlap'
with open(f_enrich,'w') as f:
f.write('nperm\tnSnpOverlap\tallSnps\tenrichment\n')
obs = perm[0]
betterScore = 0
for n in perm:
snpsOverlap = perm[n]
enrich = round(perm[n]/allSnps,5)
f.write('%s\t%s\t%s\t%s\n' % (n,snpsOverlap,allSnps,enrich))
if n > 0:
if snpsOverlap >= obs:
betterScore += 1
with open (f_locus_score, 'w') as g, open(f_snp_score, 'w') as h:
g.write('locus\toverlap\tscore\n')
h.write('locus\tld_snp\toverlap\n')
for snp in locus_score:
## if locus overlaps annotation site
if locus_score[snp]['overlap']:
g.write('%s\t%s\t%s\n'\
% (snp,locus_score[snp]['overlap'],\
locus_score[snp]['locus_score']/nPerm))
for ldsnp in ldsnpPeakInfo[snp]:
h.write('%s\t%s\t%s\n'\
% (snp,ldsnp,ldsnpPeakInfo[snp][ldsnp]))
else:
g.write('%s\t%s\t%s\n'\
% (snp,'N/A','N/A'))
# calculate p-value and write to log file
pval = betterScore/nPerm
print "p-value = {}".format(pval)
print 'Detailed enrichment results per permutation written to {}'\
.format(f_enrich)
print 'Locus scores written to {}'\
.format(f_locus_score)
print 'SNP scores written to {}'\
.format(f_snp_score)
def mergeTree(fin1):
"""
Read bed files and merge overlapping intervals
"""
tree = defaultdict(lambda: ClusterTree(0, 1))
iterFileTree(fin1,tree)
mergeIntervals = 0
for chrom in tree:
mergeIntervals += len(tree[chrom].getregions())
print "There are {mergeIntervals} regions after merging overlaping"\
" intervals".format(**locals())
return tree
def iterFileTree(fin,trees):
n=0
for line in gzip.open(fin, 'r'):
n+=1
chrom, beg, end = line.rstrip().split("\t")[:3]
if chrom == "Chrom": continue
trees[chrom].insert(int(beg), int(end), n)
print "Read {n} lines from {fin}".format(**locals())
def merge2IntervalTree(mergeTree):
convTree = ChromTree()
i = 0
for chrom, trees in mergeTree.iteritems():
for beg, end, ns in mergeTree[chrom].getregions():
convTree.insert(chrom,beg,end,(beg,end))
return convTree
def enrich_shift_conditional_tabixLd(snpInfoChr,tabixDir,r2min,window,expand,a_tree,b_tree,minShift,maxShift,nPerm,fOut,nold,flatld):
"""
Calculates the enrichment of annotation a stratifing on annotation b by random
peak shift, peaks flip around if shift falls outside LD boundrie
"""
if nold:
ldInfo = mapToLdInfo(snpInfoChr)
else:
if flatld:
ldInfo = getLdInfoProxyFinderFile(snpInfoChr,flatld,window,r2min)
else:
ldInfo = getLdInfoTabix(snpInfoChr,tabixDir,window,r2min)
snpPeakInfo = shift_conditional(snpInfoChr,a_tree,b_tree,ldInfo,expand,minShift,maxShift,nPerm)
overlapStratPerm(snpPeakInfo,snpInfoChr,fOut,nPerm,ldInfo)
def shift_conditional(snpInfoChr,a_tree,b_tree,ldInfo,expand,minShift,maxShift,nPerm):
"""
Distinguishes between enrichment being driven by one annotation (a) over
other (b).
Annotations a falling within b are shited within all b annotation merged
into one segment and all a outside are shifted within single segment
unmapped by b.
"""
# define LD boundries
bounds = getLdBounds(ldInfo,snpInfoChr)
# map peaks to LD boundries
a_peaks2Snp = peaks2region(bounds,a_tree,expand)
b_peaks2Snp = peaks2region(bounds,b_tree,expand)
i = 0
# define the results dictionary
snpPeakInfo = {}
count_perm = 0
if minShift == "False" or maxShift == "False":
print "Shifting annotations by random value within LD boundries"
else:
print "Shifting annotations by random value within specified "\
"{} and {} range".format(minShift,maxShift)
# set results to non-overlap up front
for chrom in bounds:
for snp in bounds[chrom]:
for n in range(0,nPerm+1):
snpPeakInfo.setdefault(n,{})
snpPeakInfo[n][snp] = 0
for chrom in bounds:
for snp in bounds[chrom]:
# zero if no annotation a maps to this snp
if not a_peaks2Snp[snp]:
# test another snp
continue
# no background annotation
elif not b_peaks2Snp[snp]:
maxbp = bounds[chrom][snp]['maxbp'] + expand
minbp = bounds[chrom][snp]['minbp'] - expand
if minShift == "False" or maxShift == "False":
#shift by random value within the size of the region
min_shift = 1
max_shift = maxbp-minbp
else:
#shift by specified value
min_shift = minShift
max_shift = maxShift
# test overlap within LD boundries
for n in range(0,nPerm+1):
# observed, no shifting
if n == 0:
shiftVal = 0
else:
# random shift defined by value between specified
# minShift and maxShift
shiftVal = randShift(min_shift,max_shift)
# test SNPs for overlap with a
for ldsnp in ldInfo[snp]:
ldbp = ldInfo[snp][ldsnp]['bp2'] + shiftVal
# check if shifted snp maps outside ld boundries
ldbp = check_in_bounds(ldbp,maxbp,minbp)
# test for peak overlap
in_peak = binarySearch(a_peaks2Snp[snp],ldbp)
if in_peak:
snpPeakInfo[n][snp] = 1
break
else:
maxbp = bounds[chrom][snp]['maxbp'] + expand
minbp = bounds[chrom][snp]['minbp'] - expand
# assign SNPs to B and Genome
snps_in_b,snps_in_g = \
map_snps_to_annot(ldInfo[snp],b_peaks2Snp[snp])
# map a to b and genome
a_in_b, a_in_g = \
find_interval_overlap(a_peaks2Snp[snp],b_peaks2Snp[snp])
# make list of genomic intervals not overlapping with B
g_peaks2Snp = \
non_annotation_intervals(b_peaks2Snp[snp],minbp,maxbp)
# stitch and remap b intervals
remap_b, remap_a_in_b, remap_snps_in_b, remap_b_maxbp = \
segment_remap(b_peaks2Snp[snp],a_in_b,snps_in_b,minbp,maxbp)
remap_b_minbp = 0
# stitch and remap g intervals
remap_g, remap_a_in_g, remap_snps_in_g, remap_g_maxbp = \
segment_remap(g_peaks2Snp,a_in_g,snps_in_g,minbp,maxbp)
remap_g_minbp = 0
# do shifting
for n in range(0,nPerm+1):
# ensure there are A in B and in G for testing, and there\
# are SNPs in B to test
if remap_a_in_b and remap_a_in_g:
test = shift_test_overlap\
(n,remap_b_minbp,remap_b_maxbp,minShift,maxShift,\
remap_snps_in_b,remap_a_in_b)
# next n if overlapped, else test in g
if test:
snpPeakInfo[n][snp] = test
continue
# test for overlap within G
else:
test = shift_test_overlap\
(n,remap_g_minbp,remap_g_maxbp,minShift,maxShift,\
remap_snps_in_g,remap_a_in_g)
snpPeakInfo[n][snp] = test
# check if there are A in B but not G
elif remap_a_in_b and not remap_a_in_g:
test = shift_test_overlap\
(n,remap_b_minbp,remap_b_maxbp,minShift,maxShift,\
remap_snps_in_b,remap_a_in_b)
snpPeakInfo[n][snp] = test
# check if there are A in G but not B
elif remap_a_in_g and not remap_a_in_b:
test = shift_test_overlap\
(n,remap_g_minbp,remap_g_maxbp,minShift,maxShift,\
remap_snps_in_g,remap_a_in_g)
snpPeakInfo[n][snp] = test
else:
sys.exit("I missed something. No A annotations"\
" for SNP {}".fomrat(snp))
return snpPeakInfo
def map_snps_to_annot(ldInfo,annot_list):
"""
Maps SNPs to annotation if they overlap it, otherwise to Genome (fragments
unmapped by annotation B).
Returns two lists with bp of all tested SNPs.
"""
snps_in_b = []
snps_in_g = []
for ldsnp in ldInfo:
ldbp = ldInfo[ldsnp]['bp2']
in_peak = binarySearch(annot_list,ldbp)
if in_peak:
snps_in_b.append(ldbp)
else:
snps_in_g.append(ldbp)
snps_in_b.sort()
snps_in_g.sort()
return snps_in_b, snps_in_g
def find_interval_overlap(a_annot_list,b_annot_list):
"""
Finds annotations A overlapping or mapping outside B.
Returns two lists of tuples.
"""
a_annot_list_cp = copy.deepcopy(a_annot_list)
a_in_b = []
a_in_g = []
for b in b_annot_list:
b_st = b[0]
b_en = b[1]
# this will be modified through iterations
a_annot_list_mod = copy.deepcopy(a_annot_list_cp)
for a in a_annot_list_mod:
a_st = a[0]
a_en = a[1]
# maps outside interval b
if a_st >= b_en:
continue
# entirely maps within interval
elif a_st >= b_st and a_en <= b_en:
a_in_b.append((a_st,a_en))
a_annot_list_cp.remove(a)
# end of a overlaps interval b
elif a_st < b_st and a_en <= b_en and a_en >= b_st:
a_in_b.append((b_st,a_en))
# append the overhang
a_annot_list_cp.remove(a)
a_annot_list_cp.append((a_st,b_st))
# begining of a overlaps interval b
elif a_st >= b_st and a_st <= b_en and a_en > b_en:
a_in_b.append((a_st,b_en))
# append the overhang
a_annot_list_cp.remove(a)
a_annot_list_cp.append((b_en,a_en))
# a extends interval b on both ends
elif a_st < b_st and a_en > b_en:
a_in_b.append((b_st,b_en))
# append the overhang
a_annot_list_cp.remove(a)
a_annot_list_cp.append((a_st,b_st))
a_annot_list_cp.append((b_en,a_en))
for a in a_annot_list_cp:
a_in_g.append(a)
a_in_b.sort()
a_in_g.sort()
return a_in_b, a_in_g
def non_annotation_intervals(annot_list,minbp,maxbp):
"""
Finds genomic regions without annotations and returns them as a list of
tuples.
"""
interval_list = []
all_annot = len(annot_list)
# only one annotation
if all_annot == 1:
annot = annot_list[0]
annot_st = annot[0]
annot_en = annot[1]
# annot spans the whole region
if annot_st == minbp and annot_en == maxbp:
interval_list = []
# end of annot in the region
elif annot_st == minbp and annot_en < maxbp:
interval_list.append((annot_en,maxbp))
# start of annot in the region
elif annot_st > minbp and annot_en == maxbp:
interval_list.append((minbp,annot_en))
# whole annot within the region
elif annot_st > minbp and annot_en < maxbp:
interval_list.append((minbp,annot_st))
interval_list.append((annot_en,maxbp))
# catch other situations
else:
sys.exit('Region limits: {minbp},{maxbp}. Annotation limits:'\
' {annot_st},{annot_en}. Something weird! STOPPING\n'\
.format(**locals()))
# more than one annotation
else:
i = 0
for annot in annot_list:
i += 1
annot_st = annot[0]
annot_en = annot[1]
# if it's the last annotation
if i == 1:
# annotation starts at the region boundry
if annot_st == minbp:
st = annot_en
continue
else:
st = annot_en
interval_list.append((minbp,annot_st))
elif i == all_annot:
# if end is the same as end of the region
if annot_en == maxbp:
interval_list.append((st,annot_st))
break
else:
interval_list.append((st,annot_st))
interval_list.append((annot_en,maxbp))
# annot within region
else:
interval_list.append((st,annot_st))
st = annot_en
interval_list.sort()
return interval_list
def intervals_sum_length(intervals_list):
length = 0
for a in intervals_list:
sta = a[0]
end = a[1]
length += end - sta
return length
def segment_remap(intervals_list,a_in_intervals,snps_in_intervals,minbp,maxbp):
"""
Stiches all intervals into one region.
SNPs and annotation A within intervals are remapped to new
coordinates.
"""
remap_intervals_list = []
remap_a_in_intervals = []
remap_snps_in_intervals = []
remap_maxbp = 0
all_intervals = len(intervals_list)
all_a = len(a_in_intervals)
all_snps = len(snps_in_intervals)
i = 0
for i in range(0,all_intervals):
if i == 0:
sta = intervals_list[0][0]
end = intervals_list[0][1]
delta = sta
remap_sta = sta - delta
remap_end = end - delta
remap_intervals_list.append((remap_sta,remap_end))
# remap a annotations falling into this interval
for a in a_in_intervals:
a_sta = a[0]
a_end = a[1]
# don't iterate if start maps beyon the interval
if a_sta > end:
break
elif a_sta >= sta and a_end <= end:
remap_a_sta = a_sta - delta
remap_a_end = a_end - delta
remap_a_in_intervals.append((remap_a_sta,remap_a_end))
# remap SNPs falling into this interval
for bp in snps_in_intervals:
if bp > end:
break
elif bp >= sta and bp <= end:
remap_bp = bp - delta
remap_snps_in_intervals.append(remap_bp)
if all_intervals == 1:
remap_maxbp = remap_end
else:
sta = intervals_list[i][0]
delta = delta + sta - end
end = intervals_list[i][1]