comparison jbrowse2.py @ 108:5d1259b88c27 draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 9de1453a009c254ab11b16ae8a56cb5c22b131bf
author fubar
date Sat, 29 Jun 2024 06:10:22 +0000
parents a074cd6b5905
children 27e9ddaaf325
comparison
equal deleted inserted replaced
107:a074cd6b5905 108:5d1259b88c27
2 2
3 import argparse 3 import argparse
4 import binascii 4 import binascii
5 import copy 5 import copy
6 import datetime 6 import datetime
7 # import hashlib
8 import json 7 import json
9 import logging 8 import logging
10 import os 9 import os
11 import re 10 import re
12 import shutil 11 import shutil
19 from collections import defaultdict 18 from collections import defaultdict
20 19
21 logging.basicConfig(level=logging.DEBUG) 20 logging.basicConfig(level=logging.DEBUG)
22 log = logging.getLogger("jbrowse") 21 log = logging.getLogger("jbrowse")
23 22
24 JB2VER = "v2.11.1" 23 JB2VER = "v2.12.3"
25 # version pinned if cloning - but not cloning now 24 # version pinned if cloning - but not cloning now
26 logCommands = True 25 logCommands = True
27 # useful for seeing what's being written but not for production setups 26 # useful for seeing what's being written but not for production setups
28 TODAY = datetime.datetime.now().strftime("%Y-%m-%d") 27 TODAY = datetime.datetime.now().strftime("%Y-%m-%d")
29 SELF_LOCATION = os.path.dirname(os.path.realpath(__file__)) 28 SELF_LOCATION = os.path.dirname(os.path.realpath(__file__))
755 style_json = self._prepare_track_style(trackDict) 754 style_json = self._prepare_track_style(trackDict)
756 trackDict["style"] = style_json 755 trackDict["style"] = style_json
757 self.tracksToAdd[gname].append(copy.copy(trackDict)) 756 self.tracksToAdd[gname].append(copy.copy(trackDict))
758 self.trackIdlist.append(copy.copy(tId)) 757 self.trackIdlist.append(copy.copy(tId))
759 if self.config_json.get("plugins", None): 758 if self.config_json.get("plugins", None):
760 self.config_json["plugins"].append(mafPlugin[0]) 759 self.config_json["plugins"].append(mafPlugin["plugins"][0])
761 else: 760 else:
762 self.config_json.update(mafPlugin) 761 self.config_json.update(mafPlugin)
763 762
764 def _blastxml_to_gff3(self, xml, min_gap=10): 763 def _blastxml_to_gff3(self, xml, min_gap=10):
765 gff3_unrebased = tempfile.NamedTemporaryFile(delete=False) 764 gff3_unrebased = tempfile.NamedTemporaryFile(delete=False)
1066 trackDict["style"] = style_json 1065 trackDict["style"] = style_json
1067 self.tracksToAdd[trackData["assemblyNames"]].append(copy.copy(trackDict)) 1066 self.tracksToAdd[trackData["assemblyNames"]].append(copy.copy(trackDict))
1068 self.trackIdlist.append(copy.copy(tId)) 1067 self.trackIdlist.append(copy.copy(tId))
1069 1068
1070 def add_bed(self, data, ext, trackData): 1069 def add_bed(self, data, ext, trackData):
1070 bedPlugin = {"name": "BedScorePlugin", "umdLoc": { "uri": "bedscoreplugin.js" } }
1071 tId = trackData["label"] 1071 tId = trackData["label"]
1072 categ = trackData["category"] 1072 categ = trackData["category"]
1073 useuri = trackData["useuri"].lower() == "yes" 1073 useuri = trackData["useuri"].lower() == "yes"
1074 if useuri: 1074 if useuri:
1075 url = data 1075 url = data
1076 else: 1076 else:
1077 url = tId + ".gz" 1077 url = tId + ".gz"
1078 dest = os.path.join(self.outdir, url) 1078 dest = os.path.join(self.outdir, url)
1079 self._sort_bed(data, dest) 1079 self._sort_bed(data, dest)
1080 if True or trackData.get("usebedscore",None):
1081 bedgzlocation = {
1082 "uri": url,
1083 "columnNames": ["chr","start","end","name","score"],
1084 "scoreColumn": "score",
1085 }
1086 else:
1087 bedgzlocation = {
1088 "uri": url,
1089 }
1080 trackDict = { 1090 trackDict = {
1081 "type": "FeatureTrack", 1091 "type": "FeatureTrack",
1082 "trackId": tId, 1092 "trackId": tId,
1083 "name": trackData["name"], 1093 "name": trackData["name"],
1084 "assemblyNames": [trackData["assemblyNames"]], 1094 "assemblyNames": [trackData["assemblyNames"]],
1085 "adapter": { 1095 "adapter": {
1086 "category": [ 1096 "category": [
1087 categ, 1097 categ,
1088 ], 1098 ],
1089 "type": "BedTabixAdapter", 1099 "type": "BedTabixAdapter",
1090 "bedGzLocation": { 1100 "bedGzLocation": bedgzlocation,
1091 "uri": url,
1092 },
1093 "index": { 1101 "index": {
1094 "location": { 1102 "location": {
1095 "uri": url + ".tbi", 1103 "uri": url + ".tbi",
1096 } 1104 },
1097 }, 1105 },
1098 }, 1106 },
1099 "displays": [ 1107 "displays": [
1100 { 1108 {
1101 "type": "LinearBasicDisplay", 1109 "type": "LinearBasicDisplay",
1102 "displayId": "%s-LinearBasicDisplay" % tId, 1110 "displayId": "%s-LinearBasicDisplay" % tId,
1111 "renderer": {
1112 "type": "SvgFeatureRenderer",
1113 "color1": "jexl:customColor(feature)",
1114 },
1103 }, 1115 },
1104 { 1116 {
1105 "type": "LinearPileupDisplay", 1117 "type": "LinearPileupDisplay",
1106 "displayId": "%s-LinearPileupDisplay" % tId, 1118 "displayId": "%s-LinearPileupDisplay" % tId,
1107 }, 1119 },
1111 }, 1123 },
1112 ], 1124 ],
1113 } 1125 }
1114 style_json = self._prepare_track_style(trackDict) 1126 style_json = self._prepare_track_style(trackDict)
1115 trackDict["style"] = style_json 1127 trackDict["style"] = style_json
1128 if self.config_json.get("plugins", None):
1129 self.config_json["plugins"].append(bedPlugin)
1130 else:
1131 self.config_json["plugins"] = [bedPlugin,]
1116 self.tracksToAdd[trackData["assemblyNames"]].append(copy.copy(trackDict)) 1132 self.tracksToAdd[trackData["assemblyNames"]].append(copy.copy(trackDict))
1117 self.trackIdlist.append(copy.copy(tId)) 1133 self.trackIdlist.append(copy.copy(tId))
1118 1134
1119 def add_paf(self, data, trackData, pafOpts, **kwargs): 1135 def add_paf(self, data, trackData, pafOpts, **kwargs):
1120 tname = trackData["name"] 1136 tname = trackData["name"]
1338 tracks_data = [] 1354 tracks_data = []
1339 for track_conf in self.tracksToAdd[gnome]: 1355 for track_conf in self.tracksToAdd[gnome]:
1340 tId = track_conf["trackId"] 1356 tId = track_conf["trackId"]
1341 if tId in default_data[gnome]["visibility"]["default_on"]: 1357 if tId in default_data[gnome]["visibility"]["default_on"]:
1342 track_types[tId] = track_conf["type"] 1358 track_types[tId] = track_conf["type"]
1343 style_data = default_data[gnome]["style"].get(tId, {}) 1359 display = {"type": "linearBasicDisplay"}
1344 if not style_data:
1345 logging.debug(
1346 "No style data for %s in available default data %s"
1347 % (tId, default_data)
1348 )
1349 else:
1350 logging.debug(
1351 "style data for %s = %s"
1352 % (tId, style_data)
1353 )
1354
1355 if style_data.get('type',None) == None:
1356 style_data["type"] = "LinearBasicDisplay"
1357 if "displays" in track_conf: 1360 if "displays" in track_conf:
1358 disp = track_conf["displays"][0]["type"] 1361 display["type"] = track_conf["displays"][0]["type"]
1359 style_data["type"] = disp 1362 display["configuration"] = track_conf["displays"][0]["displayId"]
1360 if track_conf.get("style_labels", None): 1363 if track_conf.get("style_labels", None):
1361 # TODO fix this: it should probably go in a renderer block (SvgFeatureRenderer) but still does not work 1364 # TODO fix this: it should probably go in a renderer block (SvgFeatureRenderer) but still does not work
1362 # TODO move this to per track displays? 1365 # TODO move this to per track displays?
1363 style_data["labels"] = track_conf["style_labels"] 1366 style_data["labels"] = track_conf["style_labels"]
1364 tracks_data.append( 1367 tracks_data.append(
1365 { 1368 {
1366 "type": track_types[tId], 1369 "type": track_types[tId],
1367 "configuration": tId, 1370 "configuration": tId,
1368 "displays": [style_data], 1371 "displays": [display],
1369 } 1372 }
1370 ) 1373 )
1371 first = [x for x in self.ass_first_contigs if x[0] == gnome] 1374 first = [x for x in self.ass_first_contigs if x[0] == gnome]
1372 drdict = { 1375 drdict = {
1373 "reversed": False, 1376 "reversed": False,
1552 shutil.rmtree(path) 1555 shutil.rmtree(path)
1553 else: 1556 else:
1554 os.remove(path) 1557 os.remove(path)
1555 except OSError as e: 1558 except OSError as e:
1556 log.error("Error: %s - %s." % (e.filename, e.strerror)) 1559 log.error("Error: %s - %s." % (e.filename, e.strerror))
1557 shutil.copyfile(os.path.join(INSTALLED_TO, "jb2_webserver.py"), os.path.join(dest, "jb2_webserver.py")) 1560 for neededfile in ["jb2_webserver.py", "bedscoreplugin.js"]:
1561 shutil.copyfile(os.path.join(INSTALLED_TO, neededfile), os.path.join(dest, neededfile))
1558 1562
1559 1563
1560 def parse_style_conf(item): 1564 def parse_style_conf(item):
1561 if item.text.lower() in ["false", "true", "yes", "no"]: 1565 if item.text.lower() in ["false", "true", "yes", "no"]:
1562 return item.text.lower in ("yes", "true") 1566 return item.text.lower in ("yes", "true")
1625 pass 1629 pass
1626 1630
1627 trackfiles = track.findall("files/trackFile") 1631 trackfiles = track.findall("files/trackFile")
1628 if trackfiles: 1632 if trackfiles:
1629 for x in trackfiles: 1633 for x in trackfiles:
1634 isBed = False
1635 if x.attrib['ext'] == "bed":
1636 isBed = True
1630 track_conf["label"] = "%s_%d" % ( 1637 track_conf["label"] = "%s_%d" % (
1631 x.attrib["label"].replace(" ", "_").replace(",", "_").replace("/","_"), 1638 x.attrib["label"].replace(" ", "_").replace(",", "_").replace("/","_"),
1632 trackI, 1639 trackI,
1633 ) 1640 )
1634 trackI += 1 1641 trackI += 1
1673 "bigwig_multiple", 1680 "bigwig_multiple",
1674 "MultiBigWig", # Giving an hardcoded name for now 1681 "MultiBigWig", # Giving an hardcoded name for now
1675 {}, # No metadata for multiple bigwig 1682 {}, # No metadata for multiple bigwig
1676 ) 1683 )
1677 ) 1684 )
1678
1679 track_conf["category"] = track.attrib["cat"] 1685 track_conf["category"] = track.attrib["cat"]
1680 track_conf["format"] = track.attrib["format"] 1686 track_conf["format"] = track.attrib["format"]
1681 track_conf["conf"] = etree_to_dict(track.find("options")) 1687 track_conf["conf"] = etree_to_dict(track.find("options"))
1682 keys = jc.process_annotations(track_conf) 1688 keys = jc.process_annotations(track_conf)
1683 if keys: 1689 if keys:
1684 for key in keys: 1690 for key in keys:
1685 vis = track.attrib.get("visibility", "default_off") 1691 vis = track.attrib.get("visibility", "default_off")
1686 if not vis: 1692 if not vis:
1687 vis = "default_off" 1693 vis = "default_off"
1688 default_session_data[primaryGenome]["visibility"][vis].append(key) 1694 default_session_data[primaryGenome]["visibility"][vis].append(key)
1689 trakdat = jc.tracksToAdd[primaryGenome]
1690 stile = {}
1691 for trak in trakdat:
1692 if trak["trackId"] == key:
1693 stile = trak.get("style", {})
1694 if track.find("options/style"):
1695 for item in track.find("options/style"):
1696 if item.text:
1697 stile[item.tag] = parse_style_conf(item)
1698 logging.debug("stile=%s" % stile)
1699 default_session_data[primaryGenome]["style"][key] = stile
1700 if track.find("options/style_labels"):
1701 default_session_data[primaryGenome]["style_labels"][key] = {
1702 item.tag: parse_style_conf(item)
1703 for item in track.find("options/style_labels")
1704 }
1705 default_session_data[primaryGenome]["tracks"].append(key) 1695 default_session_data[primaryGenome]["tracks"].append(key)
1706 default_session_data["defaultLocation"] = root.find( 1696 default_session_data["defaultLocation"] = root.find(
1707 "metadata/general/defaultLocation" 1697 "metadata/general/defaultLocation"
1708 ).text 1698 ).text
1709 default_session_data["session_name"] = root.find( 1699 default_session_data["session_name"] = root.find(