Beispiel #1
0
def get_response_content(fs):
    out = StringIO()
    #
    N_hap = 2 * fs.N_diploid
    if fs.nmutants >= N_hap:
        raise Exception('too many initial mutant alleles')
    p0 = fs.nmutants / float(N_hap)
    h_values = [float(h) for h in fs.h_values]
    s_values = [float(s) for s in fs.s_values]
    #
    print >> out, '<html><body><table border="1" cellpadding="10">'
    #
    headers = (
            'method', 'h', 's',
            'fAA', 'faA', 'faa',
            'pfix',
            )
    print >> out, get_html_table_row(headers)
    #
    for h in h_values:
        for s in s_values:
            for method_name in ('finite', 'diffusion'):
                sigma = s / float(fs.N_diploid)
                #
                fAA = 1.0 + sigma
                faA = 1.0 + h * sigma
                faa = 1.0
                #
                if method_name == 'diffusion':
                    pfix = kimura.get_fixation_probability_chen(p0, s, h)
                elif method_name == 'finite':
                    P = np.exp(wfengine.create_diallelic_chen(
                        fs.N_diploid, fAA, faA, faa))
                    MatrixUtil.assert_transition_matrix(P)
                    v = get_pfix_finite(P)
                    pfix = v[fs.nmutants - 1]
                else:
                    raise Exception('internal error')
                values = (
                        method_name, h, s,
                        fAA, faA, faa,
                        pfix,
                        )
                print >> out, get_html_table_row(values)
    print >> out, '</table></body></html>'
    return out.getvalue()
Beispiel #2
0
def get_response_content(fs):
    # define the initial frequency
    p = 0.1
    # define some selection coefficients to plot
    h_low = 0.2
    h_high = 0.8
    h_values = np.linspace(h_low, h_high, 6*10 + 1)
    # define some dominance coefficients
    hs_values = [-0.2, -0.05, 0, 0.05, 0.2]
    colors = ['blue', 'green', 'black', 'orange', 'red']
    # get the values for each h
    arr = []
    for hs in hs_values:
        v = [kimura.get_fixation_probability_chen(p, hs/h, h) for h in h_values]
        arr.append(v)
    #
    # define the r script
    out = StringIO()
    print >> out, 'h.values <- c', str(tuple(h_values))
    print >> out, 'ha <- c', str(tuple(arr[0]))
    print >> out, 'hb <- c', str(tuple(arr[1]))
    print >> out, 'hc <- c', str(tuple(arr[2]))
    print >> out, 'hd <- c', str(tuple(arr[3]))
    print >> out, 'he <- c', str(tuple(arr[4]))
    print >> out, mk_call_str('plot', 'h.values', 'ha',
            type='"l"',
            xlab='"h"',
            ylab='"probability of eventual fixation"',
            main=(
                '"fixation probabilities for various h*s ; '
                's = 2*N*sigma ; p0 = %s"' % p),
            ylim='c(0, 0.2)',
            col='"%s"' % colors[0],
            )
    print >> out, mk_call_str('lines', 'h.values', 'hb', col='"%s"' % colors[1])
    print >> out, mk_call_str('lines', 'h.values', 'hc', col='"%s"' % colors[2])
    print >> out, mk_call_str('lines', 'h.values', 'hd', col='"%s"' % colors[3])
    print >> out, mk_call_str('lines', 'h.values', 'he', col='"%s"' % colors[4])
    script = out.getvalue().rstrip()
    # 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_no_table(
            script, device_name)
    if retcode:
        raise RUtil.RError(r_err)
    return image_data
Beispiel #3
0
def get_response_content(fs):
    # define the initial frequency
    p = 0.8
    # define some selection coefficients to plot
    low = -5
    high = 5
    s_values = np.linspace(low, high, (high - low) * 20 + 1)
    # define some dominance coefficients
    h_values = [2, 3, 5, 7]
    # get the values for each h
    arr = []
    for h in h_values:
        v = [kimura.get_fixation_probability_chen(p, s, h) for s in s_values]
        arr.append(v)
    #
    # define the r script
    out = StringIO()
    print >> out, 'title.string <- "my title"'
    print >> out, 's.values <- c', str(tuple(s_values))
    print >> out, 'ha <- c', str(tuple(arr[0]))
    print >> out, 'hb <- c', str(tuple(arr[1]))
    print >> out, 'hc <- c', str(tuple(arr[2]))
    print >> out, 'hd <- c', str(tuple(arr[3]))
    print >> out, mk_call_str(
        'plot',
        's.values',
        'ha',
        type='"l"',
        xlab='"selection coefficient (s)"',
        ylab='"probability of eventual fixation"',
        main='"fixation probabilities for various h ; p0 = 0.8"',
        ylim='c(0.5,1)',
    )
    print >> out, mk_call_str('lines', 's.values', 'hb', col='"red"')
    print >> out, mk_call_str('lines', 's.values', 'hc', col='"green"')
    print >> out, mk_call_str('lines', 's.values', 'hd', col='"blue"')
    script = out.getvalue().rstrip()
    # 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_no_table(
        script, device_name)
    if retcode:
        raise RUtil.RError(r_err)
    return image_data
Beispiel #4
0
def get_response_content(fs):
    # define the initial frequency
    p = 0.8
    # define some selection coefficients to plot
    low = -5
    high = 5
    s_values = np.linspace(low, high, (high - low) * 20 + 1)
    # define some dominance coefficients
    h_values = [2, 3, 5, 7]
    # get the values for each h
    arr = []
    for h in h_values:
        v = [kimura.get_fixation_probability_chen(p, s, h) for s in s_values]
        arr.append(v)
    #
    # define the r script
    out = StringIO()
    print >> out, 'title.string <- "my title"'
    print >> out, "s.values <- c", str(tuple(s_values))
    print >> out, "ha <- c", str(tuple(arr[0]))
    print >> out, "hb <- c", str(tuple(arr[1]))
    print >> out, "hc <- c", str(tuple(arr[2]))
    print >> out, "hd <- c", str(tuple(arr[3]))
    print >> out, mk_call_str(
        "plot",
        "s.values",
        "ha",
        type='"l"',
        xlab='"selection coefficient (s)"',
        ylab='"probability of eventual fixation"',
        main='"fixation probabilities for various h ; p0 = 0.8"',
        ylim="c(0.5,1)",
    )
    print >> out, mk_call_str("lines", "s.values", "hb", col='"red"')
    print >> out, mk_call_str("lines", "s.values", "hc", col='"green"')
    print >> out, mk_call_str("lines", "s.values", "hd", col='"blue"')
    script = out.getvalue().rstrip()
    # 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_no_table(script, device_name)
    if retcode:
        raise RUtil.RError(r_err)
    return image_data
Beispiel #5
0
def get_response_content(fs):
    # define the initial frequency
    p = 0.1
    # define some selection coefficients to plot
    low = -20
    high = 20
    s_values = np.linspace(low, high, (high-low)*10 + 1)
    # define some dominance coefficients
    h_values = [-3, 0, 1, 2]
    # get the values for each h
    arr = []
    for h in h_values:
        v = [kimura.get_fixation_probability_chen(p, s, h) for s in s_values]
        arr.append(v)
    #
    # define the r script
    out = StringIO()
    print >> out, 's.values <- c', str(tuple(s_values))
    print >> out, 'ha <- c', str(tuple(arr[0]))
    print >> out, 'hb <- c', str(tuple(arr[1]))
    print >> out, 'hc <- c', str(tuple(arr[2]))
    print >> out, 'hd <- c', str(tuple(arr[3]))
    print >> out, mk_call_str('plot', 's.values', 'ha',
            type='"l"',
            xlab='"selection coefficient (s)"',
            ylab='"probability of eventual fixation"',
            main='"fixation probabilities for various h ; p0 = 0.1"',
            ylim='c(0,1)',
            )
    print >> out, mk_call_str('lines', 's.values', 'hb', col='"red"')
    print >> out, mk_call_str('lines', 's.values', 'hc', col='"green"')
    print >> out, mk_call_str('lines', 's.values', 'hd', col='"blue"')
    script = out.getvalue().rstrip()
    # 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_no_table(
            script, device_name)
    if retcode:
        raise RUtil.RError(r_err)
    return image_data
Beispiel #6
0
def get_response_content(fs):
    out = StringIO()
    #
    nmutants = 1
    N_hap = 2 * fs.N_diploid
    if nmutants >= N_hap:
        raise Exception('too many initial mutant alleles')
    p0 = nmutants / float(N_hap)
    h_values = [float(h) for h in fs.h_values]
    s_values = [float(s) for s in fs.s_values]
    #
    print >> out, '<html><body><table border="1" cellpadding="10">'
    #
    headers = (
            'h', 's',
            'fAA', 'faA', 'faa',
            'pfix',
            'first order',
            )
    print >> out, get_html_table_row(headers)
    #
    for h in h_values:
        for s in s_values:
            sigma = s / float(fs.N_diploid)
            #
            fAA = 1.0 + sigma
            faA = 1.0 + h * sigma
            faa = 1.0
            #
            pfix = kimura.get_fixation_probability_chen(p0, s, h)
            lim = p0 * get_pfix_transformed_limit(s, h)
            values = (
                    h, s,
                    fAA, faA, faa,
                    pfix,
                    lim.real,
                    )
            print >> out, get_html_table_row(values)
    print >> out, '</table></body></html>'
    return out.getvalue()
Beispiel #7
0
def get_response_content(fs):
    # define the initial frequency
    p = 0.1
    # define some selection coefficients to plot
    h_low = 0.2
    h_high = 0.8
    h_values = np.linspace(h_low, h_high, 6*10 + 1)
    # define some dominance coefficients
    hs_values = [-0.2, -0.1, -0.04, 0, 0.04, 0.1, 0.2]
    #colors = ['blue', 'green', 'black', 'orange', 'red']
    colors = ['darkviolet', 'blue', 'green', 'black', 'yellow', 'orange', 'red']
    # get the values for each h
    arr = []
    xaxis = []
    for hs in hs_values:
        s_values = hs / h_values
        v = [kimura.get_fixation_probability_chen(p, hs/h, h) for h in h_values]
        xaxis.append(s_values)
        arr.append(v)
    #
    # define the r script
    out = StringIO()
    print >> out, 'h.values <- c', str(tuple(h_values))
    print >> out, 'ha <- c', str(tuple(arr[0]))
    print >> out, 'hb <- c', str(tuple(arr[1]))
    print >> out, 'hc <- c', str(tuple(arr[2]))
    print >> out, 'hd <- c', str(tuple(arr[3]))
    print >> out, 'he <- c', str(tuple(arr[4]))
    print >> out, 'hf <- c', str(tuple(arr[5]))
    print >> out, 'hg <- c', str(tuple(arr[6]))
    print >> out, 'ha.x <- c', str(tuple(xaxis[0]))
    print >> out, 'hb.x <- c', str(tuple(xaxis[1]))
    print >> out, 'hc.x <- c', str(tuple(xaxis[2]))
    print >> out, 'hd.x <- c', str(tuple(xaxis[3]))
    print >> out, 'he.x <- c', str(tuple(xaxis[4]))
    print >> out, 'hf.x <- c', str(tuple(xaxis[5]))
    print >> out, 'hg.x <- c', str(tuple(xaxis[6]))
    print >> out, mk_call_str('plot', 'ha.x', 'ha',
            type='"l"',
            xlab='"selection coefficient (s)"',
            ylab='"probability of eventual fixation"',
            main=(
                '"fixation probabilities for various h*s ; '
                's = 2*N*sigma ; p0 = %s"' % p),
            xlim='c(-1, 1)',
            ylim='c(0.05, 0.15)',
            col='"%s"' % colors[0],
            )
    print >> out, mk_call_str('lines', 'hb.x', 'hb', col='"%s"' % colors[1])
    print >> out, mk_call_str('lines', 'hc.x', 'hc', col='"%s"' % colors[2])
    print >> out, mk_call_str('lines', 'hd.x', 'hd', col='"%s"' % colors[3])
    print >> out, mk_call_str('lines', 'he.x', 'he', col='"%s"' % colors[4])
    print >> out, mk_call_str('lines', 'hf.x', 'hf', col='"%s"' % colors[5])
    print >> out, mk_call_str('lines', 'hg.x', 'hg', col='"%s"' % colors[6])
    print >> out, mk_call_str(
            'legend',
            '"topleft"',
            'c' + str(rev('hs = %s' % x for x in hs_values)),
            lty='c' + str(rev([1]*7)),
            lwd='c' + str(rev([2.5]*7)),
            col='c' + str(rev(colors)),
            )
    script = out.getvalue().rstrip()
    # 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_no_table(
            script, device_name)
    if retcode:
        raise RUtil.RError(r_err)
    return image_data
Beispiel #8
0
def get_response_content(fs):
    # define the initial frequency
    p = 0.01
    density = 2
    # dominance
    if fs.h_dense:
        h_low = 0.2
        h_high = 0.8
        h_values = np.linspace(h_low, h_high, 6 * density + 1)
    elif fs.h_sparse:
        h_low = 0
        h_high = 1
        h_values = np.linspace(h_low, h_high, 2 + 1)
    # selection
    s_low = -1.0
    s_high = 1.0
    s_values = fs.s_scale * np.linspace(s_low, s_high, 20 * density + 1)
    # hs product
    hs_values = np.outer(h_values, s_values)
    # compute all values in the grid
    f_values = np.zeros((len(h_values), len(s_values)))
    for i, h in enumerate(h_values):
        for j, s in enumerate(s_values):
            f_values[i, j] = kimura.get_fixation_probability_chen(p, s, h)
    if fs.x_is_log_pfix:
        f_values = np.log(f_values / p)
    # define the r script
    out = StringIO()
    if fs.infer_s:
        ylab_string = '"selection coefficient (s)"'
        ylim_low = np.min(s_values)
        ylim_high = np.max(s_values)
    elif fs.infer_hs:
        ylab_string = '"product of dominance and selection (hs)"'
        ylim_low = np.min(hs_values)
        ylim_high = np.max(hs_values)
    if fs.x_is_pfix:
        xlab_string = '"probability of eventual fixation"'
    elif fs.x_is_log_pfix:
        xlab_string = '"log of ratios of probability of eventual fixation"'
    xlim_low = np.min(f_values)
    xlim_high = np.max(f_values)
    colors = ['orange', 'green', 'blue']
    if fs.h_dense:
        main_string = ('"s = 2*N*sigma ; '
                       '%s < h < %s ; '
                       'p0 = %s"' % (h_low, h_high, p))
    elif fs.h_sparse:
        main_string = ('"s = 2*N*sigma ; ' 'p0 = %s"' % p)
    print >> out, mk_call_str(
        'plot',
        'c(0,0)',
        'c(0,0)',
        type='"n"',
        xlab=xlab_string,
        ylab=ylab_string,
        main=main_string,
        xlim='c(%s, %s)' % (xlim_low, xlim_high),
        ylim='c(%s, %s)' % (ylim_low, ylim_high),
    )
    if fs.h_dense:
        for h, s_row in zip(h_values, f_values):
            if fs.infer_s:
                y_vect = s_values
            elif fs.infer_hs:
                y_vect = h * s_values
            print >> out, mk_call_str(
                'lines',
                'c' + str(tuple(s_row)),
                'c' + str(tuple(y_vect)),
            )
        for s, h_col in zip(s_values, f_values.T):
            if fs.infer_s:
                y_vect = s * np.ones_like(h_col)
            elif fs.infer_hs:
                y_vect = s * h_values
            print >> out, mk_call_str(
                'lines',
                'c' + str(tuple(h_col)),
                'c' + str(tuple(y_vect)),
            )
    elif fs.h_sparse:
        for i, (h, s_row) in enumerate(zip(h_values, f_values)):
            if fs.infer_s:
                y_vect = s_values
            elif fs.infer_hs:
                y_vect = h * s_values
            print >> out, mk_call_str(
                'lines',
                'c' + str(tuple(s_row)),
                'c' + str(tuple(y_vect)),
                col='"%s"' % colors[i],
            )
    if fs.h_sparse:
        print >> out, mk_call_str(
            'legend',
            '"topleft"',
            'c' + str(tuple('h = %s' % x for x in h_values)),
            lty='c' + str(tuple([1] * len(h_values))),
            lwd='c' + str(tuple([2.5] * len(h_values))),
            col='c' + str(tuple(colors)),
        )
    script = out.getvalue().rstrip()
    # 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_no_table(
        script, device_name)
    if retcode:
        raise RUtil.RError(r_err)
    return image_data
Beispiel #9
0
def get_response_content(fs):
    # define the initial frequency
    p = 0.1
    # define some selection coefficients to plot
    h_low = 0.2
    h_high = 0.8
    h_values = np.linspace(h_low, h_high, 6 * 10 + 1)
    # define some dominance coefficients
    hs_values = [-0.2, -0.05, 0, 0.05, 0.2]
    colors = ['blue', 'green', 'black', 'orange', 'red']
    # get the values for each h
    arr = []
    for hs in hs_values:
        v = [
            kimura.get_fixation_probability_chen(p, hs / h, h)
            for h in h_values
        ]
        arr.append(v)
    #
    # define the r script
    out = StringIO()
    print >> out, 'h.values <- c', str(tuple(h_values))
    print >> out, 'ha <- c', str(tuple(arr[0]))
    print >> out, 'hb <- c', str(tuple(arr[1]))
    print >> out, 'hc <- c', str(tuple(arr[2]))
    print >> out, 'hd <- c', str(tuple(arr[3]))
    print >> out, 'he <- c', str(tuple(arr[4]))
    print >> out, mk_call_str(
        'plot',
        'h.values',
        'ha',
        type='"l"',
        xlab='"h"',
        ylab='"probability of eventual fixation"',
        main=('"fixation probabilities for various h*s ; '
              's = 2*N*sigma ; p0 = %s"' % p),
        ylim='c(0, 0.2)',
        col='"%s"' % colors[0],
    )
    print >> out, mk_call_str('lines',
                              'h.values',
                              'hb',
                              col='"%s"' % colors[1])
    print >> out, mk_call_str('lines',
                              'h.values',
                              'hc',
                              col='"%s"' % colors[2])
    print >> out, mk_call_str('lines',
                              'h.values',
                              'hd',
                              col='"%s"' % colors[3])
    print >> out, mk_call_str('lines',
                              'h.values',
                              'he',
                              col='"%s"' % colors[4])
    script = out.getvalue().rstrip()
    # 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_no_table(
        script, device_name)
    if retcode:
        raise RUtil.RError(r_err)
    return image_data