-
Notifications
You must be signed in to change notification settings - Fork 55
/
VEP.pm
7062 lines (5455 loc) · 222 KB
/
VEP.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
=head1 LICENSE
Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Copyright [2016-2023] 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::Variation::Utils::Sequence
#
#
=head1 NAME
Bio::EnsEMBL::Variation::Utils::VEP - Methods used by the Variant Effect Predictor
=head1 SYNOPSIS
use Bio::EnsEMBL::Variation::Utils::VEP qw(configure);
my $config = configure();
=head1 METHODS
=cut
use strict;
use warnings;
package Bio::EnsEMBL::Variation::Utils::VEP;
# module list
use Getopt::Long;
use FileHandle;
use File::Path qw(mkpath);
use Storable qw(nstore_fd fd_retrieve freeze thaw);
use Scalar::Util qw(weaken looks_like_number);
use Digest::MD5 qw(md5_hex);
use IO::Socket;
use IO::Select;
use Exporter;
my ($CAN_USE_TABIX_PM, $CAN_USE_TABIX_CL, $CAN_USE_CGI);
BEGIN {
if (eval { require Tabix; 1 }) {
$CAN_USE_TABIX_PM = 1;
}
elsif (`which tabix 2>&1` =~ /tabix$/) {
$CAN_USE_TABIX_CL = 1;
}
# use Sereal
eval q{ use Sereal; 1; };
if (eval { require CGI; 1 }) {
$CAN_USE_CGI = 1;
}
}
use Bio::EnsEMBL::Registry;
use Bio::EnsEMBL::Variation::VariationFeature;
use Bio::EnsEMBL::Variation::DBSQL::VariationFeatureAdaptor;
use Bio::EnsEMBL::Variation::Utils::VariationEffect qw(overlap);
use Bio::EnsEMBL::Utils::Sequence qw(reverse_comp);
use Bio::EnsEMBL::Variation::Utils::Sequence qw(unambiguity_code SO_variation_class);
use Bio::EnsEMBL::Variation::Utils::EnsEMBL2GFF3;
use Bio::EnsEMBL::Variation::StructuralVariationFeature;
use Bio::EnsEMBL::Variation::DBSQL::StructuralVariationFeatureAdaptor;
use Bio::EnsEMBL::Variation::TranscriptStructuralVariation;
use Bio::EnsEMBL::Variation::Source;
# we need to manually include all these modules for caching to work
use Bio::EnsEMBL::CoordSystem;
use Bio::EnsEMBL::Transcript;
use Bio::EnsEMBL::Translation;
use Bio::EnsEMBL::Exon;
use Bio::EnsEMBL::ProteinFeature;
use Bio::EnsEMBL::Analysis;
use Bio::EnsEMBL::Funcgen::RegulatoryFeature;
use Bio::EnsEMBL::Funcgen::MotifFeature;
use Bio::EnsEMBL::Funcgen::BindingMatrix;
use Bio::EnsEMBL::DBSQL::GeneAdaptor;
use Bio::EnsEMBL::DBSQL::SliceAdaptor;
use Bio::EnsEMBL::DBSQL::TranslationAdaptor;
use Bio::EnsEMBL::DBSQL::TranscriptAdaptor;
use Bio::EnsEMBL::DBSQL::MetaContainer;
use Bio::EnsEMBL::DBSQL::CoordSystemAdaptor;
use vars qw(@ISA @EXPORT_OK);
@ISA = qw(Exporter);
@EXPORT_OK = qw(
&_valid_region_regex
&check_format
&detect_format
&parse_line
&vf_to_consequences
&validate_vf
&read_cache_info
&get_version_data
&load_dumped_variation_cache
&load_dumped_transcript_cache
&prefetch_transcript_data
&get_all_consequences
&get_slice
&build_slice_cache
&build_full_cache
®ions_from_hash
&get_time
&debug
&convert_to_vcf
&progress
&end_progress
@REG_FEAT_TYPES
@OUTPUT_COLS
@VCF_COLS
@EXTRA_HEADERS
%COL_DESCS
@VEP_WEB_CONFIG
%FILTER_SHORTCUTS
@PICK_ORDER
);
our @OUTPUT_COLS = qw(
Uploaded_variation
Location
Allele
Gene
Feature
Feature_type
Consequence
cDNA_position
CDS_position
Protein_position
Amino_acids
Codons
Existing_variation
Extra
);
our @VCF_COLS = (
'Allele',
'Consequence',
'IMPACT',
'SYMBOL',
'Gene',
'Feature_type',
'Feature',
'BIOTYPE',
'EXON',
'INTRON',
'HGVSc',
'HGVSp',
'cDNA_position',
'CDS_position',
'Protein_position',
);
# define headers that would normally go in the extra field
# keyed on the config parameter used to turn it on
our @EXTRA_HEADERS = (
# general
{ flag => 'individual', cols => ['IND','ZYG'] },
{ flag => 'allele_number', cols => ['ALLELE_NUM'] },
{ flag => 'user', cols => ['IMPACT','DISTANCE','STRAND','FLAGS'] },
{ flag => 'flag_pick', cols => ['PICK'] },
{ flag => 'flag_pick_allele',cols => ['PICK'] },
{ flag => 'variant_class', cols => ['VARIANT_CLASS']},
{ flag => 'minimal', cols => ['MINIMISED']},
# gene-related
{ flag => 'symbol', cols => ['SYMBOL','SYMBOL_SOURCE','HGNC_ID'] },
{ flag => 'biotype', cols => ['BIOTYPE'] },
{ flag => 'canonical', cols => ['CANONICAL'] },
{ flag => 'tsl', cols => ['TSL']},
{ flag => 'appris', cols => ['APPRIS']},
{ flag => 'ccds', cols => ['CCDS'] },
{ flag => 'protein', cols => ['ENSP'] },
{ flag => 'uniprot', cols => ['SWISSPROT', 'TREMBL', 'UNIPARC'] },
{ flag => 'xref_refseq', cols => ['RefSeq'] },
{ flag => 'refseq', cols => ['REFSEQ_MATCH'] },
{ flag => 'merged', cols => ['REFSEQ_MATCH'] },
{ flag => 'gene_phenotype', cols => ['GENE_PHENO'] },
# non-synonymous predictions
{ flag => 'sift', cols => ['SIFT'] },
{ flag => 'polyphen', cols => ['PolyPhen'] },
# transcript/protein stuff
{ flag => 'numbers', cols => ['EXON','INTRON'] },
{ flag => 'domains', cols => ['DOMAINS'] },
{ flag => 'hgvs', cols => ['HGVSc','HGVSp','HGVS_OFFSET'] },
# frequency stuff
{ flag => 'gmaf', cols => ['GMAF'] },
{ flag => 'maf_1kg', cols => ['AFR_MAF','AMR_MAF','EAS_MAF','EUR_MAF','SAS_MAF'] },
{ flag => 'maf_esp', cols => ['AA_MAF','EA_MAF'] },
{ flag => 'maf_exac', cols => ['ExAC_MAF','ExAC_Adj_MAF','ExAC_AFR_MAF','ExAC_AMR_MAF','ExAC_EAS_MAF','ExAC_FIN_MAF','ExAC_NFE_MAF','ExAC_OTH_MAF','ExAC_SAS_MAF'] },
{ flag => 'check_frequency', cols => ['FREQS'] },
# misc variation stuff
{ flag => 'check_existing', cols => ['CLIN_SIG','SOMATIC','PHENO'] },
{ flag => 'pubmed', cols => ['PUBMED'] },
{ flag => 'check_svs', cols => ['SV'] },
# regulatory
{ flag => 'regulatory', cols => ['MOTIF_NAME','MOTIF_POS','HIGH_INF_POS','MOTIF_SCORE_CHANGE'] },
{ flag => 'cell_type', cols => ['CELL_TYPE'] },
);
our %COL_DESCS = (
'Uploaded_variation' => 'Identifier of uploaded variant',
'ID' => 'Identifier of uploaded variant',
'Location' => 'Location of variant in standard coordinate format (chr:start or chr:start-end)',
'Allele' => 'The variant allele used to calculate the consequence',
'Gene' => 'Stable ID of affected gene',
'Feature' => 'Stable ID of feature',
'Feature_type' => 'Type of feature - Transcript, RegulatoryFeature or MotifFeature',
'Consequence' => 'Consequence type',
'cDNA_position' => 'Relative position of base pair in cDNA sequence',
'CDS_position' => 'Relative position of base pair in coding sequence',
'Protein_position' => 'Relative position of amino acid in protein',
'Amino_acids' => 'Reference and variant amino acids',
'Codons' => 'Reference and variant codon sequence',
'Existing_variation' => 'Identifier(s) of co-located known variants',
'IMPACT' => 'Subjective impact classification of consequence type',
'CANONICAL' => 'Indicates if transcript is canonical for this gene',
'TSL' => 'Transcript support level',
'APPRIS' => 'Annotates alternatively spliced transcripts as primary or alternate based on a range of computational methods',
'CCDS' => 'Indicates if transcript is a CCDS transcript',
'SYMBOL' => 'Gene symbol (e.g. HGNC)',
'SYMBOL_SOURCE' => 'Source of gene symbol',
'SOURCE' => 'Source of transcript in merged gene set',
'HGNC_ID' => 'Stable identifer of HGNC gene symbol',
'ENSP' => 'Protein identifer',
'FLAGS' => 'Transcript quality flags',
'SWISSPROT' => 'Best match UniProtKB/Swiss-Prot accession',
'TREMBL' => 'Best match UniProtKB/TrEMBL accession',
'UNIPARC' => 'Best match UniParc accession',
'HGVSc' => 'HGVS coding sequence name',
'HGVSp' => 'HGVS protein sequence name',
'SIFT' => 'SIFT prediction and/or score',
'PolyPhen' => 'PolyPhen prediction and/or score',
'EXON' => 'Exon number(s) / total',
'INTRON' => 'Intron number(s) / total',
'DOMAINS' => 'The source and identifer of any overlapping protein domains',
'MOTIF_NAME' => 'The source and identifier of a transcription factor binding profile (TFBP) aligned at this position',
'MOTIF_POS' => 'The relative position of the variation in the aligned TFBP',
'HIGH_INF_POS' => 'A flag indicating if the variant falls in a high information position of the TFBP',
'MOTIF_SCORE_CHANGE' => 'The difference in motif score of the reference and variant sequences for the TFBP',
'CELL_TYPE' => 'List of cell types and classifications for regulatory feature',
'IND' => 'Individual name',
'ZYG' => 'Zygosity of individual genotype at this locus',
'SV' => 'IDs of overlapping structural variants',
'FREQS' => 'Frequencies of overlapping variants used in filtering',
'GMAF' => 'Minor allele and frequency of existing variant in 1000 Genomes combined population',
'AFR_MAF' => 'Frequency of existing variant in 1000 Genomes combined African population',
'AMR_MAF' => 'Frequency of existing variant in 1000 Genomes combined American population',
'ASN_MAF' => 'Frequency of existing variant in 1000 Genomes combined Asian population',
'EAS_MAF' => 'Frequency of existing variant in 1000 Genomes combined East Asian population',
'EUR_MAF' => 'Frequency of existing variant in 1000 Genomes combined European population',
'SAS_MAF' => 'Frequency of existing variant in 1000 Genomes combined South Asian population',
'AA_MAF' => 'Frequency of existing variant in NHLBI-ESP African American population',
'EA_MAF' => 'Frequency of existing variant in NHLBI-ESP European American population',
'ExAC_MAF', => 'Frequency of existing variant in ExAC combined population',
'ExAC_Adj_MAF', => 'Adjusted frequency of existing variant in ExAC combined population',
'ExAC_AFR_MAF', => 'Frequency of existing variant in ExAC African/American population',
'ExAC_AMR_MAF', => 'Frequency of existing variant in ExAC American population',
'ExAC_EAS_MAF', => 'Frequency of existing variant in ExAC East Asian population',
'ExAC_FIN_MAF', => 'Frequency of existing variant in ExAC Finnish population',
'ExAC_NFE_MAF', => 'Frequency of existing variant in ExAC Non-Finnish European population',
'ExAC_OTH_MAF', => 'Frequency of existing variant in ExAC combined other combined populations',
'ExAC_SAS_MAF', => 'Frequency of existing variant in ExAC South Asian population',
'DISTANCE' => 'Shortest distance from variant to transcript',
'CLIN_SIG' => 'ClinVar clinical significance of the dbSNP variant',
'BIOTYPE' => 'Biotype of transcript or regulatory feature',
'PUBMED' => 'Pubmed ID(s) of publications that cite existing variant',
'ALLELE_NUM' => 'Allele number from input; 0 is reference, 1 is first alternate etc',
'STRAND' => 'Strand of the feature (1/-1)',
'PICK' => 'Indicates if this consequence has been picked as the most severe',
'SOMATIC' => 'Somatic status of existing variant',
'REFSEQ_MATCH' => 'RefSeq transcript match status',
'VARIANT_CLASS' => 'SO variant class',
'PHENO' => 'Indicates if existing variant(s) is associated with a phenotype, disease or trait; multiple values correspond to multiple variants',
'GENE_PHENO' => 'Indicates if gene is associated with a phenotype, disease or trait',
'MINIMISED' => 'Alleles in this variant have been converted to minimal representation before consequence calculation',
'HGVS_OFFSET' => 'Indicates by how many bases the HGVS notations for this variant have been shifted',
);
our @REG_FEAT_TYPES = qw(
RegulatoryFeature
MotifFeature
);
our @VEP_WEB_CONFIG = qw(
format
check_existing
coding_only
core_type
symbol
protein
hgvs
terms
check_frequency
freq_filter
freq_gt_lt
freq_freq
freq_pop
filter_common
sift
polyphen
regulatory
);
our @VAR_CACHE_COLS = qw(
variation_name
failed
somatic
start
end
allele_string
strand
minor_allele
minor_allele_freq
clin_sig
phenotype_or_disease
);
our @PICK_ORDER = qw(mane_select mane_plus_clinical canonical appris tsl biotype ccds rank length ensembl refseq);
# parses a line of input, returns VF object(s)
sub parse_line {
my $config = shift;
my $line = shift;
# find out file format - will only do this on first line
if(!defined($config->{format}) || (defined($config->{format}) && $config->{format} eq 'guess')) {
$config->{format} = &detect_format($line);
debug("Detected format of input file as ", $config->{format}) unless defined($config->{quiet});
# HGVS and ID formats need DB
die("ERROR: Can't use ".uc($config->{format})." format in offline mode") if $config->{format} =~ /id|hgvs/ && defined($config->{offline});
}
# check that format is vcf when using --individual
die("ERROR: --individual only compatible with VCF input files\n") if defined($config->{individual}) && $config->{format} ne 'vcf';
my $parse_method = 'parse_'.$config->{format};
$parse_method =~ s/vep_//;
my $method_ref = \&$parse_method;
my $vfs = &$method_ref($config, $line);
$vfs = minimise_alleles($config, $vfs) if defined($config->{minimal});
$vfs = add_lrg_mappings($config, $vfs) if defined($config->{lrg});
$_->{_line} = $line for @$vfs;
return $vfs;
}
sub _valid_region_regex {
return qr/^([^:]+):(\d+)-(\d+)(:[-\+]?1)?[\/:]([a-z]{3,}|[ACGTN-]+)$/i;
}
# sub-routine to check format of string
sub check_format {
my @line = @_;
my $format;
# any changes here must be copied to the JavaScript file to run instant VEP:
# public-plugins/tools/htdocs/components/20_VEPForm.js
# region: chr21:10-10:1/A
if ( scalar @line == 1 && $line[0] =~ &_valid_region_regex() ) {
$format = 'region';
}
# SPDI: NC_000016.10:68684738:G:A
elsif ( scalar @line == 1 && $line[0] =~ /^(.*?\:){2}([^\:]+|)$/i ) {
$format = 'spdi';
}
# CAID: CA9985736
elsif ( scalar @line == 1 && $line[0] =~ /^CA\d{1,}$/i ) {
$format = 'caid';
}
# HGVS: ENST00000285667.3:c.1047_1048insC
elsif (
scalar @line == 1 &&
$line[0] =~ /^([^\:]+)\:.*?([cgmrp]?)\.?([\*\-0-9]+.*)$/i
) {
$format = 'hgvs';
}
# variant identifier: rs123456
elsif ( scalar @line == 1 ) {
$format = 'id';
}
# VCF: 20 14370 rs6054257 G A 29 0 NS=58;DP=258;AF=0.786;DB;H2 GT:GQ:DP:HQ
elsif (
$line[0] =~ /(chr)?\w+/ &&
$line[1] =~ /^\d+$/ &&
$line[3] && $line[3] =~ /^[ACGTN\-\.]+$/i &&
$line[4]
) {
$format = 'vcf';
}
# ensembl: 20 14370 14370 A/G +
elsif (
$line[0] =~ /\w+/ &&
$line[1] =~ /^\d+$/ &&
$line[2] && $line[2] =~ /^\d+$/ &&
$line[3] && $line[3] =~ /([a-z]{2,})|([ACGTN-]+\/[ACGTN-]+)/i
) {
$format = 'ensembl';
}
return $format;
}
# sub-routine to detect format of input
sub detect_format {
my $line = shift;
my @data = split /\s+/, $line;
my $format = &check_format(@data);
die "ERROR: Could not detect input file format\n" unless $format;
return $format;
}
# parse a line of Ensembl format input into a variation feature object
sub parse_ensembl {
my $config = shift;
my $line = shift;
my ($chr, $start, $end, $allele_string, $strand, $var_name) = split /\s+/, $line;
# simple validity check
unless($chr && $start && $end && $allele_string) {
warning_msg($config, "Invalid input formatting on line ".$config->{line_number});
return [];
}
$strand = 1 if !defined($strand);
my $vf;
# sv?
if($allele_string !~ /\//) {
my $so_term;
# convert to SO term
my %terms = (
INS => 'insertion',
DEL => 'deletion',
TDUP => 'tandem_duplication',
DUP => 'duplication'
);
$so_term = defined $terms{$allele_string} ? $terms{$allele_string} : $allele_string;
$vf = Bio::EnsEMBL::Variation::StructuralVariationFeature->new_fast({
start => $start,
end => $end,
strand => $strand =~ /\-/ ? -1 : 1,
adaptor => $config->{svfa},
variation_name => $var_name,
chr => $chr,
class_SO_term => $so_term,
});
}
# normal vf
else {
$vf = Bio::EnsEMBL::Variation::VariationFeature->new_fast({
start => $start,
end => $end,
allele_string => $allele_string,
strand => $strand =~ /\-/ ? -1 : 1,
map_weight => 1,
adaptor => $config->{vfa},
variation_name => $var_name,
chr => $chr,
});
}
return [$vf];
}
# parse a line of VCF input into a variation feature object
sub parse_vcf {
my $config = shift;
my $line = shift;
my @data = split /\s+/, $line;
# get relevant data
my ($chr, $start, $end, $ref, $alt) = ($data[0], $data[1], $data[1], $data[3], $data[4]);
# simple validity check
unless($chr && $start && $end && $ref && $alt) {
warning_msg($config, "Invalid input formatting on line ".$config->{line_number});
return [];
}
# non-variant
my $non_variant = 0;
if($alt eq '.') {
if(defined($config->{allow_non_variant})) {
$non_variant = 1;
}
else {
return [];
}
}
# some VCF files have a GRCh37 pos defined in GP flag in INFO column
# if user has requested, we can use that as the position instead
if(defined $config->{gp}) {
$chr = undef;
$start = undef;
foreach my $pair(split ';', $data[7]) {
my ($key, $value) = split '=', $pair;
if($key eq 'GP') {
($chr, $start) = split ':', $value;
$end = $start;
}
}
unless(defined($chr) and defined($start)) {
warning_msg($config, "No GP flag found in INFO column on line ".$config->{line_number});
return [];
}
}
# adjust end coord
$end += (length($ref) - 1);
# structural variation
if(
$ref.$alt !~ /^[ACGT\,]+$/ &&
(
(defined($data[7]) && $data[7] =~ /SVTYPE/) ||
$alt =~ /[\<|\[]^\*[\]\>]/
)
) {
# parse INFO field
my %info = ();
foreach my $bit(split ';', ($data[7] || '')) {
my ($key, $value) = split '=', $bit;
$info{$key} = $value;
}
# like indels, SVs have the base before included for reference
$start++;
# work out the end coord
if(defined($info{END})) {
$end = $info{END};
}
elsif(defined($info{SVLEN})) {
$end = $start + abs($info{SVLEN}) - 1;
}
# check for imprecise breakpoints
my ($min_start, $max_start, $min_end, $max_end);
if(defined($info{CIPOS})) {
my ($low, $high) = split ',', $info{CIPOS};
$min_start = $start + $low;
$max_start = $start + $high;
}
if(defined($info{CIEND})) {
my ($low, $high) = split ',', $info{CIEND};
$min_end = $end + $low;
$max_end = $end + $high;
}
# get type
my $type;
if($alt =~ /\<|\[|\]|\>/) {
$type = $alt;
$type =~ s/\<|\>//g;
$type =~ s/\:.+//g;
if($start >= $end && $type =~ /del/i) {
warning_msg($config, "WARNING: VCF line on line ".$config->{line_number}." looks incomplete, skipping:\n$line\n");
return [];
}
}
else {
$type = $info{SVTYPE};
}
my $so_term;
if(defined($type)) {
# convert to SO term
my %terms = (
INS => 'insertion',
DEL => 'deletion',
TDUP => 'tandem_duplication',
DUP => 'duplication'
);
$so_term = defined $terms{$type} ? $terms{$type} : $type;
}
my $svf = Bio::EnsEMBL::Variation::StructuralVariationFeature->new_fast({
start => $start,
inner_start => $max_start,
outer_start => $min_start,
end => $end,
inner_end => $min_end,
outer_end => $max_end,
strand => 1,
adaptor => $config->{svfa},
variation_name => $data[2] eq '.' ? undef : $data[2],
chr => $chr,
class_SO_term => $so_term,
});
return [$svf];
}
# normal variation
else {
# find out if any of the alt alleles make this an insertion or a deletion
my ($is_indel, $is_sub, $ins_count, $total_count);
foreach my $alt_allele(split ',', $alt) {
$is_indel = 1 if $alt_allele =~ /^[DI]/;
$is_indel = 1 if length($alt_allele) != length($ref);
$is_sub = 1 if length($alt_allele) == length($ref);
$ins_count++ if length($alt_allele) > length($ref);
$total_count++;
}
# multiple alt alleles?
if($alt =~ /\,/) {
if($is_indel) {
my @alts;
# find out if all the alts start with the same base
# ignore "*"-types
my %first_bases = map {substr($_, 0, 1) => 1} grep {!/\*/} ($ref, split(',', $alt));
if(scalar keys %first_bases == 1) {
$ref = substr($ref, 1) || '-';
$start++;
foreach my $alt_allele(split ',', $alt) {
$alt_allele = substr($alt_allele, 1) unless $alt_allele =~ /\*/;
$alt_allele = '-' if $alt_allele eq '';
push @alts, $alt_allele;
}
}
else {
push @alts, split(',', $alt);
}
$alt = join "/", @alts;
}
else {
# for substitutions we just need to replace ',' with '/' in $alt
$alt =~ s/\,/\//g;
}
}
elsif($is_indel) {
# insertion or deletion (VCF 4+)
if(substr($ref, 0, 1) eq substr($alt, 0, 1)) {
# chop off first base
$ref = substr($ref, 1) || '-';
$alt = substr($alt, 1) || '-';
$start++;
}
}
my $original_alt = $alt;
# create VF object
my $vf = Bio::EnsEMBL::Variation::VariationFeature->new_fast({
start => $start,
end => $end,
allele_string => $non_variant ? $ref : $ref.'/'.$alt,
strand => 1,
map_weight => 1,
adaptor => $config->{vfa},
variation_name => $data[2] eq '.' ? undef : $data[2],
chr => $chr,
});
# flag as non-variant
$vf->{non_variant} = 1 if $non_variant;
# individuals?
if(defined($config->{individual})) {
my @alleles = split '\/', $ref.'/'.$original_alt;
my @return;
foreach my $ind(keys %{$config->{ind_cols}}) {
# get alleles present in this individual
my @bits;
my $gt = (split ':', $data[$config->{ind_cols}->{$ind}])[0];
my $phased = ($gt =~ /\|/ ? 1 : 0);
foreach my $bit(split /\||\/|\\/, $gt) {
push @bits, $alleles[$bit] unless $bit eq '.';
}
# shallow copy VF
my $vf_copy = { %$vf };
bless $vf_copy, ref($vf);
# get non-refs, remembering to exclude "*"-types
my %non_ref = map {$_ => 1} grep {$_ ne $ref && $_ !~ /\*/} @bits;
# construct allele_string
if(scalar keys %non_ref) {
$vf_copy->{allele_string} = $ref."/".(join "/", keys %non_ref);
}
else {
$vf_copy->{allele_string} = $ref;
$vf_copy->{hom_ref} = 1;
if(defined($config->{process_ref_homs})) {
$vf_copy->{allele_string} .= "/".$ref;
}
else {
$vf_copy->{non_variant} = 1;
}
}
# store phasing info
$vf_copy->{phased} = defined($config->{phased} ? 1 : $phased);
# store GT
$vf_copy->{genotype} = \@bits;
# store individual name
$vf_copy->{individual} = $ind;
push @return, $vf_copy;
}
return \@return;
}
else {
return [$vf];
}
}
}
# parse a line of HGVS input into a variation feature object
sub parse_hgvs {
my $config = shift;
my $line = shift;
# simple validity check
unless($line) {
warning_msg($config, "Invalid input formatting on line ".$config->{line_number});
return [];
}
my $vf;
# not all hgvs notations are supported yet, so we have to wrap it in an eval
eval { $vf = $config->{vfa}->fetch_by_hgvs_notation($line, $config->{sa}, $config->{ta}) };
if((!defined($vf) || (defined $@ && length($@) > 1)) && defined($config->{coordinator})) {
eval { $vf = $config->{vfa}->fetch_by_hgvs_notation($line, $config->{ofsa}, $config->{ofta}) };
}
if(!defined($vf) || (defined $@ && length($@) > 1)) {
warning_msg($config, "WARNING: Unable to parse HGVS notation \'$line\'\n$@");
return [];
}
# get whole chromosome slice
my $slice = $vf->slice->adaptor->fetch_by_region($vf->slice->coord_system->name, $vf->slice->seq_region_name);
$vf = $vf->transfer($slice);
# name it after the HGVS
$vf->{variation_name} = $line;
# add chr attrib
$vf->{chr} = $vf->slice->seq_region_name;
return [$vf];
}
# parse a variation identifier e.g. a dbSNP rsID
sub parse_id {
my $config = shift;
my $line = shift;
# simple validity check
unless($line) {
warning_msg($config, "Invalid input formatting on line ".$config->{line_number});
return [];
}
# tell adaptor to fetch failed variants
# but store state to restore afterwards
my $prev = $config->{va}->db->include_failed_variations;
$config->{va}->db->include_failed_variations(1);
my $v_obj = $config->{va}->fetch_by_name($line);
return [] unless defined $v_obj;
my @vfs = @{$v_obj->get_all_VariationFeatures};
for(@vfs) {
delete $_->{dbID};
delete $_->{overlap_consequences};
$_->{chr} = $_->seq_region_name;
$config->{slice_cache}->{$_->{chr}} = $_->slice;
$_->{variation_name} = $line;
}
# restore state
$config->{va}->db->include_failed_variations($prev);
return \@vfs;
}
# converts to VCF format
sub convert_to_vcf {
my $config = shift;
my $vf = shift;
# look for imbalance in the allele string
if($vf->isa('Bio::EnsEMBL::Variation::VariationFeature')) {
my %allele_lengths;
my @alleles = split '\/', $vf->allele_string;
map {reverse_comp(\$_)} @alleles if $vf->strand < 0;
foreach my $allele(@alleles) {
$allele =~ s/\-//g;
$allele_lengths{length($allele)} = 1;
}
# in/del/unbalanced
if(scalar keys %allele_lengths > 1) {
# we need the ref base before the variation
# default to N in case we can't get it
my $prev_base = 'N';
if(defined($vf->slice) && ref($vf->slice) eq 'Bio::EnsEMBL::Slice') {
my $slice = $vf->slice->sub_Slice($vf->start - 1, $vf->start - 1);
$prev_base = $slice->seq if defined($slice);
}
for my $i(0..$#alleles) {
$alleles[$i] =~ s/\-//g;
$alleles[$i] = $prev_base.$alleles[$i];
}
return [
$vf->{chr} || $vf->seq_region_name,
$vf->start - 1,
$vf->variation_name,
shift @alleles,
(join ",", @alleles),
'.', '.', '.'
];
}
# balanced sub
else {
return [
$vf->{chr} || $vf->seq_region_name,
$vf->start,
$vf->variation_name,
shift @alleles,
(join ",", @alleles),
'.', '.', '.'
];
}
}
# SV
else {
# convert to SO term
my %terms = (
'insertion' => 'INS',
'deletion' => 'DEL',
'tandem_duplication' => 'TDUP',
'duplication' => 'DUP'
);
my $alt = '<'.($terms{$vf->class_SO_term} || $vf->class_SO_term).'>';
return [
$vf->{chr} || $vf->seq_region_name,
$vf->start,
$vf->variation_name,
'.',
$alt,
'.', '.', '.'
];
}
}
# tries to map a VF to the LRG coordinate system
sub add_lrg_mappings {
my $config = shift;
my $vfs = shift;
my @new_vfs;
foreach my $vf(@$vfs) {
# add the unmapped VF to the array
push @new_vfs, $vf;
# make sure the VF has an attached slice
$vf->{slice} ||= get_slice($config, $vf->{chr}, undef, 1);
next unless defined($vf->{slice});
# transform LRG <-> chromosome
my $new_vf = $vf->transform($vf->{slice}->coord_system->name eq 'lrg' ? 'chromosome' : 'lrg');
# add it to the array if transformation worked
if(defined($new_vf)) {
# update new VF's chr entry
$new_vf->{chr} = $new_vf->seq_region_name;
push @new_vfs, $new_vf;
}
}
return \@new_vfs;
}
sub minimise_alleles {
my $config = shift;
my $vfs = shift;
my @new_vfs;
foreach my $vf(@$vfs) {
# skip VFs with more than one alt
# they get taken care of later by split_variants/rejoin_variants
if(!$vf->{allele_string} || $vf->{allele_string} =~ /.+\/.+\/.+/ || $vf->{allele_string} !~ /.+\/.+/) {
push @new_vfs, $vf;
}