annotate jbrowse2/jbrowse2.py @ 3:0d953fc4ca97 draft

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