/
vcf-contrast
executable file
·198 lines (175 loc) · 6.2 KB
/
vcf-contrast
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
#!/usr/bin/env perl
#
# Author: petr.danecek@sanger
#
use strict;
use warnings;
use Carp;
use Vcf;
my $opts = parse_params();
query_vcf($opts);
exit;
#--------------------------------
sub error
{
my (@msg) = @_;
if ( scalar @msg ) { confess @msg; }
print
"About: Find differences amongst samples\n",
"Usage: vcf-contrast +<list> -<list> [OPTIONS] file.vcf.gz\n",
"Options:\n",
" +<list> List of samples where unique variant is expected\n",
" -<list> List of background samples\n",
" -d, --min-DP <int> Minimum depth across all -<list> samples\n",
" -f, --apply-filters Skip sites with FILTER column different from PASS or \".\"\n",
" -h, -?, --help This help message.\n",
"Example:\n",
" # Test if any of the samples A,B is different from all C,D,E\n",
" vcf-contrast +A,B -C,D,E -m file.vcf.gz\n",
"\n",
" # Similar to above but require minimum mapping quality of 20\n",
" vcf-annotate -f MinMQ=20 file.vcf.gz | vcf-contrast +A,B,C -D,E,F -f\n",
"\n";
exit -1;
}
sub parse_params
{
my $opts = {};
while (defined(my $arg=shift(@ARGV)))
{
if ( -e $arg ) { $$opts{vcf}=$arg; next }
if ( $arg eq '-?' || $arg eq '-h' || $arg eq '--help' ) { error(); }
if ( $arg eq '-d' || $arg eq '--min-DP' ) { $$opts{min_dp}=shift(@ARGV); next; }
if ( $arg eq '-f' || $arg eq '--apply-filters' ) { $$opts{apply_filters}=1; next; }
if ( $arg eq '-m' || $arg eq '--mendelian' ) { $$opts{mendelian}=1; next; }
if ( $arg=~/^\+/ && !exists($$opts{var_samples}) ) { @{$$opts{var_samples}}=split(/,/,$'); next }
if ( $arg=~/^-/ && !exists($$opts{bg_samples}) ) { @{$$opts{bg_samples}}=split(/,/,$'); next }
error("Unknown parameter \"$arg\". Run -h for help.\n");
}
if ( !exists($$opts{var_samples}) ) { error("Missing the list of variant samples (+<list>).\n") }
if ( !exists($$opts{bg_samples}) ) { error("Missing the list of background samples (-<list>).\n") }
return $opts;
}
sub init_columns
{
my ($vcf,@samples) = @_;
my @out;
for my $sample (@samples)
{
push @out, $vcf->get_column_index($sample);
}
return (@out);
}
sub query_vcf
{
my ($opts) = @_;
my $vcf = exists($$opts{vcf}) ? Vcf->new(file=>$$opts{vcf}) : Vcf->new(fh=>\*STDIN);
$vcf->parse_header;
my @cols = qw(CHROM POS NOVEL_ALLELE NOVEL_GT SCORE IS_INDEL);
for my $sample (@{$$opts{var_samples}}) { push @cols,"+GT:$sample"; }
for my $sample (@{$$opts{var_samples}}) { push @cols,"+PL:$sample"; }
for my $sample (@{$$opts{bg_samples}}) { push @cols,"-GT:$sample"; }
for my $sample (@{$$opts{bg_samples}}) { push @cols,"-PL:$sample"; }
print "#";
for (my $i=0; $i<@cols; $i++)
{
print "[",$i+1,"]$cols[$i]\t";
}
print "\n";
my @var_cols = init_columns($vcf,@{$$opts{var_samples}});
my @bg_cols = init_columns($vcf,@{$$opts{bg_samples}});
while (my $rec=$vcf->next_data_array)
{
if ( $$opts{apply_filters} && $$rec[6] ne '.' && $$rec[6] ne 'PASS' ) { next; }
my $ipl = $vcf->get_tag_index($$rec[8],'PL',':');
if ( $ipl<0 ) { error("todo: currently only PL-based analysis implemented"); }
my $idp;
if ( exists($$opts{min_dp}) )
{
$idp = $vcf->get_tag_index($$rec[8],'DP',':');
if ( $idp<0 ) { error("todo: DP not available"); }
}
my (@bg_pls, @bg_als, @bg_gts, @var_pls,@var_gts, $min_dp);
for my $bg_col (@bg_cols)
{
if ( defined $idp )
{
my $dp = $vcf->get_field($$rec[$bg_col],$idp);
if ( !defined $min_dp or $min_dp>$dp ) { $min_dp=$dp; }
}
my $pl = $vcf->get_field($$rec[$bg_col],$ipl);
my @gt = likely_gt($pl);
push @bg_pls, $pl;
push @bg_als, \@gt;
push @bg_gts, join('/',sort @gt);
}
if ( defined $min_dp && $min_dp<$$opts{min_dp} ) { next; }
my $novel_gt = 1;
my $novel_al = 0;
my $min_score;
for my $var_col (@var_cols)
{
my $var_pl = $vcf->get_field($$rec[$var_col],$ipl);
my @var_als = likely_gt($var_pl);
my $var_gt = join('/',sort @var_als);
push @var_pls, $var_pl;
push @var_gts, $var_gt;
my $bg_score;
my %als;
for (my $i=0; $i<@bg_cols; $i++)
{
my $score = same_pls($var_pl, $bg_pls[$i]);
if ( !defined $bg_score or $score<$bg_score ) { $bg_score = $score; }
for my $al (@{$bg_als[$i]}) { $als{$al} = 1; }
if ( $var_gt eq $bg_gts[$i] ) { $novel_gt = 0; }
}
if ( !$bg_score ) { next; }
if ( !defined $min_score or $min_score>$bg_score ) { $min_score = $bg_score; }
for my $al (@var_als)
{
if ( !exists($als{$al}) ) { $novel_al = 1; }
}
}
if ( !$min_score ) { next; }
if ( !$novel_gt && !$novel_al ) { next; }
my $is_indel = 0;
for my $al ($$rec[3], $$rec[4])
{
if ( length($al)>1 ) { $is_indel=1; last; }
}
print "$$rec[0]\t$$rec[1]\t$novel_al\t$novel_gt\t$min_score\t$is_indel\t";
print join("\t",@var_gts), "\t", join("\t",@var_pls), "\t";
print join("\t",@bg_gts), "\t", join("\t",@bg_pls), "\n";
}
}
sub likely_gt
{
my ($pl) = @_;
my @pls = split(/,/,$pl);
my ($min,$imin,$jmin);
my $idx=0;
my $i=0;
while ($idx<@pls)
{
for (my $j=0; $j<=$i; $j++)
{
if ( !defined $min or $min>$pls[$idx] ) { $min=$pls[$idx]; $imin=$i; $jmin=$j; }
$idx++;
}
$i++;
}
return ($jmin,$imin);
}
sub same_pls
{
my ($pla,$plb) = @_;
my @pla = split(/,/,$pla);
my @plb = split(/,/,$plb);
my $min;
my $imin;
for (my $i=0; $i<@pla; $i++)
{
if ( !defined $min or $pla[$i]+$plb[$i]<$min ) { $min=$pla[$i]+$plb[$i]; $imin=$i; }
}
return $min;
}