Repository navigation
Expand file tree
/
Copy pathPromoterAI.pm
More file actions
348 lines (272 loc) · 9.8 KB
/
Copy pathPromoterAI.pm
File metadata and controls
348 lines (272 loc) · 9.8 KB
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
=head1 LICENSE
Copyright [2026] 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
PromoterAI
=head1 SYNOPSIS
mv PromoterAI.pm ~/.vep/Plugins
./vep -i variations.vcf --plugin PromoterAI,file=/path/to/PromoterAI.tsv.gz
=head1 DESCRIPTION
An Ensembl VEP plugin that adds PromoterAI scores to promoter variants, predicting their impact on gene expression.
Options are passed to the plugin as key=value pairs:
file : (mandatory) Tabix-indexed file from Illumina PromoterAI (see below)
cols : (optional) Colon-separated list of columns to return from the plugin
file (default: "tss_pos:promoterAI"); use 'all' to print all data
match_to : (optional) Feature type to match PromoterAI scores to.
One of ["transcript", "gene", "any"] (default: "transcript").
When "any", matching is done only on the genomic location and the alternative allele sequence.
Choosing "any" can be useful to annotate promotor variants that are further away from affected gene region,
("intergenic" variant in VEP, whithout link to the nearest gene region),
but this can lead to more noisy output for variants in regions with multiple overlapping genes/transcripts.
To download the PromoterAI scores file to use with VEP (GRCh38 based),
please follow the instructions found in the README at https://github.com/Illumina/PromoterAI.
You need a valid license agreement as described in the README to obtain and use the PromoterAI scores.
Please cite the PromoterAI publication alongside Ensembl VEP if you use this resource:
https://www.science.org/doi/10.1126/science.ads7373
Necessary before using the plugin:
Do the following steps to index the annotations file before using the plugin:
zcat promoterAI.tsv.gz | sed '1s/.*/#&/' | bgzip > promoterAI.tsv.bgz
tabix -s 1 -b 2 -e 2 promoterAI.tsv.bgz
You must have the Bio::DB::HTS module or the tabix utility must be installed
in your path to use this plugin.
=cut
package PromoterAI;
use strict;
use warnings;
use File::Basename;
use File::Spec;
use Try::Tiny;
use Bio::EnsEMBL::Variation::Utils::BaseVepTabixPlugin;
use Bio::EnsEMBL::Variation::Utils::Sequence qw(get_matched_variant_alleles);
use base qw(Bio::EnsEMBL::Variation::Utils::BaseVepTabixPlugin);
sub _plugin_name {
return 'PromoterAI';
}
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;
}
# Rename the "promoterAI" column to "score"
$self->{colnames} = [ map { $_ eq "promoterAI" ? "score" : $_ } @{ $self->{colnames} } ];
$self->{cols} = [ map { $_ eq "promoterAI" ? "score" : $_ } @{ $self->{cols} } ];
# Prefix all column names
$self->{colnames} = _prefix_cols $self->{colnames};
$self->{cols} = _prefix_cols $self->{cols};
}
sub _join_results {
my $self = shift;
my $all_results = shift;
my $res = shift;
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 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();
my $file = $param_hash->{file};
die "\n ERROR: No PromoterAI file specified\nTry using 'file=path/to/data.tsv.bgz'\n"
unless defined($file);
$self->add_file($file);
# Store the feature matching level
my $match_to = $param_hash->{match_to} || "transcript";
if($match_to ne 'transcript' && $match_to ne 'gene' && $match_to ne 'any') {
die "\n ERROR: match_to must be 'transcript' or 'gene'\n";
}
$self->{match_to} = $match_to;
# Parse column names
my $default_cols = "tss_pos:promoterAI";
if( $self->{match_to} eq 'any' ){
$default_cols .= ":gene_id";
}
if( $self->{match_to} ne 'transcript' ){
$default_cols .= ":transcript_id";
}
my $cols = $param_hash->{cols} || $default_cols;
$self->_parse_colnames($cols);
if ((defined $self->{config}->{refseq} || defined $self->{config}->{merged}) && $self->{match_to} eq 'transcript') {
warn "WARNING: PromoterAI transcript matching may fail with --refseq/--merged; consider match_to=any\n";
}
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;
# 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 run {
my ($self, $vfoa) = @_;
my $vf = $vfoa->variation_feature;
(my $vf_chr = $vf->{chr}) =~ s/^chr//;
my ($vf_start, $vf_end) = ($vf->{start}, $vf->{end});
$vf_end = $vf_start if $vf_start > $vf_end;
my @data = @{ $self->get_data($vf_chr, $vf_start -2, $vf_end) };
return {} unless @data;
# Filter the data to match the VariantFeatureOverlapAllele's...
my $joined_results = {};
my $vfoa_variation_feature_seq = $vfoa->variation_feature_seq;
my $vfoa_transcript_id;
my $vfoa_gene_id;
try {
$vfoa_transcript_id = $vfoa->transcript->stable_id;
}
catch {
$vfoa_transcript_id = undef;
};
try {
$vfoa_gene_id = $vfoa->transcript->{_gene_stable_id};
}
catch {
$vfoa_gene_id = undef;
};
for my $data_candidate (@data) {
# * Alternative allele sequence
my $candidate_alt_allele = $data_candidate->{'alt'};
my $seq_filtered_matches = get_matched_variant_alleles(
{
ref => $vf->ref_allele_string,
alts => [ $vfoa_variation_feature_seq ],
pos => $vf->{start},
strand => $vf->strand
},
{
ref => $data_candidate->{ref},
alts => [ $candidate_alt_allele ],
pos => $data_candidate->{start},
strand => $data_candidate->{strand}
}
);
if(!@$seq_filtered_matches){
next;
}
# * feature ID
if( $self->{match_to} eq "transcript" ){
my $candidate_transcript_id = $data_candidate->{'transcript_id'};
$candidate_transcript_id =~ s/\.[0-9]+//; # remove transcript version
if(!defined($vfoa_transcript_id) || $vfoa_transcript_id ne $candidate_transcript_id){
next;
}
}
elsif( $self->{match_to} eq "gene" ){
my $candidate_gene_id = $data_candidate->{'gene_id'};
$candidate_gene_id =~ s/\.[0-9]+//; # remove gene version
if(!defined($vfoa_gene_id) || $vfoa_gene_id ne $candidate_gene_id){
next;
}
}
my $promoterAI_result = $data_candidate->{'result'};
# Filter user-selected columns
$promoterAI_result = _filter_selected_cols($promoterAI_result, $self->{cols});
$joined_results = $self->_join_results($joined_results, $promoterAI_result);
}
return $joined_results;
}
sub parse_data {
my ($self, $line) = @_;
my $header = $self->{file_column};
my ($chr, $pos, $ref, $alt, $gene, $gene_id, $transcript_id, $strand, $tss_position, $score) = split /\t/, $line;
# 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) {
$pos++;
$ref = substr($ref, 1) || "-";
$alt = substr($alt, 1) || "-";
}
}
my %result;
@result{ @{ $self->{colnames} } } = ($gene, $gene_id, $transcript_id, $strand, $tss_position, $score);
return {
ref => $ref,
alt => $alt,
start => $pos,
strand => $strand,
gene_id => $gene_id,
transcript_id => $transcript_id,
result => \%result
}
}
sub get_start {
return $_[1]->{start};
}
sub get_end {
return $_[1]->{end};
}
1;