]> git.donarmstrong.com Git - samtools.git/blobdiff - misc/samtools.pl
* samtools-0.1.5-15 (r419)
[samtools.git] / misc / samtools.pl
index 9bbce6f79bdd2ceafba1eecd7ec0db7301a0baf9..0b0e9d2d378e4a5965c2ecea9a4c570fa6983112 100755 (executable)
@@ -6,11 +6,11 @@ use strict;
 use warnings;
 use Getopt::Std;
 
-my $version = '0.3.1';
+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}};
@@ -36,12 +36,13 @@ sub showALEN {
 #
 
 sub varFilter {
-  my %opts = (d=>3, D=>100, l=>30, Q=>25, G=>25, s=>100, w=>10, W=>10, N=>2, p=>undef);
+  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}]
 
@@ -72,9 +73,7 @@ Options: -Q INT    minimum RMS mapping quality [$opts{Q}]
        }
        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;
@@ -82,6 +81,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) {
@@ -103,6 +103,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) {
@@ -213,6 +214,55 @@ 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");
+       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
 #