comparison jbrowse.py @ 5:ae9382cfb6ac draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse commit 3bbca939ca8a3298a3a2d6450abb04a96851e1ed
author iuc
date Sat, 25 Jun 2016 15:06:43 -0400
parents 7342f467507b
children ad4b9d7eae6a
comparison
equal deleted inserted replaced
4:d022577ff9cf 5:ae9382cfb6ac
77 var opacity = (score - ({min})) / (({max}) - ({min})); 77 var opacity = (score - ({min})) / (({max}) - ({min}));
78 opacity = Math.log10(opacity) + Math.log10({max}); 78 opacity = Math.log10(opacity) + Math.log10({max});
79 """, 79 """,
80 'blast': """ 80 'blast': """
81 var opacity = 0; 81 var opacity = 0;
82 if(score == 0.0) { 82 if(score == 0.0) {{
83 opacity = 1; 83 opacity = 1;
84 } else{ 84 }} else {{
85 opacity = (20 - Math.log10(score)) / 180; 85 opacity = (20 - Math.log10(score)) / 180;
86 } 86 }}
87 """ 87 """
88 } 88 }
89 89
90 BREWER_COLOUR_IDX = 0 90 BREWER_COLOUR_IDX = 0
91 BREWER_COLOUR_SCHEMES = [ 91 BREWER_COLOUR_SCHEMES = [
148 148
149 def hex_from_rgb(self, r, g, b): 149 def hex_from_rgb(self, r, g, b):
150 return '#%02x%02x%02x' % (r, g, b) 150 return '#%02x%02x%02x' % (r, g, b)
151 151
152 def _get_colours(self): 152 def _get_colours(self):
153 r, g, b = self.BREWER_COLOUR_SCHEMES[self.brewer_colour_idx] 153 r, g, b = self.BREWER_COLOUR_SCHEMES[self.brewer_colour_idx % len(self.BREWER_COLOUR_SCHEMES)]
154 self.brewer_colour_idx += 1 154 self.brewer_colour_idx += 1
155 return r, g, b 155 return r, g, b
156 156
157 def parse_colours(self, track, trackFormat, gff3=None): 157 def parse_colours(self, track, trackFormat, gff3=None):
158 # Wiggle tracks have a bicolor pallete 158 # Wiggle tracks have a bicolor pallete
204 elif trackFormat == 'gene_calls': 204 elif trackFormat == 'gene_calls':
205 # Default values, based on GFF3 spec 205 # Default values, based on GFF3 spec
206 min_val = 0 206 min_val = 0
207 max_val = 1000 207 max_val = 1000
208 # Get min/max and build a scoring function since JBrowse doesn't 208 # Get min/max and build a scoring function since JBrowse doesn't
209 if scales['type'] == 'automatic': 209 if scales['type'] == 'automatic' or scales['type'] == '__auto__':
210 min_val, max_val = self.min_max_gff(gff3) 210 min_val, max_val = self.min_max_gff(gff3)
211 else: 211 else:
212 min_val = scales['min'] 212 min_val = scales.get('min', 0)
213 max_val = scales['max'] 213 max_val = scales.get('max', 1000)
214 214
215 if scheme['color'] == '__auto__': 215 if scheme['color'] == '__auto__':
216 user_color = 'undefined' 216 user_color = 'undefined'
217 auto_color = "'%s'" % self.hex_from_rgb(*self._get_colours()) 217 auto_color = "'%s'" % self.hex_from_rgb(*self._get_colours())
218 elif scheme['color'].startswith('#'): 218 elif scheme['color'].startswith('#'):
314 for genome_path in self.genome_paths: 314 for genome_path in self.genome_paths:
315 self.subprocess_check_call([ 315 self.subprocess_check_call([
316 'perl', self._jbrowse_bin('prepare-refseqs.pl'), 316 'perl', self._jbrowse_bin('prepare-refseqs.pl'),
317 '--fasta', genome_path]) 317 '--fasta', genome_path])
318 318
319 # Generate name
320 # self.subprocess_check_call([
321 # 'perl', self._jbrowse_bin('generate-names.pl'),
322 # '--hashBits', '16'
323 # ])
324
319 def _add_json(self, json_data): 325 def _add_json(self, json_data):
320 if len(json_data.keys()) == 0: 326
321 return 327 cmd = [
322 328 'perl', self._jbrowse_bin('add-json.pl'),
323 tmp = tempfile.NamedTemporaryFile(delete=False) 329 json.dumps(json_data),
324 tmp.write(json.dumps(json_data)) 330 os.path.join('data', 'trackList.json')
325 tmp.close() 331 ]
326 cmd = ['perl', self._jbrowse_bin('add-track-json.pl'), tmp.name, 332 self.subprocess_check_call(cmd)
327 os.path.join('data', 'trackList.json')]
328 self.subprocess_check_call(cmd)
329 os.unlink(tmp.name)
330 333
331 def _add_track_json(self, json_data): 334 def _add_track_json(self, json_data):
332 if len(json_data.keys()) == 0: 335 if len(json_data.keys()) == 0:
333 return 336 return
334 337
351 return gff3_unrebased.name 354 return gff3_unrebased.name
352 355
353 def add_blastxml(self, data, trackData, blastOpts, **kwargs): 356 def add_blastxml(self, data, trackData, blastOpts, **kwargs):
354 gff3 = self._blastxml_to_gff3(data, min_gap=blastOpts['min_gap']) 357 gff3 = self._blastxml_to_gff3(data, min_gap=blastOpts['min_gap'])
355 358
356 if 'parent' in blastOpts: 359 if 'parent' in blastOpts and blastOpts['parent'] != 'None':
357 gff3_rebased = tempfile.NamedTemporaryFile(delete=False) 360 gff3_rebased = tempfile.NamedTemporaryFile(delete=False)
358 cmd = ['python', os.path.join(INSTALLED_TO, 'gff3_rebase.py')] 361 cmd = ['python', os.path.join(INSTALLED_TO, 'gff3_rebase.py')]
359 if blastOpts.get('protein', 'false') == 'true': 362 if blastOpts.get('protein', 'false') == 'true':
360 cmd.append('--protein2dna') 363 cmd.append('--protein2dna')
361 cmd.extend([os.path.realpath(blastOpts['parent']), gff3]) 364 cmd.extend([os.path.realpath(blastOpts['parent']), gff3])
455 cmd = [ 458 cmd = [
456 'perl', self._jbrowse_bin('flatfile-to-json.pl'), 459 'perl', self._jbrowse_bin('flatfile-to-json.pl'),
457 self.TN_TABLE.get(format, 'gff'), 460 self.TN_TABLE.get(format, 'gff'),
458 data, 461 data,
459 '--trackLabel', trackData['label'], 462 '--trackLabel', trackData['label'],
460 '--trackType', 'JBrowse/View/Track/CanvasFeatures', 463 # '--trackType', 'JBrowse/View/Track/CanvasFeatures',
461 '--key', trackData['key'] 464 '--key', trackData['key']
462 ] 465 ]
463 466
464 config = copy.copy(trackData) 467 config = copy.copy(trackData)
465 clientConfig = trackData['style'] 468 clientConfig = trackData['style']
468 if 'match' in gffOpts: 471 if 'match' in gffOpts:
469 config['glyph'] = 'JBrowse/View/FeatureGlyph/Segments' 472 config['glyph'] = 'JBrowse/View/FeatureGlyph/Segments'
470 cmd += ['--type', gffOpts['match']] 473 cmd += ['--type', gffOpts['match']]
471 474
472 cmd += ['--clientConfig', json.dumps(clientConfig), 475 cmd += ['--clientConfig', json.dumps(clientConfig),
476 ]
477
478 if 'trackType' in gffOpts:
479 cmd += [
480 '--trackType', gffOpts['trackType']
481 ]
482 else:
483 cmd += [
473 '--trackType', 'JBrowse/View/Track/CanvasFeatures' 484 '--trackType', 'JBrowse/View/Track/CanvasFeatures'
474 ] 485 ]
475 486
476 cmd.extend(['--config', json.dumps(config)]) 487 cmd.extend(['--config', json.dumps(config)])
477 488
478 self.subprocess_check_call(cmd) 489 self.subprocess_check_call(cmd)
479 490
503 for subkey in colourOptions['style']: 514 for subkey in colourOptions['style']:
504 outputTrackConfig['style'][subkey] = colourOptions['style'][subkey] 515 outputTrackConfig['style'][subkey] = colourOptions['style'][subkey]
505 else: 516 else:
506 outputTrackConfig[key] = colourOptions[key] 517 outputTrackConfig[key] = colourOptions[key]
507 518
519 # import pprint; pprint.pprint(track)
520 # import sys; sys.exit()
508 if dataset_ext in ('gff', 'gff3', 'bed'): 521 if dataset_ext in ('gff', 'gff3', 'bed'):
509 self.add_features(dataset_path, dataset_ext, outputTrackConfig, 522 self.add_features(dataset_path, dataset_ext, outputTrackConfig,
510 track['conf']['options']['gff']) 523 track['conf']['options']['gff'])
511 elif dataset_ext == 'bigwig': 524 elif dataset_ext == 'bigwig':
512 self.add_bigwig(dataset_path, outputTrackConfig, 525 self.add_bigwig(dataset_path, outputTrackConfig,
529 elif dataset_ext == 'blastxml': 542 elif dataset_ext == 'blastxml':
530 self.add_blastxml(dataset_path, outputTrackConfig, track['conf']['options']['blast']) 543 self.add_blastxml(dataset_path, outputTrackConfig, track['conf']['options']['blast'])
531 elif dataset_ext == 'vcf': 544 elif dataset_ext == 'vcf':
532 self.add_vcf(dataset_path, outputTrackConfig) 545 self.add_vcf(dataset_path, outputTrackConfig)
533 546
547 # Return non-human label for use in other fields
548 yield outputTrackConfig['label']
549
550 def add_final_data(self, data):
551 viz_data = {}
552 if len(data['visibility']['default_on']) > 0:
553 viz_data['defaultTracks'] = ','.join(data['visibility']['default_on'])
554
555 if len(data['visibility']['always']) > 0:
556 viz_data['alwaysOnTracks'] = ','.join(data['visibility']['always'])
557
558 if len(data['visibility']['force']) > 0:
559 viz_data['forceTracks'] = ','.join(data['visibility']['force'])
560
561 generalData = {}
562 if data['general']['aboutDescription'] is not None:
563 generalData['aboutThisBrowser'] = {'description': data['general']['aboutDescription'].strip()}
564
565 generalData['view'] = {
566 'trackPadding': data['general']['trackPadding']
567 }
568 generalData['shareLink'] = (data['general']['shareLink'] == 'true')
569 generalData['show_tracklist'] = (data['general']['show_tracklist'] == 'true')
570 generalData['show_nav'] = (data['general']['show_nav'] == 'true')
571 generalData['show_overview'] = (data['general']['show_overview'] == 'true')
572 generalData['show_menu'] = (data['general']['show_menu'] == 'true')
573 generalData['hideGenomeOptions'] = (data['general']['hideGenomeOptions'] == 'true')
574
575 viz_data.update(generalData)
576 self._add_json(viz_data)
577
534 def clone_jbrowse(self, jbrowse_dir, destination): 578 def clone_jbrowse(self, jbrowse_dir, destination):
535 """Clone a JBrowse directory into a destination directory. 579 """Clone a JBrowse directory into a destination directory.
536 """ 580 """
537 # JBrowse seems to have included some bad symlinks, cp ignores bad symlinks 581 # JBrowse seems to have included some bad symlinks, cp ignores bad symlinks
538 # unlike copytree 582 # unlike copytree
568 genomes=[os.path.realpath(x.text) for x in root.findall('metadata/genomes/genome')], 612 genomes=[os.path.realpath(x.text) for x in root.findall('metadata/genomes/genome')],
569 standalone=args.standalone, 613 standalone=args.standalone,
570 gencode=root.find('metadata/gencode').text 614 gencode=root.find('metadata/gencode').text
571 ) 615 )
572 616
617 extra_data = {
618 'visibility': {
619 'default_on': [],
620 'default_off': [],
621 'force': [],
622 'always': [],
623 },
624 'general': {
625 'defaultLocation': root.find('metadata/general/defaultLocation').text,
626 'trackPadding': int(root.find('metadata/general/trackPadding').text),
627 'shareLink': root.find('metadata/general/shareLink').text,
628 'aboutDescription': root.find('metadata/general/aboutDescription').text,
629 'show_tracklist': root.find('metadata/general/show_tracklist').text,
630 'show_nav': root.find('metadata/general/show_nav').text,
631 'show_overview': root.find('metadata/general/show_overview').text,
632 'show_menu': root.find('metadata/general/show_menu').text,
633 'hideGenomeOptions': root.find('metadata/general/hideGenomeOptions').text,
634 }
635 }
573 for track in root.findall('tracks/track'): 636 for track in root.findall('tracks/track'):
574 track_conf = {} 637 track_conf = {}
575 track_conf['trackfiles'] = [ 638 track_conf['trackfiles'] = [
576 (os.path.realpath(x.attrib['path']), x.attrib['ext'], x.attrib['label']) 639 (os.path.realpath(x.attrib['path']), x.attrib['ext'], x.attrib['label'])
577 for x in track.findall('files/trackFile') 640 for x in track.findall('files/trackFile')
584 track_conf['style'] = {t.tag: t.text for t in track.find('options/style')} 647 track_conf['style'] = {t.tag: t.text for t in track.find('options/style')}
585 except TypeError, te: 648 except TypeError, te:
586 track_conf['style'] = {} 649 track_conf['style'] = {}
587 pass 650 pass
588 track_conf['conf'] = etree_to_dict(track.find('options')) 651 track_conf['conf'] = etree_to_dict(track.find('options'))
589 jc.process_annotations(track_conf) 652 keys = jc.process_annotations(track_conf)
653
654
655 for key in keys:
656 extra_data['visibility'][track.attrib.get('visibility', 'default_off')].append(key)
657
658 jc.add_final_data(extra_data)