コード例 #1
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()
コード例 #2
0
ファイル: 20120620a.py プロジェクト: argriffing/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()
コード例 #3
0
ファイル: 20120621b.py プロジェクト: argriffing/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
    if nchromosomes < 2:
        raise ValueError(
                'please use a population of more than one chromosome')
    if npositions < 2:
        raise ValueError(
                'please use chromosomes longer than one position')
    ndimcap = 12
    if nchromosomes * npositions > ndimcap:
        raise ValueError(
                'at most 2^%d states are allowed per generation' % ndimcap)
    #
    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]
    #
    mutation = fs.mutation_param
    recombination = fs.recombination_param
    selection = fs.selection_param
    # define the prior probabilities of properties of the history
    nsiteboundaries = npositions - 1
    ngenboundaries = fs.ngenerations - 1
    no_mutation_prior = (1 - mutation)**(
            npositions*ngenboundaries*nchromosomes)
    no_recombination_prior = (1 - recombination)**(
            nsiteboundaries*ngenboundaries*nchromosomes)
    # get the transition matrices
    P_sr_s = pgmfancy.get_selection_recombination_transition_matrix_s(
            ci_to_short, short_to_count, sorted_chrom_lists,
            selection, recombination, nchromosomes, npositions)
    P_s_s = pgmfancy.get_selection_transition_matrix_s(
            ci_to_short, short_to_count, sorted_chrom_lists,
            selection, nchromosomes, npositions)
    P_m_s = pgmfancy.get_mutation_transition_matrix_s(
            ci_to_short, short_to_count, sorted_chrom_lists,
            mutation, nchromosomes, npositions)
    # define some conditional probabilities
    p_b_given_a_s = linalg.matrix_power(
            np.dot(P_sr_s, P_m_s), fs.ngenerations-1)[
                    initial_short, final_short]
    p_b_given_a_no_mutation_s = linalg.matrix_power(
            P_sr_s, fs.ngenerations-1)[
                    initial_short, final_short]
    p_b_given_a_no_recombination_s = linalg.matrix_power(
            np.dot(P_s_s, P_m_s), fs.ngenerations-1)[
                    initial_short, final_short]
    # define the conditional properties of properties of the history
    no_mutation_posterior = (
            no_mutation_prior * p_b_given_a_no_mutation_s) / (
                    p_b_given_a_s)
    no_recombination_posterior = (
            no_recombination_prior * p_b_given_a_no_recombination_s) / (
                    p_b_given_a_s)
    #
    out = StringIO()
    print >> out, 'probability of no mutation in the path history:'
    print >> out, 'unconditional:        %s' % no_mutation_prior
    print >> out, 'endpoint-conditioned: %s' % no_mutation_posterior
    print >> out
    print >> out, 'probability of no recombination in the path history:'
    print >> out, 'unconditional:        %s' % no_recombination_prior
    print >> out, 'endpoint-conditioned: %s' % no_recombination_posterior
    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
    if nchromosomes < 2:
        raise ValueError('please use a population of more than one chromosome')
    if npositions < 2:
        raise ValueError('please use chromosomes longer than one position')
    ndimcap = 12
    if nchromosomes * npositions > ndimcap:
        raise ValueError('at most 2^%d states are allowed per generation' %
                         ndimcap)
    #
    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]
    #
    mutation = fs.mutation_param
    recombination = fs.recombination_param
    selection = fs.selection_param
    # define the prior probabilities of properties of the history
    nsiteboundaries = npositions - 1
    ngenboundaries = fs.ngenerations - 1
    no_mutation_prior = (1 - mutation)**(npositions * ngenboundaries *
                                         nchromosomes)
    no_recombination_prior = (1 -
                              recombination)**(nsiteboundaries *
                                               ngenboundaries * nchromosomes)
    # get the transition matrices
    P_sr_s = pgmfancy.get_selection_recombination_transition_matrix_s(
        ci_to_short, short_to_count, sorted_chrom_lists, selection,
        recombination, nchromosomes, npositions)
    P_s_s = pgmfancy.get_selection_transition_matrix_s(ci_to_short,
                                                       short_to_count,
                                                       sorted_chrom_lists,
                                                       selection, nchromosomes,
                                                       npositions)
    P_m_s = pgmfancy.get_mutation_transition_matrix_s(ci_to_short,
                                                      short_to_count,
                                                      sorted_chrom_lists,
                                                      mutation, nchromosomes,
                                                      npositions)
    # define some conditional probabilities
    p_b_given_a_s = linalg.matrix_power(np.dot(P_sr_s, P_m_s),
                                        fs.ngenerations - 1)[initial_short,
                                                             final_short]
    p_b_given_a_no_mutation_s = linalg.matrix_power(
        P_sr_s, fs.ngenerations - 1)[initial_short, final_short]
    p_b_given_a_no_recombination_s = linalg.matrix_power(
        np.dot(P_s_s, P_m_s), fs.ngenerations - 1)[initial_short, final_short]
    # define the conditional properties of properties of the history
    no_mutation_posterior = (no_mutation_prior *
                             p_b_given_a_no_mutation_s) / (p_b_given_a_s)
    no_recombination_posterior = (no_recombination_prior *
                                  p_b_given_a_no_recombination_s) / (
                                      p_b_given_a_s)
    #
    out = StringIO()
    print >> out, 'probability of no mutation in the path history:'
    print >> out, 'unconditional:        %s' % no_mutation_prior
    print >> out, 'endpoint-conditioned: %s' % no_mutation_posterior
    print >> out
    print >> out, 'probability of no recombination in the path history:'
    print >> out, 'unconditional:        %s' % no_recombination_prior
    print >> out, 'endpoint-conditioned: %s' % no_recombination_posterior
    print >> out
    return out.getvalue()