예제 #1
0
파일: 20120621a.py 프로젝트: BIGtigr/xgcode
def get_response_content(fs):
    parental_state = multiline_state_to_ndarray(fs.parental_state)
    nchromosomes, npositions = parental_state.shape
    if nchromosomes * npositions > 10:
        raise ValueError('at most 2^10 states are allowed')
    #
    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
    P = get_transition_matrix(
            selection, mutation, recombination, nchromosomes, npositions)
    # sample the endpoint conditioned path
    initial_integer_state = popgenmarkov.bin2d_to_int(initial_state)
    final_integer_state = popgenmarkov.bin2d_to_int(final_state)
    path = sample_endpoint_conditioned_path(
            initial_integer_state, final_integer_state,
            fs.ngenerations, P)
    print >> out, 'sampled endpoint conditioned path, including endpoints:'
    print >> out
    for integer_state in path:
        # print integer_state
        print >> out, ndarray_to_multiline_state(
                popgenmarkov.int_to_bin2d(
                    integer_state, nchromosomes, npositions))
        print >> out
    return out.getvalue()
예제 #2
0
 def test_regress_mutation_probability_endpoint_conditioning(self):
     ngenerations = 10
     selection = 2.0
     mutation = 0.0001
     recombination = 0.001
     nchromosomes = 2
     npositions = 4
     K_initial = np.array([
         [1,1,1,1],
         [0,0,0,0]], dtype=np.int8)
     K_final = np.array([
         [1,1,0,0],
         [0,0,1,1]], dtype=np.int8)
     #
     ngenboundaries = ngenerations - 1
     no_mutation_prior = (1 - mutation)**(
             npositions*ngenboundaries*nchromosomes)
     #
     initial_long = popgenmarkov.bin2d_to_int(K_initial)
     final_long = popgenmarkov.bin2d_to_int(K_final)
     ci_to_short, short_to_count, sorted_chrom_lists = get_state_space_info(
             nchromosomes, npositions)
     initial_ci = chroms_to_index(
             sorted(popgenmarkov.bin_to_int(row) for row in K_initial),
             npositions)
     initial_short = ci_to_short[initial_ci]
     final_ci = chroms_to_index(
             sorted(popgenmarkov.bin_to_int(row) for row in K_final),
             npositions)
     final_short = ci_to_short[final_ci]
     # get an answer using the less efficient methods
     P_sr = popgenmarkov.get_selection_recombination_transition_matrix(
             selection, recombination, nchromosomes, npositions)
     P_m = popgenmarkov.get_mutation_transition_matrix(
             mutation, nchromosomes, npositions)
     p_b_given_a = linalg.matrix_power(np.dot(P_sr, P_m), ngenerations-1)[
             initial_long, final_long]
     p_b_given_a_no_mutation = linalg.matrix_power(P_sr, ngenerations-1)[
             initial_long, final_long]
     no_mutation_posterior = (
             no_mutation_prior * p_b_given_a_no_mutation) / p_b_given_a
     # get an answer using the more efficient methods
     P_sr_s = get_selection_recombination_transition_matrix_s(
             ci_to_short, short_to_count, sorted_chrom_lists,
             selection, recombination, nchromosomes, npositions)
     P_m_s = get_mutation_transition_matrix_s(
             ci_to_short, short_to_count, sorted_chrom_lists,
             mutation, nchromosomes, npositions)
     p_b_given_a_s = linalg.matrix_power(
             np.dot(P_sr_s, P_m_s), ngenerations-1)[
                     initial_short, final_short]
     p_b_given_a_no_mutation_s = linalg.matrix_power(
             P_sr_s, ngenerations-1)[
                     initial_short, final_short]
     no_mutation_posterior_s = (
             no_mutation_prior * p_b_given_a_no_mutation_s) / p_b_given_a_s
     #
     self.assertTrue(
             np.allclose(no_mutation_posterior, no_mutation_posterior_s))
예제 #3
0
파일: pgmfancy.py 프로젝트: BIGtigr/xgcode
 def test_regress_mutation_probability_endpoint_conditioning(self):
     ngenerations = 10
     selection = 2.0
     mutation = 0.0001
     recombination = 0.001
     nchromosomes = 2
     npositions = 4
     K_initial = np.array([[1, 1, 1, 1], [0, 0, 0, 0]], dtype=np.int8)
     K_final = np.array([[1, 1, 0, 0], [0, 0, 1, 1]], dtype=np.int8)
     #
     ngenboundaries = ngenerations - 1
     no_mutation_prior = (1 - mutation)**(npositions * ngenboundaries *
                                          nchromosomes)
     #
     initial_long = popgenmarkov.bin2d_to_int(K_initial)
     final_long = popgenmarkov.bin2d_to_int(K_final)
     ci_to_short, short_to_count, sorted_chrom_lists = get_state_space_info(
         nchromosomes, npositions)
     initial_ci = chroms_to_index(
         sorted(popgenmarkov.bin_to_int(row) for row in K_initial),
         npositions)
     initial_short = ci_to_short[initial_ci]
     final_ci = chroms_to_index(
         sorted(popgenmarkov.bin_to_int(row) for row in K_final),
         npositions)
     final_short = ci_to_short[final_ci]
     # get an answer using the less efficient methods
     P_sr = popgenmarkov.get_selection_recombination_transition_matrix(
         selection, recombination, nchromosomes, npositions)
     P_m = popgenmarkov.get_mutation_transition_matrix(
         mutation, nchromosomes, npositions)
     p_b_given_a = linalg.matrix_power(np.dot(P_sr, P_m),
                                       ngenerations - 1)[initial_long,
                                                         final_long]
     p_b_given_a_no_mutation = linalg.matrix_power(
         P_sr, ngenerations - 1)[initial_long, final_long]
     no_mutation_posterior = (no_mutation_prior *
                              p_b_given_a_no_mutation) / p_b_given_a
     # get an answer using the more efficient methods
     P_sr_s = get_selection_recombination_transition_matrix_s(
         ci_to_short, short_to_count, sorted_chrom_lists, selection,
         recombination, nchromosomes, npositions)
     P_m_s = get_mutation_transition_matrix_s(ci_to_short, short_to_count,
                                              sorted_chrom_lists, mutation,
                                              nchromosomes, npositions)
     p_b_given_a_s = linalg.matrix_power(np.dot(P_sr_s, P_m_s),
                                         ngenerations - 1)[initial_short,
                                                           final_short]
     p_b_given_a_no_mutation_s = linalg.matrix_power(
         P_sr_s, ngenerations - 1)[initial_short, final_short]
     no_mutation_posterior_s = (no_mutation_prior *
                                p_b_given_a_no_mutation_s) / p_b_given_a_s
     #
     self.assertTrue(
         np.allclose(no_mutation_posterior, no_mutation_posterior_s))