comparison 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
comparison
equal deleted inserted replaced
4:c7c1474a6b7d 5:e7c80e9b70ae
4 import trackObject 4 import trackObject
5 import utils 5 import utils
6 import subprocess 6 import subprocess
7 import string 7 import string
8 import shutil 8 import shutil
9 from mako.lookup import TemplateLookup 9 import tempfile
10
11 #TODO: package JBrowse file conversion .pl files
10 12
11 class TrackHub: 13 class TrackHub:
12 def __init__(self, inputFiles, reference, outputDirect, tool_dir): 14 def __init__(self, inputFiles, reference, outputDirect, tool_dir, genome, extra_files_path):
13 self.input_files = inputFiles 15 self.input_files = inputFiles.tracks
14 self.out_path = outputDirect 16 self.outfile = outputDirect
17 self.outfolder = extra_files_path
18 self.out_path = os.path.join(extra_files_path, genome)
15 self.reference = reference 19 self.reference = reference
16 self.tool_dir = tool_dir 20 self.tool_dir = tool_dir
21 self.raw = os.path.join(self.out_path, 'raw')
22 self.json = os.path.join(self.out_path, 'json')
17 try: 23 try:
18 if not self.out_path: 24 if not self.out_path:
19 raise ValueError('empty output path\n') 25 raise ValueError('empty output path\n')
20 if os.path.exists(self.out_path): 26 if not os.path.exists(self.out_path):
21 shutil.rmtree(self.out_path) 27 raise ValueError('the output folder has not been created')
22 os.mkdir(self.out_path)
23 self.json = os.path.join(self.out_path, 'json')
24 if os.path.exists(self.json): 28 if os.path.exists(self.json):
25 shutil.rmtree(self.json) 29 shutil.rmtree(self.json)
26 os.mkdir(self.json) 30 os.makedirs(self.json)
27 self.raw = os.path.join(self.out_path, 'raw')
28 if os.path.exists(self.raw):
29 shutil.rmtree(self.raw)
30 shutil.move('raw', self.out_path)
31 except OSError as e: 31 except OSError as e:
32 print "Cannot create json folder error({0}): {1}".format(e.errno, e.strerror) 32 print "Cannot create json folder error({0}): {1}".format(e.errno, e.strerror)
33 else: 33 else:
34 print "Create jbrowse folder {}".format(self.out_path) 34 print "Create jbrowse folder {}".format(self.out_path)
35 35
37 self.prepareRefseq() 37 self.prepareRefseq()
38 for input_file in self.input_files: 38 for input_file in self.input_files:
39 self.addTrack(input_file) 39 self.addTrack(input_file)
40 self.indexName() 40 self.indexName()
41 self.makeArchive() 41 self.makeArchive()
42 shutil.rmtree(self.out_path) 42 #shutil.rmtree(self.out_path)
43 self.outHtml()
43 print "Success!\n" 44 print "Success!\n"
44 45
45 def prepareRefseq(self): 46 def prepareRefseq(self):
46 try: 47 try:
47 p = subprocess.Popen([os.path.join(self.tool_dir, 'prepare-refseqs.pl'), '--fasta', self.reference, '--out', self.json]) 48 #print os.path.join(self.tool_dir, 'prepare-refseqs.pl') + ", '--fasta', " + self.reference +", '--out', self.json])"
49 p = subprocess.Popen(['prepare-refseqs.pl', '--fasta', self.reference, '--out', self.json])
48 # Wait for process to terminate. 50 # Wait for process to terminate.
49 p.communicate() 51 p.communicate()
50 except OSError as e: 52 except OSError as e:
51 print "Cannot prepare reference error({0}): {1}".format(e.errno, e.strerror) 53 print "Cannot prepare reference error({0}): {1}".format(e.errno, e.strerror)
52 54 #TODO: hard coded the bam and bigwig tracks. Need to allow users to customize the settings
53 def addTrack(self, track): 55 def addTrack(self, track):
54 if track.dataType == 'bam': 56 if track['dataType'] == 'bam':
55 json_file = os.path.join(self.json, "trackList.json") 57 json_file = os.path.join(self.json, "trackList.json")
56 bam_track = dict() 58 bam_track = dict()
57 bam_track['type'] = 'JBrowse/View/Track/Alignments2' 59 bam_track['type'] = 'JBrowse/View/Track/Alignments2'
58 bam_track['label'] = track.fileName 60 bam_track['label'] = track.fileName
59 bam_track['urlTemplate'] = os.path.join('../raw', track.fileName) 61 bam_track['urlTemplate'] = os.path.join('../raw', track.fileName)
60 utils.add_tracks_to_json(json_file, bam_track, 'add_tracks') 62 utils.add_tracks_to_json(json_file, bam_track, 'add_tracks')
61 print "add bam track\n" 63 print "add bam track\n"
62 elif track.dataType == 'bigwig': 64 elif track['dataType'] == 'bigwig':
63 json_file = os.path.join(self.json, "trackList.json") 65 json_file = os.path.join(self.json, "trackList.json")
64 bigwig_track = dict() 66 bigwig_track = dict()
65 bigwig_track['label'] = 'rnaseq' 67 bigwig_track['label'] = 'rnaseq'
66 bigwig_track['key'] = 'RNA-Seq Coverage' 68 bigwig_track['key'] = 'RNA-Seq Coverage'
67 bigwig_track['urlTemplate'] = os.path.join('../raw', track.fileName) 69 bigwig_track['urlTemplate'] = os.path.join('../raw', track.fileName)
72 bigwig_track['style']['neg_color'] = '#005EFF' 74 bigwig_track['style']['neg_color'] = '#005EFF'
73 bigwig_track['style']['clip_marker_color'] = 'red' 75 bigwig_track['style']['clip_marker_color'] = 'red'
74 bigwig_track['style']['height'] = 100 76 bigwig_track['style']['height'] = 100
75 utils.add_tracks_to_json(json_file, bigwig_track, 'add_tracks') 77 utils.add_tracks_to_json(json_file, bigwig_track, 'add_tracks')
76 else: 78 else:
77 gff3_file = os.path.join(self.raw, track.fileName) 79 gff3_file = os.path.join(self.raw, track['fileName'])
78 label = track.fileName 80 label = track['fileName']
79 if track.dataType == 'bedSpliceJunctions' or track.dataType == 'gtf': 81 if track['dataType'] == 'bedSpliceJunctions' or track['dataType'] == 'gtf':
80 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]) 82 p = subprocess.Popen(['flatfile-to-json.pl', '--gff', gff3_file, '--trackType', 'CanvasFeatures', '--trackLabel', label, '--config', '{"glyph": "JBrowse/View/FeatureGlyph/Segments"}', '--out', self.json])
81 elif track.dataType == 'gff3-transcript': 83 elif track['dataType'] == 'gff3-transcript':
82 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]) 84 p = subprocess.Popen(['flatfile-to-json.pl', '--gff', gff3_file, '--trackType', 'MyPlugin/GenePred', '--trackLabel', label, '--config', '{"transcriptType": "transcript"}', '--out', self.json])
83 elif track.dataType == 'gff3': 85 elif track['dataType'] == 'gff3':
84 p = subprocess.Popen([os.path.join(self.tool_dir, 'flatfile-to-json.pl'), '--gff', gff3_file, '--trackType', 'MyPlugin/GenePred', '--trackLabel', label, '--out', self.json]) 86 p = subprocess.Popen(['flatfile-to-json.pl', '--gff', gff3_file, '--trackType', 'MyPlugin/GenePred', '--trackLabel', label, '--out', self.json])
85 else: 87 else:
86 p = subprocess.Popen([os.path.join(self.tool_dir, 'flatfile-to-json.pl'), '--gff', gff3_file, '--trackType', 'CanvasFeatures', '--trackLabel', label, '--out', self.json]) 88 p = subprocess.Popen(['flatfile-to-json.pl', '--gff', gff3_file, '--trackType', 'CanvasFeatures', '--trackLabel', label, '--out', self.json])
87 p.communicate() 89 p.communicate()
88 90
89 def indexName(self): 91 def indexName(self):
90 p = subprocess.Popen([os.path.join(self.tool_dir, 'generate-names.pl'), '-v', '--out', self.json]) 92 p = subprocess.Popen(['generate-names.pl', '-v', '--out', self.json])
91 p.communicate() 93 p.communicate()
92 print "finished name index \n" 94 print "finished name index \n"
93 95
94 def makeArchive(self): 96 def makeArchive(self):
95 filename = os.path.basename(self.out_path) 97 shutil.make_archive(self.out_path, 'zip', self.out_path)
96 shutil.make_archive(filename, 'tar', self.out_path) 98 shutil.rmtree(self.out_path)
97 99
98 100 def outHtml(self):
99 101 #htmloutput = tempfile.NamedTemporaryFile(self.outfile, suffix = '.html', bufsize=0, delete=False)
100 102 with open(self.outfile, 'w') as htmlfile:
103 htmlstr = 'The JBrowse Hub is created: <br>'
104 zipfiles = '<li><a href = "%s">Download</a></li>'
105 filedir_abs = os.path.abspath(self.outfile)
106 filedir = os.path.dirname(filedir_abs)
107 filedir = os.path.join(filedir, self.outfolder)
108 for root, dirs, files in os.walk(filedir):
109 for file in files:
110 if file.endswith('.zip'):
111 relative_directory = os.path.relpath(root, filedir)
112 relative_file_path = os.path.join(relative_directory, file)
113 htmlstr += zipfiles % relative_file_path
114 #htmlstr = htmlstr % zipfile
115 htmlfile.write(htmlstr)
101 116
117
118
119
120