13 my $version = '0.1.0';
14 &usage if (@ARGV < 1);
15 my $command = shift(@ARGV);
16 my %func = (subsam=>\&subsam, listsam=>\&listsam, fillac=>\&fillac, qstats=>\&qstats);
17 die("Unknown command \"$command\".\n") if (!defined($func{$command}));
22 die(qq/Usage: vcfutils.pl subsam <in.vcf> [samples]\n/) if (@ARGV == 0);
24 my $fn = shift(@ARGV);
26 open($fh, ($fn =~ /\.gz$/)? "gzip -dc $fn |" : $fn) || die;
27 $h{$_} = 1 for (@ARGV);
33 my @s = @t[0..8]; # all fixed fields + FORMAT
40 pop(@s) if (@s == 9); # no sample selected; remove the FORMAT field
41 print join("\t", @s), "\n";
45 print join("\t", @t[0..7]), "\n";
47 print join("\t", @t[0..8], map {$t[$_]} @col), "\n";
55 die(qq/Usage: vcfutils.pl listsam <in.vcf>\n/) if (@ARGV == 0 && -t STDIN);
59 print join("\n", @t[9..$#t]), "\n";
66 die(qq/Usage: vcfutils.pl fillac <in.vcf>\n\nNote: The GT field MUST BE present and always appear as the first field.\n/) if (@ARGV == 0 && -t STDIN);
76 if ($t[$_] =~ /^(\d+).(\d+)/) {
81 my $AC = "AC=" . join("\t", @c[1..$#c]) . ";AN=$n";
83 $info =~ s/(;?)AC=(\d+)//;
84 $info =~ s/(;?)AN=(\d+)//;
91 print join("\t", @t), "\n";
97 my %opts = (r=>'', s=>0.01);
98 getopts('r:s:', \%opts);
99 die("Usage: vcfutils.pl qstats [-r ref.vcf] <in.vcf>\n
100 Note: This command discards indels. Output: QUAL #non-indel #SNPs #transitions #joint ts/tv #joint/#ref #joint/#non-indel \n") if (@ARGV == 0 && -t STDIN);
101 my %ts = (AG=>1, GA=>1, CT=>1, TC=>1);
103 if ($opts{r}) { # read the reference positions
105 open($fh, $opts{r}) || die;
108 $h{$1,$2} = 1 if (/^(\S+)\s+(\d+)/);
112 my $hsize = scalar(keys %h);
117 next if (length($t[3]) != 1 || uc($t[3]) eq 'N');
118 my @s = split(',', $t[4]);
119 $t[5] = 3 if ($t[5] < 0);
120 $t[3] = uc($t[3]); $t[4] = uc($t[4]);
121 next if (length($s[0]) != 1);
122 push(@a, [$t[5], ($t[4] eq '.' || $t[4] eq $t[3])? 0 : 1, $ts{$t[3].$s[0]}? 1 : 0, $h{$t[0],$t[1]}? 1 : 0]);
124 push(@a, [-1, 0, 0, 0]); # end marker
125 die("[qstats] No SNP data!\n") if (@a == 0);
126 @a = sort {$b->[0]<=>$a->[0]} @a;
129 my @c = (0, 0, 0, 0);
131 if ($p->[0] == -1 || ($p->[0] != $last && $c[0]/@a > $next)) {
133 $x[0] = sprintf("%.3f", $c[1]-$c[2]? $c[2] / ($c[1] - $c[2]) : 100);
134 $x[1] = sprintf("%.3f", $hsize? $c[3] / $hsize : 0);
135 $x[2] = sprintf("%.3f", $c[3] / $c[1]);
136 print join("\t", $last, @c, @x), "\n";
137 $next = $c[0]/@a + $opts{s};
139 ++$c[0]; $c[1] += $p->[1]; $c[2] += $p->[2]; $c[3] += $p->[3];
146 Usage: vcfutils.pl <command> [<arguments>]\n
147 Command: subsam get a subset of samples
148 listsam list the samples
149 fillac fill the allele count field
150 qstats SNP stats stratified by QUAL