comparison gfapts/gfap_r1.0_known_var_finder.pl~ @ 1:028f435b6cfb draft default tip

Uploaded
author rdaveau
date Fri, 03 Aug 2012 05:50:41 -0400
parents
children
comparison
equal deleted inserted replaced
0:f753b30013e6 1:028f435b6cfb
1 #!/usr/bin/perl
2
3 use strict;
4 #use lib 'inc/perlmod';
5 #use ngsutil qw[ :DEFAULT &varscan ];
6 use warnings FATAL => qw[ numeric uninitialized ];
7 use File::Basename;
8 use Getopt::Long;
9
10 #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
11 # ngsutil.pm
12 sub explode_varcall{
13 my $N=0;
14 $_=shift @_ foreach my($POS, $REF, $ALT);
15 $_=$POS foreach my($START, $END);
16 my(@length, @range, @idx, @VAR, @POS);
17 @{$_}=() foreach (\@length, \@range, \@idx, \@VAR, \@POS);
18 push @length, length($_) foreach ($REF, $ALT);
19 @range=sort{ $a<=>$b } @length;
20 if($range[0]==1){
21 if($range[1]!=1){
22 foreach ($REF, $ALT){
23 $_=substr($_, 1);
24 $_=~s/^$/-/;
25 }
26 if($length[0]!=1){
27 $END+=$length[0]-1;
28 $START++;
29 }
30 }
31 push @POS, $START, $END;
32 push @VAR, $REF, $ALT;
33 }else{
34 my @N=();
35 undef $_ foreach my ($i, $VAR);
36 $_-=2 foreach (@length, @range);
37 $_++ foreach ($START, $END);
38 $_=substr($_, 1) foreach ($REF, $ALT);
39 my $indel='-' x ($range[1]-$range[0]);
40 $VAR.=($_>$range[0])?
41 ('-'):((substr($REF, $_, 1) ne substr($ALT, $_, 1))?
42 0:1) for 0 .. $range[1];
43 $N++ while $VAR =~ /0/g;
44 if($length[0]<$length[1]){
45 @VAR=($VAR);
46 @N=($N);
47 $N=0;
48 undef($VAR);
49 $VAR.=($_>$range[0])?
50 ('-'):((substr($REF, $length[0]-$_, 1) ne substr($ALT, $length[1]-$_, 1))?
51 0:1) for reverse 0 .. $range[1];
52 $N++ while $VAR =~ /0/g;
53 if($N>=$N[0]){ $N=shift(@N); $VAR=shift(@VAR); }
54 else{ $REF=$indel . $REF; }
55 }else{ $ALT.=$indel; }
56 foreach (qw[ 0 \- ]){
57 push @idx, [ $-[0], $+[0]-$-[0] ] while ($VAR =~ /$_+/g);
58 }
59 @{$_}=() foreach (\@VAR, \@POS);
60 foreach my $k (@idx){
61 push @VAR, substr($_, ${$k}[0], ${$k}[1]) || '-' foreach ($REF, $ALT);
62 push @POS, ${$k}[0], sum(@{$k})-1;
63 }
64 $_+=$START foreach @POS;
65 $_=~s/\-+/\-/ foreach @VAR;
66 for($i=0; $i<$#POS; $i+=2){ $POS[$i+1]=$POS[$i] if $VAR[$i] eq '-'; }
67 }
68 return(\@POS, \@VAR);
69 }
70
71 sub varscan{
72 $_=shift @_ foreach my($kname, $fpath, $href);
73 my($k, @buffer);
74 open IN, "<$fpath" or die $!;
75 while(<IN>){
76 next if /^#/;
77 chomp;
78 @buffer=split /\s+/, $_;
79 next if !exists $$href{($k=join(':', @buffer[0..2]))};
80 next if $$href{$k}->{ref} !~ $buffer[3];
81 next if $$href{$k}->{alt} !~ $buffer[4];
82 splice(@buffer, 0, 5);
83 $$href{$k}->{$kname}=join(':', @buffer);
84 }
85 close IN;
86 }
87 #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
88
89 my($varfile, $buildver, $outdir, $dir_1000g, $dir_dbsnp, $dir_cosmic, $release_1000g, $release_dbsnp, $release_cosmic, $outfile, $k, @buffer, @varlist, %opts, %varlist);
90
91 GetOptions(\%opts, "varfile=s", "buildver=s", "outdir=s", "dir_1000g=s", "dir_dbsnp=s", "dir_cosmic=s", "release_1000g=s", "release_dbsnp=s", "release_cosmic=s", "outfile=s");
92 $varfile = $opts{varfile};
93 $buildver = $opts{buildver};
94 $outdir = $opts{outdir};
95 $dir_1000g = $opts{dir_1000g};
96 $dir_dbsnp = $opts{dir_dbsnp};
97 $dir_cosmic = $opts{dir_cosmic};
98 $release_1000g = $opts{release_1000g};
99 $release_dbsnp = $opts{release_dbsnp};
100 $release_cosmic = $opts{release_cosmic};
101 $outfile = $opts{outfile};
102
103 my $fname = readlink($varfile) || $varfile;
104 $fname = basename($fname);
105
106 my %k=(
107 '1000g' => {
108 'dir' => $dir_1000g, 'release' => $release_1000g, 'value' => join(':', ('0.00000')x5), 'header' => join(':', 'AF_ALL', 'AF_AFR', 'AF_AMR', 'AF_ASN', 'AF_EUR')
109 }, 'dbsnp' => {
110 'dir' => $dir_dbsnp, 'release' => $release_dbsnp, 'value' => join(':', ('na')x2), 'header' => join(':', 'rs', 'dbsnp')
111 }, 'cosmic_var' => {
112 'dir' => $dir_cosmic, 'release' => $release_cosmic, 'value' => join(':', '0.00000', 'na'), 'header' => join(':', 'AF_COS', 'cid')
113 }
114 );
115
116 my %legend=(
117 'chr' => 'chromosome identifier',
118 'start' => "${buildver} 1-based start position",
119 'end' => "${buildver} 1-based end position",
120 'ref' => 'reference allele',
121 'alt' => 'alternate allele',
122 'QC' => 'Phred-scaled call quality',
123 'NRF' => '#reads consistent w/ the reference allele on the F-strand',
124 'NRR' => '#reads consistent w/ the reference allele on the R-strand',
125 'NAF' => '#reads consistent w/ the alternate allele on the F-strand',
126 'NAR' => '#reads consistent w/ the alternate allele on the R-strand',
127 'DP' => 'total #reads in call ie. NRF+NRR+NAF+NAR',
128 'AD' => 'total #reads consistent w/ the alternate allele ie. NAF+NAR',
129 'AF' => 'alternate allele ratio ie. AD/DP',
130 'VCF.FILTER' => 'FILTER field from the input vcf file',
131 'DPT.FILTER' => 'check for heterogeneous depth in substituted blocks',
132 'VAR.FILTER' => 'GFAP default FILTER to discriminate between TP and FP variants',
133 'P.str' => 'NRF+NAF vs. NRR+NAR binomial test P-value ie. total strand bias',
134 'P.ref' => 'NRF vs. NRR binomial test P-value ie. reference allele strand bias',
135 'P.alt' => 'NAF vs. NAR binomial test P-value ie. alternate allele strand bias',
136 'AF_ALL' => "global AF in ${release_1000g} 1000g data",
137 'AF_AFR' => "AF in AFR ${release_1000g} 1000g data",
138 'AF_AMR' => "AF in AMR ${release_1000g} 1000g data",
139 'AF_ASN' => "AF in ASN ${release_1000g} 1000g data",
140 'AF_EUR' => "AF in EUR ${release_1000g} 1000g data",
141 'AF_COS' => "AF in ${release_cosmic} cosmic data",
142 'rs' => "dbsnp rs identifier(s) from ${release_dbsnp} release",
143 'dbsnp' => "dbsnp build version(s) from ${release_dbsnp} release",
144 'cid' => "cosmic mutation identifier from ${release_cosmic} release"
145 );
146 my @header=('chr', 'start', 'end', 'ref', 'alt', 'DPT.FILTER', 'QC', 'NRF', 'NRR', 'NAF', 'NAR', 'VCF.FILTER', 'P.str', 'P.ref', 'P.alt', 'DP', 'AD', 'AF', 'VAR.FILTER');
147 my @k=qw[ 1000g dbsnp cosmic_var ];
148
149 open IN, "<$varfile" or die $!;
150 while(<IN>){
151 chomp;
152 @buffer=split /\s+/, $_;
153 $buffer[0]=~s/^chr(.+)$/$1/;
154 push @varlist, ($k=join(':', @buffer[0..2]));
155 shift(@buffer) for 0..2;
156 $varlist{$k}->{$_}=shift(@buffer) foreach qw[ ref alt ];
157 $varlist{$k}->{cov}=join(':', (($buffer[0] eq 'unk')?'SKIP':'PASS'), @buffer[1..$#buffer]);
158 }
159 close IN;
160
161 foreach $k (@k){
162 push @header, split(/:/, $k{$k}->{header});
163 varscan($k, $k{$k}->{file}, \%varlist);
164 }
165
166 my @idx=(0..4,7..10,15..17,6,12..14,11,5,18..23,26..27,24..25);
167 open OUT, ">${outdir}/${fname}.dbi" or die $!;
168 print OUT '#', join(' = ', $_, $legend{$_}), "\n" foreach @header[@idx];
169 print OUT '#', join("\t", @header[@idx]), "\n";
170 foreach $k (@varlist){
171 @buffer=(split(/:/, 'chr'.$k), $varlist{$k}->{ref}, $varlist{$k}->{alt});
172 push @buffer, split(/:/, ($varlist{$k}->{$_} || $k{$_}->{value})) foreach ('cov', @k);
173 print OUT join("\t", @buffer[@idx]), "\n";
174 }
175 close OUT;
176
177 system "rm $outfile; ln -s ${outdir}/${fname}.dbi $outfile" and die $!;