Merge branch 'upstream'
[samtools.git] / misc / samtools.pl
1 #!/usr/bin/perl -w
2
3 # Author: lh3
4
5 use strict;
6 use warnings;
7 use Getopt::Std;
8
9 my $version = '0.3.3';
10 &usage if (@ARGV < 1);
11
12 my $command = shift(@ARGV);
13 my %func = (showALEN=>\&showALEN, pileup2fq=>\&pileup2fq, varFilter=>\&varFilter,
14                         unique=>\&unique, uniqcmp=>\&uniqcmp);
15
16 die("Unknown command \"$command\".\n") if (!defined($func{$command}));
17 &{$func{$command}};
18 exit(0);
19
20 #
21 # showALEN
22 #
23
24 sub showALEN {
25   die(qq/Usage: samtools.pl showALEN <in.sam>\n/) if (@ARGV == 0 && -t STDIN);
26   while (<>) {
27         my @t = split;
28         next if (/^\@/ || @t < 11);
29         my $l = 0;
30         $_ = $t[5];
31         s/(\d+)[MI]/$l+=$1/eg;
32         print join("\t", @t[0..5]), "\t$l\t", join("\t", @t[6..$#t]), "\n";
33   }
34 }
35
36 #
37 # varFilter
38 #
39
40 sub varFilter {
41   my %opts = (d=>3, D=>100, l=>30, Q=>25, q=>10, G=>25, s=>100, w=>10, W=>10, N=>2, p=>undef);
42   getopts('pq:d:D:l:Q:w:W:N:G:', \%opts);
43   die(qq/
44 Usage:   samtools.pl varFilter [options] <in.cns-pileup>
45
46 Options: -Q INT    minimum RMS mapping quality for SNPs [$opts{Q}]
47          -q INT    minimum RMS mapping quality for gaps [$opts{q}]
48          -d INT    minimum read depth [$opts{d}]
49          -D INT    maximum read depth [$opts{D}]
50
51          -G INT    min indel score for nearby SNP filtering [$opts{G}]
52          -w INT    SNP within INT bp around a gap to be filtered [$opts{w}]
53
54          -W INT    window size for filtering dense SNPs [$opts{W}]
55          -N INT    max number of SNPs in a window [$opts{N}]
56
57          -l INT    window size for filtering adjacent gaps [$opts{l}]
58
59          -p        print filtered variants
60 \n/) if (@ARGV == 0 && -t STDIN);
61
62   # calculate the window size
63   my ($ol, $ow, $oW) = ($opts{l}, $opts{w}, $opts{W});
64   my $max_dist = $ol > $ow? $ol : $ow;
65   $max_dist = $oW if ($max_dist < $oW);
66   # the core loop
67   my @staging; # (indel_filtering_score, flt_tag)
68   while (<>) {
69         my @t = split;
70         next if (uc($t[2]) eq uc($t[3]) || $t[3] eq '*/*'); # skip non-var sites
71         # clear the out-of-range elements
72         while (@staging) {
73           last if ($staging[0][2] eq $t[0] && $staging[0][3] + $max_dist >= $t[1]);
74           varFilter_aux(shift(@staging), $opts{p}); # calling a function is a bit slower, not much
75         }
76         my ($flt, $score) = (0, -1);
77         # first a simple filter
78         if ($t[7] < $opts{d}) {
79           $flt = 2;
80         } elsif ($t[7] > $opts{D}) {
81           $flt = 3;
82         }
83         # site dependent filters
84         if ($flt == 0) {
85           if ($t[2] eq '*') { # an indel
86                 $flt = 1 if ($t[6] < $opts{q});
87                 # filtering SNPs
88                 if ($t[5] >= $opts{G}) {
89                   for my $x (@staging) {
90                         next if ($x->[0] >= 0 || $x->[3] + $ow < $t[1]);
91                         $x->[1] = 5 if ($x->[1] == 0);
92                   }
93                 }
94                 # calculate the filtering score (different from indel quality)
95                 $score = $t[5];
96                 $score += $opts{s} * $t[10] if ($t[8] ne '*');
97                 $score += $opts{s} * $t[11] if ($t[9] ne '*');
98                 # check the staging list for indel filtering
99                 for my $x (@staging) {
100                   next if ($x->[0] < 0 || $x->[3] + $ol < $t[1]);
101                   if ($x->[0] < $score) {
102                         $x->[1] = 6;
103                   } else {
104                         $flt = 6; last;
105                   }
106                 }
107           } else { # a SNP
108                 $flt = 1 if ($t[6] < $opts{Q});
109                 # check adjacent SNPs
110                 my $k = 1;
111                 for my $x (@staging) {
112                   ++$k if ($x->[0] < 0 && $x->[3] + $oW >= $t[1] && ($x->[1] == 0 || $x->[1] == 4 || $x->[1] == 5));
113                 }
114                 # filtering is necessary
115                 if ($k > $opts{N}) {
116                   $flt = 4;
117                   for my $x (@staging) {
118                          $x->[1] = 4 if ($x->[0] < 0 && $x->[3] + $oW >= $t[1] && $x->[1] == 0);
119                   }
120                 } else { # then check gap filter
121                   for my $x (@staging) {
122                         next if ($x->[0] < 0 || $x->[3] + $ow < $t[1]);
123                         if ($x->[0] >= $opts{G}) {
124                           $flt = 5; last;
125                         }
126                   }
127                 }
128           }
129         }
130         push(@staging, [$score, $flt, @t]);
131   }
132   # output the last few elements in the staging list
133   while (@staging) {
134         varFilter_aux(shift @staging, $opts{p});
135   }
136 }
137
138 sub varFilter_aux {
139   my ($first, $is_print) = @_;
140   if ($first->[1] == 0) {
141         print join("\t", @$first[2 .. @$first-1]), "\n";
142   } elsif ($is_print) {
143         print STDERR join("\t", substr("UQdDWGgX", $first->[1], 1), @$first[2 .. @$first-1]), "\n";
144   }
145 }
146
147 #
148 # pileup2fq
149 #
150
151 sub pileup2fq {
152   my %opts = (d=>3, D=>255, Q=>25, G=>25, l=>10);
153   getopts('d:D:Q:G:l:', \%opts);
154   die(qq/
155 Usage:   samtools.pl pileup2fq [options] <in.cns-pileup>
156
157 Options: -d INT    minimum depth        [$opts{d}]
158          -D INT    maximum depth        [$opts{D}]
159          -Q INT    min RMS mapQ         [$opts{Q}]
160          -G INT    minimum indel score  [$opts{G}]
161          -l INT    indel filter winsize [$opts{l}]\n
162 /) if (@ARGV == 0 && -t STDIN);
163
164   my ($last_chr, $seq, $qual, @gaps, $last_pos);
165   my $_Q = $opts{Q};
166   my $_d = $opts{d};
167   my $_D = $opts{D};
168
169   $last_chr = '';
170   while (<>) {
171         my @t = split;
172         if ($last_chr ne $t[0]) {
173           &p2q_post_process($last_chr, \$seq, \$qual, \@gaps, $opts{l}) if ($last_chr);
174           $last_chr = $t[0];
175           $last_pos = 0;
176           $seq = ''; $qual = '';
177           @gaps = ();
178         }
179         if ($t[1] - $last_pos != 1) {
180           $seq .= 'n' x ($t[1] - $last_pos - 1);
181           $qual .= '!' x ($t[1] - $last_pos - 1);
182         }
183         if ($t[2] eq '*') {
184           push(@gaps, $t[1]) if ($t[5] >= $opts{G});
185         } else {
186           $seq .= ($t[6] >= $_Q && $t[7] >= $_d && $t[7] <= $_D)? uc($t[3]) : lc($t[3]);
187           my $q = $t[4] + 33;
188           $q = 126 if ($q > 126);
189           $qual .= chr($q);
190         }
191         $last_pos = $t[1];
192   }
193   &p2q_post_process($last_chr, \$seq, \$qual, \@gaps, $opts{l});
194 }
195
196 sub p2q_post_process {
197   my ($chr, $seq, $qual, $gaps, $l) = @_;
198   &p2q_filter_gaps($seq, $gaps, $l);
199   print "\@$chr\n"; &p2q_print_str($seq);
200   print "+\n"; &p2q_print_str($qual);
201 }
202
203 sub p2q_filter_gaps {
204   my ($seq, $gaps, $l) = @_;
205   for my $g (@$gaps) {
206         my $x = $g > $l? $g - $l : 0;
207         substr($$seq, $x, $l + $l) = lc(substr($$seq, $x, $l + $l));
208   }
209 }
210
211 sub p2q_print_str {
212   my ($s) = @_;
213   my $l = length($$s);
214   for (my $i = 0; $i < $l; $i += 60) {
215         print substr($$s, $i, 60), "\n";
216   }
217 }
218
219 #
220 # unique
221 #
222
223 sub unique {
224   my %opts = (f=>250.0, q=>5, r=>2, a=>1, b=>3);
225   getopts('Qf:q:r:a:b:', \%opts);
226   die("Usage: samtools.pl unique [-f $opts{f}] <in.sam>\n") if (@ARGV == 0 && -t STDIN);
227   my $last = '';
228   my $recal_Q = !defined($opts{Q});
229   my @a;
230   while (<>) {
231         my $score = -1;
232         print $_ if (/^\@/);
233         $score = $1 if (/AS:i:(\d+)/);
234         my @t = split("\t");
235         next if (@t < 11);
236         if ($score < 0) { # AS tag is unavailable
237           my $cigar = $t[5];
238           my ($mm, $go, $ge) = (0, 0, 0);
239           $cigar =~ s/(\d+)[ID]/++$go,$ge+=$1/eg;
240           $cigar = $t[5];
241           $cigar =~ s/(\d+)M/$mm+=$1/eg;
242           $score = $mm * $opts{a} - $go * $opts{q} - $ge * $opts{r}; # no mismatches...
243         }
244         $score = 1 if ($score < 1);
245         if ($t[0] ne $last) {
246           &unique_aux(\@a, $opts{f}, $recal_Q) if (@a);
247           $last = $t[0];
248         }
249         push(@a, [$score, \@t]);
250   }
251   &unique_aux(\@a, $opts{f}, $recal_Q) if (@a);
252 }
253
254 sub unique_aux {
255   my ($a, $fac, $is_recal) = @_;
256   my ($max, $max2, $max_i) = (0, 0, -1);
257   for (my $i = 0; $i < @$a; ++$i) {
258         if ($a->[$i][0] > $max) {
259           $max2 = $max; $max = $a->[$i][0]; $max_i = $i;
260         } elsif ($a->[$i][0] > $max2) {
261           $max2 = $a->[$i][0];
262         }
263   }
264   if ($is_recal) {
265         my $q = int($fac * ($max - $max2) / $max + .499);
266         $q = 250 if ($q > 250);
267         $a->[$max_i][1][4] = $q < 250? $q : 250;
268   }
269   print join("\t", @{$a->[$max_i][1]});
270   @$a = ();
271 }
272
273 #
274 # uniqcmp: compare two SAM files
275 #
276
277 sub uniqcmp {
278   my %opts = (q=>10, s=>100);
279   getopts('pq:s:', \%opts);
280   die("Usage: samtools.pl uniqcmp <in1.sam> <in2.sam>\n") if (@ARGV < 2);
281   my ($fh, %a);
282   warn("[uniqcmp] read the first file...\n");
283   &uniqcmp_aux($ARGV[0], \%a, 0);
284   warn("[uniqcmp] read the second file...\n");
285   &uniqcmp_aux($ARGV[1], \%a, 1);
286   warn("[uniqcmp] stats...\n");
287   my @cnt;
288   $cnt[$_] = 0 for (0..9);
289   for my $x (keys %a) {
290         my $p = $a{$x};
291         my $z;
292         if (defined($p->[0]) && defined($p->[1])) {
293           $z = ($p->[0][0] == $p->[1][0] && $p->[0][1] eq $p->[1][1] && abs($p->[0][2] - $p->[1][2]) < $opts{s})? 0 : 1;
294           if ($p->[0][3] >= $opts{q} && $p->[1][3] >= $opts{q}) {
295                 ++$cnt[$z*3+0];
296           } elsif ($p->[0][3] >= $opts{q}) {
297                 ++$cnt[$z*3+1];
298           } elsif ($p->[1][3] >= $opts{q}) {
299                 ++$cnt[$z*3+2];
300           }
301           print STDERR "$x\t$p->[0][1]:$p->[0][2]\t$p->[0][3]\t$p->[0][4]\t$p->[1][1]:$p->[1][2]\t$p->[1][3]\t$p->[1][4]\t",
302                 $p->[0][5]-$p->[1][5], "\n" if ($z && defined($opts{p}) && ($p->[0][3] >= $opts{q} || $p->[1][3] >= $opts{q}));
303         } elsif (defined($p->[0])) {
304           ++$cnt[$p->[0][3]>=$opts{q}? 6 : 7];
305           print STDERR "$x\t$p->[0][1]:$p->[0][2]\t$p->[0][3]\t$p->[0][4]\t*\t0\t*\t",
306                 $p->[0][5], "\n" if (defined($opts{p}) && $p->[0][3] >= $opts{q});
307         } else {
308           print STDERR "$x\t*\t0\t*\t$p->[1][1]:$p->[1][2]\t$p->[1][3]\t$p->[1][4]\t",
309                 -$p->[1][5], "\n" if (defined($opts{p}) && $p->[1][3] >= $opts{q});
310           ++$cnt[$p->[1][3]>=$opts{q}? 8 : 9];
311         }
312   }
313   print "Consistent (high, high):   $cnt[0]\n";
314   print "Consistent (high, low ):   $cnt[1]\n";
315   print "Consistent (low , high):   $cnt[2]\n";
316   print "Inconsistent (high, high): $cnt[3]\n";
317   print "Inconsistent (high, low ): $cnt[4]\n";
318   print "Inconsistent (low , high): $cnt[5]\n";
319   print "Second missing (high):     $cnt[6]\n";
320   print "Second missing (low ):     $cnt[7]\n";
321   print "First  missing (high):     $cnt[8]\n";
322   print "First  missing (low ):     $cnt[9]\n";
323 }
324
325 sub uniqcmp_aux {
326   my ($fn, $a, $which) = @_;
327   my $fh;
328   $fn = "samtools view $fn |" if ($fn =~ /\.bam/);
329   open($fh, $fn) || die;
330   while (<$fh>) {
331         my @t = split;
332         next if (@t < 11);
333 #       my $l = ($t[5] =~ /^(\d+)S/)? $1 : 0;
334         my $l = 0;
335         my ($x, $nm) = (0, 0);
336         $nm = $1 if (/NM:i:(\d+)/);
337         $_ = $t[5];
338         s/(\d+)[MI]/$x+=$1/eg;
339         @{$a->{$t[0]}[$which]} = (($t[1]&0x10)? 1 : 0, $t[2], $t[3]-$l, $t[4], "$x:$nm", $x - 4 * $nm);
340   }
341   close($fh);
342 }
343
344 #
345 # Usage
346 #
347
348 sub usage {
349   die(qq/
350 Program: samtools.pl (helper script for SAMtools)
351 Version: $version
352 Contact: Heng Li <lh3\@sanger.ac.uk>\n
353 Usage:   samtools.pl <command> [<arguments>]\n
354 Command: varFilter     filtering SNPs and short indels
355          pileup2fq     generate fastq from `pileup -c'
356          showALEN      print alignment length (ALEN) following CIGAR
357 \n/);
358 }