Mercurial > repos > rmarenco > hubarchivecreator
comparison util/subtools.py @ 13:25809f699cb3 draft
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 65ab931ef2b05a5acf06cbde3a746c94a0a0a4cb
author | rmarenco |
---|---|
date | Thu, 11 Aug 2016 19:02:29 -0400 |
parents | acc233161f50 |
children | 3233451a3bd6 |
comparison
equal
deleted
inserted
replaced
12:747475757cb0 | 13:25809f699cb3 |
---|---|
4 """ | 4 """ |
5 This class handles the subprocess calls of the different tools used | 5 This class handles the subprocess calls of the different tools used |
6 in HubArchiveCreator | 6 in HubArchiveCreator |
7 """ | 7 """ |
8 | 8 |
9 import logging | |
9 import os | 10 import os |
10 import subprocess | 11 import subprocess |
11 | 12 import sys |
13 | |
14 class PopenError(Exception): | |
15 def __init__(self, cmd, error, return_code): | |
16 self.cmd = cmd | |
17 self.error = error | |
18 self.return_code = return_code | |
19 | |
20 def __str__(self): | |
21 message = "The subprocess {0} has returned the error: {1}.".format(self.cmd, self.return_code) | |
22 message = ','.join((message, "Its error message is: {0}".format(self.error))) | |
23 return repr(message) | |
12 | 24 |
13 def _handleExceptionAndCheckCall(array_call, **kwargs): | 25 def _handleExceptionAndCheckCall(array_call, **kwargs): |
14 """ | 26 """ |
15 This class handle exceptions and call the tool. | 27 This class handle exceptions and call the tool. |
16 It maps the signature of subprocess.check_call: | 28 It maps the signature of subprocess.check_call: |
18 """ | 30 """ |
19 stdin = kwargs.get('stdin') | 31 stdin = kwargs.get('stdin') |
20 stdout = kwargs.get('stdout') | 32 stdout = kwargs.get('stdout') |
21 stderr = kwargs.get('stderr') | 33 stderr = kwargs.get('stderr') |
22 shell = kwargs.get('shell') | 34 shell = kwargs.get('shell') |
35 | |
36 cmd = array_call[0] | |
37 | |
38 output = None | |
39 error = None | |
40 | |
41 # TODO: Check the value of array_call and <=[0] | |
42 logging.debug("Calling {0}:".format(cmd)) | |
43 | |
44 logging.debug("---------") | |
45 | |
46 # TODO: Use universal_newlines option from Popen? | |
23 try: | 47 try: |
24 p = subprocess.check_call(array_call, stdin=stdin, stdout=stdout, stderr=stderr, shell=shell) | 48 p = subprocess.Popen(array_call, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell) |
25 except subprocess.CalledProcessError: | 49 output, error = p.communicate() |
26 raise | 50 |
51 logging.debug("\t{0}".format(output)) | |
52 # If we detect an error from the subprocess, then we raise an exception | |
53 # TODO: Manage if we raise an exception for everything, or use CRITICAL etc... but not stop process | |
54 # TODO: The responsability of returning a sys.exit() should not be there, but up in the app. | |
55 if p.returncode: | |
56 raise PopenError(cmd, error, p.returncode) | |
57 | |
58 except OSError as e: | |
59 message = "The subprocess {0} has encountered an OSError: {1}".format(cmd, e.strerror) | |
60 if e.filename: | |
61 message = '\n'.join((message, ", against this file: {0}".format(e.filename))) | |
62 logging.error(message) | |
63 sys.exit(-1) | |
64 except PopenError as p: | |
65 message = "The subprocess {0} has returned the error: {1}.".format(p.cmd, p.return_code) | |
66 message = '\n'.join((message, "Its error message is: {0}".format(p.error))) | |
67 | |
68 logging.exception(message) | |
69 | |
70 sys.exit(p.return_code) | |
71 except Exception as e: | |
72 message = "The subprocess {0} has encountered an unknown error: {1}".format(cmd, e) | |
73 logging.exception(message) | |
74 | |
75 sys.exit(-1) | |
27 return p | 76 return p |
28 | 77 |
29 | 78 |
30 def twoBitInfo(two_bit_file_name, two_bit_info_file): | 79 def twoBitInfo(two_bit_file_name, two_bit_info_file): |
31 """ | 80 """ |
74 """ | 123 """ |
75 array_call = ['gff3ToGenePred', input_gff3_file_name, gene_pred_file_name] | 124 array_call = ['gff3ToGenePred', input_gff3_file_name, gene_pred_file_name] |
76 p = _handleExceptionAndCheckCall(array_call) | 125 p = _handleExceptionAndCheckCall(array_call) |
77 return p | 126 return p |
78 | 127 |
128 def genePredToBigGenePred(gene_pred_file_name, unsorted_bigGenePred_file_name): | |
129 """ | |
130 Call genePredToBigGenePred and write the result into unsorted_bigGenePred_file_name | |
131 :param gene_pred_file_name: | |
132 :param unsorted_bigGenePred_file_name: | |
133 :return: | |
134 """ | |
135 array_call = ['genePredToBigGenePred', | |
136 gene_pred_file_name, | |
137 unsorted_bigGenePred_file_name] | |
138 p = _handleExceptionAndCheckCall(array_call) | |
139 return p | |
79 | 140 |
80 def genePredToBed(gene_pred_file_name, unsorted_bed_file_name): | 141 def genePredToBed(gene_pred_file_name, unsorted_bed_file_name): |
81 """ | 142 """ |
82 Call genePredToBed and write the result into unsorted_bed_file_name | 143 Call genePredToBed and write the result into unsorted_bed_file_name |
83 :param gene_pred_file_name: | 144 :param gene_pred_file_name: |
111 array_call = ['sort', '-k2rn', two_bit_info_file_name, '-o', chrom_sizes_file_name] | 172 array_call = ['sort', '-k2rn', two_bit_info_file_name, '-o', chrom_sizes_file_name] |
112 p = _handleExceptionAndCheckCall(array_call) | 173 p = _handleExceptionAndCheckCall(array_call) |
113 return p | 174 return p |
114 | 175 |
115 | 176 |
116 def bedToBigBed(sorted_bed_file_name, chrom_sizes_file_name, big_bed_file_name, typeOption=None, autoSql=None): | 177 def bedToBigBed(sorted_bed_file_name, chrom_sizes_file_name, big_bed_file_name, |
178 typeOption=None, autoSql=None, tab=False): | |
117 """ | 179 """ |
118 Call bedToBigBed on sorted_bed_file_name, using chrom_sizes_file_name and write the result into big_bed_file_name | 180 Call bedToBigBed on sorted_bed_file_name, using chrom_sizes_file_name and write the result into big_bed_file_name |
119 :param sorted_bed_file_name: | 181 :param sorted_bed_file_name: |
120 :param chrom_sizes_file_name: | 182 :param chrom_sizes_file_name: |
121 :param big_bed_file_name: | 183 :param big_bed_file_name: |
122 :return: | 184 :return: |
123 """ | 185 """ |
186 | |
187 # TODO: Move this into the _handleExceptionAndCheckCall function | |
188 # Parse the array | |
189 logging.debug("sorted_bed_file_name: {0}".format(sorted_bed_file_name)) | |
190 logging.debug("chrom_sizes_file_name: {0}".format(chrom_sizes_file_name)) | |
191 logging.debug("big_bed_file_name: {0}".format(big_bed_file_name)) | |
192 logging.debug("typeOption: {0}".format(typeOption)) | |
193 logging.debug("autoSql: {0}".format(autoSql)) | |
194 logging.debug("tab option: {0}".format(tab)) | |
195 | |
124 array_call = ['bedToBigBed', sorted_bed_file_name, chrom_sizes_file_name, big_bed_file_name] | 196 array_call = ['bedToBigBed', sorted_bed_file_name, chrom_sizes_file_name, big_bed_file_name] |
125 if typeOption: | 197 if typeOption: |
198 typeOption = ''.join(['-type=', typeOption]) | |
126 array_call.append(typeOption) | 199 array_call.append(typeOption) |
127 if autoSql: | 200 if autoSql: |
201 autoSql = ''.join(['-as=', autoSql]) | |
128 array_call.append(autoSql) | 202 array_call.append(autoSql) |
203 if tab: | |
204 array_call.append('-tab') | |
129 | 205 |
130 p = _handleExceptionAndCheckCall(array_call) | 206 p = _handleExceptionAndCheckCall(array_call) |
131 return p | 207 return p |
132 | 208 |
133 | 209 |