def plots_only(arg):
    """
    Handles arrugments and wraps things up for drawing the plots without
    running any wrapped programs.
    """
    # Relative to abs path
    prefix_abs_path = os.path.abspath(arg.prefix)
    # Get all files matching the provided prefix
    prefix_dir, prefix_name = os.path.split(prefix_abs_path)

    if prefix_dir == "":
        prefix_dir = "."
    k_vals = arg.bestk

    if arg.program == "faststructure":
        infiles = [
            os.path.join(prefix_abs_path, "fS_run_K." + x + ".meanQ")
            for x in k_vals
        ]

    elif arg.program == "structure":
        infiles = [
            os.path.join(prefix_abs_path, "str_K" + x + "_rep1_f")
            for x in k_vals
        ]

    else:
        infiles = [
            os.path.join(os.path.join(prefix_abs_path, "mav_K" + x),
                         "outputQmatrix_ind_K" + x + ".csv") for x in k_vals
        ]

    for filename in infiles:
        sanity.file_checker(
            filename, "There was a problem with the deduced "
            "filename '{}'. Please check "
            "it.".format(filename))

    if not infiles:
        logging.error("No input files that match the provided prefix. "
                      "Aborting.")
        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.program,
            arg.outpath,
            bestk,
            popfile=arg.popfile,
            indfile=arg.indfile,
            filter_k=bestk,
            bw=arg.blacknwhite,
            use_ind=arg.use_ind)
Beispiel #2
0
def plots_only(arg):
    """
    Handles arrugments and wraps things up for drawing the plots without Running
    any wrapped programs.
    """
    # Get all files matching the provided prefix
    prefix_dir, prefix_name = os.path.split(arg.prefix)

    if prefix_dir == "":
        prefix_dir = "."

    if arg.program == "faststructure":
        infiles = [
            os.path.join(prefix_dir, x) for x in os.listdir(prefix_dir)
            if x.startswith(prefix_name) and x.endswith(".meanQ")
        ]
    elif arg.program == "structure":
        infiles = [
            os.path.join(prefix_dir, x) for x in os.listdir(prefix_dir)
            if x.startswith(prefix_name) and "rep1_" in x
        ]
    else:
        infiles = [
            os.path.join(prefix_dir, x) for x in os.listdir(prefix_dir)
            if x.startswith(prefix_name) and x.endswith(".csv")
        ]

    for filename in infiles:
        sanity.file_checker(
            filename, "There was a problem with the deduced "
            "filename '{}'. Please check "
            "it.".format(filename))

    if not infiles:
        logging.error("No input files that match the provided prefix. "
                      "Aborting.")
        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.program,
            arg.outpath,
            bestk,
            popfile=arg.popfile,
            indfile=arg.indfile,
            filter_k=bestk,
            bw=arg.blacknwhite,
            use_ind=arg.use_ind)
def spooky_scary_skeletons(arg):
    """
    Generates skeleton parameter files for STRUCTURE.
    """
    try:
        import structure_threader.skeletons.stparams as parameters
    except ImportError:
        import skeletons.stparams as parameters

    sanity.file_checker(arg.outpath, is_file=False)
    main_file = os.path.join(arg.outpath, "mainparams")
    with open(main_file, 'w') as fhandle:
        fhandle.write(parameters.MAINPARAMS)
    extra_file = os.path.join(arg.outpath, "extraparams")
    with open(extra_file, 'w') as fhandle:
        fhandle.write(parameters.EXTRAPARAMS)
Beispiel #4
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.
    """

    # 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)
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)