-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert2annovar.pl
executable file
·3244 lines (2777 loc) · 166 KB
/
convert2annovar.pl
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 perl
use warnings;
use strict;
use Getopt::Long;
use Pod::Usage;
use File::Basename;
our $REVISION = '$Revision: f98de7f0a9145baca0dd81fa66f8e3db0603abf9 $';
our $DATE = '$Date: 2019-10-24 00:05:27 -0400 (Thu, 24 Oct 2019) $';
our $AUTHOR = '$Author: Kai Wang <[email protected]> $';
our ($verbose, $help, $man);
our ($variantfile);
our ($outfile, $format, $includeinfo, $snpqual, $snppvalue, $coverage, $maxcoverage, $chr, $chrmt, $altcov, $allelicfrac, $fraction, $species,
$filterword, $confraction, $allallele, $withzyg, $comment, $allsample, $genoqual, $varqual, $dbsnpfile, $withfreq, $withfilter, $seqdir, $inssize, $delsize, $subsize, $genefile, $splicing_threshold, $context, $avsnpfile, $keepindelref);
our %iupac = (R=>'AG', Y=>'CT', S=>'CG', W=>'AT', K=>'GT', M=>'AC', A=>'AA', C=>'CC', G=>'GG', T=>'TT', B=>'CGT', D=>'AGT', H=>'ACT', V=>'ACG', N=>'ACGT', '.'=>'-', '-'=>'-'); ### <<< FOR 5500SOLiD LifeScope ( S=>'GC' is replaced by S=>'CG')
our %iupacrev = reverse %iupac; ### <<< FOR 5500SOLiD LifeScope
GetOptions('verbose|v'=>\$verbose, 'help|h'=>\$help, 'man|m'=>\$man, 'outfile=s'=>\$outfile, 'format=s'=>\$format, 'includeinfo'=>\$includeinfo,
'snpqual=f'=>\$snpqual, 'snppvalue=f'=>\$snppvalue, 'coverage=i'=>\$coverage, 'maxcoverage=i'=>\$maxcoverage, 'chr=s'=>\$chr, 'chrmt=s'=>\$chrmt,
'fraction=f'=>\$fraction, 'altcov=i'=>\$altcov, 'allelicfrac'=>\$allelicfrac,
'species'=>\$species, 'filter=s'=>\$filterword, 'confraction=f'=>\$confraction, 'allallele!'=>\$allallele, 'withzyg'=>\$withzyg,
'comment'=>\$comment, 'allsample'=>\$allsample, 'genoqual=f'=>\$genoqual, 'varqual=f'=>\$varqual, 'dbsnpfile=s'=>\$dbsnpfile, 'withfreq'=>\$withfreq,
'withfilter'=>\$withfilter, 'seqdir=s'=>\$seqdir, 'inssize=i'=>\$inssize, 'delsize=i'=>\$delsize, 'subsize=i'=>\$subsize, 'genefile=s'=>\$genefile,
'splicing_threshold=i'=>\$splicing_threshold, 'context'=>\$context, 'avsnpfile=s'=>\$avsnpfile, 'keepindelref'=>\$keepindelref) or pod2usage ();
$help and pod2usage (-verbose=>1, -exitval=>1, -output=>\*STDOUT);
$man and pod2usage (-verbose=>2, -exitval=>1, -output=>\*STDOUT);
@ARGV or pod2usage (-verbose=>0, -exitval=>1, -output=>\*STDOUT);
@ARGV == 1 or pod2usage ("Syntax error");
($variantfile) = @ARGV;
$chrmt ||= 'M';
#prepare PATH environmental variable
my $path = File::Basename::dirname ($0);
$path and $ENV{PATH} = "$path:$ENV{PATH}"; #set up the system executable path to include the path where this program is located in
if (not $format) {
$format = 'pileup';
print STDERR "NOTICE: the default --format argument is set as 'pileup'\n";
}
$format eq 'vcf' and $format = 'vcf4';
if ($allsample) {
defined $withfreq or defined $outfile or pod2usage ("Error in argument: please specify --outfile when --allsample is specified (unless -withfreq is set)");
$format eq 'vcf4' or pod2usage ("Error in argument: the --allsample argument is supported only if --format is 'vcf4'");
defined $withfreq or print STDERR "NOTICE: output files will be written to $outfile.<samplename>.avinput\n";
} else {
if (defined $outfile) {
open (STDOUT, ">$outfile") or die "Error: cannot write to output file $outfile: $!\n";
}
}
defined $snpqual and $format eq 'pileup' || $format eq 'vcf4old' || pod2usage ("Error in argument: the --snpqual is supported only for the 'pileup' or 'vcf4old' format");
defined $snppvalue and $format eq 'gff3-solid' || pod2usage ("Error in argument: the --snppvalue is supported only for the 'gff3-solid' format");
if (not defined $snpqual and $format eq 'pileup') {
$snpqual = 20;
print STDERR "NOTICE: the default --snpqual argument for pileup format is set as 20\n";
}
if (not defined $snppvalue) {
$snppvalue = 1; #by default, do not use any of the P-value cutoff in filtering out GFF3-SOLID files (this is differnt from handling pileup files)
}
if (not defined $coverage) {
$coverage = 0;
}
if (defined $fraction) {
$format eq 'pileup' or $format eq 'vcf4' or pod2usage ("Error in argument: the '--fraction' argument is supported for the pileup or vcf4 format only");
$format eq 'vcf4old' and print STDERR "NOTICE: the --fraction argument works ONLY on indels for vcf4old format\n";
$fraction >= 0 and $fraction <=1 or pod2suage ("Error in argument: the --fraction argument must be between 0 and 1 inclusive");
} else {
$fraction = 0;
}
if (defined $withfilter) {
$format eq 'vcf4' or $format eq 'vcf4old' or pod2usage ("Error in argument: the '-withfilter' argument is supported for the vcf4 or vcf4old format only");
}
if (defined $confraction) {
$format eq 'vcf4old' and print STDERR "NOTICE: the --confraction argument works ONLY on indels for vcf4old format\n";
$confraction >= 0 and $fraction <=1 or pod2suage ("Error in argument: the --confraction argument must be between 0 and 1 inclusive");
} else {
$confraction = 0;
}
if (defined $altcov) {
$format eq 'pileup' or pod2usage ("Error in argument: the '--altcov' argument is supported for the '--format pileup' only");
$altcov < $coverage or pod2suage ("Error in argument: the --altcov argument must be less than --coverage");
$altcov > 0 or pod2suage ("Error in argument: the --altcov argument must be a positive integer");
}
if (defined $species) {
$format eq 'gff3-solid' or pod2usage ("Error in argument: the '--species' argument is only necessary for the '--format gff3-solid'");
}
if ($allallele) {
$format eq 'vcf4old' or pod2usage ("Error in argument: the '--allallele' argument is only supported for the '--format vcf4old'");
}
if ($withfreq and $withzyg) {
pod2usage ("Error in argument: -withfreq and -withzyg are mutually exclusive");
}
if ($format eq 'pileup') {
convertPileup ($variantfile);
} elsif ($format eq 'cg') {
convertCG ($variantfile);
} elsif ($format eq 'cgmastervar') {
convertCGMasterVar ($variantfile);
} elsif ($format eq 'gff3-solid') {
convertGFF3SolidSNP ($variantfile);
} elsif ($format eq 'soap') {
print STDERR "WARNING: the support for '--format soap' is not well developed yet and may contain bugs for indel analysis.\n";
convertSOAP ($variantfile);
} elsif ($format eq 'maq') {
print STDERR "WARNING: the support for '--format maq' is not well developed yet and may contain bugs.\n";
convertMAQSNP ($variantfile);
} elsif ($format eq 'casava') {
convertCASAVA ($variantfile, $chr);
} elsif ($format eq 'vcf4old') {
convertVCF4Old ($variantfile);
} elsif ($format eq 'vcf4') {
convertVCF4 ($variantfile);
} elsif ($format eq 'annovar') {
convertANNOVAR ($variantfile);
} elsif ($format eq 'annovar2vcf') {
convertANNOVAR2VCF ($variantfile);
} elsif ($format eq 'bed') {
convertBED ($variantfile);
} elsif ($format eq 'rsid') {
defined $dbsnpfile or defined $avsnpfile or pod2usage ("Error in argument: please specify --dbsnpfile or -avsnpfile when the --format is 'rsid'");
if (defined $dbsnpfile) {
convertRsid ($variantfile);
} else {
convertAvsnpid ($variantfile);
}
} elsif ($format eq 'region') {
defined $subsize or $subsize = 1;
$variantfile =~ m/(chr)?(\w+):(\d+)-(\d+)$/ or pod2usage "Error in argument: for '-format region', the region should be specified in 'chr:start-end' format";
$seqdir or pod2usage "Error in argument: please specify -seqdir for the '-format region'\n";
convertRegion ($variantfile);
} elsif ($format eq 'transcript') {
defined $subsize or $subsize = 1;
defined $genefile or pod2usage ("Error in argument; please specify -genefile for the '-format transcript'");
$seqdir or pod2usage "Error in argument: please specify -seqdir for the '-format transcript'\n";
$splicing_threshold ||= 2;
convertTranscript ($variantfile);
} else {
pod2usage ("Error in argument: the --format $format is not currently supported. Please contact ANNOVAR developer for adding the support");
}
sub convertTranscript {
my ($transcript) = @_;
my ($foundmatch) = (0);
my (@allregion);
open (GENE, $genefile) or die "Error: cannot read from gene file $genefile: $!\n";
while (<GENE>) {
s/[\r\n]+$//; #deleting the newline characters
my @record = split (/\t/, $_);
my ($name, $chr, $dbstrand, $txstart, $txend, $cdsstart, $cdsend, $exoncount, $exonstart, $exonend, $id, $name2, $cdsstartstat, $cdsendstat, $exonframes);
@record >= 11 or die "Error: invalid record in genefile (>=11 fields expected): <$_>\n";
if (@record == 12) { #knownGene
($name, $chr, $dbstrand, $txstart, $txend, $cdsstart, $cdsend, $exoncount, $exonstart, $exonend) = @record[0..9];
} else { #other gene definition
($name, $chr, $dbstrand, $txstart, $txend, $cdsstart, $cdsend, $exoncount, $exonstart, $exonend, $id, $name2, $cdsstartstat, $cdsendstat, $exonframes) = @record[1..15];
}
if ($transcript eq $name) {
$foundmatch++;
my @exonstart = split (/,/, $exonstart);
my @exonend = split (/,/, $exonend);
map {$_++} @exonstart;
for my $i (0 .. @exonstart-1) {
push @allregion, "$chr:" . ($exonstart[$i]-$splicing_threshold) . '-' . ($exonend[$i]+$splicing_threshold);
}
}
}
print STDERR "NOTICE: ${\(scalar @allregion)} regions will be analyzed for possible mutations\n";
$verbose and print STDERR "NOTICE: These regions will be analyzed: @allregion\n";
$foundmatch or print STDERR "WARNING: the specified transcript $transcript is not found in genefile $genefile\n";
$foundmatch > 1 and print STDERR "WARNING: the specified transcript $transcript occurs $foundmatch times in genefile $genefile\n";
for my $nextregion (@allregion) {
convertRegion ($nextregion);
}
}
sub convertRegion {
my ($region) = @_;
$region =~ m/(chr)?(\w+):(\d+)-(\d+)$/;
my ($chr, $start, $end) = ($2, $3, $4);
$end >= $start or die "Error: end position must be equal or larger than start position in region specification '$region'\n";
my $sc = "echo $region | retrieve_seq_from_fasta.pl -format simple -tabout -seqdir $seqdir -outfile stdout stdin";
my $result = qx/$sc/;
$result =~ m/\S+\t(\S+)/ or die "Error: unable to retrieve user-specified region $region from seqdir $seqdir\n";
my $seq = $1;
if (not defined $subsize or $subsize) { #default value is 1
my @allnt = generateAllNT ($subsize);
for my $i (0 .. length($seq)-$subsize) {
for my $nt (@allnt) {
substr($seq, $i, $subsize) eq $nt and next;
substr($seq, $i, $subsize) =~ m/^N+$/ and next; #if it is only composed of N, skip this position
print STDOUT join ("\t", $chr, $start+$i, $start+$i+$subsize-1, substr($seq, $i, $subsize), $nt), "\n";
}
}
}
if ($delsize) {
for my $i (0 .. length($seq)-$delsize) {
print STDOUT join ("\t", $chr, $start+$i, $start+$i, substr($seq, $i, $delsize), '-'), "\n";
}
}
if ($inssize) {
my @allnt = generateAllNT ($inssize);
for my $i (0 .. length($seq)-1) {
for my $nt (@allnt) {
print STDOUT join ("\t", $chr, $start+$i, $start+$i, '-', $nt), "\n";
}
}
}
}
sub generateAllNT {
my ($size) = @_;
my @oldstr = (''); #initialize as four empty string
my @newstr;
for my $i (1 .. $size) {
@newstr = ();
for my $nt (qw/A C G T/) {
for my $str (@oldstr) {
push @newstr, $str.$nt;
}
}
@oldstr = @newstr;
}
return (@newstr);
}
sub convertPileup {
my ($variantfile) = @_;
my ($countline, $countvar, $counthom, $counthet, $countindel, $countsnp, $countti, $counttv) = qw/0 0 0 0 0 0 0 0/;
if ($variantfile eq 'stdin') {
*VAR = *STDIN;
} elsif ($variantfile =~ m/\.gz$/) {
open (VAR, "gunzip -c $variantfile |") or die "Error: cannot read from STDIN uncompressing variant file $variantfile: $!\n";
} else {
open (VAR, $variantfile) or die "Error: cannot read from variant file $variantfile: $!\n";
}
print STDERR "NOTICE: Column 6-9 in output are heterozygosity status, SNP quality, total reads, reads with mutation\n";
while (<VAR>) {
s/[\r\n]+$//;
$countline++;
my $hom = 'hom';
my @field = split (/\t/, $_);
@field >= 10 or die "Error: invalid record found in pileupfile $variantfile (at least 10 fields expected): <$_>\n";
my ($chr, $pos, $wt, $call, @other) = @field;
my ($cons_qual, $snp_quality, $readcount, $readallele) = @field[4,5,7,8];
$chr =~ s/^chr//;
$wt = uc $wt; #wt may or may not in upper case, it depends on the input FASTA file
$call = uc $call; #usually call is in upper case
$readallele = uc $readallele; #lower case indicate the opposite strand
$includeinfo or @other = (); #unless -includeinfo is set, the other will not be printed
$snp_quality >= $snpqual or next; #quality of the variant call
$readcount >= $coverage or next; #read count of the variant call
$maxcoverage and $readcount <= $maxcoverage || next; #maximum coverage of the variant call
if ($wt eq '*') { #indel
#example:
#1 970271 * +C/+C 39 106 44 5 +C * 1 4 0 0 0
#1 1548977 * */+CCG 29 29 42 3 * +CCG 2 1 0 0 0
#1 1674810 * */+C 24 24 42 6 * +C 5 1 0 0 0
#1 968466 * -CT/-CT 53 339 55 5 -CT * 5 0 0 0 0
#1 1093600 * -GAAA/* 29 29 53 3 -GAAA * 1 2 0 0 0
#1 1110101 * */-A 41 41 17 6 * -A 5 1 0 0 0
#1 1215395 * */-TC 26 26 32 4 * -TC 3 1 0 0 0
my @obs = split (/\//, $call); #example: '+AG/+AG' as homozygotes, '*/-TA' or '*/+T' as heterozygotes
@obs == 2 or die "Error: pileup record contains more than two alternative alleles: <$_>\n";
my ($end, $ref, $alt);
my ($indelreadcount); #number of reads supporting the indel
if ($obs[0] eq $obs[1]) {
#something weird may occur in SamTools output: 22 15231121 * */* 360 0 32 158 * +GA 156 2 0 0 0
$obs[0] eq '*' and next;
#for deletions, SAMTOOLS represent deletion using a location before the first deleted base in the reference sequence coordinate system
#for example, a deletion in Samtools output is "1 109266688 * */-CTT 1429 1429 58 43 * -CTT 24 19 0 0 0"
#the correct ANNOVAR input (for rs35029887) should be "1 109266689 109266691 CTT - het 1429"
#insertions are fine without change; for example, check rs5745466 in Genome Browser; samtools report "1 76119508 * +AT/+AT"
#for this insertion, ANNOVAR input file (for rs5745466) becomes "1 76119508 76119508 - AT hom 1601"
if ($obs[0] =~ m/^\-/) {
$pos++; #add 1 to position in deletion
}
$indelreadcount = calculateIndelReadCount ($obs[0], \@field);
$indelreadcount/$readcount >= $fraction or next; #do not meet minimum alternative allele fraction threshold
defined $altcov and $indelreadcount >= $altcov || next;
if ($chr eq $chrmt or $allelicfrac) {
$hom = sprintf ("%.3f", $indelreadcount/$readcount);
}
($end, $ref, $alt) = recalculateEndRefObs ($pos, $wt, $obs[0]);
print STDOUT join ("\t", $chr, $pos, $end, $ref, $alt, $hom, $snp_quality, $readcount, $indelreadcount, @other), "\n";
$counthom++;
} else {
$hom = 'het';
if ($obs[0] =~ m/^[\-\+]/) {
$obs[0] =~ m/^\-/ and $pos++;
($end, $ref, $alt) = recalculateEndRefObs ($pos, $wt, $obs[0]);
$indelreadcount = calculateIndelReadCount ($obs[0], \@field);
$indelreadcount/$readcount >= $fraction or next; #do not meet minimum alternative allele fraction threshold
defined $altcov and $indelreadcount >= $altcov || next;
if ($chr eq $chrmt or $allelicfrac) {
$hom = sprintf ("%.3f", $indelreadcount/$readcount);
}
print STDOUT join ("\t", $chr, $pos, $end, $ref, $alt, $hom, $snp_quality, $readcount, $indelreadcount, @other), "\n";
$counthet++;
}
if ($obs[1] =~ m/^[\-\+]/) {
$obs[1] =~ m/^\-/ and $pos++;
($end, $ref, $alt) = recalculateEndRefObs ($pos, $wt, $obs[1]);
$indelreadcount = calculateIndelReadCount ($obs[1], \@field);
$indelreadcount/$readcount >= $fraction or next; #do not meet minimum alternative allele fraction threshold
defined $altcov and $indelreadcount >= $altcov || next;
if ($chr eq $chrmt or $allelicfrac) {
$hom = sprintf ("%.3f", $indelreadcount/$readcount);
}
print STDOUT join ("\t", $chr, $pos, $end, $ref, $alt, $hom, $snp_quality, $readcount, $indelreadcount, @other), "\n";
$counthet++;
}
}
$countindel++;
} else {
#1 798494 G A 36 36 58 3 AAA bbb
#1 798677 T K 33 33 52 26 ,$.,,G.GG,.,......,..G,,... b`bbBaJIbFbZWaTNQbb_VZcbbb
#1 856182 G A 42 42 50 5 AAAAA B\bbb
#1 861034 A M 48 48 49 14 ,$,.,..,cc.c.,c bbBbb`]BFbHbBB
#1 864289 T K 22 22 56 6 .g,,g, BH^_BB
$wt eq $call and next; #this is not a SNP
my $obs = $iupac{$call} or die "Error: invalid best call ($call) in <$_>\n";
my @obs = split (//, $obs);
@obs == 2 or die "Error: observed IUPAC allele $call should correspond to two nucleotide alleles: <$_>\n";
if ($obs[0] ne $obs[1]) {
$hom = 'het';
}
if ($obs[0] eq $wt) { #obs[0] is guaranteed to be an alternative allele
@obs = @obs[1,0];
}
if ($wt eq 'A' and $obs[0] eq 'G' or $wt eq 'G' and $obs[0] eq 'A' or $wt eq 'C' and $obs[0] eq 'T' or $wt eq 'T' and $obs[0] eq 'C') {
unless ($wt ne $obs[0] and $wt ne $obs[1] and $obs[0] ne $obs[1]) {
$countti++;
}
} else {
unless ($wt ne $obs[0] and $wt ne $obs[1] and $obs[0] ne $obs[1]) {
$counttv++;
}
}
my $mutallelecount;
if ($obs[1] eq $wt) { #het SNP
if ($chr eq $chrmt or $allelicfrac) {
$hom = calculateAllelicFraction ($obs[0], $field[8], $readcount);
}
$mutallelecount = calculateMutAlleleCount ($obs[0], $readallele);
$mutallelecount/$readcount >= $fraction or next; #do not meet minimum alternative allele fraction threshold
defined $altcov and $mutallelecount >= $altcov || next;
print STDOUT join ("\t", $chr, $pos, $pos, $wt, $obs[0], $hom, $snp_quality, $readcount, $mutallelecount, @other), "\n";
$counthet++;
} elsif ($obs[1] ne $obs[0]) { #het SNP but both differ from reference allele
if ($chr eq $chrmt or $allelicfrac) {
$hom = calculateAllelicFraction ($obs[1], $field[8], $readcount);
}
$mutallelecount = calculateMutAlleleCount ($obs[1], $readallele);
$mutallelecount/$readcount >= $fraction or next; #do not meet minimum alternative allele fraction threshold
defined $altcov and $mutallelecount >= $altcov || next;
print STDOUT join ("\t", $chr, $pos, $pos, $wt, $obs[1], $hom, $snp_quality, $readcount, $mutallelecount, @other), "\n";
if ($chr eq $chrmt) {
$hom = calculateAllelicFraction ($obs[0], $field[8], $readcount);
}
$mutallelecount = calculateMutAlleleCount ($obs[0], $readallele);
$mutallelecount/$readcount >= $fraction or next; #do not meet minimum alternative allele fraction threshold
defined $altcov and $mutallelecount >= $altcov || next;
print STDOUT join ("\t", $chr, $pos, $pos, $wt, $obs[0], $hom, $snp_quality, $readcount, $mutallelecount, @other), "\n";
$counthet++;
$counthet++;
} else { #homo SNP
if ($chr eq $chrmt or $allelicfrac) {
$hom = calculateAllelicFraction ($obs[0], $field[8], $readcount);
}
$mutallelecount = calculateMutAlleleCount ($obs[0], $readallele);
$mutallelecount/$readcount >= $fraction or next; #do not meet minimum alternative allele fraction threshold
defined $altcov and $mutallelecount >= $altcov || next;
print STDOUT join ("\t", $chr, $pos, $pos, $wt, $obs[0], $hom, $snp_quality, $readcount, $mutallelecount, @other), "\n";
$counthom++;
}
$countsnp++;
}
$countvar++;
}
my $triallelic = $countsnp-$countti-$counttv;
print STDERR "NOTICE: Read $countline lines and wrote ${\($counthet+$counthom)} different variants at $countvar genomic positions ($countsnp SNPs and $countindel indels)\n";
print STDERR "NOTICE: Among ${\($counthet+$counthom)} different variants at $countvar positions, $counthet are heterozygotes, $counthom are homozygotes\n";
print STDERR "NOTICE: Among $countsnp SNPs, $countti are transitions, $counttv are transversions", $triallelic?", $triallelic have more than 2 alleles\n":"\n";
}
sub calculateIndelReadCount {
my ($obs, $field) = @_;
#make sure to use upper case in the comparison, for example:
#chr10 130133 * */-ca 189 189 59 31 * -ca 27 4 0 0 0
if ($obs eq uc $field->[8]) {
return $field->[10];
} elsif ($obs eq uc $field->[9]) {
return $field->[11];
} else {
die "Error: invalid record in pileup file (indel counts cannot be inferred): <$obs> vs <@$field>\n";
}
}
sub calculateMutAlleleCount {
my ($allele, $string) = @_; #they should be already both in upper case
$string =~ s/\^.//g; #^ is followed by mapping quality
$string =~ s/\$//g;
$string =~ s/[+-]1[^\d]//g; #1 followed by a non-number
$string =~ s/[+-]2..//g;
$string =~ s/[+-]3...//g;
$string =~ s/[+-]4....//g;
$string =~ s/[+-]5.....//g;
$string =~ s/[+-]6......//g;
$string =~ s/[+-]7.......//g;
$string =~ s/[+-]8........//g;
$string =~ s/[+-]9.........//g;
$string =~ s/[+-]10..........//g;
#make sure to use upper case letter
my @string = split (//, uc $string);
my $count = 0;
for my $i (0 .. @string-1) {
$allele eq $string[$i] and $count++;
}
return $count;
}
sub calculateAllelicFraction {
my ($obs, $readbase, $readcount) = @_;
my @readbase = split (//, $readbase);
my $count=0;
for my $i (0 .. @readbase-1) {
uc $obs eq uc $readbase[$i] and $count++;
}
my $hom = $count/$readcount;
length ($hom) > 5 and $hom > 0.001 and $hom = sprintf ("%.3f", $hom);
return $hom;
}
sub recalculateEndRefObs { #recalculate end position, reference allele and observed allele
my ($end, $ref, $obs) = @_;
if ($obs =~ m/^\-(\w+)/) { #deletion
$end += (length ($1)-1);
$ref = $1;
$obs = '-';
} elsif ($obs =~ m/^\+(\w+)/) { #insertion
$ref = '-';
$obs = $1;
} else {
die "Error: cannot handle $end, $ref, $obs\n";
}
return ($end, $ref, $obs);
}
sub convertBED {
my ($variantfile) = @_;
my ($foundheader, $countline, $countvar) = qw/0 0 0/;
my ($prechr, $prestart, $preend, $prevartype, $preref, $preobs, $prescore, $prexref) = qw/0 0 0 0 0 0 0 0/;
if ($variantfile eq 'stdin') {
*VAR = *STDIN;
} elsif ($variantfile =~ m/\.gz$/) {
open (VAR, "gunzip -c $variantfile |") or die "Error: cannot read from STDIN uncompressing variant file $variantfile: $!\n";
} else {
open (VAR, $variantfile) or die "Error: cannot read from variant file $variantfile: $!\n";
}
print STDERR "NOTICE: Converting variants from $variantfile\n";
while (<VAR>) {
s/[\r\n]+$//;
$countline++;
m/^#/ and next; #comment lines
if (m/^track/) {
$foundheader++;
next;
}
if (not $foundheader) {
$countline > 10 and die "Error: invalid BED file format for $variantfile (track record is not found within the first 10 lines)\n";
}
my ($chrom, $start, $end, @otherinfo) = split (/\t/, $_);
$chrom =~ s/^chr//;
print join ("\t", $chrom, $start+1, $end, 0, 0, @otherinfo), "\n";
$countvar++;
}
print STDERR "NOTICE: Done with $countline lines and $countvar variants\n";
}
sub convertCG {
my ($variantfile) = @_;
my ($foundheader, $countline, @field);
my ($prechr, $prestart, $preend, $prevartype, $preref, $preobs, $prescore, $prexref) = qw/0 0 0 0 0 0 0 0/;
if ($variantfile eq 'stdin') {
*VAR = *STDIN;
} elsif ($variantfile =~ m/\.gz$/) {
open (VAR, "gunzip -c $variantfile |") or die "Error: cannot read from STDIN uncompressing variant file $variantfile: $!\n";
} else {
open (VAR, $variantfile) or die "Error: cannot read from variant file $variantfile: $!\n";
}
print STDERR "NOTICE: Converting variants from $variantfile\n";
while (<VAR>) {
s/[\r\n]+$//;
$countline++;
if (m/^>locus/) {
$foundheader++;
}
if (not $foundheader) {
$countline > 50 and die "Error: invalid CG-var file format for $variantfile (>locus record is not found within the first 50 lines)\n";
next;
}
my ($locus, $ploidy, $haplo, $chr, $start, $end, $vartype, $ref, $obs, $score, $haplolink, $xref) = split (/\t/, $_);
$chr =~ s/^chr//;
$vartype eq 'ins' or $start++; #CG use zero-start, half-open coordinate. Insertion does not need to be processed (example, "16476 2 2 chr1 751820 751820 ins T 49 dbsnp:rs59038458")
$obs eq '' and $obs = '-';
$ref eq '' and $ref = '-';
if ($vartype =~ m/^snp|ins|del|delins|sub$/) { #in new versions of the files, "sub" is used instead of "delins".
#$chr eq 'M' and next; #ignore chrM markers as they are not diploid
if ($chr eq $prechr and $start eq $prestart and $end eq $preend and $obs eq $preobs) { #homozygous mutation
print $chr, "\t", $start, "\t", $end, "\t", $ref, "\t", $obs, "\t", $vartype, "\t", ($score+$prescore)/2, "\t", "hom\t", $xref, "\n";
($prechr, $prestart, $preend, $prevartype, $preref, $preobs, $prescore, $prexref) = qw/0 0 0 0 0 0 0 0/;
} else {
if ($prestart and $preend) {
print $prechr, "\t", $prestart, "\t", $preend, "\t", $preref, "\t", $preobs, "\t", $prevartype, "\t", $prescore, "\thet\t", $prexref, "\n";
}
($prechr, $prestart, $preend, $prevartype, $preref, $preobs, $prescore, $prexref) = ($chr, $start, $end, $vartype, $ref, $obs, $score, $xref);
}
}
}
if ($prestart and $preend) {
print $prechr, "\t", $prestart, "\t", $preend, "\t", $preref, "\t", $preobs, "\t", $prevartype, "\t", $prescore, "\thet\t", $prexref, "\n";
}
print STDERR "NOTICE: Done with $countline lines\n";
}
sub convertCGMasterVar {
#this subroutine converts CG masterVar format into ANNOVAR input format
#example input file is below:
#SEGDUP_GENERATED_AT 2010-Dec-01 13:40
#SOFTWARE_VERSION 2.0.2.26
#TYPE VAR-OLPL
#>locus ploidy chromosome begin end zygosity varType reference allele1Seq allele2Seq allele1VarScoreVAF allele2VarScoreVAF allele1VarScoreEAF allele2VarScoreEAF allele1VarQuality allele2VarQuality allele1HapLink allele2HapLink allele1XRef allele2XRef evidenceIntervalId allele1ReadCount allele2ReadCount referenceAlleleReadCount totalReadCount allele1Gene allele2Gene pfam miRBaseId repeatMasker segDupOverlap relativeCoverageDiploid calledPloidy relativeCoverageNondiploid calledLevel
#1 2 chr1 0 10000 no-call no-ref = ? ?
#2 2 chr1 10000 11038 no-call complex = ? ? 1.13 N 1.02 1.006
#3 2 chr1 11038 11055 hom ref = = = 1.13 N 1.02 1.006
#4 2 chr1 11055 11082 no-call complex = ? ? 1.13 N 1.02 1.006
#5 2 chr1 11082 11109 hom ref = = = 1.13 N 1.02 1.006
my ($variantfile) = @_;
my ($foundheader, $countline);
if ($variantfile eq 'stdin') {
*VAR = *STDIN;
} elsif ($variantfile =~ m/\.gz$/) {
open (VAR, "gunzip -c $variantfile |") or die "Error: cannot read from STDIN uncompressing variant file $variantfile: $!\n";
} else {
open (VAR, $variantfile) or die "Error: cannot read from variant file $variantfile: $!\n";
}
print STDERR "NOTICE: Converting variants from $variantfile\n";
while (<VAR>) {
s/[\r\n]+$//;
$countline++;
if (m/^>locus/) {
$foundheader++;
}
if (not $foundheader) {
$countline > 50 and die "Error: invalid CG-var file format for $variantfile (>locus record is not found within the first 50 lines)\n";
next;
}
my ($locus, $ploidy, $chr, $start, $end, $zygosity, $vartype, $ref, $obs1, $obs2, @otherinfo) = split (/\t/, $_, -1);
my ($a1scorevaf, $a2scorevaf, $a1scoreeaf, $a2scoreeaf, $a1varqual, $a2varqual, $a1haplink, $a2haplink, $a1xref, $a2xref, $interval, $a1read, $a2read, $refread, $totalread) = @otherinfo;
$chr =~ s/^chr//;
$vartype eq 'ins' or $start++; #CG use zero-start, half-open coordinate. Insertion does not need to be processed (example, "16476 2 2 chr1 751820 751820 ins T 49 dbsnp:rs59038458")
$obs1 eq '' and $obs1 = '-';
$obs2 eq '' and $obs2 = '-';
$ref eq '' and $ref = '-';
#zygosity explanation:
##no-call: All alleles are partially or fully no-called.
##hap: Haploid, fully called locus.
##half: Diploid locus where one of the alleles is fully called and the other contains no-calls.
##hom: Diploid, homozygous, fully called locus.
##het-ref: Diploid, heterozygous, fully called locus where one of the alleles is identical to the reference.
##het-alt: Diploid, heterozygous, fully called locus where both alleles differ from the reference.
#vartype explanation:
##snp, ins, del, or sub: Fully called or half-called locus that contains only a single isolated variation.
##ref: Fully called or half-called locus that contains only reference calls and no calls and at least one allele is fully called.
##complex: Locus that contains multiple variations or has no-calls in all alleles. This is also the value for all loci where the reference itself is ambiguous.
##no-ref: Locus where the reference genome is N.
##PAR-called-in-X: Locus on the pseudo-autosomal region of the Y chromosomes in males.
$zygosity eq 'no-call' and next; #ignore locus without calls
if ($vartype =~ m/^snp|ins|del|delins|sub$/) { #in new versions of the files, "sub" is used instead of "delins".
if ($coverage) {
$totalread >= $coverage or next;
}
if ($maxcoverage) {
$totalread <= $maxcoverage or next;
}
if ($zygosity eq 'hom' or $zygosity eq 'hap') {
print $chr, "\t", $start, "\t", $end, "\t", $ref, "\t", $obs1, "\t", 'hom', "\t", $vartype, "\t", $totalread, "\t", $includeinfo?join("\t", "\t", @otherinfo):'', "\n";
} else {
print $chr, "\t", $start, "\t", $end, "\t", $ref, "\t", $obs1, "\t", 'het', "\t", $vartype, "\t", $totalread, "\t", $includeinfo?join("\t", "\t", @otherinfo):'', "\n";
}
}
}
print STDERR "NOTICE: Done with $countline lines\n";
}
sub convertGFF3SolidSNP {
my ($variantfile) = @_;
my ($countline, $countvar, $countallvar, @other) = (0, 0, 0);
my ($unknown_count); #count of variants with 'unknown' variation type
if ($variantfile eq 'stdin') {
*VAR = *STDIN;
} elsif ($variantfile =~ m/\.gz$/) {
open (VAR, "gunzip -c $variantfile |") or die "Error: cannot read from STDIN uncompressing variant file $variantfile: $!\n";
} else {
open (VAR, $variantfile) or die "Error: cannot read from variant file $variantfile: $!\n";
}
$_ = <VAR>;
s/[\r\n]+$//;
m/^##gff-version\s+3/ or die "Error: invalid first line in GFF3 file ('##gff-version 3' expected): <$_>\n";
$_ = <VAR>;
s/[\r\n]+$//;
(m/^##solid-gff-version/ || m/^##source-version/) or print STDERR "WARNING: problematic second line in GFF3-SOLiD file ('##solid-gff-version' or '##source-version' expected): <$_>\n"; ### <<< FOR 5500SOLiD LifeScope
print STDERR "NOTICE: Column 6-9 in output are heterozygosity status, variant score (P-value), total clipped normal coverage reads, total reads with mutated allele\n";
while (<VAR>) {
s/[\r\n]+$//;
$countline++;
if ($comment) {
m/^#/ and print and next; #keep comment in output file
} else {
m/^##/ and next; #header of comment lines
m/^#/ and next; #header of results lines
}
my @field = split (/\t/, $_);
@field == 9 or die "Error: invalid record found in $variantfile (10 fields expected): <$_>\n";
my ($chr, $program, $type, $pos, $end, $score, $attribute) = @field[0,1,2,3,4,5,8]; #score is the P-value for the SNP calls
$chr eq 'chr_name' and next; #header line
if ($score ne '.') {
$score >=0 and $score <=1 or die "Error: invalid score record found in file (0-1 range expected): <$_>\n";
$score <= $snppvalue or next;
}
if ($species and $species eq 'human') {
$chr eq '23' and $chr = 'X';
$chr eq '24' and $chr = 'Y';
$chr eq '25' and $chr = 'M';
}
$includeinfo and @other = ($attribute); #unless -includeinfo is set, the other will not be printed
my ($readcount, $mutallelecount) = ('.', '.'); #total coverage, coverage for mutated alleles
if ($type eq 'unknown') {
#SOLiD GDD3 may have unknown variation types
#chr1 AB_SOLiD Small Indel Tool unknown 3833062 3833153 1 . . ID=5483;len=no_call;allele-call-pos=3833062;allele-call=/CCAC;allele-pos=3833057;alleles=atccatccacccatc/aTCCATCCACCCACCCATC/NO_CALL;allele-counts=REF,2,2;tight_chrom_pos=none;loose_chrom_pos=3833058-3833069;no_nonred_reads=3;coverage_ratio=8.0000;experimental-zygosity=HEMIZYGOUS;experimental-zygosity-score=1.0000;run_names=L1_1_50_10_r,L1_1_50_15_r,L1_1_50_15_r,L1_1_50_12_r;bead_ids=1018_196_970,699_1263_465,220_513_923,2022_1532_1071;overall_qvs=4,6,2,50;no_mismatches=5,4,2,0;read_pos=27,29,31,13;from_end_pos=23,21,19,37;strands=+,+,+,+;tags=R3,F3,F3,F3;indel_sizes=-92,-112,4,4;non_indel_no_mismatches=0,0,8,0;unmatched-lengths=50,50,50,50;ave-unmatched=50.0000;anchor-match-lengths=48,49,49,49;ave-anchor-length=48.7500;read_seqs=G23223321322112233223100132013201320110011001322332,T33223321322112233223100132013201320110013021322332,T33223321322112233223100132013201320110011001322332,T31001320132013201100110013223322113030332233113032;base_qvs=;non_indel_seqs=T21322332211221121322332230321212121223322332233221,G12020202202020012001200213022002130012332310122030,G12020202202020012001000210022012110312331331122030,G22111012101031010100002002321020002202121121313021;non_indel_qvs=
$unknown_count++;
next; #do not count this one!
}
if ($program eq 'SOLiD_diBayes' or $program eq 'AB_SOLiD SNP caller') { #SNP variants
#detailed description can be found at http://solidsoftwaretools.com/gf/download/docmanfileversion/208/866/DiBayes_Documentation_v1.2.pdf
#chr1 SOLiD_diBayes SNP 559817 559817 0.094413 . . genotype=Y;reference=T;coverage=9;refAlleleCounts=5;refAlleleStarts=4;refAlleleMeanQV=23;novelAlleleCounts=2;novelAlleleStarts=2;novelAlleleMeanQV=14;diColor1=11;diColor2=33;het=1;flag=
#chr1 SOLiD_diBayes SNP 714068 714068 0.000000 . . genotype=M;reference=C;coverage=13;refAlleleCounts=7;refAlleleStarts=6;refAlleleMeanQV=25;novelAlleleCounts=6;novelAlleleStarts=4;novelAlleleMeanQV=22;diColor1=00;diColor2=11;het=1;flag=
#chr1 SOLiD_diBayes SNP 714835 714835 0.041579 . . genotype=R;reference=A;coverage=5;refAlleleCounts=3;refAlleleStarts=3;refAlleleMeanQV=18;novelAlleleCounts=2;novelAlleleStarts=2;novelAlleleMeanQV=20;diColor1=02;diColor2=20;het=1;flag=
$pos == $end or die "Error: invalid record found in GFF3-SOLiD file: start and end discordant: <$_>\n";
my ($wt, $call);
my ($hit); ### <<< FOR 5500SOLiD LifeScope
if ($attribute =~ m/ref_base=(\w)/) {
$wt = $1;
} elsif ($attribute =~ m/reference=(\w)/) {
$wt = $1;
} else {
die "Error: invalid record found in GFF3-SOLiD file (ref_base/reference was not found): <$_>\n";
}
if ($attribute =~ m/consen_base=(\w)/) {
$call = $1;
} elsif ($attribute =~ m/genotype=(\w)/) {
$call = $1;
} elsif ($attribute =~ m/allele-call=([\w\/]+)/) { ### <<< FOR 5500SOLiD LifeScope
$hit = $1;
if ($hit =~ m/\//) {
$call = $iupacrev{join("",sort(split(/\//,$hit)))};
} else {
$call = $hit;
}
} else {
die "Error: invalid record found in GFF3-SOLiD file (consen_base was not found): <$_>\n";
}
if ($attribute =~ m/coverage=(\d+)/) {
$readcount = $1;
$readcount >= $coverage or next; #read count of the variant call
$maxcoverage and $readcount <= $maxcoverage || next;
}
if ($attribute =~ m/novelAlleleCounts=(\d+)/) {
$mutallelecount = $1;
$mutallelecount/$readcount >= $fraction or next; #do not meet minimum alternative allele fraction threshold
defined $altcov and $mutallelecount >= $altcov || next;
}
my $obs = $iupac{$call} or die "Error: invalid best call in <$_>\n";
my @obs = split (//, $obs);
@obs == 2 or die "Error: observed IUPAC allele $call should correspond to two nucleotide alleles: <$_>\n";
if ($obs[0] eq $wt and $obs[1] eq $wt) {
die "Error: reference alleles are identical to observed alleles: <$_>\n";
} elsif ($obs[0] eq $wt) {
print $chr, "\t", $pos, "\t", $pos, "\t", $wt, "\t", $obs[1], "\t", "het\t", "$score\t$readcount\t$mutallelecount\t", join ("\t", @other), "\n";
} elsif ($obs[1] eq $wt) {
print $chr, "\t", $pos, "\t", $pos, "\t", $wt, "\t", $obs[0], "\t", "het\t", "$score\t$readcount\t$mutallelecount\t", join ("\t", @other), "\n";
} elsif ($obs[1] ne $obs[0]) {
print $chr, "\t", $pos, "\t", $pos, "\t", $wt, "\t", $obs[0], "\t", "het\t", "$score\t$readcount\t$mutallelecount\t", join ("\t", @other), "\n";
print $chr, "\t", $pos, "\t", $pos, "\t", $wt, "\t", $obs[1], "\t", "het\t", "$score\t$readcount\t$mutallelecount\t", join ("\t", @other), "\n";
$countallvar++;
} else {
print $chr, "\t", $pos, "\t", $pos, "\t", $wt, "\t", $obs[0], "\t", "hom\t", "$score\t$readcount\t$mutallelecount\t", join ("\t", @other), "\n";
}
} elsif ($program eq 'AB_CNV_PIPELINE') { #CNV
if ($attribute =~ m/copynum=(\d+)/ or $attribute =~ m/copynumber=(\d+)/) {
if ($1 < 2) {
print $chr, "\t", $pos, "\t", $end, "\t", 0, "\t", '-', "\t", "unk\t", "$score\t.\t.\t", join ("\t", @other), "\n";
} elsif ($1 > 2) {
print $chr, "\t", $end, "\t", $end, "\t", '-', "\t", 0, "\t", "unk\t", "$score\t.\t.\t", join ("\t", @other), "\n";
}
} else {
print $chr, "\t", $end, "\t", $end, "\t", '-', "\t", 0, "\t", "unk\t", "$score\t.\t.\t", join ("\t", @other), "\n";
}
} elsif ($program eq 'AB_SOLiD Large Indel Tool') { #CNV
#http://solidsoftwaretools.com/gf/download/docmanfileversion/182/780/Large_Indel_Documentation_v1.0.0.pdf
## [FIELDS] (1) chromosome (2) version (3) indel type (4) breakpoint start (5) breakpoint end (6) p-value (7) NA (8) NA (9) attributes
#chr10 AB_SOLiD Large Indel Tool insertion 151910 151969 2.77548e-11 . . dev=-71;avgDev=-1.63884;zygosity=HOMOZYGOUS;nRef=0;nDev=14;refDev=0;devDev=-1.60924;refVar=0;devVar=0.0159438;beadIds=1750_720_1641,649_1680_794,503_1756_608,1726_174_1362,1208_1772_353,872_594_1604,1924_990_858,1899_961_1848,901_1226_378,323_1750_1017,1185_180_1237,1519_490_1074,1291_94_324,285_758_922,1135_95_1594,1055_218_1279,
#chr10 AB_SOLiD Large Indel Tool insertion 154109 154729 2.1559e-11 . . dev=-66;avgDev=-1.51253;zygosity=HOMOZYGOUS;nRef=0;nDev=15;refDev=0;devDev=-1.02864;refVar=0;devVar=0.133236;beadIds=1728_1671_1739,1250_231_25,811_783_1090,1035_908_491,649_1680_794,503_1756_608,1726_174_1362,1208_1772_353,872_594_1604,1924_990_858,1899_961_1848,901_1226_378,323_1750_1017,1185_180_1237,1519_490_1074,1291_94_324,285_758_922,1135_95_1594,1055_218_1279,
my ($call, @call, $zygosity);
if ($attribute =~ m#zygosity=HEMIZYGOUS#) {
$zygosity = 'het';
} elsif ($attribute =~ m#zygosity=HOMOZYGOUS#) {
$zygosity = 'hom';
} else {
$zygosity = 'unk';
}
if ($type eq 'insertion') {
#the true boundary is unknown (start is different from end) so we cannot use "-" to represent reference allele.
print $chr, "\t", $pos, "\t", $end, "\t", 0, "\t", 0, "\t", $zygosity, "\t", "$score\t.\t.\t", join ("\t", @other), "\n";
} elsif ($type eq 'deletion') {
print $chr, "\t", $pos, "\t", $end, "\t", 0, "\t", '-', "\t", $zygosity, "\t", "$score\t.\t.\t", join ("\t", @other), "\n";
}
} elsif ($program eq 'AB_SOLiD Small Indel Tool') { #small indels
#typical simple insertion and deletions
#chr1 AB_SOLiD Small Indel Tool deletion 1352612 1352614 1 . . ID=1290;del_len=3;allele-call-pos=1352612;allele-call=cct/;allele-pos=1352610;alleles=cccctccat/cCCCAT;allele-counts=REF,2;tight_chrom_pos=1352612-1352614;loose_chrom_pos=1352612-1352614;no_nonred_reads=2;coverage_ratio=11.5000;experimental-zygosity=HEMIZYGOUS;experimental-zygosity-score=1.0000;run_names=L1_1_25_3_r,L1_1_25_8_r;bead_ids=1470_2000_506,822_1710_1767;overall_qvs=18,19;no_mismatches=3,3;read_pos=6,13;from_end_pos=19,12;strands=-,+;tags=R3,R3;indel_sizes=-3,-3;non_indel_no_mismatches=1,-1;unmatched-lengths=25,25;ave-unmatched=25.0000;anchor-match-lengths=24,99;ave-anchor-length=61.5000;read_seqs=G0112310001100003120031200,G0300213000011000132110021;base_qvs=;non_indel_seqs=T2120033002022200220000002,;non_indel_qvs=
#chr1 AB_SOLiD Small Indel Tool insertion_site 1311162 1311162 1 . . ID=1249;ins_len=1;allele-call-pos=1311162;allele-call=/G;allele-pos=1311161;alleles=gaggggggg/GAGGGGGGGG/NO_CALL;allele-counts=REF,3,1;tight_chrom_pos=none;loose_chrom_pos=1311160-1311169;no_nonred_reads=3;coverage_ratio=4.6667;experimental-zygosity=HEMIZYGOUS;experimental-zygosity-score=1.0000;run_names=L1_1_25_6_r,L1_1_50_10_r,L1_1_25_2_r,L1_1_25_3_r;bead_ids=850_837_429,1160_878_181,404_1050_1881,1084_64_1343;overall_qvs=20,56,25,25;no_mismatches=3,2,2,1;read_pos=11,22,11,11;from_end_pos=14,28,14,14;strands=+,-,-,-;tags=R3,F3,F3,F3;indel_sizes=1,1,1,1;non_indel_no_mismatches=-1,1,0,1;unmatched-lengths=25,50,25,25;ave-unmatched=31.2500;anchor-match-lengths=99,49,24,24;ave-anchor-length=49.0000;read_seqs=G1020001130221020000000020,T03223323210110021000000022122030100020221222222122,T0102210000000221223301000,T0102210000000221220301000;base_qvs=;non_indel_seqs=,G21202030032202013220021321131212021000122300013132,G1331133120001221220120120,G1331133120001221220120220;non_indel_qvs=
#sometimes, allele-call is ambiguous that requires a "block substitution" representation (although they were annotated as insertion or deletion by SOLiD, they should be treated as block substitution by ANNOVAR)
#sometimes, mutiple allele calls may be present at the same site
#chr1 AB_SOLiD Small Indel Tool deletion 1209357 1209360 1 . . ID=1101;del_len=4;allele-call-pos=1209357;allele-call=ggtggg/TT;allele-pos=1209355;alleles=ggggtgggggggtt/gGTTGGGGTT/gGTGTTTTGCCTT/NO_CALL;allele-counts=REF,3,1,1;tight_chrom_pos=none;loose_chrom_pos=1209357-1209363;no_nonred_reads=4;coverage_ratio=3.0000;experimental-zygosity=HEMIZYGOUS;experimental-zygosity-score=0.9888;run_names=L1_1_25_1_r,L1_1_25_2_r,L1_1_25_4_r,L1_1_25_3_r,L1_1_25_7_r;bead_ids=1017_1024_53,1493_1896_615,1794_647_1473,307_1116_687,773_1492_1671;overall_qvs=24,24,28,24,8;no_mismatches=2,3,2,3,2;read_pos=14,9,14,9,15;from_end_pos=11,16,11,16,10;strands=-,+,-,+,+;tags=F3,R3,F3,F3,F3;indel_sizes=-4,-4,-4,-4,3;non_indel_no_mismatches=1,0,0,0,0;unmatched-lengths=25,25,25,25,25;ave-unmatched=25.0000;anchor-match-lengths=24,24,24,24,24;ave-anchor-length=24.0000;read_seqs=T2221100101000101000221100,G0001122000100000101001020,T2221100101000101000221100,T1112200010100010100112000,T1011220000111000130200001;base_qvs=;non_indel_seqs=G0312033221312111022200300,T0111113210210112100001130,G0312133221312111022200300,G0231003132222112000012020,G3121331033101113122312020;non_indel_qvs=
#chr1 AB_SOLiD Small Indel Tool deletion 1209436 1209436 1 . . ID=1103;del_len=1;allele-call-pos=1209436;allele-call=ag/A/G;allele-pos=1209434;alleles=tgagggggtt/tGAGGGGTT/tGGGGGGTT;allele-counts=REF,1,1;tight_chrom_pos=none;loose_chrom_pos=1209436-1209441;no_nonred_reads=2;coverage_ratio=5.0000;experimental-zygosity=HEMIZYGOUS;experimental-zygosity-score=1.0000;run_names=L1_1_25_6_r,L1_1_25_2_r;bead_ids=1315_1584_2005,1706_194_437;overall_qvs=28,21;no_mismatches=0,3;read_pos=9,7;from_end_pos=16,18;strands=-,-;tags=R3,R3;indel_sizes=-1,-1;non_indel_no_mismatches=-1,0;unmatched-lengths=25,25;ave-unmatched=25.0000;anchor-match-lengths=99,24;ave-anchor-length=61.5000;read_seqs=G3001010000011001010000001,G3010100022110010111000110;base_qvs=;non_indel_seqs=,T1112003220020013202122300;non_indel_qvs=
#chr1 AB_SOLiD Small Indel Tool insertion_site 1424540 1424540 1 . . ID=1376;ins_len=3;allele-call-pos=1424540;allele-call=tt/CCCAC;allele-pos=1424537;alleles=ttttttg/TTTCCCACTG/NO_CALL;allele-counts=REF,1,1;tight_chrom_pos=none;loose_chrom_pos=1424536-1424543;no_nonred_reads=2;coverage_ratio=11.5000;experimental-zygosity=HEMIZYGOUS;experimental-zygosity-score=1.0000;run_names=L1_1_25_7_r,L1_1_50_16_r;bead_ids=703_437_370,1089_878_1744;overall_qvs=1,9;no_mismatches=3,4;read_pos=5,35;from_end_pos=20,15;strands=-,-;tags=R3,F3;indel_sizes=3,3;non_indel_no_mismatches=2,0;unmatched-lengths=25,50;ave-unmatched=37.5000;anchor-match-lengths=24,47;ave-anchor-length=35.5000;read_seqs=G2032002200200000000000020,T30100102220312202103112130230322210121100200002100;base_qvs=;non_indel_seqs=T2121120003012303000000000,G22213300221101011121030022002222300220322213303102;non_indel_qvs=
my ($call, @call, $zygosity);
my ($refcall, $gapnonred, %temphash); ### <<< FOR 5500SOLiD LifeScope
#if ($attribute =~ m#experimental-zygosity=HEMIZYGOUS# ||$attribute =~ m#zygosity=HEMIZYGOUS#) { ### <<< FOR 5500SOLiD LifeScope
# $zygosity = 'het';
#} elsif ($attribute =~ m#experimental-zygosity=HOMOZYGOUS# || $attribute =~ m#zygosity=HOMOZYGOUS#) { ### <<< FOR 5500SOLiD LifeScope
#the above 3 lines are replaced by the following 3 lines on 20120618
if ($attribute =~ m#zygosity=(MULTI-)?HEMIZYGOUS#) { ### <<< FOR 5500SOLiD LifeScope
$zygosity = 'het';
} elsif ($attribute =~ m#zygosity=(MULTI-)?HOMOZYGOUS#) { ### <<< FOR 5500SOLiD LifeScope
$zygosity = 'hom';
} else {
$zygosity = 'unk';
}
$score = '.'; #by default, score=1 in the output
#no_nonred_reads: Number of reads with unique start positions (non-redundant reads).
#coverage_ratio: Clipped normal coverage/number of non-redundant reads.Clipped coverage is where the parts of the read that are within 5 bp at either end are not counted as a part of coverage.
if ($attribute =~ m/no_nonred_reads=(\d+);coverage_ratio=([\d\.]+)/) {
$readcount = int ($1*$2);
$readcount >= $coverage or next; #clipped normal read count of the variant call (this could even be lower than mut allele count)
$maxcoverage and $readcount <= $maxcoverage || next;
} elsif ($attribute =~ m/gap-nonred-reads=(\d+)/) { ### <<< FOR 5500SOLiD LifeScope
$gapnonred = $1;
$attribute =~ m/coverage_ratio=(\d+)/;
$readcount = int($gapnonred*$1);
$readcount >= $coverage or next;
$maxcoverage and $readcount <= $maxcoverage || next;
} else {
$readcount = '.';
}
if ($attribute =~ m/allele-counts=REF,(\d+)/) {
$mutallelecount = $1;
} elsif ($attribute =~ m/context-variant-reads=(\d+)/) { ### <<< FOR 5500SOLiD LifeScope
$mutallelecount = $1;
}
if ($attribute =~ m#reference=([\w\-]+)#) { ### <<< FOR 5500SOLiD LifeScope (using "reference" tag for the reference allele)
$refcall = $1;
$attribute =~ m#;allele\-call=([\w\-\/]+)#;
foreach my $item(split(/\//, $1)) { $temphash{$item}++; } # collecting unique alleles
delete $temphash{"possibleOthers"}; # ingore the "possibleOthers" allele
@call = keys %temphash;
if ($1 eq '-/-') { # a simple deletion ["allele-call=-/-"] (for the end position, "$end" is already not used)
print $chr, "\t", $pos, "\t", $pos+length($refcall)-1, "\t", $refcall, "\t", '-', "\t", $zygosity, "\t", "$score\t$readcount\t$mutallelecount\t", join ("\t", @other), "\n";
} elsif ($refcall eq '-') { # a simple insertion (single or multiple allele) ["reference=-"]
for my $i (0 .. @call-1) {
next if ($refcall eq $call[$i]);
print $chr, "\t", $pos, "\t", $pos, "\t", '-', "\t", $call[$i], "\t", $zygosity, "\t", "$score\t$readcount\t$mutallelecount\t", join ("\t", @other), "\n";
$i > 0 and $countallvar++;
}
} else { # an indel that may have several alleles, or may require a block substitution representation
for my $i (0 .. @call-1) {
next if ($refcall eq $call[$i]);
# for the end position, "$pos+length($call[0])-1" is already not used.
print $chr, "\t", $pos, "\t", $pos+length($refcall)-1, "\t", $refcall, "\t", $call[$i], "\t", $zygosity, "\t", "$score\t$readcount\t$mutallelecount\t", join ("\t", @other), "\n";
$i > 0 and $countallvar++;
}
}
} elsif ($attribute =~ m#allele\-call=([\w\/]+)#) {
@call = split (/\//, $1);
if (@call == 1) { #a simple deletion
print $chr, "\t", $pos, "\t", $end, "\t", $call[0], "\t", '-', "\t", $zygosity, "\t", "$score\t$readcount\t$mutallelecount\t", join ("\t", @other), "\n";
} elsif ($call[0] eq '') { #a simple insertion (single or multiple allele)
for my $i (1 .. @call-1) {
print $chr, "\t", $pos, "\t", $pos, "\t", '-', "\t", $call[$i], "\t", $zygosity, "\t", "$score\t$readcount\t$mutallelecount\t", join ("\t", @other), "\n";
$i > 1 and $countallvar++;
}
} else { #an indel that may have several alleles, or may require a block substitution representation
for my $i (1 .. @call-1) {
print $chr, "\t", $pos, "\t", $pos+length($call[0])-1, "\t", $call[0], "\t", $call[$i], "\t", $zygosity, "\t", "$score\t$readcount\t$mutallelecount\t", join ("\t", @other), "\n";
$i > 1 and $countallvar++;
}
}
} else {
$call = '0';
print $chr, "\t", $pos, "\t", $end, "\t", $call, "\t", '-', "\t", $zygosity, "\t", "$score\t$readcount\t$mutallelecount\t", join ("\t", @other), "\n";
}
} else {
die "Error: unrecognizable genotype calling program encountered (valid types are SOLiD_diBayes, AB_CNV_PIPELINE, AB_SOLiD Large Indel Tool, AB_SOLiD Small Indel Tool): <$_>\n";
}
$countvar++; #variation positions
$countallvar++; #all variants (maybe several at one variation position)
}
print STDERR "NOTICE: Finished processing $variantfile with $countline input lines\n";
print STDERR "NOTICE: Wrote variants in $countvar variation positions ($countallvar variants considering multi-allelic ones)\n";
$unknown_count and print STDERR "WARNING: $unknown_count variants with 'unknown' variation type were skipped\n";
}
sub convertSOAP {
my ($variantfile) = @_;
my ($countline, $countvar, @other);
if ($variantfile eq 'stdin') {
*VAR = *STDIN;
} elsif ($variantfile =~ m/\.gz$/) {
open (VAR, "gunzip -c $variantfile |") or die "Error: cannot read from STDIN uncompressing variant file $variantfile: $!\n";
} else {
open (VAR, $variantfile) or die "Error: cannot read from variant file $variantfile: $!\n";
}
while (<VAR>) {
s/[\r\n]+$//;
$countline++;
my @field = split (/\t/, $_);
if (@field == 18) { #snp file
my ($chr, $pos, $wt, $call, @other) = @field;
$chr =~ s/^chr//;
$includeinfo or @other = (); #unless -includeinfo is set, the other will not be printed
my $obs = $iupac{$call} or die "Error: invalid best call in <$_>\n";
my @obs = split (//, $obs);
@obs == 2 or die "Error: observed IUPAC allele $call should correspond to two nucleotide alleles: <$_>\n";
if ($obs[0] eq $wt and $obs[1] eq $wt) {
die "Error: reference alleles are identical to observed alleles: <$_>\n";
} elsif ($obs[0] eq $wt) {
print $chr, "\t", $pos, "\t", $pos, "\t", $wt, "\t", $obs[1], "\t", "het\t", join ("\t", @other), "\n";
} elsif ($obs[1] eq $wt) {
print $chr, "\t", $pos, "\t", $pos, "\t", $wt, "\t", $obs[0], "\t", "het\t", join ("\t", @other), "\n";
} elsif ($obs[1] ne $obs[0]) {
print $chr, "\t", $pos, "\t", $pos, "\t", $wt, "\t", $obs[0], "\t", "het\t", join ("\t", @other), "\n";
print $chr, "\t", $pos, "\t", $pos, "\t", $wt, "\t", $obs[1], "\t", "het\t", join ("\t", @other), "\n";
$countvar++;
} else {
print $chr, "\t", $pos, "\t", $pos, "\t", $wt, "\t", $obs[0], "\t", "hom\t", join ("\t", @other), "\n";
}
} elsif (@field == 17) { #snp file
my ($chr, $pos, $wt, $call, @other) = @field;
$chr =~ s/^chr//;
$includeinfo or @other = (); #unless -includeinfo is set, the other will not be printed
my $obs = $iupac{$call} or die "Error: invalid best call in <$_>\n";
my @obs = split (//, $obs);
@obs == 2 or die "Error: observed IUPAC allele $call should correspond to two nucleotide alleles: <$_>\n";
if ($obs[0] eq $wt and $obs[1] eq $wt) {
die "Error: reference alleles are identical to observed alleles: <$_>\n";
} elsif ($obs[0] eq $wt) {
print $chr, "\t", $pos, "\t", $pos, "\t", $wt, "\t", $obs[1], "\t", "het\t", join ("\t", @other), "\n";
} elsif ($obs[1] eq $wt) {
print $chr, "\t", $pos, "\t", $pos, "\t", $wt, "\t", $obs[0], "\t", "het\t", join ("\t", @other), "\n";
} elsif ($obs[1] ne $obs[0]) {
print $chr, "\t", $pos, "\t", $pos, "\t", $wt, "\t", $obs[0], "\t", "het\t", join ("\t", @other), "\n";
print $chr, "\t", $pos, "\t", $pos, "\t", $wt, "\t", $obs[1], "\t", "het\t", join ("\t", @other), "\n";
$countvar++;
} else {
print $chr, "\t", $pos, "\t", $pos, "\t", $wt, "\t", $obs[0], "\t", "hom\t", join ("\t", @other), "\n";
}
} elsif (@field == 6) { #indel file
my ($chr, $pos, $strand, $indellen, $call, $homo) = @field;
$homo eq 'Homo' and $homo = 'hom';
$homo eq 'Hete' and $homo = 'het';
$chr =~ s/^chr//;
$includeinfo or @other = (); #unless -includeinfo is set, the other will not be printed
if ($indellen =~ m/^\+(\d+)$/) { #insertion
length ($call) == $1 or die "Error: invalid record found in SOAPindel file: <$_>\n";
print join("\t", $chr, $pos, $pos, '-', $call, $homo), "\n";
} elsif ($indellen =~ m/^\-(\d+)$/) { #deletion
length ($call) == $1 or die "Error: invalid record found in SOAPindel file: <$_>\n";
print join("\t", $chr, $pos, $pos+$1-1, $call, '-', $homo), "\n";
} else {
die "Error: invalid record found in SOAPindel file: <$_>\n";
}
} else {
die "Error: invalid record found in $variantfile (18, 17 or 6 fields expected, observed ${\(scalar @field)} fields): <$_>\n";