annotate tools/mira4_0/mira4_convert.py @ 3:a4f602cc3aa9 draft

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