Example #1
0
def get_r_tikz_corr_plot(nsels, time_stats):
    """
    @param time_stats: a list of stats for each time point
    @return: tikz code corresponding to an R plot
    """
    out = StringIO()
    time_stats_trans = zip(*time_stats)
    y_low = -1
    y_high = 1
    ylim = RUtil.mk_call_str("c", y_low, y_high)
    print >> out, RUtil.mk_call_str(
        "plot",
        "my.table$t",
        "my.table$corr.mi.diag.approx",
        type='"n"',
        ylim=ylim,
        xlab='"time"',
        ylab='"correlation"',
        main='"correlation with mutual information"',
    )
    colors = ("red", "orange", "green", "blue", "black")
    plot_indices = (7, 8, 9, 10, 11)
    for c, plot_index in zip(colors, plot_indices):
        header = g_time_stats_headers[plot_index]
        print >> out, RUtil.mk_call_str("lines", "my.table$t", "my.table$%s" % header, col='"%s"' % c)
    return out.getvalue()
Example #2
0
def get_r_tikz_info_plot(nsels, time_stats):
    """
    @param time_stats: a list of stats for each time point
    @return: tikz code corresponding to an R plot
    """
    out = StringIO()
    time_stats_trans = zip(*time_stats)
    y_low = 0
    y_high = math.log(2)
    ylim = RUtil.mk_call_str("c", y_low, y_high)
    print >> out, RUtil.mk_call_str(
        "plot",
        "my.table$t",
        "my.table$info.mi.diag.approx",
        type='"n"',
        ylim=ylim,
        xlab='"time"',
        ylab='"info"',
        main='"informativeness with respect to MI"',
    )
    colors = ("red", "orange", "green", "blue", "black")
    plot_indices = (17, 18, 19, 20, 21)
    for c, plot_index in zip(colors, plot_indices):
        header = g_time_stats_headers[plot_index]
        print >> out, RUtil.mk_call_str("lines", "my.table$t", "my.table$%s" % header, col='"%s"' % c)
    return out.getvalue()
Example #3
0
def get_r_tikz_prop_plot(nsels, time_stats):
    """
    @param time_stats: a list of stats for each time point
    @return: tikz code corresponding to an R plot
    """
    out = StringIO()
    time_stats_trans = zip(*time_stats)
    y_low = 0
    y_high = 1
    ylim = RUtil.mk_call_str("c", y_low, y_high)
    print >> out, RUtil.mk_call_str(
        "plot",
        "my.table$t",
        "my.table$prop.mi.diag.approx",
        type='"n"',
        ylim=ylim,
        xlab='"time"',
        ylab='"proportion"',
        main='"proportion of same sign difference as MI"',
    )
    colors = ("red", "orange", "green", "blue", "black")
    plot_indices = (12, 13, 14, 15, 16)
    for c, plot_index in zip(colors, plot_indices):
        header = g_time_stats_headers[plot_index]
        print >> out, RUtil.mk_call_str("lines", "my.table$t", "my.table$%s" % header, col='"%s"' % c)
    return out.getvalue()
Example #4
0
def get_r_tikz_corr_plot(nsels, time_stats):
    """
    @param time_stats: a list of stats for each time point
    @return: tikz code corresponding to an R plot
    """
    out = StringIO()
    time_stats_trans = zip(*time_stats)
    y_low = -1
    y_high = 1
    ylim = RUtil.mk_call_str('c', y_low, y_high)
    print >> out, RUtil.mk_call_str(
            'plot',
            'my.table$t',
            'my.table$corr.mi.diag.approx',
            type='"n"',
            ylim=ylim,
            xlab='"time"',
            ylab='"correlation"',
            main='"correlation with mutual information"')
    colors = ('red', 'orange', 'green', 'blue', 'black')
    plot_indices = (7, 8, 9, 10, 11)
    for c, plot_index in zip(colors, plot_indices):
        header = g_time_stats_headers[plot_index]
        print >> out, RUtil.mk_call_str(
                'lines',
                'my.table$t',
                'my.table$%s' % header,
                col='"%s"' % c)
    return out.getvalue()
Example #5
0
def get_r_tikz_mi_plot_script(nsels, time_stats):
    """
    At each time point plot mutual information for all matrices.
    @param time_stats: a list of stats for each time point
    @return: tikz code corresponding to an R plot
    """
    out = StringIO()
    time_stats_trans = zip(*time_stats)
    mi_mut = time_stats_trans[1]
    mi_min_sels = time_stats_trans[6]
    mi_max_sels = time_stats_trans[2]
    y_low = min(mi_min_sels + mi_mut)
    y_high = max(mi_max_sels + mi_mut)
    ylim = RUtil.mk_call_str('c', y_low, y_high)
    print >> out, RUtil.mk_call_str(
            'plot',
            'my.table$t',
            'my.table$mut',
            type='"n"',
            ylim=ylim,
            xlab='"time"',
            ylab='"MI"',
            main='"MI for mut process and %d mut.sel processes"' % nsels)
    colors = ('red', 'blue', 'green', 'black', 'green', 'blue')
    plot_indices = (1, 2, 3, 4, 5, 6)
    for c, plot_index in zip(colors, plot_indices):
        header = g_time_stats_headers[plot_index]
        print >> out, RUtil.mk_call_str(
                'lines',
                'my.table$t',
                'my.table$%s' % header,
                col='"%s"' % c)
    return out.getvalue()
Example #6
0
def get_r_tikz_prop_plot(nsels, time_stats):
    """
    @param time_stats: a list of stats for each time point
    @return: tikz code corresponding to an R plot
    """
    out = StringIO()
    time_stats_trans = zip(*time_stats)
    y_low = 0
    y_high = 1
    ylim = RUtil.mk_call_str('c', y_low, y_high)
    print >> out, RUtil.mk_call_str(
            'plot',
            'my.table$t',
            'my.table$prop.mi.diag.approx',
            type='"n"',
            ylim=ylim,
            xlab='"time"',
            ylab='"proportion"',
            main='"proportion of same sign difference as MI"')
    colors = ('red', 'orange', 'green', 'blue', 'black')
    plot_indices = (12, 13, 14, 15, 16)
    for c, plot_index in zip(colors, plot_indices):
        header = g_time_stats_headers[plot_index]
        print >> out, RUtil.mk_call_str(
                'lines',
                'my.table$t',
                'my.table$%s' % header,
                col='"%s"' % c)
    return out.getvalue()
Example #7
0
def get_r_tikz_info_plot(nsels, time_stats):
    """
    @param time_stats: a list of stats for each time point
    @return: tikz code corresponding to an R plot
    """
    out = StringIO()
    time_stats_trans = zip(*time_stats)
    y_low = 0
    y_high = math.log(2)
    ylim = RUtil.mk_call_str('c', y_low, y_high)
    print >> out, RUtil.mk_call_str(
            'plot',
            'my.table$t',
            'my.table$info.mi.diag.approx',
            type='"n"',
            ylim=ylim,
            xlab='"time"',
            ylab='"info"',
            main='"informativeness with respect to MI"')
    colors = ('red', 'orange', 'green', 'blue', 'black')
    plot_indices = (17, 18, 19, 20, 21)
    for c, plot_index in zip(colors, plot_indices):
        header = g_time_stats_headers[plot_index]
        print >> out, RUtil.mk_call_str(
                'lines',
                'my.table$t',
                'my.table$%s' % header,
                col='"%s"' % c)
    return out.getvalue()
Example #8
0
def get_r_tikz_mi_plot_script(nsels, time_stats):
    """
    At each time point plot mutual information for all matrices.
    @param time_stats: a list of stats for each time point
    @return: tikz code corresponding to an R plot
    """
    out = StringIO()
    time_stats_trans = zip(*time_stats)
    mi_mut = time_stats_trans[1]
    mi_min_sels = time_stats_trans[6]
    mi_max_sels = time_stats_trans[2]
    y_low = min(mi_min_sels + mi_mut)
    y_high = max(mi_max_sels + mi_mut)
    ylim = RUtil.mk_call_str("c", y_low, y_high)
    print >> out, RUtil.mk_call_str(
        "plot",
        "my.table$t",
        "my.table$mut",
        type='"n"',
        ylim=ylim,
        xlab='"time"',
        ylab='"MI"',
        main='"MI for mut process and %d mut.sel processes"' % nsels,
    )
    colors = ("red", "blue", "green", "black", "green", "blue")
    plot_indices = (1, 2, 3, 4, 5, 6)
    for c, plot_index in zip(colors, plot_indices):
        header = g_time_stats_headers[plot_index]
        print >> out, RUtil.mk_call_str("lines", "my.table$t", "my.table$%s" % header, col='"%s"' % c)
    return out.getvalue()
Example #9
0
def get_response_content(fs):
    # legend labels
    label_a = 'N=%d mu=%f' % (fs.nstates_a, fs.mu_a)
    label_b = 'N=%d mu=%f' % (fs.nstates_b, fs.mu_b)
    arr, headers = make_table(fs)
    # compute the max value
    ymax = math.log(max(fs.nstates_a, fs.nstates_b))
    nfifths = int(math.floor(ymax * 5.0)) + 1
    ylim = RUtil.mk_call_str('c', 0, 0.2 * nfifths)
    # write the R script body
    out = StringIO()
    print >> out, RUtil.mk_call_str(
            'plot',
            'my.table$t',
            'my.table$alpha',
            type='"n"',
            ylim=ylim,
            xlab='"time"',
            ylab='"information"',
            main='"comparison of an information criterion for two processes"',
            )
    # draw some horizontal lines
    for i in range(nfifths+1):
        print >> out, RUtil.mk_call_str(
                'abline',
                h=0.2*i,
                col='"lightgray"',
                lty='"dotted"')
    colors = ('darkblue', 'darkred')
    for c, header in zip(colors, headers[1:]):
        print >> out, RUtil.mk_call_str(
                'lines',
                'my.table$t',
                'my.table$%s' % header,
                col='"%s"' % c,
                )
    legend_names = (label_a, label_b)
    legend_name_str = 'c(' + ', '.join('"%s"' % s for s in legend_names) + ')'
    legend_col_str = 'c(' + ', '.join('"%s"' % s for s in colors) + ')'
    legend_lty_str = 'c(' + ', '.join('1' for s in colors) + ')'
    print >> out, RUtil.mk_call_str(
            'legend',
            '"%s"' % fs.legend_placement,
            legend_name_str,
            col=legend_col_str,
            lty=legend_lty_str,
            )
    script_body = out.getvalue()
    # create the R plot image
    table_string = RUtil.get_table_string(arr, headers)
    device_name = Form.g_imageformat_to_r_function[fs.imageformat]
    retcode, r_out, r_err, image_data = RUtil.run_plotter(
            table_string, script_body, device_name)
    if retcode:
        raise RUtil.RError(r_err)
    return image_data
Example #10
0
def get_response_content(fs):
    # legend labels
    label_a = 'N=%d mu=%f' % (fs.nstates_a, fs.mu_a)
    label_b = 'N=%d mu=%f' % (fs.nstates_b, fs.mu_b)
    arr, headers = make_table(fs)
    # compute the max value
    ymax = math.log(max(fs.nstates_a, fs.nstates_b))
    nfifths = int(math.floor(ymax * 5.0)) + 1
    ylim = RUtil.mk_call_str('c', 0, 0.2 * nfifths)
    # write the R script body
    out = StringIO()
    print >> out, RUtil.mk_call_str(
        'plot',
        'my.table$t',
        'my.table$alpha',
        type='"n"',
        ylim=ylim,
        xlab='"time"',
        ylab='"information"',
        main='"comparison of an information criterion for two processes"',
    )
    # draw some horizontal lines
    for i in range(nfifths + 1):
        print >> out, RUtil.mk_call_str('abline',
                                        h=0.2 * i,
                                        col='"lightgray"',
                                        lty='"dotted"')
    colors = ('darkblue', 'darkred')
    for c, header in zip(colors, headers[1:]):
        print >> out, RUtil.mk_call_str(
            'lines',
            'my.table$t',
            'my.table$%s' % header,
            col='"%s"' % c,
        )
    legend_names = (label_a, label_b)
    legend_name_str = 'c(' + ', '.join('"%s"' % s for s in legend_names) + ')'
    legend_col_str = 'c(' + ', '.join('"%s"' % s for s in colors) + ')'
    legend_lty_str = 'c(' + ', '.join('1' for s in colors) + ')'
    print >> out, RUtil.mk_call_str(
        'legend',
        '"%s"' % fs.legend_placement,
        legend_name_str,
        col=legend_col_str,
        lty=legend_lty_str,
    )
    script_body = out.getvalue()
    # create the R plot image
    table_string = RUtil.get_table_string(arr, headers)
    device_name = Form.g_imageformat_to_r_function[fs.imageformat]
    retcode, r_out, r_err, image_data = RUtil.run_plotter(
        table_string, script_body, device_name)
    if retcode:
        raise RUtil.RError(r_err)
    return image_data
Example #11
0
def get_response_content(fs):
    distn_modes = [x for x in g_ordered_modes if x in fs.distribution]
    if not distn_modes:
        raise ValueError('no distribution mode was specified')
    colors = [g_mode_to_color[m] for m in distn_modes]
    arr, headers = make_table(fs, distn_modes)
    distn_headers = headers[1:]
    # Get the largest value in the array,
    # skipping the first column.
    arrmax = np.max(arr[:, 1:])
    # write the R script body
    out = StringIO()
    ylim = RUtil.mk_call_str('c', 0, arrmax + 0.1)
    sel_str = {
        BALANCED: 'balanced',
        HALPERN_BRUNO: 'Halpern-Bruno',
    }[fs.selection]
    print >> out, RUtil.mk_call_str(
        'plot',
        'my.table$t',
        'my.table$%s' % distn_headers[0],
        type='"n"',
        ylim=ylim,
        xlab='""',
        ylab='"relaxation time"',
        main='"Effect of selection (%s) on relaxation time for %d states"' %
        (sel_str, fs.nstates),
    )
    for c, header in zip(colors, distn_headers):
        print >> out, RUtil.mk_call_str(
            'lines',
            'my.table$t',
            'my.table$%s' % header,
            col='"%s"' % c,
        )
    mode_names = [s.replace('_', ' ') for s in distn_modes]
    legend_name_str = 'c(' + ', '.join('"%s"' % s for s in mode_names) + ')'
    legend_col_str = 'c(' + ', '.join('"%s"' % s for s in colors) + ')'
    legend_lty_str = 'c(' + ', '.join(['1'] * len(distn_modes)) + ')'
    print >> out, RUtil.mk_call_str(
        'legend',
        '"%s"' % fs.legend_placement,
        legend_name_str,
        col=legend_col_str,
        lty=legend_lty_str,
    )
    script_body = out.getvalue()
    # create the R plot image
    table_string = RUtil.get_table_string(arr, headers)
    device_name = Form.g_imageformat_to_r_function[fs.imageformat]
    retcode, r_out, r_err, image_data = RUtil.run_plotter(
        table_string, script_body, device_name)
    if retcode:
        raise RUtil.RError(r_err)
    return image_data
Example #12
0
def get_response_content(fs):
    distn_modes = [x for x in g_ordered_modes if x in fs.distribution]
    if not distn_modes:
        raise ValueError('no distribution mode was specified')
    colors = [g_mode_to_color[m] for m in distn_modes]
    arr, headers = make_table(fs, distn_modes)
    distn_headers = headers[1:]
    # Get the largest value in the array,
    # skipping the first column.
    arrmax = np.max(arr[:,1:])
    # write the R script body
    out = StringIO()
    ylim = RUtil.mk_call_str('c', 0, arrmax + 0.1)
    sel_str = {
            BALANCED : 'balanced',
            HALPERN_BRUNO : 'Halpern-Bruno',
            }[fs.selection]
    print >> out, RUtil.mk_call_str(
            'plot',
            'my.table$t',
            'my.table$%s' % distn_headers[0],
            type='"n"',
            ylim=ylim,
            xlab='""',
            ylab='"relaxation time"',
            main='"Effect of selection (%s) on relaxation time for %d states"' % (sel_str, fs.nstates),
            )
    for c, header in zip(colors, distn_headers):
        print >> out, RUtil.mk_call_str(
                'lines',
                'my.table$t',
                'my.table$%s' % header,
                col='"%s"' % c,
                )
    mode_names = [s.replace('_', ' ') for s in distn_modes]
    legend_name_str = 'c(' + ', '.join('"%s"' % s for s in mode_names) + ')'
    legend_col_str = 'c(' + ', '.join('"%s"' % s for s in colors) + ')'
    legend_lty_str = 'c(' + ', '.join(['1']*len(distn_modes)) + ')'
    print >> out, RUtil.mk_call_str(
            'legend',
            '"%s"' % fs.legend_placement,
            legend_name_str,
            col=legend_col_str,
            lty=legend_lty_str,
            )
    script_body = out.getvalue()
    # create the R plot image
    table_string = RUtil.get_table_string(arr, headers)
    device_name = Form.g_imageformat_to_r_function[fs.imageformat]
    retcode, r_out, r_err, image_data = RUtil.run_plotter(
            table_string, script_body, device_name)
    if retcode:
        raise RUtil.RError(r_err)
    return image_data
Example #13
0
def get_R_tick_cmd(axis, positions):
    """
    @param axis: 1 for x, 2 for y
    @param positions: a sequence of positions
    @return: a single line R command to draw the ticks
    """
    s = 'c(' + ', '.join(str(x) for x in positions) + ')'
    return RUtil.mk_call_str('axis', axis, at=s)
Example #14
0
def get_R_tick_cmd(axis, positions):
    """
    @param axis: 1 for x, 2 for y
    @param positions: a sequence of positions
    @return: a single line R command to draw the ticks
    """
    s = 'c(' + ', '.join(str(x) for x in positions) + ')'
    return RUtil.mk_call_str('axis', axis, at=s)
Example #15
0
def get_plot_scripts(sequence_lengths):
    scripts = []
    # get the plot for the mean
    out = StringIO()
    print >> out, RUtil.mk_call_str(
            'plot',
            'my.table$sequence.length',
            'my.table$mean.mean',
            xlab="''",
            ylab="'mean'",
            xaxt="'n'",
            main="'posterior statistics of rates among branches'",
            #type='"n"',
            )
    print >> out, get_R_tick_cmd(1, sequence_lengths)
    scripts.append(out.getvalue().rstrip())
    # get the plot for the mean
    out = StringIO()
    print >> out, RUtil.mk_call_str(
            'plot',
            'my.table$sequence.length',
            'my.table$var.mean',
            xlab="''",
            ylab="'coeff of variation'",
            xaxt="'n'",
            #type='"n"',
            )
    print >> out, get_R_tick_cmd(1, sequence_lengths)
    scripts.append(out.getvalue().rstrip())
    # get the plot for the mean
    out = StringIO()
    print >> out, RUtil.mk_call_str(
            'plot',
            'my.table$sequence.length',
            'my.table$cov.mean',
            xlab="'sequence length'",
            ylab="'parent-child correlation'",
            xaxt="'n'",
            #type='"n"',
            )
    print >> out, get_R_tick_cmd(1, sequence_lengths)
    scripts.append(out.getvalue().rstrip())
    return scripts
Example #16
0
def get_plot_scripts(sequence_lengths):
    scripts = []
    # get the plot for the mean
    out = StringIO()
    print >> out, RUtil.mk_call_str(
        'plot',
        'my.table$sequence.length',
        'my.table$mean.mean',
        xlab="''",
        ylab="'mean'",
        xaxt="'n'",
        main="'posterior statistics of rates among branches'",
        #type='"n"',
    )
    print >> out, get_R_tick_cmd(1, sequence_lengths)
    scripts.append(out.getvalue().rstrip())
    # get the plot for the mean
    out = StringIO()
    print >> out, RUtil.mk_call_str(
        'plot',
        'my.table$sequence.length',
        'my.table$var.mean',
        xlab="''",
        ylab="'coeff of variation'",
        xaxt="'n'",
        #type='"n"',
    )
    print >> out, get_R_tick_cmd(1, sequence_lengths)
    scripts.append(out.getvalue().rstrip())
    # get the plot for the mean
    out = StringIO()
    print >> out, RUtil.mk_call_str(
        'plot',
        'my.table$sequence.length',
        'my.table$cov.mean',
        xlab="'sequence length'",
        ylab="'parent-child correlation'",
        xaxt="'n'",
        #type='"n"',
    )
    print >> out, get_R_tick_cmd(1, sequence_lengths)
    scripts.append(out.getvalue().rstrip())
    return scripts
Example #17
0
def get_r_prop_script(nsels, time_stats):
    """
    @param time_stats: a list of stats for each time point
    @return: R code
    """
    out = StringIO()
    time_stats_trans = zip(*time_stats)
    y_low = 0
    y_high = 1
    ylim = RUtil.mk_call_str('c', y_low, y_high)
    print >> out, RUtil.mk_call_str(
            'plot',
            'my.table$t',
            'my.table$prop.sel.vs.mut',
            type='"l"',
            ylim=ylim,
            xlab='"time"',
            ylab='"proportion"',
            main='"proportion of mut-sel MI greater than mutation MI"')
    return out.getvalue()
Example #18
0
def get_r_prop_script(nsels, time_stats):
    """
    @param time_stats: a list of stats for each time point
    @return: R code
    """
    out = StringIO()
    time_stats_trans = zip(*time_stats)
    y_low = 0
    y_high = 1
    ylim = RUtil.mk_call_str('c', y_low, y_high)
    print >> out, RUtil.mk_call_str(
        'plot',
        'my.table$t',
        'my.table$prop.sel.vs.mut',
        type='"l"',
        ylim=ylim,
        xlab='"time"',
        ylab='"proportion"',
        main='"proportion of mut-sel MI greater than mutation MI"')
    return out.getvalue()
Example #19
0
def get_response_content(fs):
    distn_modes = [x for x in g_ordered_modes if x in fs.distribution]
    if not distn_modes:
        raise ValueError("no distribution mode was specified")
    colors = [g_mode_to_color[m] for m in distn_modes]
    arr, headers = make_table(fs, distn_modes)
    distn_headers = headers[1:]
    # Get the largest value in the array,
    # skipping the first column.
    arrmax = np.max(arr[:, 1:])
    # write the R script body
    out = StringIO()
    ylim = RUtil.mk_call_str("c", 0, arrmax + 0.1)
    sel_str = {BALANCED: "f=1/2", HALPERN_BRUNO: "Halpern-Bruno"}[fs.selection]
    print >> out, RUtil.mk_call_str(
        "plot",
        "my.table$t",
        "my.table$%s" % distn_headers[0],
        type='"n"',
        ylim=ylim,
        xlab='"time"',
        ylab='"expected log L-ratio"',
        main='"Effect of selection (%s) on log L-ratio for %d states"' % (sel_str, fs.nstates),
    )
    for c, header in zip(colors, distn_headers):
        print >> out, RUtil.mk_call_str("lines", "my.table$t", "my.table$%s" % header, col='"%s"' % c)
    mode_names = [s.replace("_", " ") for s in distn_modes]
    legend_name_str = "c(" + ", ".join('"%s"' % s for s in mode_names) + ")"
    legend_col_str = "c(" + ", ".join('"%s"' % s for s in colors) + ")"
    legend_lty_str = "c(" + ", ".join(["1"] * len(distn_modes)) + ")"
    print >> out, RUtil.mk_call_str(
        "legend", '"%s"' % fs.legend_placement, legend_name_str, col=legend_col_str, lty=legend_lty_str
    )
    script_body = out.getvalue()
    # create the R plot image
    table_string = RUtil.get_table_string(arr, headers)
    device_name = Form.g_imageformat_to_r_function[fs.imageformat]
    retcode, r_out, r_err, image_data = RUtil.run_plotter(table_string, script_body, device_name)
    if retcode:
        raise RUtil.RError(r_err)
    return image_data
Example #20
0
def get_r_tikz_script(nsels, name):
    """
    This is obsolete because I am now using pure R output.
    @param nsels: the number of mutation-selection balance matrices
    @param name: the name of the variable whose histogram should be plotted
    @return: tikz code corresponding to an R plot
    """
    out = StringIO()
    print >> out, RUtil.mk_call_str('hist',
                                    'my.table$%s' % name,
                                    xlab='"%s"' % name,
                                    ylab='"counts"',
                                    main='"%s; N=%d"' % (name, nsels))
    return out.getvalue()
Example #21
0
def get_r_tikz_script(nsels, name):
    """
    This is obsolete because I am now using pure R output.
    @param nsels: the number of mutation-selection balance matrices
    @param name: the name of the variable whose histogram should be plotted
    @return: tikz code corresponding to an R plot
    """
    out = StringIO()
    print >> out, RUtil.mk_call_str(
            'hist',
            'my.table$%s' % name,
            xlab = '"%s"' % name,
            ylab = '"counts"',
            main='"%s; N=%d"' % (name, nsels))
    return out.getvalue()
Example #22
0
def get_r_comboscript(nsels, names):
    """
    Do all of the plots at once.
    @param nsels: the number of mutation-selection balance matrices
    @param names: the names of the variable whose histogram should be plotted
    @return: tikz code corresponding to an R plot
    """
    out = StringIO()
    print >> out, 'par(mfrow=c(3,1))'
    for name in names:
        print >> out, RUtil.mk_call_str('hist',
                                        'my.table$%s' % name,
                                        xlab='"%s"' % name,
                                        ylab='"counts"',
                                        main='"%s; N=%d"' % (name, nsels))
    return out.getvalue()
Example #23
0
def get_r_comboscript(nsels, names):
    """
    Do all of the plots at once.
    @param nsels: the number of mutation-selection balance matrices
    @param names: the names of the variable whose histogram should be plotted
    @return: tikz code corresponding to an R plot
    """
    out = StringIO()
    print >> out, 'par(mfrow=c(3,1))'
    for name in names:
        print >> out, RUtil.mk_call_str(
                'hist',
                'my.table$%s' % name,
                xlab = '"%s"' % name,
                ylab = '"counts"',
                main='"%s; N=%d"' % (name, nsels))
    return out.getvalue()
Example #24
0
def get_r_band_script(nsels, time_stats):
    """
    @param time_stats: a list of stats for each time point
    @return: R code
    """
    out = StringIO()
    time_stats_trans = zip(*time_stats)
    mi_mut = time_stats_trans[1]
    # set up the correctly sized plot
    mi_min_sels = time_stats_trans[6]
    mi_max_sels = time_stats_trans[2]
    y_low = min(mi_min_sels + mi_mut)
    y_high = max(mi_max_sels + mi_mut)
    ylim = RUtil.mk_call_str('c', y_low, y_high)
    print >> out, RUtil.mk_call_str(
            'plot',
            'my.table$t',
            'my.table$mut',
            type='"n"',
            ylim=ylim,
            xlab='"time"',
            ylab='"MI"',
            main='"MI for mut process (red) and %d mut-sel processes"' % nsels)
    # draw a light gray polygon covering all selection mutual information
    print >> out, RUtil.mk_call_str(
            'polygon',
            'c(my.table$t, rev(my.table$t))',
            'c(my.table$mut.sel.max, rev(my.table$mut.sel.min))',
            col='"gray80"',
            border='NA')
    # draw a darker gray polygon covering most of selection mutual information
    print >> out, RUtil.mk_call_str(
            'polygon',
            'c(my.table$t, rev(my.table$t))',
            'c(my.table$mut.sel.high, rev(my.table$mut.sel.low))',
            col='"gray50"',
            border='NA')
    # draw the black line representing the mean selection mutual information
    print >> out, RUtil.mk_call_str(
            'lines',
            'my.table$t',
            'my.table$mut.sel.mean',
            col='"black"')
    # draw the red line representing the mutation mutual information
    print >> out, RUtil.mk_call_str(
            'lines',
            'my.table$t',
            'my.table$mut',
            col='"red"')
    return out.getvalue()
Example #25
0
def get_response_content(fs):
    # create the R table string and scripts
    headers = [
            'entropy',
            'analog']
    distributions = []
    nstates = 4
    npoints = 5000
    arr = []
    best_pair = None
    for i in range(npoints):
        weights = [random.expovariate(1) for j in range(nstates)]
        total = sum(weights)
        distn = [x / total for x in weights]
        entropy = -sum(p * math.log(p) for p in distn)
        sum_squares = sum(p*p for p in distn)
        sum_cubes = sum(p*p*p for p in distn)
        analog = math.log(sum_squares / sum_cubes)
        row = [entropy, analog]
        arr.append(row)
        dist = (entropy - 1.0)**2 + (analog - 0.4)**2
        if (best_pair is None) or (dist < best_pair[0]):
            best_pair = (dist, distn)
    # get the R table
    table_string = RUtil.get_table_string(arr, headers)
    # get the R script
    out = StringIO()
    title = ', '.join(str(x) for x in best_pair[1])
    print >> out, RUtil.mk_call_str(
            'plot',
            'my.table$entropy',
            'my.table$analog',
            pch='20',
            main='"%s"' % title)
    script = out.getvalue()
    # create the R plot image
    device_name = Form.g_imageformat_to_r_function[fs.imageformat]
    retcode, r_out, r_err, image_data = RUtil.run_plotter(
            table_string, script, device_name)
    if retcode:
        raise RUtil.RError(r_err)
    return image_data
Example #26
0
def get_r_band_script(nsels, time_stats):
    """
    @param time_stats: a list of stats for each time point
    @return: R code
    """
    out = StringIO()
    time_stats_trans = zip(*time_stats)
    mi_mut = time_stats_trans[1]
    # set up the correctly sized plot
    mi_min_sels = time_stats_trans[6]
    mi_max_sels = time_stats_trans[2]
    y_low = min(mi_min_sels + mi_mut)
    y_high = max(mi_max_sels + mi_mut)
    ylim = RUtil.mk_call_str('c', y_low, y_high)
    print >> out, RUtil.mk_call_str(
        'plot',
        'my.table$t',
        'my.table$mut',
        type='"n"',
        ylim=ylim,
        xlab='"time"',
        ylab='"MI"',
        main='"MI for mut process (red) and %d mut-sel processes"' % nsels)
    # draw a light gray polygon covering all selection mutual information
    print >> out, RUtil.mk_call_str(
        'polygon',
        'c(my.table$t, rev(my.table$t))',
        'c(my.table$mut.sel.max, rev(my.table$mut.sel.min))',
        col='"gray80"',
        border='NA')
    # draw a darker gray polygon covering most of selection mutual information
    print >> out, RUtil.mk_call_str(
        'polygon',
        'c(my.table$t, rev(my.table$t))',
        'c(my.table$mut.sel.high, rev(my.table$mut.sel.low))',
        col='"gray50"',
        border='NA')
    # draw the black line representing the mean selection mutual information
    print >> out, RUtil.mk_call_str('lines',
                                    'my.table$t',
                                    'my.table$mut.sel.mean',
                                    col='"black"')
    # draw the red line representing the mutation mutual information
    print >> out, RUtil.mk_call_str('lines',
                                    'my.table$t',
                                    'my.table$mut',
                                    col='"red"')
    return out.getvalue()
Example #27
0
def get_r_cross_script(ncrossing_list):
    """
    @param time_stats: a list of stats for each time point
    @return: R code
    """
    out = StringIO()
    low = min(ncrossing_list)
    high = max(ncrossing_list)
    n_to_count = defaultdict(int)
    for n in ncrossing_list:
        n_to_count[n] += 1
    counts = [n_to_count[i] for i in range(low, high+1)]
    s = ', '.join('"%s"' % i for i in range(low, high+1))
    print >> out, RUtil.mk_call_str(
            'barplot',
            'c(' + ', '.join(str(x) for x in counts) + ')',
            'names.arg=c(' + s + ')',
            xlab='"number of crossings"',
            ylab='"frequency"',
            main='"number of times mut-sel MI crosses mut MI"')
    return out.getvalue()
Example #28
0
def get_r_cross_script(ncrossing_list):
    """
    @param time_stats: a list of stats for each time point
    @return: R code
    """
    out = StringIO()
    low = min(ncrossing_list)
    high = max(ncrossing_list)
    n_to_count = defaultdict(int)
    for n in ncrossing_list:
        n_to_count[n] += 1
    counts = [n_to_count[i] for i in range(low, high + 1)]
    s = ', '.join('"%s"' % i for i in range(low, high + 1))
    print >> out, RUtil.mk_call_str(
        'barplot',
        'c(' + ', '.join(str(x) for x in counts) + ')',
        'names.arg=c(' + s + ')',
        xlab='"number of crossings"',
        ylab='"frequency"',
        main='"number of times mut-sel MI crosses mut MI"')
    return out.getvalue()
Example #29
0
def get_response_content(fs):
    # create the R table string and scripts
    headers = ['entropy', 'analog']
    distributions = []
    nstates = 4
    npoints = 5000
    arr = []
    best_pair = None
    for i in range(npoints):
        weights = [random.expovariate(1) for j in range(nstates)]
        total = sum(weights)
        distn = [x / total for x in weights]
        entropy = -sum(p * math.log(p) for p in distn)
        sum_squares = sum(p * p for p in distn)
        sum_cubes = sum(p * p * p for p in distn)
        analog = math.log(sum_squares / sum_cubes)
        row = [entropy, analog]
        arr.append(row)
        dist = (entropy - 1.0)**2 + (analog - 0.4)**2
        if (best_pair is None) or (dist < best_pair[0]):
            best_pair = (dist, distn)
    # get the R table
    table_string = RUtil.get_table_string(arr, headers)
    # get the R script
    out = StringIO()
    title = ', '.join(str(x) for x in best_pair[1])
    print >> out, RUtil.mk_call_str('plot',
                                    'my.table$entropy',
                                    'my.table$analog',
                                    pch='20',
                                    main='"%s"' % title)
    script = out.getvalue()
    # create the R plot image
    device_name = Form.g_imageformat_to_r_function[fs.imageformat]
    retcode, r_out, r_err, image_data = RUtil.run_plotter(
        table_string, script, device_name)
    if retcode:
        raise RUtil.RError(r_err)
    return image_data
Example #30
0
def get_ggplot2_x_tick_cmd(positions):
    s = 'c(' + ', '.join(str(x) for x in positions) + ')'
    return RUtil.mk_call_str('scale_x_discrete', breaks=s)
Example #31
0
def get_ggplot2_scripts(nsamples, sequence_lengths, midpoints):
    scripts = []
    # get the plot for the mean
    out = StringIO()
    print >> out, RUtil.mk_call_str(
            'ggplot', 'my.table',
            RUtil.mk_call_str(
                'aes',
                x='midpoint',
                y='mean.mean')), '+'
    print >> out, RUtil.mk_call_str(
            'geom_errorbar',
            RUtil.mk_call_str(
                'aes',
                ymin='mean.low',
                ymax='mean.high',
                colour='factor(sequence.length)'),
            width='20'), '+'
    print >> out, "opts(title='mcmc chain length %d') +" % nsamples
    print >> out, "geom_point() + xlab('midpoint') + ylab('mean of rates') +"
    print >> out, "scale_color_discrete('length') +"
    print >> out, get_ggplot2_x_tick_cmd(midpoints)
    scripts.append(out.getvalue().rstrip())
    # get the plot for the coefficient of variation
    out = StringIO()
    print >> out, RUtil.mk_call_str(
            'ggplot', 'my.table',
            RUtil.mk_call_str(
                'aes',
                x='midpoint',
                y='var.mean')), '+'
    print >> out, RUtil.mk_call_str(
            'geom_errorbar',
            RUtil.mk_call_str(
                'aes',
                ymin='var.low',
                ymax='var.high',
                colour='factor(sequence.length)'),
            width='20'), '+'
    print >> out, "geom_point() + xlab('midpoint') +"
    print >> out, "ylab('coefficient of variation of rates') +"
    print >> out, "scale_color_discrete('length') +"
    print >> out, get_ggplot2_x_tick_cmd(midpoints)
    scripts.append(out.getvalue().rstrip())
    # get the plot for the correlation
    out = StringIO()
    print >> out, RUtil.mk_call_str(
            'ggplot', 'my.table',
            RUtil.mk_call_str(
                'aes',
                x='midpoint',
                y='cov.mean')), '+'
    print >> out, RUtil.mk_call_str(
            'geom_errorbar',
            RUtil.mk_call_str(
                'aes',
                ymin='cov.low',
                ymax='cov.high',
                colour='factor(sequence.length)'),
            width='20'), '+'
    print >> out, "geom_point() + xlab('midpoint') +"
    print >> out, "ylab('parent child correlation of rates') +"
    print >> out, "scale_color_discrete('length') +"
    print >> out, get_ggplot2_x_tick_cmd(midpoints)
    scripts.append(out.getvalue().rstrip())
    return scripts
Example #32
0
def get_ggplot2_legend_cmd():
    s_labels = "c('57', '114', '228', '456')"
    return RUtil.mk_call_str('scale_colour_discrete',
            labels=s_labels)
Example #33
0
def get_ggplot2_x_tick_cmd(positions):
    s = 'c(' + ', '.join(str(x) for x in positions) + ')'
    return RUtil.mk_call_str('scale_x_discrete', breaks=s)
Example #34
0
def get_ggplot2_scripts(sequence_lengths):
    scripts = []
    # get the plot for the mean
    out = StringIO()
    print >> out, RUtil.mk_call_str(
            'ggplot', 'my.table',
            RUtil.mk_call_str(
                'aes',
                x='sequence.length',
                y='mean.mean')), '+'
    print >> out, RUtil.mk_call_str(
            'geom_errorbar',
            RUtil.mk_call_str(
                'aes',
                ymin='mean.low',
                ymax='mean.high'),
            width='20'), '+'
    print >> out, "geom_point() + xlab('sequence length') + ylab('mean') +"
    print >> out, get_ggplot2_x_tick_cmd(sequence_lengths)
    scripts.append(out.getvalue().rstrip())
    # get the plot for the coefficient of variation
    out = StringIO()
    print >> out, RUtil.mk_call_str(
            'ggplot', 'my.table',
            RUtil.mk_call_str(
                'aes',
                x='sequence.length',
                y='var.mean')), '+'
    print >> out, RUtil.mk_call_str(
            'geom_errorbar',
            RUtil.mk_call_str(
                'aes',
                ymin='var.low',
                ymax='var.high'),
            width='20'), '+'
    print >> out, "geom_point() + xlab('sequence length') +"
    print >> out, "ylab('coefficient of variation') +"
    print >> out, get_ggplot2_x_tick_cmd(sequence_lengths)
    scripts.append(out.getvalue().rstrip())
    # get the plot for the correlation
    out = StringIO()
    print >> out, RUtil.mk_call_str(
            'ggplot', 'my.table',
            RUtil.mk_call_str(
                'aes',
                x='sequence.length',
                y='cov.mean')), '+'
    print >> out, RUtil.mk_call_str(
            'geom_errorbar',
            RUtil.mk_call_str(
                'aes',
                ymin='cov.low',
                ymax='cov.high'),
            width='20'), '+'
    print >> out, "geom_point() + xlab('sequence length') +"
    print >> out, "ylab('parent child correlation') +"
    print >> out, get_ggplot2_x_tick_cmd(sequence_lengths)
    scripts.append(out.getvalue().rstrip())
    return scripts
Example #35
0
def get_ggplot2_scripts(sequence_lengths):
    scripts = []
    # get the plot for the mean
    out = StringIO()
    print >> out, RUtil.mk_call_str(
        'ggplot', 'my.table',
        RUtil.mk_call_str('aes', x='sequence.length', y='mean.mean')), '+'
    print >> out, RUtil.mk_call_str('geom_errorbar',
                                    RUtil.mk_call_str('aes',
                                                      ymin='mean.low',
                                                      ymax='mean.high'),
                                    width='20'), '+'
    print >> out, "geom_point() + xlab('sequence length') + ylab('mean') +"
    print >> out, get_ggplot2_x_tick_cmd(sequence_lengths)
    scripts.append(out.getvalue().rstrip())
    # get the plot for the coefficient of variation
    out = StringIO()
    print >> out, RUtil.mk_call_str(
        'ggplot', 'my.table',
        RUtil.mk_call_str('aes', x='sequence.length', y='var.mean')), '+'
    print >> out, RUtil.mk_call_str('geom_errorbar',
                                    RUtil.mk_call_str('aes',
                                                      ymin='var.low',
                                                      ymax='var.high'),
                                    width='20'), '+'
    print >> out, "geom_point() + xlab('sequence length') +"
    print >> out, "ylab('coefficient of variation') +"
    print >> out, get_ggplot2_x_tick_cmd(sequence_lengths)
    scripts.append(out.getvalue().rstrip())
    # get the plot for the correlation
    out = StringIO()
    print >> out, RUtil.mk_call_str(
        'ggplot', 'my.table',
        RUtil.mk_call_str('aes', x='sequence.length', y='cov.mean')), '+'
    print >> out, RUtil.mk_call_str('geom_errorbar',
                                    RUtil.mk_call_str('aes',
                                                      ymin='cov.low',
                                                      ymax='cov.high'),
                                    width='20'), '+'
    print >> out, "geom_point() + xlab('sequence length') +"
    print >> out, "ylab('parent child correlation') +"
    print >> out, get_ggplot2_x_tick_cmd(sequence_lengths)
    scripts.append(out.getvalue().rstrip())
    return scripts
Example #36
0
def get_ggplot2_legend_cmd():
    s_labels = "c('57', '114', '228', '456')"
    return RUtil.mk_call_str('scale_colour_discrete', labels=s_labels)
Example #37
0
def get_ggplot2_scripts(nsamples, sequence_lengths, midpoints):
    scripts = []
    # get the plot for the mean
    out = StringIO()
    print >> out, RUtil.mk_call_str(
        'ggplot', 'my.table',
        RUtil.mk_call_str('aes', x='midpoint', y='mean.mean')), '+'
    print >> out, RUtil.mk_call_str('geom_errorbar',
                                    RUtil.mk_call_str(
                                        'aes',
                                        ymin='mean.low',
                                        ymax='mean.high',
                                        colour='factor(sequence.length)'),
                                    width='20'), '+'
    print >> out, "opts(title='mcmc chain length %d') +" % nsamples
    print >> out, "geom_point() + xlab('midpoint') + ylab('mean of rates') +"
    print >> out, "scale_color_discrete('length') +"
    print >> out, get_ggplot2_x_tick_cmd(midpoints)
    scripts.append(out.getvalue().rstrip())
    # get the plot for the coefficient of variation
    out = StringIO()
    print >> out, RUtil.mk_call_str(
        'ggplot', 'my.table',
        RUtil.mk_call_str('aes', x='midpoint', y='var.mean')), '+'
    print >> out, RUtil.mk_call_str('geom_errorbar',
                                    RUtil.mk_call_str(
                                        'aes',
                                        ymin='var.low',
                                        ymax='var.high',
                                        colour='factor(sequence.length)'),
                                    width='20'), '+'
    print >> out, "geom_point() + xlab('midpoint') +"
    print >> out, "ylab('coefficient of variation of rates') +"
    print >> out, "scale_color_discrete('length') +"
    print >> out, get_ggplot2_x_tick_cmd(midpoints)
    scripts.append(out.getvalue().rstrip())
    # get the plot for the correlation
    out = StringIO()
    print >> out, RUtil.mk_call_str(
        'ggplot', 'my.table',
        RUtil.mk_call_str('aes', x='midpoint', y='cov.mean')), '+'
    print >> out, RUtil.mk_call_str('geom_errorbar',
                                    RUtil.mk_call_str(
                                        'aes',
                                        ymin='cov.low',
                                        ymax='cov.high',
                                        colour='factor(sequence.length)'),
                                    width='20'), '+'
    print >> out, "geom_point() + xlab('midpoint') +"
    print >> out, "ylab('parent child correlation of rates') +"
    print >> out, "scale_color_discrete('length') +"
    print >> out, get_ggplot2_x_tick_cmd(midpoints)
    scripts.append(out.getvalue().rstrip())
    return scripts