phyloio 2.1.1 → 2.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phyloio",
3
- "version": "2.1.1",
3
+ "version": "2.2.2",
4
4
  "description": "JS library for visualising and comparing phylogenetic trees",
5
5
  "scripts": {
6
6
  "test": "npm run-script build-jest; jest --silent=false",
@@ -0,0 +1,297 @@
1
+ /**
2
+ * @jest-environment jsdom
3
+ */
4
+
5
+
6
+ const PhyloIO = require("./dist-jest/phylo.js").PhyloIO;
7
+ const utils = require('./src/utils.js')
8
+
9
+ var data = '((C,D)1,(A,(B,X)3)2,E);'
10
+
11
+ // get string made of concatenation of leaves name from node.leaves
12
+ const getSortedLeaves = (node) => {
13
+ return node.leaves.map(leaf => leaf.name).sort();
14
+ }
15
+
16
+ const verify_boostrap_default = (node, children) => {
17
+
18
+ var con_leaves = getSortedLeaves(node)
19
+
20
+ if ( con_leaves.length <= 1) {
21
+ return;
22
+ }
23
+
24
+ var str_leaves = JSON.stringify(con_leaves);
25
+
26
+ switch (str_leaves){
27
+ case JSON.stringify(['A', 'B', 'X']):
28
+ expect(node.extended_informations.Data).toBe("2");
29
+ break;
30
+ case JSON.stringify(['B', 'X']):
31
+ expect(node.extended_informations.Data).toBe("3");
32
+ break;
33
+ case JSON.stringify(['C', 'D']):
34
+ expect(node.extended_informations.Data).toBe("1");
35
+ break;
36
+ case JSON.stringify(['A', 'B', 'C', 'D', 'E', 'X']):
37
+ expect(node.extended_informations.Data).toBeUndefined();
38
+ break;
39
+ default:
40
+ expect(true).toBe(false);
41
+ break
42
+ }
43
+ }
44
+
45
+ test('test reroot on X' , () => {
46
+
47
+ const phylo = PhyloIO.init();
48
+
49
+ // >>> test with internal label for branches
50
+ var m1 = phylo._create_model(data, {'data_type' : 'nhx'})
51
+ m1.settings.edge_related_data.push('Data');
52
+ m1.traverse(m1.data,verify_boostrap_default);
53
+
54
+ // reroot on X
55
+ var x_node = m1.data.leaves.find(leaf => leaf.name === 'X');
56
+ m1.reroot(x_node);
57
+ m1.traverse(m1.data,function(node, children) {
58
+
59
+ var con_leaves = getSortedLeaves(node)
60
+
61
+ if ( con_leaves.length <= 1) {
62
+ return;
63
+ }
64
+
65
+ var str_leaves = JSON.stringify(con_leaves);
66
+
67
+ switch (str_leaves){
68
+ case JSON.stringify(['C', 'D', 'E']):
69
+ expect(node.extended_informations.Data).toBe("2");
70
+ break;
71
+ case JSON.stringify(['C', 'D']):
72
+ expect(node.extended_informations.Data).toBe("1");
73
+ break;
74
+ case JSON.stringify(['A', 'B', 'C', 'D', 'E', 'X']):
75
+ expect(node.extended_informations.Data).toBeNull();
76
+ break;
77
+ case JSON.stringify(['A', 'C', 'D', 'E']):
78
+ expect(node.extended_informations.Data).toBe("3");
79
+ break;
80
+ case JSON.stringify(['A', 'B', 'C', 'D', 'E']):
81
+ expect(node.extended_informations.Data).toBeUndefined();
82
+ break;
83
+ default:
84
+ expect(true).toBe(false);
85
+ break
86
+ }
87
+ });
88
+
89
+ // >>> test without internal label for branches
90
+ var m2 = phylo._create_model(data, {'data_type' : 'nhx'})
91
+ m2.traverse(m2.data,verify_boostrap_default);
92
+
93
+ // reroot on X
94
+ var x_node2 = m2.data.leaves.find(leaf => leaf.name === 'X');
95
+ m2.reroot(x_node2);
96
+ m2.traverse(m2.data,function(node, children) {
97
+
98
+ var con_leaves = getSortedLeaves(node)
99
+
100
+ if ( con_leaves.length <= 1) {
101
+ return;
102
+ }
103
+
104
+ var str_leaves = JSON.stringify(con_leaves);
105
+
106
+ switch (str_leaves){
107
+ case JSON.stringify(['C', 'D', 'E']):
108
+ expect(node.extended_informations.Data).toBeUndefined()
109
+ break;
110
+ case JSON.stringify(['C', 'D']):
111
+ expect(node.extended_informations.Data).toBe("1");
112
+ break;
113
+ case JSON.stringify(['A', 'B', 'C', 'D', 'E', 'X']):
114
+ expect(node.extended_informations.Data).toBeUndefined();
115
+ break;
116
+ case JSON.stringify(['A', 'C', 'D', 'E']):
117
+ expect(node.extended_informations.Data).toBe("2");
118
+ break;
119
+ case JSON.stringify(['A', 'B', 'C', 'D', 'E']):
120
+ expect(node.extended_informations.Data).toBe("3");
121
+ break;
122
+ default:
123
+ expect(true).toBe(false);
124
+ break
125
+ }
126
+ });
127
+
128
+ })
129
+
130
+ test('test reroot on B/X > C/D > B/X ' , () => {
131
+
132
+ const phylo = PhyloIO.init();
133
+
134
+ // >>> test with internal label for branches
135
+ var m1 = phylo._create_model(data, {'data_type' : 'nhx'})
136
+ m1.settings.edge_related_data.push('Data');
137
+ m1.traverse(m1.data,verify_boostrap_default);
138
+
139
+ // reroot on BX
140
+ var bx_node = null;
141
+ m1.traverse(m1.data, function(node, children) {
142
+
143
+ var con_leaves = getSortedLeaves(node)
144
+
145
+ if ( con_leaves.length <= 1) {
146
+ return;
147
+ }
148
+
149
+ var str_leaves = JSON.stringify(con_leaves);
150
+
151
+ if (str_leaves === JSON.stringify(['B', 'X'])) {
152
+ bx_node = node;
153
+ }
154
+ });
155
+ m1.reroot(bx_node);
156
+ m1.traverse(m1.data,function(node, children) {
157
+
158
+ var con_leaves = getSortedLeaves(node)
159
+
160
+ if ( con_leaves.length <= 1) {
161
+ return;
162
+ }
163
+
164
+ var str_leaves = JSON.stringify(con_leaves);
165
+
166
+ switch (str_leaves){
167
+ case JSON.stringify(['C', 'D', 'E']):
168
+ expect(node.extended_informations.Data).toBe("2");
169
+ break;
170
+ case JSON.stringify(['C', 'D']):
171
+ expect(node.extended_informations.Data).toBe("1");
172
+ break;
173
+ case JSON.stringify(['A', 'B', 'C', 'D', 'E', 'X']):
174
+ expect(node.extended_informations.Data).toBeNull();
175
+ break;
176
+ case JSON.stringify(['A', 'C', 'D', 'E']):
177
+ expect(node.extended_informations.Data).toBe("3");
178
+ break;
179
+ case JSON.stringify(['B', 'X']):
180
+ expect(node.extended_informations.Data).toBe("3");
181
+ break;
182
+ default:
183
+ expect(true).toBe(false);
184
+ break
185
+ }
186
+ });
187
+
188
+ // reroot on CD
189
+ var cd_node = null;
190
+ m1.traverse(m1.data, function(node, children) {
191
+
192
+ var con_leaves = getSortedLeaves(node)
193
+
194
+ if ( con_leaves.length <= 1) {
195
+ return;
196
+ }
197
+
198
+ var str_leaves = JSON.stringify(con_leaves);
199
+
200
+ if (str_leaves === JSON.stringify(['C', 'D'])) {
201
+ cd_node = node;
202
+ }
203
+ });
204
+ m1.reroot(cd_node);
205
+ m1.traverse(m1.data,function(node, children) {
206
+
207
+ var con_leaves = getSortedLeaves(node)
208
+
209
+ if ( con_leaves.length <= 1) {
210
+ return;
211
+ }
212
+
213
+ var str_leaves = JSON.stringify(con_leaves);
214
+
215
+ switch (str_leaves){
216
+ case JSON.stringify(['A', 'B', 'X']):
217
+ expect(node.extended_informations.Data).toBe("2");
218
+ break;
219
+ case JSON.stringify(['C', 'D']):
220
+ expect(node.extended_informations.Data).toBe("1");
221
+ break;
222
+ case JSON.stringify(['A', 'B', 'C', 'D', 'E', 'X']):
223
+ expect(node.extended_informations.Data).toBeNull();
224
+ break;
225
+ case JSON.stringify(['A', 'B', 'E', 'X']):
226
+ expect(node.extended_informations.Data).toBe("1");
227
+ break;
228
+ case JSON.stringify(['B', 'X']):
229
+ expect(node.extended_informations.Data).toBe("3");
230
+ break;
231
+ default:
232
+ expect(true).toBe(false);
233
+ break
234
+ }
235
+ });
236
+
237
+ // reroot on BX
238
+ var bx2_node = null;
239
+ m1.traverse(m1.data, function(node, children) {
240
+
241
+ var con_leaves = getSortedLeaves(node)
242
+
243
+ if ( con_leaves.length <= 1) {
244
+ return;
245
+ }
246
+
247
+ var str_leaves = JSON.stringify(con_leaves);
248
+
249
+ console.log(str_leaves)
250
+
251
+ if (str_leaves === JSON.stringify(['B', 'X'])) {
252
+
253
+ bx2_node = node;
254
+ }
255
+ });
256
+ m1.reroot(bx2_node);
257
+ m1.traverse(m1.data,function(node, children) {
258
+
259
+ var con_leaves = getSortedLeaves(node)
260
+
261
+ if ( con_leaves.length <= 1) {
262
+ return;
263
+ }
264
+
265
+ var str_leaves = JSON.stringify(con_leaves);
266
+
267
+ switch (str_leaves){
268
+ case JSON.stringify(['C', 'D', 'E']):
269
+ expect(node.extended_informations.Data).toBe("2");
270
+ break;
271
+ case JSON.stringify(['C', 'D']):
272
+ expect(node.extended_informations.Data).toBe("1");
273
+ break;
274
+ case JSON.stringify(['A', 'B', 'C', 'D', 'E', 'X']):
275
+ expect(node.extended_informations.Data).toBeNull();
276
+ break;
277
+ case JSON.stringify(['A', 'C', 'D', 'E']):
278
+ expect(node.extended_informations.Data).toBe("3");
279
+ break;
280
+ case JSON.stringify(['B', 'X']):
281
+ expect(node.extended_informations.Data).toBe("3");
282
+ break;
283
+ default:
284
+ expect(true).toBe(false);
285
+ break
286
+ }
287
+ });
288
+
289
+
290
+ })
291
+
292
+
293
+
294
+
295
+
296
+
297
+
package/src/model.js CHANGED
@@ -794,10 +794,6 @@ export default class Model {
794
794
  parent.branch_length = old_distance /2
795
795
  parent.extended_informations['Length'] = old_distance/2
796
796
 
797
-
798
-
799
- // ((C,D)1,(A,(B,X)3)2,E); to test
800
-
801
797
  // Until we reach the old root reverse child/parent order
802
798
  var child = root
803
799
  var stack = []
@@ -812,29 +808,26 @@ export default class Model {
812
808
  parent.values_before_reverse = {}
813
809
  parent.branch_length_before_reverse = parent.branch_length
814
810
 
815
- for (var key of this.settings.edge_related_data) {
816
-
817
- var value = key;
811
+ for (var value of this.settings.edge_related_data) {
818
812
 
819
813
  parent.values_before_reverse[value] = parent.extended_informations[value]
820
814
 
815
+ if (value=== 'Length'){
816
+ parent.branch_length = child.branch_length_before_reverse || child.branch_length;
817
+ parent.extended_informations['Length'] = parent.branch_length;
821
818
 
822
- if (value=== 'Length'){
823
- parent.branch_length = child.branch_length_before_reverse || child.branch_length;
824
- parent.extended_informations['Length'] = parent.branch_length;
825
-
819
+ }
820
+ else{
821
+ if ( child.values_before_reverse && value in child.values_before_reverse){
822
+ parent.extended_informations[value] = child.values_before_reverse[value]
826
823
  }
827
824
  else{
828
- if ( child.values_before_reverse && value in child.values_before_reverse){
829
- parent.extended_informations[value] = child.values_before_reverse[value]
830
- }
831
- else{
832
- parent.extended_informations[value] = child.extended_informations[value]
833
- child.extended_informations[value] = null
834
- }
835
-
825
+ parent.extended_informations[value] = child.extended_informations[value]
826
+ child.extended_informations[value] = null
836
827
  }
837
828
 
829
+ }
830
+
838
831
 
839
832
 
840
833
  }
@@ -890,7 +883,6 @@ export default class Model {
890
883
 
891
884
  for (var childy of root.children) {
892
885
 
893
- console.log(childy, childy.extended_informations[key], root.extended_informations[key] )
894
886
  childy.extended_informations[key] = root.extended_informations[key]
895
887
 
896
888
  }
@@ -909,6 +901,8 @@ export default class Model {
909
901
 
910
902
  this.traverse(root, function(n,c){
911
903
  n.leaves = this.get_leaves(n)
904
+ n.values_before_reverse = {}
905
+ n.branch_length_before_reverse = undefined
912
906
  })
913
907
 
914
908