这是一个由我的教授分发并由学生修改的群体遗传学程序。
基本上,它应该使用给定的样本、人口和突变率 (u) 来模拟预期的突变次数 20 次。然而,一个关键部分是总分支长度 (L),它是各种较小分支长度 (branch_length) 的总和。但是,当我如下定义 L 时,它会不断返回错误:
L += branch_length
NameError: name 'L' is not defined
我不确定哪里出了问题,因为 tree_depth 的定义方式相同并且可以完美运行。
完整代码如下:
from random import expovariate
from pgen import poidev
K = 77 # sample size (number of gene copies)
twoN = 5000 # population size
u = .001
tree_depth = 0.0 # age of last common ancestor in generations
# Each pass through loop deals with one coalescent interval.
for A in range(20):
while K > 1:
h = K*(K-1)/(2.0*twoN) # hazard of a coalescent event
t = expovariate(h) # time until next coalescent event
tree_depth += t
branch_length = t*K
K -= 1
L += branch_length
S = poidev(u*L)
print "Simulation:", A+1, "Total Mutations:", S
print "Total tree depth (L):", L, "generations"
我是不是错过了一些非常非常明显的东西?提前致谢。
最佳答案
L += x
将 x 添加到现有的 L,但您尚未初始化 L。据推测,您希望 L = 0
位于文件顶部的某个位置.
关于 python 2.7 : L += branch_length results in NameError: name 'L' not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7693273/