comparison util/subtools.py @ 16:3233451a3bd6 draft

planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit fc73ec22a0db3ab09c4ac13dc58f0b54ae37845c
author rmarenco
date Sun, 25 Sep 2016 11:25:38 -0400
parents 25809f699cb3
children 884ee2a71680
comparison
equal deleted inserted replaced
15:2a45cd656e8e 16:3233451a3bd6
26 """ 26 """
27 This class handle exceptions and call the tool. 27 This class handle exceptions and call the tool.
28 It maps the signature of subprocess.check_call: 28 It maps the signature of subprocess.check_call:
29 See https://docs.python.org/2/library/subprocess.html#subprocess.check_call 29 See https://docs.python.org/2/library/subprocess.html#subprocess.check_call
30 """ 30 """
31 stdin = kwargs.get('stdin') 31 stdout = kwargs.get('stdout', subprocess.PIPE)
32 stdout = kwargs.get('stdout') 32 stderr = kwargs.get('stderr', subprocess.PIPE)
33 stderr = kwargs.get('stderr') 33 shell = kwargs.get('shell', False)
34 shell = kwargs.get('shell')
35 34
36 cmd = array_call[0] 35 cmd = array_call[0]
37 36
38 output = None 37 output = None
39 error = None 38 error = None
43 42
44 logging.debug("---------") 43 logging.debug("---------")
45 44
46 # TODO: Use universal_newlines option from Popen? 45 # TODO: Use universal_newlines option from Popen?
47 try: 46 try:
48 p = subprocess.Popen(array_call, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell) 47 p = subprocess.Popen(array_call, stdout=stdout, stderr=stderr, shell=shell)
48
49 # TODO: Change this because of possible memory issues => https://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate
50
49 output, error = p.communicate() 51 output, error = p.communicate()
50 52
51 logging.debug("\t{0}".format(output)) 53 if stdout == subprocess.PIPE:
54 logging.debug("\t{0}".format(output))
55 else:
56 logging.debug("\tOutput in file {0}".format(stdout.name))
52 # If we detect an error from the subprocess, then we raise an exception 57 # 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 58 # 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. 59 # TODO: The responsability of returning a sys.exit() should not be there, but up in the app.
55 if p.returncode: 60 if p.returncode:
56 raise PopenError(cmd, error, p.returncode) 61 if stderr == subprocess.PIPE:
62 raise PopenError(cmd, error, p.returncode)
63 else:
64 # TODO: To Handle properly with a design behind, if we received a option as a file for the error
65 raise Exception("Error when calling {0}. Error as been logged in your file {1}. Error code: {2}"\
66 .format(cmd, stderr.name, p.returncode))
57 67
58 except OSError as e: 68 except OSError as e:
59 message = "The subprocess {0} has encountered an OSError: {1}".format(cmd, e.strerror) 69 message = "The subprocess {0} has encountered an OSError: {1}".format(cmd, e.strerror)
60 if e.filename: 70 if e.filename:
61 message = '\n'.join((message, ", against this file: {0}".format(e.filename))) 71 message = '\n'.join((message, ", against this file: {0}".format(e.filename)))
73 logging.exception(message) 83 logging.exception(message)
74 84
75 sys.exit(-1) 85 sys.exit(-1)
76 return p 86 return p
77 87
78
79 def twoBitInfo(two_bit_file_name, two_bit_info_file): 88 def twoBitInfo(two_bit_file_name, two_bit_info_file):
80 """ 89 """
81 Call twoBitInfo and write the result into twoBit_info_file 90 Call twoBitInfo and write the result into twoBit_info_file
82 :param two_bit_file_name: 91 :param two_bit_file_name:
83 :param two_bit_info_file: 92 :param two_bit_info_file:
85 """ 94 """
86 array_call = ['twoBitInfo', two_bit_file_name, two_bit_info_file] 95 array_call = ['twoBitInfo', two_bit_file_name, two_bit_info_file]
87 p = _handleExceptionAndCheckCall(array_call) 96 p = _handleExceptionAndCheckCall(array_call)
88 return p 97 return p
89 98
90
91 def faToTwoBit(fasta_file_name, twoBitFile): 99 def faToTwoBit(fasta_file_name, twoBitFile):
92 """ 100 """
93 This function call faToTwoBit UCSC tool, and return the twoBitFile 101 This function call faToTwoBit UCSC tool, and return the twoBitFile
94 :param fasta_file_name: 102 :param fasta_file_name:
95 :param mySpecieFolder: 103 :param mySpecieFolder:
99 array_call = ['faToTwoBit', fasta_file_name, twoBitFile] 107 array_call = ['faToTwoBit', fasta_file_name, twoBitFile]
100 _handleExceptionAndCheckCall(array_call) 108 _handleExceptionAndCheckCall(array_call)
101 109
102 return twoBitFile 110 return twoBitFile
103 111
104
105 def gtfToGenePred(input_gtf_file_name, gene_pred_file_name): 112 def gtfToGenePred(input_gtf_file_name, gene_pred_file_name):
106 """ 113 """
107 Call gtfToGenePred and write the result into gene_pred_file_name 114 Call gtfToGenePred and write the result into gene_pred_file_name
108 :param input_gtf_file_name: 115 :param input_gtf_file_name:
109 :param gene_pred_file_name: 116 :param gene_pred_file_name:
110 :return: 117 :return:
111 """ 118 """
112 array_call = ['gtfToGenePred', input_gtf_file_name, gene_pred_file_name] 119 array_call = ['gtfToGenePred', input_gtf_file_name, gene_pred_file_name]
113 p = _handleExceptionAndCheckCall(array_call) 120 p = _handleExceptionAndCheckCall(array_call)
114 return p 121 return p
115
116 122
117 def gff3ToGenePred(input_gff3_file_name, gene_pred_file_name): 123 def gff3ToGenePred(input_gff3_file_name, gene_pred_file_name):
118 """ 124 """
119 Call gff3ToGenePred and write the result into gene_pred_file_name 125 Call gff3ToGenePred and write the result into gene_pred_file_name
120 :param input_gff3_file_name: 126 :param input_gff3_file_name:
147 """ 153 """
148 array_call = ['genePredToBed', gene_pred_file_name, unsorted_bed_file_name] 154 array_call = ['genePredToBed', gene_pred_file_name, unsorted_bed_file_name]
149 p = _handleExceptionAndCheckCall(array_call) 155 p = _handleExceptionAndCheckCall(array_call)
150 return p 156 return p
151 157
152
153 def sort(unsorted_bed_file_name, sorted_bed_file_name): 158 def sort(unsorted_bed_file_name, sorted_bed_file_name):
154 """ 159 """
155 Call sort with -k1,1 -k2,2n on unsorted_bed_file_name and write the result into sorted_bed_file_name 160 Call sort with -k1,1 -k2,2n on unsorted_bed_file_name and write the result into sorted_bed_file_name
156 :param unsorted_bed_file_name: 161 :param unsorted_bed_file_name:
157 :param sorted_bed_file_name: 162 :param sorted_bed_file_name:
159 """ 164 """
160 array_call = ['sort', '-k', '1,1', '-k', '2,2n', unsorted_bed_file_name, '-o', sorted_bed_file_name] 165 array_call = ['sort', '-k', '1,1', '-k', '2,2n', unsorted_bed_file_name, '-o', sorted_bed_file_name]
161 p = _handleExceptionAndCheckCall(array_call) 166 p = _handleExceptionAndCheckCall(array_call)
162 return p 167 return p
163 168
164
165 def sortChromSizes(two_bit_info_file_name, chrom_sizes_file_name): 169 def sortChromSizes(two_bit_info_file_name, chrom_sizes_file_name):
166 """ 170 """
167 Call sort with -k2rn on two_bit_info_file_name and write the result into chrom_sizes_file_name 171 Call sort with -k2rn on two_bit_info_file_name and write the result into chrom_sizes_file_name
168 :param two_bit_info_file_name: 172 :param two_bit_info_file_name:
169 :param chrom_sizes_file_name: 173 :param chrom_sizes_file_name:
170 :return: 174 :return:
171 """ 175 """
172 array_call = ['sort', '-k2rn', two_bit_info_file_name, '-o', chrom_sizes_file_name] 176 array_call = ['sort', '-k2rn', two_bit_info_file_name, '-o', chrom_sizes_file_name]
173 p = _handleExceptionAndCheckCall(array_call) 177 p = _handleExceptionAndCheckCall(array_call)
174 return p 178 return p
175
176 179
177 def bedToBigBed(sorted_bed_file_name, chrom_sizes_file_name, big_bed_file_name, 180 def bedToBigBed(sorted_bed_file_name, chrom_sizes_file_name, big_bed_file_name,
178 typeOption=None, autoSql=None, tab=False): 181 typeOption=None, autoSql=None, tab=False):
179 """ 182 """
180 Call bedToBigBed on sorted_bed_file_name, using chrom_sizes_file_name and write the result into big_bed_file_name 183 Call bedToBigBed on sorted_bed_file_name, using chrom_sizes_file_name and write the result into big_bed_file_name
204 array_call.append('-tab') 207 array_call.append('-tab')
205 208
206 p = _handleExceptionAndCheckCall(array_call) 209 p = _handleExceptionAndCheckCall(array_call)
207 return p 210 return p
208 211
209
210 def sortBam(input_bam_file_name, output_sorted_bam_name): 212 def sortBam(input_bam_file_name, output_sorted_bam_name):
211 """ 213 """
212 Call samtools on input_bam_file_name and output the result in output_sorted_bam_name 214 Call samtools on input_bam_file_name and output the result in output_sorted_bam_name
213 :param input_bam_file_name: 215 :param input_bam_file_name:
214 :param output_sorted_bam_name: 216 :param output_sorted_bam_name:
216 """ 218 """
217 array_call = ['samtools', 'sort', input_bam_file_name, '-o', output_sorted_bam_name] 219 array_call = ['samtools', 'sort', input_bam_file_name, '-o', output_sorted_bam_name]
218 p = _handleExceptionAndCheckCall(array_call) 220 p = _handleExceptionAndCheckCall(array_call)
219 return p 221 return p
220 222
221
222 def createBamIndex(input_sorted_bam_file_name, output_name_index_name): 223 def createBamIndex(input_sorted_bam_file_name, output_name_index_name):
223 """ 224 """
224 Call `samtools index` on imput_sorted_bam_file_name and output the result in output_name_index_name 225 Call `samtools index` on imput_sorted_bam_file_name and output the result in output_name_index_name
225 :param input_sorted_bam_file_name: 226 :param input_sorted_bam_file_name:
226 :param output_name_index_name: 227 :param output_name_index_name:
227 :return: 228 :return:
228 """ 229 """
229 array_call = ['samtools', 'index', input_sorted_bam_file_name, output_name_index_name] 230 array_call = ['samtools', 'index', input_sorted_bam_file_name, output_name_index_name]
230 p = _handleExceptionAndCheckCall(array_call) 231 p = _handleExceptionAndCheckCall(array_call)
231 return p 232 return p
233
234 def pslToBigPsl(input_psl_file_name, output_bed12_file_name):
235 """
236 Call `pslToBigPsl` on input_psl_file_name and output the result in output_bed12_file_name
237 :param input_psl_file_name: Name of the psl input file
238 :param output_bed12_file_name: Name of the output file where to store the result of the cmd
239 :return:
240 """
241 # The command to send
242 array_call = ['pslToBigPsl', input_psl_file_name, output_bed12_file_name]
243
244 p = _handleExceptionAndCheckCall(array_call)
245 return p