-
Notifications
You must be signed in to change notification settings - Fork 4
/
Burden_testing.pl
executable file
·1164 lines (956 loc) · 42.1 KB
/
Burden_testing.pl
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
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
use JSON;
use File::Basename;
use Getopt::Long qw(GetOptions);
use Data::Types;
use File::Path qw(make_path);
use Cwd qw(abs_path);
# Version information:
our $version = "v7.1 Last modified: 2020.Sep.30";
# Get script directory:
my $scriptDir;
BEGIN{
$scriptDir = dirname(abs_path(__FILE__));
}
$\="\n";
print "ScriptDir : ".$scriptDir;
use lib $scriptDir;
##----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#
# ASSUMING EIGEN SCORES ARE b37 BASED, CADD SCORES ARE b38 BASED (v. 1.5, https://krishna.gs.washington.edu/download/CADD/v1.5/GRCh38/whole_genome_SNVs.tsv.gz)
#
##----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Custom modules:
use GENCODE;
use Scoring;
# Status report:
print "[Info] Script version: $version";
#exit(0);
#printf "[Info] Run date: %s", DateTime->now->strftime("%Y. %b %d %H:%M");
printf "[Info] The script was called with the following parameters:\n%s\n", join(" ", $0, @ARGV);
# some default parameter values
my $parameters = {
"build" => "38",
"extend" => 0, # Basepairs with which the GENCODE features will be extended.
"MAF" => 0.05, # The upper threshold of the minor allel frequency.
"MAC" => 0, # The lower threshold of the minor allele count.
"missingthreshold" => 0.01, # The upper threshold of the missingness. (below which the the missingness will be imputed)
"score" => "NA", # Applied score to weight variants.
"cutoff" => 0, # Score threshold below which the variants will be removed.
"floor" => 0, # All the scores below threshold will be set to this value.
"shift" => 0, # A value with which the scores of the variants will be shifted.
"chr_prefix" => "chr", # Chromosome prefix in VCF files or list
"scriptDir"=>${scriptDir},
"lof" => undef,
"smmat" => undef,
"vcf" => undef
};
# LoF VEP consequences
$parameters->{"lof_cons"} = {
"transcript_ablation" => 10,
"splice_acceptor_variant" => 9,
"splice_donor_variant" => 8,
"stop_gained" => 7,
"frameshift_variant" => 6,
"stop_lost" => 5,
"start_lost" => 4,
"transcript_amplification" => 3,
"inframe_insertion" => 2,
"inframe_deletion" => 1
};
# Command line options without default values:
my ($inputFile,$outputDir,$outputFile, $help);
# Parsing command line options:
GetOptions(
# Input/Output:
'input=s' => \$inputFile,
'output=s' => \$outputFile,
'output-dir=s' => \$outputDir,
# Selecting region source:
'GENCODE=s' => \$parameters->{"input"}->{"GENCODE"},
'GTEx=s' => \$parameters->{"input"}->{"GTEx"},
'overlap=s' => \$parameters->{"input"}->{"overlap"},
# Extending regions with a defined length:
'extend=s' => \$parameters->{"extend"},
# Skipping minor transcripts by APPRIS:
'SkipMinor' => \$parameters->{"minor"},
# Variant features:
'MAF=s' => \$parameters->{"MAF"},
'MAC=s' => \$parameters->{"MAC"},
# Verbose output:
'verbose' => \$parameters->{"verbose"},
# bgizipped, indexed, 1-based input list for SMMAT
'smmat=s' => \$parameters->{"smmat"},
# specifying config file:
'config=s' => \$parameters->{"configName"},
# Which score do we need:
'score=s' => \$parameters->{"score"},
'maxVars=s' => \$parameters->{"maxVars"},
# Do we need LoF variants only:
'lof' => \$parameters->{"lof"},
'loftee' => \$parameters->{"loftee"}, # Filters only high and low confident loss of function variants
'lofteeHC' => \$parameters->{"lofteeHC"}, # Filters for only high confident loss of function variants
# Accepting missingness filter:
'missingness=s' => \$parameters->{"missingthreshold"},
# Changing scores that will be used for weights:
'shift=s' => \$parameters->{"shift"}, # The value used to shift eigen scores:
'cutoff=s' => \$parameters->{"cutoff"}, # hard threshold that will be applied on scores:
'floor=s' => \$parameters->{"floor"}, # How do we want to floor Eigen values:
'chromosome-prefix=s' => \$parameters->{"chr_prefix"}, # Chromosome prefix in VCF file(s)
'vcf=s' => \$parameters->{"vcf"},
'help|h' => \$help
);
$parameters->{"maxVars"}=1000 unless $parameters->{"maxVars"};
&usage && die "[Error] No output directory specified. Exiting." unless $outputDir;
if (! -d $outputDir){
die "[Error] Could not create output directory ($outputDir). Exiting." unless make_path($outputDir)==1;
}
$parameters->{"tempdir"}=$outputDir."/prepare_regions_tempfiles";
if (! -d $parameters->{"tempdir"}){
die "[Error] Could not create temp directory (".$parameters->{"tempdir"}."). Exiting." unless make_path($parameters->{"tempdir"})==1;
}
&usage && die "[Error] No config file specified. Exiting." unless $parameters->{"configName"};
&usage && die "[Error] The specified config file does not exist. Exiting." unless -e $parameters->{"configName"};
&usage && die "[Error] Gene list input file has to be specified with the --input option. Exiting." unless $inputFile;
&usage && die "[Error] The specified input gene list does not exist. Exiting." unless -e $inputFile;
&usage && die "[Error] Output file has to be specified with the --output option. Exiting." unless $outputFile;
&usage && die "[Error] VCF files or a SMMAT input list have to be specified with the --vcf or --smmat option. Exiting." unless(defined($parameters->{"vcf"}) || defined($parameters->{"smmat"}));
&usage && die "[Error] both VCF files and a SMMAT input list are specified. Exiting." if(defined($parameters->{"vcf"}) && defined($parameters->{"smmat"}));
if (! defined($parameters->{"smmat"})){
&usage && die "[Error] No VCF files exist." unless &checkVCFs($parameters->{"vcf"});
}
$parameters = &readConfigFile($parameters);
# check if necessary parameters are set
foreach my $k ("Linked_features","gencode_file","VEPdir","VEPexec"){
if (!exists($parameters->{$k})){
die "[Error] Key ".$k." is not defined in the config file. Exiting."
}
}
# If the score option is not empty, we have to check if it's a valid score, and the
# required files are specified in the config file.
$parameters = &check_scores($parameters) if $parameters->{"score"} ne "NA";
# Processing the requested features:
$parameters = &parseGENCODE($parameters) if $parameters->{"input"}->{"GENCODE"};
$parameters = &parseRegulation($parameters) if $parameters->{"input"}->{"GTEx"} || $parameters->{"input"}->{"overlap"};
our $verbose = $parameters->{"verbose"};
# Report submitted parameters:
&print_parameters($parameters);
# Initializing helper objects:
print "[Info] Initializing GENCODE data" if ($verbose);
my $GENCODE_data = GENCODE->new($parameters);
die "[Error] Could not initialize GENCODE" if $GENCODE_data->{"failed"};
print "[Info] Initializing score data data" if ($verbose);
my $AddScore = Scoring->new($parameters);
# Open files:
open (my $INPUT, "<", $inputFile) or die "[Error] Input file ($inputFile) could not be opened. Exiting.";
my ($SNPinfo,$SNPfile,$genotypeFile);
if (defined($parameters->{"vcf"})){
open ($SNPfile, ">", $outputDir."/".$outputFile."_variant_file.txt") or die "[Error] Output file could not be opened.";
open ($genotypeFile, ">", $outputDir."/".$outputFile."_genotype_file.txt") or die "[Error] Output genotype file could not be opened.";
}
if (defined($parameters->{"vcf"})){
open ($SNPinfo, ">", $outputDir."/".$outputFile."_SNPinfo_file.txt") or die "[Error] Output SNPinfo file could not be opened.";
}
else{
open ($SNPinfo, ">", $outputDir."/".$outputFile.".txt") or die "[Error] Output group file could not be opened.";
}
# Processing the input file gene by gene:
my $gene_count = 0;
# to detect ID/name duplicates in the input list
my %counts;
while ( my $ID = <$INPUT> ){
next if $ID=~/^\s*$/;
chomp $ID;
if (exists($counts{$ID})){
print "[Warning]: gene $ID occurs more than once in the input list";
next;
}
else{
$counts{$ID}=1;
}
# skip GENCODE duplicates
if ($GENCODE_data->isDuplicateName($ID)){
print "[Warning]: gene $ID occurs more than once in GENCODE data; skipping";
next;
}
my $gene_coords = $GENCODE_data->GetCoordinates($ID);
if (! defined($gene_coords)) {
print "[Warning] Gene $ID was not found in the GENCODE data; skipping [NO_GENE]\n";
next;
}
# stableID is the full ID
foreach my $stableID (keys %$gene_coords){
my $rec=$gene_coords->{$stableID};
my $chr=$rec->{chr};
my $start=$rec->{start};
my $end=$rec->{end};
my $name=$rec->{name};
print "[Info] Queried gene: $name (Ensembl ID: $ID ($stableID)), Genomic location: $chr:$start-$end (Input: $ID ($stableID))";
# all lines from the Linked_features file that are associated with the current gene
my $bedlines = &BedToolsQuery($chr, $start, $end, $stableID, $parameters->{"Linked_features"});
# remove some lines we're not interested in
my $CollapsedBed = &FilterLines($bedlines, $stableID, $parameters);
unless ( $CollapsedBed ){
print "[WARNING] Gene $name ($stableID) did not yield any regions. Skipped. [NO_REGION]\n";
next;
}
# CollapsedBed is 0-based
# Once we have the genomic regions, the overlapping variants are extracted
my $variants = &getVariants($CollapsedBed, $parameters);
# Filtering variants based on the provided parameters:
my ($hash, $genotypes) = &processVar($variants, $parameters,$stableID);
# Gene will be skipped if there are no suitable variations left (for MONSTER):
if (scalar keys %{$hash} < 2 && !defined($parameters->{"smmat"})){
print "[Warning] Gene $name ($stableID) is skipped as not enough variants left to test [NOT_ENOUGH_VAR]\n";
undef $hash;
undef $genotypes;
next;
}
# The gene will be skipped if there are too many variants (for MONSTER):
if (scalar keys %{$hash} > $parameters->{"maxVars"} && !defined($parameters->{"smmat"})){
print "[Warning] Gene $name ($stableID) is skipped as more than ".$parameters->{"maxVars"}." variants are in the set [TOO_MANY_VAR]\n";
undef $hash;
undef $genotypes;
next;
}
# If we want we can add scores:
$hash = $AddScore->AddScore($hash) if $parameters->{"score"} ne "NA";
checkScores($hash) if $parameters->{"score"} ne "NA";
if ($parameters->{"score"} ne "NA" && scalar keys %{$hash} < 1){
print "[Warning] Gene $name ($stableID) is skipped as no variants remaining post-scoring [NO_VAR_REMAIN]\n";
}
# We don't save anything unless there at least two variants (for MONSTER):
if (!defined($parameters->{"smmat"})){
next unless scalar keys %{$hash} > 1;
}
next unless scalar keys %{$hash} > 0;
# Once we have the scores we have to print out the SNP file:
my $flag=0;
$flag=1 if $parameters->{"score"} ne "NA";
# output original ID
if (defined($parameters->{"smmat"})){
&print_group_file($hash, $ID, $SNPinfo, $parameters->{"build"},$flag);
}
else{# output for MONSTER from VCFs
&print_SNPlist($hash, $ID, $SNPfile,$flag);
&print_SNP_info($hash, $ID, $SNPinfo, $gene_count, $parameters->{"build"},$flag);
&print_genotypes($genotypes, $genotypeFile, $parameters, $gene_count);
}
$gene_count ++; # So the header will only be printed once.
}
}
close $INPUT;
if (defined($parameters->{"vcf"})){
close $SNPfile;
close $genotypeFile;
}
close $SNPinfo;
sub check_parameters {
my $parameters = shift;
# checking build:
die "[Error] Only GRCh37 and GRCh38 genome builds are supported (--build 37 or 38)." unless $parameters->{"build"} eq "37" || $parameters->{"build"} eq "38";
# Checking MAF (a float below 1):
my $maf=$parameters->{"MAF"};
die "[Error] $maf must be a float number < 1" unless defined($maf) && is_float($maf) && $maf<1;
# Checking MAC (an integer):
my $mac=$parameters->{"MAC"};
die "[Error] $mac must be an integer number > 0" unless defined($mac) && is_int($mac) && $mac>0;
# Checking missingness (a float below 1):
my $miss=$parameters->{"missingthreshold"};
die "[Error] $miss must be a float number < 1" unless defined($miss) && is_float($miss) && $miss<1;
}
sub check_scores {
my $params = $_[0];
# Check if the specified score is a valid, supported score:
my %acceptedScores = ("CADD" => 1,
# "Eigen" => 1,
# "EigenPC" => 1,
"EigenPhred" => 1);
# "EigenPCPhred" => 1,
# "Linsight" => 1,
# "Mixed" => 1);
# report if the specified score is not supported:
unless (exists $acceptedScores{$params->{"score"}}){
print "[Info] Supported weighting options: ", join(", ", keys %acceptedScores), "";
printf "[Warning] The specified score (%s) is currently not supported.", $params->{"score"};
print "[Warning] No weighting will be applied.";
$params->{"score"} = "NA";
return $params;
}
# Now, let's check if the requirements of the specified weighting methods are satisified.
if ($params->{"score"} eq "CADD" ){
if( ! exists $params->{"caddPath"}){
print "[Warning] The config file has not entry for 'caddPath', pointing to the genome-wide CADD scores.";
print "[Warning] CADD scores as weight cannot be used. No weights will be applied.";
$params->{"score"} = "NA";
return $params;
}
elsif (! -e $params->{"caddPath"}){
printf "[Warning] The specified genome-wide CADD scores are not available %s.", $params->{"caddPath"};
print "[Warning] CADD scores as weight cannot be used. No weights will be applied.";
$params->{"score"} = "NA";
return $params;
}
}
elsif ($params->{"score"} =~ /eigen/i){
if (! exists $params->{"EigenPath"} ) {
print "[Warning] The config file has not entry for 'EigenPath', pointing to the genome-wide Eigen scores.";
print "[Warning] Eigen scores as weights cannot be used. No weights will be applied.";
$params->{"score"} = "NA";
return $params;
}
}
return $params;
}
sub readConfigFile {
my $params = $_[0];
open(my $CONF, "<", $params->{"configName"}) or die "[Error] In readConfigFile: config file (".$params->{"configName"}.") could not be opened. Exiting.";
while ( my $line = <$CONF>) {
chomp $line;
next unless $line;
next if $line =~ /^#/;
my ($key, $value) = $line =~ /^(\S+)=(\S+)/;
$params->{$key} = $value if $key && $value;
}
return $params;
}
sub parseGENCODE {
my %AcceptedFeatures = ( "gene" => 1, "exon" => 1, "transcript" => 1, "CDS" => 1, "UTR" => 1);
my $parameters = $_[0];
foreach my $feature (split(",", $parameters->{"input"}->{"GENCODE"})){
# Checking if the provided feature is exists or not:
if (exists $AcceptedFeatures{$feature}) {
$parameters->{"GENCODE"}->{$feature} = 1;
}
else {
printf STDERR "[Error] The provided GENCODE feature name ($feature) is not supported: %s. Use these: %s", $feature, join(", ", keys %AcceptedFeatures);
}
}
return $parameters
}
sub parseRegulation {
# Accepted features: promoter, CTCF, enhancer, promoterFlank, openChrom, TF_bind, allreg
my %AcceptedFeatures = ( "promoter" => 1, "CTCF" => 1, "enhancer" => 1, "promoterFlank" => 1, "openChrom" => 1, "TF_bind" => 1, "allreg" => 1 );
my $parameters = $_[0];
my @reg_class = ("GTEx", "overlap");
foreach my $class (@reg_class){
next unless exists $parameters->{"input"}->{$class};
my %hash = ();
# Looping through all the submitted features:
foreach my $feature (split(",",$parameters->{"input"}->{$class})){
unless (exists $AcceptedFeatures{$feature}){
printf STDERR "[Error] The provided regulatory feature name ($feature) is not supported: %s. Use these: %s", $feature, join(", ", keys %AcceptedFeatures);
next;
}
$hash{'TF_binding_site'} = 1 if "TF_bind" eq $feature;
$hash{'CTCF_binding_site'} = 1 if "CTCF" eq $feature;
$hash{'enhancer'} = 1 if "enhancer" eq $feature;
$hash{'open_chromatin_region'} = 1 if "openChrom" eq $feature;
$hash{'promoter'} = 1 if "promoter" eq $feature;
$hash{'allreg'} = 1 if "allreg" eq $feature;
$hash{'promoter_flanking_region'} = 1 if "promoterFlank" eq $feature;
}
$parameters->{$class} = \%hash;
}
return $parameters;
}
sub print_parameters {
local $\="\n";
$parameters = $_[0];
printf "\n[Info] Current genome build: GRCh%s", $parameters->{"build"};
print "\n[Info] Selected features:";
printf "\tThe following GENCODE features are considered: %s", join (", ", keys %{$parameters->{"GENCODE"}}) if exists $parameters->{"GENCODE"};
printf "\tGENCODE features are extended by: %sbp", $parameters->{"extend"} if exists $parameters->{"GENCODE"};
printf "\tThe following GTEx linked regulatory features are considered: %s", join (", ", keys %{$parameters->{"GTEx"}}) if exists $parameters->{"GTEx"};
printf "\tThe following overlapping regulatory features are considered: %s", join (", ", keys %{$parameters->{"overlap"}}) if exists $parameters->{"overlap"};
print "\n[Info] Variant filters:";
printf "\tUpper MAF threshold: %s", $parameters->{"MAF"};
printf "\tLower MAC threshold: %s", $parameters->{"MAC"};
printf "\tUpper missingness threshold: %s", $parameters->{"missingthreshold"};
print "\tOnly severe variants will be included in the test." if $parameters->{"lof"};
print "\tOnly loss-of-function variants will be included in the test (loftee HC and LC)." if $parameters->{"loftee"};
print "\tOnly high-confidence loss-of-function variants will be included in the test (loftee HC)." if $parameters->{"lofteeHC"};
print "\n[Info] Variant weighting:";
printf "\tWeighting method: %s\n", $parameters->{"score"};
if ($parameters->{"score"} ne "NA"){
printf "\tScore lower cutoff: %s", $parameters->{"cutoff"};
printf "\tScore floor applied: %s", $parameters->{"floor"};
printf "\tScore shifted by: %s\n", $parameters->{"shift"};
}
}
# get relevant lines from Linked_features file
sub BedToolsQuery {
my ($chr, $start, $end, $stable_ID, $geneBedFile) = @_;
my $queryString = sprintf("intersectBed -wb -a <(echo -e \"%s\\t%s\\t%s\\t%s\") -b %s -sorted 2>/dev/null | cut -f9-",$chr, $start-1, $end, $stable_ID, $geneBedFile); # start and end are from GENCODE (1-based), bed is 0-based
print "[Info] IntersectBed query string: $queryString" if $verbose;
my $query =Scoring::backticks_bash($queryString);
#print "QUERY: $query" if $verbose;
return $query;
}
sub formatLines {
my %hash = %{$_[0]};
my $ext = $_[1] // 0;
# 0-based
return sprintf("%s\t%s\t%s\t%s", $hash{"chr"}, $hash{"start"} - $ext, $hash{"end"} + $ext, encode_json(\%hash))
}
sub getVariants {
# merged is 0-based
my ($merged, $parameters) = @_;
my $inputvcfpattern=$parameters->{"vcf"};
my $prefix=$parameters->{"chr_prefix"};
my $smmat=$parameters->{"smmat"};
my $distance = 0;
my $variants;
my $source;
# Finding out which chromosome are we on:
my ($chr) = $merged =~ /^(.+?)\t/; # WATCHOUT !
if (defined($smmat)){
print "[Info] Extracting variants from the list:" if $verbose;
$source=$smmat;
# SMMAT lists have no chromosome prefix
$prefix="";
}
else{
print "[Info] Extracting variants from vcf file(s):" if $verbose;
(my $vcf = $inputvcfpattern ) =~ s/\%/$chr/g;
if (! -e $vcf){
print "[Warning] VCF file for chromosome $chr does not exist";
return "";
}
$source=$vcf;
}
my $tabix_query = sprintf("tabix %s ", $source);
# looping through all lines:
foreach my $line (split("\n", $merged)){
my ($chr, $start, $end) = split("\t", $line);
$distance += $end - $start;
$tabix_query .= sprintf(" %s%s:%s-%s", $prefix, $chr, $start+1, $end); # VCFs and lists are 1-based
}
print "$tabix_query" if $verbose;
$variants = Scoring::backticks_bash($tabix_query);
print sprintf("[Info] Total covered genomic regions: %s bp", $distance) if $verbose;
return $variants;
}
sub FilterLines {
my ($lines, $stable_ID, $parameters) = @_;
my $stable_IDprefix=$stable_ID;
if ($stable_ID=~/(ENSG\d+)\./){
$stable_IDprefix=$1;
}
my @output_lines = ();
my %hash = ();
# The following hashes read from the parameter set:
my %GENCODE = exists $parameters->{"GENCODE"} ? %{$parameters->{"GENCODE"}} : ();
my %GTEx = exists $parameters->{"GTEx"} ? %{$parameters->{"GTEx"}} : ();
my %overlap = exists $parameters->{"overlap"} ? %{$parameters->{"overlap"}} : ();
foreach my $line (split (/\n/, $lines)){
#print "LINE: $line";
# Decoding json line:
my %annot_hash = %{decode_json($line)};
# Filtering for lines which correspond to our target gene:
my $gID=$annot_hash{"gene_ID"};
if ($gID=~/ENSG\d+\./){ # full ID in the record
next unless $gID eq $stable_ID;
}
else{ # ID prefix in the record, so we compare it against the stable_ID prefix
next unless $gID eq $stable_IDprefix;
}
my $source = $annot_hash{"source"};
my $class = exists $annot_hash{"class"} ? $annot_hash{"class"} : "?";
# print "\n$source $class";
if ($source eq "GENCODE" && exists $GENCODE{$class}) {
# If the user has specified, minor transcripts will be excluded:
next if exists $GENCODE{"minor"} && $annot_hash{"appris"} eq "Minor";
push (@output_lines, formatLines(\%annot_hash, $parameters->{'extend'}));
$hash{GENCODE}{$class} ++;
}
elsif (($source eq "GTEx" && exists $GTEx{$class})
|| ($source eq "GTEx" && exists $GTEx{'allreg'})){
push (@output_lines, formatLines(\%annot_hash));
$hash{GTEx}{$class} ++;
}
elsif (($source eq "overlap" && exists $overlap{$class})
|| ($source eq "overlap" && exists $overlap{'allreg'})){
push (@output_lines, formatLines(\%annot_hash));
$hash{overlap}{$class} ++;
}
}
if ($verbose) {
if (exists $hash{GENCODE}) {
my $report = '';
foreach my $feature (keys %{$hash{GENCODE}}){
$report .= "$feature: $hash{GENCODE}{$feature}, ";
}
print "[Info] From the GENCODE dataset the following features were extracted: $report" if $verbose;
}
if (exists $hash{GTEx}) {
my $report = '';
foreach my $feature (keys %{$hash{GTEx}}){
$report .= "$feature: $hash{GTEx}{$feature}, ";
}
print "[Info] The following regulatory features were linked to the gene: $report" if $verbose;
}
if (exists $hash{overlap}) {
my $report = '';
foreach my $feature (keys %{$hash{overlap}}){
$report .= "$feature: $hash{overlap}{$feature}, ";
}
print "[Info] The following overlapping regulatory features were extracted: $report" if $verbose;
}
}
# Report:
# print "[Info] Selected lines:\n", join("\n", @output_lines),"" if $verbose;
# Saving temporary bedfile:
my $tmpName=$parameters->{"tempdir"}."/filtered_regions.bed";
open (my $tempbed, "> $tmpName");
foreach my $line (@output_lines){
print $tempbed $line;
}
close $tempbed;
`sort -k1,1 -k2,2n $tmpName | sponge $tmpName`;
# Collapsing overlapping features:
my $queryString = "mergeBed -i $tmpName";
my $merged = Scoring::backticks_bash($queryString);
return $merged;
}
sub checkScores{
my %hash = %{$_[0]};
for my $v (keys(%hash)){
die "[Error]: score for $v is not defined" unless (defined($hash{$v}{"score"}));
}
}
sub getVariantType{
my ($ref,$alt)=@_;
return "SNP" if length($ref)==1 && length($alt)==1;
return "DEL" if length($ref)>length($alt);
return "INS" if length($ref)<length($alt);
return "NA";
}
# Using VEP
sub getConsequences{
my $variants = $_[0];
my $parameters = $_[1];
my $stable_ID=$_[2];
# stable ID is the raw ID, potentially with suffix
if ($stable_ID=~/(ENSG\d+)\./){
$stable_ID=$1;
}
my $vepin;
my $vepout;
my %cons=();
local $\="\n";
local $,="\t";
my $fname1=$parameters->{"tempdir"}."/vep_input.txt";
my $vepdir=$parameters->{"VEPdir"};
my $vepexec=$parameters->{"VEPexec"};
open ($vepin, ">", $fname1) or die "[Error] Input file for VEP could not be opened.";
my @total_vars = split("\n", $variants);
my $count=0;
while (@total_vars){
my $variant = shift @total_vars;
# line: CHROM POS ID REF ALT ...
my ($chr, $pos, $id, $ref, $alt, @therest) = split(/\t/, $variant);
(my $c = $chr ) =~ s/chr//i;
my $varID=$c."_".$pos."_".$ref."_".$alt;
# skip multiallelics
if ($alt=~/,/){
$cons{$varID}="NA";
next;
}
my $vtype=getVariantType($ref,$alt);
if ($vtype eq "SNP"){
print $vepin $c,$pos,$pos,$ref."/".$alt,"+",$varID;
$count++;
}
elsif($vtype eq "DEL"){
if (length($alt)==1){
my $r=substr($ref,1,length($ref)-1);
print $vepin $c,$pos+1,$pos+length($ref)-1,$r."/-","+",$varID;
}
else{
my $L1=length($alt);
my $r=substr($ref,$L1,length($ref)-1);
print $vepin $c,$pos+$L1,$pos+length($ref)-1,$r."/-","+",$varID;
}
$count++;
}
elsif($vtype eq "INS"){
if (length($ref)==1){
my $a=substr($alt,1,length($alt)-1);
print $vepin $c,$pos+1,$pos,"-/".$a,"+",$varID;
}
else{
my $L1=length($ref);
my $a=substr($alt,$L1,length($alt)-$L1);
print $vepin $c,$pos+$L1,$pos+$L1-1,"-/".$a,"+",$varID;
}
$count++;
}
else{
print "[Error] could not determine variant type of $variant";
$cons{$varID}="NA";
next;
}
}
close($vepin);
return undef unless($count>0);
my $queryString="PERL5LIB=\$PERL5LIB:$vepdir ".$vepexec." -i ".$fname1." --dir ".$vepdir." --dir_cache ".$vepdir." -o STDOUT --offline --no_stats | grep -v \"^#\" | awk -v g=".$stable_ID." 'BEGIN{FS=\"\\t\";}\$4==g{print \$0;}' | cut -f 1,7";
print $queryString if $verbose;
my $query =Scoring::backticks_bash($queryString);
my %max_severity;
foreach my $line (split (/\n/, $query)){
print "getConsequences output: ".$line if $verbose;
my @a=split(/\t/,$line);
my $ID=$a[0];
my $effs=$a[1];
$max_severity{$ID}=0 unless exists($max_severity{$ID});
foreach my $e (split(/,/,$effs)){
if (! exists($parameters->{"lof_cons"}->{$e})){ # low severity
$cons{$ID}=$e if ($max_severity{$ID}==0);
}
else{
if ($parameters->{"lof_cons"}->{$e} > $max_severity{$ID}){
$max_severity{$ID}=$parameters->{"lof_cons"}->{$e};
$cons{$ID}=$e;
}
}
}
}
return \%cons;
}
sub processVar {
my $variants = $_[0]; # List of all overlapping variants
my $parameters = $_[1]; # All the submitted parameters to filter variants.
my $stable_ID=$_[2];
my %hash = (); # SNP info container.
my %genotypeContainer = (); # genotype information container.
# Which build are we using?
my $build = "GRCh".$parameters->{"build"};
print "[Info] Processing variants:" if $verbose;
my $cons;
# --------------------------------------------------------
# we only need consequences if --lof option is provided
if (defined($parameters->{"lof"})){
$cons=getConsequences($variants,$parameters,$stable_ID);
}
# --------------------------------------------------------
my @total_vars = split("\n", $variants);
printf "[Info] Total number of overlapping variants: %s\n", scalar(@total_vars) if $verbose;
if ($parameters->{"smmat"}){
while (@total_vars){
my $consequence="NA"; # default
# Removing one variant at a time:
my $variant = shift @total_vars;
# line: CHROM POS ID REF ALT
my ($chr, $pos, $id,$a1, $a2) = split(/\t/, $variant);
my $SNPID = sprintf("%s_%s_%s_%s", $chr, $pos, $a1, $a2);
# We don't consider indels if weights are used:
if (( length($a2) > 1 || length($a1) > 1 ) && $parameters->{"score"} ne "NA"){
print "[Warning] $SNPID will be omitted because it's an indel and we use scores for weighting! ($a1/$a2).";
next;
}
# We don't consider multialleleic sites
if ( $a2 =~ /,/){
print "[Warning] $SNPID will be omitted because it's multiallelic! ($a2).";
next;
}
# --------------------------------------------------------
if (defined($parameters->{"lof"})){
(my $chr2 = $chr ) =~ s/chr//i;
my $varID=$chr2."_".$pos."_".$a1."_".$a2;
if (defined($cons)){
if (exists($cons->{$varID})){
$consequence=$cons->{$varID};
print "CONSEQUENCE: ".$varID." ".$consequence if $verbose;
}
}
}
# If loss of function variants are required, we skip all those variants that are not LoF:
if ( $parameters->{"lof"} && ! exists $parameters->{"lof_cons"}->{$consequence} ) {
printf "[Warning] $SNPID will be omitted because its consequence (%s) is not lof\n", $consequence;
next;
}
# --------------------------------------------------------
# TODO: check if we need that for SMMAT
# Generating variant name (Sometimes the long allele names cause problems):
#my $short_a1 = length $a1 > 5 ? substr($a1,0,4) : $a1;
#my $short_a2 = length $a2 > 5 ? substr($a2,0,4) : $a2;
#$SNPID = sprintf("%s_%s_%s_%s", $chr, $pos, $short_a1, $short_a2);
$hash{$SNPID}{"alleles"} = [$a1, $a2];
$hash{$SNPID}{$build} = [$chr, $pos - 1, $pos]; # 0-based
$hash{$SNPID}{"consequence"} = $consequence;
}
}
else{
while (@total_vars){
my $consequence="NA"; # default
# Removing one variant at a time:
my $variant = shift @total_vars;
# line: CHROM POS ID REF ALT QUAL FILTER INFO FORMAT EGAN00001033155
my ($chr, $pos, $id, $a1, $a2, $qual, $filter, $info, $format, @genotypes) = split(/\t/, $variant);
# --------------------------------------------------------
if (defined($parameters->{"lof"})){
(my $chr2 = $chr ) =~ s/chr//i;
my $varID=$chr2."_".$pos."_".$a1."_".$a2;
if (defined($cons)){
if (exists($cons->{$varID})){
$consequence=$cons->{$varID};
print "CONSEQUENCE: ".$varID." ".$consequence if $verbose;
}
}
}
# --------------------------------------------------------
# Generating variant name (Sometimes the long allele names cause problems):
my $short_a1 = length $a1 > 5 ? substr($a1,0,4) : $a1;
my $short_a2 = length $a2 > 5 ? substr($a2,0,4) : $a2;
my $SNPID = sprintf("%s_%s_%s_%s", $chr, $pos, $short_a1, $short_a2);
# Parsing info field for relevant information:
(my $ac )= $info =~ /AC=(.+?)[;\b]/;
(my $an )= $info =~ /AN=(.+?)[;\b]/;
# If AN and AC values are not found we skip the variant:
if (! $an){
print "[Warning] $SNPID has no AN; skipping";
next;
}
if (! $ac){
print "[Warning] $SNPID has no AC; skipping";
next;
}
# --------------------------------------------------------
# We add NA if consequence was not found:
#(my $consequence ) = $info =~ /consequence=(.+?)[;\b]/;
#$consequence = "NA" unless $consequence;
# --------------------------------------------------------
# We don't consider multialleleic sites
if ( $a2 =~ /,/){
print "[Warning] $SNPID will be omitted because it's multiallelic! ($a2).";
next;
}
print "[Warning] $variant has no AC" if $ac eq "NA";
print "[Warning] $variant has no AN" if $an eq "NA";
# Calculating values for filtering:
my $missingness = (scalar(@genotypes)*2 - $an)/(scalar(@genotypes)*2);
my $MAF = $ac/$an;
# This flag shows if the non-ref allele is the major one:
my $genotypeFlip = 0;
if ( $MAF > 0.5 ){
print "[Info] MAF of $SNPID is $MAF is greater then 0.5, genotype is flipped.";
$MAF = 1 - $MAF;
$genotypeFlip = 1;
}
my $MAC = $ac;
$MAC = $an - $ac if $MAC > $an / 2;
# We don't consider indels if weights are used:
if (( length($a2) > 1 || length($a1) > 1 ) && $parameters->{"score"} ne "NA"){
print "[Warning] $SNPID will be omitted because it's an indel and we use scores for weighting! ($a1/$a2).";
next;
}
# Filter out variants because of high missingness:
if ( $missingness > $parameters->{"missingthreshold"} ){
print "[Warning] $SNPID will be omitted because of high missingness ($missingness).";
next;
}
# Filter out variant because of high MAF (regardless of the applied weight):
if ( $MAF > $parameters->{'MAF'} ){
printf "[Warning] $SNPID will be omitted because of high MAF (%.3f, cutoff: %s)\n", $MAF, $parameters->{'MAF'};
next;
}
# Filter out variant because of high MAF:
if ( $ac < $parameters->{'MAC'} ){
printf "[Warning] $SNPID will be omitted because of low minor allele count ($ac, cutoff: %s)\n", $parameters->{'MAC'};
next;
}
# If loss of function variants are required, we skip all those variants that are not LoF:
if ( $parameters->{"lof"} && ! exists $parameters->{"lof_cons"}->{$consequence} ) {
printf "[Warning] $SNPID will be omitted because its consequence (%s) is not lof\n", $consequence;
next;
}
# If loftee or lofteeHC are enabled, the script skips the variant if no LoF_conf tag is present in the info field.
if (( $parameters->{"lofteeHC"} || $parameters->{"loftee"}) && $info !~ /LoF_conf/ ){
die "[Warning] Based on the provided parameters, variant selection based on the loftee prediction was requested.\nNo\"LoF_conf\" tag.Skipping.";
}
# If loftee is enabled, only low and high-confidence loss-of-function variants will be selected:
if ( $parameters->{"loftee"} && $info =~ /LoF_conf\=-/ ) {
print "[Warning] $SNPID will be omitted because it is not a high- or low-confidence loss of function variant";
next;
}
# If lofteeHC is enabled, only high-confidence loss-of-function variants will be selected:
if ( $parameters->{"lofteeHC"} && $info !~ /LoF_conf\=HC/ ) {
print "[Warning] $SNPID will be omitted because it is not a high- confidence loss of function variant";
next;
}
# If everything went fine, initializing variant by adding missingness to the hash:
$hash{$SNPID}{"missingness"} = $missingness;
# Storing variant data for all variants:
# TODO: if genotypeFlip == 1, then ac, an and MAF refer to a1, not a2
$hash{$SNPID}{"alleles"} = [$a1, $a2];
# TODO: for indels end should be different
$hash{$SNPID}{$build} = [$chr, $pos - 1, $pos]; # 0-based
$hash{$SNPID}{"frequencies"} = [$ac, $an, $MAF];
$hash{$SNPID}{"consequence"} = $consequence;
$hash{$SNPID}{"rsID"} = $id;
# Parsing genotypes:
foreach my $gt (@genotypes){
my @fields = split(":", $gt);
if ($fields[0] eq "0/0") { push(@{$genotypeContainer{$SNPID}}, $genotypeFlip == 1 ? 2 : 0) }
elsif ($fields[0] eq "1/1") { push(@{$genotypeContainer{$SNPID}}, $genotypeFlip == 1 ? 0 : 2) }
elsif ($fields[0] eq "1/0" or $fields[0] eq "0/1") { push(@{$genotypeContainer{$SNPID}}, 1) }
else {push(@{$genotypeContainer{$SNPID}}, "-9")}
}
}
}
printf ( "[Info] Number of filtered variants: %s\n", scalar(keys %hash));
return (\%hash, \%genotypeContainer);
}
sub print_SNPlist {
local $\=undef;
my %hash = %{$_[0]};
my $gene_name = $_[1];
my $outputhandle = $_[2];
my $flag = $_[3];
# Get the list of variants:
my @snpIDs = keys %hash;