#!/usr/bin/env python3

#authors:
# Kelsey Florek (kelsey.florek@slh.wisc.edu)
# Kevin Libuit (kevin.libuit@dgs.virginia.gov)

import sys,os,re
import argparse
import core.calldocker as cd
import core.sb_programs as sb_prog
from core.autopath import path_replacer

if __name__ == '__main__':

    #setup argparser to display help if no arguments
    class MyParser(argparse.ArgumentParser):
        def error(self, message):
            sys.stderr.write('error: %s\n' % message)
            self.print_help()
            sys.exit(2)

    parser = MyParser(usage="staphb_toolkit_run [optional arguments] <application> [application arguments]")
    subparsers = parser.add_subparsers(title='application',metavar='',dest="subparser_name",required=True)
    parser.add_argument("--overide_path",default=False, action='store_true',help="Overide the automatic path mounting that is performed for docker containers (Not yet operational)")

    #parser for applications
    #-----------------------------------------
    parser_spades = subparsers.add_parser('spades', help='SPAdes - St. Petersburg genome assembler', add_help=False)
    parser_mash = subparsers.add_parser('mash', help='MASH - Fast genome and metagenome distance estimation using MinHash', add_help=False)
    parser_seqyclean = subparsers.add_parser('seqyclean', help='SeqyClean - Pre-process and clean NGS data in order to prepare for downstream analysis', add_help=False)
    parser_shovill = subparsers.add_parser('shovill', help='Shovill - Faster SPAdes assembler', add_help=False)
    parser_prokka = subparsers.add_parser('prokka', help='Prokka - Rapid prokaryotic genome annotation', add_help=False)
    parser_abricate = subparsers.add_parser('abricate', help='Abricate - Mass screening of contigs for antimicrobial and virulence genes', add_help=False)
    parser_iqtree = subparsers.add_parser('iqtree', help='IQ-TREE - A fast and effective stochastic algorithm to infer phylogenetic trees by maximum likelihood.', add_help=False)
    parser_lyveset = subparsers.add_parser('lyveset', help='LYVE-SET - a method of using hqSNPs to create a phylogeny.', add_help=False)
    parser_quast = subparsers.add_parser('quast', help='Quast - Genome assembly evaluation tool.', add_help=False)
    parser_roary = subparsers.add_parser('roary', help='Roary - Rapid large-scale prokaryote pan genome analysis.', add_help=False)
    parser_seqsero = subparsers.add_parser('seqsero', help='SeqSero - Salmonella serotyping from genome sequencing data.', add_help=False)
    parser_serotypefinder = subparsers.add_parser('serotypefinder', help='SerotypeFinder - identifies the serotype in total or partial sequenced isolates of E. coli.', add_help=False)
    parser_unicycler = subparsers.add_parser('unicycler', help='Unicycler - an assembly pipeline for bacterial genomes.', add_help=False)
    #-----------------------------------------

    #handel the arguments and perform automatic path replacement
    parser_args = parser.parse_known_args()
    program = parser_args[0].subparser_name
    args = parser_args[1]
    if not parser_args[0].overide_path:
        arg_string,path_map = path_replacer(args,os.getcwd())

    #Program specific execution code
    #-----------------------------------------
    if program == 'spades':
        command = "spades.py "+arg_string
        spades = sb_prog.Run(command=command, path=path_map,docker_image="spades")
        spades.run()

    if program == 'mash':
        command = "mash "+arg_string
        mash = sb_prog.Run(command=command, path=path_map,docker_image="mash")
        mash.run()

    if program == 'seqyclean':
        command = "seqyclean "+arg_string
        seqyclean = sb_prog.Run(command=command, path=path_map,docker_image="seqyclean")
        seqyclean.run()

    if program == 'shovill':
        command = "shovill "+arg_string
        shovill = sb_prog.Run(command=command, path=path_map,docker_image="shovill")
        shovill.run()

    if program == 'prokka':
        command = "prokka "+arg_string
        prokka = sb_prog.Run(command=command, path=path_map,docker_image="prokka")
        prokka.run()

    if program == 'abricate':
        command = "abricate "+arg_string
        abricate = sb_prog.Run(command=command, path=path_map,docker_image="abricate")
        abricate.run()

    if program == 'iqtree':
        command = "iqtree "+arg_string
        iqtree = sb_prog.Run(command=command, path=path_map,docker_image="iqtree")
        iqtree.run()

    if program == 'lyveset':
        command = ""+arg_string
        lyveset = sb_prog.Run(command=command, path=path_map,docker_image="lyveset")
        lyveset.run()

    if program == 'quast':
        command = "quast.py "+arg_string
        quast = sb_prog.Run(command=command, path=path_map,docker_image="quast")
        quast.run()

    if program == 'roary':
        command = "roary "+arg_string
        roary = sb_prog.Run(command=command, path=path_map,docker_image="roary")
        roary.run()

    if program == 'seqsero':
        command = "SeqSero.py "+arg_string
        seqsero = sb_prog.Run(command=command, path=path_map,docker_image="seqsero")
        seqsero.run()

    if program == 'serotypefinder':
        command = "serotypefinder.pl "+arg_string
        serotypefinder = sb_prog.Run(command=command, path=path_map,docker_image="serotypefinder")
        serotypefinder.run()

    if program == 'unicycler':
        command = "unicycler "+arg_string
        unicycler = sb_prog.Run(command=command, path=path_map,docker_image="unicycler")
        unicycler.run()
