comparison TrackHub.py @ 1:fb5e60d4d18a draft

planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 64cfc08088d11f6818c1b4e5514ef9e67969eaff-dirty
author rmarenco
date Wed, 13 Jul 2016 13:36:37 -0400
parents
children a030b8023882
comparison
equal deleted inserted replaced
0:0f3bc17e5ede 1:fb5e60d4d18a
1 #!/usr/bin/python
2 # -*- coding: utf8 -*-
3
4 import os
5 import zipfile
6
7 from mako.lookup import TemplateLookup
8
9
10 class TrackHub(object):
11 """docstring for TrackHub"""
12
13 def __init__(self, inputFastaFile, outputFile, extra_files_path, tool_directory):
14 super(TrackHub, self).__init__()
15
16 self.rootAssemblyHub = None
17 self.mySpecieFolderPath = None
18 self.tool_directory = tool_directory
19
20 # TODO: Modify according to the files passed in parameter
21 mylookup = TemplateLookup(directories=[os.path.join(tool_directory, 'templates/trackDb')],
22 output_encoding='utf-8', encoding_errors='replace')
23 self.trackDbTemplate = mylookup.get_template("layout.txt")
24
25 self.extra_files_path = extra_files_path
26 self.outputFile = outputFile
27
28 inputFastaFile = open(inputFastaFile, 'r')
29 self.outputZip = zipfile.ZipFile(os.path.join(extra_files_path, 'myHub.zip'), 'w')
30
31 # Create the structure of the Assembly Hub
32 # TODO: Merge the following processing into a function as it is also used in twoBitCreator
33 baseNameFasta = os.path.basename(inputFastaFile.name)
34 suffixTwoBit, extensionTwoBit = os.path.splitext(baseNameFasta)
35 self.twoBitName = suffixTwoBit + '.2bit'
36
37 self.rootAssemblyHub = self.__createAssemblyHub__(toolDirectory=tool_directory,
38 extra_files_path=extra_files_path)
39
40 def createZip(self):
41 for root, dirs, files in os.walk(self.rootAssemblyHub):
42 # Get all files and construct the dir at the same time
43 for file in files:
44 self.outputZip.write(os.path.join(root, file))
45
46 self.outputZip.close()
47
48 def addTrack(self, trackDbObject=None):
49 # Create the trackDb.txt file in the specie folder, if not exists
50 # Else append the new track
51 trackDbTxtFilePath = os.path.join(self.mySpecieFolderPath, 'trackDb.txt')
52
53 # Append to trackDbTxtFilePath the trackDbTemplate populate with the newTrack object
54 with open(trackDbTxtFilePath, 'a+') as trackDbFile:
55 trackDbs = [trackDbObject]
56 htmlMakoRendered = self.trackDbTemplate.render(
57 trackDbs=trackDbs
58 )
59 trackDbFile.write(htmlMakoRendered)
60
61 def terminate(self):
62 # Just a test to output a simple HTML
63 with open(self.outputFile, 'w') as htmlOutput:
64 htmlOutput.write('<html>')
65 htmlOutput.write('<body>')
66 htmlOutput.write('<p>')
67 htmlOutput.write('The following generated by Hub Archive Creator:')
68 htmlOutput.write('</p>')
69 htmlOutput.write('<ul>')
70 for root, dirs, files in os.walk(self.extra_files_path):
71 for file in files:
72 relDir = os.path.relpath(root, self.extra_files_path)
73 htmlOutput.write(str.format('<li><a href="{0}">{1}</a></li>', os.path.join(relDir, file),
74 os.path.join(relDir, file)))
75 htmlOutput.write('<ul>')
76 htmlOutput.write('</body>')
77 htmlOutput.write('</html>')
78
79 def __createAssemblyHub__(self, toolDirectory, extra_files_path):
80 # TODO: Manage to put every fill Function in a file dedicated for reading reasons
81 # Create the root directory
82 myHubPath = os.path.join(extra_files_path, "myHub")
83 if not os.path.exists(myHubPath):
84 os.makedirs(myHubPath)
85
86 # Add the genomes.txt file
87 genomesTxtFilePath = os.path.join(myHubPath, 'genomes.txt')
88 self.__fillGenomesTxt__(genomesTxtFilePath, toolDirectory)
89
90 # Add the hub.txt file
91 hubTxtFilePath = os.path.join(myHubPath, 'hub.txt')
92 self.__fillHubTxt__(hubTxtFilePath, toolDirectory)
93
94 # Add the hub.html file
95 # TODO: Change the name and get it depending on the specie
96 hubHtmlFilePath = os.path.join(myHubPath, 'dbia.html')
97 self.__fillHubHtmlFile__(hubHtmlFilePath, toolDirectory)
98
99 # Create the specie folder
100 # TODO: Generate the name depending on the specie
101 mySpecieFolderPath = os.path.join(myHubPath, "dbia3")
102 if not os.path.exists(mySpecieFolderPath):
103 os.makedirs(mySpecieFolderPath)
104 self.mySpecieFolderPath = mySpecieFolderPath
105
106 # Create the description html file in the specie folder
107 descriptionHtmlFilePath = os.path.join(mySpecieFolderPath, 'description.html')
108 self.__fillDescriptionHtmlFile__(descriptionHtmlFilePath, toolDirectory)
109
110 # Create the file groups.txt
111 # TODO: If not inputs for this, do no create the file
112 groupsTxtFilePath = os.path.join(mySpecieFolderPath, 'groups.txt')
113 self.__fillGroupsTxtFile__(groupsTxtFilePath, toolDirectory)
114
115 # Create the folder tracks into the specie folder
116 tracksFolderPath = os.path.join(mySpecieFolderPath, "tracks")
117 if not os.path.exists(tracksFolderPath):
118 os.makedirs(tracksFolderPath)
119
120 return myHubPath
121
122 def __fillGenomesTxt__(self, genomesTxtFilePath, toolDirectory):
123 # TODO: Think about the inputs and outputs
124 # TODO: Manage the template of this file
125 # renderer = pystache.Renderer(search_dirs="templates/genomesAssembly")
126 pathTemplate = os.path.join(toolDirectory, 'templates/genomesAssembly')
127 mylookup = TemplateLookup(directories=[pathTemplate], output_encoding='utf-8', encoding_errors='replace')
128 mytemplate = mylookup.get_template("layout.txt")
129 with open(genomesTxtFilePath, 'w') as genomesTxtFile:
130 # Write the content of the file genomes.txt
131 twoBitPath = os.path.join('dbia3/', self.twoBitName)
132 htmlMakoRendered = mytemplate.render(
133 genomeName="dbia3",
134 trackDbPath="dbia3/trackDb.txt",
135 groupsPath="dbia3/groups.txt",
136 genomeDescription="March 2013 Drosophilia biarmipes unplaced genomic scaffold",
137 twoBitPath=twoBitPath,
138 organismName="Drosophilia biarmipes",
139 defaultPosition="contig1",
140 orderKey="4500",
141 scientificName="Drosophilia biarmipes",
142 pathAssemblyHtmlDescription="dbia3/description.html"
143 )
144 genomesTxtFile.write(htmlMakoRendered)
145
146 def __fillHubTxt__(self, hubTxtFilePath, toolDirectory):
147 # TODO: Think about the inputs and outputs
148 # TODO: Manage the template of this file
149 mylookup = TemplateLookup(directories=[os.path.join(toolDirectory, 'templates/hubTxt')],
150 output_encoding='utf-8', encoding_errors='replace')
151 mytemplate = mylookup.get_template('layout.txt')
152 with open(hubTxtFilePath, 'w') as genomesTxtFile:
153 # Write the content of the file genomes.txt
154 htmlMakoRendered = mytemplate.render(
155 hubName='dbiaOnly',
156 shortLabel='dbia',
157 longLabel='This hub only contains dbia with the gene predictions',
158 genomesFile='genomes.txt',
159 email='rmarenco@gwu.edu',
160 descriptionUrl='dbia.html'
161 )
162 genomesTxtFile.write(htmlMakoRendered)
163
164 def __fillHubHtmlFile__(self, hubHtmlFilePath, toolDirectory):
165 # TODO: Think about the inputs and outputs
166 # TODO: Manage the template of this file
167 # renderer = pystache.Renderer(search_dirs="templates/hubDescription")
168 # t = Template(templates.hubDescription.layout.html)
169 mylookup = TemplateLookup(directories=[os.path.join(toolDirectory, 'templates/hubDescription')],
170 output_encoding='utf-8', encoding_errors='replace')
171 mytemplate = mylookup.get_template("layout.txt")
172 with open(hubHtmlFilePath, 'w') as hubHtmlFile:
173 # Write the content of the file genomes.txt
174 # htmlPystached = renderer.render_name(
175 # "layout",
176 # {'specie': 'Dbia',
177 # 'toolUsed': 'Augustus',
178 # 'ncbiSpecieUrl': 'http://www.ncbi.nlm.nih.gov/genome/3499',
179 # 'genomeID': '3499',
180 # 'SpecieFullName': 'Drosophila biarmipes'})
181 htmlMakoRendered = mytemplate.render(
182 specie='Dbia',
183 toolUsed='Augustus',
184 ncbiSpecieUrl='http://www.ncbi.nlm.nih.gov/genome/3499',
185 genomeID='3499',
186 specieFullName='Drosophila biarmipes'
187 )
188 # hubHtmlFile.write(htmlPystached)
189 hubHtmlFile.write(htmlMakoRendered)
190
191 def __fillDescriptionHtmlFile__(self, descriptionHtmlFilePath, toolDirectory):
192 # TODO: Think about the inputs and outputs
193 # TODO: Manage the template of this file
194 mylookup = TemplateLookup(directories=[os.path.join(toolDirectory, 'templates/specieDescription')],
195 output_encoding='utf-8', encoding_errors='replace')
196 mytemplate = mylookup.get_template("layout.txt")
197 with open(descriptionHtmlFilePath, 'w') as descriptionHtmlFile:
198 # Write the content of the file genomes.txt
199 htmlMakoRendered = mytemplate.render(
200 specieDescription='This is the description of the dbia',
201 )
202 descriptionHtmlFile.write(htmlMakoRendered)
203
204 def __fillGroupsTxtFile__(self, groupsTxtFilePath, toolDirectory):
205 # TODO: Reenable this function at some point
206 mylookup = TemplateLookup(directories=[os.path.join(toolDirectory, 'templates/groupsTxt')],
207 output_encoding='utf-8', encoding_errors='replace')
208 mytemplate = mylookup.get_template("layout.txt")
209 with open(groupsTxtFilePath, 'w') as groupsTxtFile:
210 # Write the content of groups.txt
211 # groupsTxtFile.write('name map')
212 htmlMakoRendered = mytemplate.render(
213 mapName='map',
214 labelMapping='Mapping',
215 prioriy='2',
216 isClosed='0'
217 )
218 # groupsTxtFile.write(htmlMakoRendered)