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{N}]
54 -l INT window size for filtering adjacent gaps [$opts{l}]
56 -p print filtered variants
57 \n/) 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 last if ($staging[0][2] eq $t[0] && $staging[0][3] + $max_dist >= $t[1]);
71 varFilter_aux(shift(@staging), $opts{p}); # calling a function is a bit slower, not much
73 my ($flt, $score) = (0, -1);
74 # first a simple filter
75 if ($t[6] < $opts{Q}) {
77 } elsif ($t[7] < $opts{d}) {
79 } elsif ($t[7] > $opts{D}) {
82 # site dependent filters
84 if ($t[2] eq '*') { # an indel
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 # check adjacent SNPs
108 for my $x (@staging) {
109 ++$k if ($x->[0] < 0 && $x->[3] + $oW >= $t[1] && ($x->[1] == 0 || $x->[1] == 4 || $x->[1] == 5));
111 # filtering is necessary
114 for my $x (@staging) {
115 $x->[1] = 4 if ($x->[0] < 0 && $x->[3] + $oW >= $t[1] && $x->[1] == 0);
117 } else { # then check gap filter
118 for my $x (@staging) {
119 next if ($x->[0] < 0 || $x->[3] + $ow < $t[1]);
120 if ($x->[0] >= $opts{G}) {
127 push(@staging, [$score, $flt, @t]);
129 # output the last few elements in the staging list
131 varFilter_aux(shift @staging, $opts{p});
136 my ($first, $is_print) = @_;
137 if ($first->[1] == 0) {
138 print join("\t", @$first[2 .. @$first-1]), "\n";
139 } elsif ($is_print) {
140 print STDERR join("\t", substr("UQdDWGgX", $first->[1], 1), @$first[2 .. @$first-1]), "\n";
149 my %opts = (d=>3, D=>255, Q=>25, G=>25, l=>10);
150 getopts('d:D:Q:G:l:', \%opts);
152 Usage: samtools.pl pileup2fq [options] <in.cns-pileup>
154 Options: -d INT minimum depth [$opts{d}]
155 -D INT maximum depth [$opts{D}]
156 -Q INT min RMS mapQ [$opts{Q}]
157 -G INT minimum indel score [$opts{G}]
158 -l INT indel filter winsize [$opts{l}]\n
159 /) if (@ARGV == 0 && -t STDIN);
161 my ($last_chr, $seq, $qual, @gaps, $last_pos);
169 if ($last_chr ne $t[0]) {
170 &p2q_post_process($last_chr, \$seq, \$qual, \@gaps, $opts{l}) if ($last_chr);
173 $seq = ''; $qual = '';
176 if ($t[1] - $last_pos != 1) {
177 $seq .= 'n' x ($t[1] - $last_pos - 1);
178 $qual .= '!' x ($t[1] - $last_pos - 1);
181 push(@gaps, $t[1]) if ($t[5] >= $opts{G});
183 $seq .= ($t[6] >= $_Q && $t[7] >= $_d && $t[7] <= $_D)? uc($t[3]) : lc($t[3]);
185 $q = 126 if ($q > 126);
190 &p2q_post_process($last_chr, \$seq, \$qual, \@gaps, $opts{l});
193 sub p2q_post_process {
194 my ($chr, $seq, $qual, $gaps, $l) = @_;
195 &p2q_filter_gaps($seq, $gaps, $l);
196 print "\@$chr\n"; &p2q_print_str($seq);
197 print "+\n"; &p2q_print_str($qual);
200 sub p2q_filter_gaps {
201 my ($seq, $gaps, $l) = @_;
203 my $x = $g > $l? $g - $l : 0;
204 substr($$seq, $x, $l + $l) = lc(substr($$seq, $x, $l + $l));
211 for (my $i = 0; $i < $l; $i += 60) {
212 print substr($$s, $i, 60), "\n";
222 Program: samtools.pl (helper script for SAMtools)
224 Contact: Heng Li <lh3\@sanger.ac.uk>\n
225 Usage: samtools.pl <command> [<arguments>]\n
226 Command: varFilter filtering SNPs and short indels
227 pileup2fq generate fastq from `pileup -c'
228 showALEN print alignment length (ALEN) following CIGAR