-
Notifications
You must be signed in to change notification settings - Fork 0
/
variant.py
executable file
·1110 lines (794 loc) · 30.8 KB
/
variant.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
#!/bin/env pypy
"""
Tools for calling short nucleotide variants.
Usage:
variant call <genome_fasta> <bam_files>... [-r REGION] [--ref=N:R]
[--hetz=N:R] [--homz=N:R] [-q N] [-Q SAMPLES] [--keep-all]
variant recall <vcf_file> [--ref=N:R] [--hetz=N:R] [--homz=N:R]
variant somatic <vcf_file> <tumor,normal>...
variant discard if in controls <vcf_file> <control_samples>...
variant discard if in <N> controls <vcf_file> <control_samples>...
variant discard by position <vcf_file> <pos_file>
variant discard shallow <vcf_file> <min_coverage>
variant nonsynonymous <vcf_file>
variant discard 1000g <vcf_file>
variant merge <vcf_files>...
variant annotate <vcf_file> [--genome=G]
variant keep samples <vcf_file> <regex>
variant discard samples <vcf_file> <regex>
variant conservation <vcf_file>
variant plot evidence <vcf_file>
variant statistics <vcf_file>
variant signature <vcf_file> <genome_fasta>
variant top variants <vcf_file>
variant top mutated regions <vcf_file> <region_size>
variant list alt samples <vcf_file>
variant heterozygous bases <vcf_file> <pos_file>
variant heterozygous concordance <vcf_file> <pos_file> <test> <ref>
variant allele fractions <vcf_file> <pos_file>
Options:
-h --help Show this screen
-r <region> Restrict analysis to chromosomal region
-q N Minimum mapping quality score [default: 10]
-Q SAMPLES Samples for which mapping quality is ignored [default: ]
--ref=N:R Minimum evidence for homozygous reference [default: 8:0.9]
--hetz=N:R Minimum evidence for heterozygous [default: 4:0.25]
--homz=N:R Minimum evidence for homozygous alt [default: 4:0.8]
--keep-all Show sites even if they are all homozygous reference
--genome=G Genome version for annotations [default: hg19].
"""
from __future__ import print_function
import sys, subprocess, docopt, re, os, string, math, itertools
from collections import defaultdict
from pypette import zopen, shell, shell_stdout, shell_stdinout
from pypette import info, error, natural_sorted, revcomplement, read_fasta
gt_symbols = ['', '0/0', '0/1', '1/1']
##############
# BENCHMARKS #
##############
#$ time samtools mpileup -B -q20 -f ~/organisms/homo_sapiens/hg19.fa -r chr1:1-5000000 19888.bam 19893.bam > /dev/null
#real 0m24.901s
#user 0m24.406s
#sys 0m0.458s
#$ time samtools mpileup -uB -q20 -f ~/organisms/homo_sapiens/hg19.fa -r chr1:1-5000000 19888.bam 19893.bam | bcftools view -vcg -p 0.1 - > /dev/null
#real 0m57.550s
#user 0m59.659s
#sys 0m0.906s
#$ time bash -c 'samtools mpileup -B -q20 -f ~/organisms/homo_sapiens/hg19.fa -r chr1:1-5000000 19888.bam 19893.bam | ~/tools/pypette/compiled/spileup > /dev/null'
#real 0m24.477s
#user 0m31.647s
#sys 0m0.659s
#$ time snv call ~/organisms/homo_sapiens/hg19.fa 19888.bam 19893.bam > /dev/null
#real 0m38.491s
#user 0m34.363s
#sys 0m0.342s
def simple_pileup(bam_paths, genome_path, min_mapq=10, min_alt_alleles=3,
region=None):
helper_dir = os.path.dirname(os.path.realpath(__file__)) + '/compiled'
options = []
if region:
options.append('%s %s' % ('-l' if region.endswith('.bed') else '-r', region))
# samtools mpileup will automatically ignore alignments flagged as
# duplicates
cmd = 'samtools mpileup -d 1000000 -A -x -R -sB %s -q0 -f %s %s | %s/spileup %d %d' % (' '.join(options), genome_path,
' '.join(bam_paths), helper_dir, min_alt_alleles, min_mapq)
#info('Pre-filtering mutations with the following command:\n%s' % cmd)
return shell_stdout(cmd)
def call_genotypes(alt, total, options):
# 0 = unknown, 1 = ref, 2 = hetz, 3 = homz
gtypes = [0] * len(alt)
for s in range(len(alt)):
if total[s] == 0: continue
if total[s] - alt[s] >= options.min_ref_reads and \
(total[s] - alt[s]) / total[s] >= options.min_ref_ratio:
gtypes[s] = 1
ratio = float(alt[s]) / total[s]
if alt[s] >= options.min_hetz_reads and ratio >=options.min_hetz_ratio:
gtypes[s] = 2
if alt[s] >= options.min_homz_reads and ratio >=options.min_homz_ratio:
gtypes[s] = 3
return gtypes
#ref = (total_reads - reads >= options.min_ref_reads) & \
# ((total_reads - reads) / total_reads >= options.min_ref_ratio)
#hetz = (reads >= options.min_hetz_reads) & \
# (reads / total_reads >= options.min_hetz_ratio)
#homz = (reads >= options.min_homz_reads) & (ratio >= options.min_homz_ratio)
#return ref + 2*hetz + homz
################
# VARIANT CALL #
################
def variant_call(bam_paths, genome_path, options):
if not os.path.exists(genome_path):
error('Could not find genome FASTA file %s.' % genome_path)
if options.region:
for bam_path in bam_paths:
if not os.path.exists(bam_path + '.bai'):
error('No index found for BAM file %s.' % bam_path)
samples = [os.path.basename(p).replace('.bam', '') for p in bam_paths]
print('CHROM\tPOSITION\tREF\tALT\t%s' % '\t'.join(samples))
ignore_mapq = [False] * len(samples)
if options.ignore_mapq:
for s, sample in enumerate(samples):
if re.search(options.ignore_mapq, sample) != None:
ignore_mapq[s] = True
info('Ignoring mapping quality for sample %s.' % sample)
for line in simple_pileup(bam_paths, genome_path,
min_mapq=options.min_mapq,
min_alt_alleles=(0 if options.keep_all else options.min_hetz_reads),
region=options.region):
tokens = line[:-1].split('\t')
if len(tokens) < 3: error('Invalid spileup line:\n%s' % line)
if tokens[2] == 'N': continue
pileups = [p.split(' ') for p in tokens[3:]]
#total_reads = np.zeros(len(samples))
#allele_reads = defaultdict(lambda: np.zeros(len(samples)))
total_reads = [0] * len(samples)
allele_reads = defaultdict(lambda: [0] * len(samples))
for s, pileup in enumerate(pileups):
if len(pileup) < 3: continue
for a in range(0, len(pileup), 3):
count = int(pileup[a+1]) + \
(int(pileup[a+2]) if ignore_mapq[s] else 0)
total_reads[s] += count
if pileup[a] != '.': allele_reads[pileup[a]][s] = count
# Call genotypes for each allele.
for alt, reads in allele_reads.iteritems():
genotypes = call_genotypes(reads, total_reads, options)
if not options.keep_all and all(gt < 2 for gt in genotypes): continue
gtypes = ('%s:%d:%d' % (gt_symbols[g], reads[s], total_reads[s])
for s, g in enumerate(genotypes))
# Reformat indels in VCF4 format
ref = tokens[2]
if len(alt) >= 2:
if alt[1] == '+': # Insertion
alt = (ref if alt[0] == '.' else alt[0]) + alt[2:]
elif alt[1] == '-': # Deletion
ref += alt[2:]
alt = (ref[0] if alt[0] == '.' else alt[0])
print('%s\t%s\t%s\t%s\t%s' % (tokens[0], tokens[1], ref,
alt.upper(), '\t'.join(gtypes)))
##################
# VARIANT RECALL #
##################
def variant_recall(vcf_path, options):
vcf_file = zopen(vcf_path)
for line in vcf_file:
if not line.startswith('#'): break
sys.stdout.write(line)
headers = line.rstrip('\n').split('\t')
sample_col = headers.index('ESP6500' if 'ESP6500' in headers else 'ALT')+1
samples = headers[sample_col:]
for line in vcf_file:
cols = line.rstrip('\n').split('\t')
gt_reads = [gt.split(':')[1:] for gt in cols[sample_col:]]
reads = [int(gt[0]) for gt in gt_reads]
total_reads = [int(gt[1]) for gt in gt_reads]
genotypes = call_genotypes(reads, total_reads, options)
if all(gt < 2 for gt in genotypes): continue
gtypes = ('%s:%d:%d' % (gt_symbols[g], reads[s], total_reads[s])
for s, g in enumerate(genotypes))
print('%s\t%s' % ('\t'.join(cols[:sample_col]), '\t'.join(gtypes)))
####################
# VARIANT ANNOTATE #
####################
def num_lines(path):
lines = 0
for line in open(path): lines += 1
return lines
def variant_annotate(vcf_path, genome='~/tools/annovar-2016-02-01/humandb/hg38'):
format_annovar(vcf_path, 'anno_tmp.vcf')
humandb_dir, genome_version = os.path.split(genome)
shell('table_annovar.pl anno_tmp.vcf %s -buildver %s --remove --otherinfo '
'--outfile annotated -operation g,f,f,f '
'-protocol refGene,cosmic70,1000g2014oct_all,exac03' %
(humandb_dir, genome_version))
anno = open('annotated.%s_multianno.txt' % genome_version)
out = zopen('annotated.vcf.gz', 'w')
anno.next()
line = anno.next()
headers = ['CHROM', 'POSITION', 'REF', 'ALT', 'FUNCTION', 'GENE',
'EXONIC_FUNCTION', 'AA_CHANGE', 'COSMIC', '1000G', 'EXAC']
headers += line.rstrip('\n').split('\t')[20:]
out.write('\t'.join(headers) + '\n')
for line in anno:
c = line.rstrip('\n').split('\t')
out.write('\t'.join(c[0:2] + c[3:7] + c[8:13] + c[20:]))
out.write('\n')
out.close()
os.remove('anno_tmp.vcf')
os.remove('annotated.%s_multianno.txt' % genome_version)
if num_lines('annotated.invalid_input') <= 1:
os.remove('annotated.invalid_input')
if num_lines('annotated.refGene.invalid_input') <= 1:
os.remove('annotated.refGene.invalid_input')
def format_annovar(vcf_path, out_path):
out = open(out_path, 'w')
for line in zopen(vcf_path):
if line.startswith(('CHROM', '#')):
headers = line.rstrip('\n').split('\t')
headers[1] = 'START'
headers.insert(2, 'END')
out.write('\t'.join(headers) + '\n')
continue
cols = line.rstrip('\n').split('\t')
cols.insert(2, cols[1]) # Add end coordinate
ref = cols[3]; alt = cols[4]
if len(ref) == 1 and len(alt) > 1 and ref[0] == alt[0]:
# Simple insertion
ref = '-'
alt = alt[1:]
cols[1] = str(int(cols[1])+1)
cols[2] = cols[1]
elif len(ref) > 1 and len(alt) == 1 and ref[0] == alt[0]:
# Simple deletion
ref = ref[1:]
alt = '-'
cols[1] = str(int(cols[1])+1)
cols[2] = str(int(cols[1]) + len(ref) - 1)
elif len(ref) > 1 or len(alt) > 1:
# Block substitution
cols[2] = str(int(cols[1]) + len(ref) - 1)
cols[3] = ref; cols[4] = alt
out.write('\t'.join(cols))
out.write('\n')
out.close()
##################
# VARIANT FILTER #
##################
def variant_filter(vcf_path, nonsynonymous, no_1000g):
vcf_file = zopen(vcf_path)
for line in vcf_file:
if not line.startswith('#'): break
sys.stdout.write(line)
headers = line[:-1].split('\t')
if nonsynonymous and not 'EXONIC_FUNCTION' in headers:
error('Cannot find exonic function column.')
if no_1000g and not '1000G' in headers:
error('Cannot find 1000 Genomes column.')
sample_col = headers.index('ESP6500' if 'ESP6500' in headers else 'ALT')+1
col_1000g = headers.index('1000G')
col_exonic_func = headers.index('EXONIC_FUNCTION')
for line in vcf_file:
cols = line[:-1].split('\t')
if nonsynonymous:
if not cols[col_exonic_func].startswith(
('nonsynonymous', 'frameshift', 'stopgain', 'stoploss', 'nonframeshift')):
continue
if no_1000g:
if cols[col_1000g]: continue
sys.stdout.write(line)
###################
# VARIANT SOMATIC #
###################
def somatic(vcf_path, sample_pairs):
vcf_file = zopen(vcf_path)
for line in vcf_file:
if not line.startswith('##'): break
headers = line.rstrip().split('\t')
sample_col = headers.index('ESP6500' if 'ESP6500' in headers else 'ALT')+1
samples = headers[sample_col:]
# Convert sample pair names into index 2-tuples.
sample_pairs = [pair.split(',') for pair in sample_pairs]
if not all(len(pair) == 2 for pair in sample_pairs):
info([pair for pair in sample_pairs if len(pair) != 2])
error('Test and control samples must be in "test,control" format.')
for pair in sample_pairs:
if not pair[0] in samples:
error('Test sample %s was not found in VCF file.' % pair[0])
if not pair[1] in samples:
error('Control sample %s was not found in VCF file.' % pair[1])
sample_pairs = [(samples.index(pair[0]), samples.index(pair[1]))
for pair in sample_pairs]
sys.stdout.write(line)
for line in vcf_file:
cols = line.rstrip('\n').split('\t')
gt_cols = cols[sample_col:]
genotypes = [gt_symbols.index(g[:g.find(':')]) for g in gt_cols]
somatic = [genotypes[pair[0]] >= 2 and genotypes[pair[1]] == 1
for pair in sample_pairs]
if not any(somatic): continue
sys.stdout.write(line)
##################################
# VARIANT DISCARD IF IN CONTROLS #
##################################
def discard_if_in_controls(vcf_path, control_samples, threshold):
vcf_file = zopen(vcf_path)
for line in vcf_file:
if not line.startswith('##'): break
headers = line.rstrip().split('\t')
sample_col = headers.index('ESP6500' if 'ESP6500' in headers else 'ALT')+1
control = [any(re.search(rx, s) for rx in control_samples)
for s in headers[sample_col:]]
if not any(control): error('No control samples found.')
info('Using these %d control samples:' % sum(control))
for s, c in zip(headers[sample_col:], control):
if c: info('- %s' % s)
sys.stdout.write(line)
for line in vcf_file:
cols = line.rstrip('\n').split('\t')[sample_col:]
genotypes = [gt_symbols.index(c[:c.find(':')]) for c in cols]
if sum(c and gt > 1 for c, gt in zip(control, genotypes)) >= threshold:
continue
sys.stdout.write(line)
###################
# DISCARD SHALLOW #
###################
def discard_shallow(vcf_path, min_coverage):
vcf_file = zopen(vcf_path)
for line in vcf_file:
if not line.startswith('##'): break
headers = line.rstrip().split('\t')
sample_col = headers.index('ESP6500' if 'ESP6500' in headers else 'ALT')+1
sys.stdout.write(line)
for line in vcf_file:
cols = line.rstrip().split('\t')[sample_col:]
total_reads = sum(int(c.split(':')[2]) for c in cols)
if float(total_reads) / len(cols) < min_coverage: continue
sys.stdout.write(line)
###############################
# VARIANT DISCARD BY POSITION #
###############################
def variant_discard_by_position(vcf_path, pos_path):
info('Reading list of blacklisted positions...')
pos_file = zopen(pos_path)
blacklist = []
for line in pos_file:
cols = line.rstrip().split('\t')
if len(cols) < 2: continue
chr = cols[0][3:] if cols[0].startswith('chr') else cols[0]
blacklist.append(chr + ':' + cols[1])
blacklist = set(blacklist)
vcf_file = zopen(vcf_path)
for line in vcf_file:
if not line.startswith('##'): break
headers = line.rstrip().split('\t')
sample_col = headers.index('ESP6500' if 'ESP6500' in headers else 'ALT')+1
sys.stdout.write(line)
for line in vcf_file:
cols = line.rstrip().split('\t')
chr = cols[0][3:] if cols[0].startswith('chr') else cols[0]
if not chr + ':' + cols[1] in blacklist:
sys.stdout.write(line)
#################
# VARIANT MERGE #
#################
def variant_merge(vcf_paths):
sort_in, sort_out = shell_stdinout('sort -k2,2 -k3,3n -k4,4 -k5,5')
cons_headers = [] # Consensus headers
vcf_samples = [] # Sample names of each VCF
for vcf_index, vcf_path in enumerate(vcf_paths):
info('Merging VCF file %s...' % vcf_path)
vcf = zopen(vcf_path)
for line in vcf:
if not line.startswith('#'): break
headers = line.rstrip('\n').split('\t')
gtype_col = (4 if not 'ESP6500' in headers else
headers.index('ESP6500') + 1)
if not cons_headers: cons_headers = headers[:gtype_col]
if cons_headers != headers[:gtype_col]: error('Header mismatch!')
vcf_samples.append(headers[gtype_col:])
for line in vcf:
sort_in.write('%d\t%s' % (vcf_index, line))
sort_in.close()
print('\t'.join(cons_headers + sum(vcf_samples, [])))
vcf_sample_counts = [len(samples) for samples in vcf_samples]
S = sum(vcf_sample_counts)
vcf_sample_col = [sum(vcf_sample_counts[0:k])
for k in range(len(vcf_samples))]
info('Merged VCF will contain:')
info('- %d header columns' % len(cons_headers))
for samples, path in zip(vcf_samples, vcf_paths):
info('- %d columns from %s' % (len(samples), path))
prev = None
calls = [':0:0'] * S
for line in sort_out:
cols = line.rstrip('\n').split('\t')
vcf_index = int(cols[0])
call_col = vcf_sample_col[vcf_index]
if prev != cols[1:5]:
if prev != None:
print('\t'.join(prev + calls))
prev = cols[1:gtype_col+1]
calls = [':0:0'] * S
calls[call_col:call_col+vcf_sample_counts[vcf_index]] = \
cols[gtype_col+1:]
print('\t'.join(prev + calls)) # Handle the last line
########################
# VARIANT CONSERVATION #
########################
def variant_conservation(vcf_path):
vcf_file = zopen(vcf_path)
for line in vcf_file:
if not line.startswith('##'): break
headers = line.rstrip().split('\t')
sample_col = headers.index('ALT') + 1
samples = headers[sample_col:]
chr = None
Sd2 = math.ceil(len(samples) / 2.0)
for line in vcf_file:
cols = line.rstrip().split('\t')
if cols[0] != chr:
chr = cols[0]
print('variableStep chrom=%s' % chr)
genotypes = [gt_symbols.index(gt[:gt.find(':')])
for gt in cols[sample_col:]]
if any(genotypes == 0): continue
is_alt = (genotypes >= 2)
conserved = max(sum(is_alt), sum(1 - is_alt))
conserved = (conserved - Sd2) / (len(samples) - Sd2) # -> [0,1]
print('%s\t%.2f' % (cols[1], conserved))
#########################
# VARIANT PLOT EVIDENCE #
#########################
def variant_plot_evidence(vcf_path):
vcf_file = zopen(vcf_path)
for line in vcf_file:
if not line.startswith('#'): break
headers = line[:-1].split('\t')
sample_col = headers.index('ALT') + 1
samples = headers[sample_col:]
#pipe = shell_stdin('gnuplot -persist')
#pipe.write('set terminal svg\n')
#pipe.write('set output "out.svg"\n')
#pipe.write('plot "-" with points\n')
max_total = 100
ratio_bin = 0.05
bins = list(np.arange(0, 1, ratio_bin)) + [1]
hist = np.zeros((max_total+1, len(bins)))
N = 0
for line in vcf_file:
cols = line[:-1].split('\t')
gt_cols = [gt.split(':') for gt in cols[sample_col:]]
genotypes = [(gt_symbols.index(gt[0]), int(gt[1]), int(gt[2]))
for gt in gt_cols]
for gt in genotypes:
ratio = float(gt[1]) / gt[2]
total = gt[2]
if total > max_total: continue
bin = int(ratio / ratio_bin)
hist[total, bin] += 1
#if N > 10000: break
print('TOTAL\t%s' % '\t'.join([str(c) for c in bins]))
for total in range(hist.shape[0]):
print('%d\t%s' % (total, '\t'.join(
[str(c) for c in list(hist[total, :])])))
#pipe.close()
######################
# VARIANT STATISTICS #
######################
def variant_statistics(vcf_path):
vcf_file = zopen(vcf_path)
for line in vcf_file:
if not line.startswith('#'): break
headers = line.rstrip('\n').split('\t')
sample_col = headers.index('ESP6500' if 'ESP6500' in headers else 'ALT')+1
samples = headers[sample_col:]
nearby_gene_col = headers.index('NEARBY_GENES') \
if 'NEARBY_GENES' in headers else None
mutations_per_sample = np.zeros(len(samples))
mutations_per_chr = defaultdict(lambda: np.zeros(len(samples)))
mutations_per_gene = defaultdict(lambda: np.zeros(len(samples)))
for line in vcf_file:
cols = line[:-1].split('\t')
gtypes = [gt.split(':')[0] for gt in cols[sample_col:]]
gtypes = np.array([gt_symbols.index(gt) for gt in gtypes])
mutations_per_sample += (gtypes > 1)
mutations_per_chr[cols[0]] += (gtypes > 1)
if nearby_gene_col:
for nearby in cols[nearby_gene_col].split(','):
mutations_per_gene[nearby] += (gtypes > 1)
print('Sample mutation counts:')
for s, sample_name in enumerate(samples):
print('%s: %d' % (sample_name, mutations_per_sample[s]))
print('Mutations per chromosome:')
chrs = natural_sorted(mutations_per_chr.keys())
print('SAMPLE\t%s' % '\t'.join(chrs))
for s, sample_name in enumerate(samples):
total = sum(mutations_per_chr[chr][s] for chr in chrs)
if total == 0: continue
sys.stdout.write(sample_name)
for chr in chrs:
sys.stdout.write('\t%d (%.1f)' % (mutations_per_chr[chr][s],
float(mutations_per_chr[chr][s]) / total * 100))
sys.stdout.write('\n')
print('Top mutated genes:')
top_genes = sorted(mutations_per_gene.iteritems(),
key=lambda x: sum(x[1] > 0), reverse=True)
for top in top_genes[0:100]:
mut_samples = sum(top[1] > 0)
if mut_samples < 2: continue
print('%s\t%d samples' % (top[0], mut_samples))
#####################
# VARIANT SIGNATURE #
#####################
def variant_signature(vcf_path, genome_path):
vcf_file = zopen(vcf_path)
for line in vcf_file:
if not line.startswith('#'): break
chromosomes = read_fasta(genome_path)
headers = line.rstrip().split('\t')
sample_col = headers.index('ESP6500' if 'ESP6500' in headers else 'ALT')+1
samples = headers[sample_col:]
substitutions = []
for ref in 'CT':
for alt in ('AGT' if ref == 'C' else 'ACG'):
for pre in 'ACGT':
for post in 'ACGT':
substitutions.append(pre+ref+post+'>'+pre+alt+post)
sub_count = np.zeros((len(substitutions), len(samples)))
for line in vcf_file:
cols = line[:-1].split('\t')
if not cols[2] in 'ACGT' or not cols[3] in 'ACGT': continue
chr = chromosomes[cols[0]]; pos = int(cols[1])
if chr[pos-1] != cols[2]: error('Reference mismatch!')
ref = chr[pos-2:pos+1]
alt = ref[0] + cols[3] + ref[2]
if ref[1] in 'AG':
ref = revcomplement(ref)
alt = revcomplement(alt)
for s, gt in enumerate(cols[sample_col:]):
if gt_symbols.index(gt.split(':')[0]) > 1:
sub_count[substitutions.index(ref + '>' + alt), s] += 1
print('SUBSTITUTION\t%s' % '\t'.join(samples))
for sub in substitutions:
sys.stdout.write(sub)
for count in sub_count[substitutions.index(sub), :]:
sys.stdout.write('\t%d' % count)
sys.stdout.write('\n')
################
# TOP VARIANTS #
################
def top_variants(vcf_path):
vcf_file = zopen(vcf_path)
for line in vcf_file:
sys.stdout.write(line)
if not line.startswith('#'): break
headers = line.rstrip().split('\t')
sample_col = headers.index('ESP6500' if 'ESP6500' in headers else 'ALT')+1
samples = headers[sample_col:]
variants = []
for line in vcf_file:
cols = line[:-1].split('\t')
gtypes = [gt.split(':')[0] for gt in cols[sample_col:]]
variants.append((line, sum(gt_symbols.index(gt) > 1 for gt in gtypes)))
variants = sorted(variants, key=lambda x: int(x[1]), reverse=True)
for var in variants: sys.stdout.write(var[0])
############################
# VARIANT LIST ALT SAMPLES #
############################
def list_alt_samples(vcf_path):
vcf_file = zopen(vcf_path)
for line in vcf_file:
sys.stdout.write(line)
if not line.startswith('#'): break
headers = line.rstrip().split('\t')
sample_col = headers.index('ESP6500' if 'ESP6500' in headers else 'ALT')+1
samples = headers[sample_col:]
for line in vcf_file:
cols = line.rstrip('\n').split('\t')
gtypes = [gt.split(':')[0] for gt in cols[sample_col:]]
sys.stdout.write('\t'.join(cols[:sample_col]))
for s, gt in enumerate(gtypes):
if gt_symbols.index(gt) > 1: sys.stdout.write('\t%s' % samples[s])
print()
###############################
# VARIANT TOP MUTATED REGIONS #
###############################
def variant_top_mutated_regions(vcf_path, region_size):
if region_size % 2: error('Region size must be divisible by two.')
step = region_size / 2
vcf_file = zopen(vcf_path)
for line in vcf_file:
if not line.startswith('#'): break
headers = line.rstrip('\n').split('\t')
sample_col = headers.index('ESP6500' if 'ESP6500' in headers else 'ALT')+1
samples = headers[sample_col:]
# Construct chromosome map
chr_sizes = defaultdict(int)
for line in vcf_file:
cols = line.rstrip('\n').split('\t')
chr_sizes[cols[0]] = max(chr_sizes[cols[0]], int(cols[1]))
vcf_file.close()
mutated = {} # Which samples are mutated in each bin
variant_pos = {} # Position of variant in bin, -1 if various
for chr in chr_sizes:
mutated[chr] = np.zeros((chr_sizes[chr] / step + 1, len(samples)),
dtype=np.bool)
variant_pos[chr] = np.zeros(chr_sizes[chr] / step + 1, dtype=np.int32)
# Reopen VCF file (might be compressed), identify columns
vcf_file = zopen(vcf_path)
for line in vcf_file:
if not line.startswith('#'): break
# Tally mutated samples in each region
print('Tallying mutated samples...')
for line in vcf_file:
cols = line.rstrip('\n').split('\t')
pos = int(cols[1])
bin = (pos - 1) / step
vpos = variant_pos[cols[0]]
vpos[bin] = -1 if vpos[bin] > 0 and vpos[bin] != pos else pos
if bin > 0:
vpos[bin-1] = -1 if vpos[bin-1] > 0 and vpos[bin-1] != pos else pos
mut = mutated[cols[0]]
for s, gt in enumerate(cols[sample_col:]):
if gt_symbols.index(gt.split(':')[0]) <= 1: continue
mut[bin, s] = True
if bin > 0: mut[bin-1, s] = True
# Convert mutation bitmasks into counts
print('Convert to counts...')
for chr in mutated:
mutated[chr] = mutated[chr].sum(axis=1)
# Print regions in descending order starting with highest recurrence
print('Find maximum...')
highest = 0
for chr in mutated:
highest = max(highest, max(mutated[chr]))
print('Top regions with two or more mutated sites:')
for n in range(highest, 1, -1):
for chr in mutated:
mut = mutated[chr]
vpos = variant_pos[chr]
for bin in range(len(mut)):
if mut[bin] != n or vpos[bin] != -1: continue
print('%s:%d-%d\t%d samples' % (
chr, bin*step+1, bin*step+region_size, n))
########################
# VARIANT KEEP SAMPLES #
########################
def variant_keep_samples(vcf_path, regex, discard=False):
vcf_file = zopen(vcf_path)
for line in vcf_file:
if not line.startswith('#'): break
headers = line[:-1].split('\t')
sample_col = headers.index('ESP6500' if 'ESP6500' in headers else 'ALT')+1
keep_col = [c < sample_col or (re.search(regex, sample) != None) != discard
for c, sample in enumerate(headers)]
print('\t'.join(c for c, keep in zip(headers, keep_col) if keep))
for line in vcf_file:
cols = line[:-1].split('\t')
print('\t'.join(c for c, keep in zip(cols, keep_col) if keep))
##############################
# VARIANT HETEROZYGOUS BASES #
##############################
def variant_heterozygous_bases(vcf_path, kgenomes_path):
is_snp = bytearray(int(300e6))
for line in zopen(kgenomes_path):
pos = int(line[:-1].split('\t')[1])
is_snp[pos] = True
vcf_file = zopen(vcf_path)
for line in vcf_file:
if not line.startswith('#'): break
headers = line[:-1].split('\t')
sample_col = headers.index('ESP6500' if 'ESP6500' in headers else 'ALT')+1
print('\t'.join(headers[0:4]))
for line in vcf_file:
cols = line[:-1].split('\t')
if not is_snp[int(cols[1])]: continue
gt_cols = cols[sample_col:]
genotypes = [gt_symbols.index(gt[:gt.find(':')]) for gt in gt_cols]
total_reads = [float(gt.split(':')[2]) for gt in gt_cols]
if not any(g == 2 and r >= 15 for g, r in zip(genotypes, total_reads)):
continue
print('\t'.join(cols[0:4]))
############################
# VARIANT ALLELE FRACTIONS #
############################
def variant_allele_fractions(vcf_path, pos_path):
snps = set()
for line in zopen(pos_path):
pos = ':'.join(line[:-1].split('\t')[0:2])
if not pos.startswith('chr'): pos = 'chr'+pos
snps.add(pos)
vcf_file = zopen(vcf_path)
for line in vcf_file:
if not line.startswith('#'): break
headers = line[:-1].split('\t')
sample_col = headers.index('ESP6500' if 'ESP6500' in headers else 'ALT')+1
sys.stdout.write(line)
for line in vcf_file:
cols = line[:-1].split('\t')
if not ':'.join(cols[0:2]) in snps: continue
reads = [gt.split(':')[1:3] for gt in cols[sample_col:]]
sys.stdout.write('\t'.join(cols[:sample_col]))
for r in reads:
alt, total = float(r[0]), int(r[1])
sys.stdout.write('\tNaN' if total == 0 else '\t%.2f' % (alt / total))
sys.stdout.write('\n')
#frac = [f if f <= 0.5 else 1.0 - f for f in frac]
####################################
# VARIANT HETEROZYGOUS CONCORDANCE #
####################################
def variant_heterozygous_concordance(vcf_path, kgenomes_path, test_rx, ref_rx):
is_snp = np.zeros(300*1000*1000, np.bool_)
for line in zopen(kgenomes_path):
pos = int(line[:-1].split('\t')[1])
is_snp[pos] = True
vcf_file = zopen(vcf_path)
for line in vcf_file:
if not line.startswith('#'): break