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

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