Mercurial > repos > fubar > jbrowse2
comparison autogenJB2.py @ 46:4181e97c70a7 draft
planemo upload for repository https://github.com/usegalaxy-eu/temporary-tools/tree/master/jbrowse2 commit 3a43e9e0ffce0966101203102e769d1ced28618a
| author | fubar | 
|---|---|
| date | Mon, 04 Mar 2024 09:47:19 +0000 | 
| parents | bc57164eb270 | 
| children | 460d5b6c5d98 | 
   comparison
  equal
  deleted
  inserted
  replaced
| 45:0ec526d2d8c1 | 46:4181e97c70a7 | 
|---|---|
| 1 import argparse | 1 import argparse | 
| 2 import logging | 2 import logging | 
| 3 import os | |
| 3 import sys | 4 import sys | 
| 4 | 5 | 
| 5 from jbrowse2 import JbrowseConnector as jbC | 6 from jbrowse2 import JbrowseConnector as jbC | 
| 6 | 7 | 
| 7 logging.basicConfig(level=logging.debug) | 8 logging.basicConfig(level=logging.debug) | 
| 9 | 10 | 
| 10 | 11 | 
| 11 def makeDefaultLocation(): | 12 def makeDefaultLocation(): | 
| 12 | 13 | 
| 13 refName = jc.genome_firstcontig | 14 refName = jc.genome_firstcontig | 
| 14 defloc = "%s:100..10000" % refName | 15 defloc = "%s:100..10000" % refName | 
| 15 print ('defloc',defloc) | |
| 16 return defloc | 16 return defloc | 
| 17 | 17 | 
| 18 | 18 | 
| 19 if __name__ == "__main__": | 19 if __name__ == "__main__": | 
| 20 parser = argparse.ArgumentParser(description="", epilog="") | 20 parser = argparse.ArgumentParser(description="", epilog="") | 
| 21 parser.add_argument("--sessName", help="Session name", default="AutoJBrowse") | 21 parser.add_argument("--sessName", help="Session name", default="AutoJBrowse") | 
| 22 parser.add_argument( | 22 parser.add_argument( | 
| 23 "--trackmeta", | 23 "--trackmeta", | 
| 24 help="Repeatable of 'filename, filext,filepath, ... ,' for JBrowse2 tracks", | 24 help="Repeatable 'filename,filext,filepath,[bai/crai path for filesystem bam/cram]' for JBrowse2 tracks", | 
| 25 default=[], | 25 default=[], | 
| 26 action="append", | 26 action="append", | 
| 27 ) | 27 ) | 
| 28 parser.add_argument( | 28 parser.add_argument( | 
| 29 "--referencemeta", | 29 "--referencemeta", | 
| 30 help="Repeatable 'filename, filext, filepath, ... ,' for JBrowse2 reference tracks", | 30 help="Repeatable 'filename, filext, filepath, ... ,' for JBrowse2 reference tracks - usually only one needed", | 
| 31 default=[], | 31 default=[], | 
| 32 action="append", | 32 action="append", | 
| 33 ) | 33 ) | 
| 34 parser.add_argument( | 34 parser.add_argument( | 
| 35 "--pafmeta", | 35 "--pafmeta", | 
| 36 help="Repeatable. Each is a 'filename, filext, filepath, ... ,' for a JBrowse2 paf track", | 36 help="Repeatable. Each is a 'pafname, filext, filepath, ... ,' for a JBrowse2 paf track", | 
| 37 default=[], | 37 default=[], | 
| 38 action="append", | 38 action="append", | 
| 39 ) | 39 ) | 
| 40 parser.add_argument( | 40 parser.add_argument( | 
| 41 "--pafreferencemeta", | 41 "--pafreferencemeta", | 
| 42 help="Repeatable. Each is a 'pafname,filepath,refname,filepath,refname....'. Every paf must have a corresponding one ", | 42 help="Repeatable. Each is a 'pafname,refpath,refname' Every pafname must have one or more", | 
| 43 default=[], | 43 default=[], | 
| 44 action="append", | 44 action="append", | 
| 45 ) | 45 ) | 
| 46 parser.add_argument("--jbrowse2path", help="Path to JBrowse2 directory in biocontainer or Conda") | 46 parser.add_argument( | 
| 47 "--jbrowse2path", help="Path to JBrowse2 directory in biocontainer or Conda" | |
| 48 ) | |
| 47 parser.add_argument("--outdir", help="Output directory", required=True) | 49 parser.add_argument("--outdir", help="Output directory", required=True) | 
| 48 parser.add_argument("--version", "-V", action="version", version="%(prog)s 2.10.2") | 50 parser.add_argument("--version", "-V", action="version", version="%(prog)s 2.10.2") | 
| 49 args = parser.parse_args() | 51 args = parser.parse_args() | 
| 50 sessName = args.sessName | 52 sessName = args.sessName | 
| 51 # --trackmeta $jbrowseme[$key],$jbrowseme[$key].ext,'$key' | 53 # --trackmeta $jbrowseme[$key],$jbrowseme[$key].ext,'$key' | 
| 52 trackList = [x.strip().split(",") for x in args.trackmeta if x > ''] | 54 trackList = [x.strip().split(",") for x in args.trackmeta if x > ""] | 
| 53 refList = [x.strip().split(",") for x in args.referencemeta if x > ''] | 55 refList = [x.strip().split(",") for x in args.referencemeta if x > ""] | 
| 54 print("tracklist = %s\nreflist = %s" % (trackList,refList)) | |
| 55 if len(refList) > 0: | 56 if len(refList) > 0: | 
| 56 listgenomes = [f for f in refList if f[1] in ['fasta', 'fasta.gz']] | 57 listgenomes = [f for f in refList if f[1] in ["fasta", "fasta.gz"]] | 
| 57 # assume no pafs here | 58 # assume no pafs here | 
| 58 print('genomes=%s' % listgenomes) | |
| 59 if len(listgenomes) > 0: | 59 if len(listgenomes) > 0: | 
| 60 genome_paths = [ | 60 genome_paths = [x[0] for x in listgenomes] | 
| 61 x[0] for x in listgenomes | |
| 62 ] # expect genome_1_genomename.fasta etc | |
| 63 genome_names = [x[2] for x in listgenomes] | 61 genome_names = [x[2] for x in listgenomes] | 
| 62 guseuri = [] | |
| 63 for x in genome_paths: | |
| 64 if x.startswith('http://') or x.startswith('https://'): | |
| 65 guseuri.append('yes') | |
| 66 else: | |
| 67 guseuri.append('no') | |
| 64 jc = jbC( | 68 jc = jbC( | 
| 65 outdir=args.outdir, | 69 outdir=args.outdir, | 
| 66 jbrowse2path=args.jbrowse2path, | 70 jbrowse2path=args.jbrowse2path, | 
| 67 genomes=[ | 71 genomes=[ | 
| 68 { | 72 { | 
| 69 "path": x, | 73 "path": x, | 
| 70 "meta": { | 74 "label": genome_names[i], | 
| 71 "name": genome_names[i], | 75 "useuri": guseuri[i], | 
| 72 "dataset_dname": genome_names[i], | 76 "meta": {"name": genome_names[i], | 
| 73 }, | 77 "dataset_dname": genome_names[i] | 
| 78 } | |
| 74 } | 79 } | 
| 75 for i, x in enumerate(genome_paths) | 80 for i, x in enumerate(genome_paths) | 
| 76 ], | 81 ], | 
| 77 ) | 82 ) | 
| 78 sys.stdout.write('$$$ genome_paths:%s genome_names: %s' % (genome_paths,genome_names)) | 83 | 
| 79 jc.process_genomes() | 84 jc.process_genomes() | 
| 80 default_session_data = { | 85 default_session_data = { | 
| 81 "visibility": { | 86 "visibility": { | 
| 82 "default_on": [], | 87 "default_on": [], | 
| 83 "default_off": [], | 88 "default_off": [], | 
| 107 "options": { | 112 "options": { | 
| 108 "paf": {"genome": refdat, "genome_label": trackname} | 113 "paf": {"genome": refdat, "genome_label": trackname} | 
| 109 } | 114 } | 
| 110 } | 115 } | 
| 111 } | 116 } | 
| 112 elif trext == 'bam': | 117 elif trext == "bam": | 
| 113 track_conf = {"conf": {"options": {"bam": {"bam_index": track[3]}}}} | 118 ipath = track[3] | 
| 114 elif trext == 'cram': | 119 if not os.path.exists(ipath): | 
| 115 track_conf = {"conf": {"options": {"cram": {"cram_index": track[3]}}}} | 120 ipath = os.path.realpath(os.path.join(jc.outdir, trackname + '.bai')) | 
| 121 cmd = ["samtools", "index", "-b", "-o", ipath, os.path.realpath(track[0])] | |
| 122 sys.stdout.write('#### calling %s' % ' '.join(cmd)) | |
| 123 jc.subprocess_check_call(cmd) | |
| 124 track_conf = {"conf": {"options": {"bam": {"bam_index": ipath}}}} | |
| 125 elif trext == "cram": | |
| 126 ipath = track[3] | |
| 127 if not os.path.exists(ipath): | |
| 128 jc.logging.info('calling %s' % ' '.join(cmd)) | |
| 129 ipath = os.path.realpath(os.path.join('./', trackname + '.crai')) | |
| 130 cmd = ["samtools", "index", "-c", "-o", ipath, os.path.realpath(track[0])] | |
| 131 sys.stdout.write('#### calling %s' % ' '.join(cmd)) | |
| 132 jc.subprocess_check_call(cmd) | |
| 133 track_conf = {"conf": {"options": {"cram": {"cram_index": ipath}}}} | |
| 116 else: | 134 else: | 
| 117 track_conf = {} | 135 track_conf = {} | 
| 118 track_conf["format"] = trext | 136 track_conf["format"] = trext | 
| 119 track_conf["name"] = trackname | 137 track_conf["name"] = trackname | 
| 120 track_conf["label"] = trackname | 138 track_conf["label"] = trackname | 
| 121 track_conf["trackfiles"] = [(tpath, trext, trackname,{}),] | 139 useu = tpath.startswith('http://') or tpath.startswith('https://') | 
| 140 useuri = 'no' | |
| 141 if useu: | |
| 142 useuri = 'yes' | |
| 143 track_conf["trackfiles"] = [ | |
| 144 (tpath, trext, useuri, trackname, {}), | |
| 145 ] | |
| 122 track_conf["category"] = "Autogenerated" | 146 track_conf["category"] = "Autogenerated" | 
| 123 keys = jc.process_annotations(track_conf) | 147 keys = jc.process_annotations(track_conf) | 
| 124 | 148 | 
| 125 if keys: | 149 if keys: | 
| 126 for key in keys: | 150 for key in keys: | 
| 127 if trext in ["bigwig", "gff3", "gff", "vcf", "maf",]: | 151 if trext in [ | 
| 152 "bigwig", | |
| 153 "gff3", | |
| 154 "gff", | |
| 155 "vcf", | |
| 156 "maf", | |
| 157 ]: | |
| 128 default_session_data["visibility"]["default_on"].append(key) | 158 default_session_data["visibility"]["default_on"].append(key) | 
| 129 else: | 159 else: | 
| 130 default_session_data["visibility"]["default_off"].append(key) | 160 default_session_data["visibility"]["default_off"].append( | 
| 131 # if track_conf.get("style", None): | 161 key | 
| 132 # default_session_data["style"][key] = track_conf[ | 162 ) | 
| 133 # "style" | |
| 134 # ] # TODO do we need this anymore? | |
| 135 # if track_conf.get("style_lables", None): | |
| 136 # default_session_data["style_labels"][key] = track_conf.get( | |
| 137 # "style_labels", None | |
| 138 # ) | |
| 139 # general_data = { | 163 # general_data = { | 
| 140 # "analytics": root.find("metadata/general/analytics").text, | 164 # "analytics": root.find("metadata/general/analytics").text, | 
| 141 # "primary_color": root.find("metadata/general/primary_color").text, | 165 # "primary_color": root.find("metadata/general/primary_color").text, | 
| 142 # "secondary_color": root.find("metadata/general/secondary_color").text, | 166 # "secondary_color": root.find("metadata/general/secondary_color").text, | 
| 143 # "tertiary_color": root.find("metadata/general/tertiary_color").text, | 167 # "tertiary_color": root.find("metadata/general/tertiary_color").text, | 
| 150 jc.config_json["tracks"].update(jc.tracksToAdd) | 174 jc.config_json["tracks"].update(jc.tracksToAdd) | 
| 151 else: | 175 else: | 
| 152 jc.config_json["tracks"] = jc.tracksToAdd | 176 jc.config_json["tracks"] = jc.tracksToAdd | 
| 153 jc.write_config() | 177 jc.write_config() | 
| 154 defLoc = makeDefaultLocation() | 178 defLoc = makeDefaultLocation() | 
| 155 default_session_data.update({"defaultLocation": defLoc, "session_name": sessName}) | 179 default_session_data.update( | 
| 180 {"defaultLocation": defLoc, "session_name": sessName} | |
| 181 ) | |
| 156 track_conf.update(default_session_data) | 182 track_conf.update(default_session_data) | 
| 157 jc.add_default_session(default_session_data) | 183 jc.add_default_session(default_session_data) | 
| 158 # jc.text_index() not sure what broke here. | 184 # jc.text_index() not sure what broke here. | 
| 159 else: | 185 else: | 
| 160 print("!! empty collection supplied - nothing to process") | 186 sys.stderr.write( | 
| 187 "!!!! Collection has no suitable trackfiles for autogenJB2 - nothing to process" | |
| 188 ) | 
