/
BaseVEP.pm
404 lines (292 loc) · 9.44 KB
/
BaseVEP.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
=head1 LICENSE
Copyright [2016] 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.
=cut
=head1 CONTACT
Please email comments or questions to the public Ensembl
developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
Questions may also be sent to the Ensembl help desk at
<http://www.ensembl.org/Help/Contact>.
=cut
# EnsEMBL module for Bio::EnsEMBL::VEP::BaseVEP
#
#
=head1 NAME
Bio::EnsEMBL::VEP::BaseVEP - Base class used by the Bio::EnsEMBL::VEP::* classes
=cut
use strict;
use warnings;
package Bio::EnsEMBL::VEP::BaseVEP;
use Bio::EnsEMBL::Registry;
use Bio::EnsEMBL::Variation::DBSQL::VariationFeatureAdaptor;
use Bio::EnsEMBL::Variation::DBSQL::StructuralVariationFeatureAdaptor;
use Bio::EnsEMBL::Variation::DBSQL::TranscriptVariationAdaptor;
use Bio::EnsEMBL::Variation::Utils::FastaSequence qw(setup_fasta);
use Bio::EnsEMBL::Utils::Scalar qw(assert_ref);
use Bio::EnsEMBL::Utils::Exception qw(throw warning);
use Bio::EnsEMBL::VEP::Utils qw(get_time);
use Bio::EnsEMBL::Slice;
use Bio::EnsEMBL::CoordSystem;
use Bio::EnsEMBL::VEP::Stats;
use FileHandle;
# new method, may or may not be reused by child classes
sub new {
my $caller = shift;
my $class = ref($caller) || $caller;
# initialise self
my $self = bless {}, $class;
# most new methods will pass in the config in the hashref given as the first arg to new()
if(my $hashref = $_[0]) {
# does the config key exist?
if(exists($hashref->{config})) {
# copy and delete it so child class new methods don't get confused
# if they want to deal with this differently, then they shouldn't run $class->SUPER::new
my $config = delete($hashref->{config});
# do an assert ref to check the class
assert_ref($config, 'Bio::EnsEMBL::VEP::Config');
$self->{_config} = $config;
}
}
return $self;
}
# returns Bio::EnsEMBL::Variation::VEP::Config object
sub config {
return $_[0]->{_config};
}
# gets/sets the value of a config parameter given a key name
sub param {
my $self = shift;
return $self->config->param(@_);
}
# get stats object
sub stats {
my $self = shift;
my $config = $self->config;
if(!exists($config->{_stats})) {
$config->{_stats} = Bio::EnsEMBL::VEP::Stats->new({config => $self->config});
}
return $config->{_stats};
}
sub species {
my $self = shift;
if(@_) {
$self->{_species} = shift;
$self->param('species', $self->{_species});
}
else {
$self->{_species} ||= $self->param('species');
}
return $self->{_species};
}
# gets Bio::EnsEMBL::Registry, connects to DB if available
sub registry {
my $self = shift;
my $config = $self->config;
if(@_) {
$config->{_registry} = shift;
}
elsif(!exists($config->{_registry})) {
my $reg = 'Bio::EnsEMBL::Registry';
if($self->param('database') || ($self->param('cache') && !$self->param('offline'))) {
# load DB options from registry file if given
if(my $registry_file = $self->param('registry')) {
$self->status_msg("Loading DB self from registry file ", $registry_file) if $self->param('verbose');
$reg->load_all(
$registry_file,
$self->param('verbose'),
undef,
$self->param('no_slice_cache')
);
}
# otherwise manually connect to DB server
else {
my $species;
unless($self->param('is_multispecies')) {
if($self->param('species') =~ /^[a-z]+\_[a-z]+/i) {
$species = $self->param('species');
}
}
$reg->load_registry_from_db(
-host => $self->param('host'),
-user => $self->param('user'),
-pass => $self->param('password'),
-port => $self->param('port'),
-db_version => $self->param('db_version'),
-species => $species,
-verbose => $self->param('verbose'),
-no_cache => $self->param('no_slice_cache'),
);
}
eval { $reg->set_reconnect_when_lost() };
}
$config->{_registry} = $reg;
}
# copy to raw_config for plugin backward compatibility
$config->{_params}->{reg} ||= $config->{_registry};
return $config->{_registry};
}
sub get_adaptor {
my $self = shift;
my $group = shift;
my $type = shift;
throw("No adaptor group specified") unless $group;
throw("No adaptor type specified") unless $type;
my $cache = $self->{_adaptors} ||= {};
if(!exists($cache->{$group}) || !exists($cache->{$group}->{$type})) {
my $ad;
if($self->param('database') || ($self->param('cache') && !$self->param('offline'))) {
$ad = $self->registry->get_adaptor($self->species, $group, $type)
}
$ad ||= $self->_get_fake_adaptor($group, $type);
$cache->{$group}->{$type} = $ad;
}
return $cache->{$group}->{$type};
}
sub _get_fake_adaptor {
my ($self, $group, $type) = @_;
my $ad;
my $module_name = sprintf(
"Bio::EnsEMBL::%sDBSQL::%sAdaptor",
(lc($group) eq 'core' ? '' : ucfirst($group).'::'),
$type
);
eval { $ad = $module_name->new_fake($self->species()) };
return $ad;
}
sub get_slice {
my $self = shift;
my $chr = shift;
my $cache = $self->{_slice_cache} ||= {};
if(!exists($cache->{$chr})) {
my $slice;
if(my $sa = $self->get_adaptor($self->param('core_type'), 'Slice')) {
$slice = $sa->fetch_by_region(undef, $chr);
}
elsif(my $fasta_db = $self->fasta_db) {
my $fa_length = $fasta_db->length($chr);
my $length = $fa_length && $fa_length > 0 ? $fa_length : 1;
$slice = Bio::EnsEMBL::Slice->new(
-COORD_SYSTEM => $self->{_coord_system} ||= Bio::EnsEMBL::CoordSystem->new(-NAME => 'chromosome', -RANK => 1),
-START => 1,
-END => $length,
-SEQ_REGION_NAME => $chr,
-SEQ_REGION_LENGTH => $length
);
$slice->{is_fake} = 1;
}
$cache->{$chr} = $slice;
}
return $cache->{$chr};
}
sub get_database_assembly {
my $self = shift;
my $config = $self->config;
if(!exists($config->{_database_assembly})) {
my $assembly;
if(my $csa = $self->get_adaptor('core', 'CoordSystem')) {
my ($highest_cs) = @{$csa->fetch_all()};
$assembly = $highest_cs->version();
}
$config->{_database_assembly} = $assembly;
}
return $config->{_database_assembly};
}
sub fasta_db {
my $self = shift;
if(!exists($self->config->{_fasta_db})) {
my $fasta_db;
if(my $fasta_file = $self->param('fasta')) {
$fasta_db = setup_fasta(
-FASTA => $fasta_file,
-ASSEMBLY => $self->param('assembly'),
-OFFLINE => $self->param('offline'),
-SYNONYMS => $self->chromosome_synonyms,
);
$self->stats->log_fasta_chromosomes($fasta_db);
}
$self->config->{_fasta_db} = $fasta_db;
}
return $self->config->{_fasta_db};
}
sub chromosome_synonyms {
my $self = shift;
my $file = shift;
if($file) {
open IN, $file or throw("ERROR: Could not read synonyms file $file: $!");
my $synonyms = $self->config->{_chromosome_synonyms} ||= {};
while(<IN>) {
chomp;
my @split = split(/\s+/, $_);
my $ref = shift @split;
foreach my $syn(@split) {
$synonyms->{$ref}->{$syn} = 1;
$synonyms->{$syn}->{$ref} = 1;
}
}
close IN;
}
return $self->config->{_chromosome_synonyms} ||= {};
}
# adds shortcuts to named params to this object
sub add_shortcuts {
my $self = shift;
return unless $self->config;
foreach my $param(map {ref($_) eq 'ARRAY' ? @$_ : $_} @_) {
throw("ERROR: add_shortcuts would overwrite value for \"$param\"\n") if exists($self->{$param});
$self->{$param} = $self->param($param);
}
}
# prints a status message to STDOUT
sub status_msg {
my $self = shift;
return if $self->param('quiet');
my $msg = (@_ ? (join "", @_) : "No message");
print get_time()." - ".$msg.($msg =~ /\n$/ ? "" : "\n");
}
# prints warning messages to STDERR or a log file
sub warning_msg {
my $self = shift;
my $text = shift || '';
$text = 'WARNING: '.$text unless $text =~ /^warn/i;
$text = $text."\n" unless $text =~ /\n$/;
# $self->config->{warning_count}++ if $self->config;
my $fh = $self->warning_fh;
print $fh $text;
unless($self->param('quiet')) {
warn($self->param('no_progress') ? $text : "\n$text");
}
}
# gets filehandle for use by warning_msg
sub warning_fh {
my $self = shift;
unless(exists($self->config->{warning_fh})) {
my $file = $self->param('warning_file');
my $fh;
if($file && $file =~ /^stderr$/i) {
$fh = *STDERR;
}
else {
$file ||= ($self->param('output_file') || 'vep').'_warnings.txt';
$self->param('warning_file', $file);
$fh = FileHandle->new();
$fh->open(">".$file) or throw("ERROR: Could not write to warnings file $file\n");
}
$self->config->{warning_fh} = $fh;
}
return $self->config->{warning_fh};
}
sub module_prefix {
my $self = shift;
my $prefix = 'Bio::EnsEMBL::VEP';
$prefix .= '::Haplo' if $self->param('haplo');
return $prefix;
}
1;