forked from trvrb/antigen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestGeometricSeqPhenotype.java
343 lines (292 loc) · 12.9 KB
/
TestGeometricSeqPhenotype.java
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
/**
* A class for testing the methods in GeometricSeqPhenotype.java
*
* @author Thien Tran
*/
public class TestGeometricSeqPhenotype {
// Private fields
private GeometricSeqPhenotype emptyPheno; // Default constructor
private GeometricSeqPhenotype simplePheno; // Give a specific sequence
private GeometricSeqPhenotype simplePheno2; // Give a specific sequence
GeometricSeqPhenotype[] history;
/**
* Define variables needed for multiple tests.
*/
@Before
public void setUp() {
// Use default static parameters from Parameters.
// Update startingSequence and homologousImmunity only.
Parameters.startingSequence = "TGCATC";
Parameters.homologousImmunity = 0.95;
emptyPheno = new GeometricSeqPhenotype();
simplePheno = new GeometricSeqPhenotype(0.0, 0.0, new char[] { 'T', 'G', 'C',
'A', 'T', 'C' });
simplePheno2 = new GeometricSeqPhenotype(20.0, 10.0, new char[] { 'G', 'C',
'T', 'G', 'G', 'A' });
// Create a small history of GeometricSeq phenotypes.
history = new GeometricSeqPhenotype[3];
history[0] = new GeometricSeqPhenotype(1.0, 2.0, new char[] { 'T', 'G', 'C',
'G', 'C', 'C' });
history[1] = new GeometricSeqPhenotype(3.0, 2.0, new char[] { 'T', 'G', 'C',
'G', 'C', 'T' });
history[2] = new GeometricSeqPhenotype(5.0, 8.0, new char[] { 'C', 'T', 'C',
'G', 'C', 'T' });
}
/**
* Test constructors for the SequencePhenotype class.
*/
@Test
public void testConstructors() {
// Check that empty constructor works and data contents are valid.
// Nucleotide sequence must be a multiple of 3.
assertEquals(0, emptyPheno.getSequence().length() % 3);
// Nucleotide sequence must be the same length as the startingSequence.
assertEquals(Parameters.startingSequence.length(),
emptyPheno.getSequence().length());
// Check that non-empty constructor works and data contents are equal to the
// given parameters.
assertEquals("GCTGGA", simplePheno2.getSequence());
assertEquals(20.0, simplePheno2.getTraitA(), 0.0);
assertEquals(10.0, simplePheno2.getTraitB(), 0.0);
}
/**
* Test getter method.
*/
@Test
public void testGetTraits() {
assertEquals(0.0, simplePheno.getTraitA(), 0);
assertEquals(0.0, simplePheno.getTraitB(), 0);
assertEquals(20.0, simplePheno2.getTraitA(), 0);
assertEquals(10.0, simplePheno2.getTraitB(), 0);
}
/**
* Test getter method.
*/
@Test
public void testGetSequence() {
assertEquals("TGCATC", simplePheno.getSequence());
}
/**
* Test distance method.
*/
@Test
public void testDistance() {
// Calculates the distance between this GeometricSeqPhenotype and p in Euclidean
// space.
// simplePheno: traitA=0.0, traitB=0.0
// history[0]: traitA=1.0, traitB=2.0
// double distA = (0.0 - 1.0)
// double distB = (0.0 - 2.0)
// double dist = (-1.0 * -1.0) + (-2.0 * -2.0) = 5.0
// dist = Math.sqrt(5.0) = 2.2360679775
assertEquals(2.236068, simplePheno.distance(history[0]), 0.000001);
assertEquals(3.605551, simplePheno.distance(history[1]), 0.000001);
assertEquals(9.433981, simplePheno.distance(history[2]), 0.000001);
// history[0]: traitA=1.0, traitB=2.0
// history[2]: traitA=5.0, traitB=8.0
// double distA = (1.0 - 5.0)
// double distB = (2.0 - 8.0)
// double dist = (-4.0 * -4.0) + (-6.0 * -6.0) = 52.0
// dist = Math.sqrt(52.0) = 2.7.21110255093
assertEquals(7.211103, history[2].distance(history[0]), 0.000001);
assertEquals(7.211103, history[0].distance(history[2]), 0.000001);
}
/**
* Test mutate function.
*/
@Test
public void testMutate() {
GeometricSeqPhenotype mutantPheno = (GeometricSeqPhenotype) simplePheno.mutate();
String simplePhenoSeq = simplePheno.getSequence();
String mutantPhenoSeq = mutantPheno.getSequence();
// Make sure that the Hamming distance between the mutated and un-mutated
// nucleotide sequences equals one.
assertEquals(simplePhenoSeq.length(), mutantPhenoSeq.length());
int counter = 0;
for (int i = 0; i < simplePhenoSeq.length(); i++) {
if (simplePhenoSeq.charAt(i) != mutantPhenoSeq.charAt(i)) {
counter++;
}
}
assertEquals(1, counter);
assertEquals("TGCATC", simplePhenoSeq);
assertNotEquals("TGCATC", mutantPhenoSeq);
// Mutation in first codon and changes protein sequence
String[] simpleWildTypeMutantAminoAcids1 = simplePheno.mutateHelper(0, 'A');
assertArrayEquals(new String[] { "C", "S" },
simpleWildTypeMutantAminoAcids1);
// Mutation in first codon and doesn't change protein sequence
String[] simple2WildTypeMutantAminoAcids1 = simplePheno2.mutateHelper(1,
'C');
assertArrayEquals(new String[] { "A", "A" },
simple2WildTypeMutantAminoAcids1);
// Mutation in second codon and changes protein sequence
String[] simple2WildTypeMutantAminoAcids2 = simplePheno2.mutateHelper(3,
'A');
assertArrayEquals(new String[] { "G", "R" },
simple2WildTypeMutantAminoAcids2);
// Mutation in second codon and doesn't change protein sequence
String[] simpleWildTypeMutantAminoAcids2 = simplePheno.mutateHelper(5, 'T');
assertArrayEquals(new String[] { "I", "I" },
simpleWildTypeMutantAminoAcids2);
}
/**
* Test riskOfInfection calculations.
*/
@Test
public void testRiskOfInfection() {
// closestDistance * Parameters.smithConversion = 2.236068 * 0.1 = 0.2236068
// minRisk = 1.0 - Parameters.homologousImmunity = 1.0 - 0.95 = 0.05
// risk = Math.max(minRisk, risk) = max(0.05, 0.2236068) = 0.2236068
// risk = Math.min(1.0, risk) = 0.2236068
assertEquals(0.2236068, simplePheno.riskOfInfection(history), 0.00001);
// closestDistance * Parameters.smithConversion = 15.132746 * 0.1 = 1.5132746
// minRisk = 1.0 - Parameters.homologousImmunity = 1.0 - 0.95 = 0.05
// risk = Math.max(minRisk, risk) = max(0.05, 1.5132746) = 1.5132746
// risk = Math.min(1.0, risk) = 1.0
assertEquals(1.0, simplePheno2.riskOfInfection(history), 0.0);
}
/**
* A redundant test for returning the sequence, but with toString().
*/
@Test
public void testToString() {
// This may change as the class develops (i.e., new enhancements).
assertEquals("TGCATC, 0.0000, 0.0000, 0, 0", simplePheno.toString());
// Check that String representation changes after mutation.
assertNotEquals("TGCATC, 0.0000, 0.0000, 0, 0",
simplePheno.mutate().toString());
// traitA and traitB are not deterministic.
// E should be 0 and nE should be 1
assertTrue(simplePheno.mutate().toString().contains(", 0, 1"));
}
/**
* Test that all 64 codons are accounted for and corresponds to the correct amino acid.
*/
@Test
public void testCodonMap() throws FileNotFoundException {
// Initialize static parameters to create CodonMap in Biology
Parameters.load();
Parameters.initialize();
// Create a new CodonMap from a codon table txt file,
// which will be used to validate CodonMap in Biology
Map<String, String> codonMapTest = new HashMap<>();
Scanner codonTable = new Scanner(new File("codon_table.txt"));
codonTable.nextLine();
while (codonTable.hasNextLine()) {
Scanner codonLine = new Scanner(codonTable.nextLine());
String codon = codonLine.next(); // codon
codonLine.next(); // throw out second column (amino acid abbreviation)
String aminoAcid = codonLine.next(); // amino acid codon codes for
// txt file saved from GitHub uses "O" instead of "STOP" to represent stop codons
if (aminoAcid.equals("O")) {
aminoAcid = "STOP";
}
codonMapTest.put(codon, aminoAcid);
}
assertEquals(codonMapTest, Biology.CodonMap.CODONS.codonMap);
}
/**
* Test that DMS data stored in Antigen reflects the csv file read from
* parameters.yml.
*/
@Test
public void testDMSData() {
// The number of rows in the DMS file is checked in Parameters.
// Check if each row (array) in the DMS data sums up to 1.0.
int numberOfAminoAcidSites = Parameters.startingSequence.length() / 3;
int numberOfAminoAcids = Biology.AlphabetType.AMINO_ACIDS.getValidCharacters().length();
// Cycle through each array, and sum up each element in the nexted array.
for (int i = 0; i < numberOfAminoAcidSites; i++) {
double currentSiteSum = 0.0;
for (int j = 0; j < numberOfAminoAcids; j++) {
currentSiteSum += Biology.DMSData.DMS_DATA.getAminoAcidPreference(i)[j];
}
assertEquals(1.0, currentSiteSum, 0.0001);
}
}
/**
* Creates a csv file for each amino acid site's matrix of vectors in a
* directory, test/valuesGammaDistribution.
*/
@Test
public void testGammaDistribution() throws IOException {
// Initialize static parameters to test different distributions
Parameters.load();
Parameters.initialize();
if (!Parameters.predefinedVectors) {
System.out.println("Parameters.predefinedVectors is false");
return;
}
new File("testGeometricSeqPhenotype/valuesGammaDistribution").mkdirs();
String[] values = Biology.SiteMutationVectors.VECTORS.getStringOutputCSV();
Map<Integer, Biology.MutationVector[][]> matrices = Biology.SiteMutationVectors.VECTORS.getMatrices();
for (int i = 0; i < values.length; i++) {
String value = values[i];
PrintStream output = new PrintStream(
"testGeometricSeqPhenotype/valuesGammaDistribution/0_site" + i + ".csv");
output.println(value);
Biology.MutationVector[][] matrix = matrices.get(i);
System.out.println("Matrix " + i);
for (int j = 0; j < Biology.AlphabetType.AMINO_ACIDS.getValidCharacters().length(); j++) {
for (int k = 0; k < Biology.AlphabetType.AMINO_ACIDS.getValidCharacters().length(); k++) {
if (j != k) {
Biology.MutationVector mutationVector = matrix[j][k];
System.out.print("[" + mutationVector.mutA + "," + mutationVector.mutB + "]");
} else {
// The diagonal represents synonymous mutations, so
// print [0.0, 0.0]
System.out.print("[" + 0.0 + "," + 0.0 + "]");
}
}
System.out.println();
}
}
// Run: python testGammaDistribution.py
// for each i such that "0_site" + i + ".csv"
}
/**
* PrintStream (codonMutations.csv) to print
* wild type and mutant nucleotide pairs to.
*/
public static PrintStream codonMutations;
static {
try {
if (GeometricSeqPhenotype.SANITY_TEST) {
codonMutations = new PrintStream("testGeometricSeqPhenotype/codonMutations.csv");
codonMutations.println("siteN,wildCodon,mutantCodon,pairWildMutantN,wildAA,mutantAA,cycle,id");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Run: python testTransitionTransversion.py
}
/**
* PrintStream (randomMutationsDistribution.csv) to print each vector movement
* calculated by the mutate function to when Parameters.predefinedVectors == false.
*/
public static PrintStream randomMutationsDistribution;
static {
try {
if (GeometricSeqPhenotype.SANITY_TEST && !Parameters.predefinedVectors) {
randomMutationsDistribution = new PrintStream("testGeometricSeqPhenotype/randomMutationsDistribution.csv");
randomMutationsDistribution.println("mutation,r,theta");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Run: python testGammaDistribution.py
}
}