-
Notifications
You must be signed in to change notification settings - Fork 117
/
G2P.pm
2181 lines (1929 loc) · 85.1 KB
/
G2P.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-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 <https://www.ensembl.org/info/about/contact/index.html>
=cut
=head1 NAME
G2P
=head1 SYNOPSIS
mv G2P.pm ~/.vep/Plugins
./vep -i variations.vcf --plugin G2P,file=/path/to/G2P.csv
=head1 DESCRIPTION
A VEP plugin that uses G2P allelic requirements to assess variants in genes
for potential phenotype involvement.
The plugin has multiple configuration options, though minimally requires only
the CSV file of G2P data.
This Plugin is available for GRCh38 and GRCh37.
For further information see:
Thormann A, Halachev M, McLaren W, et al. Flexible and scalable diagnostic filtering of genomic variants using G2P with Ensembl VEP.
Nature Communications. 2019 May;10(1):2373. doi:10.1038/s41467-019-10016-3. PMID: 31147538; PMCID: PMC6542828.
Options are passed to the plugin as key=value pairs, (defaults in parentheses):
file : Path to G2P data file. The file needs to be uncompressed.
- Download from https://www.ebi.ac.uk/gene2phenotype/downloads
- Download from PanelApp
variant_include_list : A list of variants to include even if variants do not pass allele
frequency filtering. The include list needs to be a sorted, bgzipped and
tabixed VCF file.
af_monoallelic : maximum allele frequency for inclusion for monoallelic genes (0.0001)
af_biallelic : maximum allele frequency for inclusion for biallelic genes (0.005)
confidence_levels : Confidence levels include: definitive, strong, moderate, limited
Former confidence terms are still supported: confirmed, probable, possible, both RD and IF.
Separate multiple values with '&'.
https://www.ebi.ac.uk/gene2phenotype/terminology
Default levels are confirmed and probable.
all_confidence_levels : Set to 1 to include all confidence levels
Setting the value to 1 will overwrite any confidence levels provided with the
confidence_levels option.
af_from_vcf : set value to 1 to include allele frequencies from VCF file.
Specifiy the list of reference populations to include with '--af_from_vcf_keys'
af_from_vcf_keys : VCF collections used for annotating variant alleles with observed
allele frequencies. Allele frequencies are retrieved from VCF files. If
af_from_vcf is set to 1 but no VCF collections are specified with '--af_from_vcf_keys'
all available VCF collections are included.
Available VCF collections: 'topmed', 'uk10k', 'gnomADe', 'gnomADe_r2.1.1', 'gnomADg', 'gnomADg_v3.1.2'.
Separate multiple values with '&'.
VCF collections contain the following populations:
* 'topmed' - TOPMed (available for GRCh37 and GRCh38).
* 'uk10k' - ALSPAC, TWINSUK (available for GRCh37 and GRCh38).
* 'gnomADe' & 'gnomADe_r2.1.1' - gnomADe:AFR, gnomADe:ALL, gnomADe:AMR, gnomADe:ASJ, gnomADe:EAS, gnomADe:FIN, gnomADe:NFE, gnomADe:OTH, gnomADe:SAS (for GRCh37 and GRCh38 respectively).
* 'gnomADg' & 'gnomADg_v3.1.2' - gnomADg:AFR, gnomADg:ALL, gnomADg:AMR, gnomADg:ASJ, gnomADg:EAS, gnomADg:FIN, gnomADg:NFE, gnomADg:OTH (for GRCh37 and GRCh38 respectively).
Need to use 'af_from_vcf' parameter to use this option.
default_af : default frequency of the input variant if no frequency data is
found (0). This determines whether such variants are included;
the value of 0 forces variants with no frequency data to be
included as this is considered equivalent to having a frequency
of 0. Set to 1 (or any value higher than 'af') to exclude them.
types : SO consequence types to include. Separate multiple values with '&'
(splice_donor_variant, splice_acceptor_variant, stop_gained,
frameshift_variant, stop_lost, initiator_codon_variant,
inframe_insertion, inframe_deletion,missense_variant,
coding_sequence_variant, start_lost,transcript_ablation,
transcript_amplification, protein_altering_variant)
log_dir : write stats to log files in log_dir
txt_report : write all G2P complete genes and attributes to txt file
html_report : write all G2P complete genes and attributes to html file
filter_by_gene_symbol : set to 1 if filter by gene symbol.
Do not set if filtering by HGNC_id.
This option is set to 1 when using PanelApp files.
only_mane : set to 1 to ignore transcripts that are not MANE
N/B - Information may be lost if this option is used.
For more information - https://www.ebi.ac.uk/gene2phenotype/g2p_vep_plugin
Example:
--plugin G2P,file=G2P.csv,af_monoallelic=0.05,types=stop_gained&frameshift_variant
--plugin G2P,file=G2P.csv,af_monoallelic=0.05,af_from_vcf=1
--plugin G2P,file=G2P.csv,af_from_vcf=1,af_from_vcf_keys='topmed&gnomADe_r2.1.1'
--plugin G2P,file=G2P.csv,af_from_vcf=1,af_from_vcf_keys='topmed&gnomADe_r2.1.1',confidence_levels='confirmed&probable&both RD and IF'
--plugin G2P,file=G2P.csv
=cut
package G2P;
use strict;
use warnings;
use Cwd;
use Scalar::Util qw(looks_like_number);
use FileHandle;
use Text::CSV;
use Bio::EnsEMBL::Utils::Sequence qw(reverse_comp);
use Bio::EnsEMBL::Variation::Utils::Sequence qw(get_matched_variant_alleles);
use Bio::EnsEMBL::Variation::Utils::VEP qw(parse_line);
use Bio::EnsEMBL::Variation::DBSQL::VCFCollectionAdaptor;
use Bio::EnsEMBL::Variation::Utils::BaseVepPlugin;
use base qw(Bio::EnsEMBL::Variation::Utils::BaseVepTabixPlugin);
use List::Util qw(any);
our $CAN_USE_HTS_PM;
BEGIN {
if (eval { require Bio::DB::HTS::Tabix; 1 }) {
$CAN_USE_HTS_PM = 1;
}
}
my %DEFAULTS = (
# vars must have a frequency <= to this to pass
af_monoallelic => 0.0001,
af_biallelic => 0.005,
af_keys => [qw(AA AFR AMR EA EAS EUR SAS gnomAD gnomAD_AFR gnomAD_AMR gnomAD_ASJ gnomAD_EAS gnomAD_FIN gnomAD_NFE gnomAD_OTH gnomAD_SAS
gnomADg gnomADg_AFR gnomADg_AMR gnomADg_ASJ gnomADg_EAS gnomADg_FIN gnomADg_NFE gnomADg_OTH gnomADg_SAS
gnomADe gnomADe_AFR gnomADe_AMR gnomADe_ASJ gnomADe_EAS gnomADe_FIN gnomADe_NFE gnomADe_OTH gnomADe_SAS)],
af_from_vcf_keys => [qw(uk10k topmed gnomADe gnomADe_r2.1.1 gnomADg gnomADg_v3.1.2)],
# if no MAF data is found, default to 0
# this means absence of MAF data is considered equivalent to MAF=0
# set to 1 to do the "opposite", i.e. exclude variants with no MAF data
default_af => 0,
# adding new confidence levels based on the new terminology
confidence_levels => [qw(confirmed probable definitive strong moderate)],
# only include variants with these consequence types
# currently not ontology-resolved, exact term matches only
types => {map {$_ => 1} qw(splice_donor_variant splice_acceptor_variant stop_gained frameshift_variant stop_lost initiator_codon_variant inframe_insertion inframe_deletion missense_variant coding_sequence_variant start_lost transcript_ablation transcript_amplification protein_altering_variant)},
);
my $af_key_2_population_name = {
minor_allele_freq => 'global allele frequency (AF) from 1000 Genomes Phase 3 data',
AFR => '1000GENOMES:phase_3:AFR',
AMR => '1000GENOMES:phase_3:AMR',
EAS => '1000GENOMES:phase_3:EAS',
EUR => '1000GENOMES:phase_3:EUR',
SAS => '1000GENOMES:phase_3:SAS',
AA => 'Exome Sequencing Project 6500:African_American',
EA => 'Exome Sequencing Project 6500:European_American',
gnomAD => 'Genome Aggregation Database:Total',
gnomAD_AFR => 'Genome Aggregation Database exomes v2.1:African/African American',
gnomAD_AMR => 'Genome Aggregation Database exomes v2.1:Latino/Admixed American',
gnomAD_ASJ => 'Genome Aggregation Database exomes v2.1:Ashkenazi Jewish',
gnomAD_EAS => 'Genome Aggregation Database exomes v2.1:East Asian',
gnomAD_FIN => 'Genome Aggregation Database exomes v2.1:Finnish',
gnomAD_NFE => 'Genome Aggregation Database exomes v2.1:Non-Finnish European',
gnomAD_OTH => 'Genome Aggregation Database exomes v2.1:Other (population not assigned)',
gnomAD_SAS => 'Genome Aggregation Database exomes v2.1:South Asian',
};
my $allelic_requirements = {
'biallelic' => { af => 0.005, rules => {HET => 2, HOM => 1} },
'biallelic_autosomal' => { af => 0.005, rules => {HET => 2, HOM => 1} },
'biallelic_PAR' => { af => 0.005, rules => {HET => 2, HOM => 1} },
'monoallelic' => { af => 0.0001, rules => {HET => 1, HOM => 1} },
'monoallelic_autosomal' => => { af => 0.0001, rules => {HET => 1, HOM => 1} },
'hemizygous' => { af => 0.0001, rules => {HET => 1, HOM => 1} },
'hemizygous_biallelic' => { af => 0.0001, rules => {HET => 2, HOM => 1} },
'monoallelic_X_hem' => { af => 0.0001, rules => {HET => 1, HOM => 1} },
'monoallelic_Y_hem' => { af => 0.0001, rules => {HET => 1, HOM => 1} },
'x-linked dominant' => { af => 0.0001, rules => {HET => 1, HOM => 1} },
'monoallelic_X_het' => { af => 0.0001, rules => {HET => 1, HOM => 1} },
'x-linked over-dominance' => { af => 0.0001, rules => {HET => 1, HOM => 1} },
'mitochondrial' => { af => 0.0001, rules => {HET => 1, HOM => 1} },
'monoallelic_PAR' => { af => 0.0001, rules => {HET => 1, HOM => 1} },
};
my $supported_confidence_levels = {
'confirmed' => 1,
'definitive' => 1,
'probable' => 1,
'strong' => 1,
'moderate' => 1,
'possible' => 1,
'limited' => 1,
'both RD and IF' => 1,
};
my @allelic_requirement_terms = keys %$allelic_requirements;
# keys containing the assembly and the key to do a quick key and assembly lookup
my $afvcf_keys = {
"uk10k_GRCh37" => 1,
"uk10k_GRCh38" => 1,
"topmed_GRCh37" => 1,
"topmed_GRCh38" => 1,
"gnomADe_GRCh37" => 1,
"gnomADe_r2.1.1_GRCh38" => 1,
"gnomADg_GRCh37" => 1,
"gnomADg_v3.1.2_GRCh38" => 1
};
my $a_keys = {
"uk10k" => 1,
"topmed" => 1,
"gnomADe" => 1,
"gnomADe_r2.1.1" => 1,
"gnomADg" => 1,
"gnomADg_v3.1.2" => 1
};
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
# suppress warnings that the FeatureAdpators spit if using no_slice_cache
Bio::EnsEMBL::Utils::Exception::verbose(1999);
my $params = $self->params_to_hash();
my $file = '';
# user only supplied file as first param?
if (!keys %$params) {
$file = $self->params->[0];
$params->{file} = $file;
}
else {
$file = $params->{file};
open IN, "<", $file;
# supporting panelapp by always filtering by gene symbol if PanelApp file
while (<IN>){
$params->{filter_by_gene_symbol} = 1 if (/Model_Of_Inheritance/);
last;
}
close $file;
# process types
if ($params->{types}) {
$params->{types} = {map {$_ => 1} split(/[\;\&\|]/, $params->{types})};
}
# check af
foreach my $af (qw/af_monoallelic af_biallelic/) {
if($params->{$af}) {
die("ERROR: Invalid value for af: ".$params->{$af} . "\n") unless
looks_like_number($params->{$af}) && ($params->{$af} >= 0 && $params->{$af} <= 1)
}
my $ar = $af;
$ar =~ s/af_//;
$allelic_requirements->{$ar}->{af} = $params->{$af} if (defined $params->{$af});
}
$params->{af_keys} = \@{$DEFAULTS{af_keys}};
}
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
$year += 1900;
$mon++;
my $stamp = join('_', ($year, $mon, $mday, $hour, $min, $sec));
my $cwd_dir = getcwd;
my $new_log_dir = "$cwd_dir/g2p_log_dir\_$stamp";
my $log_dir = $params->{log_dir} || $new_log_dir;
if (!-d $log_dir) {
my $return = mkdir $log_dir, 0755;
die("ERROR: Couldn't create log_dir $log_dir $!\n") if (!$return);
$params->{log_dir} = $log_dir;
}
else{
opendir my $dh, $log_dir or die("ERROR: There was a problem opening the log_dir: $!\n");
my @check = grep {$_ ne '.' and $_ ne '..'} readdir $dh;
die("ERROR: The log directory ($log_dir) is not empty. You need to empty directory before using the plugin \n") if (scalar @check != 0);
closedir $dh;
$params->{log_dir} = $log_dir;
}
foreach my $report_type (qw/txt_report html_report/) {
if (!$params->{$report_type}) {
my $file_type = ($report_type eq 'txt_report') ? 'txt' : 'html';
$params->{$report_type} = $cwd_dir . "/$report_type\_$stamp.$file_type";
}
}
if ($params->{all_confidence_levels}) {
if ($params->{confidence_levels}) {
warn("Option all_confidence_levels set to 1 overwrites confidence levels provided with confidence_levels option.");
}
$params->{confidence_levels} = ['possible', @{$DEFAULTS{confidence_levels}}];
}
elsif ($params->{confidence_levels}) {
my @confidence_levels = ();
foreach my $confidence_level (split(/[\;\&\|]/, $params->{confidence_levels})) {
if (!$supported_confidence_levels->{$confidence_level}) {
die "$confidence_level is not a supported value for supported confidence levels. Supported values are: ", join(', ', keys %$supported_confidence_levels);
} else {
push @confidence_levels, $confidence_level;
push @confidence_levels, 'both DD and IF' if ($confidence_level eq 'both RD and IF'); # legacy support for using both DD and IF
}
}
if (scalar @confidence_levels > 0) {
$params->{confidence_levels} = \@confidence_levels;
}
}
if ($params->{af_from_vcf}) {
if ($CAN_USE_HTS_PM) {
my @vcf_collection_ids = ();
# adding a die if assembly is not used and af_from_vcf keys option is used
my $assembly = $self->{config}->{assembly};
die "Assembly needs to be defined to use af_from_vcf option" if (!defined ($assembly));
if ($params->{af_from_vcf_keys}) {
foreach my $key (split(/[\;\&\|]/, $params->{af_from_vcf_keys})) {
my $key_assembly = $key."_".$assembly;
if (!$afvcf_keys->{$key_assembly}){
# to die if key is not supported, checking with the key and the assembly
die "$key is not a supported key. Supported keys and assembly are: ", join(',', keys %$a_keys), ".\n
gnomADe and gnomADg is supported for assembly GRCh37 \n
gnomADe_r2.1.1 and gnomADg_v3.1.2 is supported for assembly GRCh38 \n" ;
}
else {
push @vcf_collection_ids, $key;
push @vcf_collection_ids, $key_assembly;
}
}
} else {
foreach my $key (@{$DEFAULTS{af_from_vcf_keys}}) {
push @vcf_collection_ids, $key;
push @vcf_collection_ids, "$key\_$assembly";
}
}
my $species = $self->{config}->{species};
my $reg = $self->{config}->{reg};
my $vca;
if (defined $self->{config}->{offline}) {
$vca = Bio::EnsEMBL::Variation::DBSQL::VCFCollectionAdaptor->new();
} else {
my $vdba = $reg->get_DBAdaptor($species, 'variation');
$vdba->dbc->reconnect_when_lost(1);
$vca = $vdba->get_VCFCollectionAdaptor;
$vca->db->use_vcf(2);
}
my $vcf_collections = $vca->fetch_all;
my @collections = ();
foreach my $vcf_collection (@$vcf_collections) {
$vcf_collection->use_db(0) if (defined $self->{config}->{offline});
my $vcf_collection_id = $vcf_collection->id;
if ($vcf_collection->assembly eq $assembly && grep {$_ =~ /$vcf_collection_id/i} @vcf_collection_ids) {
delete $vcf_collection->adaptor->{collections};
delete $vcf_collection->adaptor->{config};
my $description = $vcf_collection->description || $vcf_collection_id;
foreach my $population (@{$vcf_collection->get_all_Populations}) {
my $population_name = $population->name;
my $population_description = $population->description;
$af_key_2_population_name->{$population_name} = "$description $population_name $population_description";
}
push @collections, $vcf_collection;
}
}
warn "Couldn't find VCF collection ids for assembly " . $assembly if (!@collections);
$self->{config}->{vcf_collections} = \@collections;
$self->{config}->{use_vcf} = 1;
} else {
warn "Cannot get data from VCF without Bio::DB::HTS::Tabix";
}
}
if ($params->{variant_include_list}) {
if (! -f $params->{variant_include_list}) {
die "Variant include list (" . $params->{variant_include_list} . ") does not exist.";
}
$self->{_files} = [$params->{variant_include_list}];
}
if (defined($params->{filter_by_gene_symbol}) and $params->{filter_by_gene_symbol} != 1) {
$params->{filter_by_gene_symbol} = undef;
die "The option --filter_by_gene_symbol needs to be set to 1 \n";
}
if (defined($params->{only_mane}) and $params->{only_mane} != 1) {
$params->{only_mane} = undef;
die "The option only_mane needs to be set to 1 \n";
}
if (defined($params->{only_mane}) and $self->{config}->{assembly} ne "GRCh38") {
die "The option only_mane only works with GRCh38 assembly \n";
}
# copy in default params
$params->{$_} //= $DEFAULTS{$_} for keys %DEFAULTS;
$self->{user_params} = $params;
$self->{config}->{frequency_threshold} = _get_highest_frequency_threshold();
# read data from file
$self->{gene_data} = $self->read_gene_data_from_file($file);
$self->synonym_mappings();
$self->hgnc_mappings();
# force some config params
$self->{config}->{individual} //= ['all'];
$self->{config}->{symbol} = 1;
$self->{config}->{check_existing} = 1;
$self->{config}->{failed} = 1;
$self->{config}->{af} = 1;
$self->{config}->{af_1kg} = 1;
$self->{config}->{af_gnomad} = 1;
$self->{config}->{sift} = 'b';
$self->{config}->{polyphen} = 'b';
# tell VEP we have a cache so stuff gets shared/merged between forks
$self->{has_cache} = 1;
$self->{cache}->{g2p_in_vcf} = {};
return $self;
}
=head2 _get_highest_frequency_threshold
Description: Retrieve the highest allele frequency threshold across all defined allelic requirements.
This will speed up the filtering by allele frequency. As soon as we found a frequency
higher than this highest frequency the filtering fails and we don't need to consider
the variant further.
Returntype : Float $highest_frequency
Exceptions : None
Caller : General
Status : Stable
=cut
sub _get_highest_frequency_threshold {
my $highest_frequency = 0.0;
foreach my $ar (keys %$allelic_requirements) {
if ($allelic_requirements->{$ar}->{af} > $highest_frequency) {
$highest_frequency = $allelic_requirements->{$ar}->{af};
}
}
return $highest_frequency;
}
sub feature_types {
return ['Transcript'];
}
sub get_header_info {
my $self = shift;
return {
G2P_flag => 'Flags zygosity of valid variants for a G2P gene',
G2P_complete => 'Indicates this variant completes the allelic requirements for a G2P gene',
G2P_gene_req => 'MONO or BI depending on the context in which this gene has been explored',
};
}
=head2 run
Arg [1] : TranscriptVariationAllele $tva
Arg [2] : Hashref $line
Description: Filter input transcript variation allele on:
- G2P gene overlap
- variant consequence
- variant include list
- allele frequency
Based on the filtering results check if the allelic requirement is fulfilled and write results to hash.
Dump annnotations to a log file for generating TXT and HTML output files after VEP has finished.
Returntype : Hashref $results
Exceptions : None
Caller : General
Status : Stable
=cut
sub run {
my ($self, $tva, $line) = @_;
# only interested if we know the zygosity
my $zyg = defined($line->{Extra}) ? $line->{Extra}->{ZYG} : $line->{ZYG};
return {} unless $zyg;
return {} if ($self->{user_params}->{only_mane} && !$tva->transcript->is_mane);
# filter by G2P gene overlap
return {} if (!$self->gene_overlap_filtering($tva));
$self->set_variant_include_list_flag($tva);
# filter by variant consequence
return {} if (!$self->consequence_filtering($tva));
# filter by allele frequency
return {} if (!$self->frequency_filtering($tva));
# dump annotations for txt and html report files
$self->dump_vf_annotations($tva);
$self->dump_individual_annotations($tva, $zyg);
# check if transcript contains enough variants to fulfill the allelic requirement of the gene
my $G2P_complete = $self->is_g2p_complete($tva, $zyg);
my $G2P_flag = $self->is_valid_g2p_variant($tva, $zyg);
my $results = {};
$results->{G2P_complete} = $G2P_complete if ($G2P_complete);
$results->{G2P_flag} = $G2P_flag if ($G2P_flag);
return $results;
}
=head2 set_variant_include_list_flag
Arg [1] : TranscriptVariationAllele $tva
Description: Check if variant is part of the variant include list.
If the variant is on the variant include list then report
it in the result set regardless if the variant passed the
filtering by variant consequence and allele frequency.
Returntype : None
Exceptions : None
Caller : General
Status : Stable
=cut
sub set_variant_include_list_flag {
my $self = shift;
my $tva = shift;
return if (!$self->{user_params}->{variant_include_list});
my $vf = $tva->variation_feature;
my $allele = $tva->base_variation_feature->alt_alleles;
foreach (@{$self->get_data($vf->{chr}, $vf->{start} - 1, $vf->{end})}) {
my @vcf_alleles = split /\//, $_->allele_string;
my $ref_allele = shift @vcf_alleles;
my $matches = get_matched_variant_alleles(
{
ref => $vf->ref_allele_string,
alts => $allele,
pos => $vf->{start},
strand => $vf->strand
},
{
ref => $ref_allele,
alts => \@vcf_alleles,
pos => $_->{start},
}
);
if (scalar @$matches) {
my $vf_cache_name = $self->get_cache_name($vf);
$self->{g2p_vf_cache}->{$vf_cache_name}->{is_on_variant_include_list} = 1;
last;
}
}
}
=head2 is_valid_g2p_variant
Arg [1] : TranscriptVariationAllele $tva
Arg [2] : String $zygosity
Example : $valid_g2p_variant = $self->is_valid_g2p_variant($tva, 'HOM')
Description: Take all allelic requirements of the gene that overlap this variant
and check if the variant passes the frequency threshold filter where the threshold is defined
by the allelic requirement of the gene.
Concatenate results for several allelic requirements by ','.
Returntype : String for example "monoallelic=HOM"
Exceptions : None
Caller : General
Status : Stable
=cut
sub is_valid_g2p_variant {
my $self = shift;
my $tva = shift;
my $zyg = shift;
my $transcript = $tva->transcript;
my $gene_stable_id = get_gene_stable_id($transcript);
my @allelic_requirements = keys %{$self->{ar}->{$gene_stable_id}};
my @results = ();
foreach my $ar (@allelic_requirements) {
my $ar_rules = $allelic_requirements->{$ar};
my $af_threshold = $ar_rules->{af};
my $variants = $self->variants_filtered_by_frequency_threshold($af_threshold, [$self->{vf_cache_name}]);
if (scalar @$variants > 0) {
push @results, "$ar=$zyg";
}
}
return join(',', @results);
}
=head2 is_g2p_complete
Arg [1] : TranscriptVariationAllele $tva
Arg [2] : String $zygosity
Description: A G2P gene is considered complete if its allelic requirement is fulfilled.
Create a summary string which connects the fulfilled allelic_requirement and all variants which pass filtering.
Returntype : String $g2p_complete, for example "monoallelic=HET:7_941481_C/T&HET:7_929274_A/G,HOM:7_931481_C/T|biallelic=HOM:7_931481_C/T"
Exceptions : None
Caller : General
Status : Stable
=cut
sub is_g2p_complete {
my $self = shift;
my $tva = shift;
my $zyg = shift;
my $vf = $tva->base_variation_feature;
my $individual = $vf->{individual};
my $transcript = $tva->transcript;
my $gene_stable_id = get_gene_stable_id($transcript);
my $transcript_stable_id = $transcript->stable_id;
$self->{per_individual}->{$individual}->{$transcript_stable_id}->{$zyg}->{$self->{vf_cache_name}} = 1;
my @allelic_requirements = keys %{$self->{ar}->{$gene_stable_id}};
my @g2p_complete = ();
foreach my $ar (@allelic_requirements) {
my $zyg2var = $self->{per_individual}->{$individual}->{$transcript_stable_id};
my $filtered_zyg2var = $self->zyg2var_filtered_by_allelic_requirement_rule($ar, $zyg2var);
if (defined $filtered_zyg2var) {
my @filtered_variants = ();
foreach my $zyg (keys %$filtered_zyg2var) {
push @filtered_variants, join('&', map {"$zyg:$_"} @{$filtered_zyg2var->{$zyg}});
}
push @g2p_complete, "$ar=" . join(',', @filtered_variants);
}
}
return join('\|', @g2p_complete);
}
=head2 zyg2var_filtered_by_allelic_requirement_rule
Arg [1] : String $ar allelic requirement
Arg [2] : Hashref $zyg2var
Example : $zyg2var_filtered = $self->zyg2var_filtered_by_allelic_requirement_rule('biallelic', {
'HET' => {
'4_32941481_C/T' => 1,
'4_32929274_A/G' => 1
}
});
Description: Check if the variants fulfil the given allelic requirement. Variants are grouped by their
zygosity. The subroutine checks for each variant if the internally stored frequency for a variant
is lower than the allele frequency threshold defined by the allelic requirement. Then consider
the variants which pass frequency filtering and check if the number is sufficient to fulfil the
allelic requirement.
Returntype : Hashref $zyg2var_filtered: with zygosity as key and filtered variants in arrayref as value
For example: {
'HET' => [
'4_32941481_C/T',
'4_32929274_A/G'
]
};
Undef, if none of the variants pass the filtering
Exceptions : None
Caller : General
Status : Stable
=cut
sub zyg2var_filtered_by_allelic_requirement_rule {
my $self = shift;
my $ar = shift;
my $zyg2variants = shift;
my $ar_rules = $allelic_requirements->{$ar};
# allele frequency threshold for given allelic requirement
my $af_threshold = $ar_rules->{af};
# number of variants with a particular zygosity that need to pass filtering
# to fulfil allelic requirement
my $zyg2counts = $ar_rules->{rules};
my $results = {};
foreach my $zyg (keys %$zyg2counts) {
my $count = $zyg2counts->{$zyg};
my @all_variants = keys %{$zyg2variants->{$zyg}};
my $variants = $self->variants_filtered_by_frequency_threshold($af_threshold, \@all_variants);
if (scalar @$variants >= $count) {
$results->{$zyg} = $variants;
}
}
if (scalar keys %$results > 0) {
return $results;
} else {
return undef;
}
}
=head2 variants_filtered_by_frequency_threshold
Arg [1] : Float $af_threshold allele frequency threshold
Arg [2] : Arrayref $variants
Example : $variants_filtered = $self->variants_filtered_by_frequency_threshold(0.001, [
'4_32941481_C/T' => 1,
'4_32929274_A/G' => 1
]
);
Description: Check for each variant if the highest frequency that has been observed in any
population (stored internally under the highest_frequencies hash key for a variant) is lower
than the given allele frequency threshold. If yes, add the variant to the result set.
Also add the variant to the result set if the variant hasn't any observed allele frequencies
or if the variant is in the variant include list.
Returntype : Arrayref $variants_filtered
For example: [
'4_32941481_C/T' => 1,
'4_32929274_A/G' => 1
];
Exceptions : None
Caller : General
Status : Stable
=cut
sub variants_filtered_by_frequency_threshold {
my $self = shift;
my $af_threshold = shift;
my $variants = shift;
my @pass_variants = ();
foreach my $variant (@$variants) {
# get the highest frequency that has been observed for the variant
my $highest_frequency = $self->highest_frequency($variant);
if (! defined $highest_frequency || (defined $af_threshold && $highest_frequency <= $af_threshold) ||
$self->{g2p_vf_cache}->{$variant}->{is_on_variant_include_list}
) {
push @pass_variants, $variant;
}
}
return \@pass_variants;
}
=head2 gene_overlap_filtering
Arg [1] : TranscriptVariationAllele $tva
Example :
Description: returns 1 or 0 depending on if the gene is part of the input panel. If the gene is part of the panel we store
the allelic requirement in the internal cash under the name 'ar'. We also write G2P_gene_data and G2P_in_vcf
information to the log file. We call _dump_transcript_annotations and write transcript information to the log file.
Returntype : Boolean
Exceptions : None
Caller : run
Status : Stable
=cut
sub gene_overlap_filtering {
my $self = shift;
my $tva = shift;
my $transcript = $tva->transcript;
my $gene_stable_id = get_gene_stable_id($transcript);
my $pass_gene_overlap_filter = $self->{g2p_gene_cache}->{$gene_stable_id};
my @gene_xrefs = ();
if (! defined $pass_gene_overlap_filter && ! defined $self->{user_params}->{filter_by_gene_symbol}) {
my @gene_hgnc = split /:/, $transcript->{_gene_hgnc_id} if (defined $transcript->{_gene_hgnc_id});
my $hgnc_id = $gene_hgnc[1] if (@gene_hgnc);
if (defined $hgnc_id) {
foreach my $id (keys %{ $self->{hgnc_mapping} }) {
if ( $hgnc_id == $id) {
my $gene_symbol = $self->{hgnc_mapping}{$id};
my $gene_data = $self->gene_data($gene_symbol) if defined ($gene_symbol);
if (defined $gene_data) {
if (defined $gene_data->{'allelic requirement'} && scalar @{$gene_data->{'allelic requirement'}}) {
foreach my $ar (@{$gene_data->{'allelic requirement'}}) {
$self->{ar}->{$gene_stable_id}->{$ar} = 1;
}
$self->write_report('G2P_gene_data', $gene_stable_id, $gene_data, $gene_data->{'gene_xrefs'}, $gene_data->{'HGNC'}, $gene_data->{'confidence_category'}, $gene_data->{'confidence_value'} );
}
$self->write_report('G2P_in_vcf', $gene_stable_id);
$pass_gene_overlap_filter = 1;
last;
}
}
}
}
$self->{g2p_gene_cache}->{$gene_stable_id} = $pass_gene_overlap_filter;
}
if (! defined $pass_gene_overlap_filter && defined $self->{user_params}->{filter_by_gene_symbol} == 1) {
my $gene_symbol = $transcript->{_gene_symbol} || $transcript->{_gene_hgnc};
$pass_gene_overlap_filter = 0;
foreach my $gene_id ($gene_symbol, $gene_stable_id) {
my $gene_data = $self->gene_data($gene_id) if (defined $gene_id);
if (defined $gene_data) {
if (defined $gene_data->{'allelic requirement'} && scalar @{$gene_data->{'allelic requirement'}}) {
foreach my $ar (@{$gene_data->{'allelic requirement'}}) {
$self->{ar}->{$gene_stable_id}->{$ar} = 1;
}
$self->write_report('G2P_gene_data', $gene_stable_id, $gene_data, $gene_data->{'gene_xrefs'}, undef, $gene_data->{'confidence_category'}, $gene_data->{'confidence_value'});
}
$self->write_report('G2P_in_vcf', $gene_stable_id);
$pass_gene_overlap_filter = 1;
last;
}
}
$self->{g2p_gene_cache}->{$gene_stable_id} = $pass_gene_overlap_filter;
}
$self->_dump_transcript_annotations($transcript) if ($pass_gene_overlap_filter);
return $self->{g2p_gene_cache}->{$gene_stable_id};
}
=head2 consequence_filtering
Arg [1] : TranscriptVariationAllele $tva
Description: returns 1 or 0 depending on if the variant passes consequence filtering.
If the variant is on the variant include list we return 1, regardless
if the variant consequence is defined in types.
Returntype : Boolean
Exceptions : None
Caller : General
Status : Stable
=cut
sub consequence_filtering {
my $self = shift;
my $tva = shift;
my $vf = $tva->base_variation_feature;
my $vf_cache_name = $self->get_cache_name($vf);
return ((grep { $self->{user_params}->{types}->{$_->SO_term} } @{$tva->get_all_OverlapConsequences}) ||
$self->{g2p_vf_cache}->{$vf_cache_name}->{is_on_variant_include_list});
}
=head2 _dump_transcript_annotations
Arg [1] : Transcript $transcript
Description: Write G2P_transcript_data to log file.
Returntype : None
Exceptions : None
Caller : General
Status : Stable
=cut
sub _dump_transcript_annotations {
my $self = shift;
my $transcript = shift;
my $transcript_stable_id = $transcript->stable_id;
if (!defined $self->{g2p_transcript_cache}->{$transcript_stable_id}) {
my $gene_stable_id = get_gene_stable_id($transcript);
if ($transcript->is_canonical) {
$self->write_report('G2P_transcript_data', "$gene_stable_id\t$transcript_stable_id\tis_canonical");
}
$self->{g2p_transcript_cache}->{$transcript_stable_id} = 1;
}
}
=head2 get_cache_name
Arg [1] : VariationFeature $vf
Description: Create a variant identifier for internal use which is stored in the internal cache
Returntype : String $vf_cache_name for example 19_8412862_G/A
Exceptions : None
Caller : General
Status : Stable
=cut
sub get_cache_name {
my $self = shift;
my $vf = shift;
my $cache_name = ($vf->{original_chr} || $vf->{chr}) . '_' . $vf->{start} . '_' . ($vf->{allele_string} || $vf->{class_SO_term});
return $cache_name;
}
=head2 get_gene_stable_id
Arg [1] : Transcript $transcript
Description: Retrives the Ensembl or RefSeq gene stable id from the transcript object
Returntype : String $gene_stable_id
Exceptions : None
Caller : General
Status : Stable
=cut
sub get_gene_stable_id {
my $transcript = shift;
return $transcript->{_gene}->stable_id;
}
=head2 frequency_filtering
Arg [1] : TranscriptVariationAllele $tva
Description: returns 1 or 0 depending on if the variant passes frequency filtering.
We consider allele frequencies from the VEP cache and VCF files for filtering.
Returntype : Boolean
Exceptions : None
Caller : General
Status : Stable
=cut
sub frequency_filtering {
my $self = shift;
my $tva = shift;
my $vf = $tva->base_variation_feature;
# Set up caching to avoid looking up frequencies for each overlapping transcript
my $vf_cache_name = $self->get_cache_name($vf);
$self->{vf_cache_name} = $vf_cache_name;
$self->{g2p_vf_cache} = {} if (!defined $self->{g2p_vf_cache}->{$vf_cache_name});
# Retrieve cached result
my $pass_frequency_filter = $self->{g2p_vf_cache}->{$vf_cache_name}->{pass_frequency_filter};
return $pass_frequency_filter if (defined $pass_frequency_filter);
# Check frequencies from cache files first
$pass_frequency_filter = $self->_vep_cache_frequency_filtering($tva);
# Check frequencies from VCF files if user is providing use_vcf flag
if ($pass_frequency_filter && $self->{config}->{use_vcf}) {
$pass_frequency_filter = $self->_vcf_frequency_filtering($tva);
}
$self->{g2p_vf_cache}->{$vf_cache_name}->{pass_frequency_filter} = $pass_frequency_filter;
return $self->{g2p_vf_cache}->{$vf_cache_name}->{pass_frequency_filter};
}
=head2 _vep_cache_frequency_filtering
Arg [1] : TranscriptVariationAllele $tva
Description: Returns 1 or 0 depending on if the variant passes frequency filtering where allele frequencies come from the VEP cache.
Return 1 if no observed allele frequencies exist for the given variant which means that the filtering passes.
Returntype : Boolean
Exceptions : None
Caller : General
Status : Stable
=cut
sub _vep_cache_frequency_filtering {
my $self = shift;
my $tva = shift;
my $allele = $tva->base_variation_feature->alt_alleles;
my $vf = $tva->base_variation_feature;
my $frequency_threshold = $self->{config}->{frequency_threshold};
my $existing = $vf->{existing}; # Get existing variants from cache file which are stored on VF level
my @keys = @{$self->{user_params}->{af_keys}}; # Consider user defined list of af keys
my $dumped_annotations = 0; # Indicates if existing annotations have already been dumped for txt and html report files
my $vf_cache_name = $self->{vf_cache_name};
foreach my $existing_var (@$existing) {
my @frequencies = grep defined, @{$existing_var}{@keys};
if ($existing_var->{matched_alleles}) { # Get matched alleles from input variant and existing variant, in case input variant was normalized to match variant from cache file
$allele = $existing_var->{matched_alleles}[0]->{b_allele};
}
next if (!@frequencies);
if ($self->_exceeds_frequency_threshold(\@frequencies, $allele, $frequency_threshold) && !$self->{g2p_vf_cache}->{$vf_cache_name}->{is_on_variant_include_list}) {
return 0; # Return 0 (failed filtering) if frequencies exceed threshold and variant is not on variant_include_list
} else {
# Dump annotations for txt and html report files
$self->_dump_existing_vf_frequencies($existing_var, $allele);
$self->_dump_existing_vf_annotations($existing_var);
$dumped_annotations = 1;
}
}
# If we get to this point it means that there were no frequencies for the input variant in the cache files
# and we pass the filtering step.
# We need to dump 'empty' annotations for such variants to indicate that there are no available frequencies
$self->_dump_existing_vf_annotations() if (!$dumped_annotations);
return 1;
}
=head2 _dump_existing_vf_frequencies
Arg [1] : Hashref $existing_var
Description: The $existing_var contains everything that is stored for the variant in the VEP cache file.
Extract allele frequencies from $existing_var and write it to the log file as G2P_frequencies.
Store the highest observed frequency for the variant which is used later for filtering.
Returntype : None
Exceptions : None
Caller : General
Status : Stable
=cut
sub _dump_existing_vf_frequencies {
my $self = shift;
my $existing_var = shift;
my $allele = shift;
my @keys = @{$self->{user_params}->{af_keys}};
my @frequencies = ();
my $higest_frequency = 0;
foreach my $population_name (@keys) {
my $af = $existing_var->{$population_name};
next if (!defined $af);
foreach my $pair (split(',', $af)) {
my ($a, $f) = split(':', $pair);
if(($a || '') eq $allele && defined($f)) {
push @frequencies, "$population_name=$f";
$higest_frequency = $f if ($f > $higest_frequency);
}
}
}
$self->highest_frequency($self->{vf_cache_name}, $higest_frequency);
$self->write_report('G2P_frequencies', $self->{vf_cache_name}, \@frequencies);
}
=head2 _dump_exisiting_vf_annotations