/
test_decomposition_view.js
431 lines (355 loc) · 14.9 KB
/
test_decomposition_view.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
requirejs([
'jquery',
'underscore',
'model',
'view',
'three'
], function($, _, model, DecompositionView, THREE) {
$(document).ready(function() {
var DecompositionModel = model.DecompositionModel;
module('Decomposition View', {
setup: function() {
// setup function
var data = {
name: 'pcoa',
sample_ids: ['PC.636', 'PC.635'],
coordinates: [
[-0.276542, -0.144964, 0.066647, -0.067711, 0.176070, 0.072969,
-0.229889, -0.046599],
[-0.237661, 0.046053, -0.138136, 0.159061, -0.247485, -0.115211,
-0.112864, 0.064794]],
percents_explained: [26.6887048633, 16.2563704022, 13.7754129161,
11.217215823, 10.024774995, 8.22835130237,
7.55971173665, 6.24945796136]};
var md_headers = ['SampleID', 'LinkerPrimerSequence', 'Treatment',
'DOB'];
var metadata = [['PC.636', 'YATGCTGCCTCCCGTAGGAGT', 'Control',
'20070314'],
['PC.635', 'YATGCTGCCTCCCGTAGGAGT', 'Fast',
'20071112']];
this.decomp = new DecompositionModel(data, md_headers, metadata);
data.edges = [['PC.636', 'PC.635']];
this.decompWithEdges = new DecompositionModel(data, md_headers,
metadata);
/* `this.expected`: this is the same as the object declared above,
* except this object is not being passed anywhere and is only used to
* check that nothing has changed between methods.
*/
data = {name: 'pcoa',
sample_ids: ['PC.636', 'PC.635'],
coordinates: [
[-0.276542, -0.144964, 0.066647, -0.067711, 0.176070, 0.072969,
-0.229889, -0.046599],
[-0.237661, 0.046053, -0.138136, 0.159061, -0.247485, -0.115211,
-0.112864, 0.064794]],
percents_explained: [26.6887048633, 16.2563704022,
13.7754129161, 11.217215823,
10.024774995, 8.22835130237,
7.55971173665, 6.24945796136]};
md_headers = ['SampleID', 'LinkerPrimerSequence', 'Treatment',
'DOB'];
metadata = [
['PC.636', 'YATGCTGCCTCCCGTAGGAGT', 'Control', '20070314'],
['PC.635', 'YATGCTGCCTCCCGTAGGAGT', 'Fast', '20071112']];
this.expected = new DecompositionModel(data, md_headers, metadata);
data.edges = [['PC.636', 'PC.635']];
this.expectedWithEdges = new DecompositionModel(data, md_headers,
metadata);
},
teardown: function() {
// teardown function
this.decomp = null;
this.expected = null;
}
});
/**
*
* Test that the Decomposition View object is initialized correctly
*
*/
test('Test constructor', function() {
var obs;
var dv = new DecompositionView(this.decomp);
deepEqual(dv.decomp, this.expected, 'decomp set correctly');
equal(dv.count, 2, 'count set correctly');
equal(dv.getVisibleCount(), 2, 'visibleCount set correctly');
deepEqual(dv.visibleDimensions, [0, 1, 2],
'visibleDimensions set correctly');
deepEqual(dv.tubes, [], 'tubes set correctly');
equal(dv.axesColor, '#FFFFFF');
equal(dv.backgroundColor, '#000000');
deepEqual(dv.axesOrientation, [1, 1, 1]);
/*
I'm unsure on how to test this, so right now just testing what I think
makes sense to test
*/
obs = [dv.markers[0].position.x,
dv.markers[0].position.y,
dv.markers[0].position.z];
exp = [-0.276542, -0.144964, 0.066647];
deepEqual(obs, exp, 'First marker position set correctly');
obs = [dv.markers[1].position.x,
dv.markers[1].position.y,
dv.markers[1].position.z];
exp = [-0.237661, 0.046053, -0.138136];
deepEqual(obs, exp, 'Second marker position set correctly');
deepEqual(dv.lines, {'left': null, 'right': null});
/*
TODO: How do we test this?
*/
// deepEqual(dv._genericSphere, undefined,
// "_genericSphere set correctly");
});
test('Test constructor (biplot)', function(assert) {
// this test is pretty much the same as above except for arrow types
this.decomp.type = 'arrow';
var view = new DecompositionView(this.decomp);
this.expected.type = 'arrow';
deepEqual(view.decomp, this.expected);
equal(view.count, 2);
equal(view.getVisibleCount(), 2);
deepEqual(view.visibleDimensions, [0, 1, 2]);
deepEqual(view.tubes, []);
equal(view.axesColor, '#FFFFFF');
equal(view.backgroundColor, '#000000');
deepEqual(view.axesOrientation, [1, 1, 1]);
// testing the markers
assert.ok(view.markers.length === 2);
equal(view.markers[0].line.name, 'PC.636');
equal(view.markers[0].cone.name, 'PC.636');
equal(view.markers[1].line.name, 'PC.635');
equal(view.markers[1].cone.name, 'PC.635');
var a = [0.19977209326971626, 0, 0.8289251461730293, 0.52246934148585];
var b = [-0.3246511569372691, 0, 0.5585576432564162, 0.7632922018854449];
deepEqual(view.markers[0].quaternion.toArray(), a);
deepEqual(view.markers[1].quaternion.toArray(), b);
deepEqual(view.markers[0].position.toArray(), [0, 0, 0]);
deepEqual(view.markers[1].position.toArray(), [0, 0, 0]);
// bulk checks
view.markers.map(function(marker) {
assert.ok(marker instanceof THREE.ArrowHelper);
deepEqual(marker.line.material.color.getHex(), 0xc0c0c0);
deepEqual(marker.cone.material.color.getHex(), 0xc0c0c0);
});
});
test('Test constructor (with edges)', function(assert) {
// this test is pretty much the same as above except checking for edges
var view = new DecompositionView(this.decompWithEdges);
deepEqual(view.decomp, this.expectedWithEdges);
equal(view.count, 2);
equal(view.getVisibleCount(), 2);
deepEqual(view.visibleDimensions, [0, 1, 2]);
deepEqual(view.tubes, []);
equal(view.axesColor, '#FFFFFF');
equal(view.backgroundColor, '#000000');
deepEqual(view.axesOrientation, [1, 1, 1]);
// testing the markers
assert.ok(view.markers.length === 2);
// check for the edges
assert.ok(view.lines.left instanceof THREE.LineSegments);
assert.ok(view.lines.right instanceof THREE.LineSegments);
equal(view.lines.left.material.color.getHex(), 0xffffff);
equal(view.lines.right.material.color.getHex(), 0xff0000);
deepEqual(view.lines.left.geometry.attributes.position.array.length, 6);
deepEqual(view.lines.right.geometry.attributes.position.array.length, 6);
});
/**
*
* Test that getVisibleCount is correctly updated
*
*/
test('Test getVisibleCount', function() {
var dv = new DecompositionView(this.decomp);
dv.markers[0].visible = false;
equal(dv.getVisibleCount(), 1);
dv.markers[1].visible = false;
equal(dv.getVisibleCount(), 0);
dv.markers[0].visible = true;
dv.markers[1].visible = true;
equal(dv.getVisibleCount(), 2);
});
/**
*
* Test that changeVisibleDimensions updates the meshes position
*
*/
test('Test changeVisibleDimensions', function() {
var dv = new DecompositionView(this.decomp);
dv.changeVisibleDimensions([2, 3, 4]);
obs = [dv.markers[0].position.x,
dv.markers[0].position.y,
dv.markers[0].position.z];
exp = [0.066647, -0.067711, 0.176070];
deepEqual(obs, exp, 'First marker position updated correctly');
obs = [dv.markers[1].position.x,
dv.markers[1].position.y,
dv.markers[1].position.z];
exp = [-0.138136, 0.159061, -0.247485];
deepEqual(obs, exp, 'Second marker position updated correctly');
deepEqual(dv.axesOrientation, [1, 1, 1]);
});
/**
*
* Test that changeVisibleDimensions with edges
*
*/
test('Test changeVisibleDimensions with edges', function() {
var dv = new DecompositionView(this.decompWithEdges);
dv.changeVisibleDimensions([2, 3, 4]);
obs = [dv.markers[0].position.x, dv.markers[0].position.y,
dv.markers[0].position.z];
var exp = [0.066647, -0.067711, 0.176070];
deepEqual(obs, exp);
obs = [dv.markers[1].position.x,
dv.markers[1].position.y,
dv.markers[1].position.z];
exp = [-0.138136, 0.159061, -0.247485];
deepEqual(obs, exp);
exp = [0.0666470006108284, -0.0677110031247139, 0.17607000470161438,
-0.035744499415159225, 0.04567499831318855, -0.0357074998319149];
exp = new Float32Array(exp);
deepEqual(dv.lines.left.geometry.attributes.position.array, exp);
exp = [-0.13813599944114685, 0.159060999751091, -0.2474849969148636,
-0.035744499415159225, 0.04567499831318855, -0.0357074998319149];
exp = new Float32Array(exp);
deepEqual(dv.lines.right.geometry.attributes.position.array, exp);
deepEqual(dv.axesOrientation, [1, 1, 1]);
});
test('Test changeVisibleDimensions (2D)', function() {
var dv = new DecompositionView(this.decomp);
dv.changeVisibleDimensions([2, 3, null]);
obs = [dv.markers[0].position.x,
dv.markers[0].position.y,
dv.markers[0].position.z];
exp = [0.066647, -0.067711, 0];
deepEqual(obs, exp);
obs = [dv.markers[1].position.x,
dv.markers[1].position.y,
dv.markers[1].position.z];
exp = [-0.138136, 0.159061, 0];
deepEqual(obs, exp);
deepEqual(dv.axesOrientation, [1, 1, 1]);
});
/**
*
* Test the changeVisibleDimensions throws an error if the number of
* dimensions passes is different the 3
*
*/
test('Test changeVisibleDimensions excepts', function() {
throws(
function() {
var dv = new DecompositionView(this.decomp);
dv.changeVisibleDimensions([2, 3, 4, 5]);
},
Error,
'An error is raised if the number of dimensions is not 3'
);
});
/**
*
* Test that changeVisibleDimensions updates the meshes position
*
*/
test('Test change flip axes', function(assert) {
var dv = new DecompositionView(this.decomp);
// copy the arrays
expa = _.clone(dv.markers[0].position.toArray());
expb = _.clone(dv.markers[1].position.toArray());
// flip the orientation of the position
expb[1] = expb[1] * -1;
expa[1] = expa[1] * -1;
// change the position of the decomposition view and ...
dv.flipVisibleDimension(1);
// ... Check for the following things:
//
// 1.- The position themselves
// 2.- The ranges i.e. positions still fall within the dimensionRanges.
// 3.- The axis orientation vector
obs = dv.markers[0].position.toArray();
deepEqual(obs, expa, 'First marker position updated correctly');
assert.ok(obs[1] <= dv.decomp.dimensionRanges.max[1],
'Falls within range (max)');
assert.ok(obs[1] >= dv.decomp.dimensionRanges.min[1],
'Falls within range (min)');
obs = dv.markers[1].position.toArray();
deepEqual(obs, expb, 'Second marker position updated correctly');
assert.ok(obs[1] <= dv.decomp.dimensionRanges.max[1],
'Falls within range (max)');
assert.ok(obs[1] >= dv.decomp.dimensionRanges.min[1],
'Falls within range (min)');
deepEqual(dv.axesOrientation, [1, -1, 1]);
});
test('Test change flip axes (2D)', function(assert) {
var dv = new DecompositionView(this.decomp);
var expa = [-0.276542, 0.144964, 0], expb = [-0.237661, -0.046053, 0];
dv.changeVisibleDimensions([0, 1, null]);
// change the position of the decomposition view and ...
dv.flipVisibleDimension(1);
// ... Check for the following things:
//
// 1.- The position themselves
// 2.- The ranges i.e. positions still fall within the dimensionRanges.
// 3.- The axis orientation vector
obs = dv.markers[0].position.toArray();
deepEqual(obs, expa, 'First marker position updated correctly');
obs = dv.markers[1].position.toArray();
deepEqual(obs, expb, 'Second marker position updated correctly');
deepEqual(dv.axesOrientation, [1, -1, 1]);
});
/**
*
* Test that changeVisibleDimensions and flip axis
*
*/
test('Test changing the orientations and then flipping a dimension',
function() {
var dv = new DecompositionView(this.decomp);
deepEqual(dv.axesOrientation, [1, 1, 1]);
dv.changeVisibleDimensions([2, 3, 4]);
obs = dv.markers[0].position.toArray();
exp = [0.066647, -0.067711, 0.176070];
deepEqual(obs, exp, 'First marker position updated correctly');
obs = dv.markers[1].position.toArray();
exp = [-0.138136, 0.159061, -0.247485];
deepEqual(obs, exp, 'Second marker position updated correctly');
deepEqual(dv.axesOrientation, [1, 1, 1]);
dv.flipVisibleDimension(3);
obs = dv.markers[0].position.toArray();
exp = [0.066647, 0.067711, 0.176070];
deepEqual(obs, exp, 'First marker position updated correctly');
obs = dv.markers[1].position.toArray();
exp = [-0.138136, -0.159061, -0.247485];
deepEqual(obs, exp, 'Second marker position updated correctly');
deepEqual(dv.axesOrientation, [1, -1, 1]);
});
test('Test showEdgesForPlottables', function() {
var dv = new DecompositionView(this.decompWithEdges);
dv.hideEdgesForPlottables();
dv.showEdgesForPlottables();
exp = [-0.2765420079231262, -0.14496399462223053, 0.0666470006108284,
-0.25710150599479675, -0.04945550113916397,
-0.035744499415159225];
exp = new Float32Array(exp);
deepEqual(dv.lines.left.geometry.attributes.position.array, exp);
exp = [-0.23766100406646729, 0.046052999794483185, -0.13813599944114685,
-0.25710150599479675, -0.04945550113916397,
-0.035744499415159225];
exp = new Float32Array(exp);
deepEqual(dv.lines.right.geometry.attributes.position.array, exp);
// shouldn't error and work fine
dv.showEdgesForPlottables('Not a plottable');
});
test('Test hideEdgesForPlottables', function() {
var dv = new DecompositionView(this.decompWithEdges);
dv.hideEdgesForPlottables();
exp = [0, 0, 0, 0, 0, 0];
exp = new Float32Array(exp);
deepEqual(dv.lines.left.geometry.attributes.position.array, exp);
exp = [0, 0, 0, 0, 0, 0];
exp = new Float32Array(exp);
deepEqual(dv.lines.right.geometry.attributes.position.array, exp);
dv.hideEdgesForPlottables('Not a plottable');
});
});
});