annotate PsiCLASS-1.0.2/samtools-0.1.19/bcftools/vcfutils.pl @ 0:903fc43d6227 draft default tip

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1 #!/usr/bin/perl -w
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 # Author: lh3
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 use strict;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 use warnings;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 use Getopt::Std;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 &main;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 exit;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 sub main {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13 &usage if (@ARGV < 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 my $command = shift(@ARGV);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 my %func = (subsam=>\&subsam, listsam=>\&listsam, fillac=>\&fillac, qstats=>\&qstats, varFilter=>\&varFilter,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 hapmap2vcf=>\&hapmap2vcf, ucscsnp2vcf=>\&ucscsnp2vcf, filter4vcf=>\&varFilter, ldstats=>\&ldstats,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 gapstats=>\&gapstats, splitchr=>\&splitchr, vcf2fq=>\&vcf2fq);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 die("Unknown command \"$command\".\n") if (!defined($func{$command}));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 &{$func{$command}};
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 sub splitchr {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 my %opts = (l=>5000000);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 getopts('l:', \%opts);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 my $l = $opts{l};
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 die(qq/Usage: vcfutils.pl splitchr [-l $opts{l}] <in.fa.fai>\n/) if (@ARGV == 0 && -t STDIN);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 while (<>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 my @t = split;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 my $last = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 for (my $i = 0; $i < $t[1];) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 my $e = ($t[1] - $i) / $l < 1.1? $t[1] : $i + $l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 print "$t[0]:".($i+1)."-$e\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 $i = $e;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 sub subsam {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 die(qq/Usage: vcfutils.pl subsam <in.vcf> [samples]\n/) if (@ARGV == 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 my ($fh, %h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 my $fn = shift(@ARGV);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 my @col;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 open($fh, ($fn =~ /\.gz$/)? "gzip -dc $fn |" : $fn) || die;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 $h{$_} = 1 for (@ARGV);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 while (<$fh>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 if (/^##/) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 print;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 } elsif (/^#/) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 my @t = split;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 my @s = @t[0..8]; # all fixed fields + FORMAT
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 for (9 .. $#t) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 if ($h{$t[$_]}) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 push(@s, $t[$_]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 push(@col, $_);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 pop(@s) if (@s == 9); # no sample selected; remove the FORMAT field
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 print join("\t", @s), "\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 my @t = split;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 if (@col == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 print join("\t", @t[0..7]), "\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 print join("\t", @t[0..8], map {$t[$_]} @col), "\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 close($fh);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 sub listsam {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 die(qq/Usage: vcfutils.pl listsam <in.vcf>\n/) if (@ARGV == 0 && -t STDIN);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 while (<>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 if (/^#/ && !/^##/) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 my @t = split;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 print join("\n", @t[9..$#t]), "\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 exit;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 sub fillac {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 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);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 while (<>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 if (/^#/) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 print;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 my @t = split;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 my @c = (0, 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 my $n = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 my $s = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 @_ = split(":", $t[8]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 for (0 .. $#_) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 if ($_[$_] eq 'GT') { $s = $_; last; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 if ($s < 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 print join("\t", @t), "\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 next;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 for (9 .. $#t) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 if ($t[$_] =~ /^0,0,0/) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 } elsif ($t[$_] =~ /^([^\s:]+:){$s}(\d+).(\d+)/) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 ++$c[$2]; ++$c[$3];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 $n += 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 my $AC = "AC=" . join("\t", @c[1..$#c]) . ";AN=$n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 my $info = $t[7];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 $info =~ s/(;?)AC=(\d+)//;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 $info =~ s/(;?)AN=(\d+)//;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 if ($info eq '.') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 $info = $AC;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 $info .= ";$AC";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 $t[7] = $info;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 print join("\t", @t), "\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 sub ldstats {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 my %opts = (t=>0.9);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 getopts('t:', \%opts);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 die("Usage: vcfutils.pl ldstats [-t $opts{t}] <in.vcf>\n") if (@ARGV == 0 && -t STDIN);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 my $cutoff = $opts{t};
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 my ($last, $lastchr) = (0x7fffffff, '');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 my ($x, $y, $n) = (0, 0, 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 while (<>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 if (/^([^#\s]+)\s(\d+)/) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 my ($chr, $pos) = ($1, $2);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 if (/NEIR=([\d\.]+)/) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 ++$n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 ++$y, $x += $pos - $last if ($lastchr eq $chr && $pos > $last && $1 > $cutoff);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 $last = $pos; $lastchr = $chr;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 print "Number of SNP intervals in strong LD (r > $opts{t}): $y\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 print "Fraction: ", $y/$n, "\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 print "Length: $x\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 sub qstats {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 my %opts = (r=>'', s=>0.02, v=>undef);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 getopts('r:s:v', \%opts);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 die("Usage: vcfutils.pl qstats [-r ref.vcf] <in.vcf>\n
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 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);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 my %ts = (AG=>1, GA=>1, CT=>1, TC=>1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 my %h = ();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 my $is_vcf = defined($opts{v})? 1 : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 if ($opts{r}) { # read the reference positions
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 my $fh;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 open($fh, $opts{r}) || die;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 while (<$fh>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 next if (/^#/);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 if ($is_vcf) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 my @t = split;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 $h{$t[0],$t[1]} = $t[4];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 $h{$1,$2} = 1 if (/^(\S+)\s+(\d+)/);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 close($fh);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 my $hsize = scalar(keys %h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 my @a;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168 while (<>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 next if (/^#/);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 my @t = split;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 next if (length($t[3]) != 1 || uc($t[3]) eq 'N');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 $t[3] = uc($t[3]); $t[4] = uc($t[4]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 my @s = split(',', $t[4]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 $t[5] = 3 if ($t[5] eq '.' || $t[5] < 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 next if (length($s[0]) != 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 my $hit;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 if ($is_vcf) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 $hit = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 my $aa = $h{$t[0],$t[1]};
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 if (defined($aa)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181 my @aaa = split(",", $aa);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 for (@aaa) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183 $hit = 1 if ($_ eq $s[0]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 $hit = defined($h{$t[0],$t[1]})? 1 : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189 push(@a, [$t[5], ($t[4] eq '.' || $t[4] eq $t[3])? 0 : 1, $ts{$t[3].$s[0]}? 1 : 0, $hit]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 push(@a, [-1, 0, 0, 0]); # end marker
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 die("[qstats] No SNP data!\n") if (@a == 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193 @a = sort {$b->[0]<=>$a->[0]} @a;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 my $next = $opts{s};
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 my $last = $a[0];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196 my @c = (0, 0, 0, 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 my @lc;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198 $lc[1] = $lc[2] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199 for my $p (@a) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 if ($p->[0] == -1 || ($p->[0] != $last && $c[0]/@a > $next)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201 my @x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 $x[0] = sprintf("%.4f", $c[1]-$c[2]? $c[2] / ($c[1] - $c[2]) : 100);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203 $x[1] = sprintf("%.4f", $hsize? $c[3] / $hsize : 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204 $x[2] = sprintf("%.4f", $c[3] / $c[1]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 my $a = $c[1] - $lc[1];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206 my $b = $c[2] - $lc[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
207 $x[3] = sprintf("%.4f", $a-$b? $b / ($a-$b) : 100);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
208 print join("\t", $last, @c, @x), "\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
209 $next = $c[0]/@a + $opts{s};
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
210 $lc[1] = $c[1]; $lc[2] = $c[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
211 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
212 ++$c[0]; $c[1] += $p->[1]; $c[2] += $p->[2]; $c[3] += $p->[3];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
213 $last = $p->[0];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
214 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
215 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
216
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
217 sub varFilter {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
218 my %opts = (d=>2, D=>10000000, a=>2, W=>10, Q=>10, w=>3, p=>undef, 1=>1e-4, 2=>1e-100, 3=>0, 4=>1e-4, G=>0, S=>1000, e=>1e-4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
219 getopts('pd:D:W:Q:w:a:1:2:3:4:G:S:e:', \%opts);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
220 die(qq/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
221 Usage: vcfutils.pl varFilter [options] <in.vcf>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
222
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
223 Options: -Q INT minimum RMS mapping quality for SNPs [$opts{Q}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
224 -d INT minimum read depth [$opts{d}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
225 -D INT maximum read depth [$opts{D}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
226 -a INT minimum number of alternate bases [$opts{a}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
227 -w INT SNP within INT bp around a gap to be filtered [$opts{w}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
228 -W INT window size for filtering adjacent gaps [$opts{W}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
229 -1 FLOAT min P-value for strand bias (given PV4) [$opts{1}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
230 -2 FLOAT min P-value for baseQ bias [$opts{2}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
231 -3 FLOAT min P-value for mapQ bias [$opts{3}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
232 -4 FLOAT min P-value for end distance bias [$opts{4}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
233 -e FLOAT min P-value for HWE (plus F<0) [$opts{e}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
234 -p print filtered variants
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
235
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
236 Note: Some of the filters rely on annotations generated by SAMtools\/BCFtools.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
237 \n/) if (@ARGV == 0 && -t STDIN);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
238
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
239 # calculate the window size
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
240 my ($ol, $ow) = ($opts{W}, $opts{w});
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
241 my $max_dist = $ol > $ow? $ol : $ow;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
242 # the core loop
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
243 my @staging; # (indel_filtering_score, flt_tag, indel_span; chr, pos, ...)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
244 while (<>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
245 my @t = split;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
246 if (/^#/) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
247 print; next;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
248 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
249 next if ($t[4] eq '.'); # skip non-var sites
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
250 next if ($t[3] eq 'N'); # skip sites with unknown ref ('N')
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
251 # check if the site is a SNP
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
252 my $type = 1; # SNP
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
253 if (length($t[3]) > 1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
254 $type = 2; # MNP
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
255 my @s = split(',', $t[4]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
256 for (@s) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
257 $type = 3 if (length != length($t[3]));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
258 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
259 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
260 my @s = split(',', $t[4]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
261 for (@s) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
262 $type = 3 if (length > 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
263 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
264 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
265 # clear the out-of-range elements
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
266 while (@staging) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
267 # Still on the same chromosome and the first element's window still affects this position?
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
268 last if ($staging[0][3] eq $t[0] && $staging[0][4] + $staging[0][2] + $max_dist >= $t[1]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
269 varFilter_aux(shift(@staging), $opts{p}); # calling a function is a bit slower, not much
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
270 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
271 my $flt = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
272 # parse annotations
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
273 my ($dp, $mq, $dp_alt) = (-1, -1, -1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
274 if ($t[7] =~ /DP4=(\d+),(\d+),(\d+),(\d+)/i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
275 $dp = $1 + $2 + $3 + $4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
276 $dp_alt = $3 + $4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
277 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
278 if ($t[7] =~ /DP=(\d+)/i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
279 $dp = $1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
280 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
281 $mq = $1 if ($t[7] =~ /MQ=(\d+)/i);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
282 # the depth and mapQ filter
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
283 if ($dp >= 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
284 if ($dp < $opts{d}) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
285 $flt = 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
286 } elsif ($dp > $opts{D}) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
287 $flt = 3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
288 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
289 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
290 $flt = 4 if ($dp_alt >= 0 && $dp_alt < $opts{a});
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
291 $flt = 1 if ($flt == 0 && $mq >= 0 && $mq < $opts{Q});
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
292 $flt = 7 if ($flt == 0 && /PV4=([^,]+),([^,]+),([^,]+),([^,;\t]+)/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
293 && ($1<$opts{1} || $2<$opts{2} || $3<$opts{3} || $4<$opts{4}));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
294 $flt = 8 if ($flt == 0 && ((/MXGQ=(\d+)/ && $1 < $opts{G}) || (/MXSP=(\d+)/ && $1 >= $opts{S})));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
295 # HWE filter
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
296 if ($t[7] =~ /G3=([^;,]+),([^;,]+),([^;,]+).*HWE=([^;,]+)/ && $4 < $opts{e}) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
297 my $p = 2*$1 + $2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
298 my $f = ($p > 0 && $p < 1)? 1 - $2 / ($p * (1-$p)) : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
299 $flt = 9 if ($f < 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
300 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
301
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
302 my $score = $t[5] * 100 + $dp_alt;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
303 my $rlen = length($t[3]) - 1; # $indel_score<0 for SNPs
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
304 if ($flt == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
305 if ($type == 3) { # an indel
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
306 # filtering SNPs and MNPs
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
307 for my $x (@staging) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
308 next if (($x->[0]&3) == 3 || $x->[1] || $x->[4] + $x->[2] + $ow < $t[1]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
309 $x->[1] = 5;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
310 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
311 # check the staging list for indel filtering
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
312 for my $x (@staging) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
313 next if (($x->[0]&3) != 3 || $x->[1] || $x->[4] + $x->[2] + $ol < $t[1]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
314 if ($x->[0]>>2 < $score) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
315 $x->[1] = 6;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
316 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
317 $flt = 6; last;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
318 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
319 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
320 } else { # SNP or MNP
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
321 for my $x (@staging) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
322 next if (($x->[0]&3) != 3 || $x->[4] + $x->[2] + $ow < $t[1]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
323 if ($x->[4] + length($x->[7]) - 1 == $t[1] && substr($x->[7], -1, 1) eq substr($t[4], 0, 1)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
324 && length($x->[7]) - length($x->[6]) == 1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
325 $x->[1] = 5;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
326 } else { $flt = 5; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
327 last;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
328 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
329 # check MNP
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
330 for my $x (@staging) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
331 next if (($x->[0]&3) == 3 || $x->[4] + $x->[2] < $t[1]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
332 if ($x->[0]>>2 < $score) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
333 $x->[1] = 8;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
334 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
335 $flt = 8; last;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
336 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
337 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
338 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
339 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
340 push(@staging, [$score<<2|$type, $flt, $rlen, @t]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
341 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
342 # output the last few elements in the staging list
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
343 while (@staging) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
344 varFilter_aux(shift @staging, $opts{p});
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
345 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
346 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
347
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
348 sub varFilter_aux {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
349 my ($first, $is_print) = @_;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
350 if ($first->[1] == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
351 print join("\t", @$first[3 .. @$first-1]), "\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
352 } elsif ($is_print) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
353 print STDERR join("\t", substr("UQdDaGgPMS", $first->[1], 1), @$first[3 .. @$first-1]), "\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
354 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
355 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
356
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
357 sub gapstats {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
358 my (@c0, @c1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
359 $c0[$_] = $c1[$_] = 0 for (0 .. 10000);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
360 while (<>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
361 next if (/^#/);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
362 my @t = split;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
363 next if (length($t[3]) == 1 && $t[4] =~ /^[A-Za-z](,[A-Za-z])*$/); # not an indel
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
364 my @s = split(',', $t[4]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
365 for my $x (@s) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
366 my $l = length($x) - length($t[3]) + 5000;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
367 if ($x =~ /^-/) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
368 $l = -(length($x) - 1) + 5000;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
369 } elsif ($x =~ /^\+/) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
370 $l = length($x) - 1 + 5000;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
371 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
372 $c0[$l] += 1 / @s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
373 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
374 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
375 for (my $i = 0; $i < 10000; ++$i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
376 next if ($c0[$i] == 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
377 $c1[0] += $c0[$i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
378 $c1[1] += $c0[$i] if (($i-5000)%3 == 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
379 printf("C\t%d\t%.2f\n", ($i-5000), $c0[$i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
380 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
381 printf("3\t%d\t%d\t%.3f\n", $c1[0], $c1[1], $c1[1]/$c1[0]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
382 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
383
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
384 sub ucscsnp2vcf {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
385 die("Usage: vcfutils.pl <in.ucsc.snp>\n") if (@ARGV == 0 && -t STDIN);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
386 print "##fileformat=VCFv4.0\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
387 print join("\t", "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO"), "\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
388 while (<>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
389 my @t = split("\t");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
390 my $indel = ($t[9] =~ /^[ACGT](\/[ACGT])+$/)? 0 : 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
391 my $pos = $t[2] + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
392 my @alt;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
393 push(@alt, $t[7]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
394 if ($t[6] eq '-') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
395 $t[9] = reverse($t[9]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
396 $t[9] =~ tr/ACGTRYMKWSNacgtrymkwsn/TGCAYRKMWSNtgcayrkmwsn/;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
397 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
398 my @a = split("/", $t[9]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
399 for (@a) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
400 push(@alt, $_) if ($_ ne $alt[0]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
401 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
402 if ($indel) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
403 --$pos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
404 for (0 .. $#alt) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
405 $alt[$_] =~ tr/-//d;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
406 $alt[$_] = "N$alt[$_]";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
407 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
408 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
409 my $ref = shift(@alt);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
410 my $af = $t[13] > 0? ";AF=$t[13]" : '';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
411 my $valid = ($t[12] eq 'unknown')? '' : ";valid=$t[12]";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
412 my $info = "molType=$t[10];class=$t[11]$valid$af";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
413 print join("\t", $t[1], $pos, $t[4], $ref, join(",", @alt), 0, '.', $info), "\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
414 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
415 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
416
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
417 sub hapmap2vcf {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
418 die("Usage: vcfutils.pl <in.ucsc.snp> <in.hapmap>\n") if (@ARGV == 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
419 my $fn = shift(@ARGV);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
420 # parse UCSC SNP
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
421 warn("Parsing UCSC SNPs...\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
422 my ($fh, %map);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
423 open($fh, ($fn =~ /\.gz$/)? "gzip -dc $fn |" : $fn) || die;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
424 while (<$fh>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
425 my @t = split;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
426 next if ($t[3] - $t[2] != 1); # not SNP
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
427 @{$map{$t[4]}} = @t[1,3,7];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
428 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
429 close($fh);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
430 # write VCF
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
431 warn("Writing VCF...\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
432 print "##fileformat=VCFv4.0\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
433 while (<>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
434 my @t = split;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
435 if ($t[0] eq 'rs#') { # the first line
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
436 print join("\t", "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT", @t[11..$#t]), "\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
437 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
438 next unless ($map{$t[0]});
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
439 next if (length($t[1]) != 3); # skip non-SNPs
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
440 my $a = \@{$map{$t[0]}};
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
441 my $ref = $a->[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
442 my @u = split('/', $t[1]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
443 if ($u[1] eq $ref) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
444 $u[1] = $u[0]; $u[0] = $ref;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
445 } elsif ($u[0] ne $ref) { next; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
446 my $alt = $u[1];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
447 my %w;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
448 $w{$u[0]} = 0; $w{$u[1]} = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
449 my @s = (@$a[0,1], $t[0], $ref, $alt, 0, '.', '.', 'GT');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
450 my $is_tri = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
451 for (@t[11..$#t]) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
452 if ($_ eq 'NN') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
453 push(@s, './.');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
454 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
455 my @a = ($w{substr($_,0,1)}, $w{substr($_,1,1)});
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
456 if (!defined($a[0]) || !defined($a[1])) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
457 $is_tri = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
458 last;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
459 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
460 push(@s, "$a[0]/$a[1]");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
461 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
462 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
463 next if ($is_tri);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
464 print join("\t", @s), "\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
465 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
466 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
467 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
468
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
469 sub vcf2fq {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
470 my %opts = (d=>3, D=>100000, Q=>10, l=>5);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
471 getopts('d:D:Q:l:', \%opts);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
472 die(qq/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
473 Usage: vcfutils.pl vcf2fq [options] <all-site.vcf>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
474
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
475 Options: -d INT minimum depth [$opts{d}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
476 -D INT maximum depth [$opts{D}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
477 -Q INT min RMS mapQ [$opts{Q}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
478 -l INT INDEL filtering window [$opts{l}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
479 \n/) if (@ARGV == 0 && -t STDIN);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
480
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
481 my ($last_chr, $seq, $qual, $last_pos, @gaps);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
482 my $_Q = $opts{Q};
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
483 my $_d = $opts{d};
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
484 my $_D = $opts{D};
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
485
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
486 my %het = (AC=>'M', AG=>'R', AT=>'W', CA=>'M', CG=>'S', CT=>'Y',
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
487 GA=>'R', GC=>'S', GT=>'K', TA=>'W', TC=>'Y', TG=>'K');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
488
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
489 $last_chr = '';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
490 while (<>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
491 next if (/^#/);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
492 my @t = split;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
493 if ($last_chr ne $t[0]) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
494 &v2q_post_process($last_chr, \$seq, \$qual, \@gaps, $opts{l}) if ($last_chr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
495 ($last_chr, $last_pos) = ($t[0], 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
496 $seq = $qual = '';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
497 @gaps = ();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
498 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
499 die("[vcf2fq] unsorted input\n") if ($t[1] - $last_pos < 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
500 if ($t[1] - $last_pos > 1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
501 $seq .= 'n' x ($t[1] - $last_pos - 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
502 $qual .= '!' x ($t[1] - $last_pos - 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
503 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
504 if (length($t[3]) == 1 && $t[7] !~ /INDEL/ && $t[4] =~ /^([A-Za-z.])(,[A-Za-z])*$/) { # a SNP or reference
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
505 my ($ref, $alt) = ($t[3], $1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
506 my ($b, $q);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
507 $q = $1 if ($t[7] =~ /FQ=(-?[\d\.]+)/);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
508 if ($q < 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
509 $_ = ($t[7] =~ /AF1=([\d\.]+)/)? $1 : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
510 $b = ($_ < .5 || $alt eq '.')? $ref : $alt;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
511 $q = -$q;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
512 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
513 $b = $het{"$ref$alt"};
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
514 $b ||= 'N';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
515 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
516 $b = lc($b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
517 $b = uc($b) if (($t[7] =~ /MQ=(\d+)/ && $1 >= $_Q) && ($t[7] =~ /DP=(\d+)/ && $1 >= $_d && $1 <= $_D));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
518 $q = int($q + 33 + .499);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
519 $q = chr($q <= 126? $q : 126);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
520 $seq .= $b;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
521 $qual .= $q;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
522 } elsif ($t[4] ne '.') { # an INDEL
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
523 push(@gaps, [$t[1], length($t[3])]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
524 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
525 $last_pos = $t[1];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
526 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
527 &v2q_post_process($last_chr, \$seq, \$qual, \@gaps, $opts{l});
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
528 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
529
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
530 sub v2q_post_process {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
531 my ($chr, $seq, $qual, $gaps, $l) = @_;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
532 for my $g (@$gaps) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
533 my $beg = $g->[0] > $l? $g->[0] - $l : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
534 my $end = $g->[0] + $g->[1] + $l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
535 $end = length($$seq) if ($end > length($$seq));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
536 substr($$seq, $beg, $end - $beg) = lc(substr($$seq, $beg, $end - $beg));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
537 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
538 print "\@$chr\n"; &v2q_print_str($seq);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
539 print "+\n"; &v2q_print_str($qual);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
540 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
541
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
542 sub v2q_print_str {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
543 my ($s) = @_;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
544 my $l = length($$s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
545 for (my $i = 0; $i < $l; $i += 60) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
546 print substr($$s, $i, 60), "\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
547 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
548 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
549
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
550 sub usage {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
551 die(qq/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
552 Usage: vcfutils.pl <command> [<arguments>]\n
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
553 Command: subsam get a subset of samples
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
554 listsam list the samples
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
555 fillac fill the allele count field
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
556 qstats SNP stats stratified by QUAL
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
557
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
558 hapmap2vcf convert the hapmap format to VCF
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
559 ucscsnp2vcf convert UCSC SNP SQL dump to VCF
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
560
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
561 varFilter filtering short variants (*)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
562 vcf2fq VCF->fastq (**)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
563
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
564 Notes: Commands with description endting with (*) may need bcftools
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
565 specific annotations.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
566 \n/);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
567 }