Mercurial > repos > yating-l > jbrowse_hub
comparison TrackHub.py @ 50:3e5b6f6a9abe draft
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 6391cdb975d14b5a7060fa1665a8a575797a0eaa-dirty
author | yating-l |
---|---|
date | Thu, 30 Mar 2017 16:03:16 -0400 |
parents | 193e94a43f0f |
children | acedf82ea46c |
comparison
equal
deleted
inserted
replaced
49:193e94a43f0f | 50:3e5b6f6a9abe |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 import os | 3 import os |
4 import subprocess | 4 import subprocess |
5 import shutil | 5 import shutil |
6 import json | |
6 import utils | 7 import utils |
7 | 8 |
8 | 9 |
9 class TrackHub: | 10 class TrackHub: |
10 def __init__(self, inputFiles, reference, outputDirect, tool_dir, genome, extra_files_path, metaData): | 11 def __init__(self, inputFiles, reference, outputDirect, tool_dir, genome, extra_files_path, metaData): |
36 print "Success!\n" | 37 print "Success!\n" |
37 | 38 |
38 def prepareRefseq(self): | 39 def prepareRefseq(self): |
39 try: | 40 try: |
40 #print os.path.join(self.tool_dir, 'prepare-refseqs.pl') + ", '--fasta', " + self.reference +", '--out', self.json])" | 41 #print os.path.join(self.tool_dir, 'prepare-refseqs.pl') + ", '--fasta', " + self.reference +", '--out', self.json])" |
41 p = subprocess.Popen(['prepare-refseqs.pl', '--fasta', self.reference, '--out', self.json]) | 42 subprocess.call(['prepare-refseqs.pl', '--fasta', self.reference, '--out', self.json]) |
42 # Wait for process to terminate. | |
43 p.communicate() | |
44 except OSError as e: | 43 except OSError as e: |
45 print "Cannot prepare reference error({0}): {1}".format(e.errno, e.strerror) | 44 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 | 45 #TODO: hard coded the bam and bigwig tracks. Need to allow users to customize the settings |
47 def addTrack(self, track): | 46 def addTrack(self, track): |
48 #print "false_path" , track['false_path'] | 47 #print "false_path" , track['false_path'] |
55 self.Bam(track, metadata) | 54 self.Bam(track, metadata) |
56 # print "add bam track\n" | 55 # print "add bam track\n" |
57 elif track['dataType'] == 'bigwig': | 56 elif track['dataType'] == 'bigwig': |
58 self.BigWig(track, metadata) | 57 self.BigWig(track, metadata) |
59 else: | 58 else: |
60 gff3_file = os.path.join(self.raw, track['fileName']) | 59 flat_file = os.path.join(self.raw, track['fileName']) |
61 if track['dataType'] == 'bedSpliceJunctions' or track['dataType'] == 'gtf' or track['dataType'] == 'blastxml': | 60 if track['dataType'] == 'bed': |
62 p = subprocess.Popen(['flatfile-to-json.pl', '--gff', gff3_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]) | 61 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]) |
62 elif track['dataType'] == 'bedSpliceJunctions' or track['dataType'] == 'gtf' or track['dataType'] == 'blastxml': | |
63 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]) | |
63 elif track['dataType'] == 'gff3_transcript': | 64 elif track['dataType'] == 'gff3_transcript': |
64 p = subprocess.Popen(['flatfile-to-json.pl', '--gff', gff3_file, '--trackType', metadata['type'], '--trackLabel', metadata['label'], '--Config', '{"transcriptType": "transcript", "category" : "%s"}' % metadata['category'], '--clientConfig', '{"color" : "%s"}' % metadata['color'], '--out', self.json]) | 65 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]) |
65 else: | 66 else: |
66 p = subprocess.Popen(['flatfile-to-json.pl', '--gff', gff3_file, '--trackType', metadata['type'], '--trackLabel', metadata['label'], '--Config', '{"category" : "%s"}' % metadata['category'], '--clientConfig', '{"color" : "%s"}' % metadata['color'], '--out', self.json]) | 67 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]) |
67 p.communicate() | |
68 | 68 |
69 def indexName(self): | 69 def indexName(self): |
70 p = subprocess.Popen(['generate-names.pl', '-v', '--out', self.json]) | 70 subprocess.call(['generate-names.pl', '-v', '--out', self.json]) |
71 p.communicate() | |
72 print "finished name index \n" | 71 print "finished name index \n" |
73 | 72 |
74 def makeArchive(self): | 73 def makeArchive(self): |
75 shutil.make_archive(self.out_path, 'zip', self.out_path) | 74 shutil.make_archive(self.out_path, 'zip', self.out_path) |
76 data_folder = '/var/www/html/JBrowse-1.12.1/jbrowse_hub' | 75 data_folder = '/var/www/html/JBrowse-1.12.1/jbrowse_hub' |
81 else: | 80 else: |
82 os.remove(data_folder) | 81 os.remove(data_folder) |
83 except OSError as oserror: | 82 except OSError as oserror: |
84 print "Cannot create data folder({0}): {1}".format(oserror.errno, oserror.strerror) | 83 print "Cannot create data folder({0}): {1}".format(oserror.errno, oserror.strerror) |
85 shutil.copytree(self.out_path, data_folder) | 84 shutil.copytree(self.out_path, data_folder) |
86 p = subprocess.Popen(['chmod', '-R', 'o+rx', '/var/www/html/JBrowse-1.12.1/jbrowse_hub']) | 85 subprocess.call(['chmod', '-R', 'o+rx', '/var/www/html/JBrowse-1.12.1/jbrowse_hub']) |
87 p.communicate() | |
88 shutil.rmtree(self.out_path) | 86 shutil.rmtree(self.out_path) |
89 | 87 |
90 #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 | 88 #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 |
91 #run it locally. May need modify | 89 #run it locally. May need modify |
92 def outHtml(self): | 90 def outHtml(self): |
93 with open(self.outfile, 'w') as htmlfile: | 91 with open(self.outfile, 'w') as htmlfile: |
94 htmlstr = 'The JBrowse Hub is created: <br>' | 92 htmlstr = 'The JBrowse Hub is created: <br>' |
95 zipfiles = '<li><a href = "%s">Download</a></li>' | 93 zipfiles = '<li><a href = "%s">Download</a></li>' |
96 jbrowse_hub = '<li><a href = "http://192.168.56.11/JBrowse-1.12.1/index.html?data=jbrowse_hub/json" target="_blank">View JBrowse Hub</a></li>' | 94 jbrowse_hub = '<li><a href = "/static/JBrowse-1.12.1/index.html?data=jbrowse_hub/json" target="_blank">View JBrowse Hub</a></li>' |
97 filedir_abs = os.path.abspath(self.outfile) | 95 filedir_abs = os.path.abspath(self.outfile) |
98 filedir = os.path.dirname(filedir_abs) | 96 filedir = os.path.dirname(filedir_abs) |
99 filedir = os.path.join(filedir, self.outfolder) | 97 filedir = os.path.join(filedir, self.outfolder) |
100 for root, dirs, files in os.walk(filedir): | 98 for root, dirs, files in os.walk(filedir): |
101 for file in files: | 99 for file in files: |
120 bam_track['storeClass'] = 'JBrowse/Store/SeqFeature/BAM' | 118 bam_track['storeClass'] = 'JBrowse/Store/SeqFeature/BAM' |
121 bam_track['urlTemplate'] = os.path.join('../raw', track['fileName']) | 119 bam_track['urlTemplate'] = os.path.join('../raw', track['fileName']) |
122 bam_track['baiUrlTemplate'] = os.path.join('../raw', track['index']) | 120 bam_track['baiUrlTemplate'] = os.path.join('../raw', track['index']) |
123 bam_track['label'] = metadata['label'] | 121 bam_track['label'] = metadata['label'] |
124 bam_track['category'] = metadata['category'] | 122 bam_track['category'] = metadata['category'] |
125 utils.add_tracks_to_json(json_file, bam_track, 'add_tracks') | 123 bam_track = json.dumps(bam_track) |
126 | 124 #Use add-track-json.pl to add bam track to json file |
125 new_track = subprocess.Popen(['echo', bam_track], stdout=subprocess.PIPE) | |
126 subprocess.call(['add-track-json.pl', json_file], stdin=new_track.stdout) | |
127 | |
127 def BigWig(self, track, metadata): | 128 def BigWig(self, track, metadata): |
128 #create trackList.json if not exist | 129 #create trackList.json if not exist |
129 self.createTrackList() | 130 self.createTrackList() |
130 json_file = os.path.join(self.json, "trackList.json") | 131 json_file = os.path.join(self.json, "trackList.json") |
131 bigwig_track = dict() | 132 bigwig_track = dict() |
133 bigwig_track['type'] = 'JBrowse/View/Track/Wiggle/XYPlot' | 134 bigwig_track['type'] = 'JBrowse/View/Track/Wiggle/XYPlot' |
134 bigwig_track['storeClass'] = 'JBrowse/Store/SeqFeature/BigWig' | 135 bigwig_track['storeClass'] = 'JBrowse/Store/SeqFeature/BigWig' |
135 bigwig_track['label'] = metadata['label'] | 136 bigwig_track['label'] = metadata['label'] |
136 bigwig_track['style'] = metadata['style'] | 137 bigwig_track['style'] = metadata['style'] |
137 bigwig_track['category'] = metadata['category'] | 138 bigwig_track['category'] = metadata['category'] |
138 utils.add_tracks_to_json(json_file, bigwig_track, 'add_tracks') | 139 bigwig_track = json.dumps(bigwig_track) |
140 #Use add-track-json.pl to add bigwig track to json file | |
141 new_track = subprocess.Popen(['echo', bigwig_track], stdout=subprocess.PIPE) | |
142 #output = new_track.communicate()[0] | |
143 subprocess.call(['add-track-json.pl', json_file], stdin=new_track.stdout) | |
139 | 144 |
140 #If the metadata is not set, use the default value | 145 #If the metadata is not set, use the default value |
141 def SetMetadata(self, track, metadata): | 146 def SetMetadata(self, track, metadata): |
142 if 'label' not in metadata.keys() or metadata['label'] == '': | 147 if 'label' not in metadata.keys() or metadata['label'] == '': |
143 metadata['label'] = track['fileName'] | 148 metadata['label'] = track['fileName'] |
145 metadata['color'] = "#daa520" | 150 metadata['color'] = "#daa520" |
146 if track['dataType'] == 'bigwig': | 151 if track['dataType'] == 'bigwig': |
147 if 'style' not in metadata.keys(): | 152 if 'style' not in metadata.keys(): |
148 metadata['style'] = {} | 153 metadata['style'] = {} |
149 if 'pos_color' not in metadata['style'] or metadata['style']['pos_color'] == '': | 154 if 'pos_color' not in metadata['style'] or metadata['style']['pos_color'] == '': |
150 metadata['pos_color'] = "#FFA600" | 155 metadata['style']['pos_color'] = "#FFA600" |
151 if 'neg_color' not in metadata['style'] or metadata['style']['neg_color'] == '': | 156 if 'neg_color' not in metadata['style'] or metadata['style']['neg_color'] == '': |
152 metadata['neg_color'] = "#005EFF" | 157 metadata['style']['neg_color'] = "#005EFF" |
153 if 'category' not in metadata.keys() or metadata['category'] == '': | 158 if 'category' not in metadata.keys() or metadata['category'] == '': |
154 metadata['category'] = "Default group" | 159 metadata['category'] = "Default group" |
155 if track['dataType'] == 'blastxml': | 160 if track['dataType'] == 'blastxml': |
156 metadata['type'] = "G-OnRamp_plugin/BlastAlignment" | 161 metadata['type'] = "G-OnRamp_plugin/BlastAlignment" |
157 elif track['dataType'] == 'gff3_transcript' or track['dataType'] == 'gff3_mrna': | 162 elif track['dataType'] == 'gff3_transcript' or track['dataType'] == 'gff3_mrna': |