Debian Codegraph @

src/dr/evolution/wrightfisher/CodonFitnessFunction.java

raw symbols SCIP
1 /*
2 * CodonFitnessFunction.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.evolution.datatype.AminoAcids;
29 import dr.evolution.datatype.Codons;
30 import dr.evolution.datatype.GeneticCode;
31 import dr.math.MathUtils;
32 import dr.math.distributions.GammaDistribution;
33
34 public class CodonFitnessFunction extends FitnessFunction {
35
36 /**
37 * selection coefficients are gamma distributed with parameters alpha and beta
38 */
39 public CodonFitnessFunction(int codonCount, double alpha, double beta, double pInv) {
40
41 GammaDistribution gamma = new GammaDistribution(beta, alpha);
42 stateSize = 20;
43
44 fitness = new double[codonCount][stateSize];
45 byte[] aaFittest = new byte[codonCount];
46 fittest = getFittestGenome(codonCount, aaFittest);
47 for (int i = 0; i < codonCount; i++) {
48
49 fitness[i][aaFittest[i]] = 1.0;
50 for (int j = 0; j < stateSize; j++) {
51
52 if (j != aaFittest[i]) {
53
54 if (MathUtils.nextDouble() < pInv) {
55 fitness[i][j] = 0.0;
56 } else {
57
58 double prob = Math.round(MathUtils.nextDouble() * 1000.0) / 1000.0;
59 while ((prob <= 0.0) || (prob >= 1.0)) {
60 prob = Math.round(MathUtils.nextDouble() * 1000.0) / 1000.0;
61 }
62 fitness[i][j] = Math.max(0.0, 1.0 - gamma.quantile(prob));
63 }
64 }
65 }
66 }
67 }
68
69 /**
70 * Assumes the sequence is represented as nucleotides
71 */
72 public final double getFitness(byte[] sequence) {
73
74 double totalFitness = 1.0;
75
76 int count = 0;
77 for (int i = 0; i < sequence.length; i += 3) {
78 int codonState = (sequence[i] * 16) + (sequence[i + 1] * 4) + sequence[i + 2];
79 //int codonState = codons.getState(sequence[i], sequence[i+1], sequence[i+2]);
80
81 int aminoAcid = geneticCode.getAminoAcidState(codonState);
82
83 //System.out.println(sequence[i] + "" + sequence[i+1] + "" + sequence[i+2] +"="+ AminoAcids.INSTANCE.getChar(aminoAcid));
84
85 // stop codons are lethal of course
86 if (aminoAcid < 0 || aminoAcid >= stateSize) {
87 //System.out.println("Stop codon found! " + aminoAcid);
88 return 0.0;
89 }
90 totalFitness *= fitness[count][aminoAcid];
91 if (totalFitness == 0.0) return 0.0;
92 count += 1;
93 }
94 return totalFitness;
95 }
96
97 /**
98 * @return the relative fitness increase of the new state at given position to the old state.
99 */
100 public double getFitnessFactor(int pos, byte newState, byte oldState) {
101 throw new RuntimeException();
102 }
103
104 public final double[][] getFitnessTable() {
105
106 for (int j = 0; j < fitness[0].length; j++) {
107 for (int i = 0; i < fitness.length; i++) {
108 System.out.print((Math.round(fitness[i][j] * 1000.0) / 1000.0) + "\t");
109 }
110 System.out.println();
111 }
112
113 return fitness;
114 }
115
116 public byte[] getFittestGenome(int codonCount, byte[] aa) {
117
118 byte[] genome = new byte[codonCount * 3];
119
120 for (int i = 0; i < codonCount; i++) {
121
122 int aminoAcid = stateSize;
123 int pos1 = MathUtils.nextInt(4);
124 int pos2 = MathUtils.nextInt(4);
125 int pos3 = MathUtils.nextInt(4);
126 int codonState = (pos1 * 16) + (pos2 * 4) + pos3;
127 //int codonState = codons.getState(pos1, pos2, pos3);
128 aminoAcid = geneticCode.getAminoAcidState(codonState);
129
130 while (aminoAcid >= stateSize) {
131 pos1 = MathUtils.nextInt(4);
132 pos2 = MathUtils.nextInt(4);
133 pos3 = MathUtils.nextInt(4);
134 codonState = (pos1 * 16) + (pos2 * 4) + pos3;
135 //codonState = codons.getState(pos1, pos2, pos3);
136 aminoAcid = geneticCode.getAminoAcidState(codonState);
137 }
138 System.out.print(AminoAcids.INSTANCE.getChar(aminoAcid));
139
140 genome[i * 3] = (byte) pos1;
141 genome[i * 3 + 1] = (byte) pos2;
142 genome[i * 3 + 2] = (byte) pos3;
143 aa[i] = (byte) aminoAcid;
144 }
145 System.out.println();
146
147 return genome;
148 }
149
150 public void initializeToFittest(byte[] genome) {
151 System.arraycopy(fittest, 0, genome, 0, fittest.length);
152 }
153
154 int stateSize = 20;
155 double[][] fitness;
156 byte[] fittest = null;
157 GeneticCode geneticCode = GeneticCode.UNIVERSAL;
158 Codons codons = Codons.UNIVERSAL;
159 }