Mercurial > repos > yating-l > jbrowsearchivecreator
comparison tracks/TrackStyles.py @ 6:237707a6b74d draft
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a500f7ab2119cc5faaf80393bd87428389d06880-dirty
| author | yating-l |
|---|---|
| date | Thu, 15 Feb 2018 17:05:05 -0500 |
| parents | |
| children | 62dee5369e80 |
comparison
equal
deleted
inserted
replaced
| 5:e762f4b9e4bd | 6:237707a6b74d |
|---|---|
| 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 os.chmod(cssFilePath, 0o755) | |
| 38 return cssFilePath | |
| 39 | |
| 40 def _getCssTemplate(self): | |
| 41 mylookup = TemplateLookup(directories=[os.path.join(self.tool_directory, 'templates')], | |
| 42 output_encoding='utf-8', encoding_errors='replace') | |
| 43 cssTemplate = mylookup.get_template("custom_track_styles.css") | |
| 44 return cssTemplate | |
| 45 | |
| 46 | |
| 47 def _addCssToTrackList(self): | |
| 48 with open(self.trackList, 'r+') as track: | |
| 49 data = json.load(track) | |
| 50 css_path = os.path.join('data', self.cssFolderName, self.cssFileName) | |
| 51 data['css'] = {'url': css_path} | |
| 52 json_string = json.dumps(data, indent=4, separators=(',', ': ')) | |
| 53 track.seek(0) | |
| 54 track.write(json_string) | |
| 55 track.truncate() | |
| 56 self.logger.debug("added customized css url to trackList.json") | |
| 57 | |
| 58 | |
| 59 |
