Mention ‘SAMtools’ in libbam-dev's description, to make it easier to find with apt...
[samtools.git] / misc / samtools.pl
index 86b285c5fa6c144a1d60ed61cb48a23bde7f134f..d03c1c7f148dcca835946e7ccd9ee52f3b6dc00b 100755 (executable)
@@ -10,8 +10,8 @@ my $version = '0.3.3';
 &usage if (@ARGV < 1);
 
 my $command = shift(@ARGV);
-my %func = (showALEN=>\&showALEN, pileup2fq=>\&pileup2fq, varFilter=>\&varFilter,
-                       unique=>\&unique, uniqcmp=>\&uniqcmp);
+my %func = (showALEN=>\&showALEN, pileup2fq=>\&pileup2fq, varFilter=>\&varFilter, plp2vcf=>\&plp2vcf,
+                       unique=>\&unique, uniqcmp=>\&uniqcmp, sra2hdr=>\&sra2hdr, sam2fq=>\&sam2fq);
 
 die("Unknown command \"$command\".\n") if (!defined($func{$command}));
 &{$func{$command}};
@@ -37,9 +37,21 @@ sub showALEN {
 # varFilter
 #
 
+#
+# Filtration code:
+#
+# d low depth
+# D high depth
+# W too many SNPs in a window (SNP only)
+# G close to a high-quality indel (SNP only)
+# Q low RMS mapping quality (SNP only)
+# g close to another indel with higher quality (indel only)
+# s low SNP quality (SNP only)
+# i low indel quality (indel only)
+
 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('pq: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, S=>'', i=>'');
+  getopts('pq:d:D:l:Q:w:W:N:G:S:i:', \%opts);
   die(qq/
 Usage:   samtools.pl varFilter [options] <in.cns-pileup>
 
@@ -47,6 +59,8 @@ 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}]
+         -S INT    minimum SNP quality [$opts{S}]
+         -i INT    minimum indel quality [$opts{i}]
 
          -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}]
@@ -70,7 +84,8 @@ Options: -Q INT    minimum RMS mapping quality for SNPs [$opts{Q}]
        next if (uc($t[2]) eq uc($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]);
+      # Still on the same chromosome and the first element's window still affects this position?  
+         last if ($staging[0][3] eq $t[0] && $staging[0][4] + $staging[0][2] + $max_dist >= $t[1]);
          varFilter_aux(shift(@staging), $opts{p}); # calling a function is a bit slower, not much
        }
        my ($flt, $score) = (0, -1);
@@ -80,14 +95,31 @@ Options: -Q INT    minimum RMS mapping quality for SNPs [$opts{Q}]
        } elsif ($t[7] > $opts{D}) {
          $flt = 3;
        }
+    if ($t[2] eq '*') { # an indel
+        if ($opts{i} && $opts{i}>$t[5]) { $flt = 8; }
+    }
+    elsif ($opts{S} && $opts{S}>$t[5]) { $flt = 7; }    # SNP
+
        # site dependent filters
+    my $len=0;
        if ($flt == 0) {
          if ($t[2] eq '*') { # an indel
+        # If deletion, remember the length of the deletion
+        my ($a,$b) = split(m{/},$t[3]);
+        my $alen = length($a) - 1;
+        my $blen = length($b) - 1;
+        if ( $alen>$blen )
+        {
+            if ( substr($a,0,1) eq '-' ) { $len=$alen; }
+        }
+        elsif ( substr($b,0,1) eq '-' ) { $len=$blen; }
+
                $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]);
+            # Is it a SNP and is it outside the SNP filter window?
+                       next if ($x->[0] >= 0 || $x->[4] + $x->[2] + $ow < $t[1]);
                        $x->[1] = 5 if ($x->[1] == 0);
                  }
                }
@@ -97,7 +129,8 @@ Options: -Q INT    minimum RMS mapping quality for SNPs [$opts{Q}]
                $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]);
+          # Is it a SNP and is it outside the gap filter window
+                 next if ($x->[0] < 0 || $x->[4] + $x->[2] + $ol < $t[1]);
                  if ($x->[0] < $score) {
                        $x->[1] = 6;
                  } else {
@@ -109,17 +142,17 @@ Options: -Q INT    minimum RMS mapping quality for SNPs [$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));
+                 ++$k if ($x->[0] < 0 && $x->[4] + $x->[2] + $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);
+                        $x->[1] = 4 if ($x->[0] < 0 && $x->[4] + $x->[2] + $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]);
+                       next if ($x->[0] < 0 || $x->[4] + $x->[2] + $ow < $t[1]);
                        if ($x->[0] >= $opts{G}) {
                          $flt = 5; last;
                        }
@@ -127,7 +160,7 @@ Options: -Q INT    minimum RMS mapping quality for SNPs [$opts{Q}]
                }
          }
        }
-       push(@staging, [$score, $flt, @t]);
+       push(@staging, [$score, $flt, $len, @t]);
   }
   # output the last few elements in the staging list
   while (@staging) {
@@ -138,9 +171,9 @@ Options: -Q INT    minimum RMS mapping quality for SNPs [$opts{Q}]
 sub varFilter_aux {
   my ($first, $is_print) = @_;
   if ($first->[1] == 0) {
-       print join("\t", @$first[2 .. @$first-1]), "\n";
+       print join("\t", @$first[3 .. @$first-1]), "\n";
   } elsif ($is_print) {
-       print STDERR join("\t", substr("UQdDWGgX", $first->[1], 1), @$first[2 .. @$first-1]), "\n";
+       print STDERR join("\t", substr("UQdDWGgsiX", $first->[1], 1), @$first[3 .. @$first-1]), "\n";
   }
 }
 
@@ -216,16 +249,113 @@ sub p2q_print_str {
   }
 }
 
+#
+# sam2fq
+#
+
+sub sam2fq {
+  my %opts = (n=>20, p=>'');
+  getopts('n:p:', \%opts);
+  die("Usage: samtools.pl sam2fq [-n 20] [-p <prefix>] <inp.sam>\n") if (@ARGV == 0 && -t STDIN);
+  if ($opts{p} && $opts{n} > 1) {
+       my $pre = $opts{p};
+       my @fh;
+       for (0 .. $opts{n}-1) {
+         open($fh[$_], sprintf("| gzip > $pre.%.3d.fq.gz", $_)) || die;
+       }
+       my $i = 0;
+       while (<>) {
+         next if (/^@/);
+         chomp;
+         my @t = split("\t");
+         next if ($t[9] eq '*');
+         my ($name, $seq, $qual);
+         if ($t[1] & 16) { # reverse strand
+               $seq = reverse($t[9]);
+               $qual = reverse($t[10]);
+               $seq =~ tr/ACGTacgt/TGCAtgca/;
+         } else {
+               ($seq, $qual) = @t[9,10];
+         }
+         $name = $t[0];
+         $name .= "/1" if ($t[1] & 0x40);
+         $name .= "/2" if ($t[1] & 0x80);
+         print {$fh[$i]} "\@$name\n$seq\n";
+         if ($qual ne '*') {
+               print {$fh[$i]} "+\n$qual\n";
+         }
+         $i = 0 if (++$i == $opts{n});
+       }
+       close($fh[$_]) for (0 .. $opts{n}-1);
+  } else {
+       die("To be implemented.\n");
+  }
+}
+
+#
+# sra2hdr
+#
+
+# This subroutine does not use an XML parser. It requires that the SRA
+# XML files are properly formated.
+sub sra2hdr {
+  my %opts = ();
+  getopts('', \%opts);
+  die("Usage: samtools.pl sra2hdr <SRA.prefix>\n") if (@ARGV == 0);
+  my $pre = $ARGV[0];
+  my $fh;
+  # read sample
+  my $sample = 'UNKNOWN';
+  open($fh, "$pre.sample.xml") || die;
+  while (<$fh>) {
+       $sample = $1 if (/<SAMPLE.*alias="([^"]+)"/i);
+  }
+  close($fh);
+  # read experiment
+  my (%exp2lib, $exp);
+  open($fh, "$pre.experiment.xml") || die;
+  while (<$fh>) {
+       if (/<EXPERIMENT.*accession="([^\s"]+)"/i) {
+         $exp = $1;
+       } elsif (/<LIBRARY_NAME>\s*(\S+)\s*<\/LIBRARY_NAME>/i) {
+         $exp2lib{$exp} = $1;
+       }
+  }
+  close($fh);
+  # read run
+  my ($run, @fn);
+  open($fh, "$pre.run.xml") || die;
+  while (<$fh>) {
+       if (/<RUN.*accession="([^\s"]+)"/i) {
+         $run = $1; @fn = ();
+       } elsif (/<EXPERIMENT_REF.*accession="([^\s"]+)"/i) {
+         print "\@RG\tID:$run\tSM:$sample\tLB:$exp2lib{$1}\n";
+       } elsif (/<FILE.*filename="([^\s"]+)"/i) {
+         push(@fn, $1);
+       } elsif (/<\/RUN>/i) {
+         if (@fn == 1) {
+               print STDERR "$fn[0]\t$run\n";
+         } else {
+               for (0 .. $#fn) {
+                 print STDERR "$fn[$_]\t$run", "_", $_+1, "\n";
+               }
+         }
+       }
+  }
+  close($fh);
+}
+
 #
 # unique
 #
 
 sub unique {
   my %opts = (f=>250.0, q=>5, r=>2, a=>1, b=>3);
-  getopts('Qf:q:r:a:b:', \%opts);
+  getopts('Qf:q:r:a:b:m', \%opts);
   die("Usage: samtools.pl unique [-f $opts{f}] <in.sam>\n") if (@ARGV == 0 && -t STDIN);
   my $last = '';
   my $recal_Q = !defined($opts{Q});
+  my $multi_only = defined($opts{m});
   my @a;
   while (<>) {
        my $score = -1;
@@ -243,16 +373,16 @@ sub unique {
        }
        $score = 1 if ($score < 1);
        if ($t[0] ne $last) {
-         &unique_aux(\@a, $opts{f}, $recal_Q) if (@a);
+         &unique_aux(\@a, $opts{f}, $recal_Q, $multi_only) if (@a);
          $last = $t[0];
        }
        push(@a, [$score, \@t]);
   }
-  &unique_aux(\@a, $opts{f}, $recal_Q) if (@a);
+  &unique_aux(\@a, $opts{f}, $recal_Q, $multi_only) if (@a);
 }
 
 sub unique_aux {
-  my ($a, $fac, $is_recal) = @_;
+  my ($a, $fac, $is_recal, $multi_only) = @_;
   my ($max, $max2, $max_i) = (0, 0, -1);
   for (my $i = 0; $i < @$a; ++$i) {
        if ($a->[$i][0] > $max) {
@@ -262,9 +392,11 @@ sub unique_aux {
        }
   }
   if ($is_recal) {
-       my $q = int($fac * ($max - $max2) / $max + .499);
-       $q = 250 if ($q > 250);
-       $a->[$max_i][1][4] = $q < 250? $q : 250;
+       if (!$multi_only || @$a > 1) {
+         my $q = int($fac * ($max - $max2) / $max + .499);
+         $q = 250 if ($q > 250);
+         $a->[$max_i][1][4] = $q < 250? $q : 250;
+       }
   }
   print join("\t", @{$a->[$max_i][1]});
   @$a = ();
@@ -341,6 +473,44 @@ sub uniqcmp_aux {
   close($fh);
 }
 
+sub plp2vcf {
+  while (<>) {
+       my @t = split;
+       next if ($t[3] eq '*/*');
+       if ($t[2] eq '*') { # indel
+         my @s = split("/", $t[3]);
+         my (@a, @b);
+         my ($ref, $alt);
+         for (@s) {
+               next if ($_ eq '*');
+               if (/^-/) {
+                 push(@a, 'N'.substr($_, 1));
+                 push(@b, 'N');
+               } elsif (/^\+/) {
+                 push(@a, 'N');
+                 push(@b, 'N'.substr($_, 1));
+               }
+         }
+         if ($a[0] && $a[1]) {
+               if (length($a[0]) < length($a[1])) {
+                 $ref = $a[1];
+                 $alt = ($b[0] . ('N' x (length($a[1]) - length($a[0])))) . ",$b[1]";
+               } elsif (length($a[0]) > length($a[1])) {
+                 $ref = $a[0];
+                 $alt = ($b[1] . ('N' x (length($a[0]) - length($a[1])))) . ",$b[0]";
+               } else {
+                 $ref = $a[0];
+                 $alt = ($b[0] eq $b[1])? $b[0] : "$b[0],$b[1]";
+               }
+         } else {
+               $ref = $a[0]; $alt = $b[0];
+         }
+         print join("\t", @t[0,1], '.', $ref, $alt, $t[5], '.', '.'), "\n";
+       } else { # SNP
+       }
+  }
+}
+
 #
 # Usage
 #