Mercurial > repos > yating-l > jbrowse_hub
diff TrackHub.py @ 5:e7c80e9b70ae draft
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
author | yating-l |
---|---|
date | Tue, 14 Mar 2017 12:24:37 -0400 |
parents | e4f3f2ed4fa5 |
children | e1f188b43750 |
line wrap: on
line diff
--- a/TrackHub.py Fri Mar 10 14:57:36 2017 -0500 +++ b/TrackHub.py Tue Mar 14 12:24:37 2017 -0400 @@ -6,28 +6,28 @@ import subprocess import string import shutil -from mako.lookup import TemplateLookup +import tempfile + +#TODO: package JBrowse file conversion .pl files class TrackHub: - def __init__(self, inputFiles, reference, outputDirect, tool_dir): - self.input_files = inputFiles - self.out_path = outputDirect + def __init__(self, inputFiles, reference, outputDirect, tool_dir, genome, extra_files_path): + self.input_files = inputFiles.tracks + self.outfile = outputDirect + self.outfolder = extra_files_path + self.out_path = os.path.join(extra_files_path, genome) self.reference = reference self.tool_dir = tool_dir + self.raw = os.path.join(self.out_path, 'raw') + self.json = os.path.join(self.out_path, 'json') try: if not self.out_path: raise ValueError('empty output path\n') - if os.path.exists(self.out_path): - shutil.rmtree(self.out_path) - os.mkdir(self.out_path) - self.json = os.path.join(self.out_path, 'json') + if not os.path.exists(self.out_path): + raise ValueError('the output folder has not been created') if os.path.exists(self.json): shutil.rmtree(self.json) - os.mkdir(self.json) - self.raw = os.path.join(self.out_path, 'raw') - if os.path.exists(self.raw): - shutil.rmtree(self.raw) - shutil.move('raw', self.out_path) + os.makedirs(self.json) except OSError as e: print "Cannot create json folder error({0}): {1}".format(e.errno, e.strerror) else: @@ -39,19 +39,21 @@ self.addTrack(input_file) self.indexName() self.makeArchive() - shutil.rmtree(self.out_path) + #shutil.rmtree(self.out_path) + self.outHtml() print "Success!\n" def prepareRefseq(self): try: - p = subprocess.Popen([os.path.join(self.tool_dir, 'prepare-refseqs.pl'), '--fasta', self.reference, '--out', self.json]) + #print os.path.join(self.tool_dir, 'prepare-refseqs.pl') + ", '--fasta', " + self.reference +", '--out', self.json])" + p = subprocess.Popen(['prepare-refseqs.pl', '--fasta', self.reference, '--out', self.json]) # Wait for process to terminate. p.communicate() except OSError as e: print "Cannot prepare reference error({0}): {1}".format(e.errno, e.strerror) - + #TODO: hard coded the bam and bigwig tracks. Need to allow users to customize the settings def addTrack(self, track): - if track.dataType == 'bam': + if track['dataType'] == 'bam': json_file = os.path.join(self.json, "trackList.json") bam_track = dict() bam_track['type'] = 'JBrowse/View/Track/Alignments2' @@ -59,7 +61,7 @@ bam_track['urlTemplate'] = os.path.join('../raw', track.fileName) utils.add_tracks_to_json(json_file, bam_track, 'add_tracks') print "add bam track\n" - elif track.dataType == 'bigwig': + elif track['dataType'] == 'bigwig': json_file = os.path.join(self.json, "trackList.json") bigwig_track = dict() bigwig_track['label'] = 'rnaseq' @@ -74,28 +76,45 @@ bigwig_track['style']['height'] = 100 utils.add_tracks_to_json(json_file, bigwig_track, 'add_tracks') else: - gff3_file = os.path.join(self.raw, track.fileName) - label = track.fileName - if track.dataType == 'bedSpliceJunctions' or track.dataType == 'gtf': - p = subprocess.Popen([os.path.join(self.tool_dir, 'flatfile-to-json.pl'), '--gff', gff3_file, '--trackType', 'CanvasFeatures', '--trackLabel', label, '--config', '{"glyph": "JBrowse/View/FeatureGlyph/Segments"}', '--out', self.json]) - elif track.dataType == 'gff3-transcript': - p = subprocess.Popen([os.path.join(self.tool_dir, 'flatfile-to-json.pl'), '--gff', gff3_file, '--trackType', 'MyPlugin/GenePred', '--trackLabel', label, '--config', '{"transcriptType": "transcript"}', '--out', self.json]) - elif track.dataType == 'gff3': - p = subprocess.Popen([os.path.join(self.tool_dir, 'flatfile-to-json.pl'), '--gff', gff3_file, '--trackType', 'MyPlugin/GenePred', '--trackLabel', label, '--out', self.json]) + gff3_file = os.path.join(self.raw, track['fileName']) + label = track['fileName'] + if track['dataType'] == 'bedSpliceJunctions' or track['dataType'] == 'gtf': + p = subprocess.Popen(['flatfile-to-json.pl', '--gff', gff3_file, '--trackType', 'CanvasFeatures', '--trackLabel', label, '--config', '{"glyph": "JBrowse/View/FeatureGlyph/Segments"}', '--out', self.json]) + elif track['dataType'] == 'gff3-transcript': + p = subprocess.Popen(['flatfile-to-json.pl', '--gff', gff3_file, '--trackType', 'MyPlugin/GenePred', '--trackLabel', label, '--config', '{"transcriptType": "transcript"}', '--out', self.json]) + elif track['dataType'] == 'gff3': + p = subprocess.Popen(['flatfile-to-json.pl', '--gff', gff3_file, '--trackType', 'MyPlugin/GenePred', '--trackLabel', label, '--out', self.json]) else: - p = subprocess.Popen([os.path.join(self.tool_dir, 'flatfile-to-json.pl'), '--gff', gff3_file, '--trackType', 'CanvasFeatures', '--trackLabel', label, '--out', self.json]) + p = subprocess.Popen(['flatfile-to-json.pl', '--gff', gff3_file, '--trackType', 'CanvasFeatures', '--trackLabel', label, '--out', self.json]) p.communicate() def indexName(self): - p = subprocess.Popen([os.path.join(self.tool_dir, 'generate-names.pl'), '-v', '--out', self.json]) + p = subprocess.Popen(['generate-names.pl', '-v', '--out', self.json]) p.communicate() print "finished name index \n" def makeArchive(self): - filename = os.path.basename(self.out_path) - shutil.make_archive(filename, 'tar', self.out_path) - - - - + shutil.make_archive(self.out_path, 'zip', self.out_path) + shutil.rmtree(self.out_path) + + def outHtml(self): + #htmloutput = tempfile.NamedTemporaryFile(self.outfile, suffix = '.html', bufsize=0, delete=False) + with open(self.outfile, 'w') as htmlfile: + htmlstr = 'The JBrowse Hub is created: <br>' + zipfiles = '<li><a href = "%s">Download</a></li>' + filedir_abs = os.path.abspath(self.outfile) + filedir = os.path.dirname(filedir_abs) + filedir = os.path.join(filedir, self.outfolder) + for root, dirs, files in os.walk(filedir): + for file in files: + if file.endswith('.zip'): + relative_directory = os.path.relpath(root, filedir) + relative_file_path = os.path.join(relative_directory, file) + htmlstr += zipfiles % relative_file_path + #htmlstr = htmlstr % zipfile + htmlfile.write(htmlstr) + + + +