-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulate_stabilizing_qt.py
More file actions
275 lines (202 loc) · 7.07 KB
/
simulate_stabilizing_qt.py
File metadata and controls
275 lines (202 loc) · 7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import argparse
import random
from random import choice
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm
class effectSizes:
@classmethod
def effectSizeList(cls,a,d):
effectList={}
for i in range(0, len(a)):
effectList[i]={"2":a[i],"1":d,"0":-a[i]}
return effectList
class BaseHaplotypes:
def __init__(self, ne):
self.ne=ne
if self.ne<0:
raise ValueError("Number of diploid individuals should be larger than 0")
@classmethod
def make_basepop(self, popsize, counts):
basepop=[]
for l in counts:
basepop.append(random.sample([1 for i in range(0,l)] + [0 for i in range(l,popsize)], popsize))
diploids=make_diploids(basepop,popsize/2)
return diploids
class make_diploids:
def __init__(self,haplotypes,ne):
self.haplotypes=haplotypes
self.ne=ne
def convert_to_diploids(self):
diploid_basepop=[]
for locus in range(0, len(self.haplotypes)):
x=self.haplotypes[locus][::2]
y=self.haplotypes[locus][1::2]
diploid_basepop.append(list(zip(x,y)))
diploid_basepop=[list(list(zip(*diploid_basepop))[i]) for i in range(0,int(self.ne))]
return(diploid_basepop)
class individuals:
def __init__(self,genotype,phenotype, fitness):
self.genotype=genotype
self.phenotype=phenotype
self.fitness=fitness
def geno(self):
return self.genotype
def pheno(self):
return self.phenotype
def fit(self):
return self.fitness
def gametes(self):
gams=[]
for i in self.genotype:
gams.append(random.choice(i))
return(gams)
class PopInfo:
def __init__(self, population,effects,qff):
self.population=population
self.effects=effects
self.qff=qff
def popFunc(self):
mypop=[]
for ind in self.population.convert_to_diploids():
phenotype=getPhenotype(ind,self.effects).estimation()
fitness=getFitness(phenotype,self.qff).estimation()
ind=individuals(ind, phenotype,fitness)
mypop.append(ind)
return(mypop)
class getPhenotype:
def __init__(self,genotype,effect):
self.genotype=genotype
self.effects=effects
if len(self.genotype)!=len(self.effects):
raise ValueError("Inconsistent number of selected loci and effect sizes")
def estimation(self):
ph=0
for locus in range(0, len(self.genotype)):
ph+=(self.effects[locus][str(sum(self.genotype[locus]))])
return(ph)
class getFitness:
def __init__(self, phenotype,qff):
self.phenotype=phenotype
self.minFit=qff[0]
self.maxFit=qff[1]
self.mean=qff[2]
self.sd=qff[3]
def estimation(self):
scl=norm.pdf(self.mean, loc=self.mean, scale=self.sd)
fitness=norm.pdf(self.phenotype, loc=self.mean, scale=self.sd)
diff=self.maxFit-self.minFit
sc_fitness=(fitness*diff/scl) +self.minFit
return sc_fitness
class Selection:
def __init__(self, pop,effects,qff):
self.pop=pop
self.popsize=len(self.pop)
self.effects=effects
self.qff=qff
def fitnessTuple(self):
ft=[]
evoPop=[]
ftsum=0
for i in self.pop:
ftsum+=i.fit()
ft.append((ftsum,i))
for i in range(0,self.popsize):
r1=random.uniform(0,ftsum)
r2=random.uniform(0,ftsum)
ind1,f1=binarySearch(ft,r1)
ind2,f2=binarySearch(ft,r2)
g1=ind1.gametes()
g2=ind2.gametes()
genotype=list(zip(g1,g2))
phenotype=getPhenotype(genotype,self.effects).estimation()
fitness=getFitness(phenotype,self.qff).estimation()
ind=individuals(genotype, phenotype,fitness)
evoPop.append(ind)
return(evoPop)
def binarySearch(fitTuple,rand,lo=0, hi=None):
if hi==None:
hi=len(fitTuple)
while lo<hi:
mid=int((lo+hi)/2)
midval=fitTuple[mid][0]
if midval<rand:
lo=mid+1
elif(midval>rand):
hi=mid
else:
return (fitTuple[mid+1][1],mid+1)
return (fitTuple[lo][1],lo)
def getPopPheno(population):
meanPheno=0
phenoList=[]
for pop in population:
meanPheno=0
for ind in pop[2]:
meanPheno+=ind.pheno()
avgPheno=meanPheno/len(pop[2])
phenoList.append(avgPheno)
return(phenoList)
def plotPhenotypes(populations,qff):
mean=qff[2]
box_plot_data=[]
phenos=getPopPheno(populations)
if args.gen<=20:
for i in range(0,args.gen+1):
# box_plot_data.append(phenos[((args.gen+1)*i):(args.gen+((args.gen+1)*i))+1])
box_plot_data.append(phenos[i::args.gen+1])
# print(len(box_plot_data), len(list(map(str,range(0,args.gen)))))
plt.boxplot(box_plot_data,labels=list(map(str,range(0,args.gen+1))))
plt.axhline(y=mean, linewidth=1,linestyle='--', color = 'black')
plt.show()
elif args.gen>20 and args.gen <=100:
for i in range(0,args.gen+1,5):
box_plot_data.append(phenos[i::args.gen+1])
plt.boxplot(box_plot_data,labels=list(map(str,range(0,args.gen+1,5))) )
plt.axhline(y=mean, linewidth=1,linestyle='--', color = 'black')
plt.xticks(rotation=90)
plt.show()
else:
for i in range(0,args.gen+1,10):
box_plot_data.append(phenos[i::args.gen+1])
plt.boxplot(box_plot_data,labels=list(map(str,range(0,args.gen+1,10))) )
plt.axhline(y=mean, linewidth=1,linestyle='--', color = 'black')
plt.xticks(rotation=90)
plt.show()
parser=argparse.ArgumentParser(description= """
Description
-----------
Python script that simulates the evolutionary trajectory of a quantitative trait under stabilizing selection
(for diploids).
Authors
-----------
Vlachos Christos""",formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("-Ne",type=int, required=True,dest="ne",default=None, help="Population Size")
parser.add_argument("-a",type=str, required=True,dest="a",default=None, help="List of effect sizes for every QTL")
parser.add_argument("-p",type=str, required=True,dest="p",default=None, help="Initial Allele Frequency of every QTL")
parser.add_argument("-d",type=float, required=True,dest="d",default=None, help="Dominance effect")
parser.add_argument("-qff",type=str, required=True,dest="qff",default=None, help="Quantitative fitness function in the form: minFitness:maxFitness:mean:sd")
parser.add_argument("--replicates",type=int, required=True,dest="repl",default=None, help="Number of replicates")
parser.add_argument("--generations",type=int, required=True,dest="gen",default=None, help="Number of generations")
#parser.add_argument("--drift-distribution", required=False,dest="distr",default=False, help="If True returns the distribution of allele frequencies in the last generation (s must be 0)")
args = parser.parse_args()
popsize=args.ne*2
pcounts= list(map(int, [i*popsize for i in list(map(float,args.p.split(',')))]))
a=list(map(float, args.a.split(',')))
d=args.d
qff=list(map(float, list(args.qff.split(':'))))
effects=effectSizes.effectSizeList(a,d)
allpops=[]
print(effects)
for rep in range(1, int(args.repl)+1):
basepop=BaseHaplotypes.make_basepop(popsize,pcounts)
population=PopInfo(basepop,effects,qff).popFunc()
allpops.append((rep,0,population))
for g in range(1,int(args.gen)+1):
print("Simulating generation {0} of replicate {1}".format(g,rep))
population=(Selection(population,effects,qff).fitnessTuple())
allpops.append((rep,g,population))
print("\n")
#print(allpops)
plotPhenotypes(allpops,qff)
#pop.selected_pop