#!/apps/rocs/2020.08/cascadelake/software/Anaconda3/2021.05/bin/python

import os
import sys
import argparse

help_0 = """
Usage: splicekit <command> [options]

Commands:
  -- Processing
     process           run all available analyses

  -- Initialization
     setup             initialize folder structure
     annotation        download sample annotation from Moose
     features          create junction/exon/anchor count tables from .bam files
  
  -- Splicing analyses
     edgeR             run edgeR analyses on junctions, exons and anchors
     judge             run juDGE plot analysis
     promisc           promiscuity analysis: genes and their junctions changes across conditions
     clusterlogfc      log fold-change clusters of significant changes (FDR<0.05) on junction, exon, gene level

  -- scanRBP, motif and sequence analyses
     motifs            run motif and scanRBP analysis

  -- JBrowse2 genomic browser
     jbrowse2 [process | start] processes (creates) all required JBrowse2 files and/or starts JBrowse2 web server

Options

  -- hostname
     If you are submitting 'splicekit process' to the cluster, you can provide a custom hostname. This will be used to create JBrowse2 URLs.

"""

help_edgeR = """
Usage: splicekit edgeR [sub-command]

Sub-commands:
     junctions      run edgeR on junctions table data
     exons          run edgeR on exons table data
     anchors        run edgeR on anchors table data
     genes          run edgeR on genes table data

If no sub-command is given, all analyses will be run (junctions, exons, anchors, genes).
"""

parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, add_help=False)
parser.add_argument('command', help="command(s) to run", nargs='*')
parser.add_argument("-help", "-h", "--help", action="store_true")
parser.add_argument("-version", "--version", help="Print version", action="store_true")
parser.add_argument("-force", "--force", help="Force recreation / overwrite of results", action="store_true", default=False)
parser.add_argument("-hostname", "--hostname", help="If you are submitting 'splicekit process' to the cluster, you can provide a custom hostname. This will be used to create JBrowse2 URLs.", default=None)
args = parser.parse_args()

# lookup version without loading splicekit
import importlib
splicekit_init = importlib.util.find_spec("splicekit")
splicekit_path = splicekit_init.origin
splicekit_folder = os.path.dirname(splicekit_path)
version = open(os.path.join(splicekit_folder, "version"), "rt").readlines()[0].replace("\n", "").replace("\r", "")

print(f"[splicekit] v{version}, https://github.com/bedapub/splicekit")
print()

if args.version:
    sys.exit(0)

if len(args.command)==0 and args.help:
    print(help_0)
    sys.exit(0)

if len(args.command)==0 and not args.help:
    print()
    print("[splicekit] Please specify an action to perform or specify --help to get help on available actions. Running 'splicekit process' will perform all available analyses.")
    sys.exit(0)

# loading splicekit modules takes time, only load if the user is not asking to display help
if not args.help:
    import splicekit
    if args.hostname!=None:
        splicekit.config.jbrowse2_config(args.hostname)

if args.command[0]=="basic":
    splicekit.setup()
    splicekit.annotation()
    splicekit.features()
    splicekit.edgeR()
elif args.command[0]=="setup":
    splicekit.setup()
elif args.command[0]=="annotation":
    splicekit.annotation()
elif args.command[0]=="junctions":
    splicekit.junctions()
elif args.command[0]=="junctions_master":
    splicekit.junctions_master()
elif args.command[0]=="exons":
    splicekit.exons()
elif args.command[0]=="genes":
    splicekit.genes()
elif args.command[0]=="anchors":
    splicekit.anchors()
elif args.command[0]=="features":
    splicekit.features()
elif args.command[0]=="edgeR":
    if args.help:
        print(help_edgeR)
        sys.exit(0)
    if len(args.command)>1 and args.command[1] in ["junctions", "exons", "anchors", "genes"]:
        splicekit.core.features.load_genes()
        splicekit.edgeR(run=args.command[1])
    elif len(args.command)==1:
        splicekit.edgeR()
    else:
        print(help_edgeR)
        sys.exit(0)
elif args.command[0]=="motifs":
    splicekit.motifs()
elif args.command[0]=="promisc":
    splicekit.promisc()
elif args.command[0]=="cassettes":
    splicekit.cassettes()
elif args.command[0]=="patterns":
    splicekit.patterns()
elif args.command[0]=="clusterlogfc":
    splicekit.clusterlogfc_process()
elif args.command[0]=="process":
    splicekit.process(force=args.force)
elif args.command[0]=="judge":
    splicekit.judge_process()
elif args.command[0]=="juan":
    splicekit.juan()
elif args.command[0] == "jbrowse2":
    if len(args.command)>1:
        if args.command[1]=="process":
            splicekit.jbrowse2_process(force_samples=args.force, force_annotation=args.force)
        if args.command[1]=="start":
            splicekit.core.jbrowse2.start()
    else:
        splicekit.jbrowse2_process(force_samples=args.force, force_annotation=args.force)
        splicekit.core.jbrowse2.start()
else:
    print(help_0)
