]> git.donarmstrong.com Git - samtools.git/commitdiff
* samtools-0.1.5-15 (r419)
authorHeng Li <lh3@live.co.uk>
Tue, 28 Jul 2009 08:52:33 +0000 (08:52 +0000)
committerHeng Li <lh3@live.co.uk>
Tue, 28 Jul 2009 08:52:33 +0000 (08:52 +0000)
 * in sam_open(), return NULL when the file cannot be opened.
 * make wgsim_eval.pl more robust to imperfect SAM
 * add "unique" command to samtools.pl

bam_import.c
bamtk.c
misc/samtools.pl
misc/wgsim_eval.pl

index 81d3720870d418f3b9bab901e2876506aa6d8828..a49f9e83a248f85100341c9f0c6b124b971c2e7a 100644 (file)
@@ -487,9 +487,11 @@ int sam_read1(tamFile fp, bam_header_t *header, bam1_t *b)
 tamFile sam_open(const char *fn)
 {
        tamFile fp;
+       gzFile gzfp = (strcmp(fn, "-") == 0)? gzdopen(fileno(stdin), "r") : gzopen(fn, "r");
+       if (gzfp == 0) return 0;
        fp = (tamFile)calloc(1, sizeof(struct __tamFile_t));
        fp->str = (kstring_t*)calloc(1, sizeof(kstring_t));
-       fp->fp = (strcmp(fn, "-") == 0)? gzdopen(fileno(stdin), "r") : gzopen(fn, "r");
+       fp->fp = gzfp;
        fp->ks = ks_init(fp->fp);
        return fp;
 }
diff --git a/bamtk.c b/bamtk.c
index 33e97d879916558fe1d232490c636d29b24d744a..daae6ab2fd967dd1e4e0303d82b4e0e4fb299c4f 100644 (file)
--- a/bamtk.c
+++ b/bamtk.c
@@ -4,7 +4,7 @@
 #include "bam.h"
 
 #ifndef PACKAGE_VERSION
-#define PACKAGE_VERSION "0.1.5-14 (r417)"
+#define PACKAGE_VERSION "0.1.5-15 (r419)"
 #endif
 
 int bam_taf2baf(int argc, char *argv[]);
index c014c52bcf0880d39ba23f567a31396017220142..0b0e9d2d378e4a5965c2ecea9a4c570fa6983112 100755 (executable)
@@ -10,7 +10,7 @@ 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}};
@@ -215,27 +215,52 @@ sub p2q_print_str {
 }
 
 #
-# varStats
+# unique
 #
 
-sub varStats {
-  my %opts = (d=>'', c=>5);
-  getopts('d:c:', \%opts);
-  die("Usage: samtools.pl varStats [-d dbSNP.snp] [-c $opts{c}] <in.plp.snp>\n") if (@ARGV == 0 && -t STDIN);
-  my (@cnt, %hash);
-  my $col = $opts{c} - 1;
+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 @t = split;
-       if ($t[2] eq '*') {
-       } else {
-         my $q = $t[$col];
-         $q = 99 if ($q > 99);
-         $q = int($q/10);
-         my $is_het = ($t[3] =~ /^[ACGT]$/)? 0 : 1;
-         ++$cnt[$q][$is_het];
-         $hash{$t[0],$t[1]} = $q;
+       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 = ();
 }
 
 #
index 5339fd4406571b1bd67c8c97918af91c5b8b3ff2..412243f9a5485a1962473fe4ce4dd3559a4e3597 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/perl -w
 
 # Contact: lh3
-# Version: 0.1.4
+# Version: 0.1.5
 
 use strict;
 use warnings;
@@ -11,17 +11,18 @@ use Getopt::Std;
 exit;
 
 sub wgsim_eval {
-  my %opts;
-  getopts('pc', \%opts);
-  die("Usage: wgsim_eval.pl [-pc] <in.sam>\n") if (@ARGV == 0 && -t STDIN);
+  my %opts = (g=>5);
+  getopts('pcg:', \%opts);
+  die("Usage: wgsim_eval.pl [-pc] [-g $opts{g}] <in.sam>\n") if (@ARGV == 0 && -t STDIN);
   my (@c0, @c1);
   my ($max_q, $flag) = (0, 0);
-  my $gap = 5;
+  my $gap = $opts{g};
   $flag |= 1 if (defined $opts{p});
   $flag |= 2 if (defined $opts{c});
   while (<>) {
        next if (/^\@/);
-       my @t = split;
+       my @t = split("\t");
+       next if (@t < 11);
        my $line = $_;
        my ($q, $is_correct, $chr, $left, $rght) = (int($t[4]/10), 1, $t[2], $t[3], $t[3]);
        $max_q = $q if ($q > $max_q);
@@ -29,8 +30,8 @@ sub wgsim_eval {
        $_ = $t[5]; s/(\d+)[MDN]/$rght+=$1,'x'/eg;
        --$rght;
        # correct for soft clipping
-       $left -= $1 if (/^(\d+)S/);
-       $rght += $1 if (/(\d+)S$/);
+       $left -= $1 if (/^(\d+)[SH]/);
+       $rght += $1 if (/(\d+)[SH]$/);
        # skip unmapped reads
        next if (($t[1]&0x4) || $chr eq '*');
        # parse read name and check