+ push(@staging, [$score, $flt, @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[2 .. @$first-1]), "\n";
+ } elsif ($is_print) {
+ print STDERR join("\t", substr("UQdDWGgX", $first->[1], 1), @$first[2 .. @$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 = ();