10 &usage if (@ARGV < 1);
12 my $command = shift(@ARGV);
13 my %func = (showALEN=>\&showALEN, pileup2fq=>\&pileup2fq, varFilter=>\&varFilter);
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, 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 [$opts{Q}]
45 -d INT minimum read depth [$opts{d}]
46 -D INT maximum read depth [$opts{D}]
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}]
51 -W INT window size for filtering dense SNPs [$opts{W}]
52 -N INT max number of SNPs in a window [$opts{W}]
54 -l INT window size for filtering adjacent gaps [$opts{l}]
56 -p print filtered variants
57 /) if (@ARGV == 0 && -t STDIN);
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);
64 my @staging; # (indel_filtering_score, flt_tag)
67 next if ($t[2] eq $t[3] || $t[3] eq '*/*'); # skip non-var sites
68 # clear the out-of-range elements
70 if ($staging[0][2] ne $t[0] || $staging[0][3] + $max_dist < $t[1]) {
71 varFilter_aux(shift @staging, $opts{p});
76 my ($flt, $score) = (0, -1);
77 # first a simple filter
78 if ($t[6] < $opts{Q}) {
80 } elsif ($t[7] < $opts{d}) {
82 } elsif ($t[7] > $opts{D}) {
85 # site dependent filters
87 if ($t[2] eq '*') { # an indel
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);
95 # calculate the filtering score (different from indel quality)
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) {
109 # check adjacent SNPs
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));
114 # filtering is necessary
117 for my $x (@staging) {
118 $x->[1] = 4 if ($x->[0] < 0 && $x->[3] + $oW >= $t[1] && $x->[1] == 0);
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}) {
130 push(@staging, [$score, $flt, @t]);
132 # output the last few elements in the staging list
134 varFilter_aux(shift @staging, $opts{p});
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";
152 my %opts = (d=>3, D=>255, Q=>25, G=>25, l=>10);
153 getopts('d:D:Q:G:l:', \%opts);
155 Usage: samtools.pl pileup2fq [options] <in.cns-pileup>
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);
164 my ($last_chr, $seq, $qual, @gaps, $last_pos);
172 if ($last_chr ne $t[0]) {
173 &p2q_post_process($last_chr, \$seq, \$qual, \@gaps, $opts{l}) if ($last_chr);
176 $seq = ''; $qual = '';
179 if ($t[1] - $last_pos != 1) {
180 $seq .= 'n' x ($t[1] - $last_pos - 1);
181 $qual .= '!' x ($t[1] - $last_pos - 1);
184 push(@gaps, $t[1]) if ($t[5] >= $opts{G});
186 $seq .= ($t[6] >= $_Q && $t[7] >= $_d && $t[7] <= $_D)? uc($t[3]) : lc($t[3]);
188 $q = 126 if ($q > 126);
193 &p2q_post_process($last_chr, \$seq, \$qual, \@gaps, $opts{l});
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);
203 sub p2q_filter_gaps {
204 my ($seq, $gaps, $l) = @_;
206 my $x = $g > $l? $g - $l : 0;
207 substr($$seq, $x, $l + $l) = lc(substr($$seq, $x, $l + $l));
214 for (my $i = 0; $i < $l; $i += 60) {
215 print substr($$s, $i, 60), "\n";
225 Program: samtools.pl (helper script for SAMtools)
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