annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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)
31a41ce128cc planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit 691e5366893905d30943a3cb8cdfb6341f0f5362-dirty
yating-l
parents:
diff changeset
37 return cssFilePath
31a41ce128cc planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit 691e5366893905d30943a3cb8cdfb6341f0f5362-dirty
yating-l
parents:
diff changeset
38
31a41ce128cc planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit 691e5366893905d30943a3cb8cdfb6341f0f5362-dirty
yating-l
parents:
diff changeset
39 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
40 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
41 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
42 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
43 return cssTemplate
31a41ce128cc planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit 691e5366893905d30943a3cb8cdfb6341f0f5362-dirty
yating-l
parents:
diff changeset
44
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 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
47 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
48 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
49 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
50 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
51 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
52 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
53 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
54 track.truncate()
31a41ce128cc planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit 691e5366893905d30943a3cb8cdfb6341f0f5362-dirty
yating-l
parents:
diff changeset
55 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
56
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