Mercurial > repos > yating-l > jbrowsearchivecreator
comparison tracks/TrackStyles.py @ 25:31a41ce128cc draft
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit 691e5366893905d30943a3cb8cdfb6341f0f5362-dirty
| author | yating-l |
|---|---|
| date | Fri, 13 Oct 2017 12:44:31 -0400 |
| parents | |
| children | 7b955a58d8f2 |
comparison
equal
deleted
inserted
replaced
| 24:bb6fdccef474 | 25:31a41ce128cc |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 import os | |
| 3 import json | |
| 4 import logging | |
| 5 from mako.lookup import TemplateLookup | |
| 6 | |
| 7 class TrackStyles(object): | |
| 8 def __init__(self, tool_directory, species_folder, trackListFile, cssFolderName="css", cssFileName="custom_track_styles.css"): | |
| 9 self.logger = logging.getLogger(__name__) | |
| 10 self.tool_directory = tool_directory | |
| 11 self.species_folder = species_folder | |
| 12 self.trackList = trackListFile | |
| 13 self.cssFolderName = cssFolderName | |
| 14 self.cssFileName = cssFileName | |
| 15 self.cssFilePath = self._createCssFile() | |
| 16 self.cssTemplate = self._getCssTemplate() | |
| 17 self._addCssToTrackList() | |
| 18 | |
| 19 | |
| 20 def addCustomColor(self, feature_class_name, feature_color): | |
| 21 with open(self.cssFilePath, 'a+') as css: | |
| 22 htmlMakoRendered = self.cssTemplate.render( | |
| 23 label = feature_class_name, | |
| 24 color = feature_color | |
| 25 ) | |
| 26 css.write(htmlMakoRendered) | |
| 27 self.logger.debug("create customized track css class: cssFilePath= %s", self.cssFilePath) | |
| 28 | |
| 29 | |
| 30 def _createCssFile(self): | |
| 31 cssFolderPath = os.path.join(self.species_folder, self.cssFolderName) | |
| 32 cssFilePath = os.path.join(cssFolderPath, self.cssFileName) | |
| 33 if not os.path.exists(cssFilePath): | |
| 34 if not os.path.exists(cssFolderPath): | |
| 35 os.mkdir(cssFolderPath) | |
| 36 os.mknod(cssFilePath) | |
| 37 return cssFilePath | |
| 38 | |
| 39 def _getCssTemplate(self): | |
| 40 mylookup = TemplateLookup(directories=[os.path.join(self.tool_directory, 'templates')], | |
| 41 output_encoding='utf-8', encoding_errors='replace') | |
| 42 cssTemplate = mylookup.get_template("custom_track_styles.css") | |
| 43 return cssTemplate | |
| 44 | |
| 45 | |
| 46 def _addCssToTrackList(self): | |
| 47 with open(self.trackList, 'r+') as track: | |
| 48 data = json.load(track) | |
| 49 css_path = os.path.join('data', self.cssFolderName, self.cssFileName) | |
| 50 data['css'] = {'url': css_path} | |
| 51 json_string = json.dumps(data, indent=4, separators=(',', ': ')) | |
| 52 track.seek(0) | |
| 53 track.write(json_string) | |
| 54 track.truncate() | |
| 55 self.logger.debug("added customized css url to trackList.json") | |
| 56 | |
| 57 | |
| 58 |
