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