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);
29 s/(\d+)[SMI]/$l+=$1/eg;
30 print join("\t", @t[0..5]), "\t$l\t", join("\t", @t[6..$#t]), "\n";
39 my %opts = (d=>3, D=>100, l=>30, Q=>25, q=>10, G=>25, s=>100, w=>10, W=>10, N=>2, p=>undef);
40 getopts('pd:D:l:Q:w:W:N:G:', \%opts);
42 Usage: samtools.pl varFilter [options] <in.cns-pileup>
44 Options: -Q INT minimum RMS mapping quality for SNPs [$opts{Q}]
45 -q INT minimum RMS mapping quality for gaps [$opts{q}]
46 -d INT minimum read depth [$opts{d}]
47 -D INT maximum read depth [$opts{D}]
49 -G INT min indel score for nearby SNP filtering [$opts{G}]
50 -w INT SNP within INT bp around a gap to be filtered [$opts{w}]
52 -W INT window size for filtering dense SNPs [$opts{W}]
53 -N INT max number of SNPs in a window [$opts{N}]
55 -l INT window size for filtering adjacent gaps [$opts{l}]
57 -p print filtered variants
58 \n/) if (@ARGV == 0 && -t STDIN);
60 # calculate the window size
61 my ($ol, $ow, $oW) = ($opts{l}, $opts{w}, $opts{W});
62 my $max_dist = $ol > $ow? $ol : $ow;
63 $max_dist = $oW if ($max_dist < $oW);
65 my @staging; # (indel_filtering_score, flt_tag)
68 next if ($t[2] eq $t[3] || $t[3] eq '*/*'); # skip non-var sites
69 # clear the out-of-range elements
71 last if ($staging[0][2] eq $t[0] && $staging[0][3] + $max_dist >= $t[1]);
72 varFilter_aux(shift(@staging), $opts{p}); # calling a function is a bit slower, not much
74 my ($flt, $score) = (0, -1);
75 # first a simple filter
76 if ($t[7] < $opts{d}) {
78 } elsif ($t[7] > $opts{D}) {
81 # site dependent filters
83 if ($t[2] eq '*') { # an indel
84 $flt = 1 if ($t[6] < $opts{q});
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);
92 # calculate the filtering score (different from indel quality)
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) {
106 $flt = 1 if ($t[6] < $opts{Q});
107 # check adjacent SNPs
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));
112 # filtering is necessary
115 for my $x (@staging) {
116 $x->[1] = 4 if ($x->[0] < 0 && $x->[3] + $oW >= $t[1] && $x->[1] == 0);
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}) {
128 push(@staging, [$score, $flt, @t]);
130 # output the last few elements in the staging list
132 varFilter_aux(shift @staging, $opts{p});
137 my ($first, $is_print) = @_;
138 if ($first->[1] == 0) {
139 print join("\t", @$first[2 .. @$first-1]), "\n";
140 } elsif ($is_print) {
141 print STDERR join("\t", substr("UQdDWGgX", $first->[1], 1), @$first[2 .. @$first-1]), "\n";
150 my %opts = (d=>3, D=>255, Q=>25, G=>25, l=>10);
151 getopts('d:D:Q:G:l:', \%opts);
153 Usage: samtools.pl pileup2fq [options] <in.cns-pileup>
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);
162 my ($last_chr, $seq, $qual, @gaps, $last_pos);
170 if ($last_chr ne $t[0]) {
171 &p2q_post_process($last_chr, \$seq, \$qual, \@gaps, $opts{l}) if ($last_chr);
174 $seq = ''; $qual = '';
177 if ($t[1] - $last_pos != 1) {
178 $seq .= 'n' x ($t[1] - $last_pos - 1);
179 $qual .= '!' x ($t[1] - $last_pos - 1);
182 push(@gaps, $t[1]) if ($t[5] >= $opts{G});
184 $seq .= ($t[6] >= $_Q && $t[7] >= $_d && $t[7] <= $_D)? uc($t[3]) : lc($t[3]);
186 $q = 126 if ($q > 126);
191 &p2q_post_process($last_chr, \$seq, \$qual, \@gaps, $opts{l});
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);
201 sub p2q_filter_gaps {
202 my ($seq, $gaps, $l) = @_;
204 my $x = $g > $l? $g - $l : 0;
205 substr($$seq, $x, $l + $l) = lc(substr($$seq, $x, $l + $l));
212 for (my $i = 0; $i < $l; $i += 60) {
213 print substr($$s, $i, 60), "\n";
222 my %opts = (f=>5.0, q=>5, r=>2, a=>1, b=>3);
223 getopts('f:', \%opts);
224 die("Usage: samtools.pl unique [-f $opts{f}] <in.sam>\n") if (@ARGV == 0 && -t STDIN);
230 $score = $1 if (/AS:i:(\d+)/);
232 if ($score < 0) { # AS tag is unavailable
234 my ($mm, $go, $ge) = (0, 0, 0);
235 $cigar =~ s/(\d+)[ID]/++$go,$ge+=$1/eg;
237 $cigar =~ s/(\d+)M/$mm+=$1/eg;
238 $score = $mm * $opts{a} - $go * $opts{q} - $ge * $opts{r}; # no mismatches...
240 $score = 0 if ($score < 0);
241 if ($t[0] ne $last) {
242 &unique_aux(\@a, $opts{f}) if (@a);
245 push(@a, [$score, \@t]);
247 &unique_aux(\@a, $opts{f}) if (@a);
252 my ($max, $max2, $max_i) = (-1, -1, -1);
253 for (my $i = 0; $i < @$a; ++$i) {
254 if ($a->[$i][0] > $max) {
255 $max2 = $max; $max = $a->[$i][0]; $max_i = $i;
256 } elsif ($a->[$i][0] > $max2) {
260 my $q = int($fac * ($max - $max2) + .499);
261 $a->[$max_i][1][4] = $q < 250? $q : 250;
262 print join("\t", @{$a->[$max_i][1]});
272 Program: samtools.pl (helper script for SAMtools)
274 Contact: Heng Li <lh3\@sanger.ac.uk>\n
275 Usage: samtools.pl <command> [<arguments>]\n
276 Command: varFilter filtering SNPs and short indels
277 pileup2fq generate fastq from `pileup -c'
278 showALEN print alignment length (ALEN) following CIGAR