Mercurial > repos > yating-l > jbrowsearchivecreator
comparison TrackHub.py @ 0:804a93e87cc8 draft
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f22711ea7a464bdaf4d5aaea07f2eacf967aa66e-dirty
author | yating-l |
---|---|
date | Wed, 12 Apr 2017 17:41:55 -0400 |
parents | |
children | 25ad6770359e |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:804a93e87cc8 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 import os | |
4 import subprocess | |
5 import shutil | |
6 import json | |
7 import utils | |
8 | |
9 | |
10 class TrackHub: | |
11 def __init__(self, inputFiles, reference, outputDirect, tool_dir, genome, extra_files_path, metaData, jbrowse_host): | |
12 self.input_files = inputFiles.tracks | |
13 self.outfile = outputDirect | |
14 self.outfolder = extra_files_path | |
15 self.out_path = os.path.join(extra_files_path, genome) | |
16 self.reference = reference | |
17 self.tool_dir = tool_dir | |
18 self.metaData = metaData | |
19 self.raw = os.path.join(self.out_path, 'raw') | |
20 self.json = os.path.join(self.out_path, 'json') | |
21 self.jbrowse_host = jbrowse_host | |
22 try: | |
23 if os.path.exists(self.json): | |
24 shutil.rmtree(self.json) | |
25 os.makedirs(self.json) | |
26 except OSError as e: | |
27 print "Cannot create json folder error({0}): {1}".format(e.errno, e.strerror) | |
28 else: | |
29 print "Create jbrowse folder {}".format(self.out_path) | |
30 | |
31 def createHub(self): | |
32 self.prepareRefseq() | |
33 for input_file in self.input_files: | |
34 self.addTrack(input_file) | |
35 self.indexName() | |
36 slink = self.makeArchive() | |
37 self.outHtml(slink) | |
38 print "Success!\n" | |
39 | |
40 def prepareRefseq(self): | |
41 try: | |
42 #print os.path.join(self.tool_dir, 'prepare-refseqs.pl') + ", '--fasta', " + self.reference +", '--out', self.json])" | |
43 subprocess.call(['prepare-refseqs.pl', '--fasta', self.reference, '--out', self.json]) | |
44 except OSError as e: | |
45 print "Cannot prepare reference error({0}): {1}".format(e.errno, e.strerror) | |
46 #TODO: hard coded the bam and bigwig tracks. Need to allow users to customize the settings | |
47 def addTrack(self, track): | |
48 #print "false_path" , track['false_path'] | |
49 if track['false_path'] in self.metaData.keys(): | |
50 metadata = self.metaData[track['false_path']] | |
51 else: | |
52 metadata = {} | |
53 self.SetMetadata(track, metadata) | |
54 if track['dataType'] == 'bam': | |
55 self.Bam(track, metadata) | |
56 # print "add bam track\n" | |
57 elif track['dataType'] == 'bigwig': | |
58 self.BigWig(track, metadata) | |
59 else: | |
60 flat_file = os.path.join(self.raw, track['fileName']) | |
61 if track['dataType'] == 'bed': | |
62 subprocess.call(['flatfile-to-json.pl', '--bed', flat_file, '--trackType', metadata['type'], '--trackLabel', metadata['label'], '--Config', '{"category" : "%s"}' % metadata['category'], '--clientConfig', '{"color" : "%s"}' % metadata['color'], '--out', self.json]) | |
63 elif track['dataType'] == 'bedSpliceJunctions' or track['dataType'] == 'gtf' or track['dataType'] == 'blastxml': | |
64 subprocess.call(['flatfile-to-json.pl', '--gff', flat_file, '--trackType', metadata['type'], '--trackLabel', metadata['label'], '--Config', '{"glyph": "JBrowse/View/FeatureGlyph/Segments", "category" : "%s"}' % metadata['category'], '--clientConfig', '{"color" : "%s"}' % metadata['color'], '--out', self.json]) | |
65 elif track['dataType'] == 'gff3_transcript': | |
66 subprocess.call(['flatfile-to-json.pl', '--gff', flat_file, '--trackType', metadata['type'], '--trackLabel', metadata['label'], '--Config', '{"transcriptType": "transcript", "category" : "%s"}' % metadata['category'], '--clientConfig', '{"color" : "%s"}' % metadata['color'], '--out', self.json]) | |
67 else: | |
68 subprocess.call(['flatfile-to-json.pl', '--gff', flat_file, '--trackType', metadata['type'], '--trackLabel', metadata['label'], '--Config', '{"category" : "%s"}' % metadata['category'], '--clientConfig', '{"color" : "%s"}' % metadata['color'], '--out', self.json]) | |
69 | |
70 def indexName(self): | |
71 subprocess.call(['generate-names.pl', '-v', '--out', self.json]) | |
72 print "finished name index \n" | |
73 | |
74 def makeArchive(self): | |
75 shutil.make_archive(self.out_path, 'zip', self.out_path) | |
76 file_dir = os.path.abspath(self.outfile) | |
77 source_dir = os.path.dirname(file_dir) | |
78 folder_name = os.path.basename(self.outfolder) | |
79 source_name = os.path.basename(self.out_path) | |
80 source = os.path.join(source_dir, folder_name, source_name) | |
81 slink = source.replace('/', '_') | |
82 slink = os.path.join('/var/www/html/JBrowse-1.12.1/data', slink) | |
83 try: | |
84 if os.path.islink(slink): | |
85 os.unlink(slink) | |
86 except OSError as oserror: | |
87 print "Cannot create symlink to the data({0}): {1}".format(oserror.errno, oserror.strerror) | |
88 os.symlink(source, slink) | |
89 return slink | |
90 ''' | |
91 data_folder = '/gonramp/static/JBrowse-1.12.1/jbrowse_hub' | |
92 try: | |
93 if os.path.exists(data_folder): | |
94 if os.path.isdir(data_folder): | |
95 shutil.rmtree(data_folder) | |
96 else: | |
97 os.remove(data_folder) | |
98 except OSError as oserror: | |
99 print "Cannot create data folder({0}): {1}".format(oserror.errno, oserror.strerror) | |
100 shutil.copytree(self.out_path, data_folder) | |
101 subprocess.call(['chmod', '-R', 'o+rx', '/var/www/html/JBrowse-1.12.1/jbrowse_hub']) | |
102 shutil.rmtree(self.out_path) | |
103 ''' | |
104 | |
105 #TODO: this will list all zip files in the filedir and sub-dirs. worked in Galaxy but all list zip files in test-data when | |
106 #run it locally. May need modify | |
107 def outHtml(self, slink): | |
108 with open(self.outfile, 'w') as htmlfile: | |
109 htmlstr = 'The JBrowse Hub is created: <br>' | |
110 zipfiles = '<li><a href = "%s">Download</a></li>' | |
111 url = self.jbrowse_host + "/JBrowse-1.12.1/index.html?data=%s" | |
112 jbrowse_hub = '<li><a href = "%s" target="_blank">View JBrowse Hub</a></li>' % url | |
113 filedir_abs = os.path.abspath(self.outfile) | |
114 filedir = os.path.dirname(filedir_abs) | |
115 filedir = os.path.join(filedir, self.outfolder) | |
116 for root, dirs, files in os.walk(filedir): | |
117 for file in files: | |
118 if file.endswith('.zip'): | |
119 relative_directory = os.path.relpath(root, filedir) | |
120 relative_file_path = os.path.join(relative_directory, file) | |
121 htmlstr += zipfiles % relative_file_path | |
122 link_name = os.path.basename(slink) | |
123 relative_path = os.path.join('data', link_name + '/json') | |
124 htmlstr += jbrowse_hub % relative_path | |
125 htmlfile.write(htmlstr) | |
126 | |
127 def createTrackList(self): | |
128 trackList = os.path.join(self.json, "trackList.json") | |
129 if not os.path.exists(trackList): | |
130 os.mknod(trackList) | |
131 | |
132 def Bam(self, track, metadata): | |
133 #create trackList.json if not exist | |
134 self.createTrackList() | |
135 json_file = os.path.join(self.json, "trackList.json") | |
136 bam_track = dict() | |
137 bam_track['type'] = 'JBrowse/View/Track/Alignments2' | |
138 bam_track['storeClass'] = 'JBrowse/Store/SeqFeature/BAM' | |
139 bam_track['urlTemplate'] = os.path.join('../raw', track['fileName']) | |
140 bam_track['baiUrlTemplate'] = os.path.join('../raw', track['index']) | |
141 bam_track['label'] = metadata['label'] | |
142 bam_track['category'] = metadata['category'] | |
143 bam_track = json.dumps(bam_track) | |
144 #Use add-track-json.pl to add bam track to json file | |
145 new_track = subprocess.Popen(['echo', bam_track], stdout=subprocess.PIPE) | |
146 subprocess.call(['add-track-json.pl', json_file], stdin=new_track.stdout) | |
147 | |
148 def BigWig(self, track, metadata): | |
149 #create trackList.json if not exist | |
150 self.createTrackList() | |
151 json_file = os.path.join(self.json, "trackList.json") | |
152 bigwig_track = dict() | |
153 bigwig_track['urlTemplate'] = os.path.join('../raw', track['fileName']) | |
154 bigwig_track['type'] = 'JBrowse/View/Track/Wiggle/XYPlot' | |
155 bigwig_track['storeClass'] = 'JBrowse/Store/SeqFeature/BigWig' | |
156 bigwig_track['label'] = metadata['label'] | |
157 bigwig_track['style'] = metadata['style'] | |
158 bigwig_track['category'] = metadata['category'] | |
159 bigwig_track = json.dumps(bigwig_track) | |
160 #Use add-track-json.pl to add bigwig track to json file | |
161 new_track = subprocess.Popen(['echo', bigwig_track], stdout=subprocess.PIPE) | |
162 #output = new_track.communicate()[0] | |
163 subprocess.call(['add-track-json.pl', json_file], stdin=new_track.stdout) | |
164 | |
165 #If the metadata is not set, use the default value | |
166 def SetMetadata(self, track, metadata): | |
167 if 'label' not in metadata.keys() or metadata['label'] == '': | |
168 metadata['label'] = track['fileName'] | |
169 if 'color' not in metadata.keys() or metadata['color'] == '': | |
170 metadata['color'] = "#daa520" | |
171 if track['dataType'] == 'bigwig': | |
172 if 'style' not in metadata.keys(): | |
173 metadata['style'] = {} | |
174 if 'pos_color' not in metadata['style'] or metadata['style']['pos_color'] == '': | |
175 metadata['style']['pos_color'] = "#FFA600" | |
176 if 'neg_color' not in metadata['style'] or metadata['style']['neg_color'] == '': | |
177 metadata['style']['neg_color'] = "#005EFF" | |
178 if 'category' not in metadata.keys() or metadata['category'] == '': | |
179 metadata['category'] = "Default group" | |
180 if track['dataType'] == 'blastxml': | |
181 metadata['type'] = "G-OnRamp_plugin/BlastAlignment" | |
182 elif track['dataType'] == 'gff3_transcript' or track['dataType'] == 'gff3_mrna': | |
183 metadata['type'] = "G-OnRamp_plugin/GenePred" | |
184 else: | |
185 metadata['type'] = "CanvasFeatures" | |
186 | |
187 | |
188 | |
189 | |
190 | |
191 | |
192 |