]> git.donarmstrong.com Git - samtools.git/blob - misc/samtools.pl
* samtools.pl-0.3.1
[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{W}]
53
54          -l INT    window size for filtering adjacent gaps [$opts{l}]
55
56          -p        print filtered variants
57 /) 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           if ($staging[0][2] ne $t[0] || $staging[0][3] + $max_dist < $t[1]) {
71                 varFilter_aux(shift @staging, $opts{p});
72           } else {
73                 last;
74           }
75         }
76         my ($flt, $score) = (0, -1);
77         # first a simple filter
78         if ($t[6] < $opts{Q}) {
79           $flt = 1;
80         } elsif ($t[7] < $opts{d}) {
81           $flt = 2;
82         } elsif ($t[7] > $opts{D}) {
83           $flt = 3;
84         }
85         # site dependent filters
86         if ($flt == 0) {
87           if ($t[2] eq '*') { # an indel
88                 # filtering SNPs
89                 if ($t[5] >= $opts{G}) {
90                   for my $x (@staging) {
91                         next if ($x->[0] >= 0 || $x->[3] + $ow < $t[1]);
92                         $x->[1] = 5 if ($x->[1] == 0);
93                   }
94                 }
95                 # calculate the filtering score (different from indel quality)
96                 $score = $t[5];
97                 $score += $opts{s} * $t[10] if ($t[8] ne '*');
98                 $score += $opts{s} * $t[11] if ($t[9] ne '*');
99                 # check the staging list for indel filtering
100                 for my $x (@staging) {
101                   next if ($x->[0] < 0 || $x->[3] + $ol < $t[1]);
102                   if ($x->[0] < $score) {
103                         $x->[1] = 6;
104                   } else {
105                         $flt = 6; last;
106                   }
107                 }
108           } else { # a SNP
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 # Usage
221 #
222
223 sub usage {
224   die(qq/
225 Program: samtools.pl (helper script for SAMtools)
226 Version: $version
227 Contact: Heng Li <lh3\@sanger.ac.uk>\n
228 Usage:   samtools.pl <command> [<arguments>]\n
229 Command: varFilter     filtering SNPs and short indels
230          pileup2fq     generate fastq from `pileup -c'
231          showALEN      print alignment length (ALEN) following CIGAR
232 \n/);
233 }