changeset 1:0ff100a057ef

Migrated tool version 0.0.2 from old tool shed archive to new tool shed repository
author brad-chapman
date Tue, 07 Jun 2011 16:26:46 -0400
parents d2c1af657010
children e2edfa478eb4
files bam_to_bigwig/README.txt bam_to_bigwig/bam_to_bigwig.xml bam_to_bigwig/bam_to_wiggle.py
diffstat 3 files changed, 21 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/bam_to_bigwig/README.txt	Tue Jun 07 16:25:46 2011 -0400
+++ b/bam_to_bigwig/README.txt	Tue Jun 07 16:26:46 2011 -0400
@@ -8,6 +8,9 @@
 
 https://github.com/chapmanb/bcbb/blob/master/nextgen/scripts/bam_to_wiggle.py
 
+Place the script in the same directory as the XML configuration file, or
+provide a soft link to it.
+
 This requires:
 
 Python2, version 2.6 or better
--- a/bam_to_bigwig/bam_to_bigwig.xml	Tue Jun 07 16:25:46 2011 -0400
+++ b/bam_to_bigwig/bam_to_bigwig.xml	Tue Jun 07 16:26:46 2011 -0400
@@ -1,6 +1,6 @@
-<tool id="bam_to_bigwig" name="BAM to BigWig" version="0.0.1">
+<tool id="bam_to_bigwig" name="BAM to BigWig" version="0.0.2">
   <description>Calculates coverage from a BAM alignment file</description>
-  <command>bam_to_wiggle.py $align --outfile=$out</command>
+  <command interpreter="python">bam_to_wiggle.py $align --outfile=$out</command>
   <inputs>
     <param format="bam" name="align" type="data" label="BAM alignment file"/>
   </inputs>
--- a/bam_to_bigwig/bam_to_wiggle.py	Tue Jun 07 16:25:46 2011 -0400
+++ b/bam_to_bigwig/bam_to_wiggle.py	Tue Jun 07 16:26:46 2011 -0400
@@ -21,19 +21,21 @@
 The script requires:
     pysam (http://code.google.com/p/pysam/)
     wigToBigWig from UCSC (http://hgdownload.cse.ucsc.edu/admin/exe/)
+If a configuration file is used, then PyYAML is also required (http://pyyaml.org/)
 """
 import os
 import sys
 import subprocess
+import tempfile
 from optparse import OptionParser
-from contextlib import contextmanager
+from contextlib import contextmanager, closing
 
-import yaml
 import pysam
 
 def main(bam_file, config_file=None, chrom='all', start=0, end=None,
          outfile=None):
     if config_file:
+        import yaml
         with open(config_file) as in_handle:
             config = yaml.load(in_handle)
     else:
@@ -45,9 +47,14 @@
     if end is not None:
         end = int(end)
     regions = [(chrom, start, end)]
-    if not os.path.exists(outfile):
-        wig_file = "%s.wig" % os.path.splitext(bam_file)[0]
-        with open(wig_file, "w") as out_handle:
+    if os.path.abspath(bam_file) == os.path.abspath(outfile):
+        sys.stderr.write("Bad arguments, input and output files are the same.\n")
+        sys.exit(1)
+    if not (os.path.exists(outfile) and os.path.getsize(outfile) > 0):
+        #Use a temp file to avoid any possiblity of not having write permission
+        out_handle = tempfile.NamedTemporaryFile(delete=False)
+        wig_file = out_handle.name
+        with closing(out_handle):
             chr_sizes, wig_valid = write_bam_track(bam_file, regions, config, out_handle)
         try:
             if wig_valid:
@@ -68,21 +75,14 @@
         "name=%s" % os.path.splitext(os.path.split(bam_file)[-1])[0],
         "visibility=full",
         ]))
-    sizes = []
     is_valid = False
     with indexed_bam(bam_file, config) as work_bam:
-        for ref_info in work_bam.header.get("SQ", []):
-            sizes.append((ref_info["SN"], ref_info["LN"]))
+        sizes = zip(work_bam.references, work_bam.lengths)
         if len(regions) == 1 and regions[0][0] == "all":
-            regions = []
-            for ref_info in work_bam.header.get("SQ", []):
-                regions.append((ref_info["SN"], 0, None))
+            regions = [(name, 0, length) for name, length in sizes]
         for chrom, start, end in regions:
-            if end is None:
-                for ref_info in work_bam.header.get("SQ", []):
-                    if ref_info["SN"] == chrom:
-                        end = int(ref_info["LN"])
-                        break
+            if end is None and chrom in work_bam.references:
+                end = work_bam.lengths[work_bam.references.index(chrom)]
             assert end is not None, "Could not find %s in header" % chrom
             out_handle.write("variableStep chrom=%s\n" % chrom)
             for col in work_bam.pileup(chrom, start, end):