annotate jbrowse2/jbrowse2.py @ 6:88b9b105c09b draft

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