Ejemplo n.º 1
0
def parse_arguments():

    parser = argparse.ArgumentParser(description='redraw trees only')

    parser.add_argument("--env",
                        action="store",
                        dest="path_env",
                        required=True)

    parser.add_argument("--project",
                        action="store",
                        dest="path_project",
                        required=True)

    parser.add_argument("--config", nargs='+', dest="configs", required=True)

    # parse arguments
    params = parser.parse_args()

    # read environment configuration
    envs = utils.read_json_config(params.path_env)

    # get config json files
    config_jsons = utils.handle_config_args(params.path_project,
                                            params.configs)

    return params, envs, config_jsons
Ejemplo n.º 2
0
def parse_arguments():

    parser = argparse.ArgumentParser(description='run simulation')

    parser.add_argument("--env",
                        action="store",
                        dest="path_env",
                        required=True)

    parser.add_argument("--project",
                        action="store",
                        dest="path_project",
                        required=True)

    parser.add_argument("--config", nargs='+', dest="configs", required=True)

    parser.add_argument("--quiet", action="store_true", dest="quiet")

    parser.add_argument("--simulate-tree-only",
                        action="store_true",
                        default=False,
                        dest="simulate_tree_only")

    # parse arguments
    params = parser.parse_args()

    # read environment configuration
    envs = utils.read_json_config(params.path_env)

    # get config json files
    config_jsons = utils.handle_config_args(params.path_project,
                                            params.configs)

    return params, envs, config_jsons
Ejemplo n.º 3
0
def get_simulation_output_path(path_project, config_filename):

    # read simulation configuration
    config = utils.read_json_config(os.path.join(path_project,
                                                 config_filename))

    path_simulation_output = os.path.join(
        path_project, config[const.CONFIG_PATH_RELATIVE_OUTPUT])

    return path_simulation_output
Ejemplo n.º 4
0
def run(path_matlab, path_project, config_filename, simulate_tree_only, quiet):
    "run function"

    # read simulation configuration
    config = utils.read_json_config(os.path.join(path_project,
                                                 config_filename))

    # get seed from simulation.xml
    seed = get_seed_from_simulation_xml(
        os.path.join(
            path_project,
            config.get(const.CONFIG_PROGRAM_FILE, const.FILE_SIMULATION_XML)))

    # run stochastic lineage tree simulation
    simulate_lineage_tree(path_matlab, path_project, config_filename)

    path_simulation_output = os.path.join(
        path_project, config[const.CONFIG_PATH_RELATIVE_OUTPUT])

    # take simulation tree and make ascii plot
    generate_tree_ascii_plot(
        os.path.join(path_simulation_output, const.FILE_SIMULATION_NEWICK))

    # run genotyping simulation (wga proportion, dropout, coverage)
    # output mutation table
    run_genotyping_simulation(
        config.get(const.CONFIG_SIMULATION_BIALLELIC, 'False'), path_project,
        path_simulation_output, seed)

    # user wants to generate tree only
    if simulate_tree_only:
        return

    methods = []

    # if biallelic=true
    if config.get(const.CONFIG_SIMULATION_BIALLELIC, 'False'):
        methods = handle_biallelic(path_project, path_simulation_output)
    else:
        methods = handle_monoallelic(path_project, path_simulation_output)

    for path_reconstruction_output, calling in methods:

        # reconstruct using TMC based on mutation table generated from simulation
        reconstruct_TMC(
            calling, path_reconstruction_output, 'root',
            config.get(const.CONFIG_RECONSTRUCT_SCORING_METHOD, 'uri10'),
            config.get(const.CONFIG_RECONSTRUCT_CHOOSING_METHOD, 'mms'), quiet)

        # take reconstructed tree and make ascii plot
        generate_tree_ascii_plot(
            os.path.join(path_reconstruction_output,
                         const.FILE_RECONSTRUCTED_NEWICK))

        # highlight tree differences and save to png
        highlight_tree_differences_to_png(
            envs[const.ENV_MATLAB_KEY],
            os.path.join(path_simulation_output, const.FILE_SIMULATION_NEWICK),
            os.path.join(path_reconstruction_output,
                         const.FILE_RECONSTRUCTED_NEWICK),
            os.path.join(path_reconstruction_output, const.FILE_SISTERS_COUNT),
            os.path.join(path_reconstruction_output, const.FILE_DIFF_METRICS))

        # compare simulation tree and reconstructed tree
        compare(
            os.path.join(path_simulation_output, const.FILE_SIMULATION_NEWICK),
            os.path.join(path_reconstruction_output,
                         const.FILE_RECONSTRUCTED_NEWICK),
            os.path.join(path_reconstruction_output,
                         const.FILE_COMPARISON_METRICS_RAW))

        # report the final comparison metrics in a pretty format
        report(
            os.path.join(path_reconstruction_output,
                         const.FILE_COMPARISON_METRICS_RAW),
            os.path.join(path_reconstruction_output,
                         const.FILE_COMPARISON_METRICS_PRETTY), quiet)
Ejemplo n.º 5
0
def make_html(report_title, path_project, config_jsons,
              exclude_mutation_table):

    templ = """
<html>
<head>
    <title>{{reportTitle}}</title>
    <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Open+Sans" />
    <style>
        table {
            font-family: "Open Sans";
            font-size: 14px;
        }
        .tree-img {
            width: 650px;
        }
        .metrics-table {
            min-width: 390px;
        }
        .overall-score {
            font-family: "Open Sans";
            font-size: 120px;
            text-align: center;
            width: 40%;
        }
    </style>
</head>
<body>

    {% for item in items %}
    <h1>{{item.config.contents.title}} ({{item.config.filename}})</h1>
    <hr/>

    <h2>Parameters</h2>
    <table>
    <tr>
        <td style="width:230px">Simulation Seed:</td>
        <td>{{item.simulation.seed}}</td>
    </tr>
    <tr>
        <td>Simulation Time:</td>
        <td>{{item.simulation.time}}</td>
    </tr>
    <tr>
        <td>Number of Microsatellite Loci:</td>
        <td>{{item.simulation.numOfLoci}}
    </tr>
    <tr>
        <td>Mutation:</td>
        <td>{{item.config.contents.addMutations}}{% if item.config.contents.mutationSpeed %} / Speed {{item.config.contents.mutationSpeed}} {% endif %}</td>
    </tr>
    <tr>
        <td>Allelic Dropouts:</td>
        <td>{{item.config.contents.addAllelicDropOuts}}</td>
    </tr>
    <tr>
        <td>Noises:</td>
        <td>{{item.config.contents.addNoises}}</td>
    </tr>
    <tr>
        <td>Early Stop Population:</td>
        <td>{{item.config.contents.earlyStopPopulation}}</td>
    </tr>
    <tr>
        <td>Scoring Method:</td>
        <td>{% if item.config.contents.scoringMethod %}{{item.config.contents.scoringMethod}}{% else %}probably uri10?{% endif %}</td>
    </table>

    <h2>Mutation Table</h2>
    {{item.simulation.mutationTable}}

    <p style="page-break-after: always;">&nbsp;</p>

    <table>
    <tr>
        <td>
            <img src="{{item.simulation.img}}" class="tree-img"/>
        </td>
        <td>
            <img src="{{item.reconstructed.img}}" class="tree-img"/>
        </td>
    </tr>
    </table>

    <table cellpadding="0" cellspacing="0" border="0" style="width:100%" width="100%">
    <tr>
        <td>{{item.metrics.compare1}}</td>
        <td class="overall-score">{{item.metrics.compare2}}</td>
    </tr>
    </table>

    <p style="page-break-after: always;">&nbsp;</p>

    <table>
    <tr>
        <td>
            <pre>{{item.simulation.ascii}}</pre>
        </td>
        <td style="padding-left:30px;">
            <pre>{{item.reconstructed.ascii}}</pre>
        </td>
    </tr>
    </table>

    <p style="page-break-after: always;">&nbsp;</p>

    {% endfor %}

</body>
</html>
"""

    items = []

    for config_json in config_jsons:

        if not config_json:
            continue
        if not os.path.exists(os.path.join(path_project, config_json)):
            raise Exception("Unable to find {}".format(config_json))

        # read simulation configuration
        config = utils.read_json_config(os.path.join(path_project,
                                                     config_json))

        path_output = os.path.join(path_project,
                                   config[const.CONFIG_PATH_RELATIVE_OUTPUT])

        # path_diff_metrics = os.path.join(path_output, const.FILE_DIFF_METRICS)

        # diff_metrics = get_diff_metrics(path_diff_metrics)
        # overall_score = '{0:2.1f}%'.format((diff_metrics['total'] - (
        #     diff_metrics['diff'] + diff_metrics['missing'])) / diff_metrics['total'] * 100.0)

        if exclude_mutation_table:
            mutation_table = "excluded"
        else:
            mutation_table = convert_mutation_table_to_html(
                path_output, const.FILE_MUTATION_TABLE)

        df_metrics, metrics_html = convert_metrics_to_html(
            path_output, const.FILE_COMPARISON_METRICS_RAW)

        sim_xdoc = read_simulation_xml(
            os.path.join(path_project, const.FILE_SIMULATION_XML))

        sim_seed, sim_time = get_sim_seed_time(sim_xdoc)
        num_of_ms_loci = get_num_of_ms_loci(sim_xdoc)

        item = {
            "config": {
                "filename": config_json,
                "contents": config
            },
            "simulation": {
                "time":
                sim_time,
                "seed":
                sim_seed,
                "numOfLoci":
                num_of_ms_loci,
                "mutationTable":
                mutation_table,
                "img":
                "{}/{}.png".format(config[const.CONFIG_PATH_RELATIVE_OUTPUT],
                                   const.FILE_SIMULATION_NEWICK)
            },
            "reconstructed": {
                "img":
                "{}/{}.png".format(config[const.CONFIG_PATH_RELATIVE_OUTPUT],
                                   const.FILE_RECONSTRUCTED_NEWICK)
            },
            "metrics": {
                "compare1": metrics_html,
                "compare2": df_metrics.loc['Triples_toYuleAvg'][0]
            }
        }

        item["simulation"]["ascii"] = get_ascii_plot(
            os.path.join(path_output, const.FILE_SIMULATION_NEWICK))

        item["reconstructed"]["ascii"] = get_ascii_plot(
            os.path.join(path_output, const.FILE_RECONSTRUCTED_NEWICK))

        items.append(item)

    template = Template(templ)
    html = template.render(reportTitle=report_title, items=items)

    with open(os.path.join(path_project, const.FILE_REPORT_HTML),
              'wt') as fout:
        fout.write(html)
        fout.write('\n')