]> git.donarmstrong.com Git - samtools.git/blobdiff - misc/samtools.pl
* samtools-0.1.7-2 (r520)
[samtools.git] / misc / samtools.pl
index c175539f3a933c1abc24c1129038c80f28c56394..1d9cf59b2ebd84a6ccc5c3bbc8eb48718c555d90 100755 (executable)
@@ -6,12 +6,12 @@ use strict;
 use warnings;
 use Getopt::Std;
 
-my $version = '0.3.2 (r321)';
+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);
+                       unique=>\&unique, uniqcmp=>\&uniqcmp, sra2hdr=>\&sra2hdr, sam2fq=>\&sam2fq);
 
 die("Unknown command \"$command\".\n") if (!defined($func{$command}));
 &{$func{$command}};
@@ -37,9 +37,19 @@ 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)
+
 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);
+  getopts('pq:d:D:l:Q:w:W:N:G:', \%opts);
   die(qq/
 Usage:   samtools.pl varFilter [options] <in.cns-pileup>
 
@@ -67,7 +77,7 @@ Options: -Q INT    minimum RMS mapping quality for SNPs [$opts{Q}]
   my @staging; # (indel_filtering_score, flt_tag)
   while (<>) {
        my @t = split;
-       next if ($t[2] eq $t[3] || $t[3] eq '*/*'); # skip non-var sites
+       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]);
@@ -216,6 +226,102 @@ 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
 #
@@ -302,7 +408,11 @@ sub uniqcmp {
                $p->[0][5]-$p->[1][5], "\n" if ($z && defined($opts{p}) && ($p->[0][3] >= $opts{q} || $p->[1][3] >= $opts{q}));
        } elsif (defined($p->[0])) {
          ++$cnt[$p->[0][3]>=$opts{q}? 6 : 7];
+         print STDERR "$x\t$p->[0][1]:$p->[0][2]\t$p->[0][3]\t$p->[0][4]\t*\t0\t*\t",
+               $p->[0][5], "\n" if (defined($opts{p}) && $p->[0][3] >= $opts{q});
        } else {
+         print STDERR "$x\t*\t0\t*\t$p->[1][1]:$p->[1][2]\t$p->[1][3]\t$p->[1][4]\t",
+               -$p->[1][5], "\n" if (defined($opts{p}) && $p->[1][3] >= $opts{q});
          ++$cnt[$p->[1][3]>=$opts{q}? 8 : 9];
        }
   }
@@ -326,7 +436,8 @@ sub uniqcmp_aux {
   while (<$fh>) {
        my @t = split;
        next if (@t < 11);
-       my $l = ($t[5] =~ /^(\d+)S/)? $1 : 0;
+#      my $l = ($t[5] =~ /^(\d+)S/)? $1 : 0;
+       my $l = 0;
        my ($x, $nm) = (0, 0);
        $nm = $1 if (/NM:i:(\d+)/);
        $_ = $t[5];