Mercurial > repos > brad-chapman > bam_to_bigwig
annotate bam_to_bigwig.py @ 6:9163e1db4c16 draft default tip
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
| author | lparsons |
|---|---|
| date | Thu, 12 May 2016 11:39:13 -0400 |
| parents | |
| children |
| rev | line source |
|---|---|
|
6
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
1 #!/usr/bin/env python |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
2 """Convert BAM files to BigWig file format in a specified region. |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
3 |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
4 Original version copyright Brad Chapman with revisions from Peter Cock |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
5 and ideas from Lance Parsons |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
6 |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
7 Usage: |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
8 bam_to_bigwig.py <BAM file> [--outfile=<output file name>] [--split] |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
9 |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
10 The --split argument is passed to bedtools genomecov |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
11 |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
12 The script requires: |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
13 pysam (http://code.google.com/p/pysam/) |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
14 bedtools genomecov (http://code.google.com/p/bedtools/) |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
15 bedGraphToBigWig from UCSC (http://hgdownload.cse.ucsc.edu/admin/exe/) |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
16 """ |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
17 import os |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
18 import sys |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
19 import subprocess |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
20 import tempfile |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
21 from optparse import OptionParser |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
22 from contextlib import contextmanager, closing |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
23 |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
24 import pysam |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
25 |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
26 |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
27 def main(bam_file, outfile=None, split=False): |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
28 config = {"program": {"ucsc_bedGraphToBigWig": ["bedGraphToBigWig"], |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
29 "bedtools_genomeCoverageBed": |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
30 ["bedtools", "genomecov"]}} |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
31 if outfile is None: |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
32 outfile = "%s.bigwig" % os.path.splitext(bam_file)[0] |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
33 if os.path.abspath(bam_file) == os.path.abspath(outfile): |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
34 sys.stderr.write("Bad arguments, " |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
35 "input and output files are the same.\n") |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
36 sys.exit(1) |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
37 if os.path.exists(outfile) and os.path.getsize(outfile) > 0: |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
38 sys.stderr.write("Warning, output file already exists.\n") |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
39 |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
40 sizes = get_sizes(bam_file, config) |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
41 print "Have %i references" % len(sizes) |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
42 if not sizes: |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
43 sys.stderr.write("Problem reading BAM header.\n") |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
44 sys.exit(1) |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
45 |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
46 # Use a temp file to avoid any possiblity of not having write permission |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
47 temp_handle = tempfile.NamedTemporaryFile(delete=False) |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
48 temp_file = temp_handle.name |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
49 with closing(temp_handle): |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
50 print "Calculating coverage..." |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
51 convert_to_graph(bam_file, split, config, temp_handle) |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
52 try: |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
53 print("Converting %i MB graph file to bigwig..." % |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
54 (os.path.getsize(temp_file) // (1024 * 1024))) |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
55 # Can't pipe this as stdin due to converter design, |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
56 # https://lists.soe.ucsc.edu/pipermail/genome/2011-March/025455.html |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
57 convert_to_bigwig(temp_file, sizes, config, outfile) |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
58 finally: |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
59 if os.path.isfile(temp_file): |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
60 os.remove(temp_file) |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
61 print "Done" |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
62 |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
63 |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
64 @contextmanager |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
65 def indexed_bam(bam_file, config): |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
66 if not os.path.exists(bam_file + ".bai"): |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
67 pysam.index(bam_file) |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
68 sam_reader = pysam.Samfile(bam_file, "rb") |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
69 yield sam_reader |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
70 sam_reader.close() |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
71 |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
72 |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
73 def get_sizes(bam_file, config): |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
74 with indexed_bam(bam_file, config) as work_bam: |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
75 sizes = zip(work_bam.references, work_bam.lengths) |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
76 return sizes |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
77 |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
78 |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
79 def convert_to_graph(bam_file, split, config, out_handle): |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
80 cl = config["program"]["bedtools_genomeCoverageBed"] + \ |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
81 ["-ibam", bam_file, "-bg"] |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
82 if split: |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
83 cl.append("-split") |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
84 new_env = os.environ.copy() |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
85 new_env['LC_COLLATE'] = 'C' |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
86 p1 = subprocess.Popen(cl, stdout=subprocess.PIPE) |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
87 p2 = subprocess.Popen(["sort", "-k1,1", "-k2,2n"], |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
88 env=new_env, |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
89 stdin=p1.stdout, |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
90 stdout=out_handle) |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
91 p1.stdout.close() |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
92 p2.communicate() |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
93 |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
94 |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
95 def convert_to_bigwig(bedgraph_file, chr_sizes, config, bw_file): |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
96 # This will be fine under Galaxy, but could use temp folder? |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
97 size_file = "%s-sizes.txt" % (os.path.splitext(bw_file)[0]) |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
98 with open(size_file, "w") as out_handle: |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
99 for chrom, size in chr_sizes: |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
100 out_handle.write("%s\t%s\n" % (chrom, size)) |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
101 try: |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
102 cl = config["program"]["ucsc_bedGraphToBigWig"] + \ |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
103 [bedgraph_file, size_file, bw_file] |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
104 subprocess.check_call(cl) |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
105 finally: |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
106 os.remove(size_file) |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
107 return bw_file |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
108 |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
109 |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
110 if __name__ == "__main__": |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
111 parser = OptionParser() |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
112 parser.add_option("-o", "--outfile", dest="outfile") |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
113 parser.add_option("-s", "--split", action="store_true", dest="split") |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
114 (options, args) = parser.parse_args() |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
115 if len(args) not in [1, 2]: |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
116 print "Incorrect arguments" |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
117 print __doc__ |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
118 sys.exit() |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
119 kwargs = dict( |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
120 outfile=options.outfile, |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
121 split=options.split) |
|
9163e1db4c16
planemo upload for repository https://github.com/lparsons/galaxy_tools/tree/master/tools/bam_to_bigwig/ commit b5c3df21127fc27f66a97761173c07e161f2fa8e
lparsons
parents:
diff
changeset
|
122 main(*args, **kwargs) |
