9 my $version = '0.3.2 (r321)';
10 &usage if (@ARGV < 1);
12 my $command = shift(@ARGV);
13 my %func = (showALEN=>\&showALEN, pileup2fq=>\&pileup2fq, varFilter=>\&varFilter, unique=>\&unique);
15 die("Unknown command \"$command\".\n") if (!defined($func{$command}));
24 die(qq/Usage: samtools.pl showALEN <in.sam>\n/) if (@ARGV == 0 && -t STDIN);
27 next if (/^\@/ || @t < 11);
30 s/(\d+)[SMI]/$l+=$1/eg;
31 print join("\t", @t[0..5]), "\t$l\t", join("\t", @t[6..$#t]), "\n";
40 my %opts = (d=>3, D=>100, l=>30, Q=>25, q=>10, G=>25, s=>100, w=>10, W=>10, N=>2, p=>undef);
41 getopts('pd:D:l:Q:w:W:N:G:', \%opts);
43 Usage: samtools.pl varFilter [options] <in.cns-pileup>
45 Options: -Q INT minimum RMS mapping quality for SNPs [$opts{Q}]
46 -q INT minimum RMS mapping quality for gaps [$opts{q}]
47 -d INT minimum read depth [$opts{d}]
48 -D INT maximum read depth [$opts{D}]
50 -G INT min indel score for nearby SNP filtering [$opts{G}]
51 -w INT SNP within INT bp around a gap to be filtered [$opts{w}]
53 -W INT window size for filtering dense SNPs [$opts{W}]
54 -N INT max number of SNPs in a window [$opts{N}]
56 -l INT window size for filtering adjacent gaps [$opts{l}]
58 -p print filtered variants
59 \n/) if (@ARGV == 0 && -t STDIN);
61 # calculate the window size
62 my ($ol, $ow, $oW) = ($opts{l}, $opts{w}, $opts{W});
63 my $max_dist = $ol > $ow? $ol : $ow;
64 $max_dist = $oW if ($max_dist < $oW);
66 my @staging; # (indel_filtering_score, flt_tag)
69 next if ($t[2] eq $t[3] || $t[3] eq '*/*'); # skip non-var sites
70 # clear the out-of-range elements
72 last if ($staging[0][2] eq $t[0] && $staging[0][3] + $max_dist >= $t[1]);
73 varFilter_aux(shift(@staging), $opts{p}); # calling a function is a bit slower, not much
75 my ($flt, $score) = (0, -1);
76 # first a simple filter
77 if ($t[7] < $opts{d}) {
79 } elsif ($t[7] > $opts{D}) {
82 # site dependent filters
84 if ($t[2] eq '*') { # an indel
85 $flt = 1 if ($t[6] < $opts{q});
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);
93 # calculate the filtering score (different from indel quality)
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) {
107 $flt = 1 if ($t[6] < $opts{Q});
108 # check adjacent SNPs
110 for my $x (@staging) {
111 ++$k if ($x->[0] < 0 && $x->[3] + $oW >= $t[1] && ($x->[1] == 0 || $x->[1] == 4 || $x->[1] == 5));
113 # filtering is necessary
116 for my $x (@staging) {
117 $x->[1] = 4 if ($x->[0] < 0 && $x->[3] + $oW >= $t[1] && $x->[1] == 0);
119 } else { # then check gap filter
120 for my $x (@staging) {
121 next if ($x->[0] < 0 || $x->[3] + $ow < $t[1]);
122 if ($x->[0] >= $opts{G}) {
129 push(@staging, [$score, $flt, @t]);
131 # output the last few elements in the staging list
133 varFilter_aux(shift @staging, $opts{p});
138 my ($first, $is_print) = @_;
139 if ($first->[1] == 0) {
140 print join("\t", @$first[2 .. @$first-1]), "\n";
141 } elsif ($is_print) {
142 print STDERR join("\t", substr("UQdDWGgX", $first->[1], 1), @$first[2 .. @$first-1]), "\n";
151 my %opts = (d=>3, D=>255, Q=>25, G=>25, l=>10);
152 getopts('d:D:Q:G:l:', \%opts);
154 Usage: samtools.pl pileup2fq [options] <in.cns-pileup>
156 Options: -d INT minimum depth [$opts{d}]
157 -D INT maximum depth [$opts{D}]
158 -Q INT min RMS mapQ [$opts{Q}]
159 -G INT minimum indel score [$opts{G}]
160 -l INT indel filter winsize [$opts{l}]\n
161 /) if (@ARGV == 0 && -t STDIN);
163 my ($last_chr, $seq, $qual, @gaps, $last_pos);
171 if ($last_chr ne $t[0]) {
172 &p2q_post_process($last_chr, \$seq, \$qual, \@gaps, $opts{l}) if ($last_chr);
175 $seq = ''; $qual = '';
178 if ($t[1] - $last_pos != 1) {
179 $seq .= 'n' x ($t[1] - $last_pos - 1);
180 $qual .= '!' x ($t[1] - $last_pos - 1);
183 push(@gaps, $t[1]) if ($t[5] >= $opts{G});
185 $seq .= ($t[6] >= $_Q && $t[7] >= $_d && $t[7] <= $_D)? uc($t[3]) : lc($t[3]);
187 $q = 126 if ($q > 126);
192 &p2q_post_process($last_chr, \$seq, \$qual, \@gaps, $opts{l});
195 sub p2q_post_process {
196 my ($chr, $seq, $qual, $gaps, $l) = @_;
197 &p2q_filter_gaps($seq, $gaps, $l);
198 print "\@$chr\n"; &p2q_print_str($seq);
199 print "+\n"; &p2q_print_str($qual);
202 sub p2q_filter_gaps {
203 my ($seq, $gaps, $l) = @_;
205 my $x = $g > $l? $g - $l : 0;
206 substr($$seq, $x, $l + $l) = lc(substr($$seq, $x, $l + $l));
213 for (my $i = 0; $i < $l; $i += 60) {
214 print substr($$s, $i, 60), "\n";
223 my %opts = (f=>5.0, q=>5, r=>2, a=>1, b=>3);
224 getopts('f:', \%opts);
225 die("Usage: samtools.pl unique [-f $opts{f}] <in.sam>\n") if (@ARGV == 0 && -t STDIN);
231 $score = $1 if (/AS:i:(\d+)/);
234 if ($score < 0) { # AS tag is unavailable
236 my ($mm, $go, $ge) = (0, 0, 0);
237 $cigar =~ s/(\d+)[ID]/++$go,$ge+=$1/eg;
239 $cigar =~ s/(\d+)M/$mm+=$1/eg;
240 $score = $mm * $opts{a} - $go * $opts{q} - $ge * $opts{r}; # no mismatches...
242 $score = 0 if ($score < 0);
243 if ($t[0] ne $last) {
244 &unique_aux(\@a, $opts{f}) if (@a);
247 push(@a, [$score, \@t]);
249 &unique_aux(\@a, $opts{f}) if (@a);
254 my ($max, $max2, $max_i) = (-1, -1, -1);
255 for (my $i = 0; $i < @$a; ++$i) {
256 if ($a->[$i][0] > $max) {
257 $max2 = $max; $max = $a->[$i][0]; $max_i = $i;
258 } elsif ($a->[$i][0] > $max2) {
262 my $q = int($fac * ($max - $max2) + .499);
263 $a->[$max_i][1][4] = $q < 250? $q : 250;
264 print join("\t", @{$a->[$max_i][1]});
274 Program: samtools.pl (helper script for SAMtools)
276 Contact: Heng Li <lh3\@sanger.ac.uk>\n
277 Usage: samtools.pl <command> [<arguments>]\n
278 Command: varFilter filtering SNPs and short indels
279 pileup2fq generate fastq from `pileup -c'
280 showALEN print alignment length (ALEN) following CIGAR