comparison jbrowse2.py @ 5:efc64d8f4b72 draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 79e81bef99f582680d9aa4eec88980c675f3fae9
author fubar
date Tue, 09 Jan 2024 05:24:48 +0000
parents 2de9f585505b
children 79f7265f90bd
comparison
equal deleted inserted replaced
4:54396f0323f4 5:efc64d8f4b72
17 17
18 logging.basicConfig(level=logging.INFO) 18 logging.basicConfig(level=logging.INFO)
19 log = logging.getLogger("jbrowse") 19 log = logging.getLogger("jbrowse")
20 TODAY = datetime.datetime.now().strftime("%Y-%m-%d") 20 TODAY = datetime.datetime.now().strftime("%Y-%m-%d")
21 GALAXY_INFRASTRUCTURE_URL = None 21 GALAXY_INFRASTRUCTURE_URL = None
22 JB2REL="v2.10.0" 22 JB2REL = "v2.10.0"
23 # version pinned for cloning 23 # version pinned for cloning
24 24
25 mapped_chars = { 25 mapped_chars = {
26 ">": "__gt__", 26 ">": "__gt__",
27 "<": "__lt__", 27 "<": "__lt__",
433 style_data = { 433 style_data = {
434 "type": "LinearBasicDisplay", 434 "type": "LinearBasicDisplay",
435 "displayId": "%s-LinearBasicDisplay" % trackDict["trackId"], 435 "displayId": "%s-LinearBasicDisplay" % trackDict["trackId"],
436 } 436 }
437 437
438 if trackDict.get("displays", None): 438 if trackDict.get("displays", None): # use first if multiple like bed
439 style_data["type"] = trackDict["displays"]["type"] 439 style_data["type"] = trackDict["displays"][0]["type"]
440 style_data["displayId"] = trackDict["displays"]["displayId"] 440 style_data["displayId"] = trackDict["displays"][0]["displayId"]
441 return {"displays": [style_data]} 441 return {
442 "displays": [
443 style_data,
444 ]
445 }
442 446
443 def process_genomes(self): 447 def process_genomes(self):
444 assemblies = [] 448 assemblies = []
445 for i, genome_node in enumerate(self.genome_paths): 449 for i, genome_node in enumerate(self.genome_paths):
446 if self.debug:
447 log.info("genome_node=%s" % str(genome_node))
448 genome_name = genome_node["meta"]["dataset_dname"].strip() 450 genome_name = genome_node["meta"]["dataset_dname"].strip()
449 if len(genome_name.split()) > 1: 451 if len(genome_name.split()) > 1:
450 genome_name = genome_name.split()[0] 452 genome_name = genome_name.split()[0]
451 # spaces and cruft break scripts when substituted 453 # spaces and cruft break scripts when substituted
452 fapath = genome_node["path"] 454 fapath = genome_node["path"]
548 uri: 'https://s3.amazonaws.com/jbrowse.org/genomes/GRCh38/fasta/GRCh38.fa.gz.fai', 550 uri: 'https://s3.amazonaws.com/jbrowse.org/genomes/GRCh38/fasta/GRCh38.fa.gz.fai',
549 gziLocation: 551 gziLocation:
550 uri: 'https://s3.amazonaws.com/jbrowse.org/genomes/GRCh38/fasta/GRCh38.fa.gz.gzi', 552 uri: 'https://s3.amazonaws.com/jbrowse.org/genomes/GRCh38/fasta/GRCh38.fa.gz.gzi',
551 Cool will not be likely to be a good fit - see discussion at https://github.com/GMOD/jbrowse-components/issues/2438 553 Cool will not be likely to be a good fit - see discussion at https://github.com/GMOD/jbrowse-components/issues/2438
552 """ 554 """
553 log.info("#### trackData=%s" % trackData)
554 tId = trackData["label"] 555 tId = trackData["label"]
555 # can be served - if public. 556 # can be served - if public.
556 # dsId = trackData["metadata"]["dataset_id"] 557 # dsId = trackData["metadata"]["dataset_id"]
557 # url = "%s/api/datasets/%s/display?to_ext=hic " % (self.giURL, dsId) 558 # url = "%s/api/datasets/%s/display?to_ext=hic " % (self.giURL, dsId)
558 hname = trackData["name"] 559 hname = trackData["name"]
577 "type": "LinearHicDisplay", 578 "type": "LinearHicDisplay",
578 "displayId": "%s-LinearHicDisplay" % tId, 579 "displayId": "%s-LinearHicDisplay" % tId,
579 }, 580 },
580 ], 581 ],
581 } 582 }
582 # style_json = self._prepare_track_style(trackDict) 583 style_json = self._prepare_track_style(trackDict)
583 # trackDict["style"] = style_json 584 trackDict["style"] = style_json
584 self.tracksToAdd.append(trackDict) 585 self.tracksToAdd.append(trackDict)
585 self.trackIdlist.append(tId) 586 self.trackIdlist.append(tId)
586 587
587 def add_maf(self, data, trackData): 588 def add_maf(self, data, trackData):
588 """ 589 """
612 gname, 613 gname,
613 INSTALLED_TO, 614 INSTALLED_TO,
614 dest, 615 dest,
615 ] 616 ]
616 self.subprocess_check_call(cmd) 617 self.subprocess_check_call(cmd)
617 if True or self.debug:
618 log.info("### convertMAF.sh called as %s" % " ".join(cmd))
619 # Construct samples list 618 # Construct samples list
620 # We could get this from galaxy metadata, not sure how easily. 619 # We could get this from galaxy metadata, not sure how easily.
621 ps = subprocess.Popen(["grep", "^s [^ ]*", "-o", data], stdout=subprocess.PIPE) 620 ps = subprocess.Popen(["grep", "^s [^ ]*", "-o", data], stdout=subprocess.PIPE)
622 output = subprocess.check_output(("sort", "-u"), stdin=ps.stdout) 621 output = subprocess.check_output(("sort", "-u"), stdin=ps.stdout)
623 ps.wait() 622 ps.wait()
624 outp = output.decode("ascii") 623 outp = output.decode("ascii")
625 soutp = outp.split("\n") 624 soutp = outp.split("\n")
626 samp = [x.split("s ")[1] for x in soutp if x.startswith("s ")] 625 samp = [x.split("s ")[1] for x in soutp if x.startswith("s ")]
627 samples = [x.split(".")[0] for x in samp] 626 samples = [x.split(".")[0] for x in samp]
628 if self.debug:
629 log.info("### got samples = %s " % (samples))
630 trackDict = { 627 trackDict = {
631 "type": "MafTrack", 628 "type": "MafTrack",
632 "trackId": tId, 629 "trackId": tId,
633 "name": trackData["name"], 630 "name": trackData["name"],
634 "adapter": { 631 "adapter": {
642 "uri": fname + ".sorted.bed.gz.tbi", 639 "uri": fname + ".sorted.bed.gz.tbi",
643 }, 640 },
644 }, 641 },
645 }, 642 },
646 "assemblyNames": [self.genome_name], 643 "assemblyNames": [self.genome_name],
647 } 644 "displays": [
648 # style_json = self._prepare_track_style(trackDict) 645 {
649 # trackDict["style"] = style_json 646 "type": "LinearBasicDisplay",
647 "displayId": "%s-LinearBasicDisplay" % tId,
648 },
649 {"type": "LinearArcDisplay", "displayId": "%s-LinearArcDisplay" % tId},
650 ],
651 }
652 style_json = self._prepare_track_style(trackDict)
653 trackDict["style"] = style_json
650 self.tracksToAdd.append(trackDict) 654 self.tracksToAdd.append(trackDict)
651 self.trackIdlist.append(tId) 655 self.trackIdlist.append(tId)
652 if self.config_json.get("plugins", None): 656 if self.config_json.get("plugins", None):
653 self.config_json["plugins"].append(mafPlugin[0]) 657 self.config_json["plugins"].append(mafPlugin[0])
654 else: 658 else:
712 "displayId": "%s-LinearBasicDisplay" % tId, 716 "displayId": "%s-LinearBasicDisplay" % tId,
713 }, 717 },
714 {"type": "LinearArcDisplay", "displayId": "%s-LinearArcDisplay" % tId}, 718 {"type": "LinearArcDisplay", "displayId": "%s-LinearArcDisplay" % tId},
715 ], 719 ],
716 } 720 }
721 style_json = self._prepare_track_style(trackDict)
722 trackDict["style"] = style_json
717 self.tracksToAdd.append(trackDict) 723 self.tracksToAdd.append(trackDict)
718 self.trackIdlist.append(tId) 724 self.trackIdlist.append(tId)
719 725
720 os.unlink(gff3) 726 os.unlink(gff3)
721 727
742 "type": "LinearWiggleDisplay", 748 "type": "LinearWiggleDisplay",
743 "displayId": "%s-LinearWiggleDisplay" % tId, 749 "displayId": "%s-LinearWiggleDisplay" % tId,
744 } 750 }
745 ], 751 ],
746 } 752 }
747 # style_json = self._prepare_track_style(trackDict) 753 style_json = self._prepare_track_style(trackDict)
748 # trackDict["style"] = style_json 754 trackDict["style"] = style_json
749 self.tracksToAdd.append(trackDict) 755 self.tracksToAdd.append(trackDict)
750 self.trackIdlist.append(tId) 756 self.trackIdlist.append(tId)
751 757
752 def add_bam(self, data, trackData, bamOpts, bam_index=None, **kwargs): 758 def add_bam(self, data, trackData, bamOpts, bam_index=None, **kwargs):
753 tId = trackData["label"] 759 tId = trackData["label"]
754 fname = "%s.bam" % trackData["label"] 760 fname = "%s.bam" % trackData["label"]
755 dest = "%s/%s" % (self.outdir, fname) 761 dest = "%s/%s" % (self.outdir, fname)
756 url = fname 762 url = fname
757 self.subprocess_check_call(["cp", data, dest]) 763 self.subprocess_check_call(["cp", data, dest])
758 log.info("### copied %s to %s" % (data, dest))
759 bloc = {"uri": url} 764 bloc = {"uri": url}
760 if bam_index is not None and os.path.exists(os.path.realpath(bam_index)): 765 if bam_index is not None and os.path.exists(os.path.realpath(bam_index)):
761 # bai most probably made by galaxy and stored in galaxy dirs, need to copy it to dest 766 # bai most probably made by galaxy and stored in galaxy dirs, need to copy it to dest
762 self.subprocess_check_call( 767 self.subprocess_check_call(
763 ["cp", os.path.realpath(bam_index), dest + ".bai"] 768 ["cp", os.path.realpath(bam_index), dest + ".bai"]
790 "type": "LinearAlignmentsDisplay", 795 "type": "LinearAlignmentsDisplay",
791 "displayId": "%s-LinearAlignmentsDisplay" % tId, 796 "displayId": "%s-LinearAlignmentsDisplay" % tId,
792 }, 797 },
793 ], 798 ],
794 } 799 }
795 # style_json = self._prepare_track_style(trackDict) 800 style_json = self._prepare_track_style(trackDict)
796 # trackDict["style"] = style_json 801 trackDict["style"] = style_json
797 self.tracksToAdd.append(trackDict) 802 self.tracksToAdd.append(trackDict)
798 self.trackIdlist.append(tId) 803 self.trackIdlist.append(tId)
799 804
800 def add_vcf(self, data, trackData): 805 def add_vcf(self, data, trackData):
801 tId = trackData["label"] 806 tId = trackData["label"]
838 "type": "LinearPairedArcDisplay", 843 "type": "LinearPairedArcDisplay",
839 "displayId": "%s-LinearPairedArcDisplay" % tId, 844 "displayId": "%s-LinearPairedArcDisplay" % tId,
840 }, 845 },
841 ], 846 ],
842 } 847 }
843 # style_json = self._prepare_track_style(trackDict) 848 style_json = self._prepare_track_style(trackDict)
844 # trackDict["style"] = style_json 849 trackDict["style"] = style_json
845 self.tracksToAdd.append(trackDict) 850 self.tracksToAdd.append(trackDict)
846 self.trackIdlist.append(tId) 851 self.trackIdlist.append(tId)
847 852
848 def _sort_gff(self, data, dest): 853 def _sort_gff(self, data, dest):
849 # Only index if not already done 854 # Only index if not already done
891 "displayId": "%s-LinearBasicDisplay" % tId, 896 "displayId": "%s-LinearBasicDisplay" % tId,
892 }, 897 },
893 {"type": "LinearArcDisplay", "displayId": "%s-LinearArcDisplay" % tId}, 898 {"type": "LinearArcDisplay", "displayId": "%s-LinearArcDisplay" % tId},
894 ], 899 ],
895 } 900 }
896 # style_json = self._prepare_track_style(trackDict) 901 style_json = self._prepare_track_style(trackDict)
897 # trackDict["style"] = style_json 902 trackDict["style"] = style_json
898 self.tracksToAdd.append(trackDict) 903 self.tracksToAdd.append(trackDict)
899 self.trackIdlist.append(tId) 904 self.trackIdlist.append(tId)
900 905
901 def add_bed(self, data, ext, trackData): 906 def add_bed(self, data, ext, trackData):
902 url = "%s.%s" % (trackData["label"], ext) 907 url = "%s.%s" % (trackData["label"], ext)
920 } 925 }
921 }, 926 },
922 }, 927 },
923 "displays": [ 928 "displays": [
924 { 929 {
930 "type": "LinearPileupDisplay",
931 "displayId": "%s-LinearPileupDisplay" % tId,
932 },
933 {
925 "type": "LinearBasicDisplay", 934 "type": "LinearBasicDisplay",
926 "displayId": "%s-LinearBasicDisplay" % tId, 935 "displayId": "%s-LinearBasicDisplay" % tId,
927 }, 936 },
928 {"type": "LinearArcDisplay", "displayId": "%s-LinearArcDisplay" % tId}, 937 {"type": "LinearArcDisplay", "displayId": "%s-LinearArcDisplay" % tId},
929 ], 938 ],
930 } 939 }
931 # style_json = self._prepare_track_style(trackDict) 940 style_json = self._prepare_track_style(trackDict)
932 # trackDict["style"] = style_json 941 trackDict["style"] = style_json
933 self.tracksToAdd.append(trackDict) 942 self.tracksToAdd.append(trackDict)
934 self.trackIdlist.append(tId) 943 self.trackIdlist.append(tId)
935 944
936 def add_paf(self, data, trackData, pafOpts, **kwargs): 945 def add_paf(self, data, trackData, pafOpts, **kwargs):
937 tname = trackData["name"] 946 tname = trackData["name"]
1149 1158
1150 # TODO using the default session for now, but check out session specs in the future https://github.com/GMOD/jbrowse-components/issues/2708 1159 # TODO using the default session for now, but check out session specs in the future https://github.com/GMOD/jbrowse-components/issues/2708
1151 1160
1152 # We need to know the track type from the config.json generated just before 1161 # We need to know the track type from the config.json generated just before
1153 track_types = {} 1162 track_types = {}
1154 logging.info("### add default session has data = %s\n" % str(data))
1155 with open(self.config_json_file, "r") as config_file: 1163 with open(self.config_json_file, "r") as config_file:
1156 config_json = json.load(config_file) 1164 config_json = json.load(config_file)
1157 logging.info("### config.json read \n%s\n" % (config_json))
1158 1165
1159 for track_conf in self.tracksToAdd: # config_json["tracks"]: 1166 for track_conf in self.tracksToAdd: # config_json["tracks"]:
1160 track_types[track_conf["trackId"]] = track_conf["type"] 1167 track_types[track_conf["trackId"]] = track_conf["type"]
1161 logging.info(
1162 "### self.tracksToAdd = %s; track_types = %s"
1163 % (str(self.tracksToAdd), str(track_types))
1164 )
1165 1168
1166 for on_track in data["visibility"]["default_on"]: 1169 for on_track in data["visibility"]["default_on"]:
1167 style_data = {"type": "LinearBasicDisplay", "height": 100} 1170 style_data = {"type": "LinearBasicDisplay", "height": 100}
1168 if on_track in data["style"]: 1171 if on_track in data["style"]:
1169 if "display" in data["style"][on_track]: 1172 if "display" in data["style"][on_track]:
1261 json.dump(config_json, config_file, indent=2) 1264 json.dump(config_json, config_file, indent=2)
1262 1265
1263 def clone_jbrowse(self): 1266 def clone_jbrowse(self):
1264 """Clone a JBrowse directory into a destination directory.""" 1267 """Clone a JBrowse directory into a destination directory."""
1265 dest = self.outdir 1268 dest = self.outdir
1266 cmd = ["jbrowse", "create", "-t",JB2REL, "-f", dest] 1269 cmd = ["jbrowse", "create", "-t", JB2REL, "-f", dest]
1267 self.subprocess_check_call(cmd) 1270 self.subprocess_check_call(cmd)
1268 for fn in [ 1271 for fn in [
1269 "asset-manifest.json", 1272 "asset-manifest.json",
1270 "favicon.ico", 1273 "favicon.ico",
1271 "robots.txt", 1274 "robots.txt",
1435 except TypeError: 1438 except TypeError:
1436 track_conf["style"] = {} 1439 track_conf["style"] = {}
1437 pass 1440 pass
1438 track_conf["conf"] = etree_to_dict(track.find("options")) 1441 track_conf["conf"] = etree_to_dict(track.find("options"))
1439 jc.add_general_configuration(general_data) 1442 jc.add_general_configuration(general_data)
1440 print("## processed", str(track_conf), "trackIdlist", jc.trackIdlist)
1441 x = open(args.xml, "r").read() 1443 x = open(args.xml, "r").read()
1442 log.info(
1443 "###done processing xml=%s; trackIdlist=%s, config=%s"
1444 % (x, jc.trackIdlist, str(jc.config_json))
1445 )
1446 jc.config_json["tracks"] = jc.tracksToAdd 1444 jc.config_json["tracks"] = jc.tracksToAdd
1447 if jc.usejson: 1445 if jc.usejson:
1448 jc.write_config() 1446 jc.write_config()
1449 # jc.add_default_view() 1447 # jc.add_default_view()
1450 jc.add_default_session(default_session_data) 1448 jc.add_default_session(default_session_data)