]> git.donarmstrong.com Git - samtools.git/blob - misc/samtools.pl
fixed a typo
[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.1';
10 &usage if (@ARGV < 1);
11
12 my $command = shift(@ARGV);
13 my %func = (showALEN=>\&showALEN, pileup2fq=>\&pileup2fq, varFilter=>\&varFilter);
14
15 die("Unknown command \"$command\".\n") if (!defined($func{$command}));
16 &{$func{$command}};
17 exit(0);
18
19 #
20 # showALEN
21 #
22
23 sub showALEN {
24   die(qq/Usage: samtools.pl showALEN <in.sam>\n/) if (@ARGV == 0 && -t STDIN);
25   while (<>) {
26         my @t = split;
27         my $l = 0;
28         $_ = $t[5];
29         s/(\d+)[SMI]/$l+=$1/eg;
30         print join("\t", @t[0..5]), "\t$l\t", join("\t", @t[6..$#t]), "\n";
31   }
32 }
33
34 #
35 # varFilter
36 #
37
38 sub varFilter {
39   my %opts = (d=>3, D=>100, l=>30, Q=>25, G=>25, s=>100, w=>10, W=>10, N=>2, p=>undef);
40   getopts('pd:D:l:Q:w:W:N:G:', \%opts);
41   die(qq/
42 Usage:   samtools.pl varFilter [options] <in.cns-pileup>
43
44 Options: -Q INT    minimum RMS mapping quality [$opts{Q}]
45          -d INT    minimum read depth [$opts{d}]
46          -D INT    maximum read depth [$opts{D}]
47
48          -G INT    min indel score for nearby SNP filtering [$opts{G}]
49          -w INT    SNP within INT bp around a gap to be filtered [$opts{w}]
50
51          -W INT    window size for filtering dense SNPs [$opts{W}]
52          -N INT    max number of SNPs in a window [$opts{N}]
53
54          -l INT    window size for filtering adjacent gaps [$opts{l}]
55
56          -p        print filtered variants
57 \n/) if (@ARGV == 0 && -t STDIN);
58
59   # calculate the window size
60   my ($ol, $ow, $oW) = ($opts{l}, $opts{w}, $opts{W});
61   my $max_dist = $ol > $ow? $ol : $ow;
62   $max_dist = $oW if ($max_dist < $oW);
63   # the core loop
64   my @staging; # (indel_filtering_score, flt_tag)
65   while (<>) {
66         my @t = split;
67         next if ($t[2] eq $t[3] || $t[3] eq '*/*'); # skip non-var sites
68         # clear the out-of-range elements
69         while (@staging) {
70           last if ($staging[0][2] eq $t[0] && $staging[0][3] + $max_dist >= $t[1]);
71           varFilter_aux(shift(@staging), $opts{p}); # calling a function is a bit slower, not much
72         }
73         my ($flt, $score) = (0, -1);
74         # first a simple filter
75         if ($t[6] < $opts{Q}) {
76           $flt = 1;
77         } elsif ($t[7] < $opts{d}) {
78           $flt = 2;
79         } elsif ($t[7] > $opts{D}) {
80           $flt = 3;
81         }
82         # site dependent filters
83         if ($flt == 0) {
84           if ($t[2] eq '*') { # an indel
85                 # filtering SNPs
86                 if ($t[5] >= $opts{G}) {
87                   for my $x (@staging) {
88                         next if ($x->[0] >= 0 || $x->[3] + $ow < $t[1]);
89                         $x->[1] = 5 if ($x->[1] == 0);
90                   }
91                 }
92                 # calculate the filtering score (different from indel quality)
93                 $score = $t[5];
94                 $score += $opts{s} * $t[10] if ($t[8] ne '*');
95                 $score += $opts{s} * $t[11] if ($t[9] ne '*');
96                 # check the staging list for indel filtering
97                 for my $x (@staging) {
98                   next if ($x->[0] < 0 || $x->[3] + $ol < $t[1]);
99                   if ($x->[0] < $score) {
100                         $x->[1] = 6;
101                   } else {
102                         $flt = 6; last;
103                   }
104                 }
105           } else { # a SNP
106                 # check adjacent SNPs
107                 my $k = 1;
108                 for my $x (@staging) {
109                   ++$k if ($x->[0] < 0 && $x->[3] + $oW >= $t[1] && ($x->[1] == 0 || $x->[1] == 4 || $x->[1] == 5));
110                 }
111                 # filtering is necessary
112                 if ($k > $opts{N}) {
113                   $flt = 4;
114                   for my $x (@staging) {
115                          $x->[1] = 4 if ($x->[0] < 0 && $x->[3] + $oW >= $t[1] && $x->[1] == 0);
116                   }
117                 } else { # then check gap filter
118                   for my $x (@staging) {
119                         next if ($x->[0] < 0 || $x->[3] + $ow < $t[1]);
120                         if ($x->[0] >= $opts{G}) {
121                           $flt = 5; last;
122                         }
123                   }
124                 }
125           }
126         }
127         push(@staging, [$score, $flt, @t]);
128   }
129   # output the last few elements in the staging list
130   while (@staging) {
131         varFilter_aux(shift @staging, $opts{p});
132   }
133 }
134
135 sub varFilter_aux {
136   my ($first, $is_print) = @_;
137   if ($first->[1] == 0) {
138         print join("\t", @$first[2 .. @$first-1]), "\n";
139   } elsif ($is_print) {
140         print STDERR join("\t", substr("UQdDWGgX", $first->[1], 1), @$first[2 .. @$first-1]), "\n";
141   }
142 }
143
144 #
145 # pileup2fq
146 #
147
148 sub pileup2fq {
149   my %opts = (d=>3, D=>255, Q=>25, G=>25, l=>10);
150   getopts('d:D:Q:G:l:', \%opts);
151   die(qq/
152 Usage:   samtools.pl pileup2fq [options] <in.cns-pileup>
153
154 Options: -d INT    minimum depth        [$opts{d}]
155          -D INT    maximum depth        [$opts{D}]
156          -Q INT    min RMS mapQ         [$opts{Q}]
157          -G INT    minimum indel score  [$opts{G}]
158          -l INT    indel filter winsize [$opts{l}]\n
159 /) if (@ARGV == 0 && -t STDIN);
160
161   my ($last_chr, $seq, $qual, @gaps, $last_pos);
162   my $_Q = $opts{Q};
163   my $_d = $opts{d};
164   my $_D = $opts{D};
165
166   $last_chr = '';
167   while (<>) {
168         my @t = split;
169         if ($last_chr ne $t[0]) {
170           &p2q_post_process($last_chr, \$seq, \$qual, \@gaps, $opts{l}) if ($last_chr);
171           $last_chr = $t[0];
172           $last_pos = 0;
173           $seq = ''; $qual = '';
174           @gaps = ();
175         }
176         if ($t[1] - $last_pos != 1) {
177           $seq .= 'n' x ($t[1] - $last_pos - 1);
178           $qual .= '!' x ($t[1] - $last_pos - 1);
179         }
180         if ($t[2] eq '*') {
181           push(@gaps, $t[1]) if ($t[5] >= $opts{G});
182         } else {
183           $seq .= ($t[6] >= $_Q && $t[7] >= $_d && $t[7] <= $_D)? uc($t[3]) : lc($t[3]);
184           my $q = $t[4] + 33;
185           $q = 126 if ($q > 126);
186           $qual .= chr($q);
187         }
188         $last_pos = $t[1];
189   }
190   &p2q_post_process($last_chr, \$seq, \$qual, \@gaps, $opts{l});
191 }
192
193 sub p2q_post_process {
194   my ($chr, $seq, $qual, $gaps, $l) = @_;
195   &p2q_filter_gaps($seq, $gaps, $l);
196   print "\@$chr\n"; &p2q_print_str($seq);
197   print "+\n"; &p2q_print_str($qual);
198 }
199
200 sub p2q_filter_gaps {
201   my ($seq, $gaps, $l) = @_;
202   for my $g (@$gaps) {
203         my $x = $g > $l? $g - $l : 0;
204         substr($$seq, $x, $l + $l) = lc(substr($$seq, $x, $l + $l));
205   }
206 }
207
208 sub p2q_print_str {
209   my ($s) = @_;
210   my $l = length($$s);
211   for (my $i = 0; $i < $l; $i += 60) {
212         print substr($$s, $i, 60), "\n";
213   }
214 }
215
216 #
217 # Usage
218 #
219
220 sub usage {
221   die(qq/
222 Program: samtools.pl (helper script for SAMtools)
223 Version: $version
224 Contact: Heng Li <lh3\@sanger.ac.uk>\n
225 Usage:   samtools.pl <command> [<arguments>]\n
226 Command: varFilter     filtering SNPs and short indels
227          pileup2fq     generate fastq from `pileup -c'
228          showALEN      print alignment length (ALEN) following CIGAR
229 \n/);
230 }