예제 #1
0
파일: 20120711a.py 프로젝트: BIGtigr/xgcode
def get_response_content(fs):
    # precompute some transition matrices
    P_drift_selection = pgmsinglesite.create_drift_selection_transition_matrix(
        fs.npop, fs.selection_ratio)
    MatrixUtil.assert_transition_matrix(P_drift_selection)
    P_mutation = pgmsinglesite.create_mutation_transition_matrix(
        fs.npop, fs.mutation_ab, fs.mutation_ba)
    MatrixUtil.assert_transition_matrix(P_mutation)
    # define the R table headers
    headers = ['generation', 'number.of.mutants']
    # compute the path samples
    P = np.dot(P_drift_selection, P_mutation)
    mypath = PathSampler.sample_endpoint_conditioned_path(
        fs.nmutants_initial, fs.nmutants_final, fs.ngenerations, P)
    arr = [[i, nmutants] for i, nmutants in enumerate(mypath)]
    # create the R table string and scripts
    # get the R table
    table_string = RUtil.get_table_string(arr, headers)
    # get the R script
    script = get_ggplot()
    # 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
예제 #2
0
def get_response_content(fs):
    # precompute some transition matrices
    P_drift_selection = pgmsinglesite.create_drift_selection_transition_matrix(
            fs.npop, fs.selection_ratio)
    MatrixUtil.assert_transition_matrix(P_drift_selection)
    P_mutation = pgmsinglesite.create_mutation_transition_matrix(
            fs.npop, fs.mutation_ab, fs.mutation_ba)
    MatrixUtil.assert_transition_matrix(P_mutation)
    # define the R table headers
    headers = ['generation', 'number.of.mutants']
    # compute the path samples
    P = np.dot(P_drift_selection, P_mutation)
    mypath = PathSampler.sample_endpoint_conditioned_path(
            fs.nmutants_initial, fs.nmutants_final, fs.ngenerations, P)
    arr = [[i, nmutants] for i, nmutants in enumerate(mypath)]
    # create the R table string and scripts
    # get the R table
    table_string = RUtil.get_table_string(arr, headers)
    # get the R script
    script = get_ggplot()
    # 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
예제 #3
0
파일: 20120620a.py 프로젝트: BIGtigr/xgcode
def get_response_content(fs):
    initial_state = multiline_state_to_ndarray(fs.initial_state)
    final_state = multiline_state_to_ndarray(fs.final_state)
    if initial_state.shape != final_state.shape:
        raise ValueError(
            'initial and final states do not have the same dimensions')
    nchromosomes, npositions = initial_state.shape
    ndimcap = 12
    if nchromosomes * npositions > ndimcap:
        raise ValueError('at most 2^%d states are allowed per generation' %
                         ndimcap)
    #
    mutation = 0.5 * fs.mutation_param
    recombination = 0.5 * fs.recombination_param
    selection = fs.selection_param
    #
    out = StringIO()
    print >> out, 'number of chromosomes:'
    print >> out, nchromosomes
    print >> out
    print >> out, 'number of positions per chromosome:'
    print >> out, npositions
    print >> out
    # define the transition matrix
    results = pgmfancy.get_state_space_info(nchromosomes, npositions)
    ci_to_short, short_to_count, sorted_chrom_lists = results
    initial_ci = pgmfancy.chroms_to_index(
        sorted(popgenmarkov.bin_to_int(row) for row in initial_state),
        npositions)
    initial_short = ci_to_short[initial_ci]
    final_ci = pgmfancy.chroms_to_index(
        sorted(popgenmarkov.bin_to_int(row) for row in final_state),
        npositions)
    final_short = ci_to_short[final_ci]
    P_sr_s = pgmfancy.get_selection_recombination_transition_matrix_s(
        ci_to_short, short_to_count, sorted_chrom_lists, selection,
        recombination, nchromosomes, npositions)
    P_m_s = pgmfancy.get_mutation_transition_matrix_s(ci_to_short,
                                                      short_to_count,
                                                      sorted_chrom_lists,
                                                      mutation, nchromosomes,
                                                      npositions)
    P = np.dot(P_sr_s, P_m_s)
    # sample the endpoint conditioned path
    path = PathSampler.sample_endpoint_conditioned_path(
        initial_short, final_short, fs.ngenerations, P)
    print >> out, 'sampled endpoint conditioned path, including endpoints:'
    print >> out
    # print the initial state without shuffling of individuals
    print >> out, ndarray_to_multiline_state(initial_state)
    print >> out
    # print the intermediate states with randomly permuted individuals
    for short_index in path[1:-1]:
        chroms = sorted_chrom_lists[short_index]
        random.shuffle(chroms)
        K = np.array([popgenmarkov.int_to_bin(x, npositions) for x in chroms])
        print >> out, ndarray_to_multiline_state(K)
        print >> out
    # print the final state without shuffling of individuals
    print >> out, ndarray_to_multiline_state(final_state)
    print >> out
    return out.getvalue()
예제 #4
0
def get_response_content(fs):
    initial_state = multiline_state_to_ndarray(fs.initial_state)
    final_state = multiline_state_to_ndarray(fs.final_state)
    if initial_state.shape != final_state.shape:
        raise ValueError(
                'initial and final states do not have the same dimensions')
    nchromosomes, npositions = initial_state.shape
    ndimcap = 12
    if nchromosomes * npositions > ndimcap:
        raise ValueError(
                'at most 2^%d states are allowed per generation' % ndimcap)
    #
    mutation = 0.5 * fs.mutation_param
    recombination = 0.5 * fs.recombination_param
    selection = fs.selection_param
    #
    out = StringIO()
    print >> out, 'number of chromosomes:'
    print >> out, nchromosomes
    print >> out
    print >> out, 'number of positions per chromosome:'
    print >> out, npositions
    print >> out
    # define the transition matrix
    results = pgmfancy.get_state_space_info(nchromosomes, npositions)
    ci_to_short, short_to_count, sorted_chrom_lists = results
    initial_ci = pgmfancy.chroms_to_index(
            sorted(popgenmarkov.bin_to_int(row) for row in initial_state),
            npositions)
    initial_short = ci_to_short[initial_ci]
    final_ci = pgmfancy.chroms_to_index(
            sorted(popgenmarkov.bin_to_int(row) for row in final_state),
            npositions)
    final_short = ci_to_short[final_ci]
    P_sr_s = pgmfancy.get_selection_recombination_transition_matrix_s(
            ci_to_short, short_to_count, sorted_chrom_lists,
            selection, recombination, nchromosomes, npositions)
    P_m_s = pgmfancy.get_mutation_transition_matrix_s(
            ci_to_short, short_to_count, sorted_chrom_lists,
            mutation, nchromosomes, npositions)
    P = np.dot(P_sr_s, P_m_s)
    # sample the endpoint conditioned path
    path = PathSampler.sample_endpoint_conditioned_path(
            initial_short, final_short,
            fs.ngenerations, P)
    print >> out, 'sampled endpoint conditioned path, including endpoints:'
    print >> out
    # print the initial state without shuffling of individuals
    print >> out, ndarray_to_multiline_state(initial_state)
    print >> out
    # print the intermediate states with randomly permuted individuals
    for short_index in path[1:-1]:
        chroms = sorted_chrom_lists[short_index]
        random.shuffle(chroms)
        K = np.array([popgenmarkov.int_to_bin(x, npositions) for x in chroms])
        print >> out, ndarray_to_multiline_state(K)
        print >> out
    # print the final state without shuffling of individuals
    print >> out, ndarray_to_multiline_state(final_state)
    print >> out
    return out.getvalue()