blib/script/vcf-to-table.pl untested

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 145 SCAR 40.3
CC
7
Cov
0%
genotype
42.0
get_agg_columns
6.0
BEGIN
2.0
CRAP 56.0
1 #!/usr/bin/env perl
2 #===============================================================================
3 #
4 # FILE: vcf-to-table.pl
5 #
6 # USAGE: ./vcf-to-table.pl
7 #
8 # DESCRIPTION: Flatten each line so it is in tabular format instead of INFO,FORMAT,etc
9 #
10 # VERSION: 1.0
11 # CREATED: 08/11/2014 09:55:09 AM
12 # REVISION: ---
13 #===============================================================================
14  
15 use strict;
16 use warnings;
17 no warnings 'uninitialized';
18  
19 use Vcf;
20 use Data::Dumper;
21 use IO::File;
22  
23 #Initialize VCF
24 my $vcffile = $ARGV[0];
25  
26 my $vcf = Vcf->new(file=>$vcffile);
27 $vcf->parse_header();
28 my $href = get_agg_columns($vcf);
29  
30 my(@columns, @infos, @format, @samples);
31 @columns = @{$href->{columns}};
32 @infos = @{$href->{infos}};
33 @format = @{$href->{format}};
34 @samples = @{$href->{samples}};
35  
36 #Initialize Text::Csv
37 my $fh;
38 if($ARGV[1]){
39 $fh = IO::File->new(">".$ARGV[1]);
40 }
41 else{
42 $fh = IO::File->new(">".$vcffile.".table.tsv");
43 }
44 print $fh join("\t", @columns)."\n";
45  
46 my $z=0;
47 while(my $line = $vcf->next_line()){
48 # die if $z>=500;
49  
50 # Some of the info fields, mainly the refgene ones, don't parse correctly with the next_data_hash method...
51 # my $x = $vcf->next_data_hash($line);
52  
53 my %hashline = ();
54 my @items = split(/\t/,$line);
55  
56 for(my $y=0; $y<=$#items; $y++){
57 $hashline{$columns[$y]} = $items[$y];
58 }
59  
60  
61 foreach my $i (@infos){
62 next unless $i;
63 my $af = $vcf->get_info_field($items[7], $i);
64 # print "I is $i ".Dumper($af);
65 $hashline{$i} = $af;
66 }
67  
68 foreach my $f (@format){
69 next unless $f;
70 my($idx, $pl);
71  
72 my $y=9;
73 foreach my $s (@samples){
74 $idx = $vcf->get_tag_index($items[8], $f, ':');
75 if($idx != -1 ){
76 $pl = $vcf->get_field($items[$y],$idx);
77 if($f eq "GT"){
78 my $call = genotype($vcf, $pl);
79 # print "Call for Sample $s is $call\n";
80 $hashline{$s.".GenoCall"} = $call;
81 }
82 $hashline{$s.".$f"} = $pl;
83 }
84 # print "Sample column $s $f is $pl\n" if $pl;
85 $y++;
86 }
87 }
88  
89 my @row = map { $hashline{$_} } @columns;
90 s/\n//g for @row;
91  
92 print $fh join("\t", @row)."\n";
93  
94 $z++;
95 }
96  
97 sub get_agg_columns {
98 my($vcf) = @_;
99  
100 my (@samples) = $vcf->get_samples();
101  
102 my $origcols = $vcf->{columns};
103  
104 die unless $origcols;
105  
106 my @infos = keys %{$vcf->{header}->{INFO}};
107 @infos = sort(@infos);
108  
109 my @format = keys %{$vcf->{header}->{FORMAT}};
110 @format = sort(@format);
111  
112 my @columns = ();
113  
114 push(@columns, @{$origcols});
115 push(@columns, @infos);
116  
117 foreach my $s (@samples){
118 foreach my $f (@format){
119 push (@columns, $s.".$f");
120 }
121 push(@columns, $s.".GenoCall");
122 }
123  
124 my $href = {};
125 $href->{columns} = \@columns;
126 $href->{samples} = \@samples;
127 $href->{format} = \@format;
128 $href->{infos} = \@infos;
129  
130 return($href);
131 }
132  
133 sub genotype {
134 my($vcf, $geno) = @_;
135  
136 my @geno = $vcf->split_gt($geno);
137  
138 if(scalar @geno != 2){
139 return "DIP".scalar @geno;
140 }
141 elsif($geno[0] == 0 && $geno[1] == 0){
142 return "REF";
143 }
144 elsif($geno[0] == 1 && $geno[1] == 0){
145 return "HET";
146 }
147 elsif($geno[0] == 0 && $geno[1] == 1){
148 return "HET";
149 }
150 elsif($geno[0] == 1 && $geno[1] == 1){
151 return "ALT";
152 }
153 else{
154 return "U";
155 }
156 }