-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewick_builder.js
269 lines (218 loc) · 7.75 KB
/
newick_builder.js
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
let rasterLineWidth = 0.5;
let defaultTree = "(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5);";
let step2;
let step1;
const branch_distance = 35;
const treeLineWidth = 2;
drawRaster();
//spit en verwerk de newick input string
function getValues() {
//step 1: filtering outer brackets outer brackets
const regex = /(?<=\()(.*)(?=\))/gm;
const str1 = document.getElementById("newick_text_input").value;
step1 = regex.exec(str1)
console.log("step1 string:", step1);
//de eerste takken eruit halen
const str2 = step1[0];
step2 = str2.split(/,(?![^()]*\))/gm);
console.log("step 2 string:", step2)
}
//haal de input string op een reops de voldgende functies
function showTree(){
console.log('button pressed');
let x = document.getElementById("newick_text_input").value;
document.getElementById("test_text").innerHTML = x;
validateInput();
getValues();
clearCanvas();
drawRaster();
drawTree();
}
//teken de boomm
function drawTree(){
let treeArray = step2;
console.log("treeArray:" + treeArray);
//initialiseer het canvas
let c = document.getElementById("drawing_canvas");
let ctx = c.getContext("2d");
//cirkel op het beginpunt
ctx.beginPath();
ctx.moveTo(180, 200);
ctx.arc(180, 200, 3, 0, 2* Math.PI);
ctx.strokeStyle = "#000"
ctx.stroke();
let amount_branches_start = step2.length;
console.log("amount branches:", amount_branches_start);
let line_length = branch_distance * amount_branches_start;
let start_point = 200 - (line_length / 2);
console.log("starting point", start_point);
//eerste verticale lijn tekenen
ctx.beginPath();
ctx.moveTo(180, start_point);
ctx.lineTo(180, (start_point + line_length));
ctx.lineWidth = treeLineWidth;
ctx.strokeStyle = "#000";
ctx.stroke();
//horizontale lijnen tekenen
for (i = 0; i < treeArray.length; i++){
console.log("loop:" + i);
const nameFilterRegex = /(.*)(?=:)/gm
const lengthFilterRegex = /(?<=:)(.*)/gm
let lineStr = treeArray[i];
let lineName = (nameFilterRegex.exec(lineStr))[0];
let lineLength = (lengthFilterRegex.exec(lineStr))[0];
console.log("lineName:" + lineName);
console.log("lineLength:" + lineLength);
let lineLengthPx = (500*lineLength);
let lineHeightPx = (200+(i*branch_distance));
console.log("lineLengthPx:" + lineLengthPx);
console.log("lineHeightPx:" + lineHeightPx);
ctx.beginPath();
ctx.moveTo(180, lineHeightPx);
ctx.lineTo((180 + lineLengthPx), lineHeightPx);
ctx.font = "15px Arial";
ctx.fillText((lineName), (190 + lineLengthPx), (lineHeightPx + 7));
ctx.lineWidth = 1;
ctx.strokeStyle = "#000";
ctx.stroke();
}
}
//als je op de knop 'load default' klikt laadt hij de default tree in en laat hij hem zien
function loadDefault() {
document.getElementById("newick_text_input").value = defaultTree;
showTree();
}
//hetv tekenen van een raster met 20px tussen de lijnen
function drawRaster(){
let c = document.getElementById("drawing_canvas");
let ctx = c.getContext("2d");
for (i = 0; i < 1000; (i = i+20)){
ctx.beginPath();
ctx.moveTo(i, 0);
ctx.lineTo(i, 400);
ctx.lineWidth = rasterLineWidth;
ctx.strokeStyle = "#eee";
ctx.stroke();
}
for (i = 0; i < 1000; (i = i+20)){
ctx.beginPath();
ctx.moveTo(0, i);
ctx.lineTo(1000, i);
ctx.lineWidth = rasterLineWidth;
ctx.strokeStyle = "#eee";
ctx.stroke();
}
}
// een test cirkel een lijtjes tekenen
function drawTest(){
let c = document.getElementById("drawing_canvas");
let ctx = c.getContext("2d");
//cirkel op het beginpunt
ctx.beginPath();
ctx.moveTo(180, 200);
ctx.arc(180, 200, 3, 0, 2* Math.PI);
ctx.strokeStyle = "#000"
ctx.stroke();
//eerste lijn
ctx.beginPath();
ctx.moveTo(180, 200);
ctx.lineTo(680, 200);
ctx.lineWidth = 1;
ctx.strokeStyle = "#000";
ctx.stroke();
}
//als je in het input fiels op enter drukt klikt hij op de showtree knop
document.getElementById('newick_text_input').onkeypress = function(e){
if (!e) e = window.event;
var keyCode = e.keyCode || e.which;
if (keyCode == '13'){
// Enter pressed
showTree();
return false;
}
}
//kijkt od er iets ij\n de file input zien endals dat zo laadt hij het bestand als text
document.getElementById("input_file").addEventListener("change",function(){
let file = this.files[0];
if (file) {
let reader = new FileReader();
reader.onload = function (evt) {
console.log(evt);
document.getElementById("test_text").innerHTML = evt.target.result;
document.getElementById("newick_text_input").value = evt.target.result;
};
reader.onerror = function (evt) {
console.error("An error ocurred reading the file",evt);
};
reader.readAsText(file, "UTF-8");
let str = document.getElementById("newick_text_input").value;
}
},false);
function readNewick() {
let amount_branch = new RegExp(!'()');
console.log(amount_branch);
}
//valideer de user input of er daadwerkelijk kloppende newick staat en geef relevenate foutmeldeinge aan de gebruiker
function validateInput(){
console.log("Validating input...");
let newick = document.getElementById("newick_text_input").value;
let validationErrors = 0;
document.getElementById("error_message").innerHTML = "";
linebreak = document.createElement("br");
//Checking if there is something in the input
if (newick == ""){
validationErrors++;
document.getElementById("error_message").innerHTML = "INVALID INPUT; newick input is empty!";
error_message.appendChild(linebreak);
console.log("%cINVALID INPUT; newick input is empty!", 'color: red;')
}
//Checking start with (
if (newick[0] != "("){
validationErrors++;
document.getElementById("error_message").innerHTML += "INVALID INPUT; newick input does not start with '('!";
error_message.appendChild(linebreak);
console.log("%cINVALID INPUT; newick input does not start with '('!", 'color: red;')
}
//Checking end with );
if (((newick[newick.length -1]) != ";" )&& ((newick[newick.length -2]) != ")")){
validationErrors++;
document.getElementById("error_message").innerHTML += "INVALID INPUT; newick input does not end with ');'!";
error_message.appendChild(linebreak);
console.log("%cINVALID INPUT; newick input does not end with ');'!", 'color: red;')
}
//Checking opening/closing brackets
let openBracePlace = [];
let closeBracePlace = [];
for (let i = 0; i < newick.length; i++) {
if((newick.charAt(i)) == "(") {
openBracePlace = openBracePlace.concat([i]);
}
if((newick.charAt(i)) == ")") {
closeBracePlace = closeBracePlace.concat([i]);
}
}
if (openBracePlace.length != closeBracePlace.length){
validationErrors++;
document.getElementById("error_message").innerHTML += "INVALID INPUT; amount of opening brackets does not equal amount of closing brackets in newick input!";
error_message.appendChild(linebreak);
console.log("%cINVALID INPUT; amount of opening brackets does not equal amount of closing brackets in newick input!", 'color: red;')
}
if (validationErrors > 0){
document.getElementById("newick_text_input").style = "border: 1px solid red;";
document.getElementById("error_message").style = "color: red;";
} else if (validationErrors == 0){
console.log("No validation errors found.")
document.getElementById("newick_text_input").style = "border: 1px solid green;";
document.getElementById("error_message").style = "color: black;";
}
}
//leeg het input field
function clearInput(){
document.getElementById("newick_text_input").value = "";
clearCanvas();
}
function clearCanvas(){
let c = document.getElementById("drawing_canvas");
let ctx = c.getContext("2d");
ctx.clearRect(0, 0, c.width, c.height);
}