]> git.donarmstrong.com Git - samtools.git/blobdiff - misc/samtools.pl
* samtools-0.1.5-18 (r423)
[samtools.git] / misc / samtools.pl
index e27479c3eb5bd9cde695a566b8cb7709d687255b..ade52899ef23e06293d2ad0ff7e81e67d857937f 100755 (executable)
@@ -6,11 +6,11 @@ use strict;
 use warnings;
 use Getopt::Std;
 
-my $version = '0.3.0';
+my $version = '0.3.2 (r321)';
 &usage if (@ARGV < 1);
 
 my $command = shift(@ARGV);
-my %func = (showALEN=>\&showALEN, pileup2fq=>\&pileup2fq, varFilter=>\&varFilter);
+my %func = (showALEN=>\&showALEN, pileup2fq=>\&pileup2fq, varFilter=>\&varFilter, unique=>\&unique);
 
 die("Unknown command \"$command\".\n") if (!defined($func{$command}));
 &{$func{$command}};
@@ -24,6 +24,7 @@ sub showALEN {
   die(qq/Usage: samtools.pl showALEN <in.sam>\n/) if (@ARGV == 0 && -t STDIN);
   while (<>) {
        my @t = split;
+       next if (/^\@/ || @t < 11);
        my $l = 0;
        $_ = $t[5];
        s/(\d+)[SMI]/$l+=$1/eg;
@@ -36,12 +37,13 @@ sub showALEN {
 #
 
 sub varFilter {
-  my %opts = (d=>3, D=>100, l=>30, Q=>25, G=>20, s=>100, w=>10, W=>10, N=>2);
-  getopts('d:D:l:Q:w:W:N:G:', \%opts);
+  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 varFilter [options] <in.cns-pileup>
 
-Options: -Q INT    minimum RMS mapping quality [$opts{Q}]
+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}]
 
@@ -49,10 +51,12 @@ Options: -Q INT    minimum RMS mapping quality [$opts{Q}]
          -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{W}]
+         -N INT    max number of SNPs in a window [$opts{N}]
 
-         -l INT    window size for filtering adjacent gaps [$opts{l}]\n
-/) if (@ARGV == 0 && -t STDIN);
+         -l INT    window size for filtering adjacent gaps [$opts{l}]
+
+         -p        print filtered variants
+\n/) if (@ARGV == 0 && -t STDIN);
 
   # calculate the window size
   my ($ol, $ow, $oW) = ($opts{l}, $opts{w}, $opts{W});
@@ -65,17 +69,12 @@ Options: -Q INT    minimum RMS mapping quality [$opts{Q}]
        next if ($t[2] eq $t[3] || $t[3] eq '*/*'); # skip non-var sites
        # clear the out-of-range elements
        while (@staging) {
-         if ($staging[0][2] ne $t[0] || $staging[0][3] + $max_dist < $t[1]) {
-               varFilter_aux(shift @staging);
-         } else {
-               last;
-         }
+         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
        }
        my ($flt, $score) = (0, -1);
        # first a simple filter
-       if ($t[6] < $opts{Q}) {
-         $flt = 1;
-       } elsif ($t[7] < $opts{d}) {
+       if ($t[7] < $opts{d}) {
          $flt = 2;
        } elsif ($t[7] > $opts{D}) {
          $flt = 3;
@@ -83,6 +82,7 @@ Options: -Q INT    minimum RMS mapping quality [$opts{Q}]
        # 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) {
@@ -104,6 +104,7 @@ Options: -Q INT    minimum RMS mapping quality [$opts{Q}]
                  }
                }
          } else { # a SNP
+               $flt = 1 if ($t[6] < $opts{Q});
                # check adjacent SNPs
                my $k = 1;
                for my $x (@staging) {
@@ -129,15 +130,15 @@ Options: -Q INT    minimum RMS mapping quality [$opts{Q}]
   }
   # output the last few elements in the staging list
   while (@staging) {
-       varFilter_aux(shift @staging);
+       varFilter_aux(shift @staging, $opts{p});
   }
 }
 
 sub varFilter_aux {
-  my $first = shift;
+  my ($first, $is_print) = @_;
   if ($first->[1] == 0) {
        print join("\t", @$first[2 .. @$first-1]), "\n";
-  } else {
+  } elsif ($is_print) {
        print STDERR join("\t", substr("UQdDWGgX", $first->[1], 1), @$first[2 .. @$first-1]), "\n";
   }
 }
@@ -147,7 +148,7 @@ sub varFilter_aux {
 #
 
 sub pileup2fq {
-  my %opts = (d=>3, D=>255, Q=>25, G=>50, l=>10);
+  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>
@@ -214,6 +215,56 @@ sub p2q_print_str {
   }
 }
 
+#
+# 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");
+       next if (@t < 11);
+       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
 #