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