# HG changeset patch # User brad-chapman # Date 1307478406 14400 # Node ID 0ff100a057ef5ce778957fe94aacf98c877b3265 # Parent d2c1af6570100e913fdcbee067ed1846ebe0d2ec Migrated tool version 0.0.2 from old tool shed archive to new tool shed repository diff -r d2c1af657010 -r 0ff100a057ef bam_to_bigwig/README.txt --- 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 diff -r d2c1af657010 -r 0ff100a057ef bam_to_bigwig/bam_to_bigwig.xml --- 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 @@ - + Calculates coverage from a BAM alignment file - bam_to_wiggle.py $align --outfile=$out + bam_to_wiggle.py $align --outfile=$out diff -r d2c1af657010 -r 0ff100a057ef bam_to_bigwig/bam_to_wiggle.py --- 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):