python实现孟德尔第一定律

发布时间 2023-08-30 20:29:19作者: 小鲨鱼2018

 

假定显性纯合子、杂合子、隐性纯合子的个数分别为k、m、n个,则随机收取两个个体,后代为显性性状的概率。

001、直接实现

[root@pc1 test01]# ls
test.py
[root@pc1 test01]# cat test.py      ## 计算程序
#!/usr/bin/env python
# -*- coding: utf-8 -*-

k = 2
m = 2
n = 2
t = k + m + n

# aa * aa
p1 = n/t * ((n - 1)/(t - 1))

# Aa * aa / aa * Aa
p2 = m/t * 0.5 * n/(t - 1) + n/t * m/(t - 1) * 0.5

# Aa * Aa
p3 = m/t * 0.5 * (m - 1)/(t - 1) * 0.5

print( 1 - p1 - p2 - p3)
[root@pc1 test01]# python3 test.py    ## 结算结果
0.7833333333333333

 

002、函数结构实现

[root@pc1 test01]# ls
test.py
[root@pc1 test01]# cat test.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

def mendel001(x, y, z):                ## 定义函数
        t = x + y + z
        # aa * aa
        p1 = z/t * (z - 1)/(t - 1)
        ## Aa * Aa
        p2 = y/t * 0.5 * (y - 1)/(t - 1) * 0.5
        ## aa * Aa / Aa *aa
        p3 = z/t * y/(t - 1) * 0.5 + y/t * 0.5 * z/(t - 1)
        return (1 - p1 - p2 - p3)

print(mendel001(2, 2, 2))
[root@pc1 test01]# python3 test.py      ## 计算结果
0.7833333333333333

 。