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