view autogenJB2.py @ 30:8f02a84ee278 draft

planemo upload for repository https://github.com/usegalaxy-eu/temporary-tools/tree/master/jbrowse2 commit 48bc917d34af182e9158915862c8a35723660919
author fubar
date Wed, 21 Feb 2024 02:57:30 +0000
parents
children 15da358c3108
line wrap: on
line source

import argparse
import re
import sys

from jbrowse2 import jbrowseConnector as jbC


def makeDefaultLocation(jc, defLoc=None):

    refName = None
    drdict = {
        "reversed": False,
        "assemblyName": jc.genome_name,
        "start": 0,
        "end": 100000,
    }

    if defLoc:
        loc_match = re.search(r"^([^:]+):([\d,]*)\.*([\d,]*)$", defLoc)
        # allow commas like 100,000 but ignore as integer
        if loc_match:
            refName = loc_match.group(1)
            drdict["refName"] = refName
            if loc_match.group(2) > "":
                drdict["start"] = int(loc_match.group(2).replace(",", ""))
            if loc_match.group(3) > "":
                drdict["end"] = int(loc_match.group(3).replace(",", ""))
        else:
            jc.logging.info(
                "@@@ regexp could not match contig:start..end in the supplied location %s - please fix"
                % defLoc
            )
    else:
        drdict["refName"] = jc.genome_firstcontig
    if drdict.get("refName", None):
        jc.logging.info("@@@ defaultlocation %s for default session" % drdict)
        return drdict
    else:
        jc.logging.info("@@@ no contig name found for default session - please add one!")
        return None


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="", epilog="")
    parser.add_argument("--sessname", help="Session name", default="AutoJBrowse")
    parser.add_argument(
        "--collection",
        help="Collection of 'filepath, filename, filext' for JBrowse2",
        default=[],
        action="extend",
    )
    parser.add_argument("--version", "-V", action="version", version="%(prog)s 0.0.1")
    args = parser.parse_args()
    sessName = args.sessname
    flistList = [x.split(",") for x in args.collection]
    if flistList:
        listgenomes = [f for f in flistList if f[0].startswith("REFERENCE_")]
        if len(listgenomes) > 0:
            genome_paths = [
                x[1] for x in listgenomes
            ]  # expect genome_1_genomename.fasta etc
            genome_names = [x[0].split("REFERENCE_")[1] for x in listgenomes]
            jc = jbC(
                outdir=args.outdir,
                genomes=[
                    {
                        "path": x,
                        "meta": {
                            "name": genome_names[i],
                        },
                    }
                    for i, x in enumerate(genome_paths)
                ],
            )
            jc.process_genomes()
            default_session_data = {
                "visibility": {
                    "default_on": [],
                    "default_off": [],
                },
                "style": {},
                "style_labels": {},
            }
            defLoc = makeDefaultLocation(jc)
            listtracks = [f for f in flistList if not f[0].startswith("REFERENCE_")]
            # foo.paf must have a foo_paf.fasta or fasta.gz to match
            tnames = [x[0] for x in listtracks]
            texts = [x[2] for x in listtracks]
            for i, track in enumerate(listtracks):
                if track[2] == "paf":
                    refname = track[0] + "_paf.fasta"
                    refdat = [x[1] for x in listtracks if x[0] == refname]
                    if not refdat:
                        jc.logging.warn(
                            "!! No reference file %s corresponding to paf file %s found. Not building - there must be a corresponding fasta for each paf"
                            % (refname, tnames[i])
                        )
                        sys.exit(3)
                    else:
                        track_conf = {
                            "conf": {
                                "options": {
                                    "paf": {"genome": refdat, "genome_label": track[0]}
                                }
                            }
                        }
                else:
                    track_conf = {}
                track_conf["format"] = track[2]
                track_conf["name"] = track[0]
                track_conf["label"] = track[0]
                track_conf["trackfiles"] = []
                keys = jc.process_annotations(track_conf)

                if keys:
                    for key in keys:
                        default_session_data["visibility"][
                            track.attrib.get("visibility", "default_off")
                        ].append(key)
                        # if track_conf.get("style", None):
                        # default_session_data["style"][key] = track_conf[
                        # "style"
                        # ]  # TODO do we need this anymore?
                        # if track_conf.get("style_lables", None):
                        # default_session_data["style_labels"][key] = track_conf.get(
                        # "style_labels", None
                        # )
            # general_data = {
            # "analytics": root.find("metadata/general/analytics").text,
            # "primary_color": root.find("metadata/general/primary_color").text,
            # "secondary_color": root.find("metadata/general/secondary_color").text,
            # "tertiary_color": root.find("metadata/general/tertiary_color").text,
            # "quaternary_color": root.find("metadata/general/quaternary_color").text,
            # "font_size": root.find("metadata/general/font_size").text,
            # }
            # jc.add_general_configuration(general_data)
            trackconf = jc.config_json.get("tracks", None)
            if trackconf:
                jc.config_json["tracks"].update(jc.tracksToAdd)
            else:
                jc.config_json["tracks"] = jc.tracksToAdd
            jc.write_config()
            defaultData = {"defaultLocation": defLoc, "session_name": sessName}
            jc.add_default_session(defaultData)
            # jc.text_index() not sure what broke here.
    else:
        sys.stderr.write("!! empty collection supplied - nothing to process")