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)
예제 #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 create_plts(wrapped_prog, bestk, arg):
    """
    Create plots from result dir.
    :param resultsdir: path to results directory
    """

    outdir = os.path.join(arg.outpath, "plots")
    if not os.path.exists(outdir):
        os.mkdir(outdir)

    if wrapped_prog == "structure":
        # Get only relevant output files, choosen randomly from the replictes.
        # Failsafe in case we only have 1 replicate:
        if arg.replicates == 1:
            file_to_plot = "1"
        else:
            file_to_plot = str(choice(arg.replicates))
        plt_files = [
            os.path.join(arg.outpath, "str_K") + str(i) + "_rep" +
            file_to_plot + "_f" for i in arg.k_list
        ]
    elif wrapped_prog == "maverick":
        plt_files = [
            os.path.join(os.path.join(arg.outpath, "mav_K" + str(i)),
                         "outputQmatrix_ind_K" + str(i) + ".csv")
            for i in arg.k_list
        ]
    elif wrapped_prog == "alstructure":
        plt_files = [
            os.path.join(os.path.join(arg.outpath, "alstr_K" + str(i)))
            for i in arg.k_list
        ]

    else:
        plt_files = [
            os.path.join(arg.outpath, "fS_run_K.") + str(i) + ".meanQ"
            for i in arg.k_list
        ]

    sp.main(plt_files,
            wrapped_prog,
            outdir,
            bestk=bestk,
            popfile=arg.popfile,
            indfile=arg.indfile,
            bw=arg.blacknwhite,
            use_ind=arg.use_ind)
def create_plts(resultsdir, wrapped_prog, Ks, bestk, arg):
    """
    Create plots from result dir.
    :param resultsdir: path to results directory
    """

    plt_list = [x for x in Ks if x != 1]  # Don't plot K=1

    outdir = os.path.join(resultsdir, "plots")
    if not os.path.exists(outdir):
        os.mkdir(outdir)

    if wrapped_prog == "structure":
        # Get only relevant output files, choosen randomly from the replictes.
        # Failsafe in case we only have 1 replicate:
        if arg.replicates == 1:
            file_to_plot = "1"
        else:
            file_to_plot = str(randrange(1, arg.replicates + 1))
        plt_files = [
            os.path.join(resultsdir, "str_K") + str(i) + "_rep" +
            file_to_plot + "_f" for i in plt_list
        ]
    elif wrapped_prog == "maverick":
        plt_files = [
            os.path.join(os.path.join(resultsdir, "mav_K" + str(i)),
                         "outputQmatrix_ind_K" + str(i) + ".csv")
            for i in plt_list
        ]

    else:
        plt_files = [
            os.path.join(resultsdir, "fS_run_K.") + str(i) + ".meanQ"
            for i in plt_list
        ]

    sp.main(plt_files,
            wrapped_prog,
            outdir,
            bestk=bestk,
            popfile=arg.popfile,
            indfile=arg.indfile)
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)