-
Notifications
You must be signed in to change notification settings - Fork 0
/
treeBuilder.java
304 lines (236 loc) · 8.49 KB
/
treeBuilder.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
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.BufferedWriter;
import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.ThreadLocalRandom;
import java.util.HashMap;
import java.util.Map;
public class treeBuilder {
private RnaNode<String> root; //bases of the tree, also contains the original mutation
private Mutation mutator; //mutator being used by the class
public Checker check; //Checker used by the entire class, public for HashMap access in startTester; can be returned to private and replaced with method
private BufferedWriter output; //Final output writer (for run details)
private Constants settings; //Settings file that holds all the information for the current run
private boolean endRun; //stops the loop when first compensating change is found when setting is selected
private static int cellCount = 0; //counts the number of nodes that have been added to the tree
public int cellCountGet() {
return this.cellCount;
}
public treeBuilder(Constants constant, BufferedWriter finalFile, HashMap <RnaNode, Integer> CheckMap) throws NullPointerException, IOException {
settings = constant;
output = finalFile;
endRun = false; //default, (I believe this makes it such that it stops at first complementary mutation, if found)
//Checker with all the keys
this.check = new Checker(settings);
//Mutator with correct sequences
mutator = new Mutation(settings.length());
if (settings.type()) {
//creates root of the tree, containing the first mutation and saves mutation data
root = mutator.createNextNode();
//checks original mutation is base paired, if not picks a new position
int spot = root.getPosition();
while (settings.positionV().get(spot) == 0) {
spot = ThreadLocalRandom.current().nextInt(1, settings.length());
}
//checks if original position was actually mutated
String nuc = root.getData();
root.setData(nuc, spot);
while (check.simpleMutation(root)) {
String temp = "#GUAC";
int count = ThreadLocalRandom.current().nextInt(1, temp.length());
nuc = "" + temp.charAt(count);
root.setData(nuc, spot);
}
root.setData(nuc, spot);
}
else {
root = settings.starterNode();
}
int counter = 1;
//Counts amount of changes in the sequence (including root)
for (int i = 0; i < settings.positionV().size(); i++) {
int pos2 = settings.positionV().get(i);
String bp1 = settings.sequenceV().get(i);
String bp2 = settings.sequenceV().get(pos2);
if (pos2 != 0) {
if (!check.LocCheck(bp1, bp2)) {
counter++;
}
}
}
root.sumChanges(counter);
//stores the position where the correction mutation pair is
if (CheckMap.size() == 0) {
settings.setCorrectingPosition(root.getPosition());
settings.setoriginalMutationNucleotide(root.getData());
}
else {
if (!CheckMap.containsValue(1)) {
settings.setCorrectingPosition(root.getPosition());
settings.setoriginalMutationNucleotide(root.getData());
}
else {
for (Map.Entry<RnaNode, Integer> entry : CheckMap.entrySet()) { //iterate through HashMap until find mutation that has yet to be compensated for
if (entry.getValue() == 1) { //if the current noncanonical has yet to be compensated for
settings.setCorrectingPosition(entry.getKey().getPosition());
settings.setoriginalMutationNucleotide(entry.getKey().getData());
CheckMap.put(entry.getKey(), 0);
}
break;
}
}
}
createLeftChild(root);
createRightChild(root);
if (settings.details()) {
this.writeNode(root);
}
}
public RnaNode<String> buildTree() throws FileNotFoundException, IOException{
//list of all the nodes in the current level of the tree
ArrayList<RnaNode<String>> currentLevel = new ArrayList<>();
currentLevel.add(root.getLeft());
currentLevel.add(root.getRight());
//Holders for node that is currently being worked on
RnaNode<String> currentNode = new RnaNode<String>();
RnaNode<String> finalNode = new RnaNode<String>();
int level = 0; //spots the current generation
while(currentLevel.size() > 0 && level < settings.maxLevel()){
ArrayList<RnaNode<String>> nextLevel = new ArrayList<RnaNode<String>>(); //holder for viable nodes in the next level
int i = 0;
while(i < currentLevel.size() && !endRun){
currentNode = currentLevel.get(i);
//if children are possible make left and right child
if(childrenPossible(currentNode)){
createLeftChild(currentNode);
this.cellCount++;
createRightChild(currentNode);
this.cellCount++;
//holds the identity of the compensation node
if(endRun){
finalNode = currentNode;
}
else{
nextLevel.add(currentNode.getLeft());
nextLevel.add(currentNode.getRight());
}
}
i++;
}
//prints all details for current level
if(settings.details()){
for(int x = 0; x < currentLevel.size(); x++){
this.writeNode(currentLevel.get(x));
}
}
currentLevel = nextLevel;
level++;
if(settings.endRun() && endRun){
currentLevel = new ArrayList<>();
}
}
if(finalNode.isFound()){
return finalNode;
}else{
return currentNode;
}
}
//creates left child for the current node
private void createLeftChild(RnaNode<String> current){
RnaNode<String> nextNode = mutator.createNextNode();
nextNode.setParent(current);
//saves mutation data for checkers
current.setLeft(nextNode);
}
//Create right child for the current node
private void createRightChild(RnaNode<String> current){
RnaNode<String> nextNode = mutator.createNextNode();
nextNode.setParent(current);
//saves mutation data for checkers
current.setRight(nextNode);
}
//determines whether or not further children can be created in the lineage
//return false if no more children possible/necessary
private boolean childrenPossible(RnaNode<String> current) throws FileNotFoundException, IOException{
//Checks if the current change has happened previously in the tree
RnaNode<String> temp = current.getParent();
int position = current.getPosition();
while(temp.hasParent()){
if(temp.getPosition() == position){
temp.setLastChange(false);//sets all previous positions to not last change at position in lineage
}
temp = temp.getParent();
}
//Checks if correcting mutation is found
if(check.foundCorrection(current, current.getData(), current.getPosition())){
current.found();
//current.changed(); //erroneously delegates the compensatory change as noncanonical
current.sumChanges(current.getParent().changes() - 1);
endRun = true;
return false;
}
//Check if lineage is fit to continue
else{
int count = root.changes(); //amount of mutations contained in current lineage
//Counts up how many mismatches are in lineage so far
temp = current.getParent(); //temporary holder while scanning through tree
while(temp.hasParent()){
if(temp.isChange() && temp.lastChange()){
if(settings.modelType() == 3){
if(temp.sbp()){
count++;
}else{
count++;
}
}else{
count++;
}
}
temp = temp.getParent();
}
//Model 1
if(settings.modelType() == 1){
//only increments count if a mismatch is created
if(!check.simpleMutation(current)){
count++;
}
}
//Model 2
else if(settings.modelType() == 2){
//only increments count if a mismatch is created
if(!check.complexMutation(current)){
count++;
}
}
//Model 3
else{
//only increments count if a mismatch is created
if(!check.unpairedCheck(current)){
if(current.sbp()){
count++;
}else{
count++;
}
}
}
if(count < settings.fitness()){
current.sumChanges(count);
return true;
}
else{
current.sumChanges(count);
current.died();
return false;
}
}
}
private void writeNode(RnaNode<String> current) throws IOException{
output.write(current.distanceFromRoot(current)+" "+ current.getData()+" "+current.getPosition()+" "+current.dead()+" "+current.isChange()+" "+current.isFound()
+" "+current.sbp()+" "+current.changes());
output.newLine();
}
}