ここでは集団遺伝学のイメージを掴むために簡単なシミュレーションを行います。以下ではpythonを使ってシミュレーションを行います。他のプログラミング言語でも可能ですので、すでに他の言語に親しんでいる人はお好きな言語で実装していただいて構いません。
遺伝的浮動の影響をシミュレートして、その結果を図示するところまでが目標です
標準的なpythonに加えて、いくつか機能を追加します。今回は以下のライブラリを使います。
# time
import time
# algebra
import numpy as np
# plot
import matplotlib.pyplot as plt
# make your plot more attractive
import seaborn
単純化された状況を考えます。ゲノム上のある一箇所の遺伝子座に注目します。その遺伝子座に祖先型と変異型の2タイプが存在する場合を想定します(1-locus 2-allele model)。その場所には祖先型と変異型のどちらかしか存在しません。つまり「変異型のさらに変異型」などは考えません。この仮定のもとで変異型の個数が変化するプロセスをコンピューター上で再現します。
伝統的な集団遺伝学(教科書など)では、二倍体生物の集団の大きさを$N$として、集団全体では$2N$のゲノムが存在する状態を考えるケースが多いです。半数体生物を想定している場合は集団サイズが$2N$と考えれば、結果をそのまま使えます。ここでは二倍体生物特有の現象(優性劣性のある自然選択など)は考えないこととします。以下では集団遺伝の慣習に従って$N$を個体数、集団中のゲノムの数を$2N$で表現します
いま集団に存在する$2N$個のゲノムのうち$x$個が変異型だったとします。この時の割合$p=\frac{x}{2N}$を遺伝子頻度と呼びます。今回はこの割合$p$の時間変化をシミュレートします。
以下のシミュレーションでは祖先型であっても変異型であっても生存や生殖成功率に関して何の影響もないものと仮定します(中立モデル)。この場合、子世代の各個体の持つ遺伝子型は親世代集団からランダムに選ばれたタイプ同士が組みになったものと考えることができます。このように遺伝子の頻度に基づいて次世代の遺伝子の組成を決めるモデルをWright-Fisher modelといい、集団遺伝で標準的に用いられているモデルの一つです。
いま仮に集団の大きさ(集団中のゲノム数)が100だとしましょう。二倍体の個体数だと50個体で100ゲノム、半数体の生物だと100個体で100ゲノムに相当します。環境は常に一定であり、集団の大きさは変わらないものとします。説明のため、第0世代目の生物集団ではすでに祖先型と変異型が混ざっている状態にあると考えます。第0世代目の変異型の数が40である場合について考えてみましょう。
この初期状態をプログラム中の変数で表します。集団の大きさを変数sizeであらわし、変異型の数をinitで表すものとすると、初期状態は以下のように設定できます
size=100
init=40
これが親世代です。この親世代から配偶子が作られ、遺伝子プールが形成されます。
では親世代の変異型の遺伝子頻度を求めましょう
p=init/size
print('Gene frequency (0th gen): p =',p)
この親世代の遺伝子プールから集団サイズ分だけ遺伝子をランダムに抽出して、第一世代を作ります。
この親世代の遺伝子プールからランダムに選ぶという操作をプログラム上では
という方法で実現します。
次世代の集団を作るプロセスをプログラムの流れで表現すると、
このプロセスをプログラムで表現すると、
next=0 # initialize num of mutant alleles in the next generation
# pick up 'size' genomes
for i in range(size):
# determine allele type
if( np.random.rand()<p ):
# if derived
next+=1
# print out results
print('1st generation')
print(size-next) # ancestral type
print(next) # derived type
第二世代目の集団にとっては第一世代が親ですので、第0世代の頻度ではなく、第一世代目の遺伝子頻度に基づいて第二世代を作ります。第0世代目の遺伝子頻度とは同じこともあるし微妙に変化していることもあります。この場合は、
p=next/size
print('Gene frequency (1st gen): p =',p)
親世代の頻度を反映した遺伝子プールからランダムにサンプルし、第二世代目を作ります
next=0
for i in range(size):
if( np.random.rand()<p ):
next+=1
# print out
print('2nd generation')
print(size-next) # ancestral type
print(next) # derived type
第三世代は第二世代から作られるので、第二世代目の遺伝子頻度が影響します
p=next/size
print('Gene frequency (2nd gen): p =',p)
next=0
for i in range(size):
if( np.random.rand()<p ):
next+=1
print('3rd generation')
print(size-next) # ancestral type
print(next) # derived type
p=next/size
print('Gene frequency (3rd gen): p =',p)
next=0
for i in range(size):
if( np.random.rand()<p ):
next+=1
print('4th generation')
print(size-next) # ancestral type
print(next) # derived type
p=next/size
print('Gene frequency (4th gen): p =',p)
next=0
for i in range(size):
if( np.random.rand()<p ):
next+=1
print('5th generation')
print(size-next) # ancestral type
print(next) # derived type
ここまでの計算を振り返ると、各世代で同じ操作を繰り返していることがわかります。つまり、
プロセスそのものは毎世代同一です。違うのは親世代の遺伝子頻度とその結果得られる子世代の変異型の数です。
毎回同じプロセスを繰り返す部分を「関数」として定義して独立させます。プログラムを再利用してコーディングの手間を省き、エラーを防ぎます。
def make_next_gen(derived, size=100):
'''
Calculate the number of derived alleles in the next generation.
Arguments:
derived: number of derived alleles in the parent population
size: size of the population
Returns:
the number of derived alleles in the child population
'''
p=derived/size
next=0
for i in range(size):
if( np.random.rand()<p ):
next+=1
return next
この関数を使うと、先ほどのシミュレーションは少しすっきりします。第5世代目までの変異型の数をシミュレートしてみましょう。
init = 40
print(init)
next=make_next_gen(init, 100)
print(next)
next=make_next_gen(next, 100)
print(next)
next=make_next_gen(next, 100)
print(next)
next=make_next_gen(next, 100)
print(next)
next=make_next_gen(next, 100)
print(next)
こうやってみてみると、毎世代
と、同じ操作を繰り返していることがわかります。
単純な繰り返し操作はコンピュータに任せましょう
# initialization
derived = 40
size = 100
# print out the initial number of derived alleles
print(derived)
# for 20 generations
for i in range(20):
# make the next generation
derived = make_next_gen(derived, size)
# print out the number of derived alleles
print(derived)
遺伝子数ではなく、遺伝子頻度でプリントアウトしましょう。
# initialization
derived = 40
size = 100
# print out the initial number of derived alleles
print(derived/size)
# for 20 generations
for i in range(20):
# make the next generation
derived = make_next_gen(derived, size)
# print out the number of derived alleles
print(derived/size)
シミュレーションの中心部分は完成です
さらに手を加えましょう。これまでは結果を画面に出力して、それで終わっていました。結果を解析したりグラフを作るためにはデータを保存する必要があります。
ここでは遺伝子頻度をリスト型の変数に保存します。リスト型とは一連の数字の列を保存するためのデータ型です。
trという名前のリスト型変数を作ります。入れ物だけ作り、中身は空っぽです。その後、初期頻度を追加(append)しています。毎世代得られた変異型の遺伝子頻度を追加していきます。
# initialization
derived = 40
size = 100
# initialize a list
tr = []
# append the initial frequency
tr.append( derived/size )
# 50 generations
for i in range(50):
# make next generation
derived = make_next_gen(derived, size)
# append freq
tr.append( derived/size )
print(tr)
横軸に時間を取り縦軸に頻度をとってプロットすると、頻度の軌跡を描くことができます
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.plot(tr)
ax.set_xlim(0,50)
ax.set_ylim(0,1)
plt.show()
軌跡一つだけでなく、何本も描いてみよう。やり方はシンプルです。ここまでで行ったtrajectoryの作製を、好みの回数だけ繰り返すだけです。
繰り返して得られた軌跡を、順次simという名前のリストにappendしましょう
sim.append(tr)
つまりリストのリストを作成します
# a list of lists
sim = []
# set initial conditions
derived = 40
size = 100
howmany = 1000
generations = 1000
# simulation starts
t_start = time.time()
# repeat simulation 'howmany' times
for j in range(howmany):
# reset initial condition
derived = 40
size = 100
tr = [derived/size]
# simulate frequency trajectory
for i in range(generations):
# make next generation
derived = make_next_gen(derived, size)
# append freq
tr.append( derived/size )
# append trajectory
sim.append(tr)
# finished
t_end = time.time()
print( '{0:.3f} seconds'.format(t_end-t_start))
ここで得られたsim[0]が1回目のシミュレーション結果です。sim[1]が2回目のシミュレーション結果。sim[2]が3回目の...
各sim[ i ]は数値の並びからできているリストで、0世代目からの毎世代ごとの頻度が保存されています。
この結果をプロットしましょう。以下に先頭から10回目までのシミュレーション結果をプロットした例を示します
fig=plt.figure()
ax=fig.add_subplot(111)
for i in range(10):
ax.plot(sim[i])
ax.set_xlim(0,50)
plt.show()
上でやっていることは、最初にfigure領域を作製し
fig=plt.figure()
プロット領域を作製し
ax=fig.add_subplot(1,1,1)
順次プロットを載せて
for i in range(10):
ax.plot(sim[i])
x軸の範囲を設定し、
ax.set_xlim(0,50)
画面にグラフ出力
plt.show()
以上のように頻度変化を求めることができました。
せっかくシミュレーションしたので、固定確率(割合)・固定あるいは消失までの待ち時間の分布とその平均を求めてみましょう。固定するまで、消失するまでの待ち時間を数学を使って解析的に求めることは難しいけれども、シミュレーションを使えば比較的容易に求めることができます
fixed = []
lost = []
for i in range(len(sim)):
trajectory = np.array(sim[i])
if(trajectory[-1]>0):
fixed.append( trajectory[ trajectory<1 ].shape[0] )
else:
lost.append( trajectory[ trajectory>0 ].shape[0] )
p_fix = len(fixed)/len(sim)
ave_fixation_time = np.average(fixed)
ave_time_until_lost = np.average(lost)
print( "the proportion of fixed trajectories is {0:.2f}".format(p_fix))
print( "the average time until fixation of the allele is {0:.2f}".format(ave_fixation_time))
print( "the average time until loss of the allele is {0:.2f}".format(ave_time_until_lost))
まずはシミュレーションを固定した場合と消失した場合に分ける。最初に配列を初期化して、
fixed = []
lost = []
シミュレーション結果を一つづつ取り出す。そのままでは扱いにくいのでnumpyのnp.array形式に変換する。変換したデータ(i番目のシミュレーション結果)をtrajectoryという変数に保存している
for i in range(len(sim)):
trajectory = np.array(sim[i])
次に各頻度変化の最後の部分を見て、その時の頻度を確認する。十分長い時間シミュレーションを行なっているので、全ての場合は固定あるいは消失しており、中途半端に多様性として残っている場合はないと判断する。もし固定していれば頻度が1になっているはずで、消失した場合は頻度が0になっているはず。
そこで各シミュレーションの1番最後を取り出し、その値が0よりも大きい時は固定したものと判断する。ここで1と等しいならば、という判断をしないことに注意。コンピュータ内部では二進法で計算しており、桁の末端まで厳密に数学的な実数の1.になるかどうかは保証できないから。
if(trajectory[-1]>0):
固定していた場合、頻度変化データのうち固定する前まで(頻度が1よりも小さいもの)を取り出す。
trajectory[ trajectory<1 ]
は、trajectoryというlistデータのうち、1よりも小さいものという意味。
一般にnp.arrayデータの一部だけを取り出すときは
trajectory[ 0:100 ]
と、後ろの[]に範囲を指定する。範囲の代わりに条件を入れると、その条件にあったものだけを取り出してくれる。
trajectory[ trajectory<1 ].shape
で取り出したデータの縦横の次元を表示してくれる。実際のデータの個数はshape[0]で取り出すことができる。これが頻度1になる前までの頻度データの個数、ここでは世代ごとに頻度データを計算しているので世代数、に相当する。よって
if(trajectory[-1]>0):
fixed.append( trajectory[ trajectory<1 ].shape[0] )
は固定するまでの世代数をfixedに付け足す、という意味になる。
さて、頻度変化と固定するまでの時間の分布、消失までの時間の分布を図にしてみよう。いろいろと付加的な情報も付け加えてみました。
xmax=800
ymax=70
fig = plt.figure(figsize=(6,10),dpi=80)
ax1 = fig.add_subplot(3,1,1)
for i in range(10):
ax1.plot(sim[i])
ax1.set_xlim(0,50)
ax1.set_ylabel("Allele frequency")
ax2 = fig.add_subplot(3,1,2)
ax2.hist(fixed, bins=50, normed=True)
ax2.set_xlim(0,xmax)
ax2.set_ylim(0,.01)
ax2.set_ylabel("Count")
ax2.text(500, .0060, "Time until fixation")
ax2.annotate("average ({0:.1f} generations)".format(ave_fixation_time),
xy=(ave_fixation_time, .0030),
xytext=(ave_fixation_time, .0050),
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
ax3 = fig.add_subplot(3,1,3)
ax3.hist(lost, bins=50, normed=True)
ax3.set_xlim(0,xmax)
ax3.set_ylim(0,0.01)
ax3.set_xlabel("Generation")
ax3.set_ylabel("Count")
ax3.text(500,.0060, "Time until lost")
ax3.annotate("average ({0:.1f} generations)".format(ave_time_until_lost),
xy=(ave_time_until_lost, .0030),
xytext=(ave_time_until_lost, .0050),
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
fig.tight_layout()
#fig.savefig("Drift_simulation_1.eps")
plt.show()
集団サイズが減少した時
# a list of lists
sim = []
# set initial conditions
size_ans = 1000
derived_ans = size_ans*.5
howmany = 10
generations = 100
# simulation starts
t_start = time.time()
# repeat simulation 'howmany' times
for j in range(howmany):
# reset initial condition
derived = derived_ans
size = size_ans
tr = [derived/size]
# simulate frequency trajectory
for i in range(generations):
# population size change
if i==20:
# reduction
size = int(size/10)
derived = int(derived/10)
# growth
#size *= 10
#derived *= 10
# make next generation
derived = make_next_gen(derived, size)
# append freq
tr.append( derived/size )
# append trajectory
sim.append(tr)
# finished
t_end = time.time()
print( '{0:.3f} seconds'.format(t_end-t_start))
# plot
fig=plt.figure()
ax=fig.add_subplot(111)
for i in range(10):
ax.plot(sim[i])
ax.set_xlim(0,50)
plt.show()
ボトルネック
# a list of lists
sim = []
# set initial conditions
size_ans = 1000
derived_ans = size_ans*.5
howmany = 10
generations = 100
# simulation starts
t_start = time.time()
# repeat simulation 'howmany' times
for j in range(howmany):
# reset initial condition
derived = derived_ans
size = size_ans
tr = [derived/size]
# simulate frequency trajectory
for i in range(generations):
# population size change
if i==20:
# reduction
size = int(size/10)
derived = int(derived/10)
if i==40:
# growth
size *= 10
derived *= 10
# make next generation
derived = make_next_gen(derived, size)
# append freq
tr.append( derived/size )
# append trajectory
sim.append(tr)
# finished
t_end = time.time()
print( '{0:.3f} seconds'.format(t_end-t_start))
# plot
fig=plt.figure()
ax=fig.add_subplot(111)
for i in range(10):
ax.plot(sim[i])
ax.set_xlim(0,100)
plt.show()
© 2017 Kosuke Teshima