孟德尔自由组合定理--计算机模拟

版权所有 QQ:231469242

#coding=utf-8
#孟德尔自由组合定理

import copy

#大写字母表示显性基因,小写字母表示隐性基因
#gene,如D或d
#基因型genotype
genotype11=['Y','Y']
genotype12=['y','y']
genotype13=['Y','y']
genotype21=['R','R']
genotype22=['r','r']
genotype23=['R','r']
DNA1=[genotype13,genotype23]
DNA2=[genotype12,genotype22]
DNA3=[genotype11,genotype21]
DNA4=[genotype12,genotype22]
DNA_group=[DNA3,DNA4]

#基因表现型character
#字母大小写判断a.islower(),a.isupper()
def Character(genotype1):
for i in genotype1:
if i.isupper()==True:
return "Dominant"

return "recessive"


#Phenotype输入基因型,返回它的代表基因
def Phenotype(genotype1):
for i in genotype1:
if i.isupper()==True:
return i

return genotype1[0]


#配子后,基因型genotype的可能数量
def Assort(genotype1,genotype2):
genotype_list=[]

for gene1 in genotype1:
for gene2 in genotype2:
genotype=[gene1,gene2]
genotype_list.append(genotype)

return genotype_list


#返回不重复列表
def None_repeat_list(list1):
none_repeat_list=[]
for i in list1:
#print 'i:',i
if i not in none_repeat_list:
none_repeat_list.append(i)
return none_repeat_list

#计算各个基因类型比例
def Count_list(none_repeat_list,DNA_sort_list,n):
count_list=[]
for i in none_repeat_list:
count=DNA_sort_list.count(i)
count=count*1.0/n
count_list.append(count)

return count_list

#返回各个基因类型比例
def Ratio_genotype(none_repeat_list,count_list):
ratio_genotype=zip(none_repeat_list,count_list)
return ratio_genotype


#多个基因型的自由组合
def Independent_assort(DNA1,DNA2):
genotype00=DNA1[0]
genotype01=DNA2[0]
assortment1=Assort(genotype00,genotype01)

genotype10=DNA1[1]
genotype11=DNA2[1]
assortment2=Assort(genotype10,genotype11)

result=Assort(assortment1,assortment2)

return result

#排序函数,大写字母(线性基因)在前面.例如[r,R]排序后变成[R,r]
def Sort(list1):
k=0
n=len(list1[0])
#print "n:",n
for i in range(len(list1)):
#print 'i',i
while k<n:
#print 'k',k
#print 'list1[i][k]',list1[i][k]
list1[i][k].sort()
k+=1
k=0
return list1

#DNA所有性状结果
'''>>> Character_DNA(DNA_noneRepeat_assortment)
[['Y', 'R'], ['Y', 'R'], ['Y', 'r'], ['Y', 'R'], ['Y', 'R'], ['Y', 'r'], ['y', 'R'], ['y', 'R'], ['y', 'r']]'''


def Character_DNA(DNA_noneRepeat_assortment):
character_DNA=copy.deepcopy(DNA_noneRepeat_assortment)
k=0
noneRepeat_num=len(DNA_noneRepeat_assortment)
element_num=len(DNA_noneRepeat_assortment[0])
#print "n:",n
for i in range(noneRepeat_num):
#print 'i',i
while k<element_num:
#print 'k',k
#print 'list1[i][k]',list1[i][k]
character_DNA[i][k]=Phenotype(character_DNA[i][k])
k+=1
k=0
return character_DNA


#最终性状和对应数量比
def NoneRepeat_character_ratio(ratio_character,noneRepeat_character_DNA):
num=len(ratio_character)
list1=copy.deepcopy(ratio_character)
list_num=[]
count=0
for i in noneRepeat_character_DNA:
for k in list1:
if k[0]==i:
count+=k[1]
#print "count",count
list_num.append(count)
count=0
return list_num



def Analysis(DNA_group,ratio_character_result):
print "DNA_group:",DNA_group
print 'ratio_character_result:',ratio_character_result

#多个基因型的自由组合结果(有重复)
DNA_assortment=Independent_assort(DNA3,DNA4)

#多基因自由组合后的排序,大写字母(线性基因)在前面.例如[r,R]排序后变成[R,r]
DNA_sort=Sort(DNA_assortment)
#自由组合个数
n=len(DNA_sort)


#多个基因型的自由组合结果(无重复)
DNA_noneRepeat_assortment=None_repeat_list(DNA_sort)
#排序结果数量统计
count_list=Count_list(DNA_noneRepeat_assortment,DNA_sort,n)
#排序后基因类型对应的数量统计
ratio_genotype=Ratio_genotype(DNA_noneRepeat_assortment,count_list)

#DNA性状结果(重复)
character_DNA=Character_DNA(DNA_noneRepeat_assortment)
#数量统计
ratio_character=Ratio_genotype(character_DNA,count_list)

#DNA性状结果(去重复)
noneRepeat_character_DNA=None_repeat_list(character_DNA)

#DNA性状和数量比(不重复)
count_noneRepeat_character=NoneRepeat_character_ratio(ratio_character,noneRepeat_character_DNA)

#DNA性状和数量比,最后整合
ratio_character_result=Ratio_genotype(noneRepeat_character_DNA,count_noneRepeat_character)

#分析结果
Analysis(DNA_group,ratio_character_result)

原文地址:https://www.cnblogs.com/webRobot/p/7262330.html