+ next if (uc($t[2]) eq uc($t[3]) || $t[3] eq '*/*'); # skip non-var sites
+ # clear the out-of-range elements
+ while (@staging) {
+ # Still on the same chromosome and the first element's window still affects this position?
+ last if ($staging[0][3] eq $t[0] && $staging[0][4] + $staging[0][2] + $max_dist >= $t[1]);
+ varFilter_aux(shift(@staging), $opts{p}); # calling a function is a bit slower, not much
+ }
+ my ($flt, $score) = (0, -1);
+ # first a simple filter
+ if ($t[7] < $opts{d}) {
+ $flt = 2;
+ } elsif ($t[7] > $opts{D}) {
+ $flt = 3;
+ }
+ if ($t[2] eq '*') { # an indel
+ if ($opts{i} && $opts{i}>$t[5]) { $flt = 8; }
+ }
+ elsif ($opts{S} && $opts{S}>$t[5]) { $flt = 7; } # SNP
+
+ # site dependent filters
+ my $len=0;
+ if ($flt == 0) {
+ if ($t[2] eq '*') { # an indel
+ # If deletion, remember the length of the deletion
+ my ($a,$b) = split(m{/},$t[3]);
+ my $alen = length($a) - 1;
+ my $blen = length($b) - 1;
+ if ( $alen>$blen )
+ {
+ if ( substr($a,0,1) eq '-' ) { $len=$alen; }
+ }
+ elsif ( substr($b,0,1) eq '-' ) { $len=$blen; }
+
+ $flt = 1 if ($t[6] < $opts{q});
+ # filtering SNPs
+ if ($t[5] >= $opts{G}) {
+ for my $x (@staging) {
+ # Is it a SNP and is it outside the SNP filter window?
+ next if ($x->[0] >= 0 || $x->[4] + $x->[2] + $ow < $t[1]);
+ $x->[1] = 5 if ($x->[1] == 0);
+ }
+ }
+ # calculate the filtering score (different from indel quality)
+ $score = $t[5];
+ $score += $opts{s} * $t[10] if ($t[8] ne '*');
+ $score += $opts{s} * $t[11] if ($t[9] ne '*');
+ # check the staging list for indel filtering
+ for my $x (@staging) {
+ # Is it a SNP and is it outside the gap filter window
+ next if ($x->[0] < 0 || $x->[4] + $x->[2] + $ol < $t[1]);
+ if ($x->[0] < $score) {
+ $x->[1] = 6;
+ } else {
+ $flt = 6; last;
+ }
+ }
+ } else { # a SNP
+ $flt = 1 if ($t[6] < $opts{Q});
+ # check adjacent SNPs
+ my $k = 1;
+ for my $x (@staging) {
+ ++$k if ($x->[0] < 0 && $x->[4] + $x->[2] + $oW >= $t[1] && ($x->[1] == 0 || $x->[1] == 4 || $x->[1] == 5));
+ }
+ # filtering is necessary
+ if ($k > $opts{N}) {
+ $flt = 4;
+ for my $x (@staging) {
+ $x->[1] = 4 if ($x->[0] < 0 && $x->[4] + $x->[2] + $oW >= $t[1] && $x->[1] == 0);
+ }
+ } else { # then check gap filter
+ for my $x (@staging) {
+ next if ($x->[0] < 0 || $x->[4] + $x->[2] + $ow < $t[1]);
+ if ($x->[0] >= $opts{G}) {
+ $flt = 5; last;
+ }
+ }
+ }
+ }
+ }
+ push(@staging, [$score, $flt, $len, @t]);
+ }
+ # output the last few elements in the staging list
+ while (@staging) {
+ varFilter_aux(shift @staging, $opts{p});
+ }
+}
+
+sub varFilter_aux {
+ my ($first, $is_print) = @_;
+ if ($first->[1] == 0) {
+ print join("\t", @$first[3 .. @$first-1]), "\n";
+ } elsif ($is_print) {
+ print STDERR join("\t", substr("UQdDWGgsiX", $first->[1], 1), @$first[3 .. @$first-1]), "\n";
+ }
+}
+
+#
+# pileup2fq
+#
+
+sub pileup2fq {
+ my %opts = (d=>3, D=>255, Q=>25, G=>25, l=>10);
+ getopts('d:D:Q:G:l:', \%opts);
+ die(qq/
+Usage: samtools.pl pileup2fq [options] <in.cns-pileup>
+
+Options: -d INT minimum depth [$opts{d}]
+ -D INT maximum depth [$opts{D}]
+ -Q INT min RMS mapQ [$opts{Q}]
+ -G INT minimum indel score [$opts{G}]
+ -l INT indel filter winsize [$opts{l}]\n
+/) if (@ARGV == 0 && -t STDIN);
+
+ my ($last_chr, $seq, $qual, @gaps, $last_pos);
+ my $_Q = $opts{Q};
+ my $_d = $opts{d};
+ my $_D = $opts{D};
+
+ $last_chr = '';
+ while (<>) {
+ my @t = split;
+ if ($last_chr ne $t[0]) {
+ &p2q_post_process($last_chr, \$seq, \$qual, \@gaps, $opts{l}) if ($last_chr);
+ $last_chr = $t[0];
+ $last_pos = 0;
+ $seq = ''; $qual = '';
+ @gaps = ();
+ }
+ if ($t[1] - $last_pos != 1) {
+ $seq .= 'n' x ($t[1] - $last_pos - 1);
+ $qual .= '!' x ($t[1] - $last_pos - 1);
+ }
+ if ($t[2] eq '*') {
+ push(@gaps, $t[1]) if ($t[5] >= $opts{G});
+ } else {
+ $seq .= ($t[6] >= $_Q && $t[7] >= $_d && $t[7] <= $_D)? uc($t[3]) : lc($t[3]);
+ my $q = $t[4] + 33;
+ $q = 126 if ($q > 126);
+ $qual .= chr($q);
+ }
+ $last_pos = $t[1];
+ }
+ &p2q_post_process($last_chr, \$seq, \$qual, \@gaps, $opts{l});
+}
+
+sub p2q_post_process {
+ my ($chr, $seq, $qual, $gaps, $l) = @_;
+ &p2q_filter_gaps($seq, $gaps, $l);
+ print "\@$chr\n"; &p2q_print_str($seq);
+ print "+\n"; &p2q_print_str($qual);
+}
+
+sub p2q_filter_gaps {
+ my ($seq, $gaps, $l) = @_;
+ for my $g (@$gaps) {
+ my $x = $g > $l? $g - $l : 0;
+ substr($$seq, $x, $l + $l) = lc(substr($$seq, $x, $l + $l));
+ }
+}
+
+sub p2q_print_str {
+ my ($s) = @_;
+ my $l = length($$s);
+ for (my $i = 0; $i < $l; $i += 60) {
+ print substr($$s, $i, 60), "\n";
+ }
+}
+
+#
+# sam2fq
+#
+
+sub sam2fq {
+ my %opts = (n=>20, p=>'');
+ getopts('n:p:', \%opts);
+ die("Usage: samtools.pl sam2fq [-n 20] [-p <prefix>] <inp.sam>\n") if (@ARGV == 0 && -t STDIN);
+ if ($opts{p} && $opts{n} > 1) {
+ my $pre = $opts{p};
+ my @fh;
+ for (0 .. $opts{n}-1) {
+ open($fh[$_], sprintf("| gzip > $pre.%.3d.fq.gz", $_)) || die;
+ }
+ my $i = 0;
+ while (<>) {
+ next if (/^@/);
+ chomp;
+ my @t = split("\t");
+ next if ($t[9] eq '*');
+ my ($name, $seq, $qual);
+ if ($t[1] & 16) { # reverse strand
+ $seq = reverse($t[9]);
+ $qual = reverse($t[10]);
+ $seq =~ tr/ACGTacgt/TGCAtgca/;
+ } else {
+ ($seq, $qual) = @t[9,10];
+ }
+ $name = $t[0];
+ $name .= "/1" if ($t[1] & 0x40);
+ $name .= "/2" if ($t[1] & 0x80);
+ print {$fh[$i]} "\@$name\n$seq\n";
+ if ($qual ne '*') {
+ print {$fh[$i]} "+\n$qual\n";
+ }
+ $i = 0 if (++$i == $opts{n});
+ }
+ close($fh[$_]) for (0 .. $opts{n}-1);
+ } else {
+ die("To be implemented.\n");
+ }
+}
+
+#
+# sra2hdr
+#
+
+# This subroutine does not use an XML parser. It requires that the SRA
+# XML files are properly formated.
+sub sra2hdr {
+ my %opts = ();
+ getopts('', \%opts);
+ die("Usage: samtools.pl sra2hdr <SRA.prefix>\n") if (@ARGV == 0);
+ my $pre = $ARGV[0];
+ my $fh;
+ # read sample
+ my $sample = 'UNKNOWN';
+ open($fh, "$pre.sample.xml") || die;
+ while (<$fh>) {
+ $sample = $1 if (/<SAMPLE.*alias="([^"]+)"/i);
+ }
+ close($fh);
+ # read experiment
+ my (%exp2lib, $exp);
+ open($fh, "$pre.experiment.xml") || die;
+ while (<$fh>) {
+ if (/<EXPERIMENT.*accession="([^\s"]+)"/i) {
+ $exp = $1;
+ } elsif (/<LIBRARY_NAME>\s*(\S+)\s*<\/LIBRARY_NAME>/i) {
+ $exp2lib{$exp} = $1;
+ }
+ }
+ close($fh);
+ # read run
+ my ($run, @fn);
+ open($fh, "$pre.run.xml") || die;
+ while (<$fh>) {
+ if (/<RUN.*accession="([^\s"]+)"/i) {
+ $run = $1; @fn = ();
+ } elsif (/<EXPERIMENT_REF.*accession="([^\s"]+)"/i) {
+ print "\@RG\tID:$run\tSM:$sample\tLB:$exp2lib{$1}\n";
+ } elsif (/<FILE.*filename="([^\s"]+)"/i) {
+ push(@fn, $1);
+ } elsif (/<\/RUN>/i) {
+ if (@fn == 1) {
+ print STDERR "$fn[0]\t$run\n";
+ } else {
+ for (0 .. $#fn) {
+ print STDERR "$fn[$_]\t$run", "_", $_+1, "\n";
+ }