/
Primer.java
284 lines (229 loc) · 9.9 KB
/
Primer.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
package ca.virology.baseByBase.gui.CodeHop;
import java.util.ArrayList;
import java.lang.Math;
/**
* CLASS: Primer
* <p>
* The constructor of this class contains the calculations for generating primers
* using the pre-generated consensus and core
*/
public class Primer {
boolean computeAvgTemp = false;
boolean computeConsensusClampTemp = CodeHopWizard.properTempCalc;
public int startNTPosInSeq; //inclusive
public int endNTPosInSeq; //exclusive
public int startNTPosInBlock; //inclusive
public int endNTPosInBlock; //exclusive
public String primerSeq;
public int degeneracy;
ArrayList<MatrixEntry>[] excludedAAList;
ArrayList<String> allPossibleCores;
ArrayList<Character>[] allPossibleNucs;
String clampSeq;
int clampScore;
double tempSlack = 5;
double requestedTemp;
public double temp;
public int blocknum;
String direction;
String primerName;
public Primer(DegenerateCore core, String entireConsensusSeq, int[] clampPositionScores, int blockAAStartPosition, int blocknum, String direction) {
if (direction.equals("forward")) {
this.direction = "forward";
primerName = core.aaSeq + "-F " + core.degeneracy + "x";
} else if (direction.equals("reverse")) {
this.direction = "reverse";
primerName = core.aaSeq + "-R " + core.degeneracy + "x";
}
primerSeq = "";
excludedAAList = core.excludedAA;
degeneracy = core.degeneracy;
allPossibleCores = new ArrayList();
clampSeq = "";
requestedTemp = CodeHopSelectPanel.getTemp();
this.blocknum = blocknum;
if (!computeConsensusClampTemp) {
requestedTemp = CodeHopSelectPanel.getUserDefinedConsensusClampLength();
}
temp = 0;
int currentPos = core.startNTPos - 1; //start looking at nucleotides 1 position before the core region
if (currentPos < 0) {//can probably remove this later when we change the startPos of getDegenerateCores so a core in the 0th position is never created
return;
}
//temporary - computes the average temp of all possible degenerate cores
if (computeAvgTemp) {
allPossibleNucs = getAllPossibleNucs(core.core);
getAllPossibleCores("", allPossibleNucs, 0);
temp = getAvgTemp();
}
//continue making the primer longer until the temperature has reached the correct value
while (temp < requestedTemp) {
clampSeq = entireConsensusSeq.charAt(currentPos) + clampSeq;
currentPos--;
if (computeAvgTemp) {
temp = getAvgTemp();
} else if (computeConsensusClampTemp) {
temp = Temp.getHyfiTM(clampSeq, Block.reverseAndComplement(clampSeq), CodeHopSelectPanel.getPrimerConcentration(), 1e-14, 0.05, 0.002);
} else { //mimicking temperature calculation to speedup displaying results
temp += 1;
}
// will need to change the second condition to allow the primer to be slightly under the requested temp and still work
//not enough nucleotides to reach desired temperature - no primer created
if (currentPos < 0 && temp < requestedTemp) {
if (temp < requestedTemp - tempSlack) {
return; //don't make a primer
} else {
break; //break out of loop and still make primer
}
}
}
currentPos++;
// if the temperature has gone above the requested temperature determine best temp to use (temp before or after going above requested temp)
if (temp > requestedTemp) {
double tempBefore = temp;
if (clampSeq.length() != 0) {
clampSeq = clampSeq.substring(1);
currentPos++;
}
//double tempAfter = getAvgTemp();
double tempAfter = Temp.getHyfiTM(clampSeq, Block.reverseAndComplement(clampSeq), CodeHopSelectPanel.getPrimerConcentration(), 1e-14, 0.05, 0.002);
if (Math.abs(tempBefore - requestedTemp) < Math.abs(tempAfter - requestedTemp)) {
currentPos--;
clampSeq = entireConsensusSeq.charAt(currentPos) + clampSeq;
} else {
temp = tempAfter;
}
}
primerSeq = clampSeq + core.core;
startNTPosInBlock = currentPos; //inclusive start position
endNTPosInBlock = startNTPosInBlock + primerSeq.length(); //exclusive end position
clampScore = getClampScore(clampPositionScores);
if (direction.equals("reverse")) {
adjustPositionsForDisplay();
}
startNTPosInSeq = startNTPosInBlock + blockAAStartPosition * 3;
endNTPosInSeq = startNTPosInSeq + primerSeq.length();
}
private int getClampScore(int[] clampPositionScores) {
int score = 0;
int end = startNTPosInBlock + clampSeq.length();
for (int i = startNTPosInBlock; i < end; i++) {
score += clampPositionScores[i];
}
return score / clampSeq.length();
}
public void adjustPositionsForDisplay() {
int lastNucPosInBlock = CodeHopWizard.blockList.get(blocknum - 1).matrix.blockAALen * 3 - 1;
startNTPosInBlock = lastNucPosInBlock - startNTPosInBlock - primerSeq.length() + 1;
endNTPosInBlock = startNTPosInBlock + primerSeq.length();
}
//=========================================================================
// Method: getAvgTemp()
//
// Returns the average temperature of all possible degenerate cores
//=========================================================================
public double getAvgTemp() {
double avgTemp = 0;
String primer;
for (String core : allPossibleCores) {
primer = clampSeq + core;
avgTemp += Temp.getHyfiTM(primer, Block.reverseAndComplement(primer), CodeHopSelectPanel.getPrimerConcentration(), 1e-14, 0.05, 0.002);
}
avgTemp /= allPossibleCores.size();
return avgTemp;
}
//=========================================================================
// Method: getAllPossibleCores(String, ArrayList<Character>[], int)
//
// Returns an arrayList of all possible degenerate cores
//=========================================================================
public void getAllPossibleCores(String core, ArrayList<Character>[] allPossibleNucs, int k) {
if (k == allPossibleNucs.length) {
allPossibleCores.add(core);
} else {
for (Character nuc : allPossibleNucs[k]) {
getAllPossibleCores(core + nuc, allPossibleNucs, k + 1);
}
}
}
//=========================================================================
// Method: getAllPossibleNucs(String)
//
// Returns an array of arraylists where each position in the array holds an
// arraylist that contains all possible nucleotides that can be in that
// position of the degenerate core
//=========================================================================
public ArrayList<Character>[] getAllPossibleNucs(String core) {
ArrayList<Character>[] allPossibleNucs = (ArrayList<Character>[]) new ArrayList[core.length()];
//initialize each matrix element as an empty ArrayList
for (int i = 0; i < allPossibleNucs.length; i++) {
allPossibleNucs[i] = new ArrayList();
}
for (int i = 0; i < core.length(); i++) {
switch (core.charAt(i)) {
case 'n':
allPossibleNucs[i].add('A');
allPossibleNucs[i].add('T');
allPossibleNucs[i].add('C');
allPossibleNucs[i].add('G');
break;
case 'b':
allPossibleNucs[i].add('C');
allPossibleNucs[i].add('G');
allPossibleNucs[i].add('T');
break;
case 'v':
allPossibleNucs[i].add('A');
allPossibleNucs[i].add('C');
allPossibleNucs[i].add('G');
break;
case 'h':
allPossibleNucs[i].add('A');
allPossibleNucs[i].add('C');
allPossibleNucs[i].add('T');
break;
case 'd':
allPossibleNucs[i].add('A');
allPossibleNucs[i].add('G');
allPossibleNucs[i].add('T');
break;
case 's':
allPossibleNucs[i].add('G');
allPossibleNucs[i].add('C');
break;
case 'y':
allPossibleNucs[i].add('C');
allPossibleNucs[i].add('T');
break;
case 'k':
allPossibleNucs[i].add('G');
allPossibleNucs[i].add('T');
break;
case 'm':
allPossibleNucs[i].add('A');
allPossibleNucs[i].add('C');
break;
case 'r':
allPossibleNucs[i].add('A');
allPossibleNucs[i].add('G');
break;
case 'w':
allPossibleNucs[i].add('A');
allPossibleNucs[i].add('T');
break;
case 'c':
allPossibleNucs[i].add('C');
break;
case 'g':
allPossibleNucs[i].add('G');
break;
case 't':
allPossibleNucs[i].add('T');
break;
case 'a':
allPossibleNucs[i].add('A');
}
}
return allPossibleNucs;
}
}