Mercurial > repos > yating-l > jbrowsearchivecreator
comparison util/subtools.py @ 38:d17f629f5486 draft
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit e4c1d387db160a3bf4a1e8abc288bdffbbbe2818-dirty
author | yating-l |
---|---|
date | Fri, 06 Apr 2018 13:44:56 -0400 |
parents | 68466f5064ce |
children | 4a69515eed63 |
comparison
equal
deleted
inserted
replaced
37:43909f065bee | 38:d17f629f5486 |
---|---|
7 import json | 7 import json |
8 import subprocess | 8 import subprocess |
9 import os | 9 import os |
10 import sys | 10 import sys |
11 import tempfile | 11 import tempfile |
12 import string | 12 import shutil |
13 import logging | 13 import logging |
14 | 14 |
15 class PopenError(Exception): | 15 class PopenError(Exception): |
16 def __init__(self, cmd, error, return_code): | 16 def __init__(self, cmd, error, return_code): |
17 self.cmd = cmd | 17 self.cmd = cmd |
226 filename = bamfile + '.bai' | 226 filename = bamfile + '.bai' |
227 if os.path.exists(filename): | 227 if os.path.exists(filename): |
228 return filename | 228 return filename |
229 else: | 229 else: |
230 raise ValueError('Did not find bai file') | 230 raise ValueError('Did not find bai file') |
231 | |
232 def createFastaIndex(fastaFile): | |
233 subprocess.call(['samtools', 'faidx', fastaFile]) | |
234 filename = fastaFile + '.fai' | |
235 if os.path.exists(filename): | |
236 return filename | |
237 else: | |
238 raise ValueError('Did not find fai file') | |
239 | |
240 def gff3sort(inputFile, outputFile, precise=False): | |
241 array_call = ['gff3sort.pl', inputFile, '>', outputFile] | |
242 if precise: | |
243 array_call.append('--precise') | |
244 p = _handleExceptionAndCheckCall(array_call) | |
245 return p | |
246 | |
247 def bedSort(inputFile, outputFile): | |
248 array_call = ['sort', '-k1,1', '-k2,2n', '-k6,6', inputFile, '>', outputFile] | |
249 p = _handleExceptionAndCheckCall(array_call) | |
250 return p | |
251 | |
252 def bgzip(inputFile): | |
253 subprocess.call(['bgzip', inputFile]) | |
254 filename = inputFile + '.gz' | |
255 if os.path.exists(filename): | |
256 return filename | |
257 else: | |
258 raise ValueError('Did not find gz file') | |
259 | |
260 def createTabix(inputFile, dataType): | |
261 subprocess.call(['tabix', '-p', dataType, inputFile]) | |
262 filename = inputFile + '.tbi' | |
263 if os.path.exists(filename): | |
264 return filename | |
265 else: | |
266 raise ValueError('Did not find tbi file') | |
267 | |
268 def generate_tabix_indexed_track(inputFile, dataType, outputFolder): | |
269 if "bed" in dataType: | |
270 fileType = 'bed' | |
271 sortedFile = tempfile.NamedTemporaryFile(bufsize=0) | |
272 bedSort(inputFile, sortedFile.name) | |
273 elif "gff" in dataType: | |
274 fileType = 'gff' | |
275 sortedFile = tempfile.NamedTemporaryFile(bufsize=0) | |
276 gff3sort(inputFile, sortedFile.name) | |
277 compressedFile = bgzip(sortedFile) | |
278 tabixFile = createTabix(compressedFile, fileType) | |
279 trackPath = os.path.join(outputFolder, inputFile) | |
280 trackIndexPath = os.path.join(outputFolder, inputFile+'.tbi') | |
281 shutil.copy(compressedFile, trackPath) | |
282 shutil.copy(tabixFile, trackIndexPath) | |
231 | 283 |
232 def flatfile_to_json(inputFile, dataType, trackType, trackLabel, outputFolder, options=None, compress=True): | 284 def flatfile_to_json(inputFile, dataType, trackType, trackLabel, outputFolder, options=None, compress=True): |
233 if "bed" in dataType: | 285 if "bed" in dataType: |
234 fileType = "--bed" | 286 fileType = "--bed" |
235 elif "gff" in dataType: | 287 elif "gff" in dataType: |