Debian Codegraph @

src/dr/evolution/wrightfisher/Population.java

raw symbols SCIP
1 /*
2 * Population.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evolution.wrightfisher;
27
28 import dr.math.MathUtils;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
33 public class Population {
34
35 // the sequences of this population
36 List<Genome> population = null;
37
38 double meanFitness;
39
40 // mutates the sequences
41 Mutator mutator = null;
42
43 // calculates the fitness of the sequences
44 FitnessFunction fitnessFunction = null;
45
46 public Population(int size, int sequenceLength, Mutator mutator, FitnessFunction fitnessFunction, boolean initializeToFittest) {
47 population = new ArrayList<Genome>();
48 for (int i = 0; i < size; i++) {
49 population.add(new SimpleGenome(sequenceLength, fitnessFunction, initializeToFittest));
50 }
51 this.mutator = mutator;
52 this.fitnessFunction = fitnessFunction;
53 cumulativeFitness = new double[size];
54 }
55
56 private Population(List<Genome> population, Mutator mutator, FitnessFunction fitnessFunction) {
57 this.population = population;
58 this.mutator = mutator;
59 this.fitnessFunction = fitnessFunction;
60 }
61
62 /**
63 * pick parents randomly but proportional to fitness.
64 */
65 public int pickParent() {
66
67 return MathUtils.randomChoice(cumulativeFitness);
68 }
69
70 public final Mutator getMutator() { return mutator; }
71
72 public final int getGenomeLength() {
73 return population.get(0).getGenomeLength();
74 }
75
76 public final SimpleGenome getGenome(int i) {
77 return (SimpleGenome)population.get(i);
78 }
79
80 /**
81 * This function returns the next generation!
82 */
83 public final Population nextGeneration() {
84
85 calculateCumulativeRelativeFitnesses();
86
87 int popSize = population.size();
88
89 List<Genome> nextPopulation = new ArrayList<Genome>();
90 for (int i = 0; i < popSize; i++) {
91 Genome parent = population.get(pickParent());
92 nextPopulation.add(parent.replicate(mutator, fitnessFunction));
93 }
94
95 Population p = new Population(nextPopulation, mutator, fitnessFunction);
96
97 return p;
98 }
99
100 //************************************************************************
101 // private methods
102 //************************************************************************
103
104 private void calculateCumulativeRelativeFitnesses() {
105
106 int popSize = population.size();
107
108 if (cumulativeFitness == null) {
109 cumulativeFitness = new double[popSize];
110 }
111
112 //System.err.println("popSize = " + popSize);
113 //System.err.println("cumulativeFitness.length = " + cumulativeFitness.length);
114
115
116 cumulativeFitness[0] = ((SimpleGenome)population.get(0)).fitness;
117
118 for (int i = 1; i < popSize; i++) {
119 cumulativeFitness[i] = cumulativeFitness[i-1] + ((SimpleGenome)population.get(i)).fitness;
120 }
121 double totalFitness = cumulativeFitness[popSize-1];
122 if (totalFitness == 0.0) throw new RuntimeException("Population crashed! No viable children.");
123
124 for (int i = 0; i < popSize; i++) {
125 cumulativeFitness[i] /= totalFitness;
126 }
127 meanFitness = totalFitness / popSize;
128 }
129
130
131 //************************************************************************
132 // static methods
133 //************************************************************************
134
135
136 public static Population forwardSimulation(Population startingPopulation, int generations) {
137 Population p = startingPopulation;
138 for (int i = 0; i < generations; i++) {
139 p = p.nextGeneration();
140 }
141 return p;
142 }
143
144 public int getAgeOfMRCA(double[] f) {
145
146 int popSize = population.size();
147
148 for (int i = 0; i < popSize; i++) {
149 getGenome(i).mark();
150 }
151
152 int age = 1;
153 Genome g = getGenome(0);
154 while (g != null && g.getMarks() < popSize) {
155 g = g.getParent();
156 age += 1;
157 }
158
159 for (int i = 0; i < popSize; i++) {
160 getGenome(i).unmark();
161 }
162
163 if (g != null) {
164 f[0] = g.getFitness();
165 return age;
166 } else {
167 f[0] = 0;
168 return -1;
169 }
170 }
171
172 public Genome getMRCA() {
173
174 int popSize = population.size();
175
176 for (int i = 0; i < popSize; i++) {
177 getGenome(i).mark();
178 }
179
180 Genome g = getGenome(0);
181 while (g != null && g.getMarks() < popSize) {
182 g = g.getParent();
183 }
184
185 for (int i = 0; i < popSize; i++) {
186 getGenome(i).unmark();
187 }
188
189 return g;
190 }
191
192 public void unfoldedSiteFrequencies(List<Double>[] siteFrequencies) {
193
194 final int popSize = population.size();
195
196 double[][] fitnessTable = fitnessFunction.getFitnessTable();
197
198 Genome progenitorGenome = getMRCA();
199 byte[] progenitor = progenitorGenome.getSequence();
200
201 int genomeLength = getGenome(0).getGenomeLength();
202
203 for (int j = 0; j < genomeLength; j++) {
204
205 byte wildType = progenitor[j];
206 double wildFitness = fitnessTable[j][wildType];
207 double mutantFitness = fitnessTable[j][(wildType+1)%2];
208 double relativeFitness = mutantFitness/wildFitness;
209
210 if (wildFitness == 0.0) {
211 for (int k = 0; k < genomeLength; k++) {
212 System.err.print(progenitor[k]);
213 }
214 System.err.println();
215 throw new RuntimeException("Progenitor fitness can not be zero!");
216 }
217
218 if (relativeFitness != 0.0) {
219 int mutantCount = 0;
220 for (int i = 0; i < popSize; i++) {
221 if (getGenome(i).sequence[j] != wildType) {
222 mutantCount += 1;
223 }
224 }
225 siteFrequencies[mutantCount].add(relativeFitness);
226 }
227 }
228 }
229
230 public double getMeanParentFitness() {
231 double parentFitness = 0.0;
232 int popSize = population.size();
233 for (int i = 0; i < popSize; i++) {
234 parentFitness += getGenome(i).getParent().getFitness();
235 }
236 return parentFitness/(double)popSize;
237 }
238
239 public double getProportionAsFit(double fit) {
240 int count = 0;
241 int popSize = population.size();
242 for (int i = 0; i < popSize; i++) {
243 if (getGenome(i).fitness >= fit) count += 1;
244 }
245 return (double)count/(double)popSize;
246 }
247
248 public void getTipHammingDistance(int individuals, List<Double>[] distances) {
249
250 int popSize = population.size();
251
252 for (int i = 0; i < individuals; i++) {
253 getGenome(i).mark();
254 }
255
256 for (int i = 0; i < individuals; i++) {
257 Genome tip = getGenome(i);
258 Genome a = tip;
259 int generation = 0;
260 while ((a.getMarks() < popSize) && (generation < distances.length)) {
261 distances[i].add((double) tip.hammingDistance(a));
262 generation += 1;
263 a = a.getParent();
264 }
265 }
266
267 for (int i = 0; i < individuals; i++) {
268 getGenome(i).unmark();
269 }
270 }
271
272 public void getMutationDensity(int individuals, List<Integer>[] mutations) {
273
274 for (int i = 0; i < individuals; i++) {
275 getGenome(i).mark();
276 }
277
278 for (int i = 0; i < individuals; i++) {
279 Genome genome = getGenome(i);
280 int generation = 0;
281 while ((genome.getMarks() < individuals) && (generation < mutations.length)) {
282 mutations[generation].add(genome.getMutations().length);
283 generation += 1;
284 genome = genome.getParent();
285 }
286 }
287
288 for (int i = 0; i < individuals; i++) {
289 getGenome(i).unmark();
290 }
291 }
292
293 public int getFirstActiveLineage() {
294 int popSize = population.size();
295 for (int i = 0; i < popSize; i++) {
296 if (getGenome(i).getMarks() > 0) return i;
297 }
298 return -1;
299 }
300
301
302 public int getActiveLineageCount() {
303 int count = 0;
304 int popSize = population.size();
305 for (int i = 0; i < popSize; i++) {
306 if (getGenome(i).getMarks() > 0) count += 1;
307 }
308 return count;
309 }
310
311 public int getActiveMutationsCount() {
312 int count = 0;
313 int popSize = population.size();
314 for (int i = 0; i < popSize; i++) {
315 if (getGenome(i).getMarks() > 0) count += getGenome(i).mutations.length;
316 }
317 return count;
318 }
319
320
321
322 static double[] cumulativeFitness = null;
323
324 }