annotate jbrowse2/jbrowse2.py @ 7:234cf4490901 draft

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