我正尝试在 http://rosalind.info/problems/iprb/ 解决问题
Given: Three positive integers
k
,m
, andn
, representing a population containingk+m+n
organisms:k
individuals are homozygous dominant for a factor,m
are heterozygous, andn
are homozygous recessive.Return: The probability that two randomly selected mating organisms will produce an individual possessing a dominant allele (and thus displaying the dominant phenotype). Assume that any two organisms can mate.
我的解决方案适用于示例,但不适用于生成的任何问题。经过进一步研究,我似乎应该找到随机选择任何一个生物体的概率,找到选择第二个生物体的概率,然后是该配对产生具有显性等位基因的后代的概率。
那么我的问题是:我下面的代码找到的概率是多少?对于所有可能的交配,它是否找到具有显性等位基因的后代百分比 - 因此,我的代码不是计算一个随机交配的概率,而是在测试所有配对时求解具有显性等位基因的后代百分比?
f = open('rosalind_iprb.txt', 'r')
r = f.read()
s = r.split()
############# k = # homozygotes dominant, m = #heterozygotes, n = # homozygotes recessive
k = float(s[0])
m = float(s[1])
n = float(s[2])
############# Counts for pairing between each group and within groups
k_k = 0
k_m = 0
k_n = 0
m_m = 0
m_n = 0
n_n = 0
##############
if k > 1:
k_k = 1.0 + (k-2) * 2.0
k_m = k * m
k_n = k * n
if m > 1:
m_m = 1.0 + (m-2) * 2.0
m_n = m * n
if n> 1:
n_n = 1.0 + (n-2) * 2.0
#################
dom = k_k + k_m + k_n + 0.75*m_m + 0.5*m_n
total = k_k + k_m + k_n + m_m + m_n + n_n
chance = dom/total
print chance
最佳答案
看着你的代码,我很难弄清楚它应该做什么。我会在这里解决这个问题。
让我们简化措辞。有 n1 个类型 1、n2 个类型 2 和 n3 个类型 3 项。
有多少种方法可以从所有元素中选出一套尺寸为 2 的元素? (n1 + n2 + n3) 选择 2。
每对项目的项目类型对应于以下六个无序多重集之一:{1,1}、{2,2}、{3,3}、{1,2}、{1,3} , {2,3}
有多少个 {i,i} 形式的多重集?我选择 2.
有多少个 {i,j} 形式的多重集,其中 i != j? ni * nj.
因此,六个多重集的概率如下:
- P({1,1}) = [n1 选择 2]/[(n1 + n2 + n3) 选择 2]
- P({2,2}) = [n2 选择 2]/[(n1 + n2 + n3) 选择 2]
- P({3,3}) = [n3 选择 2]/[(n1 + n2 + n3) 选择 2]
- P({1,2}) = [n1 * n2]/[(n1 + n2 + n3) 选择 2]
- P({1,3}) = [n1 * n3]/[(n1 + n2 + n3) 选择 2]
- P({2,3}) = [n2 * n3]/[(n1 + n2 + n3) 选择 2]
这些总和为 1。请注意,对于 X > 1,[X 选择 2] 只是 [X * (X - 1)/2],对于 X = 0 或 1,则为 0。
Return: The probability that two randomly selected mating organisms will produce an individual possessing a dominant allele (and thus displaying the dominant phenotype).
要回答这个问题,您只需确定六个多重集中的哪个对应于此事件。由于缺乏遗传学知识来回答这个问题,我会把它留给你。
例如,假设如果两个 parent 中的任何一个是 1 型,就会产生一个显性等位基因。那么感兴趣的事件是 {1,1}、{1,2}、{1,3} 和事件是 P({1,1}) + P({1,2}) + P({1,3})。
关于python - 罗莎琳德:孟德尔第一定律,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26385968/