| 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 |
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 |
|
| 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 |
|
| 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 |
|
| 80 |
|
| 81 |
int aminoAcid = geneticCode.getAminoAcidState(codonState); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
if (aminoAcid < 0 || aminoAcid >= stateSize) { |
| 87 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |