-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmutspecAnnot.pl
1327 lines (1077 loc) · 46.1 KB
/
mutspecAnnot.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
#-----------------------------------#
# Author: Maude #
# Script: mutspecAnnot.pl #
# Last update: 02/03/17 #
#-----------------------------------#
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
use File::Basename; # my ($filename, $directories, $suffix) = fileparse($file, qr/\.[^.]*/);
use File::Path;
use Parallel::ForkManager;
our ($verbose, $man, $help) = (0, 0, 0); # Parse options and print usage if there is a syntax error, or if usage was explicitly requested.
our ($refGenome, $output, $path_AVDB, $pathAVDBList, $folder_temp) = ("empty", "empty", "empty", "empty", "empty"); # The reference genome to use; The path for saving the result; The path to Annovar database; Text file with the list of the databases for Annovar; the path for saving the temporary files
our ($intervalEnd) = (10); # Number of bases for the flanking region for the sequence context.
our ($fullAVDB) = "yes"; # Add an option for using all Annovar databases for the annotation or only refGene + strand + context for having a quicker annotation (for large file with million of lines)
#########################################
### SPECIFY THE NUMBER OF CPU ###
#########################################
our ($max_cpu) = 8; # Max number of CPU to use for the annotation
GetOptions('verbose|v'=>\$verbose, 'help|h'=>\$help, 'man|m'=>\$man, 'refGenome=s'=>\$refGenome, 'interval=i' => \$intervalEnd, 'fullAnnotation=s' => \$fullAVDB, 'outfile|o=s' => \$output, 'pathAnnovarDB|AVDB=s' => \$path_AVDB, 'pathAVDBList=s' => \$pathAVDBList, 'pathTemporary|temp=s' => \$folder_temp, 'max_cpu=i' => \$max_cpu) or pod2usage(2);
our ($input) = @ARGV;
pod2usage(-verbose=>1, -exitval=>1, -output=>\*STDERR) if ($help);
pod2usage(-verbose=>2, -exitval=>1, -output=>\*STDERR) if ($man);
pod2usage(-verbose=>0, -exitval=>1, -output=>\*STDERR) if(@ARGV == 0); # No argument is pass to the command line print the usage of the script
pod2usage(-verbose=>0, -exitval=>1, -output=>\*STDERR) if(@ARGV == 2); # Only one argument is expected to be pass to @ARGV (the input)
######################################################################################################################################################
# GLOBAL VARIABLES #
######################################################################################################################################################
# Recover the current path
our $pwd = `pwd`;
chomp($pwd);
# Recover the filename and the input directory
our ($filename, $directories, $suffix) = fileparse($input, qr/\.[^.]*/);
# Output directories
our ($folderMutAnalysis, $folderAnnovar) = ("", "");
# File with the list of Annovar databases to use
our $listAVDB = "";
# Initialisation of chromosome, position, ref and alt values
our ($chrValue, $positionValue, $refValue, $altValue) = ("c", "s", "r", "a");
######################################################################################################################################################
# MAIN #
######################################################################################################################################################
## Check the presence of the flags and create the output and temp directories
CheckFlags();
## Format the file correctly:
## 1) Check the length of the filename (must be <= 31 characters)
## 2) Recover the input format. If MuTect output consider only "KEEP" variants
## 3) Recover the column number for chr, start, ref and alt
FormatingInputFile();
# Annotate the file with Annovar, add the strand orientation and the sequence context
FullAnnotation();
######################################################################################################################################################
# FUNCTIONS #
######################################################################################################################################################
## Check the presence of the flags and create the output and temp directories
sub CheckFlags
{
# Check the reference genome
if($refGenome eq "empty")
{
print STDERR "Missing flag !\n";
print STDERR "You forget to specify the name for the reference genome!!!\nPlease specify it with the flag --refGenome\n";
exit;
}
if($intervalEnd eq "empty")
{
print STDERR "Missing flag !\n";
print STDERR "You forget to specify the length for the sequence context!!!\nPlease specify it with the flag --intervalEnd\n";
exit;
}
# If no output is specified write the result as the same place as the input file
if($output eq "empty")
{
my $directory = dirname( $input );
$folderMutAnalysis = "$directory/Mutational_Analysis";
if(!-e $folderMutAnalysis) { mkdir($folderMutAnalysis) or die "$!: $folderMutAnalysis\n"; }
}
else
{
if(!-e $output) { mkdir($output) or die "$!: $output\n"; }
$folderMutAnalysis = "$output/Mutational_Analysis";
if(!-e $folderMutAnalysis) { mkdir($folderMutAnalysis) or die "$!: $folderMutAnalysis\n"; }
}
# Create the output folder for Annovar
$folderAnnovar = "$folderMutAnalysis/Annovar";
if(!-e $folderAnnovar) { mkdir($folderAnnovar) or die "$!: $folderAnnovar\n"; }
# Verify the access to Annovar databases
if($path_AVDB eq "empty")
{
print STDERR "Missing flag !\n";
print STDERR "You forget to specify the path to Annovar databases!!!\nPlease specify it with the flag --pathAnnovarDB\n";
exit;
}
elsif(!-e $path_AVDB)
{
print STDERR "Error message:\n";
print STDERR"\nCan't access Annovar databases!\nPlease check the access to the disk\n";
}
# Check the file list AV DB
if($pathAVDBList eq "empty")
{
print STDERR "Missing flag !\n";
print STDERR "You forget to specify the path to the list of Annovar databases!!!\nPlease specify it with the flag --pathAVDBList\n";
exit;
}
else { $listAVDB = "$pathAVDBList/${refGenome}_listAVDB.txt" }
# If no temp folder is specified write the result in the current path
if($folder_temp eq "empty") { $folder_temp = "$pwd/TEMP_MutationalAnalysis_$filename"; }
if(!-e $folder_temp) { mkdir($folder_temp) or die "$!: $folder_temp\n"; }
# Verify listAVDB is not empty
if($listAVDB eq "")
{
print STDERR "Path to the text file containing the list of Annovar databases installed is not specified !!!\n";
exit;
}
}
## Format the file in the correct format if they are vcf or MuTect output and recover the column positions
sub FormatingInputFile
{
# The input is a folder
if(-d $input)
{
foreach my $file (`ls $input`)
{
my $headerOriginalFile = "";
chomp($file);
CheckLengthFilename("$input/$file");
#################################################
### Recover the input format ###
#################################################
RecoverInputFormat("$input/$file", \$headerOriginalFile);
}
}
# The input is one file
else
{
my $headerOriginalFile = "";
CheckLengthFilename($input);
#################################################
### Recover the input format ###
#################################################
RecoverInputFormat($input, \$headerOriginalFile);
}
}
# The name for the Excel sheet can't be longer than 31 characters
sub CheckLengthFilename
{
my ($inputFile) = @_;
my ($filenameInputFile, $directoriesInputFile, $suffixInputFile) = fileparse($inputFile, qr/\.[^.]*/);
if(length($filenameInputFile) > 32)
{
print STDERR "Error message:\n";
print STDERR "The file: $inputFile must be <= 31 chars\nPlease modify it before running the script\n";
}
}
# Recover the input format. If MuTect output consider only "KEEP" variants
sub RecoverInputFormat
{
my ($file, $refS_headerOriginalFile) = @_;
my ($filename, $directories, $suffix) = fileparse($file, qr/\.[^.]*/);
my $inputFormat = "";
open(F1, $file) or die "$!: $file\n";
my $header = <F1>;
close F1;
### VCF and MuTect files have in their first line the type of the file
if($header =~ /fileformat=VCF/i) { $inputFormat = "vcf"; }
elsif($header =~ /mutect/i) { $inputFormat = "mutect"; }
else { $inputFormat = "unknown"; }
### VCF files
if($inputFormat eq "vcf")
{
### MuTect2 output VCFs
my $testVC = `grep MuTect2 $file`;
if($testVC =~ /MuTect2/)
{
# Keep only the variants passing MuTect2 filters
`grep PASS $file > $folder_temp/$filename-PASS.txt`;
# Recover the header
$$refS_headerOriginalFile = `grep '#CHROM' $file`;
# Add the header
# Sed command doesn't work... sed 's/^/some text\n/' file > res
open(OUT, ">", "$folder_temp/$filename-KEEP.txt") or die "$!: $folder_temp/$filename-KEEP.txt\n";
print OUT $$refS_headerOriginalFile;
open(F1, "$folder_temp/$filename-PASS.txt") or die "$!: $folder_temp/$filename-PASS.txt\n";
while(<F1>) { print OUT $_; }
close F1; close OUT;
`rm $folder_temp/$filename-PASS.txt`;
# Check if there if no empty column
CheckEmptyColumn("$folder_temp/$filename-KEEP.txt");
`rm $folder_temp/$filename-KEEP.txt`;
# Set the col number for the chr,start,ref and alt
($chrValue, $positionValue, $refValue, $altValue) = (0, 1, 3, 4);
}
else
{
open(F1, $file) or die "$!: $file\n";
open(OUT, ">", "$folder_temp/$filename.txt") or die "$!: $folder_temp/$filename.txt\n";
while (<F1>)
{
$_ =~ s/[\r\n]+$//;
my @tab = split("\t", $_);
# Print the VCF header
if($tab[0] eq "#CHROM")
{
$tab[0] =~ /#(.+)/;
print OUT "$1";
for(my $i=1; $i<=$#tab; $i++) { print OUT "\t$tab[$i]"; }
print OUT "\n";
}
elsif($tab[0] !~ /##/)
{
# Don't consider chromosome random, GL and MT
if( ($tab[0] =~ /random/) || ($tab[0] =~ /GL/i) ) { next; }
print OUT "$_\n";
}
}
close F1; close OUT;
## Recover the header
open(F1, "$folder_temp/$filename.txt") or die "$!: $folder_temp/$filename.txt\n";
$$refS_headerOriginalFile = <F1>;
close F1;
# Check if there if no empty column
CheckEmptyColumn("$folder_temp/$filename.txt");
`rm $folder_temp/$filename.txt`;
# Set the col number for the chr,start,ref and alt
($chrValue, $positionValue, $refValue, $altValue) = (0, 1, 3, 4);
}
}
### MuTect files
elsif($inputFormat eq "mutect")
{
`sed '1d' $file > $folder_temp/$filename-HeaderOK`;
# Keep only the SNVs of good quality
`grep -v REJECT $folder_temp/$filename-HeaderOK > $folder_temp/$filename-KEEP.txt`;
`rm $folder_temp/$filename-HeaderOK`;
# Recover the header
open(F1, "$folder_temp/$filename-KEEP.txt") or die "$!: $folder_temp/$filename-KEEP.txt\n";
$$refS_headerOriginalFile = <F1>;
close F1;
# Check if there if no empty column
CheckEmptyColumn("$folder_temp/$filename-KEEP.txt");
`rm $folder_temp/$filename-KEEP.txt`;
# Recover the name and the number of the columns that contain the chromosome number, the start position, the ref and alt alleles.
# Use the dictionary for recover the names and the position of the columns
RecoverColNameAuto("$folder_temp/$filename-KEEPColumnCorrect.txt", $$refS_headerOriginalFile, \$chrValue, \$positionValue, \$refValue, \$altValue);
}
### Unknown type
else
{
## Recover the header
open(F1, $file) or die "$!: $file\n";
$$refS_headerOriginalFile = <F1>;
close F1;
# Check if there if no empty column
CheckEmptyColumn($file);
## Recover the name and the number of the columns that contain the chromosome number, the start position, the ref and alt alleles.
# Use the dictionary for recover the names and the position of the columns
RecoverColNameAuto($file, $$refS_headerOriginalFile, \$chrValue, \$positionValue, \$refValue, \$altValue);
}
}
# Some files can have empty column with no information
sub CheckEmptyColumn
{
my ($inputFile) = @_;
my ($filename, $directories, $suffix) = fileparse($inputFile, qr/\.[^.]*/);
if($filename =~ /(.+)-KEEP/) { $filename = $1; }
open(OUT, ">", "$folder_temp/$filename-ColumnCorrect.txt") or die "$!: $folder_temp/$filename-ColumnCorrect.txt\n";
open(F1, $inputFile) or die "$!: $inputFile\n";
my $header = <F1>; $header =~ s/[\r\n]+$//; my @tabHeader = split("\t", $header);
print OUT $header, "\n";
while(<F1>)
{
$_ =~ s/[\r\n]+$//;
my @tab = split("\t", $_);
if(scalar(@tab) != scalar(@tabHeader))
{
print OUT $tab[0];
for(my $i=1; $i<=$#tabHeader; $i++)
{
if(defined($tab[$i])) { print OUT "\t$tab[$i]"; }
else { print OUT "\tNA"; }
}
print OUT "\n";
}
else { print OUT "$_\n"; }
}
close F1; close OUT;
}
# Dictionnary for extracting the name and number of columns for the chromosome, start position, ref and alt alleles.
sub RecoverColNameAuto
{
our ($inputFile, $header, $ref_chrValue, $ref_positionValue, $ref_refValue, $ref_altValue) = @_;
$header =~ s/[\r\n]+$//;
## Name of the columns
my @mutect = qw(contig position ref_allele alt_allele);
my @vcf = qw(CHROM POS REF ALT);
my @cosmic = qw(Mutation_GRCh37_chromosome_number Mutation_GRCh37_genome_position Description_Ref_Genomic Description_Alt_Genomic);
my @icgc = qw(chromosome chromosome_start reference_genome_allele mutated_to_allele);
my @tcga = qw(Chromosome Start_position Reference_Allele Tumor_Seq_Allele2);
my @ionTorrent = qw(chr Position Ref Alt);
my @proton = qw(Chrom Position Ref Variant);
my @varScan2 = qw(Chrom Position Ref VarAllele);
my @varScan2_somatic = qw(chrom position ref var);
my @annovar = qw(Chr Start Ref Obs);
my @custom = qw(Chromosome Start Wild_Type Mutant);
my @allTab = (\@mutect, \@vcf, \@cosmic, \@icgc, \@tcga, \@ionTorrent, \@proton, \@varScan2, \@varScan2_somatic, \@annovar, \@custom);
my $timer = 0; # For controlling if the names are present on the dictionnary or not
foreach my $refTab (@allTab)
{
my @tab = @$refTab;
SearchCol(\@tab);
# The columns names were find
if( ($$ref_chrValue ne "c") && ($$ref_positionValue ne "s") && ($$ref_refValue ne "r") && ($$ref_altValue ne "a") ) { last; }
# The names of the columns are not present in the dictionnary
else { $timer++; }
}
if($timer == scalar(@allTab))
{
print STDERR "Error message:\n";
print STDERR "The columns name are not in the dictionnary please change them before running the tool again\nFile concerning: $inputFile\n";
print STDERR "TIP: Use one of the columns names proposed in the section Input formats of the tool\n";
exit;
}
# Extract the number of the column that contain the information
sub SearchCol
{
my ($refTab) = @_;
my @tabNames = @$refTab;
my @tabHeader = split("\t", $header);
# For VCF
if($tabHeader[0] eq "#CHROM") { ($$ref_chrValue, $$ref_positionValue, $$ref_refValue, $$ref_altValue) = (0, 1, 3, 4); }
# For tabular files
else
{
for(my $i=0; $i<=$#tabNames; $i++)
{
for(my $j=0; $j<=$#tabHeader; $j++)
{
if($tabHeader[$j] eq $tabNames[$i])
{
if($i == 0) { $$ref_chrValue = $j; }
if($i == 1) { $$ref_positionValue = $j; }
if($i == 2) { $$ref_refValue = $j; }
if($i == 3) { $$ref_altValue = $j; }
last; # Once find pass to the next name
}
}
}
}
}
}
# Annotate the file with Annovar, add the strand orientation and the sequence context
sub FullAnnotation
{
print "-----------------------------------------------------------------\n";
print "---------------------------Annotation----------------------------\n";
print "-----------------------------------------------------------------\n";
# If the input is a folder
if(-d $input)
{
foreach my $file (`ls $folder_temp/*.txt`)
{
chomp($file);
# For recover the name of the file without extension, the directory where the file is and the extension of the file
my ($filename, $directories, $suffix) = fileparse("$folder_temp/$file", qr/\.[^.]*/);
my $filenameOK = "";
# For removing the ColumnCorrect for txt files
if($filename =~ /(.+)-ColumnCorrect/)
{
if($filename =~ /(.+)-VariantListVCF-ColumnCorrect/) { $filenameOK = $1; }
else { $filenameOK = $1; }
}
else { print STDERR "Error message:\n"; print STDERR "Case not considered for $filename!!!\n"; }
#################################################
### Cut the files in n part ###
#################################################
# Cut the file in n part depending on the number of lines and set the number of CPU to use for the annotation depending of the number of n parts
my $cpu = 0;
# Keep the original header
my $headerOriginalFile = "";
# Save the numer of lines
my $nbLine = 0;
splitInputFile($file, \$cpu, \$headerOriginalFile, $filenameOK, \$nbLine);
#################################################
### Annotate the n part ###
#################################################
annotateFile($cpu, $filenameOK, $headerOriginalFile);
#################################################
### Paste the file together ###
#################################################
createOutput($filenameOK, $headerOriginalFile, $nbLine);
}
}
# The input file is one file
else
{
#################################################
### Cut the files in n part ###
#################################################
# Cut the file in n part depending on the number of lines and set the number of CPU to use for the annotation depending of the number of n parts
my $cpu = 0;
# Keep the original header
my $headerOriginalFile = "";
# Save the numer of lines
my $nbLine = 0;
splitInputFile("$folder_temp/$filename-ColumnCorrect.txt", \$cpu, \$headerOriginalFile, $filename, \$nbLine);
#################################################
### Annotate the n part ###
#################################################
annotateFile($cpu, $filename, $headerOriginalFile);
#################################################
### Paste the file together ###
#################################################
createOutput($filename, $headerOriginalFile, $nbLine);
}
# Remove the temporary directory
rmtree($folder_temp);
}
sub splitInputFile
{
my ($inputFile, $ref_cpu, $ref_header, $filename, $ref_nbLine) = @_;
my $nbVariants = `wc -l $inputFile`;
$nbVariants =~ /(\d+).+/;
$$ref_nbLine = $1;
if($$ref_nbLine-1 <= 5000) { $$ref_cpu = 1; }
elsif( ($$ref_nbLine-1 > 5000) && ($$ref_nbLine-1 < 25000) ) { $$ref_cpu = 2; }
elsif( ($$ref_nbLine-1 >= 25000) && ($$ref_nbLine-1 < 100000) ) { $$ref_cpu = 8; }
else { $$ref_cpu = $max_cpu; }
# If the number predefined can't be used on the machine use the maximum number specify by the administrator
if($$ref_cpu > $max_cpu) { $$ref_cpu = $max_cpu }
## Recover the header
open(F1, $inputFile) or die "$!: $inputFile\n";
$$ref_header= <F1>;
close F1;
## Remove the first line of the file
my $fileNoHeader = "$folder_temp/${filename}-NoHeader";
`sed 1d $inputFile > $fileNoHeader`;
if(!-e "$folder_temp/$filename") { mkdir("$folder_temp/$filename") or die "Can't create the directory $folder_temp/$filename\n"; }
my $lines_per_temp = int(1+($1 / $$ref_cpu)); # +1 in case of the div == 0
`split -l $lines_per_temp $fileNoHeader $folder_temp/$filename/$filename-`;
if($$ref_header eq "") { print STDERR "Error message:\n"; print STDERR "No header for the file $inputFile!!!\nPlease check the format of your file\n"; }
}
sub annotateFile
{
my ($cpu, $filename, $headerOriginalFile) = @_;
my $pm = Parallel::ForkManager->new($cpu);
foreach my $tempFile (`ls $folder_temp/$filename/$filename-*`)
{
chomp($tempFile);
# Forks and returns the pid for the child:
my $pid = $pm->start and next;
# Convert the file in a correct format for Annovar: Chr Start End Ref Alt Otherinfo
my ($filenameTempFile, $directoriesTempFile, $suffixTempFile) = fileparse($tempFile, qr/\-[^.]*/);
my $outFilenameTemp = $filenameTempFile.$suffixTempFile;
Convert2AV($tempFile, $chrValue, $positionValue, $refValue, $altValue, "$folder_temp/$outFilenameTemp-AVInput");
# Annotate the file with Annovar
my $tempFileName_AVOutput = $filename.$suffixTempFile.".".${refGenome}."_multianno.txt";
if($fullAVDB eq "yes") { AnnotateAV("$folder_temp/$outFilenameTemp-AVInput", "$folder_temp/$outFilenameTemp"); }
else { annotateAV_min("$folder_temp/$outFilenameTemp-AVInput", "$folder_temp/$outFilenameTemp"); }
# Check if the annotations worked
open(F1, "$folderMutAnalysis/log_annovar.txt") or die "$!: $folderMutAnalysis/log_annovar.txt\n";
while(<F1>)
{
if($_ =~ /ERROR/i)
{
print STDERR "\n\n\t\tANNOVAR LOG FILE\n\n";
print STDERR $_;
print STDERR "\n\n\t\tANNOVAR LOG FILE\n\n\n";
}
}
close F1;
# Recover the strand orientation
my $length_AVheader = 0;
RecoverStrand("$folder_temp/$tempFileName_AVOutput", $headerOriginalFile, $path_AVDB, $refGenome, "$folder_temp/$outFilenameTemp-Strand", \$length_AVheader);
# Recover the sequence context
RecoverGenomicSequence("$folder_temp/$outFilenameTemp-Strand", $length_AVheader, $intervalEnd, $refGenome, $path_AVDB, "$folder_temp/$filename/$outFilenameTemp".".".${refGenome}."_multianno.txt");
$pm->finish; # Terminates the child process
}
# Wait all the child process
$pm->wait_all_children;
}
sub createOutput
{
my ($filename, $headerOriginalFile, $nbLine) = @_;
## For MuTect and MuTect2 calling only variants passing MuTect filters are kept and sometines there is no variant passing these filters making error in Galaxy when using "collection".
if($nbLine == 1)
{
print STDOUT "\nThe sample $filename didn't pass MuTect filters\n";
### Print Annovar minimal header + the original header of the input file
my $outputFile = "$folderAnnovar/$filename".".".${refGenome}."_multianno.txt";
open(OUT, ">", $outputFile) or die "$!: $outputFile\n";
if($fullAVDB eq "no")
{
print OUT "Chr\tStart\tEnd\tRef\tAlt\tFunc.refGene\tGene.refGene\tGeneDetail.refGene\tExonicFunc.refGene\tAAChange.refGene\tStrand\tcontext";
print OUT "\t".$headerOriginalFile;
}
### Print complete Annovar header (using the database name present in the file listAVDB) + the original header of the input file
else
{
print OUT "Chr\tStart\tEnd\tRef\tAlt";
open(F1, $listAVDB) or die "$!: $listAVDB\n";
while(<F1>)
{
if($_ =~ /^#/) { next; }
my @tab = split("\t", $_);
$tab[0] =~ /$refGenome\_(.+)\.txt/;
my $dbName = $1;
if($dbName =~ /refGene|knownGene|ensGene/)
{
print OUT "\t"."Func.$dbName\tGene.$dbName\tGeneDetail.$dbName\tExonicFunc.$dbName\tAAChange.$dbName";
}
else
{
print OUT "\t".$dbName;
}
}
print OUT "\tStrand\tcontext\t".$headerOriginalFile;
close F1;
}
close OUT;
}
else
{
CombinedTempFile("$folder_temp/$filename", "$folderAnnovar/$filename".".".${refGenome}."_multianno.txt");
}
}
sub Convert2AV
{
my ($inputFile, $chr_value, $start_value, $ref_value, $alt_value, $output) = @_;
my ($filename, $directories, $suffix) = fileparse($inputFile, qr/\.[^.]*/);
open(F1, $inputFile) or die "$!: $inputFile\n";
open(OUT, ">", $output) or die "$!: $output\n";
while(<F1>)
{
$_ =~ s/[\r\n]+$//;
my @tab = split("\t", $_);
my $chr = "";
# Replace chr23 or chr24 by X or Y
if($tab[$chr_value] =~ /23/) { $chr = "chrX"; }
elsif($tab[$chr_value] =~ /24/) { $chr = "chrY"; }
elsif($tab[$chr_value] =~ /chr/) { $chr = $tab[$chr_value]; }
else { $chr = "chr".$tab[$chr_value]; }
### Consider only "normal" chromosomes for the annotation
if( ($chr !~ /chr\d{1,2}$|chrX|chrY/) ) { next; }
### Don't consider variants with two or more alt bases
if($tab[$alt_value] =~ /\,/) { next; }
### Reformat the Indels for Annovar
# chr1 85642631 C CT => chr1 85642631 85642631 - T (mm10)
# chr5 26085724 ACTT A => chr5 26085725 26085727 CTT - (mm10)
if( ((length($tab[$ref_value]) != 1) || (length($tab[$alt_value]) != 1)) || (($tab[$ref_value] eq "-") || ($tab[$alt_value] eq "-") ) )
{
### First check if the indels in the file are not already correctly formated
if( ($tab[$ref_value] eq "-") || ($tab[$alt_value] eq "-") )
{
# For indels count the number of bases deleted or inserted for modifying the end position (if start + end is the same the annotations are not retrieved for indels)
# Insertion: start = start & end = start
if($tab[$ref_value] =~ /\-/)
{
print OUT "$chr\t$tab[$start_value]\t$tab[$start_value]\t$tab[$ref_value]\t$tab[$alt_value]";
}
## Deletion: start = start & end = start + length(del) -1
else
{
my $end = $tab[$start_value] + (length($tab[$ref_value]) - 1);
print OUT "$chr\t$tab[$start_value]\t$end\t$tab[$ref_value]\t$tab[$alt_value]";
}
}
### Indels not correctly formated for Annovar
else
{
my @tabRef = split("", $tab[$ref_value]);
my @tabAlt = split("", $tab[$alt_value]);
# Remove the first base
my $ref2 = join("", @tabRef[1 .. $#tabRef]);
my $alt2 = join("", @tabAlt[1 .. $#tabAlt]);
if(length($alt2) == 0)
{
my $altOK = "-";
my $startOK = $tab[$start_value] + 1;
my $stopOK = $startOK + length($ref2) - 1;
print OUT $chr."\t".$startOK."\t".$stopOK."\t".$ref2."\t".$altOK;
}
if(length($ref2) == 0)
{
my $refOK = "-";
print OUT $chr."\t".$tab[$start_value]."\t".$tab[$start_value]."\t".$refOK."\t".$alt2;
}
}
}
### SBS
else
{
print OUT $chr."\t".$tab[$start_value]."\t".$tab[$start_value]."\t".$tab[$ref_value]."\t".$tab[$alt_value];
}
## Print the original file at the end
foreach (@tab) { print OUT "\t$_"; }
print OUT "\n";
}
close F1; close OUT;
}
sub AnnotateAV
{
my ($inputFile, $output) = @_;
if(!-e $path_AVDB)
{
print STDERR "Error message:\n";
print STDERR "The Annovar database doesn't exists for the reference genome $refGenome!!!\n";
print STDERR "Please install the database for this genome before running Annovar\n";
}
# Extract the name of the databases
my $protocol = ""; my $operation = "";
ExtractAVDBName($listAVDB, \$protocol, \$operation);
`table_annovar.pl $inputFile $path_AVDB -buildver $refGenome -protocol $protocol -operation $operation -remove -nastring NA -otherinfo -outfile $output > $folderMutAnalysis/log_annovar.txt 2>&1`;
sub ExtractAVDBName
{
my ($listAVDB, $refS_protocol, $refS_operation) = @_;
open(F1, $listAVDB) or die "$!: $listAVDB\n";
while(<F1>)
{
if ($_ =~ /^#/) { next; }
$_ =~ s/[\r\n]+$//;
my @tab = split("\t", $_);
# db name like refGenome_dbName.txt
if( ($tab[0] =~ /\w+_(\w+)\.txt/) && ($tab[0] !~ /sites/) && ($tab[0] !~ /esp/) && ($tab[0] !~ /ljb26/) )
{
$$refS_protocol .= $1.","; $$refS_operation .= $tab[1].",";
}
# 1000 genome
if($tab[0] =~ /sites/)
{
$tab[0] =~ /\w+_(\w+)\.sites.(\d+)_(\d+)\.txt/;
my ($dbName, $year, $month) = ($1, $2, $3);
$dbName =~ tr/A-Z/a-z/;
# convert the month number into the month name
ConvertMonth(\$month);
my $AVdbName_final = "1000g".$year.$month."_".$dbName;
$$refS_protocol .=$AVdbName_final.","; $$refS_operation .= $tab[1].",";
}
# ESP
if( ($tab[0] =~ /esp/) || ($tab[0] =~ /ljb26/) )
{
$tab[0] =~ /\w+_(\w+)_(\w+)\.txt/;
my $AVdbName_final = $1."_".$2;
$$refS_protocol .=$AVdbName_final.","; $$refS_operation .= $tab[1].",";
}
}
close F1;
sub ConvertMonth
{
my ($refS_month) = @_;
if($$refS_month == 1) { $$refS_month = "janv"; }
elsif($$refS_month == 2) { $$refS_month = "feb"; }
elsif($$refS_month == 3) { $$refS_month = "mar"; }
elsif($$refS_month == 4) { $$refS_month = "apr"; }
elsif($$refS_month == 5) { $$refS_month = "may"; }
elsif($$refS_month == 6) { $$refS_month = "jun"; }
elsif($$refS_month == 7) { $$refS_month = "jul"; }
elsif($$refS_month == 8) { $$refS_month = "aug"; }
elsif($$refS_month == 9) { $$refS_month = "sept"; }
elsif($$refS_month == 10) { $$refS_month = "oct"; }
elsif($$refS_month == 11) { $$refS_month = "nov"; }
elsif($$refS_month == 12) { $$refS_month = "dec"; }
}
}
}
### Add the minimum of annotations (refGene + strand + context)
sub annotateAV_min
{
my ($inputFile, $output) = @_;
if(!-e $path_AVDB)
{
print STDERR "Error message:\n";
print STDERR "The Annovar database doesn't exists for the reference genome $refGenome!!!\n";
print STDERR "Please install the database for this genome before running Annovar\n";
}
# Extract the name of the databases
my ($protocol, $operation) = ("refGene", "g");
`table_annovar.pl $inputFile $path_AVDB -buildver $refGenome -protocol $protocol -operation $operation -remove -nastring NA -otherinfo -outfile $output > $folderMutAnalysis/log_annovar.txt 2>&1`;
}
sub RecoverStrand
{
my ($input, $headerOriginalFile, $pathDB, $refGenome, $output, $refS_lengthAVheader) = @_;
my ($chr_value, $start_value, $ref_value, $alt_value, $func_value, $geneSymbol_value) = ("", "", "", "", "", "", "", "");
$chr_value = recoverNumCol($input, "Chr");
$start_value = recoverNumCol($input, "Start");
$ref_value = recoverNumCol($input, "Ref");
$alt_value = recoverNumCol($input, "Alt");
$func_value = recoverNumCol($input, "Func.refGene");
$geneSymbol_value = recoverNumCol($input, "Gene.refGene");
#################### Convert the input file into a hash table
my %h_inputFile = ();
open(F1, $input) or die "$!: $input\n";
my $annovar_header = <F1>;
while(<F1>)
{
$_ =~ s/[\r\n]+$//;
my @tab = split("\t", $_);
# In COSMIC the chromosome X and Y are annotated 23 and 24
my $chr = "";
if($tab[$chr_value] eq "chr23") { $chr = "chrX"; }
elsif($tab[$chr_value] eq "chr24") { $chr = "chrY"; }
elsif($tab[$chr_value] eq "chr25") { $chr = "chrM"; }
else { $chr = $tab[$chr_value]; }
# Verify if the element exists
if($chr eq "") { print STDERR "Error message:\n"; print STDERR "Error RecoverStrand: The chromosome value is nor defined for $_\n"; }
if(! exists $tab[$start_value]) { print STDERR "Error message:\n"; print STDERR "Error RecoverStrand: The start value is nor defined for $_\n"; }
if(! exists $tab[$ref_value]) { print STDERR "Error message:\n"; print STDERR "Error RecoverStrand: The reference value is nor defined for $_\n"; }
if(! exists $tab[$alt_value]) { print STDERR "Error message:\n"; print STDERR "Error RecoverStrand: The alternate value is nor defined for $_\n"; }
if(! exists $tab[$func_value]) { print STDERR "Error message:\n"; print STDERR "Error RecoverStrand: The functional value is nor defined for $_\n"; }
if(! exists $tab[$geneSymbol_value]) { print STDERR "Error message:\n"; print STDERR "Error RecoverStrand: The gene symbol value is nor defined for $_\n"; }
my $geneSymbol = "";
######## For the splicing annotation we separate the gene symbol from the aa change
if($tab[$func_value] eq "splicing")
{
if($tab[$geneSymbol_value] =~ /(.+)\((.+)\)/) { $geneSymbol = $1; }
else { $geneSymbol = $tab[$geneSymbol_value]; }
}
else { $geneSymbol = $tab[$geneSymbol_value]; }
push(@{$h_inputFile{"$chr:$tab[$start_value]:$tab[$start_value]:$tab[$ref_value]:$tab[$alt_value]:$geneSymbol"}}, $_);
}
close F1;
# print "\t\tRecoverStrand: $input\n";
#################### Convert the database file into a hash table
my %h_database = ();
my ($db_geneSymbol_value, $db_strandInfo_value, $db_chr_value) = (12, 3, 2);
my $folderNameDB = $refGenome."db";
my $fileNameDB = $refGenome."_refGene.txt";
open(F1, "$pathDB/$fileNameDB") or die "$!: $pathDB/$fileNameDB\n";
while(<F1>)
{
$_ =~ s/[\r\n]+$//;
my @tab = split("\t", $_);
my $strand = "";
$strand = $tab[$db_strandInfo_value];
if($strand eq "") { print STDERR "Error message:\n"; print STDERR "Error: the strand orientation is not specify in the database refGene\n$_\n"; }
else
{
# Some genes have several strand orientation, keep the first in the database
if(! exists $h_database{"$tab[$db_geneSymbol_value]:$tab[$db_chr_value]"}) { $h_database{"$tab[$db_geneSymbol_value]:$tab[$db_chr_value]"} = $strand; }
}
}
close F1;
#################### Parse the two hash tables for recover the strand information
open(OUT, ">", $output) or die "$!: $output\n";
## Add the header only for the firts part of the files
if($input =~ /\-aa/)
{
my @tabHeaderInput = "";
$annovar_header =~ s/[\r\n]+$//; @tabHeaderInput = split("\t", $annovar_header);
# Save the length of the Annovar header for the next function (RecoverGenomicSequence)
$$refS_lengthAVheader = $#tabHeaderInput;
# Print the Annovar header until the column before OtherInfo
print OUT "$tabHeaderInput[0]";
for(my $i=1; $i<$#tabHeaderInput; $i++) { print OUT "\t$tabHeaderInput[$i]"; }
print OUT "\tStrand";
print OUT "\t",$headerOriginalFile;
}
# Timer for comparing the number of SNVs present in the hash table
my $timerUniqueSNVs = 0;
# Timer for comparing the number of SNVs with the strand orientation
my $timerSNVsStrand = 0;
foreach my $kFile (sort keys %h_inputFile)
{
my $test = 0;
my @tab = split(":", $kFile);
# Sometimes the line is not printed correctely !!!!! :@
my @tHeaderInput = split("\t", $annovar_header); my @lengthLine = split("\t", $h_inputFile{$kFile}[0]);
my @tHeaderOriginalFile = split("\t", $headerOriginalFile);
my $lengthHeader = @tHeaderInput + (scalar(@tHeaderOriginalFile)-1) ; my $lengthLine = @lengthLine;
# Save the length of the Annovar header for the next function (RecoverGenomicSequence)
$$refS_lengthAVheader = $#tHeaderInput;
foreach my $kDB (sort keys %h_database)
{
if("$tab[5]:$tab[0]" eq $kDB)
{
if($lengthHeader != $lengthLine)
{
print STDERR "Error message:\n";
print STDERR "Error Recover Strand the length of the current line is not valid!!!!!\nExpected length: $lengthHeader\tlength of the line: $lengthLine\n$h_inputFile{$kFile}[0]\n";
}
foreach my $line (@{$h_inputFile{$kFile}})
{
my @tab = split("\t", $line);
my $j = 0;
for(my $i=0; $i<$#tHeaderInput; $i++) { print OUT $tab[$i],"\t"; $j=$i }
print OUT $h_database{$kDB};
for(my $i=$j+1; $i<=$#tab; $i++) { print OUT "\t$tab[$i]"; }
print OUT "\n";
}
$timerSNVsStrand++;
$test = 1; last;
}
}
# The strand orientation isn't defined
if($test == 0)
{
my @tHeaderInput = split("\t", $annovar_header);
foreach my $line (@{$h_inputFile{$kFile}})
{
my @tab = split("\t", $line);
my $j = 0;
for(my $i=0; $i<$#tHeaderInput; $i++) { print OUT $tab[$i],"\t"; $j=$i }
print OUT "NA";
for(my $i=$j+1; $i<=$#tab; $i++) { print OUT "\t$tab[$i]"; }
print OUT "\n";
}
$timerSNVsStrand++;
}
$timerUniqueSNVs++;
}
close OUT;
# print "Strand orientation recover for $timerSNVsStrand SNVs out of $timerUniqueSNVs uniques\n";
}
sub RecoverGenomicSequence
{
my ($inputFile, $length_AVheader, $intervalEnd, $referenceGenome, $pathToRefSeq, $output) = @_;
############ 1) Transform the input file in a hash table: one for recover the sequence context and one for keeping the original file
my %h_inputFileForSeqContext = (); my %h_inputFile = ();
my $header = "";
CreateHashTable_from_InputFile($inputFile, $length_AVheader, \$header, $intervalEnd, \%h_inputFileForSeqContext, \%h_inputFile);
sub CreateHashTable_from_InputFile
{