def main():
    """
    Main function, where variables are set and other functions get called
    from.
    """

    # Make sure we provide an help message instead of an error
    if len(sys.argv) == 1:
        sys.argv += ["-h"]
    arg = argument_parser(sys.argv[1:])

    # Check the existance of several files:
    # Popfile
    if arg.popfile is not None:
        sanity.file_checker(
            arg.popfile, "The specified popfile '{}' does not "
            "exist.".format(arg.popfile))
    # Indfile
    if arg.indfile is not None:
        sanity.file_checker(
            arg.indfile, "The specified indfile '{}' does not "
            "exist.".format(arg.indfile))

    # Perform usual structure_threader run
    if arg.main_op == "run":

        # Switch relative to absolute paths
        arg.infile = os.path.abspath(arg.infile)
        arg.outpath = os.path.abspath(arg.outpath)

        # Figure out which program we are wrapping
        if "-fs" in sys.argv:
            wrapped_prog = "faststructure"
        elif "-mv" in sys.argv:
            wrapped_prog = "maverick"
        elif "-st" in sys.argv:
            wrapped_prog = "structure"

        # External program
        sanity.file_checker(
            arg.external_prog, "Could not find your external program in "
            "the specified path "
            "'{}'.".format(arg.external_prog))
        # Input file
        sanity.file_checker(
            arg.infile, "The specified infile '{}' does "
            "not exist.".format(arg.infile))
        # Output dir
        sanity.file_checker(
            arg.outpath, "Output argument '{}' is pointing to an "
            "existing file. This argument requires a "
            "directory.".format(arg.outpath), False)

        # Number of Ks
        if isinstance(arg.Ks, int):
            Ks = range(1, arg.Ks + 1)
        else:
            Ks = arg.Ks

        # Number of replicates
        replicates = range(1, arg.replicates + 1)

        threads = sanity.cpu_checker(arg.threads)

        signal.signal(signal.SIGINT, gracious_exit)

        structure_threader(Ks, replicates, threads, wrapped_prog, arg)

        if wrapped_prog == "maverick":
            bestk = maverick_merger(arg.outpath, Ks, arg.notests)
            arg.notests = True

        if arg.notests is False:
            bestk = structure_harvester(arg.outpath, wrapped_prog)
        else:
            bestk = Ks

        if arg.noplot is False:
            create_plts(arg.outpath, wrapped_prog, Ks, bestk, arg)

    # Perform only plotting operation
    if arg.main_op == "plot":

        plots_only(arg)
Beispiel #2
0
def argument_sanity(arguments, parser):
    """
    Performs some sanity checks on the user provided arguments.
    """
    if arguments.main_op == "run":
        # External program
        sanity.file_checker(
            arguments.external_prog, "Could not find your external program in "
            "the specified path "
            "'{}'.".format(arguments.external_prog))

        # Input file
        sanity.file_checker(
            arguments.infile, "The specified infile '{}' does not "
            "exist.".format(arguments.infile))

        # Output dir
        sanity.file_checker(
            arguments.outpath, "Output argument '{}' is pointing to an "
            "existing file. This argument requires a "
            "directory.".format(arguments.outpath), False)

        # Handle argparse limitations with "--" options.
        if arguments.extra_options != "":
            if "-st" not in sys.argv:
                arguments.extra_options = \
                    "--{0}".format(arguments.extra_options)
                arguments.extra_options = \
                    " --".join(arguments.extra_options.split())

        # fastStructure is really only usefull with either a pop or indfile...
        if "-fs" in sys.argv and\
            arguments.popfile is None and\
                arguments.indfile is None:
            parser.error("-fs requires either --pop or --ind.")

        # Make sure we provide paths for mainparams, extraparams and
        # parameters.txt  depending on the wrapped program.
        if arguments.params is not None:
            arguments.params = os.path.abspath(arguments.params)
        if "-mv" in sys.argv and arguments.params is None:
            parser.error("-mv requires --params.")
        elif "-mv" in sys.argv:
            sanity.file_checker(os.path.abspath(arguments.params))
        elif "-st" in sys.argv and arguments.params is None:
            arguments.params = os.path.join(os.path.dirname(arguments.infile),
                                            "mainparams")

        # Number of replicates
        arguments.replicates = range(1, arguments.replicates + 1)

        arguments.threads = sanity.cpu_checker(arguments.threads)

    elif arguments.main_op == "plot":
        if arguments.program == "faststructure" and arguments.popfile is None\
                and arguments.indfile is None:
            parser.error("fastStructure plots require either --pop or --ind.")

    elif arguments.main_op == "run" or arguments.main_op == "plot":
        # Check the existance of several files:
        # Popfile
        if arguments.popfile is not None:
            sanity.file_checker(
                arguments.popfile, "The specified popfile '{}' does not "
                "exist.".format(arguments.popfile))
        # Indfile
        if arguments.indfile is not None:
            sanity.file_checker(
                arguments.indfile, "The specified indfile '{}' does not "
                "exist.".format(arguments.indfile))

    return arguments
def main():
    """
    Main function, where variables are set and other functions get called
    from.
    """

    arg = argument_parser(sys.argv[1:])

    # Check the existance of several files:
    # Popfile
    if arg.popfile is not None:
        sanity.file_checker(
            arg.popfile, "The specified popfile '{}' does not "
            "exist.".format(arg.popfile))
    # Indfile
    if arg.indfile is not None:
        sanity.file_checker(
            arg.indfile, "The specified indfile '{}' does not "
            "exist.".format(arg.indfile))

    # Perform usual structure_threader run
    if arg.main_op == "run":

        # Switch relative to absolute paths
        arg.infile = os.path.abspath(arg.infile)
        arg.outpath = os.path.abspath(arg.outpath)

        # Figure out which program we are wrapping
        if "-fs" in sys.argv:
            wrapped_prog = "fastStructure"
        elif "-mv" in sys.argv:
            wrapped_prog = "maverick"
        elif "-st" in sys.argv:
            wrapped_prog = "structure"

        # External program
        sanity.file_checker(
            arg.external_prog, "Could not find your external program in "
            "the specified path "
            "'{}'.".format(arg.external_prog))
        # Input file
        sanity.file_checker(
            arg.infile, "The specified infile '{}' does "
            "not exist.".format(arg.infile))
        # Output dir
        sanity.file_checker(
            arg.outpath, "Output argument '{}' is pointing to an "
            "existing file. This argument requires a "
            "directory.".format(arg.outpath), False)

        # Number of Ks
        if isinstance(arg.Ks, int):
            Ks = range(1, arg.Ks + 1)
        else:
            Ks = arg.Ks

        # Number of replicates
        replicates = range(1, arg.replicates + 1)

        threads = sanity.cpu_checker(arg.threads)

        signal.signal(signal.SIGINT, gracious_exit)

        structure_threader(Ks, replicates, threads, wrapped_prog, arg)

        if wrapped_prog == "maverick":
            bestk = maverick_merger(arg.outpath, Ks, arg.notests)
            arg.notests = True

        if arg.notests is False:
            bestk = structure_harvester(arg.outpath, wrapped_prog)
        else:
            bestk = Ks

        if arg.noplot is False:
            create_plts(arg.outpath, wrapped_prog, Ks, bestk, arg)

    # Perform only plotting operation
    if arg.main_op == "plot":

        # Get all files matching the provided prefix
        if arg.format == "fastStructure":
            infiles = [
                x for x in os.listdir(".")
                if x.startswith(arg.prefix) and x.endswith(".meanQ")
            ]
        elif arg.format == "structure":
            infiles = [
                x for x in os.listdir(".")
                if x.startswith(arg.prefix) and "rep1_" in x
            ]
        else:
            infiles = [
                x for x in os.listdir(".")
                if x.startswith(arg.prefix) and x.endswith(".csv")
            ]

        if not infiles:
            print("ERROR: There are no input files that match the"
                  " provided prefix")
            raise SystemExit

        if not os.path.exists(arg.outpath):
            os.makedirs(arg.outpath)

        bestk = [int(x) for x in arg.bestk]

        sp.main(infiles,
                arg.format,
                arg.outpath,
                bestk,
                popfile=arg.popfile,
                indfile=arg.indfile,
                filter_k=bestk)