Debian Codegraph @

src/dr/evolution/wrightfisher/SimpleGenome.java

raw symbols SCIP
1 /*
2 * SimpleGenome.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.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 //int codonState = codons.getState(sequence[i], sequence[i+1], sequence[i+2]);
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 }