Mercurial > repos > peterjc > mira4_assembler
annotate tools/mira4_0/mira4_convert.py @ 39:bbf14bb9607b draft default tip
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
author | peterjc |
---|---|
date | Mon, 03 Jun 2019 13:29:00 -0400 |
parents | cee8f9005e43 |
children |
rev | line source |
---|---|
25 | 1 #!/usr/bin/env python |
2 """A simple wrapper script to call MIRA and collect its output. | |
3 | |
4 This focuses on the miraconvert binary. | |
5 """ | |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
6 |
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
7 from __future__ import print_function |
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
8 |
25 | 9 import os |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
10 import shutil |
25 | 11 import subprocess |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
12 import sys |
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
13 |
25 | 14 from optparse import OptionParser |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
15 |
25 | 16 try: |
17 from io import BytesIO | |
18 except ImportError: | |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
19 # Should we worry about Python 2.5 or older? |
25 | 20 from StringIO import StringIO as BytesIO |
21 | |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
22 # Do we need any PYTHONPATH magic? |
25 | 23 from mira4_make_bam import depad |
24 | |
35
259891fce7fd
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit e4c56df75150c82d3e9c4ac487c4209108e52412
peterjc
parents:
34
diff
changeset
|
25 WRAPPER_VER = "0.0.11" # Keep in sync with the XML file |
25 | 26 |
27 | |
28 def run(cmd): | |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
29 # Avoid using shell=True when we call subprocess to ensure if the Python |
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
30 # script is killed, so too is the child process. |
25 | 31 try: |
39
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
32 child = subprocess.Popen( |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
33 cmd, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
34 ) |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
35 except Exception as err: |
31
fd95aaef8818
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit bc3d484c5cd68ddcf456db2fff489d584aa2034c
peterjc
parents:
25
diff
changeset
|
36 sys.exit("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err)) |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
37 # Use .communicate as can get deadlocks with .wait(), |
25 | 38 stdout, stderr = child.communicate() |
39 return_code = child.returncode | |
40 if return_code: | |
41 cmd_str = " ".join(cmd) # doesn't quote spaces etc | |
42 if stderr and stdout: | |
39
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
43 sys.exit( |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
44 "Return code %i from command:\n%s\n\n%s\n\n%s" |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
45 % (return_code, cmd_str, stdout, stderr) |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
46 ) |
25 | 47 else: |
39
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
48 sys.exit( |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
49 "Return code %i from command:\n%s\n%s" % (return_code, cmd_str, stderr) |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
50 ) |
25 | 51 |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
52 |
25 | 53 def get_version(mira_binary): |
38
cee8f9005e43
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 206259620376b322fc8ed99a6efdd3712f38764b
peterjc
parents:
35
diff
changeset
|
54 """Run MIRA to find its version number.""" |
25 | 55 # At the commend line I would use: mira -v | head -n 1 |
56 # however there is some pipe error when doing that here. | |
57 cmd = [mira_binary, "-v"] | |
58 try: | |
39
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
59 child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
60 except Exception as err: |
25 | 61 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err)) |
62 sys.exit(1) | |
63 ver, tmp = child.communicate() | |
64 del child | |
65 return ver.split("\n", 1)[0].strip() | |
66 | |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
67 |
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
68 # Parse Command Line |
39
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
69 usage = ( |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
70 """Galaxy MIRA4 wrapper script v%s - use as follows: |
25 | 71 |
72 $ python mira4_convert.py ... | |
73 | |
74 This will run the MIRA miraconvert binary and collect its output files as directed. | |
39
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
75 """ |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
76 % WRAPPER_VER |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
77 ) |
25 | 78 parser = OptionParser(usage=usage) |
39
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
79 parser.add_option( |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
80 "--input", dest="input", default=None, metavar="FILE", help="MIRA input filename" |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
81 ) |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
82 parser.add_option( |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
83 "-x", "--min_length", dest="min_length", default="0", help="Minimum contig length" |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
84 ) |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
85 parser.add_option( |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
86 "-y", |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
87 "--min_cover", |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
88 dest="min_cover", |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
89 default="0", |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
90 help="Minimum average contig coverage", |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
91 ) |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
92 parser.add_option( |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
93 "-z", "--min_reads", dest="min_reads", default="0", help="Minimum reads per contig" |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
94 ) |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
95 parser.add_option( |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
96 "--maf", dest="maf", default="", metavar="FILE", help="MIRA MAF output filename" |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
97 ) |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
98 parser.add_option( |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
99 "--ace", dest="ace", default="", metavar="FILE", help="ACE output filename" |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
100 ) |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
101 parser.add_option( |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
102 "--bam", dest="bam", default="", metavar="FILE", help="Unpadded BAM output filename" |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
103 ) |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
104 parser.add_option( |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
105 "--fasta", |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
106 dest="fasta", |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
107 default="", |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
108 metavar="FILE", |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
109 help="Unpadded FASTA output filename", |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
110 ) |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
111 parser.add_option( |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
112 "--cstats", |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
113 dest="cstats", |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
114 default="", |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
115 metavar="FILE", |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
116 help="Contig statistics filename", |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
117 ) |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
118 parser.add_option( |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
119 "-v", |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
120 "--version", |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
121 dest="version", |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
122 default=False, |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
123 action="store_true", |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
124 help="Show version and quit", |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
125 ) |
25 | 126 options, args = parser.parse_args() |
127 if args: | |
31
fd95aaef8818
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit bc3d484c5cd68ddcf456db2fff489d584aa2034c
peterjc
parents:
25
diff
changeset
|
128 sys.exit("Expected options (e.g. --input example.maf), not arguments") |
25 | 129 |
130 input_maf = options.input | |
131 out_maf = options.maf | |
132 out_bam = options.bam | |
133 out_fasta = options.fasta | |
134 out_ace = options.ace | |
135 out_cstats = options.cstats | |
136 | |
35
259891fce7fd
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit e4c56df75150c82d3e9c4ac487c4209108e52412
peterjc
parents:
34
diff
changeset
|
137 if "MIRA4" in os.environ: |
25 | 138 mira_path = os.environ["MIRA4"] |
35
259891fce7fd
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit e4c56df75150c82d3e9c4ac487c4209108e52412
peterjc
parents:
34
diff
changeset
|
139 mira_convert = os.path.join(mira_path, "miraconvert") |
259891fce7fd
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit e4c56df75150c82d3e9c4ac487c4209108e52412
peterjc
parents:
34
diff
changeset
|
140 if not os.path.isfile(mira_convert): |
39
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
141 sys.exit( |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
142 "Missing miraconvert under $MIRA4, %r\nFolder contained: %s" |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
143 % (mira_convert, ", ".join(os.listdir(mira_path))) |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
144 ) |
35
259891fce7fd
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit e4c56df75150c82d3e9c4ac487c4209108e52412
peterjc
parents:
34
diff
changeset
|
145 else: |
39
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
146 sys.stderr.write( |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
147 "DEBUG: Since $MIRA4 is not set, assuming mira binaries are on $PATH.\n" |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
148 ) |
35
259891fce7fd
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit e4c56df75150c82d3e9c4ac487c4209108e52412
peterjc
parents:
34
diff
changeset
|
149 mira_path = None |
259891fce7fd
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit e4c56df75150c82d3e9c4ac487c4209108e52412
peterjc
parents:
34
diff
changeset
|
150 mira_convert = "miraconvert" |
25 | 151 |
152 mira_convert_ver = get_version(mira_convert) | |
153 if not mira_convert_ver.strip().startswith("4.0"): | |
39
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
154 sys.exit( |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
155 "This wrapper is for MIRA V4.0, not:\n%s\n%s" % (mira_convert_ver, mira_convert) |
bbf14bb9607b
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
peterjc
parents:
38
diff
changeset
|
156 ) |
25 | 157 if options.version: |
158 print("%s, MIRA wrapper version %s" % (mira_convert_ver, WRAPPER_VER)) | |
159 sys.exit(0) | |
160 | |
161 if not input_maf: | |
31
fd95aaef8818
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit bc3d484c5cd68ddcf456db2fff489d584aa2034c
peterjc
parents:
25
diff
changeset
|
162 sys.exit("Input MIRA file is required") |
25 | 163 elif not os.path.isfile(input_maf): |
31
fd95aaef8818
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit bc3d484c5cd68ddcf456db2fff489d584aa2034c
peterjc
parents:
25
diff
changeset
|
164 sys.exit("Missing input MIRA file: %r" % input_maf) |
25 | 165 |
166 if not (out_maf or out_bam or out_fasta or out_ace or out_cstats): | |
31
fd95aaef8818
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit bc3d484c5cd68ddcf456db2fff489d584aa2034c
peterjc
parents:
25
diff
changeset
|
167 sys.exit("No output requested") |
25 | 168 |
169 | |
170 def check_min_int(value, name): | |
171 try: | |
172 i = int(value) | |
31
fd95aaef8818
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit bc3d484c5cd68ddcf456db2fff489d584aa2034c
peterjc
parents:
25
diff
changeset
|
173 except ValueError: |
fd95aaef8818
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit bc3d484c5cd68ddcf456db2fff489d584aa2034c
peterjc
parents:
25
diff
changeset
|
174 sys.exit("Bad %s setting, %r" % (name, value)) |
25 | 175 if i < 0: |
31
fd95aaef8818
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit bc3d484c5cd68ddcf456db2fff489d584aa2034c
peterjc
parents:
25
diff
changeset
|
176 sys.exit("Negative %s setting, %r" % (name, value)) |
25 | 177 return i |
178 | |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
179 |
25 | 180 min_length = check_min_int(options.min_length, "minimum length") |
181 min_cover = check_min_int(options.min_cover, "minimum cover") | |
182 min_reads = check_min_int(options.min_reads, "minimum reads") | |
183 | |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
184 # TODO - Run MIRA in /tmp or a configurable directory? |
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
185 # Currently Galaxy puts us somewhere safe like: |
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
186 # /opt/galaxy-dist/database/job_working_directory/846/ |
25 | 187 temp = "." |
188 | |
189 | |
190 cmd_list = [mira_convert] | |
191 if min_length: | |
192 cmd_list.extend(["-x", str(min_length)]) | |
193 if min_cover: | |
194 cmd_list.extend(["-y", str(min_cover)]) | |
195 if min_reads: | |
196 cmd_list.extend(["-z", str(min_reads)]) | |
197 cmd_list.extend(["-f", "maf", input_maf, os.path.join(temp, "converted")]) | |
198 if out_maf: | |
199 cmd_list.append("maf") | |
200 if out_bam: | |
201 cmd_list.append("samnbb") | |
202 if not out_fasta: | |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
203 # Need this for samtools depad |
25 | 204 out_fasta = os.path.join(temp, "depadded.fasta") |
205 if out_fasta: | |
206 cmd_list.append("fasta") | |
207 if out_ace: | |
208 cmd_list.append("ace") | |
209 if out_cstats: | |
210 cmd_list.append("cstats") | |
211 run(cmd_list) | |
212 | |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
213 |
25 | 214 def collect(old, new): |
215 if not os.path.isfile(old): | |
31
fd95aaef8818
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit bc3d484c5cd68ddcf456db2fff489d584aa2034c
peterjc
parents:
25
diff
changeset
|
216 sys.exit("Missing expected output file %s" % old) |
25 | 217 shutil.move(old, new) |
218 | |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
219 |
25 | 220 if out_maf: |
221 collect(os.path.join(temp, "converted.maf"), out_maf) | |
222 if out_fasta: | |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
223 # Can we look at the MAF file to see if there are multiple strains? |
25 | 224 old = os.path.join(temp, "converted_AllStrains.unpadded.fasta") |
225 if os.path.isfile(old): | |
226 collect(old, out_fasta) | |
227 else: | |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
228 # Might the output be filtered down to zero contigs? |
25 | 229 old = os.path.join(temp, "converted.fasta") |
230 if not os.path.isfile(old): | |
31
fd95aaef8818
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit bc3d484c5cd68ddcf456db2fff489d584aa2034c
peterjc
parents:
25
diff
changeset
|
231 sys.exit("Missing expected output FASTA file") |
25 | 232 elif os.path.getsize(old) == 0: |
233 print("Warning - no contigs (harsh filters?)") | |
234 collect(old, out_fasta) | |
235 else: | |
31
fd95aaef8818
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit bc3d484c5cd68ddcf456db2fff489d584aa2034c
peterjc
parents:
25
diff
changeset
|
236 sys.exit("Missing expected output FASTA file (only generic file present)") |
25 | 237 if out_ace: |
238 collect(os.path.join(temp, "converted.maf"), out_ace) | |
239 if out_cstats: | |
240 collect(os.path.join(temp, "converted_info_contigstats.txt"), out_cstats) | |
241 | |
242 if out_bam: | |
243 assert os.path.isfile(out_fasta) | |
244 old = os.path.join(temp, "converted.samnbb") | |
245 if not os.path.isfile(old): | |
246 old = os.path.join(temp, "converted.sam") | |
247 if not os.path.isfile(old): | |
31
fd95aaef8818
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit bc3d484c5cd68ddcf456db2fff489d584aa2034c
peterjc
parents:
25
diff
changeset
|
248 sys.exit("Missing expected intermediate file %s" % old) |
25 | 249 h = BytesIO() |
250 msg = depad(out_fasta, old, out_bam, h) | |
251 if msg: | |
252 print(msg) | |
253 print(h.getvalue()) | |
254 h.close() | |
255 sys.exit(1) | |
256 h.close() | |
257 if out_fasta == os.path.join(temp, "depadded.fasta"): | |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
258 # Not asked for by Galaxy, no longer needed |
25 | 259 os.remove(out_fasta) |
260 | |
261 if min_length or min_cover or min_reads: | |
262 print("Filtered.") | |
263 else: | |
264 print("Converted.") |