| 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.GeneticCode; |
| 29 |
|
| 30 |
public class SimpleGenome implements Genome { |
| 31 |
|
| 32 |
public SimpleGenome(int size, FitnessFunction fitnessFunction, boolean initializeToFittest) { |
| 33 |
this.sequence = new byte[size]; |
| 34 |
if (initializeToFittest) fitnessFunction.initializeToFittest(sequence); |
| 35 |
this.fitness = 1.0; |
| 36 |
this.parent = null; |
| 37 |
} |
| 38 |
|
| 39 |
private SimpleGenome(byte[] sequence, double fitness, Genome parent, Mutation[] mutations) { |
| 40 |
this.sequence = sequence; |
| 41 |
this.fitness = fitness; |
| 42 |
this.parent = parent; |
| 43 |
this.mutations = mutations; |
| 44 |
} |
| 45 |
|
| 46 |
public Genome replicate(Mutator mutator, FitnessFunction fitnessFunction) { |
| 47 |
|
| 48 |
byte[] childSequence = new byte[sequence.length]; |
| 49 |
|
| 50 |
Mutation[] mutations = mutator.mutate(sequence, childSequence); |
| 51 |
|
| 52 |
double newFitness = fitnessFunction.getFitness(childSequence); |
| 53 |
|
| 54 |
return new SimpleGenome(childSequence, newFitness, this, mutations); |
| 55 |
} |
| 56 |
|
| 57 |
public byte[] getSequence() { |
| 58 |
return sequence; |
| 59 |
} |
| 60 |
|
| 61 |
public int hammingDistance(Genome other) { |
| 62 |
|
| 63 |
int distance = 0; |
| 64 |
byte[] otherSequence = other.getSequence(); |
| 65 |
for (int i = 0; i < sequence.length; i++) { |
| 66 |
if (sequence[i] != otherSequence[i]) distance += 1; |
| 67 |
} |
| 68 |
return distance; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
public String getDNASequenceString() { |
| 73 |
StringBuffer buffer = new StringBuffer(); |
| 74 |
for (int i = 0; i < sequence.length; i++) { |
| 75 |
switch (sequence[i]) { |
| 76 |
case 0: buffer.append('A'); break; |
| 77 |
case 1: buffer.append('C'); break; |
| 78 |
case 2: buffer.append('G'); break; |
| 79 |
case 3: buffer.append('T'); break; |
| 80 |
default: buffer.append('N'); break; |
| 81 |
} |
| 82 |
} |
| 83 |
return buffer.toString(); |
| 84 |
} |
| 85 |
|
| 86 |
public String getAminoAcidSequenceString() { |
| 87 |
StringBuffer buffer = new StringBuffer(); |
| 88 |
for (int i = 0; i < sequence.length; i+=3) { |
| 89 |
int codonState = (sequence[i] * 16) + (sequence[i+1] * 4) + sequence[i+2]; |
| 90 |
|
| 91 |
|
| 92 |
char aminoAcid = GeneticCode.UNIVERSAL.getAminoAcidChar(codonState); |
| 93 |
|
| 94 |
buffer.append(aminoAcid); |
| 95 |
} |
| 96 |
return buffer.toString(); |
| 97 |
} |
| 98 |
|
| 99 |
public final void mark() { |
| 100 |
marks += 1; |
| 101 |
if (parent != null) parent.mark(); |
| 102 |
} |
| 103 |
|
| 104 |
public final void unmark() { |
| 105 |
marks = 0; |
| 106 |
if (parent != null) parent.unmark(); |
| 107 |
} |
| 108 |
|
| 109 |
public final int getMarks() { return marks; } |
| 110 |
|
| 111 |
public final Genome getParent() { return parent; } |
| 112 |
|
| 113 |
public final double getFitness() { return fitness; } |
| 114 |
public final Mutation[] getMutations() { return mutations; } |
| 115 |
|
| 116 |
public final int getGenomeLength() { return sequence.length; } |
| 117 |
|
| 118 |
byte[] sequence; |
| 119 |
double fitness; |
| 120 |
Mutation[] mutations = null; |
| 121 |
Genome parent; |
| 122 |
int marks = 0; |
| 123 |
} |