10 &usage if (@ARGV < 1);
12 my $command = shift(@ARGV);
13 my %func = (showALEN=>\&showALEN, pileup2fq=>\&pileup2fq, varFilter=>\&varFilter,
14 unique=>\&unique, uniqcmp=>\&uniqcmp, sra2hdr=>\&sra2hdr, sam2fq=>\&sam2fq);
16 die("Unknown command \"$command\".\n") if (!defined($func{$command}));
25 die(qq/Usage: samtools.pl showALEN <in.sam>\n/) if (@ARGV == 0 && -t STDIN);
28 next if (/^\@/ || @t < 11);
31 s/(\d+)[MI]/$l+=$1/eg;
32 print join("\t", @t[0..5]), "\t$l\t", join("\t", @t[6..$#t]), "\n";
45 # W too many SNPs in a window (SNP only)
46 # G close to a high-quality indel (SNP only)
47 # Q low RMS mapping quality (SNP only)
48 # g close to another indel with higher quality (indel only)
51 my %opts = (d=>3, D=>100, l=>30, Q=>25, q=>10, G=>25, s=>100, w=>10, W=>10, N=>2, p=>undef);
52 getopts('pq:d:D:l:Q:w:W:N:G:', \%opts);
54 Usage: samtools.pl varFilter [options] <in.cns-pileup>
56 Options: -Q INT minimum RMS mapping quality for SNPs [$opts{Q}]
57 -q INT minimum RMS mapping quality for gaps [$opts{q}]
58 -d INT minimum read depth [$opts{d}]
59 -D INT maximum read depth [$opts{D}]
61 -G INT min indel score for nearby SNP filtering [$opts{G}]
62 -w INT SNP within INT bp around a gap to be filtered [$opts{w}]
64 -W INT window size for filtering dense SNPs [$opts{W}]
65 -N INT max number of SNPs in a window [$opts{N}]
67 -l INT window size for filtering adjacent gaps [$opts{l}]
69 -p print filtered variants
70 \n/) if (@ARGV == 0 && -t STDIN);
72 # calculate the window size
73 my ($ol, $ow, $oW) = ($opts{l}, $opts{w}, $opts{W});
74 my $max_dist = $ol > $ow? $ol : $ow;
75 $max_dist = $oW if ($max_dist < $oW);
77 my @staging; # (indel_filtering_score, flt_tag)
80 next if (uc($t[2]) eq uc($t[3]) || $t[3] eq '*/*'); # skip non-var sites
81 # clear the out-of-range elements
83 last if ($staging[0][2] eq $t[0] && $staging[0][3] + $max_dist >= $t[1]);
84 varFilter_aux(shift(@staging), $opts{p}); # calling a function is a bit slower, not much
86 my ($flt, $score) = (0, -1);
87 # first a simple filter
88 if ($t[7] < $opts{d}) {
90 } elsif ($t[7] > $opts{D}) {
93 # site dependent filters
95 if ($t[2] eq '*') { # an indel
96 $flt = 1 if ($t[6] < $opts{q});
98 if ($t[5] >= $opts{G}) {
99 for my $x (@staging) {
100 next if ($x->[0] >= 0 || $x->[3] + $ow < $t[1]);
101 $x->[1] = 5 if ($x->[1] == 0);
104 # calculate the filtering score (different from indel quality)
106 $score += $opts{s} * $t[10] if ($t[8] ne '*');
107 $score += $opts{s} * $t[11] if ($t[9] ne '*');
108 # check the staging list for indel filtering
109 for my $x (@staging) {
110 next if ($x->[0] < 0 || $x->[3] + $ol < $t[1]);
111 if ($x->[0] < $score) {
118 $flt = 1 if ($t[6] < $opts{Q});
119 # check adjacent SNPs
121 for my $x (@staging) {
122 ++$k if ($x->[0] < 0 && $x->[3] + $oW >= $t[1] && ($x->[1] == 0 || $x->[1] == 4 || $x->[1] == 5));
124 # filtering is necessary
127 for my $x (@staging) {
128 $x->[1] = 4 if ($x->[0] < 0 && $x->[3] + $oW >= $t[1] && $x->[1] == 0);
130 } else { # then check gap filter
131 for my $x (@staging) {
132 next if ($x->[0] < 0 || $x->[3] + $ow < $t[1]);
133 if ($x->[0] >= $opts{G}) {
140 push(@staging, [$score, $flt, @t]);
142 # output the last few elements in the staging list
144 varFilter_aux(shift @staging, $opts{p});
149 my ($first, $is_print) = @_;
150 if ($first->[1] == 0) {
151 print join("\t", @$first[2 .. @$first-1]), "\n";
152 } elsif ($is_print) {
153 print STDERR join("\t", substr("UQdDWGgX", $first->[1], 1), @$first[2 .. @$first-1]), "\n";
162 my %opts = (d=>3, D=>255, Q=>25, G=>25, l=>10);
163 getopts('d:D:Q:G:l:', \%opts);
165 Usage: samtools.pl pileup2fq [options] <in.cns-pileup>
167 Options: -d INT minimum depth [$opts{d}]
168 -D INT maximum depth [$opts{D}]
169 -Q INT min RMS mapQ [$opts{Q}]
170 -G INT minimum indel score [$opts{G}]
171 -l INT indel filter winsize [$opts{l}]\n
172 /) if (@ARGV == 0 && -t STDIN);
174 my ($last_chr, $seq, $qual, @gaps, $last_pos);
182 if ($last_chr ne $t[0]) {
183 &p2q_post_process($last_chr, \$seq, \$qual, \@gaps, $opts{l}) if ($last_chr);
186 $seq = ''; $qual = '';
189 if ($t[1] - $last_pos != 1) {
190 $seq .= 'n' x ($t[1] - $last_pos - 1);
191 $qual .= '!' x ($t[1] - $last_pos - 1);
194 push(@gaps, $t[1]) if ($t[5] >= $opts{G});
196 $seq .= ($t[6] >= $_Q && $t[7] >= $_d && $t[7] <= $_D)? uc($t[3]) : lc($t[3]);
198 $q = 126 if ($q > 126);
203 &p2q_post_process($last_chr, \$seq, \$qual, \@gaps, $opts{l});
206 sub p2q_post_process {
207 my ($chr, $seq, $qual, $gaps, $l) = @_;
208 &p2q_filter_gaps($seq, $gaps, $l);
209 print "\@$chr\n"; &p2q_print_str($seq);
210 print "+\n"; &p2q_print_str($qual);
213 sub p2q_filter_gaps {
214 my ($seq, $gaps, $l) = @_;
216 my $x = $g > $l? $g - $l : 0;
217 substr($$seq, $x, $l + $l) = lc(substr($$seq, $x, $l + $l));
224 for (my $i = 0; $i < $l; $i += 60) {
225 print substr($$s, $i, 60), "\n";
234 my %opts = (n=>20, p=>'');
235 getopts('n:p:', \%opts);
236 die("Usage: samtools.pl sam2fq [-n 20] [-p <prefix>] <inp.sam>\n") if (@ARGV == 0 && -t STDIN);
237 if ($opts{p} && $opts{n} > 1) {
240 for (0 .. $opts{n}-1) {
241 open($fh[$_], sprintf("| gzip > $pre.%.3d.fq.gz", $_)) || die;
248 next if ($t[9] eq '*');
249 my ($name, $seq, $qual);
250 if ($t[1] & 16) { # reverse strand
251 $seq = reverse($t[9]);
252 $qual = reverse($t[10]);
253 $seq =~ tr/ACGTacgt/TGCAtgca/;
255 ($seq, $qual) = @t[9,10];
258 $name .= "/1" if ($t[1] & 0x40);
259 $name .= "/2" if ($t[1] & 0x80);
260 print {$fh[$i]} "\@$name\n$seq\n";
262 print {$fh[$i]} "+\n$qual\n";
264 $i = 0 if (++$i == $opts{n});
266 close($fh[$_]) for (0 .. $opts{n}-1);
268 die("To be implemented.\n");
276 # This subroutine does not use an XML parser. It requires that the SRA
277 # XML files are properly formated.
281 die("Usage: samtools.pl sra2hdr <SRA.prefix>\n") if (@ARGV == 0);
285 my $sample = 'UNKNOWN';
286 open($fh, "$pre.sample.xml") || die;
288 $sample = $1 if (/<SAMPLE.*alias="([^"]+)"/i);
293 open($fh, "$pre.experiment.xml") || die;
295 if (/<EXPERIMENT.*accession="([^\s"]+)"/i) {
297 } elsif (/<LIBRARY_NAME>\s*(\S+)\s*<\/LIBRARY_NAME>/i) {
304 open($fh, "$pre.run.xml") || die;
306 if (/<RUN.*accession="([^\s"]+)"/i) {
308 } elsif (/<EXPERIMENT_REF.*accession="([^\s"]+)"/i) {
309 print "\@RG\tID:$run\tSM:$sample\tLB:$exp2lib{$1}\n";
310 } elsif (/<FILE.*filename="([^\s"]+)"/i) {
312 } elsif (/<\/RUN>/i) {
314 print STDERR "$fn[0]\t$run\n";
317 print STDERR "$fn[$_]\t$run", "_", $_+1, "\n";
330 my %opts = (f=>250.0, q=>5, r=>2, a=>1, b=>3);
331 getopts('Qf:q:r:a:b:m', \%opts);
332 die("Usage: samtools.pl unique [-f $opts{f}] <in.sam>\n") if (@ARGV == 0 && -t STDIN);
334 my $recal_Q = !defined($opts{Q});
335 my $multi_only = defined($opts{m});
340 $score = $1 if (/AS:i:(\d+)/);
343 if ($score < 0) { # AS tag is unavailable
345 my ($mm, $go, $ge) = (0, 0, 0);
346 $cigar =~ s/(\d+)[ID]/++$go,$ge+=$1/eg;
348 $cigar =~ s/(\d+)M/$mm+=$1/eg;
349 $score = $mm * $opts{a} - $go * $opts{q} - $ge * $opts{r}; # no mismatches...
351 $score = 1 if ($score < 1);
352 if ($t[0] ne $last) {
353 &unique_aux(\@a, $opts{f}, $recal_Q, $multi_only) if (@a);
356 push(@a, [$score, \@t]);
358 &unique_aux(\@a, $opts{f}, $recal_Q, $multi_only) if (@a);
362 my ($a, $fac, $is_recal, $multi_only) = @_;
363 my ($max, $max2, $max_i) = (0, 0, -1);
364 for (my $i = 0; $i < @$a; ++$i) {
365 if ($a->[$i][0] > $max) {
366 $max2 = $max; $max = $a->[$i][0]; $max_i = $i;
367 } elsif ($a->[$i][0] > $max2) {
372 if (!$multi_only || @$a > 1) {
373 my $q = int($fac * ($max - $max2) / $max + .499);
374 $q = 250 if ($q > 250);
375 $a->[$max_i][1][4] = $q < 250? $q : 250;
378 print join("\t", @{$a->[$max_i][1]});
383 # uniqcmp: compare two SAM files
387 my %opts = (q=>10, s=>100);
388 getopts('pq:s:', \%opts);
389 die("Usage: samtools.pl uniqcmp <in1.sam> <in2.sam>\n") if (@ARGV < 2);
391 warn("[uniqcmp] read the first file...\n");
392 &uniqcmp_aux($ARGV[0], \%a, 0);
393 warn("[uniqcmp] read the second file...\n");
394 &uniqcmp_aux($ARGV[1], \%a, 1);
395 warn("[uniqcmp] stats...\n");
397 $cnt[$_] = 0 for (0..9);
398 for my $x (keys %a) {
401 if (defined($p->[0]) && defined($p->[1])) {
402 $z = ($p->[0][0] == $p->[1][0] && $p->[0][1] eq $p->[1][1] && abs($p->[0][2] - $p->[1][2]) < $opts{s})? 0 : 1;
403 if ($p->[0][3] >= $opts{q} && $p->[1][3] >= $opts{q}) {
405 } elsif ($p->[0][3] >= $opts{q}) {
407 } elsif ($p->[1][3] >= $opts{q}) {
410 print STDERR "$x\t$p->[0][1]:$p->[0][2]\t$p->[0][3]\t$p->[0][4]\t$p->[1][1]:$p->[1][2]\t$p->[1][3]\t$p->[1][4]\t",
411 $p->[0][5]-$p->[1][5], "\n" if ($z && defined($opts{p}) && ($p->[0][3] >= $opts{q} || $p->[1][3] >= $opts{q}));
412 } elsif (defined($p->[0])) {
413 ++$cnt[$p->[0][3]>=$opts{q}? 6 : 7];
414 print STDERR "$x\t$p->[0][1]:$p->[0][2]\t$p->[0][3]\t$p->[0][4]\t*\t0\t*\t",
415 $p->[0][5], "\n" if (defined($opts{p}) && $p->[0][3] >= $opts{q});
417 print STDERR "$x\t*\t0\t*\t$p->[1][1]:$p->[1][2]\t$p->[1][3]\t$p->[1][4]\t",
418 -$p->[1][5], "\n" if (defined($opts{p}) && $p->[1][3] >= $opts{q});
419 ++$cnt[$p->[1][3]>=$opts{q}? 8 : 9];
422 print "Consistent (high, high): $cnt[0]\n";
423 print "Consistent (high, low ): $cnt[1]\n";
424 print "Consistent (low , high): $cnt[2]\n";
425 print "Inconsistent (high, high): $cnt[3]\n";
426 print "Inconsistent (high, low ): $cnt[4]\n";
427 print "Inconsistent (low , high): $cnt[5]\n";
428 print "Second missing (high): $cnt[6]\n";
429 print "Second missing (low ): $cnt[7]\n";
430 print "First missing (high): $cnt[8]\n";
431 print "First missing (low ): $cnt[9]\n";
435 my ($fn, $a, $which) = @_;
437 $fn = "samtools view $fn |" if ($fn =~ /\.bam/);
438 open($fh, $fn) || die;
442 # my $l = ($t[5] =~ /^(\d+)S/)? $1 : 0;
444 my ($x, $nm) = (0, 0);
445 $nm = $1 if (/NM:i:(\d+)/);
447 s/(\d+)[MI]/$x+=$1/eg;
448 @{$a->{$t[0]}[$which]} = (($t[1]&0x10)? 1 : 0, $t[2], $t[3]-$l, $t[4], "$x:$nm", $x - 4 * $nm);
459 Program: samtools.pl (helper script for SAMtools)
461 Contact: Heng Li <lh3\@sanger.ac.uk>\n
462 Usage: samtools.pl <command> [<arguments>]\n
463 Command: varFilter filtering SNPs and short indels
464 pileup2fq generate fastq from `pileup -c'
465 showALEN print alignment length (ALEN) following CIGAR