From 25f079f072d90fd2b58efc57cb241b4f1c783b39 Mon Sep 17 00:00:00 2001 From: Justin Fear Date: Wed, 23 Nov 2016 12:59:21 -0500 Subject: [PATCH] Adds function to run bigWigToBedGraph. Adds a function to run UCSC bigWigToBedGraph to allow further processing of BigWig files. --- pybedtools/contrib/bigwig.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pybedtools/contrib/bigwig.py b/pybedtools/contrib/bigwig.py index 907701a8..970a34a4 100644 --- a/pybedtools/contrib/bigwig.py +++ b/pybedtools/contrib/bigwig.py @@ -51,6 +51,28 @@ def bedgraph_to_bigwig(bedgraph, genome, output): os.system(' '.join(cmds)) return output +def bigwig_to_bedgraph(fn, chrom=None, start=None, end=None, udcDir=None): + cmds = [ + 'bigWigToBedGraph', + fn] + if chrom is not None: + cmds.extend(['-chrom', chrom]) + if start is not None: + cmds.extend(['-start', start]) + if end is not None: + cmds.extend(['-end', end]) + if udcDir is not None: + cmds.extend(['-udcDir', udcDir]) + + outfn = pybedtools.BedTool._tmp() + cmds.append(outfn) + + p = subprocess.Popen(cmds, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = p.communicate() + if p.returncode: + raise ValueError("cmds: %s\nstderr:%s\nstdout:%s" + % (" ".join(cmds), stderr, stdout)) + return pybedtools.BedTool(outfn) def wig_to_bigwig(wig, genome, output): genome_file = pybedtools.chromsizes_to_file(pybedtools.chromsizes(genome))