-
Notifications
You must be signed in to change notification settings - Fork 0
/
ic.pl
1832 lines (1616 loc) · 40.9 KB
/
ic.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/perl
# If any file header changes then search for calls to get_header to update the names, change in future to read from a file
# requires file with the mapping of directory to study/cohort name
#
#
# W.Rayner 2016
# wrayner@well.ox.ac.uk
#
# Requires GD to be installed on the system for the plotting
# - needs libgd, for ubuntu: sudo apt-get -y install libgd2-xpm-dev build-essential
# - needs GD cpan GD::Graph
# -
# -
use strict;
use warnings;
use GD::Graph::points;
use Getopt::Long;
use GD::Graph::hbars;
use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
use Time::localtime;
use File::stat;
use File::Basename;
use File::Path qw(make_path);
$| = 1;
my %altaf;
my %directorylist;
my @gwsummary;
my $reffile;
my $hrcflag = 0;
my $kgflag = 0;
my $population = 'ALL';
my $directory; # Top level directory containing files or sub-folders with files
my $infile;
my %directorymapping;
my $outputpath = 'outputDir';
my @allGWinfofiles;
my $columns = get_width();
my $mid = int($columns/2+0.5);
print "\n\n";
printf("%*s", $mid+12, "IC: a program to check imputed data files\n");
printf("%*s", $mid+5, "William Rayner 2016\n");
printf("%*s", $mid+6, "wrayner\@well.ox.ac.uk\n");
print "\n";
printf("%*s", $mid+5, "Version 1.0.2\n\n\n");
GetOptions
(
"f|file=s" => \$infile, # file containing folder to name mappings
"d|directory=s" => \$directory, # top level directory for the imputed data files
"h|hrc" => \$hrcflag, # flag to set HRC reference panel is being used
"r|ref=s" => \$reffile, # input reference file name (1000G or HRC)
"g|1000g" => \$kgflag, # flag to set 1000G reference panel is being used
"p|pop=s" => \$population, # 1000G population frequency to use in the checks
"o|output=s" => \$outputpath # Set path to an output directory, will be created if it doesn't exist
);
if (!$reffile)
{
print "No reference file specified\n";
usage();
exit;
}
if (!$directory)
{
print "No directory containing imputed summary files specified\n";
usage();
exit;
}
if (!$hrcflag and !$kgflag)
{
$hrcflag = 1;
print "No reference panel type specified, assuming HRC\n";
}
# get run date
my $dt = get_time();
# open Logfile
my $logfile = 'ic-'.$dt.'.log';
make_directory($outputpath);
$logfile = $outputpath.'/'.$logfile;
open L, ">$logfile" or die $!;
print "Options Specified:\n";
print "Directory path: $directory\n";
if ($hrcflag)
{
print "Reference: HRC\n";
print "Reference path: $reffile\n";
}
elsif ($kgflag)
{
print "Reference: 1000G\n";
print "Reference path: $reffile\n";
print "Population: $population\n";
}
print "Output Saved in: $outputpath\n";
if ($infile)
{
print "Mapping file: $infile\n";
}
print "\nLog file: $logfile\n\n";
# find all files
my @filestoprocess = find_all_files($directory);
if ($#filestoprocess > 0) #if there are no files to process, don't continue
{
# list mapping directory names to study names
if ($infile)
{
print "Directory mapping file: $infile\n";
%directorymapping = get_listing($infile);
}
else #if no mapping file for directory is given take top most directory as name
{
print "No directory mapping file, creating a directory mapping\n";
%directorymapping = get_mapping(\@filestoprocess);
my @k = keys %directorymapping;
print "Using: (name -> path)\n";
foreach my $s (@k)
{
print "$directorymapping{$s} -> $s\n";
}
print "\n";
}
if ($hrcflag)
{
my($filename, $dirs, $suffix) = fileparse($reffile);
print "Reading HRC file $filename from $dirs\n";
%altaf = read_hrc($reffile);
}
elsif ($kgflag)
{
my($filename, $dirs, $suffix) = fileparse($reffile);
print "Reading 1000G file $filename from $dirs\nUsing $population population\n";
%altaf = read_kg($reffile, $population);
}
# extract the data from each per chromosome file and invoke the plotting
%directorylist = process_data(\@filestoprocess, $outputpath);
my @directorylisting = keys %directorylist;
foreach my $subdir (@directorylisting)
{
print "Final processing: $subdir\n";
my $infofile = create_genome_wide_summary($subdir); #create summaries and return info summary file name for across cohort summary
push (@allGWinfofiles, $infofile);
study_date_upload($subdir, \@filestoprocess, \%directorymapping); #adds a file with the date of upload of the cohort based on date modified time
}
#plot genome wide info for across cohort summary
my $outfile = $outputpath.'/Summary-info-test-temp.txt';
my $allinfofile = $outputpath.'/Summary-info-test.txt';
#my @files = find_info_files($outputpath, $outfile);
open AO, ">$allinfofile" or die $!;
foreach my $file (@allGWinfofiles)
{
my $line = get_gw_counts($file);
print AO "$line";
}
close AO;
plot_summary($allinfofile, $outputpath);
my $summaryfolder = $outputpath.'/summaryOutput';
my $pathtoscript = $0;
my($scriptfile, $scriptdirs, $scriptsuffix) = fileparse($pathtoscript);
my $cmd = "java -Xmx4g -classpath $scriptdirs/Junk.jar uk.ac.ox.well.t2d.reports.imputation.Main $outputpath $summaryfolder";
print "$cmd\n";
system ($cmd);
}
else
{
print "No files found to process, exiting\n";
}
sub get_mapping
{ #derive from a directory name
my @files = @{$_[0]};
my %mapping;
my %map;
my $k = 0;
my @all;
my $max = 0;
my @maps;
foreach my $path (@files)
{
my($filename, $dirs, $suffix) = fileparse($path);
$dirs =~ s/^\///;
#print "$dirs\n";
$map{$dirs} = 1;
}
@maps = keys(%map);
foreach my $m (@maps)
{
my @directories = split(/\//, $m);
for (my $i = 0; $i <= $#directories; $i++)
{
$all[$i][$k] = $directories[$i];
if ($i > $max)
{
$max = $i;
}
}
$k++;
}
for (my $j = 0; $j <= $max; $j++)
{
my %temp;
for (my $i = 0; $i < $k; $i++)
{
if ($all[$j][$i])
{
$temp{$all[$j][$i]} = 1;
}
}
my @temparray = keys %temp;
my $count = @temparray;
if ($count == $k)
{
foreach my $m (@maps)
{
my @directories = split(/\//, $m);
$mapping{$m} = $directories[$j];
}
}
}
return %mapping;
}
sub study_date_upload
{
my $directory = $_[0];
my @files = @{$_[1]};
my %mapping = %{$_[2]};
my @directories = split(/\//, $directory);
my $finaldir = $directories[$#directories];
$finaldir =~ /(.*?)\.(.*?)/;
#my $study = $1;
#$directory =~ /(.*?)\.(.*?)/;
my $studyfromdirname = $1;
foreach my $file (@files)
{
my $studyfromfile = get_study($file, \%mapping);
if ($studyfromfile eq $studyfromdirname)
{
my $month = (localtime(stat($file)->mtime)->mon) + 1;
my $day = localtime(stat($file)->mtime)->mday;
my $year = (localtime(stat($file)->mtime)->year) + 1900;
my $timestamp = "$day.$month.$year";
my $studyfile = $directory.'/Meta-'.$studyfromdirname.'.txt';
open O, ">>$studyfile" or die $!;
print O "UPLOADED\t$timestamp\n";
close O;
}
}
close IN;
}
sub create_genome_wide_summary
{
my $directory = $_[0];
opendir(DIR, "$directory") or die "Not a directory $directory";
my @all_files = grep !/^\.\.?\z/, readdir DIR;
my %info;
my %af;
my %counts;
foreach my $file (@all_files)
{
my $filepath = $directory.'/'.$file;
if ($file =~ /^(.*?)-(.*?)\.chr(.*?)\.txt$/)
{
#print "$file\n";
my $type = $1;
my $study = $2;
my $chr = $3;
my $flag = 0;
open IN, "$filepath" or die $!;
while (<IN>)
{
chomp;
my @temp = split/\t/;
if ($type eq 'AF')
{
$af{$temp[0]} += $temp[1];
}
elsif ($type eq 'INFO')
{
$info{$temp[0]} += $temp[1];
}
elsif ($type eq 'COUNTS')
{
if ($flag)
{
$counts{'NOMATCH'} += $temp[0];
}
else
{
$counts{'MATCH'} += $temp[0];
$flag = 1;
}
}
else
{
# unknown file type
print "WARNING: $file unknown type\n";
}
}
close IN;
}
}
#print All summaries
my @directories = split(/\//, $directory);
my $finaldir = $directories[$#directories];
$finaldir =~ /(.*?)\.(.*?)/;
my $study = $1;
my $affile = $directory.'/AF-'.$study.'.GW.txt';
my $infofile = $directory.'/INFO-'.$study.'.GW.txt';
my $countsfile = $directory.'/COUNTS-'.$study.'.GW.txt';
print_summary($affile, \%af);
print_summary($infofile, \%info);
print_summary($countsfile, \%counts);
return $infofile;
}
sub print_summary
{
my $outfile = $_[0];
my %data = %{$_[1]};
my $total = 0;
my @items = keys %data;
my @sorted = sort {$a cmp $b} @items;
if ($data{'Total'})
{
$total = $data{'Total'};
}
else
{
for (my $i = 0; $i <= $#sorted; $i++)
{
$total += $data{$sorted[$i]};
}
}
open OUT, ">$outfile" or die $!;
for (my $i = 0; $i <= $#sorted; $i++)
{
my $pct = sprintf("%0.3f", $data{$sorted[$i]}/$total * 100);
print OUT "$sorted[$i]\t$data{$sorted[$i]}\t$pct\n";
}
close OUT;
}
sub read_hrc
{
my %altaf;
my $file = $_[0];
open IN, "$file" or die $!;
while (<IN>)
{
chomp;
if (!/\#.*/)
{
if ($. % 100000 == 0)
{
print L " $.\n";
}
my @temp = split/\s+/;
my $refaltchrpos = $temp[0].':'.$temp[1].'_'.$temp[3].'_'.$temp[4];
$altaf{$refaltchrpos} = $temp[7];
}
}
close IN;
return %altaf;
}
sub process_data #add new calls to subroutines here for further functionality
{
my @filelisting = @{$_[0]};
my $outputdir = $_[1];
my %genomewidelist;
for (my $i = 0; $i <= $#filelisting; $i++)
{
my $format = detect_format($filelisting[$i]); #format can be OXFORD, MINIMAC, SANGER, MINIMAC2 or UNKNOWN.
#print "$filelisting[$i]\t$format\n";
if ($format ne 'UNKNOWN' and $format ne 'MINIMAC2') #MINIMAC2 used when processing MINIMAC so ignore here
{
my $file;
my $study = get_study($filelisting[$i], \%directorymapping);
my $newdir = $outputdir.'/'.$study.'.'.$dt;
make_directory($newdir);
my $datadir = $newdir.'/RAW';
make_directory($datadir);
my $formatfile = $newdir.'/Meta-'.$study.'.txt';
open O, ">>$formatfile" or die $!;
print O "FORMAT\t$format\n";
close O;
$genomewidelist{$newdir} = 1;
if ($format eq 'OXFORD')
{
print L "$filelisting[$i]\t$format\t$study\n";
$file = process_oxford($filelisting[$i], $study, $datadir, $newdir);
}
elsif ($format eq 'SANGER')
{
print L "$filelisting[$i]\t$format\t$study\n";
$file = process_sanger($filelisting[$i], $study, $datadir, $newdir);
}
elsif ($format eq 'MINIMAC')
{
print L "$filelisting[$i]\t$format\t$study\n";
$file = process_minimac($filelisting[$i], $study, $datadir, $newdir);
}
print "$filelisting[$i]\t$format\t$study\n";
#start summarising file
my $afplotfile = plot_af($file, $study, $newdir);
my $manhfile = plot_manh_info($file, $study, $newdir);
my $afinfofile = plot_af_info($file, $study, $newdir);
my $summaryinfofile = get_counts($file, $study, $newdir, 1, 'INFO');
my $summaryinfoimage = plot_counts($summaryinfofile, $study, $newdir, 'INFO Score');
my $summaryaffile = get_counts($file, $study, $newdir, 3, 'AF');
my $summaryafimage = plot_counts($summaryaffile, $study, $newdir, 'Alt Allele Frequency');
my $positionfile = plot_position_row($file, $study, $newdir);
print L "$file\t$study\t$newdir\n";
print L "Manhattan Info plot: $manhfile\n";
print L "AF plot: $afplotfile\n";
print L "MAF vs INFO plot: $afinfofile\n";
print L "Info summary: $summaryinfofile\n";
print L "Allele Frequency summary: $summaryaffile\n";
print L "Position check file: $positionfile\n";
print "Manhattan Info plot: $manhfile\n";
print "AF plot: $afplotfile\n";
print "MAF vs INFO plot: $afinfofile\n";
print "Info summary: $summaryinfofile\n";
print "Allele Frequency summary: $summaryaffile\n";
print "Position check file: $positionfile\n";
}
}
return %genomewidelist;
}
sub get_counts
{
my $file = $_[0];
my $study = $_[1];
my $outdir = $_[2];
my $col = $_[3];
my $type = $_[4];
my %counts;
#my $chr = get_chr($file, '2');
my $total = 0;
$file =~ /.*\/(.*?)\.txt$/;
my $outfile = $outdir.'/'.$type.'-'.$1.'.txt';
open IN, "$file" or die $!;
while (<IN>)
{
chomp;
my @temp = split/\t/;
for (my $i = 10; $i >= 0; $i--)
{
if (int($temp[$col]*10) == $i)
{
$counts{$i}++;
$total++;
$i = 0;
}
}
}
open OUT, ">$outfile" or die $!;
my @c = keys %counts;
my @cs = sort {$a <=> $b} @c;
for (my $i = 0; $i <= $#cs; $i++)
{
my $band = sprintf("%0.1f",$cs[$i]/10);
my $pct = sprintf("%0.4f", $counts{$cs[$i]}/$total*100);
print OUT "$band\t$counts{$cs[$i]}\t$pct\n";
}
print OUT "Total\t$total\n";
close IN;
close OUT;
return $outfile;
}
sub make_directory
{
my $dir = $_[0];
if (-e $dir)
{
#print L "Output directory $dir exists, will not recreate\n";
}
else
{
make_path($dir);
#print L "Creating output directory $dir\n";
}
}
sub get_listing
{
my $file = $_[0];
my %directories;
open IN, "$file" or die $!;
while (<IN>)
{
chomp;
my @temp = split/\t/;
$temp[1] =~ s/\s+//g; #remove all spaces from name as these will cause problems
$directories{$temp[0]} = $temp[1];
}
return %directories;
}
sub get_study
{
my $filename = $_[0];
my %directories= %{$_[1]};
my $study;
my @path = split(/\//, $filename);
for(my $i = 0; $i <= $#path; $i++)
{
if ($directories{$path[$i]})
{
$study = $directories{$path[$i]};
}
}
if (!$study)
{
my($file, $dirs, $suffix) = fileparse($filename);
$dirs =~ s/^\///;
#print "$dirs\n";
$study = $directories{$dirs};
}
if (!$study) # all attempts at lookup have not worked, assign a name
{
$study = 'STUDY';
}
return $study;
}
sub get_time
{
my $y = localtime->year() + 1900;
my $d = localtime->mday();
my $m = localtime->mon() + 1;
my $h = localtime->hour();
my $min = localtime->min();
my $dt = $d.'-'.$m.'-'.$y; #.'.'.$h.'h'.$min.'m';
return $dt;
}
sub detect_format
{
my $file = $_[0];
my $gzipped = checkgz($file);
my $format;
my $z;
my $dose = 0;
my $oxf = 0;
my $chrom = 0;
if ($gzipped eq -1)
{
$format = 'ZIPPED';
return $format;
}
elsif ($gzipped)
{
$z = new IO::Uncompress::Gunzip "$file" or die "IO::Uncompress::Gunzip failed: $GunzipError\n";
}
else
{
open $z, "$file" or die $!;
}
my $header = <$z>;
chomp $header;
my @temp = split(/\t/, $header);
for (my $i = 0; $i <= $#temp; $i++)
{
if ($temp[$i] eq 'Dose1')
{
$dose = 1;
}
if ($temp[$i] eq 'SNPID')
{
$oxf = 1;
}
if ($temp[$i] eq '#CHROM')
{
$chrom = 1;
}
}
#if ($temp[0] eq 'SNPID')#changed to allow column order to be random
if ($oxf)
{
$format = 'OXFORD';
}
#elsif ($temp[0] eq '#CHROM')
elsif ($chrom)
{
my $dataline = <$z>;
chomp $dataline;
my @entries = split(/\t/, $dataline);
my @info = split(/;/, $entries[7]);
for (my $i = 0; $i <= $#info; $i++)
{
if ($info[$i] =~ /^R2\=.*/)
{
$format = 'MINIMAC';
}
elsif ($info[$i] =~ /^INFO\=.*/)
{
$format = 'SANGER';
}
}
if (!$format)
{
$format = 'UNKNOWN';
}
}
elsif ($temp[0] eq 'SNP' and $dose)
{
$format = 'MINIMAC2';
}
else
{
$format = 'UNKNOWN';
}
close $z;
return $format;
}
sub checkgz
{
my $file = $_[0];
my @filecomponents = split(/\./, $file);
my $zipped = 0;
if ($filecomponents[$#filecomponents] eq 'gz')
{
$zipped = 1;
}
elsif ($filecomponents[$#filecomponents] eq 'zip')
{
print L "WARNING: .zip format is not supported, skipping $file\n";
$zipped = -1;
}
else
{
$zipped = 0;
}
return $zipped;
}
sub find_all_files
{
my $path = $_[0];
my @filelist;
# Open the directory.
if (opendir (DIR, $path)) #or die "Unable to open $path: $!";
{
# Read files
my @files = grep { !/^\.{1,2}$/ } readdir (DIR);
# Close the directory.
closedir (DIR);
@files = map { $path . '/' . $_ } @files;
foreach my $file (@files)
{
if (-d $file) # If it is a directory
{
push @filelist, find_all_files($file);
}
else # If it isn't a directory
{
push @filelist, $file;
}
}
}
return @filelist;
}
sub get_header
{
my $line = $_[0];
my $header = $_[1];
my $column;
my @allheaders = split(/\s+/, $line);
for (my $i = 0; $i <= $#allheaders; $i++)
{
if ($header eq $allheaders[$i])
{
$column = $i;
}
}
return $column;
}
sub get_chr
{
my $file = $_[0];
my $column = $_[1];
my $chr;
my $z;
my $idcol = 0; #default is first column
my $gzipped = checkgz($file);
my $flag = 0;
if ($gzipped)
{
$z = new IO::Uncompress::Gunzip "$file" or die "IO::Uncompress::Gunzip failed: $GunzipError\n";
}
else
{
open $z, "$file" or die $!;
}
my $titleline = <$z>;
chomp $titleline;
my @titles = split(/\t/, $titleline);
if ($column > $#titles) #if column index passed is too large, try finding the id column, based on format, all are currently column 0
{
$flag = 1;
$column = 0;
my $format = detect_format($file);
if ($format eq 'OXFORD' or $format eq 'MINIMAC2' or $format eq 'SANGER' or $format eq 'MINIMAC')
{
$idcol = 0;
}
}
my $line = <$z>;
chomp $line;
my @temp = split(/\t/, $line);
if ($flag or $temp[$column] eq 'NA') #using ID as chr identifier
{
$temp[$idcol] =~ /^(.*?)\:.*$/;
$chr = $1;
if (!$chr) #no match as SNP is directly typed (so no position added to id)
{
$chr = $temp[$idcol];
}
}
else
{
$chr = $temp[$column];
}
close $z;
$chr =~ s/^0//;
print L "$file is Chromosome $chr\n";
return $chr;
}
sub process_minimac
{
my $file = $_[0];
my $study = $_[1];
my $datadir = $_[2];
my $outdir = $_[3];
#my @allfiles = @{$_[4]};
my $match = 0;
my $nomatch = 0;
my $afflag = 'AF';
my $infoflag = 'R2';
my $af;
my $info;
my $an;
my $gzipped = checkgz($file);
my $z;
if ($gzipped)
{
$z = new IO::Uncompress::Gunzip "$file" or die "IO::Uncompress::Gunzip failed: $GunzipError\n";
}
else
{
open $z, "$file" or die $!;
}
my $titleline = <$z>;
chomp $titleline;
my $chrcol = get_header($titleline, '#CHROM');
my $poscol = get_header($titleline, 'POS');
my $infocol = get_header($titleline, 'INFO');
my $refcol = get_header($titleline, 'REF');
my $altcol = get_header($titleline, 'ALT');
my $idcol = get_header($titleline, 'ID');
my $chr = get_chr($file, $chrcol);
my $newfile = $datadir.'/'.$study.'.chr'.$chr.'.txt';
my $outfile = set_outfilename($newfile);
open OUT, ">$outfile" or die $!;
while (<$z>)
{
chomp;
my @temp = split/\s+/;
my @info = split(/\;/, $temp[$infocol]);
for (my $i = 0; $i <= $#info; $i++)
{
if ($info[$i] =~ /$infoflag\=(.*)/)
{
$info = $1;
}
if ($info[$i] =~ /^$afflag\=(.*)/)
{
$af = $1;
}
}
if ($info < 0) #required as some info scores are negative
{
$info = 0;
}
#check id is present in HRC (%altaf) and add Alternate AF to the data file
if (!$af)
{
print L "WARNING: allele ";
}
my $id = $temp[$chrcol].':'.$temp[$poscol].'_'.$temp[$refcol].'_'.$temp[$altcol];
print OUT "$temp[$poscol]\t$info\t$temp[$chrcol]\t$af\t$temp[$refcol]\t$temp[$altcol]";
if ($altaf{$id})
{
print OUT "\t$altaf{$id}\n";
$match++;
}
else
{
print OUT "\t\n";
$nomatch++;
}
}
$outfile =~ /.*\/(.*?)\.txt$/;
my $countsfile = $outdir.'/COUNTS-'.$1.'.txt';
open O, ">$countsfile" or die;
print O "$match\n$nomatch\n";
close O;
close $z;
close OUT;
return $outfile;
}
sub process_sanger
{
my $file = $_[0];
my $study = $_[1];
my $datadir = $_[2];
my $outdir = $_[3];
my $acflag = 'AC';
my $allelenumber = 'AN';
my $infoflag = 'INFO';
my $ac;
my $info;
my $an;
my $z;
my $match = 0;
my $nomatch = 0;
my $gzipped = checkgz($file);
my $af;
if ($gzipped)
{
$z = new IO::Uncompress::Gunzip "$file" or die "IO::Uncompress::Gunzip failed: $GunzipError\n";
}
else
{
open $z, "$file" or die $!;
}
my $titleline = <$z>;
chomp $titleline;
my $chrcol = get_header($titleline, '#CHROM');
my $poscol = get_header($titleline, 'POS');
my $infocol = get_header($titleline, 'INFO');
my $refcol = get_header($titleline, 'REF');
my $altcol = get_header($titleline, 'ALT');
my $chr = get_chr($file, $chrcol);
my $newfile = $datadir.'/'.$study.'.chr'.$chr.'.txt';
my $outfile = set_outfilename($newfile);
open OUT, ">$outfile" or die $!;
while (<$z>)
{
chomp;
my @temp = split/\s+/;
my @info = split(/\;/, $temp[$infocol]);
for (my $i = 0; $i <= $#info; $i++)
{
if ($info[$i] =~ /$infoflag\=(.*)/)
{
$info = $1;
}
if ($info[$i] =~ /$acflag\=(.*)/)
{
$ac = $1;
}
if ($info[$i] =~ /$allelenumber\=(.*)/)
{
$an = $1;
}
}
if ($an)
{
#print "$allelenumber\n";
$af = sprintf("%0.7g", $ac/$an);
}
else
{
$af = 0;
}
if ($info < 0)
{
$info = 0;
}
#check id is present in HRC (%altaf) and add Alternate AF to the data file
my $id = $temp[$chrcol].':'.$temp[$poscol].'_'.$temp[$refcol].'_'.$temp[$altcol];
print OUT "$temp[$poscol]\t$info\t$temp[$chrcol]\t$af\t$temp[$refcol]\t$temp[$altcol]";
if ($altaf{$id})
{
print OUT "\t$altaf{$id}\n";
$match++;
}
else
{
print OUT "\t\n";