def get_sample_distn(M_large, M_small, mut_ratio, fit_ratio, h): """ @param M_large: an array of microstate counts @param M_small: an array of microstate counts @param mut_ratio: positive mutation rate ratio @param fit_ratio: positive fitness ratio @param h: recessiveness or dominance between 0 and 1 @return: a distribution over polymorphic samples """ # N is the population haploid count # n is the sample count N = sum(M_large[0]) n = sum(M_small[0]) nstates_large = len(M_large) nstates_small = len(M_small) if N-1 != nstates_large: raise Exception('internal error unexpected population state space') if N % 2: raise Exception('expected an even number of haploids in the population') if n-1 != nstates_small: raise Exception('internal error unexpected sample state space') # Get the value of s to construct the diallelic transition matrix. # It is positive when the lowercase allele is less fit. s = 1.0 - fit_ratio # Get the diallelic transition matrix. # The first state is allele A fixation # and the last state is allele a fixation. N_diploid = N // 2 P = np.exp(wfengine.create_diallelic_recessive(N_diploid, s, h)) MatrixUtil.assert_transition_matrix(P) # Get the expected number of visits # to polymorphic states, starting from (1, N-1) and from (N-1, 1), # assuming that fixed states are absorbing. I = np.eye(N - 1) Q = P[1:-1, 1:-1] B = np.zeros((N - 1, 2)) B[0,0] = 1 B[-1,-1] = 1 X = linalg.solve((I - Q).T, B) # At this point X has two columns, each giving an array of expectations. # The first column gives expectations starting from a single A allele. # The second column gives expectations starting from a single a allele. # To get the expectations defined by the mixture, # take the weighted sum of the columns. e_large = X[:,0] * mut_ratio + X[:,1] # Compute a multivariate hypergeometric reduction # from the population state space expectations # to the sample state space expectations. e_small = wfengine.reduce_hypergeometric(M_large, M_small, e_large) # Compute the distribution. return e_small / e_small.sum()
def get_response_content(fs): out = StringIO() print >> out, '<html><body><table border="1" cellpadding="10">' # headers = ('N', 's', '2Ns', 'h', 'f00', 'f01', 'f11', '2Nsh', 'NP01', 'NP10') print >> out, get_html_table_row(headers) # In this web script, N is a diploid population size. # get the transition matrix for x2Nsh in (0.075, 0.75): for h in (0.25, 0.5, 0.75): for N in (50, 200): x2N = 2 * N x2Ns = x2Nsh / float(h) s = x2Ns / float(x2N) # P = np.exp(wfengine.create_diallelic_recessive(N, s, 1 - h)) #print P MatrixUtil.assert_transition_matrix(P) P01, P10 = get_fixation_probabilities(P) NP01 = N * P01 NP10 = N * P10 # f00 = 1.0 f11 = 1.0 - s f01 = (1 - h) * f00 + h * f11 # values = ( N, s, x2Ns, h, f00, f01, f11, x2Nsh, NP01, NP10, ) print >> out, get_html_table_row(values) print >> out, '</table></body></html>' return out.getvalue()
def get_sample_distn(N, n, mutation_rate, fitness_ratio, h): """ @param N: haploid pop size @param n: allele sample size @param mutation_rate: mutation rate @param fitness_ratio: fitness ratio @param h: dominance parameter 0.5 when additive @return: a distribution over n+1 mono-/di-morphic sample states """ s = 1.0 - fitness_ratio P = np.exp(wfengine.create_diallelic_recessive(N // 2, s, h)) MatrixUtil.assert_transition_matrix(P) # allow mutation out of the fixed states P[0, 0] = 1.0 - mutation_rate P[0, 1] = mutation_rate P[-1, -1] = 1.0 - mutation_rate P[-1, -2] = mutation_rate MatrixUtil.assert_transition_matrix(P) # get the population stationary distribution v_large = MatrixUtil.get_stationary_distribution(P) # get the allele distribution v_small = StatsUtil.subsample_pmf_without_replacement(v_large, n) return v_small
def get_response_content(fs): out = StringIO() print >> out, '<html><body><table border="1" cellpadding="10">' # headers = ( 'N', 's', '2Ns', 'h', 'f00', 'f01', 'f11', '2Nsh', 'NP01', 'NP10') print >> out, get_html_table_row(headers) # In this web script, N is a diploid population size. # get the transition matrix for x2Nsh in (0.075, 0.75): for h in (0.25, 0.5, 0.75): for N in (50, 200): x2N = 2*N x2Ns = x2Nsh / float(h) s = x2Ns / float(x2N) # P = np.exp(wfengine.create_diallelic_recessive(N, s, 1-h)) #print P MatrixUtil.assert_transition_matrix(P) P01, P10 = get_fixation_probabilities(P) NP01 = N * P01 NP10 = N * P10 # f00 = 1.0 f11 = 1.0 - s f01 = (1-h) * f00 + h * f11 # values = ( N, s, x2Ns, h, f00, f01, f11, x2Nsh, NP01, NP10, ) print >> out, get_html_table_row(values) print >> out, '</table></body></html>' return out.getvalue()