-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUPGMA.java
More file actions
194 lines (152 loc) · 6.03 KB
/
UPGMA.java
File metadata and controls
194 lines (152 loc) · 6.03 KB
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
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class UPGMA {
// Final product string
private String tree = "";
// Holds sequences
private ArrayList<Sequence> sequences;
// Holds distances
private ArrayList<ArrayList<Double>> distances;
// Variables for finding smallest distance
private double smallestDistance;
private int smallestI;
private int smallestJ;
// Store if there are multiple trees
boolean multipleTrees = false;
public UPGMA(ArrayList<Sequence> sequences, ArrayList<ArrayList<Double>> distances) {
this.sequences = sequences;
this.distances = distances;
}
// Find smallest distance and sets the related variables
public void smallestDistance() {
// Set values
this.smallestDistance = Double.MAX_VALUE;
this.smallestI = -1;
this.smallestJ = -1;
// Smallest counter
int smallestCounter = 0;
// Find smallest distance
for (int i = 0; i < distances.size(); i++) {
for (int j = 0; j < distances.get(i).size(); j++) {
if (distances.get(i).get(j) < smallestDistance && distances.get(i).get(j) != 0) {
smallestDistance = distances.get(i).get(j);
smallestI = i;
smallestJ = j;
}
}
}
// Check for multiple smallests
for (int i = 0; i < distances.size(); i++) {
for (int j = 0; j < distances.get(i).size(); j++) {
if (distances.get(i).get(j) == smallestDistance) {
smallestCounter++;
}
}
}
// There is more than one on the counter
if(smallestCounter > 1){
multipleTrees = true;
}
}
// Join two sequences when they are determined the smallest distance
public void joinSequence() {
// Remove sequence at j and then set sequence at i to be new joined sequence
sequences.set(smallestI, new Sequence(sequences.get(smallestI).getName() + "" + sequences.get(smallestJ).getName().replaceAll("[S]", ""), null));
sequences.remove(smallestJ);
}
// Updates distance table
public void updateTable() {
// Reconstruct row
ArrayList<Double> row = new ArrayList<>();
for (int i = 0; i < smallestI; i++) {
row.add(((double) distances.get(smallestI).get(i) + distances.get(smallestJ).get(i)) / 2);
}
distances.get(smallestI).clear();
distances.get(smallestI).addAll(row);
// Keep reconstructing
for (int i = smallestI + 1; i < smallestJ; i++) {
distances.get(i).set(smallestI, (((double) distances.get(i).get(smallestI) + distances.get(smallestJ).get(i)) / 2));
}
// And reconstruct some more
for (int i = smallestJ + 1; i < distances.size(); i++) {
distances.get(i).set(smallestI, (((double) distances.get(i).get(smallestI) + distances.get(i).get(smallestJ)) / 2));
distances.get(i).remove(smallestJ);
}
// Redundant row
distances.remove(smallestJ);
}
// UPGMA algorithm
public void UPGMA() {
// Changes format
boolean firstRun = true;
// Store distance for tree
double distance = 0;
// Go until all sequences used
while (sequences.size() > 1) {
// Find smallest distance
smallestDistance();
// If j is smaller need to swap i and j
if (smallestJ < smallestI) {
int temp = smallestJ;
smallestJ = smallestI;
smallestI = temp;
}
// Special first run format
if(firstRun) {
tree = "(" + sequences.get(smallestI).getName() + ":" + (String.format("%.1f", distances.get(smallestJ).get(smallestI) / 2)) + ")" + "(" + sequences.get(smallestJ).getName() + ":" + (String.format("%.1f", distances.get(smallestJ).get(smallestI) / 2)) + ")";
// Not first run
} else {
tree = "(" + sequences.get(smallestI).getName() + ":" + (String.format("%.1f", (distances.get(smallestJ).get(smallestI) / 2 - distance))) + tree + ")" + "(" + sequences.get(smallestJ).getName() + ":" + (String.format("%.1f", distances.get(smallestJ).get(smallestI) / 2 ))+ ")" ;
}
// Set distance
distance = distances.get(smallestJ).get(smallestI) / 2;
// False after this no matter what
firstRun = false;
// Join two sequences
joinSequence();
// Update distances
updateTable();
}
/**
* FILE OUTPUT PART B
*/
// Try writer
try{
// Create writer
BufferedWriter writer2 = new BufferedWriter(new FileWriter("3.o2"));
// Write the tree to the file
writer2.write(sequences.get(0).getName());
writer2.write(tree);
// Close writer
writer2.close();
}catch (IOException e){
System.out.printf("IOException error writing to file 3.o2");
}
/**
* END PART B
*/
/**
* FILE OUTPUT PART C
*/
// Try writer
try{
// Create writer
BufferedWriter writer3 = new BufferedWriter(new FileWriter("3.o3"));
// If multiple trees write yes if not write no
if (multipleTrees){
writer3.write("YES");
}else{
writer3.write("NO");
}
// Close writer
writer3.close();
} catch (IOException e){
System.out.println("IOException error writing to file 3.o3");
}
/**
* END PART C
*/
}
}