Imported Upstream version 0.1.7~dfsg
[samtools.git] / misc / samtools.pl
index 86b285c5fa6c144a1d60ed61cb48a23bde7f134f..320e8aaeda8ff2fab7fd91ab5e02073d5f3ba8fe 100755 (executable)
@@ -11,7 +11,7 @@ my $version = '0.3.3';
 
 my $command = shift(@ARGV);
 my %func = (showALEN=>\&showALEN, pileup2fq=>\&pileup2fq, varFilter=>\&varFilter,
-                       unique=>\&unique, uniqcmp=>\&uniqcmp);
+                       unique=>\&unique, uniqcmp=>\&uniqcmp, sra2hdr=>\&sra2hdr);
 
 die("Unknown command \"$command\".\n") if (!defined($func{$command}));
 &{$func{$command}};
@@ -37,6 +37,16 @@ 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('pq:d:D:l:Q:w:W:N:G:', \%opts);
@@ -216,6 +226,59 @@ sub p2q_print_str {
   }
 }
 
+#
+# 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
 #