-
Notifications
You must be signed in to change notification settings - Fork 117
/
OpenTargets.pm
274 lines (211 loc) · 7 KB
/
OpenTargets.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
=head1 LICENSE
Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Copyright [2016-2024] 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
OpenTargets
=head1 SYNOPSIS
mv OpenTargets.pm ~/.vep/Plugins
# print Open Targets Genetics scores and respective gene identifiers (default)
./vep -i variations.vcf --plugin OpenTargets,file=path/to/data.tsv.bz
# print all information from Open Targets Genetics
./vep -i variations.vcf --plugin OpenTargets,file=path/to/data.tsv.bz,cols=all
=head1 DESCRIPTION
A VEP plugin that integrates data from Open Targets Genetics
(https://genetics.opentargets.org), a tool that highlights variant-centric
statistical evidence to allow both prioritisation of candidate causal variants
at trait-associated loci and identification of potential drug targets.
Data from Open Targets Genetics includes locus-to-gene (L2G) scores to predict
causal genes at GWAS loci.
The tabix utility must be installed in your path to use this plugin. The Open
Targets Genetics file and respective index (TBI) file can be downloaded from:
https://ftp.ebi.ac.uk/pub/databases/opentargets/genetics/latest/OTGenetics_VEP
Options are passed to the plugin as key=value pairs:
file : (mandatory) Tabix-indexed file from Open Targets Genetics
cols : (optional) Colon-separated list of columns to return from the plugin
file (default: "l2g:geneId"); use 'all' to print all data
Please cite the Open Targets Genetics publication alongside the VEP if you use
this resource: https://doi.org/10.1093/nar/gkaa84
=cut
package OpenTargets;
use strict;
use warnings;
use File::Basename;
use Bio::SeqUtils;
use Bio::EnsEMBL::Variation::Utils::Sequence qw(get_matched_variant_alleles);
use Bio::EnsEMBL::Variation::Utils::BaseVepTabixPlugin;
use base qw(Bio::EnsEMBL::Variation::Utils::BaseVepTabixPlugin);
sub _plugin_name {
return 'OpenTargets';
}
sub _prefix_cols {
my $cols = shift;
my $prefix = _plugin_name() . '_';
my @prefixed_cols = map { $_ =~ /^$prefix/ ? $_ : $prefix . $_ } @$cols;
return \@prefixed_cols;
}
sub _get_colnames {
my $self = shift;
# Open file header
open IN, "tabix -H " . $self->{_files}[0] . " |"
or die "ERROR: cannot open tabix file for " . $self->{_files}[0];
# Get last line from header
my $last;
$last = $_ while <IN>;
$last =~ s/(^#|\n$)//g;
close IN;
# Parse column names from header
my @cols = split /\t/, $last;
@cols = splice @cols, 4; # first columns only identify the variant
return \@cols;
}
sub _parse_colnames {
my $self = shift;
my $cols = shift;
# Parse file columns
$self->{colnames} = $self->_get_colnames();
if ($cols eq "all") {
$self->{cols} = $self->{colnames};
} else {
my @cols = split(/:/, $cols);
$self->{cols} = \@cols;
# Check validity of all columns
my @invalid_cols;
for my $col (@{ $self->{cols} }) {
push(@invalid_cols, $col) unless grep(/^$col$/, @{ $self->{colnames} });
}
die "\n ERROR: The following columns were not found in file header: ",
join(", ", @invalid_cols), "\n" if @invalid_cols;
}
# Prefix all column names
$self->{colnames} = _prefix_cols $self->{colnames};
$self->{cols} = _prefix_cols $self->{cols};
}
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
$self->expand_left(0);
$self->expand_right(0);
$self->get_user_params();
my $param_hash = $self->params_to_hash();
# Check file
my $file = $param_hash->{file};
die "\n ERROR: No file specified\nTry using 'file=path/to/data.tsv.bz'\n"
unless defined($file);
$self->add_file($file);
# Parse column names
my $cols = $param_hash->{cols} || "l2g:geneId";
$self->_parse_colnames($cols);
return $self;
}
sub feature_types {
return ['Feature', 'Intergenic'];
}
sub get_header_info {
my $self = shift;
my %header;
my @keys = @{ $self->{colnames} };
my $description = "column from " . basename $self->{_files}[0];
my @vals = map { $description } @keys;
@header{ @keys } = @vals;
# Custom headers
$header{_plugin_name() . '_l2g'} = "Locus-to-gene (L2G) scores to predict causal genes at GWAS loci; " . $description;
# Filter by user-selected columns
%header = map { $_ => $header{$_} } @{ $self->{cols} };
return \%header;
}
sub _filter_selected_cols {
my $res = shift;
my $cols = shift;
my %tmp = %$res;
%tmp = map { $_ => $res->{$_} } @$cols;
return \%tmp;
}
sub _join_results {
my $self = shift;
my $all_results = shift;
my $res = shift;
# Filter user-selected columns
$res = _filter_selected_cols($res, $self->{cols});
if ($self->{config}->{output_format} eq 'json' || $self->{config}->{rest}) {
# Group results for JSON and REST
my $name = _plugin_name();
$all_results->{$name} = [] unless defined $all_results->{$name};
push(@{ $all_results->{$name} }, $res);
} else {
# Create array of results per key
for (keys %$res) {
$all_results->{$_} = [] if !$all_results->{$_};
push(@{ $all_results->{$_} }, $res->{$_} || "NA");
}
}
return $all_results;
}
sub run {
my ($self, $tva) = @_;
my $vf = $tva->variation_feature;
my $allele = $tva->base_variation_feature->alt_alleles;
my @data = @{$self->get_data($vf->{chr}, $vf->{start} - 2, $vf->{end})};
my $all_results = {};
foreach (@data) {
my $matches = get_matched_variant_alleles(
{
ref => $vf->ref_allele_string,
alts => $allele,
pos => $vf->{start},
strand => $vf->strand
},
{
ref => $_->{ref},
alts => [$_->{alt}],
pos => $_->{start},
}
);
$all_results = $self->_join_results($all_results, $_->{result}) if @$matches;
}
return $all_results;
}
sub parse_data {
my ($self, $line) = @_;
my ($chrom, $pos, $ref, $alt, @vals) = split /\t/, $line;
my $start = $pos;
my $end = $pos;
# VCF-like adjustment of mismatched substitutions for comparison with VEP
if(length($alt) != length($ref)) {
my $first_ref = substr($ref, 0, 1);
my $first_alt = substr($alt, 0, 1);
if ($first_ref eq $first_alt) {
$start++;
$ref = substr($ref, 1);
$alt = substr($alt, 1);
$ref ||= '-';
$alt ||= '-';
}
}
my %res;
@res{ @{ $self->{colnames} } } = @vals;
return {
ref => $ref,
alt => $alt,
start => $start,
result => \%res
};
}
sub get_start {
return $_[1]->{start};
}
sub get_end {
return $_[1]->{end};
}
1;