forked from Ensembl/VEP_plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StructuralVariantOverlap.pm
335 lines (229 loc) · 9.93 KB
/
StructuralVariantOverlap.pm
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
=head1 LICENSE
Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Copyright [2016-2021] EMBL-European Bioinformatics Institute
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=head1 CONTACT
Ensembl <http://www.ensembl.org/info/about/contact/index.html>
=cut
=head1 NAME
StructuralVariantOverlap
=head1 SYNOPSIS
mv StructuralVariantOverlap.pm ~/.vep/Plugins
./vep -i structvariants.vcf --plugin StructuralVariantOverlap,file=gnomad_v2_sv.sites.vcf.gz
=head1 DESCRIPTION
A VEP plugin that retrieves information from overlapping structural variants.
Parameters can be set using a key=value system:
file : required - a VCF file of reference data.
percentage : percentage overlap between SVs (default: 80)
reciprocal : calculate reciprocal overlap, options: 0 or 1. (default: 0)
(overlap is expressed as % of input SV by default)
cols : colon delimited list of data types to return from the INFO fields (only AF by default)
same_type : 1/0 only report SV of the same type (eg deletions for deletions, off by default)
distance : the distance the ends of the overlapping SVs should be within.
match_type : only report reference SV which lie within or completely surround the input SV
options: within, surrounding
label : annotation label that will appear in the output (default: "SV_overlap")
Example- input: label=mydata, output: mydata_name=refSV,mydata_PC=80,mydata_AF=0.05
Example reference data
1000 Genomes Project:
ftp://ftp.1000genomes.ebi.ac.uk/vol1/ftp/phase3/integrated_sv_map/ALL.wgs.mergedSV.v8.20130502.svs.genotypes.vcf.gz
gnomAD::
https://storage.googleapis.com/gnomad-public/papers/2019-sv/gnomad_v2_sv.sites.vcf.gz
Example:
./vep -i structvariants.vcf --plugin StructuralVariantOverlap,file=gnomad_v2_sv.sites.vcf.gz
=cut
package StructuralVariantOverlap;
use strict;
use warnings;
use Bio::EnsEMBL::Variation::Utils::VEP qw(parse_line);
use base qw(Bio::EnsEMBL::Variation::Utils::BaseVepTabixPlugin);
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
## check tabix is available
die "ERROR: tabix does not seem to be in your path\n" unless `which tabix 2>&1` =~ /tabix$/;
my $params_hash = $self->params_to_hash();
## get/check annotation file
die "ERROR: file of annotations needed\n" unless $params_hash->{file};
die "ERROR: file $params_hash->{file} not found\n" unless -e $params_hash->{file};
die "ERROR: Tabix index file $params_hash->{file}\.tbi not found\n"
unless -e $params_hash->{file}.'.tbi';
$self->{file} = $params_hash->{file};
## get/set optional parameters
die "ERROR: StructuralVariantOverlap match type : ". $params_hash->{match_type} . "not recognised . Select within or surrounding\n"
if defined $params_hash->{match_type} && $params_hash->{match_type} !~/^within$|^surrounding$/;
$self->{distance} = $params_hash->{distance} ? $params_hash->{distance} : undef;
$self->{match_type} = $params_hash->{match_type} ? $params_hash->{match_type} : undef ;
$self->{required_percent_olap} = $params_hash->{percentage} ? $params_hash->{percentage} : 80;
$self->{same_type} = $params_hash->{same_type} ? $params_hash->{same_type} : 0 ;
$self->{label} = $params_hash->{label} ? $params_hash->{label} : "SV_overlap";
$self->{reciprocal} = $params_hash->{reciprocal} ? $params_hash->{reciprocal} : 0;
## allow specific columns to be rerieved from the VCF
if($params_hash->{cols}){
my @cols = split/\:/, $params_hash->{cols};
foreach my $col (@cols){
$self->{cols}->{$col} = 1;
}
}
return $self;
}
sub variant_feature_types {
return ['BaseVariationFeature'];
}
sub feature_types {
return ['Feature','Intergenic'];
}
sub get_header_info {
my $self = shift;
return $self->{header_info} if exists($self->{header_info});
my $desc;
## if any specific columns were requested; add them
if( scalar(keys %{$self->{cols}}) > 0 ){
$desc = $self->get_header_desc();
}
my $overlap_definition = $self->{reciprocal} == 1 ?
"minimum reciprocal overlap as a percent" :
"percent of input SV covered by reference SV";
my %headers = ( $self->{label}."_name" => "name of overlapping SV",
$self->{label}."_AF" => "frequency of overlapping SV",
$self->{label}."_PC" => $overlap_definition,
);
## add any specifically requested headers
foreach my $info (keys %{$self->{cols}}){
$headers{$self->{label} ."_". $info} = $desc->{$info};
}
return \%headers;
}
## read descriptions from requested info fields from reference VCF
sub get_header_desc {
my $self = shift;
my %desc;
(open my $in, "tabix -f -h ".$self->{file}." 1:1-1 |")||die "Failed to read headers from " . $self->{file} . "\n" ;
my @lines = <$in>;
while(my $line = shift @lines) {
next unless $line =~/INFO/;
my @det = split/\,|\>|\</, $line;
my $key;
foreach my $d(@det){
if($d =~/ID/){
$key = (split/\=/, $d)[1];
}
elsif($d=~/Description/){
$d =~ s/Description\=//;
$desc{$key} = $d;
}
}
}
close $in;
return \%desc;
}
sub run {
my ($self, $bvfoa) = @_;
$self->get_header_info();
my $svf = $bvfoa->base_variation_feature;
## don't annotate breakends with overlapping SV data
return {} if $svf->class_SO_term() eq 'BND';
## read & filter reference VCF
my $overlaps = $self->get_data($svf);
return {} unless $overlaps && scalar @$overlaps;
my %save;
## loop through overlapping SVs extracting required info
foreach my $osv(@{$overlaps}){
## collect output types
push @{$save{$self->{label}."_name"}}, $osv->variation_name();
push @{$save{$self->{label}."_PC"}}, $osv->{_overlap_found};
my %info;
## if any specific columns were requested; add them
my $info = (split/\s+/,$osv->{_line})[7];
my @attribs = split/\;/, $info;
foreach my $att(@attribs){
my($key, $val) = split/\=/, $att;
$info{$key} = $val;
}
if(scalar (keys %{$self->{cols}}) > 0){
foreach my $info (keys %{$self->{cols}}){
push @{$save{$self->{label} . "_" . $info}}, $info{$info};
}
}
else{
## add allele frequency as a default if available
push @{$save{$self->{label}."_AF"}}, defined $info{AF} ? $info{AF} : "-";
}
}
## format
my %output;
foreach my $t( keys %save){
$output{$t} = join(",", @{$save{$t}});
}
return \%output;
}
## read annotation file & apply filters
sub get_data{
my ($self, $svf) = @_;
my $start = $svf->{start};
my $end = $svf->{end};
my $length = $end - $start +1;
## no useful overlap can be calculated if there is no start/end available
return unless $length >0;
my @overlaps;
## adjust coords for tabix
my $s = $start - 1;
my $pos_string = sprintf("%s:%i-%i", $svf->seq_region_name, $s, $end);
# check if chromosome has prefix 'chr'
my $pos_string_chr = $pos_string;
(open my $fo, "tabix -l ".$self->{file}." |") || die "Failed to read chromosome type from " . $self->{file} . "\n";
my $line = <$fo>;
$pos_string_chr = 'chr'.$pos_string if($line =~ /chr/);
(open my $in, "tabix ".$self->{file}." $pos_string_chr |")||die "Failed to read headers from " . $self->{file} . "\n" ;
while(<$in>) {
my $olap_svs = parse_line({format => 'vcf'}, $_);
foreach my $olap_sv (@{$olap_svs}){
## skip breakends - hard to interpret here
next if $olap_sv->class_SO_term() eq 'BND';
## match on SV class if required
## confounded by different descriptions for the same event
next if $olap_sv->class_SO_term() ne $svf->class_SO_term() && $self->{same_type} ==1;
if( defined $self->{match_type}){
## check reference SV within input SV if required
next if $self->{match_type} eq "within" && ($start > $olap_sv->{start} || $end < $olap_sv->{end});
## check reference SV surrounded by input SV if required
next if $self->{match_type} eq "surrounding" && ($start < $olap_sv->{start} || $end > $olap_sv->{end});
}
## check overlap is long enough
my $overlap_found; ## % overlap
if( $self->{required_percent_olap} > 0){
my @overlap_start = sort { $a <=> $b } ($start, $olap_sv->start());
my @overlap_end = sort { $a <=> $b } ($end, $olap_sv->end());
$overlap_found = 100 * (1+ $overlap_end[0] - $overlap_start[1])/ $length;
## overlap as percentage of input SV
next unless $overlap_found > $self->{required_percent_olap};
if ($self->{reciprocal} ==1){
## check bi-directional overlap - percentage of ref SV covered
my $ref_sv_length = $olap_sv->end - $olap_sv->start +1;
my $ref_overlap_found = 100 * (1+ $overlap_end[0] - $overlap_start[1])/ $ref_sv_length;
next unless $ref_overlap_found > $self->{required_percent_olap};
## report minimum overlap if checking bidirectionally
$overlap_found = $ref_overlap_found if $ref_overlap_found < $overlap_found;
}
}
## check breakpoint ends are within required distance
if (defined $self->{distance}){
next unless abs($start - $olap_sv->{start}) < $self->{distance};
next unless abs($end - $olap_sv->{end}) < $self->{distance};
}
## cache overlap on object
$olap_sv->{_overlap_found} = $overlap_found;
push @overlaps, $olap_sv;
}
}
return \@overlaps;
}
1;