]> git.donarmstrong.com Git - samtools.git/blobdiff - misc/samtools.pl
* samtools-0.1.5-15 (r419)
[samtools.git] / misc / samtools.pl
index a26ac10f47ad76cc273966b6f5ec1e05889a715b..0b0e9d2d378e4a5965c2ecea9a4c570fa6983112 100755 (executable)
@@ -6,11 +6,11 @@ use strict;
 use warnings;
 use Getopt::Std;
 
-my $version = '0.2.1';
+my $version = '0.3.2 (r321)';
 &usage if (@ARGV < 1);
 
 my $command = shift(@ARGV);
-my %func = (snpFilter=>\&snpFilter, indelFilter=>\&indelFilter, showALEN=>\&showALEN);
+my %func = (showALEN=>\&showALEN, pileup2fq=>\&pileup2fq, varFilter=>\&varFilter, unique=>\&unique);
 
 die("Unknown command \"$command\".\n") if (!defined($func{$command}));
 &{$func{$command}};
@@ -32,118 +32,249 @@ sub showALEN {
 }
 
 #
-# indelFilter
+# varFilter
 #
 
-sub indelFilter {
-  my %opts = (D=>100, m=>10, r=>undef, s=>100);
-  getopts('D:m:rs:', \%opts); # -s for scaling factor in score calculation
-
+sub varFilter {
+  my %opts = (d=>3, D=>100, l=>30, Q=>25, q=>10, G=>25, s=>100, w=>10, W=>10, N=>2, p=>undef);
+  getopts('pd:D:l:Q:w:W:N:G:', \%opts);
   die(qq/
-Usage:   samtools.pl indelFilter [options] <in.indel>\n
-Options: -D INT    maximum read depth [$opts{D}]
-         -m INT    minimum distance between two adjacent indels [$opts{m}]
+Usage:   samtools.pl varFilter [options] <in.cns-pileup>
+
+Options: -Q INT    minimum RMS mapping quality for SNPs [$opts{Q}]
+         -q INT    minimum RMS mapping quality for gaps [$opts{q}]
+         -d INT    minimum read depth [$opts{d}]
+         -D INT    maximum read depth [$opts{D}]
+
+         -G INT    min indel score for nearby SNP filtering [$opts{G}]
+         -w INT    SNP within INT bp around a gap to be filtered [$opts{w}]
+
+         -W INT    window size for filtering dense SNPs [$opts{W}]
+         -N INT    max number of SNPs in a window [$opts{N}]
+
+         -l INT    window size for filtering adjacent gaps [$opts{l}]
+
+         -p        print filtered variants
 \n/) if (@ARGV == 0 && -t STDIN);
 
-  my (@arr1, @arr2);
-  my ($curr, $last) = (\@arr1, \@arr2);
-  my $is_ref = defined($opts{r})? 1 : 0;
+  # calculate the window size
+  my ($ol, $ow, $oW) = ($opts{l}, $opts{w}, $opts{W});
+  my $max_dist = $ol > $ow? $ol : $ow;
+  $max_dist = $oW if ($max_dist < $oW);
+  # the core loop
+  my @staging; # (indel_filtering_score, flt_tag)
   while (<>) {
        my @t = split;
-       next if ($t[2] ne '*');
-       if (!$is_ref) {
-         next if ($t[3] eq '*/*');
-         next if ($t[5] == 0);
+       next if ($t[2] eq $t[3] || $t[3] eq '*/*'); # skip non-var sites
+       # clear the out-of-range elements
+       while (@staging) {
+         last if ($staging[0][2] eq $t[0] && $staging[0][3] + $max_dist >= $t[1]);
+         varFilter_aux(shift(@staging), $opts{p}); # calling a function is a bit slower, not much
        }
-       next if ($t[7] > $opts{D});
-       # calculate indel score
-       my $score = $t[5];
-       $score += $opts{s} * $t[10] if ($t[8] ne '*');
-       $score += $opts{s} * $t[11] if ($t[9] ne '*');
-       @$curr = ($t[0], $t[1], $score, $_);
-       my $do_swap = 1;
-       if (defined $last->[0]) {
-         if ($curr->[0] eq $last->[0] && $last->[1] + $opts{m} > $curr->[1]) {
-               $do_swap = 0 if ($last->[2] > $curr->[2]);
-         } else { # then print
-               print $last->[3];
-         }
+       my ($flt, $score) = (0, -1);
+       # first a simple filter
+       if ($t[7] < $opts{d}) {
+         $flt = 2;
+       } elsif ($t[7] > $opts{D}) {
+         $flt = 3;
        }
-       if ($do_swap) {
-         my $tmp = $curr; $curr = $last; $last = $tmp;
+       # site dependent filters
+       if ($flt == 0) {
+         if ($t[2] eq '*') { # an indel
+               $flt = 1 if ($t[6] < $opts{q});
+               # filtering SNPs
+               if ($t[5] >= $opts{G}) {
+                 for my $x (@staging) {
+                       next if ($x->[0] >= 0 || $x->[3] + $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) {
+                 next if ($x->[0] < 0 || $x->[3] + $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->[3] + $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->[3] + $oW >= $t[1] && $x->[1] == 0);
+                 }
+               } else { # then check gap filter
+                 for my $x (@staging) {
+                       next if ($x->[0] < 0 || $x->[3] + $ow < $t[1]);
+                       if ($x->[0] >= $opts{G}) {
+                         $flt = 5; last;
+                       }
+                 }
+               }
+         }
        }
+       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";
   }
-  print $last->[3] if (defined $last->[0]);
 }
 
 #
-# snpFilter
+# pileup2fq
 #
 
-sub snpFilter {
-  my %opts = (f=>'', Q=>40, d=>3, w=>10, D=>0, N=>2, W=>10, q=>20, s=>50);
-  getopts('f:s:w:q:Q:d:D:W:N:', \%opts);
-  die(qq{
-Usage:   samtools.pl snpFilter [options] <cns2snp.snp>
-
-Options: -d INT        minimum depth to call a SNP [$opts{d}]
-         -D INT        maximum depth, 0 to ignore [$opts{D}]
-         -Q INT        required max mapping quality of the reads covering the SNP [$opts{Q}]
-         -q INT        minimum SNP quality [$opts{q}]
-
-         -f FILE       filtered samtools indels [null]
-         -s INT        minimum samtols indel score [$opts{s}]
-         -w INT        SNP within INT bp around an indel to be filtered [$opts{w}]
-
-         -W INT        window size for filtering dense SNPs [$opts{W}]
-         -N INT        maximum number of SNPs in a window [$opts{N}]
-\n}) unless (@ARGV);
-  my (%hash, $fh);
-  my $skip = $opts{w};
-  $opts{D} = 100000000 if ($opts{D} == 0);
-  if ($opts{f}) { # filtered samtools indel
-       my $n = 0;
-       open($fh, $opts{f}) || die;
-       while (<$fh>) {
-         my @t = split;
-         next if ($t[2] ne '*' || $t[3] eq '*/*' || $t[5] < $opts{s});
-         for (my $x = $t[1] - $skip + 1; $x < $t[1] + $skip; ++$x) {
-               $hash{$t[0],$x} = 1;
-         }
-       }
-       close($fh);
-  }
-  my (@last, $last_chr);
+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;
-       next if ($t[2] eq '*' || $hash{$t[0],$t[1]});
-       my $is_good = ($t[7] >= $opts{d} && $t[7] <= $opts{D} && $t[6] >= $opts{Q} && $t[5] >= $opts{q})? 1 : 0;
-       next unless ($is_good); # drop
-       if ($t[0] ne $last_chr) { # a different chr, print
-         map { print $_->{L} if ($_->{F}) } @last;
-         @last = ();
+       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 = ();
        }
-       # The following block implemented by Nathans Weeks.
-       push(@last, {L => $_, X => $t[1], F => 1}); # Enqueue current SNP
-       if ($#last == $opts{N}) {                   # number of SNPs in queue is N+1
-         if ($last[$#last]{X} - $last[0]{X} < $opts{W}) { # if all within window W
-               map {$_->{F} = 0} @last; # all SNPs in the window of size W are "bad"
-         }
-         print STDOUT $last[0]{L} if ($last[0]{F}); # print first SNP if good
-         shift @last # dequeue first SNP
+       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];
   }
-  # print the last few lines if applicable
-  map { print $_->{L} if ($_->{F}) } @last;
+  &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";
+  }
+}
+
+#
+# unique
+#
+
+sub unique {
+  my %opts = (f=>5.0, q=>5, r=>2, a=>1, b=>3);
+  getopts('f:', \%opts);
+  die("Usage: samtools.pl unique [-f $opts{f}] <in.sam>\n") if (@ARGV == 0 && -t STDIN);
+  my $last = '';
+  my @a;
+  while (<>) {
+       my $score = -1;
+       print $_ if (/^\@/);
+       $score = $1 if (/AS:i:(\d+)/);
+       my @t = split("\t");
+       if ($score < 0) { # AS tag is unavailable
+         my $cigar = $t[5];
+         my ($mm, $go, $ge) = (0, 0, 0);
+         $cigar =~ s/(\d+)[ID]/++$go,$ge+=$1/eg;
+         $cigar = $t[5];
+         $cigar =~ s/(\d+)M/$mm+=$1/eg;
+         $score = $mm * $opts{a} - $go * $opts{q} - $ge * $opts{r}; # no mismatches...
+       }
+       $score = 0 if ($score < 0);
+       if ($t[0] ne $last) {
+         &unique_aux(\@a, $opts{f}) if (@a);
+         $last = $t[0];
+       }
+       push(@a, [$score, \@t]);
+  }
+  &unique_aux(\@a, $opts{f}) if (@a);
+}
+
+sub unique_aux {
+  my ($a, $fac) = @_;
+  my ($max, $max2, $max_i) = (-1, -1, -1);
+  for (my $i = 0; $i < @$a; ++$i) {
+       if ($a->[$i][0] > $max) {
+         $max2 = $max; $max = $a->[$i][0]; $max_i = $i;
+       } elsif ($a->[$i][0] > $max2) {
+         $max2 = $a->[$i][0];
+       }
+  }
+  my $q = int($fac * ($max - $max2) + .499);
+  $a->[$max_i][1][4] = $q < 250? $q : 250;
+  print join("\t", @{$a->[$max_i][1]});
+  @$a = ();
+}
+
+#
+# Usage
+#
+
 sub usage {
   die(qq/
+Program: samtools.pl (helper script for SAMtools)
+Version: $version
+Contact: Heng Li <lh3\@sanger.ac.uk>\n
 Usage:   samtools.pl <command> [<arguments>]\n
-Command: indelFilter   filter indels generated by `pileup -c'
-         snpFilter     filter SNPs generated by `pileup -c'
+Command: varFilter     filtering SNPs and short indels
+         pileup2fq     generate fastq from `pileup -c'
          showALEN      print alignment length (ALEN) following CIGAR
 \n/);
 }