annotate jbrowse2/jbrowse2_jbrowsenotjson.py @ 10:0db895a99532 draft default tip

Uploaded
author fubar
date Fri, 05 Jan 2024 22:26:16 +0000
parents 88b9b105c09b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1 #!/usr/bin/env python
88b9b105c09b Uploaded
fubar
parents:
diff changeset
2 # change to accumulating all configuration for config.json based on the default from the clone
88b9b105c09b Uploaded
fubar
parents:
diff changeset
3 import argparse
88b9b105c09b Uploaded
fubar
parents:
diff changeset
4 import datetime
88b9b105c09b Uploaded
fubar
parents:
diff changeset
5 import hashlib
88b9b105c09b Uploaded
fubar
parents:
diff changeset
6 import json
88b9b105c09b Uploaded
fubar
parents:
diff changeset
7 import logging
88b9b105c09b Uploaded
fubar
parents:
diff changeset
8 import os
88b9b105c09b Uploaded
fubar
parents:
diff changeset
9 import shutil
88b9b105c09b Uploaded
fubar
parents:
diff changeset
10 import subprocess
88b9b105c09b Uploaded
fubar
parents:
diff changeset
11 import tempfile
88b9b105c09b Uploaded
fubar
parents:
diff changeset
12 import xml.etree.ElementTree as ET
88b9b105c09b Uploaded
fubar
parents:
diff changeset
13 from collections import defaultdict
88b9b105c09b Uploaded
fubar
parents:
diff changeset
14
88b9b105c09b Uploaded
fubar
parents:
diff changeset
15 logging.basicConfig(level=logging.INFO)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
16 log = logging.getLogger("jbrowse")
88b9b105c09b Uploaded
fubar
parents:
diff changeset
17 TODAY = datetime.datetime.now().strftime("%Y-%m-%d")
88b9b105c09b Uploaded
fubar
parents:
diff changeset
18 GALAXY_INFRASTRUCTURE_URL = None
88b9b105c09b Uploaded
fubar
parents:
diff changeset
19 mapped_chars = {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
20 ">": "__gt__",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
21 "<": "__lt__",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
22 "'": "__sq__",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
23 '"': "__dq__",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
24 "[": "__ob__",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
25 "]": "__cb__",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
26 "{": "__oc__",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
27 "}": "__cc__",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
28 "@": "__at__",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
29 "#": "__pd__",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
30 "": "__cn__",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
31 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
32
88b9b105c09b Uploaded
fubar
parents:
diff changeset
33
88b9b105c09b Uploaded
fubar
parents:
diff changeset
34 def etree_to_dict(t):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
35 if t is None:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
36 return {}
88b9b105c09b Uploaded
fubar
parents:
diff changeset
37
88b9b105c09b Uploaded
fubar
parents:
diff changeset
38 d = {t.tag: {} if t.attrib else None}
88b9b105c09b Uploaded
fubar
parents:
diff changeset
39 children = list(t)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
40 if children:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
41 dd = defaultdict(list)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
42 for dc in map(etree_to_dict, children):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
43 for k, v in dc.items():
88b9b105c09b Uploaded
fubar
parents:
diff changeset
44 dd[k].append(v)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
45 d = {t.tag: {k: v[0] if len(v) == 1 else v for k, v in dd.items()}}
88b9b105c09b Uploaded
fubar
parents:
diff changeset
46 if t.attrib:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
47 d[t.tag].update(("@" + k, v) for k, v in t.attrib.items())
88b9b105c09b Uploaded
fubar
parents:
diff changeset
48 if t.text:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
49 text = t.text.strip()
88b9b105c09b Uploaded
fubar
parents:
diff changeset
50 if children or t.attrib:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
51 if text:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
52 d[t.tag]["#text"] = text
88b9b105c09b Uploaded
fubar
parents:
diff changeset
53 else:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
54 d[t.tag] = text
88b9b105c09b Uploaded
fubar
parents:
diff changeset
55 return d
88b9b105c09b Uploaded
fubar
parents:
diff changeset
56
88b9b105c09b Uploaded
fubar
parents:
diff changeset
57
88b9b105c09b Uploaded
fubar
parents:
diff changeset
58 INSTALLED_TO = os.path.dirname(os.path.realpath(__file__))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
59
88b9b105c09b Uploaded
fubar
parents:
diff changeset
60
88b9b105c09b Uploaded
fubar
parents:
diff changeset
61 def metadata_from_node(node):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
62 metadata = {}
88b9b105c09b Uploaded
fubar
parents:
diff changeset
63 try:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
64 if len(node.findall("dataset")) != 1:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
65 # exit early
88b9b105c09b Uploaded
fubar
parents:
diff changeset
66 return metadata
88b9b105c09b Uploaded
fubar
parents:
diff changeset
67 except Exception:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
68 return {}
88b9b105c09b Uploaded
fubar
parents:
diff changeset
69
88b9b105c09b Uploaded
fubar
parents:
diff changeset
70 for (key, value) in node.findall("dataset")[0].attrib.items():
88b9b105c09b Uploaded
fubar
parents:
diff changeset
71 metadata["dataset_%s" % key] = value
88b9b105c09b Uploaded
fubar
parents:
diff changeset
72
88b9b105c09b Uploaded
fubar
parents:
diff changeset
73 for (key, value) in node.findall("history")[0].attrib.items():
88b9b105c09b Uploaded
fubar
parents:
diff changeset
74 metadata["history_%s" % key] = value
88b9b105c09b Uploaded
fubar
parents:
diff changeset
75
88b9b105c09b Uploaded
fubar
parents:
diff changeset
76 for (key, value) in node.findall("metadata")[0].attrib.items():
88b9b105c09b Uploaded
fubar
parents:
diff changeset
77 metadata["metadata_%s" % key] = value
88b9b105c09b Uploaded
fubar
parents:
diff changeset
78
88b9b105c09b Uploaded
fubar
parents:
diff changeset
79 for (key, value) in node.findall("tool")[0].attrib.items():
88b9b105c09b Uploaded
fubar
parents:
diff changeset
80 metadata["tool_%s" % key] = value
88b9b105c09b Uploaded
fubar
parents:
diff changeset
81
88b9b105c09b Uploaded
fubar
parents:
diff changeset
82 # Additional Mappings applied:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
83 metadata[
88b9b105c09b Uploaded
fubar
parents:
diff changeset
84 "dataset_edam_format"
88b9b105c09b Uploaded
fubar
parents:
diff changeset
85 ] = '<a target="_blank" href="http://edamontology.org/{0}">{1}</a>'.format(
88b9b105c09b Uploaded
fubar
parents:
diff changeset
86 metadata["dataset_edam_format"], metadata["dataset_file_ext"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
87 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
88 metadata["history_user_email"] = '<a href="mailto:{0}">{0}</a>'.format(
88b9b105c09b Uploaded
fubar
parents:
diff changeset
89 metadata["history_user_email"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
90 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
91 metadata["hist_name"] = metadata["history_display_name"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
92 metadata[
88b9b105c09b Uploaded
fubar
parents:
diff changeset
93 "history_display_name"
88b9b105c09b Uploaded
fubar
parents:
diff changeset
94 ] = '<a target="_blank" href="{galaxy}/history/view/{encoded_hist_id}">{hist_name}</a>'.format(
88b9b105c09b Uploaded
fubar
parents:
diff changeset
95 galaxy=GALAXY_INFRASTRUCTURE_URL,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
96 encoded_hist_id=metadata["history_id"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
97 hist_name=metadata["history_display_name"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
98 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
99 metadata[
88b9b105c09b Uploaded
fubar
parents:
diff changeset
100 "tool_tool"
88b9b105c09b Uploaded
fubar
parents:
diff changeset
101 ] = '<a target="_blank" href="{galaxy}/datasets/{encoded_id}/show_params">{tool_id}</a>'.format(
88b9b105c09b Uploaded
fubar
parents:
diff changeset
102 galaxy=GALAXY_INFRASTRUCTURE_URL,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
103 encoded_id=metadata["dataset_id"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
104 tool_id=metadata["tool_tool_id"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
105 # tool_version=metadata['tool_tool_version'],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
106 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
107 return metadata
88b9b105c09b Uploaded
fubar
parents:
diff changeset
108
88b9b105c09b Uploaded
fubar
parents:
diff changeset
109
88b9b105c09b Uploaded
fubar
parents:
diff changeset
110 class JbrowseConnector(object):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
111 def __init__(self, jbrowse, outdir, genomes, standalone=None):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
112 self.debug = False
88b9b105c09b Uploaded
fubar
parents:
diff changeset
113 self.usejson = True
88b9b105c09b Uploaded
fubar
parents:
diff changeset
114 self.giURL = GALAXY_INFRASTRUCTURE_URL
88b9b105c09b Uploaded
fubar
parents:
diff changeset
115 self.jbrowse = jbrowse
88b9b105c09b Uploaded
fubar
parents:
diff changeset
116 self.outdir = outdir
88b9b105c09b Uploaded
fubar
parents:
diff changeset
117 os.makedirs(self.outdir, exist_ok=True)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
118 self.genome_paths = genomes
88b9b105c09b Uploaded
fubar
parents:
diff changeset
119 self.standalone = standalone
88b9b105c09b Uploaded
fubar
parents:
diff changeset
120 self.trackIdlist = []
88b9b105c09b Uploaded
fubar
parents:
diff changeset
121 self.tracksToAdd = []
88b9b105c09b Uploaded
fubar
parents:
diff changeset
122 self.config_json = {}
88b9b105c09b Uploaded
fubar
parents:
diff changeset
123 self.config_json_file = os.path.realpath(os.path.join(outdir, "config.json"))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
124 if standalone == "complete":
88b9b105c09b Uploaded
fubar
parents:
diff changeset
125 self.clone_jbrowse(self.jbrowse, self.outdir)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
126 elif standalone == "minimal":
88b9b105c09b Uploaded
fubar
parents:
diff changeset
127 self.clone_jbrowse(self.jbrowse, self.outdir, minimal=True)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
128
88b9b105c09b Uploaded
fubar
parents:
diff changeset
129 def subprocess_check_call(self, command, output=None):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
130 if output:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
131 if self.debug:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
132 log.debug("cd %s && %s > %s", self.outdir, " ".join(command), output)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
133 subprocess.check_call(command, cwd=self.outdir, stdout=output)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
134 else:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
135 log.debug("cd %s && %s", self.outdir, " ".join(command))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
136 subprocess.check_call(command, cwd=self.outdir)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
137
88b9b105c09b Uploaded
fubar
parents:
diff changeset
138 def subprocess_popen(self, command):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
139 if self.debug:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
140 log.debug("cd %s && %s", self.outdir, command)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
141 p = subprocess.Popen(
88b9b105c09b Uploaded
fubar
parents:
diff changeset
142 command,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
143 shell=True,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
144 stdin=subprocess.PIPE,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
145 stdout=subprocess.PIPE,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
146 stderr=subprocess.PIPE,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
147 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
148 output, err = p.communicate()
88b9b105c09b Uploaded
fubar
parents:
diff changeset
149 retcode = p.returncode
88b9b105c09b Uploaded
fubar
parents:
diff changeset
150 if retcode != 0:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
151 log.error("cd %s && %s", self.outdir, command)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
152 log.error(output)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
153 log.error(err)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
154 raise RuntimeError("Command failed with exit code %s" % (retcode))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
155
88b9b105c09b Uploaded
fubar
parents:
diff changeset
156 def subprocess_check_output(self, command):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
157 if self.debug:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
158 log.debug("cd %s && %s", self.outdir, " ".join(command))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
159 return subprocess.check_output(command, cwd=self.outdir)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
160
88b9b105c09b Uploaded
fubar
parents:
diff changeset
161 def _jbrowse_bin(self, command):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
162 return os.path.realpath(os.path.join(self.jbrowse, "bin", command))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
163
88b9b105c09b Uploaded
fubar
parents:
diff changeset
164 def symlink_or_copy(self, src, dest):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
165 if "GALAXY_JBROWSE_SYMLINKS" in os.environ and bool(
88b9b105c09b Uploaded
fubar
parents:
diff changeset
166 os.environ["GALAXY_JBROWSE_SYMLINKS"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
167 ):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
168 cmd = ["ln", "-s", src, dest]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
169 else:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
170 cmd = ["cp", src, dest]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
171
88b9b105c09b Uploaded
fubar
parents:
diff changeset
172 return self.subprocess_check_call(cmd)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
173
88b9b105c09b Uploaded
fubar
parents:
diff changeset
174 def process_genomes(self):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
175 assemblies = []
88b9b105c09b Uploaded
fubar
parents:
diff changeset
176 for i, genome_node in enumerate(self.genome_paths):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
177 log.info("genome_node=%s" % str(genome_node))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
178 # We only expect one input genome per run. This for loop is just
88b9b105c09b Uploaded
fubar
parents:
diff changeset
179 # easier to write than the alternative / catches any possible
88b9b105c09b Uploaded
fubar
parents:
diff changeset
180 # issues.
88b9b105c09b Uploaded
fubar
parents:
diff changeset
181 genome_name = genome_node["meta"]["dataset_dname"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
182 dsId = genome_node["meta"]["dataset_id"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
183 fapath = genome_node["path"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
184 faname = genome_name + ".fa.gz"
88b9b105c09b Uploaded
fubar
parents:
diff changeset
185 faind = os.path.realpath(os.path.join(self.outdir, faname + ".gzi"))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
186 if self.standalone == "complete":
88b9b105c09b Uploaded
fubar
parents:
diff changeset
187 fadest = os.path.realpath(os.path.join(self.outdir, faname))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
188 cmd = "bgzip -i -c %s > %s && samtools faidx %s" % (
88b9b105c09b Uploaded
fubar
parents:
diff changeset
189 fapath,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
190 fadest,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
191 fadest,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
192 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
193 self.subprocess_popen(cmd)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
194 adapter = {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
195 "type": "BgzipFastaAdapter",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
196 "fastaLocation": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
197 "uri": faname,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
198 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
199 "faiLocation": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
200 "uri": faname + ".fai",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
201 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
202 "gziLocation": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
203 "uri": faname + ".gzi",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
204 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
205 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
206 else:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
207 faurl = "%s/api/datasets/%s/display" % (self.giURL, dsId)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
208 fastalocation = {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
209 "uri": faurl,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
210 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
211 failocation = {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
212 "uri": faname + ".fai",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
213 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
214 adapter = {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
215 "type": "IndexedFastaAdapter",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
216 "fastaLocation": fastalocation,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
217 "faiLocation": failocation,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
218 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
219
88b9b105c09b Uploaded
fubar
parents:
diff changeset
220 cmd = ["samtools", "faidx", fapath, "--fai-idx", faind]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
221 self.subprocess_check_call(cmd)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
222 trackDict = {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
223 "name": genome_name,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
224 "sequence": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
225 "type": "ReferenceSequenceTrack",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
226 "trackId": genome_name,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
227 "adapter": adapter,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
228 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
229 "rendering": {"type": "DivSequenceRenderer"},
88b9b105c09b Uploaded
fubar
parents:
diff changeset
230 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
231 assemblies.append(trackDict)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
232 self.genome_name = genome_name
88b9b105c09b Uploaded
fubar
parents:
diff changeset
233 if self.usejson:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
234 self.config_json["assemblies"] = assemblies
88b9b105c09b Uploaded
fubar
parents:
diff changeset
235 else:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
236 cmd = [
88b9b105c09b Uploaded
fubar
parents:
diff changeset
237 "jbrowse",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
238 "add-assembly",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
239 faname,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
240 "-t",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
241 "bgzipFasta",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
242 "-n",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
243 genome_name,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
244 "--load",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
245 "inPlace",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
246 "--faiLocation",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
247 faname + ".fai",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
248 "--gziLocation",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
249 faname + ".gzi",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
250 "--target",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
251 self.outdir,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
252 ]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
253 self.subprocess_check_call(cmd)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
254
88b9b105c09b Uploaded
fubar
parents:
diff changeset
255 def add_default_view(self):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
256 cmd = [
88b9b105c09b Uploaded
fubar
parents:
diff changeset
257 "jbrowse",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
258 "set-default-session",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
259 "-s",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
260 self.config_json_file,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
261 "-t",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
262 ",".join(self.trackIdlist),
88b9b105c09b Uploaded
fubar
parents:
diff changeset
263 "-n",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
264 "JBrowse2 in Galaxy",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
265 "--target",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
266 self.config_json_file,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
267 "-v",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
268 " LinearGenomeView",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
269 ]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
270 if True or self.debug:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
271 log.info("### calling set-default-session with cmd=%s" % " ".join(cmd))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
272 self.subprocess_check_call(cmd)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
273
88b9b105c09b Uploaded
fubar
parents:
diff changeset
274 def write_config(self):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
275 with open(self.config_json_file, "w") as fp:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
276 json.dump(self.config_json, fp)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
277
88b9b105c09b Uploaded
fubar
parents:
diff changeset
278 def add_hic(self, data, trackData):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
279 """
88b9b105c09b Uploaded
fubar
parents:
diff changeset
280 HiC adapter.
88b9b105c09b Uploaded
fubar
parents:
diff changeset
281 https://github.com/aidenlab/hic-format/blob/master/HiCFormatV9.md
88b9b105c09b Uploaded
fubar
parents:
diff changeset
282 for testing locally, these work:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
283 HiC data is from https://s3.amazonaws.com/igv.broadinstitute.org/data/hic/intra_nofrag_30.hic
88b9b105c09b Uploaded
fubar
parents:
diff changeset
284 using hg19 reference track as a
88b9b105c09b Uploaded
fubar
parents:
diff changeset
285 'BgzipFastaAdapter'
88b9b105c09b Uploaded
fubar
parents:
diff changeset
286 fastaLocation:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
287 uri: 'https://s3.amazonaws.com/jbrowse.org/genomes/GRCh38/fasta/GRCh38.fa.gz',
88b9b105c09b Uploaded
fubar
parents:
diff changeset
288 faiLocation:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
289 uri: 'https://s3.amazonaws.com/jbrowse.org/genomes/GRCh38/fasta/GRCh38.fa.gz.fai',
88b9b105c09b Uploaded
fubar
parents:
diff changeset
290 gziLocation:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
291 uri: 'https://s3.amazonaws.com/jbrowse.org/genomes/GRCh38/fasta/GRCh38.fa.gz.gzi',
88b9b105c09b Uploaded
fubar
parents:
diff changeset
292 Cool will not be likely to be a good fit - see discussion at https://github.com/GMOD/jbrowse-components/issues/2438
88b9b105c09b Uploaded
fubar
parents:
diff changeset
293 """
88b9b105c09b Uploaded
fubar
parents:
diff changeset
294 log.info("#### trackData=%s" % trackData)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
295 tId = trackData["label"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
296 dsId = trackData["metadata"]["dataset_id"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
297 url = "%s/api/datasets/%s/display?to_ext=hic " % (
88b9b105c09b Uploaded
fubar
parents:
diff changeset
298 self.giURL,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
299 dsId,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
300 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
301 hname = trackData["name"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
302 if self.standalone == "complete":
88b9b105c09b Uploaded
fubar
parents:
diff changeset
303 dest = os.path.realpath(os.path.join(self.outdir, hname))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
304 url = hname
88b9b105c09b Uploaded
fubar
parents:
diff changeset
305 cmd = ["cp", data, dest]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
306 self.subprocess_check_call(cmd)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
307 floc = {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
308 "uri": hname,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
309 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
310 else:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
311 url = "%s/api/datasets/%s/display?to_ext=hic" % (self.giURL, dsId)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
312 floc = {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
313 "uri": url,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
314 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
315 trackDict = {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
316 "type": "HicTrack",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
317 "trackId": tId,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
318 "name": hname,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
319 "assemblyNames": [self.genome_name],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
320 "adapter": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
321 "type": "HicAdapter",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
322 "hicLocation": floc,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
323 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
324 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
325 if self.usejson:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
326 self.tracksToAdd.append(trackDict)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
327 self.trackIdlist.append(tId)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
328 else:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
329 cmd = [
88b9b105c09b Uploaded
fubar
parents:
diff changeset
330 "jbrowse",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
331 "add-track",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
332 url,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
333 "-t",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
334 "HicTrack",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
335 "-a",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
336 self.genome_name,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
337 "-n",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
338 hname,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
339 "--load",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
340 "inPlace",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
341 "--target",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
342 self.outdir,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
343 ]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
344 self.subprocess_check_call(cmd)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
345
88b9b105c09b Uploaded
fubar
parents:
diff changeset
346 def add_maf(self, data, trackData):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
347 """
88b9b105c09b Uploaded
fubar
parents:
diff changeset
348 from https://github.com/cmdcolin/maf2bed
88b9b105c09b Uploaded
fubar
parents:
diff changeset
349 Note: Both formats start with a MAF as input, and note that your MAF file should contain the species name and chromosome name
88b9b105c09b Uploaded
fubar
parents:
diff changeset
350 e.g. hg38.chr1 in the sequence identifiers.
88b9b105c09b Uploaded
fubar
parents:
diff changeset
351 need the reference id - eg hg18, for maf2bed.pl as the first parameter
88b9b105c09b Uploaded
fubar
parents:
diff changeset
352 """
88b9b105c09b Uploaded
fubar
parents:
diff changeset
353 mafPlugin = {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
354 "plugins": [
88b9b105c09b Uploaded
fubar
parents:
diff changeset
355 {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
356 "name": "MafViewer",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
357 "url": "https://unpkg.com/jbrowse-plugin-mafviewer/dist/jbrowse-plugin-mafviewer.umd.production.min.js",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
358 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
359 ]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
360 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
361 tId = trackData["label"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
362 fname = "%s.bed" % tId
88b9b105c09b Uploaded
fubar
parents:
diff changeset
363 dest = os.path.realpath("%s/%s" % (self.outdir, fname))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
364 # self.symlink_or_copy(data, dest)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
365 # Process MAF to bed-like. Need build to munge chromosomes
88b9b105c09b Uploaded
fubar
parents:
diff changeset
366 gname = self.genome_name
88b9b105c09b Uploaded
fubar
parents:
diff changeset
367 cmd = [
88b9b105c09b Uploaded
fubar
parents:
diff changeset
368 "bash",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
369 os.path.join(INSTALLED_TO, "convertMAF.sh"),
88b9b105c09b Uploaded
fubar
parents:
diff changeset
370 data,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
371 gname,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
372 INSTALLED_TO,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
373 dest,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
374 ]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
375 self.subprocess_check_call(cmd)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
376 if True or self.debug:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
377 log.info("### convertMAF.sh called as %s" % " ".join(cmd))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
378 # Construct samples list
88b9b105c09b Uploaded
fubar
parents:
diff changeset
379 # We could get this from galaxy metadata, not sure how easily.
88b9b105c09b Uploaded
fubar
parents:
diff changeset
380 ps = subprocess.Popen(["grep", "^s [^ ]*", "-o", data], stdout=subprocess.PIPE)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
381 output = subprocess.check_output(("sort", "-u"), stdin=ps.stdout)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
382 ps.wait()
88b9b105c09b Uploaded
fubar
parents:
diff changeset
383 outp = output.decode("ascii")
88b9b105c09b Uploaded
fubar
parents:
diff changeset
384 soutp = outp.split("\n")
88b9b105c09b Uploaded
fubar
parents:
diff changeset
385 samp = [x.split("s ")[1] for x in soutp if x.startswith("s ")]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
386 samples = [x.split(".")[0] for x in samp]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
387 if self.debug:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
388 log.info("### got samples = %s " % (samples))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
389 trackDict = {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
390 "type": "MafTrack",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
391 "trackId": tId,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
392 "name": trackData["name"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
393 "adapter": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
394 "type": "MafTabixAdapter",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
395 "samples": samples,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
396 "bedGzLocation": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
397 "uri": fname + ".sorted.bed.gz",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
398 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
399 "index": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
400 "location": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
401 "uri": fname + ".sorted.bed.gz.tbi",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
402 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
403 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
404 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
405 "assemblyNames": [self.genome_name],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
406 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
407 self.tracksToAdd.append(trackDict)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
408 self.trackIdlist.append(tId)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
409 if self.config_json.get("plugins", None):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
410 self.config_json["plugins"].append(mafPlugin[0])
88b9b105c09b Uploaded
fubar
parents:
diff changeset
411 else:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
412 self.config_json.update(mafPlugin)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
413
88b9b105c09b Uploaded
fubar
parents:
diff changeset
414 def _blastxml_to_gff3(self, xml, min_gap=10):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
415 gff3_unrebased = tempfile.NamedTemporaryFile(delete=False)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
416 cmd = [
88b9b105c09b Uploaded
fubar
parents:
diff changeset
417 "python",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
418 os.path.join(INSTALLED_TO, "blastxml_to_gapped_gff3.py"),
88b9b105c09b Uploaded
fubar
parents:
diff changeset
419 "--trim",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
420 "--trim_end",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
421 "--include_seq",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
422 "--min_gap",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
423 str(min_gap),
88b9b105c09b Uploaded
fubar
parents:
diff changeset
424 xml,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
425 ]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
426 subprocess.check_call(cmd, cwd=self.outdir, stdout=gff3_unrebased)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
427 gff3_unrebased.close()
88b9b105c09b Uploaded
fubar
parents:
diff changeset
428 return gff3_unrebased.name
88b9b105c09b Uploaded
fubar
parents:
diff changeset
429
88b9b105c09b Uploaded
fubar
parents:
diff changeset
430 def add_blastxml(self, data, trackData, blastOpts, **kwargs):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
431 gff3 = self._blastxml_to_gff3(data, min_gap=blastOpts["min_gap"])
88b9b105c09b Uploaded
fubar
parents:
diff changeset
432
88b9b105c09b Uploaded
fubar
parents:
diff changeset
433 if "parent" in blastOpts and blastOpts["parent"] != "None":
88b9b105c09b Uploaded
fubar
parents:
diff changeset
434 gff3_rebased = tempfile.NamedTemporaryFile(delete=False)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
435 cmd = ["python", os.path.join(INSTALLED_TO, "gff3_rebase.py")]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
436 if blastOpts.get("protein", "false") == "true":
88b9b105c09b Uploaded
fubar
parents:
diff changeset
437 cmd.append("--protein2dna")
88b9b105c09b Uploaded
fubar
parents:
diff changeset
438 cmd.extend([os.path.realpath(blastOpts["parent"]), gff3])
88b9b105c09b Uploaded
fubar
parents:
diff changeset
439 subprocess.check_call(cmd, cwd=self.outdir, stdout=gff3_rebased)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
440 gff3_rebased.close()
88b9b105c09b Uploaded
fubar
parents:
diff changeset
441
88b9b105c09b Uploaded
fubar
parents:
diff changeset
442 # Replace original gff3 file
88b9b105c09b Uploaded
fubar
parents:
diff changeset
443 shutil.copy(gff3_rebased.name, gff3)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
444 os.unlink(gff3_rebased.name)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
445 url = "%s.gff3" % trackData["label"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
446 dest = os.path.realpath("%s/%s" % (self.outdir, url))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
447 self._sort_gff(gff3, dest)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
448 url = url + ".gz"
88b9b105c09b Uploaded
fubar
parents:
diff changeset
449 tId = trackData["label"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
450 trackDict = {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
451 "type": "FeatureTrack",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
452 "trackId": tId,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
453 "name": trackData["name"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
454 "assemblyNames": [self.genome_name],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
455 "adapter": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
456 "type": "Gff3TabixAdapter",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
457 "gffGzLocation": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
458 "uri": url,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
459 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
460 "index": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
461 "location": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
462 "uri": url + ".tbi",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
463 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
464 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
465 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
466 "displays": [
88b9b105c09b Uploaded
fubar
parents:
diff changeset
467 {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
468 "type": "LinearBasicDisplay",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
469 "displayId": "%s-LinearBasicDisplay" % tId,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
470 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
471 {"type": "LinearArcDisplay", "displayId": "%s-LinearArcDisplay" % tId},
88b9b105c09b Uploaded
fubar
parents:
diff changeset
472 ],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
473 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
474 if self.usejson:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
475 self.tracksToAdd.append(trackDict)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
476 self.trackIdlist.append(tId)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
477 else:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
478 cmd = [
88b9b105c09b Uploaded
fubar
parents:
diff changeset
479 "jbrowse",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
480 "add-track",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
481 url,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
482 "-t",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
483 "FeatureTrack",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
484 "-a",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
485 self.genome_name,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
486 "--indexFile",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
487 url + ".tbi",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
488 "-n",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
489 trackData["name"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
490 "--load",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
491 "inPlace",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
492 "--target",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
493 self.outdir,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
494 ]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
495 self.subprocess_check_call(cmd)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
496 os.unlink(gff3)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
497
88b9b105c09b Uploaded
fubar
parents:
diff changeset
498 def add_bigwig(self, data, trackData):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
499 url = "%s.bw" % trackData["name"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
500 if self.standalone == "complete":
88b9b105c09b Uploaded
fubar
parents:
diff changeset
501 dest = os.path.realpath(os.path.join(self.outdir, url))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
502 cmd = ["cp", data, dest]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
503 self.subprocess_check_call(cmd)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
504 bwloc = {"uri": url}
88b9b105c09b Uploaded
fubar
parents:
diff changeset
505 else:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
506 dsId = trackData["metadata"]["dataset_id"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
507 url = "%s/api/datasets/%s/display?to_ext=fasta" % (self.giURL, dsId)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
508 bwloc = {"uri": url}
88b9b105c09b Uploaded
fubar
parents:
diff changeset
509 tId = trackData["label"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
510 trackDict = {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
511 "type": "QuantitativeTrack",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
512 "trackId": tId,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
513 "name": url,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
514 "assemblyNames": [
88b9b105c09b Uploaded
fubar
parents:
diff changeset
515 self.genome_name,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
516 ],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
517 "adapter": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
518 "type": "BigWigAdapter",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
519 "bigWigLocation": bwloc,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
520 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
521 "displays": [
88b9b105c09b Uploaded
fubar
parents:
diff changeset
522 {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
523 "type": "LinearWiggleDisplay",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
524 "displayId": "%s-LinearWiggleDisplay" % tId,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
525 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
526 ],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
527 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
528 if self.usejson:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
529 self.tracksToAdd.append(trackDict)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
530 self.trackIdlist.append(tId)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
531 else:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
532 cmd = [
88b9b105c09b Uploaded
fubar
parents:
diff changeset
533 "jbrowse",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
534 "add-track",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
535 url,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
536 "-t",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
537 "QuantitativeTrack",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
538 "-a",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
539 self.genome_name,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
540 "-n",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
541 trackData["name"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
542 "--load",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
543 "inPlace",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
544 "--target",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
545 self.outdir,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
546 ]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
547 self.subprocess_check_call(cmd)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
548
88b9b105c09b Uploaded
fubar
parents:
diff changeset
549 def add_bam(self, data, trackData, bamOpts, bam_index=None, **kwargs):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
550 tId = trackData["label"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
551 fname = "%s.bam" % trackData["label"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
552 dest = os.path.realpath("%s/%s" % (self.outdir, fname))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
553 if self.standalone == "complete":
88b9b105c09b Uploaded
fubar
parents:
diff changeset
554 url = fname
88b9b105c09b Uploaded
fubar
parents:
diff changeset
555 self.subprocess_check_call(["cp", data, dest])
88b9b105c09b Uploaded
fubar
parents:
diff changeset
556 log.info("### copied %s to %s" % (data, dest))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
557 bloc = {"uri": url}
88b9b105c09b Uploaded
fubar
parents:
diff changeset
558 else:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
559 dsId = trackData["metadata"]["dataset_id"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
560 url = "%s/api/datasets/%s/display?to_ext=bam" % (self.giURL, dsId)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
561 bloc = {"uri": url}
88b9b105c09b Uploaded
fubar
parents:
diff changeset
562 if bam_index is not None and os.path.exists(os.path.realpath(bam_index)):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
563 # bai most probably made by galaxy and stored in galaxy dirs, need to copy it to dest
88b9b105c09b Uploaded
fubar
parents:
diff changeset
564 self.subprocess_check_call(
88b9b105c09b Uploaded
fubar
parents:
diff changeset
565 ["cp", os.path.realpath(bam_index), dest + ".bai"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
566 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
567 else:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
568 # Can happen in exotic condition
88b9b105c09b Uploaded
fubar
parents:
diff changeset
569 # e.g. if bam imported as symlink with datatype=unsorted.bam, then datatype changed to bam
88b9b105c09b Uploaded
fubar
parents:
diff changeset
570 # => no index generated by galaxy, but there might be one next to the symlink target
88b9b105c09b Uploaded
fubar
parents:
diff changeset
571 # this trick allows to skip the bam sorting made by galaxy if already done outside
88b9b105c09b Uploaded
fubar
parents:
diff changeset
572 if os.path.exists(os.path.realpath(data) + ".bai"):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
573 self.symlink_or_copy(os.path.realpath(data) + ".bai", dest + ".bai")
88b9b105c09b Uploaded
fubar
parents:
diff changeset
574 else:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
575 log.warn("Could not find a bam index (.bai file) for %s", data)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
576 trackDict = {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
577 "type": "AlignmentsTrack",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
578 "trackId": tId,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
579 "name": trackData["name"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
580 "assemblyNames": [self.genome_name],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
581 "adapter": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
582 "type": "BamAdapter",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
583 "bamLocation": bloc,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
584 "index": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
585 "location": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
586 "uri": fname + ".bai",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
587 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
588 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
589 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
590 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
591 if self.usejson:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
592 self.tracksToAdd.append(trackDict)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
593 self.trackIdlist.append(tId)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
594 else:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
595 cmd = [
88b9b105c09b Uploaded
fubar
parents:
diff changeset
596 "jbrowse",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
597 "add-track",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
598 fname,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
599 "-t",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
600 "AlignmentsTrack",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
601 "-l",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
602 "inPlace",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
603 "-a",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
604 self.genome_name,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
605 "--indexFile",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
606 fname + ".bai",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
607 "-n",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
608 trackData["name"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
609 "--target",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
610 self.outdir,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
611 ]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
612 self.subprocess_check_call(cmd)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
613
88b9b105c09b Uploaded
fubar
parents:
diff changeset
614 def add_vcf(self, data, trackData):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
615 tId = trackData["label"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
616 url = "%s/api/datasets/%s/display" % (
88b9b105c09b Uploaded
fubar
parents:
diff changeset
617 self.giURL,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
618 trackData["metadata"]["dataset_id"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
619 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
620 url = "%s.vcf.gz" % tId
88b9b105c09b Uploaded
fubar
parents:
diff changeset
621 dest = os.path.realpath("%s/%s" % (self.outdir, url))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
622 cmd = "bgzip -c %s > %s" % (data, dest)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
623 self.subprocess_popen(cmd)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
624 cmd = ["tabix", "-p", "vcf", dest]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
625 self.subprocess_check_call(cmd)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
626 trackDict = {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
627 "type": "VariantTrack",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
628 "trackId": tId,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
629 "name": trackData["name"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
630 "assemblyNames": [self.genome_name],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
631 "adapter": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
632 "type": "VcfTabixAdapter",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
633 "vcfGzLocation": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
634 "uri": url,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
635 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
636 "index": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
637 "location": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
638 "uri": url + ".tbi",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
639 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
640 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
641 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
642 "displays": [
88b9b105c09b Uploaded
fubar
parents:
diff changeset
643 {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
644 "type": "LinearVariantDisplay",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
645 "displayId": "%s-LinearVariantDisplay" % tId,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
646 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
647 {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
648 "type": "ChordVariantDisplay",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
649 "displayId": "%s-ChordVariantDisplay" % tId,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
650 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
651 {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
652 "type": "LinearPairedArcDisplay",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
653 "displayId": "%s-LinearPairedArcDisplay" % tId,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
654 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
655 ],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
656 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
657 if self.usejson:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
658 self.tracksToAdd.append(trackDict)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
659 self.trackIdlist.append(tId)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
660 else:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
661 cmd = [
88b9b105c09b Uploaded
fubar
parents:
diff changeset
662 "jbrowse",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
663 "add-track",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
664 url,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
665 "-t",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
666 "VariantTrack",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
667 "-a",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
668 self.genome_name,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
669 "--indexFile",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
670 url + ".tbi",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
671 "-n",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
672 trackData["name"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
673 "--load",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
674 "inPlace",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
675 "--target",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
676 self.outdir,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
677 ]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
678 self.subprocess_check_call(cmd)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
679
88b9b105c09b Uploaded
fubar
parents:
diff changeset
680 def _sort_gff(self, data, dest):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
681 # Only index if not already done
88b9b105c09b Uploaded
fubar
parents:
diff changeset
682 if not os.path.exists(dest + ".gz"):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
683 cmd = "jbrowse sort-gff %s | bgzip -c > %s.gz" % (
88b9b105c09b Uploaded
fubar
parents:
diff changeset
684 data,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
685 dest,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
686 ) # "gff3sort.pl --precise '%s' | grep -v \"^$\" > '%s'"
88b9b105c09b Uploaded
fubar
parents:
diff changeset
687 self.subprocess_popen(cmd)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
688 self.subprocess_check_call(["tabix", "-f", "-p", "gff", dest + ".gz"])
88b9b105c09b Uploaded
fubar
parents:
diff changeset
689
88b9b105c09b Uploaded
fubar
parents:
diff changeset
690 def _sort_bed(self, data, dest):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
691 # Only index if not already done
88b9b105c09b Uploaded
fubar
parents:
diff changeset
692 if not os.path.exists(dest):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
693 cmd = "sort -k1,1 -k2,2n %s | bgzip -c > %s" % (data, dest)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
694 self.subprocess_popen(cmd)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
695 cmd = ["tabix", "-f", "-p", "bed", dest]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
696 self.subprocess_check_call(cmd)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
697
88b9b105c09b Uploaded
fubar
parents:
diff changeset
698 def add_gff(self, data, ext, trackData):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
699 url = "%s.%s" % (trackData["label"], ext)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
700 dest = os.path.realpath("%s/%s" % (self.outdir, url))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
701 self._sort_gff(data, dest)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
702 url = url + ".gz"
88b9b105c09b Uploaded
fubar
parents:
diff changeset
703 tId = trackData["label"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
704 trackDict = {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
705 "type": "FeatureTrack",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
706 "trackId": tId,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
707 "name": trackData["name"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
708 "assemblyNames": [self.genome_name],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
709 "adapter": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
710 "type": "Gff3TabixAdapter",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
711 "gffGzLocation": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
712 "uri": url,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
713 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
714 "index": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
715 "location": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
716 "uri": url + ".tbi",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
717 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
718 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
719 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
720 "displays": [
88b9b105c09b Uploaded
fubar
parents:
diff changeset
721 {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
722 "type": "LinearBasicDisplay",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
723 "displayId": "%s-LinearBasicDisplay" % tId,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
724 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
725 {"type": "LinearArcDisplay", "displayId": "%s-LinearArcDisplay" % tId},
88b9b105c09b Uploaded
fubar
parents:
diff changeset
726 ],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
727 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
728 if self.usejson:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
729 self.tracksToAdd.append(trackDict)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
730 self.trackIdlist.append(tId)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
731 else:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
732 cmd = [
88b9b105c09b Uploaded
fubar
parents:
diff changeset
733 "jbrowse",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
734 "add-track",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
735 url,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
736 "-t",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
737 "FeatureTrack",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
738 "-a",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
739 self.genome_name,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
740 "-n",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
741 trackData["name"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
742 "--load",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
743 "inPlace",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
744 "--target",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
745 self.outdir,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
746 ]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
747 self.subprocess_check_call(cmd)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
748
88b9b105c09b Uploaded
fubar
parents:
diff changeset
749 def add_bed(self, data, ext, trackData):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
750 url = "%s.%s" % (trackData["label"], ext)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
751 dest = os.path.realpath("%s/%s.gz" % (self.outdir, url))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
752 self._sort_bed(data, dest)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
753 tId = trackData["label"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
754 url = url + ".gz"
88b9b105c09b Uploaded
fubar
parents:
diff changeset
755 trackDict = {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
756 "type": "FeatureTrack",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
757 "trackId": tId,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
758 "name": trackData["name"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
759 "assemblyNames": [self.genome_name],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
760 "adapter": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
761 "type": "BedTabixAdapter",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
762 "bedGzLocation": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
763 "uri": url,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
764 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
765 "index": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
766 "location": {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
767 "uri": url + ".tbi",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
768 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
769 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
770 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
771 "displays": [
88b9b105c09b Uploaded
fubar
parents:
diff changeset
772 {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
773 "type": "LinearBasicDisplay",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
774 "displayId": "%s-LinearBasicDisplay" % tId,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
775 },
88b9b105c09b Uploaded
fubar
parents:
diff changeset
776 {"type": "LinearArcDisplay", "displayId": "%s-LinearArcDisplay" % tId},
88b9b105c09b Uploaded
fubar
parents:
diff changeset
777 ],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
778 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
779 if self.usejson:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
780 self.tracksToAdd.append(trackDict)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
781 self.trackIdlist.append(tId)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
782 else:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
783 cmd = [
88b9b105c09b Uploaded
fubar
parents:
diff changeset
784 "jbrowse",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
785 "add-track",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
786 url,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
787 "-t",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
788 "FeatureTrack",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
789 "-a",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
790 self.genome_name,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
791 "--indexFile",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
792 url + ".tbi",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
793 "-n",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
794 trackData["name"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
795 "--load",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
796 "inPlace",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
797 "--target",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
798 self.outdir,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
799 ]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
800 self.subprocess_check_call(cmd)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
801
88b9b105c09b Uploaded
fubar
parents:
diff changeset
802 def process_annotations(self, track):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
803 category = track["category"].replace("__pd__date__pd__", TODAY)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
804 for i, (
88b9b105c09b Uploaded
fubar
parents:
diff changeset
805 dataset_path,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
806 dataset_ext,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
807 track_human_label,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
808 extra_metadata,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
809 ) in enumerate(track["trackfiles"]):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
810 # Unsanitize labels (element_identifiers are always sanitized by Galaxy)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
811 for key, value in mapped_chars.items():
88b9b105c09b Uploaded
fubar
parents:
diff changeset
812 track_human_label = track_human_label.replace(value, key)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
813 outputTrackConfig = {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
814 "category": category,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
815 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
816 if self.debug:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
817 log.info(
88b9b105c09b Uploaded
fubar
parents:
diff changeset
818 "Processing category = %s, track_human_label = %s",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
819 category,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
820 track_human_label,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
821 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
822 # We add extra data to hash for the case of REST + SPARQL.
88b9b105c09b Uploaded
fubar
parents:
diff changeset
823 if (
88b9b105c09b Uploaded
fubar
parents:
diff changeset
824 "conf" in track
88b9b105c09b Uploaded
fubar
parents:
diff changeset
825 and "options" in track["conf"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
826 and "url" in track["conf"]["options"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
827 ):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
828 rest_url = track["conf"]["options"]["url"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
829 else:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
830 rest_url = ""
88b9b105c09b Uploaded
fubar
parents:
diff changeset
831
88b9b105c09b Uploaded
fubar
parents:
diff changeset
832 # I chose to use track['category'] instead of 'category' here. This
88b9b105c09b Uploaded
fubar
parents:
diff changeset
833 # is intentional. This way re-running the tool on a different date
88b9b105c09b Uploaded
fubar
parents:
diff changeset
834 # will not generate different hashes and make comparison of outputs
88b9b105c09b Uploaded
fubar
parents:
diff changeset
835 # much simpler.
88b9b105c09b Uploaded
fubar
parents:
diff changeset
836 hashData = [
88b9b105c09b Uploaded
fubar
parents:
diff changeset
837 str(dataset_path),
88b9b105c09b Uploaded
fubar
parents:
diff changeset
838 track_human_label,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
839 track["category"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
840 rest_url,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
841 ]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
842 hashData = "|".join(hashData).encode("utf-8")
88b9b105c09b Uploaded
fubar
parents:
diff changeset
843 outputTrackConfig["label"] = hashlib.md5(hashData).hexdigest() + "_%s" % i
88b9b105c09b Uploaded
fubar
parents:
diff changeset
844 outputTrackConfig["metadata"] = extra_metadata
88b9b105c09b Uploaded
fubar
parents:
diff changeset
845 outputTrackConfig["name"] = track_human_label
88b9b105c09b Uploaded
fubar
parents:
diff changeset
846
88b9b105c09b Uploaded
fubar
parents:
diff changeset
847 if dataset_ext in ("gff", "gff3"):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
848 self.add_gff(
88b9b105c09b Uploaded
fubar
parents:
diff changeset
849 dataset_path,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
850 dataset_ext,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
851 outputTrackConfig,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
852 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
853 elif dataset_ext in ("hic",):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
854 self.add_hic(
88b9b105c09b Uploaded
fubar
parents:
diff changeset
855 dataset_path,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
856 outputTrackConfig,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
857 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
858 elif dataset_ext in ("bed",):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
859 self.add_bed(
88b9b105c09b Uploaded
fubar
parents:
diff changeset
860 dataset_path,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
861 dataset_ext,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
862 outputTrackConfig,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
863 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
864 elif dataset_ext in ("maf",):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
865 self.add_maf(
88b9b105c09b Uploaded
fubar
parents:
diff changeset
866 dataset_path,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
867 outputTrackConfig,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
868 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
869 elif dataset_ext == "bigwig":
88b9b105c09b Uploaded
fubar
parents:
diff changeset
870 self.add_bigwig(
88b9b105c09b Uploaded
fubar
parents:
diff changeset
871 dataset_path,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
872 outputTrackConfig,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
873 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
874 elif dataset_ext == "bam":
88b9b105c09b Uploaded
fubar
parents:
diff changeset
875 real_indexes = track["conf"]["options"]["pileup"]["bam_indices"][
88b9b105c09b Uploaded
fubar
parents:
diff changeset
876 "bam_index"
88b9b105c09b Uploaded
fubar
parents:
diff changeset
877 ]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
878 if not isinstance(real_indexes, list):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
879 # <bam_indices>
88b9b105c09b Uploaded
fubar
parents:
diff changeset
880 # <bam_index>/path/to/a.bam.bai</bam_index>
88b9b105c09b Uploaded
fubar
parents:
diff changeset
881 # </bam_indices>
88b9b105c09b Uploaded
fubar
parents:
diff changeset
882 #
88b9b105c09b Uploaded
fubar
parents:
diff changeset
883 # The above will result in the 'bam_index' key containing a
88b9b105c09b Uploaded
fubar
parents:
diff changeset
884 # string. If there are two or more indices, the container
88b9b105c09b Uploaded
fubar
parents:
diff changeset
885 # becomes a list. Fun!
88b9b105c09b Uploaded
fubar
parents:
diff changeset
886 real_indexes = [real_indexes]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
887
88b9b105c09b Uploaded
fubar
parents:
diff changeset
888 self.add_bam(
88b9b105c09b Uploaded
fubar
parents:
diff changeset
889 dataset_path,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
890 outputTrackConfig,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
891 track["conf"]["options"]["pileup"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
892 bam_index=real_indexes[i],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
893 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
894 elif dataset_ext == "blastxml":
88b9b105c09b Uploaded
fubar
parents:
diff changeset
895 self.add_blastxml(
88b9b105c09b Uploaded
fubar
parents:
diff changeset
896 dataset_path, outputTrackConfig, track["conf"]["options"]["blast"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
897 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
898 elif dataset_ext == "vcf":
88b9b105c09b Uploaded
fubar
parents:
diff changeset
899 self.add_vcf(dataset_path, outputTrackConfig)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
900 else:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
901 log.warn("Do not know how to handle %s", dataset_ext)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
902
88b9b105c09b Uploaded
fubar
parents:
diff changeset
903 def clone_jbrowse(self, jbrowse_dir, destination, minimal=False):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
904 """Clone a JBrowse directory into a destination directory."""
88b9b105c09b Uploaded
fubar
parents:
diff changeset
905 cmd = ["jbrowse", "create", "-f", self.outdir]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
906 self.subprocess_check_call(cmd)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
907 for fn in [
88b9b105c09b Uploaded
fubar
parents:
diff changeset
908 "asset-manifest.json",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
909 "favicon.ico",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
910 "robots.txt",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
911 "umd_plugin.js",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
912 "version.txt",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
913 "test_data",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
914 ]:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
915 cmd = ["rm", "-rf", os.path.join(self.outdir, fn)]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
916 self.subprocess_check_call(cmd)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
917
88b9b105c09b Uploaded
fubar
parents:
diff changeset
918
88b9b105c09b Uploaded
fubar
parents:
diff changeset
919 if __name__ == "__main__":
88b9b105c09b Uploaded
fubar
parents:
diff changeset
920 parser = argparse.ArgumentParser(description="", epilog="")
88b9b105c09b Uploaded
fubar
parents:
diff changeset
921 parser.add_argument("xml", type=argparse.FileType("r"), help="Track Configuration")
88b9b105c09b Uploaded
fubar
parents:
diff changeset
922
88b9b105c09b Uploaded
fubar
parents:
diff changeset
923 parser.add_argument("--jbrowse", help="Folder containing a jbrowse release")
88b9b105c09b Uploaded
fubar
parents:
diff changeset
924 parser.add_argument("--outdir", help="Output directory", default="out")
88b9b105c09b Uploaded
fubar
parents:
diff changeset
925 parser.add_argument(
88b9b105c09b Uploaded
fubar
parents:
diff changeset
926 "--standalone",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
927 choices=["complete", "minimal", "data"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
928 help="Standalone mode includes a copy of JBrowse",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
929 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
930 parser.add_argument("--version", "-V", action="version", version="%(prog)s 0.8.0")
88b9b105c09b Uploaded
fubar
parents:
diff changeset
931 args = parser.parse_args()
88b9b105c09b Uploaded
fubar
parents:
diff changeset
932
88b9b105c09b Uploaded
fubar
parents:
diff changeset
933 tree = ET.parse(args.xml.name)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
934 root = tree.getroot()
88b9b105c09b Uploaded
fubar
parents:
diff changeset
935
88b9b105c09b Uploaded
fubar
parents:
diff changeset
936 # This should be done ASAP
88b9b105c09b Uploaded
fubar
parents:
diff changeset
937 GALAXY_INFRASTRUCTURE_URL = root.find("metadata/galaxyUrl").text
88b9b105c09b Uploaded
fubar
parents:
diff changeset
938 # Sometimes this comes as `localhost` without a protocol
88b9b105c09b Uploaded
fubar
parents:
diff changeset
939 if not GALAXY_INFRASTRUCTURE_URL.startswith("http"):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
940 # so we'll prepend `http://` and hope for the best. Requests *should*
88b9b105c09b Uploaded
fubar
parents:
diff changeset
941 # be GET and not POST so it should redirect OK
88b9b105c09b Uploaded
fubar
parents:
diff changeset
942 GALAXY_INFRASTRUCTURE_URL = "http://" + GALAXY_INFRASTRUCTURE_URL
88b9b105c09b Uploaded
fubar
parents:
diff changeset
943
88b9b105c09b Uploaded
fubar
parents:
diff changeset
944 jc = JbrowseConnector(
88b9b105c09b Uploaded
fubar
parents:
diff changeset
945 jbrowse=args.jbrowse,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
946 outdir=args.outdir,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
947 genomes=[
88b9b105c09b Uploaded
fubar
parents:
diff changeset
948 {
88b9b105c09b Uploaded
fubar
parents:
diff changeset
949 "path": os.path.realpath(x.attrib["path"]),
88b9b105c09b Uploaded
fubar
parents:
diff changeset
950 "meta": metadata_from_node(x.find("metadata")),
88b9b105c09b Uploaded
fubar
parents:
diff changeset
951 }
88b9b105c09b Uploaded
fubar
parents:
diff changeset
952 for x in root.findall("metadata/genomes/genome")
88b9b105c09b Uploaded
fubar
parents:
diff changeset
953 ],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
954 standalone=args.standalone,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
955 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
956 jc.process_genomes()
88b9b105c09b Uploaded
fubar
parents:
diff changeset
957
88b9b105c09b Uploaded
fubar
parents:
diff changeset
958 for track in root.findall("tracks/track"):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
959 track_conf = {}
88b9b105c09b Uploaded
fubar
parents:
diff changeset
960 track_conf["trackfiles"] = []
88b9b105c09b Uploaded
fubar
parents:
diff changeset
961
88b9b105c09b Uploaded
fubar
parents:
diff changeset
962 is_multi_bigwig = False
88b9b105c09b Uploaded
fubar
parents:
diff changeset
963 try:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
964 if track.find("options/wiggle/multibigwig") and (
88b9b105c09b Uploaded
fubar
parents:
diff changeset
965 track.find("options/wiggle/multibigwig").text == "True"
88b9b105c09b Uploaded
fubar
parents:
diff changeset
966 ):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
967 is_multi_bigwig = True
88b9b105c09b Uploaded
fubar
parents:
diff changeset
968 multi_bigwig_paths = []
88b9b105c09b Uploaded
fubar
parents:
diff changeset
969 except KeyError:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
970 pass
88b9b105c09b Uploaded
fubar
parents:
diff changeset
971
88b9b105c09b Uploaded
fubar
parents:
diff changeset
972 trackfiles = track.findall("files/trackFile")
88b9b105c09b Uploaded
fubar
parents:
diff changeset
973 if trackfiles:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
974 for x in track.findall("files/trackFile"):
88b9b105c09b Uploaded
fubar
parents:
diff changeset
975 if is_multi_bigwig:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
976 multi_bigwig_paths.append(
88b9b105c09b Uploaded
fubar
parents:
diff changeset
977 (x.attrib["label"], os.path.realpath(x.attrib["path"]))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
978 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
979 else:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
980 if trackfiles:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
981 metadata = metadata_from_node(x.find("metadata"))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
982 track_conf["dataset_id"] = metadata["dataset_id"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
983 track_conf["trackfiles"].append(
88b9b105c09b Uploaded
fubar
parents:
diff changeset
984 (
88b9b105c09b Uploaded
fubar
parents:
diff changeset
985 os.path.realpath(x.attrib["path"]),
88b9b105c09b Uploaded
fubar
parents:
diff changeset
986 x.attrib["ext"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
987 x.attrib["label"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
988 metadata,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
989 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
990 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
991 else:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
992 # For tracks without files (rest, sparql)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
993 track_conf["trackfiles"].append(
88b9b105c09b Uploaded
fubar
parents:
diff changeset
994 (
88b9b105c09b Uploaded
fubar
parents:
diff changeset
995 "", # N/A, no path for rest or sparql
88b9b105c09b Uploaded
fubar
parents:
diff changeset
996 track.attrib["format"],
88b9b105c09b Uploaded
fubar
parents:
diff changeset
997 track.find("options/label").text,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
998 {},
88b9b105c09b Uploaded
fubar
parents:
diff changeset
999 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1000 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1001
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1002 if is_multi_bigwig:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1003 metadata = metadata_from_node(x.find("metadata"))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1004
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1005 track_conf["trackfiles"].append(
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1006 (
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1007 multi_bigwig_paths, # Passing an array of paths to represent as one track
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1008 "bigwig_multiple",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1009 "MultiBigWig", # Giving an hardcoded name for now
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1010 {}, # No metadata for multiple bigwig
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1011 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1012 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1013
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1014 track_conf["category"] = track.attrib["cat"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1015 track_conf["format"] = track.attrib["format"]
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1016 try:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1017 # Only pertains to gff3 + blastxml. TODO?
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1018 track_conf["style"] = {t.tag: t.text for t in track.find("options/style")}
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1019 except TypeError:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1020 track_conf["style"] = {}
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1021 pass
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1022 track_conf["conf"] = etree_to_dict(track.find("options"))
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1023 jc.process_annotations(track_conf)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1024 print("## processed", str(track_conf), "trackIdlist", jc.trackIdlist)
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1025 print(
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1026 "###done processing, trackIdlist=",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1027 jc.trackIdlist,
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1028 "config=",
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1029 str(jc.config_json),
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1030 )
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1031 jc.config_json["tracks"] = jc.tracksToAdd
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1032 if jc.usejson:
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1033 jc.write_config()
88b9b105c09b Uploaded
fubar
parents:
diff changeset
1034 jc.add_default_view()