-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathjs-tests.html
342 lines (271 loc) · 10.6 KB
/
js-tests.html
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
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Slivar javascript tests</title>
<style>
html *
{
font-size: 1.2em;
color: #555;
font-family: Arial;
}
.pass:before {
content: 'PASS: ';
color: green;
font-weight: bold;
}
.fail:before {
content: 'FAIL: ';
color: red;
font-weight: bold;
}
pre {
font-size: 0.6em;
}
.json {
font-family: monospace !important;
font-size: 0.7em !important;
}
.hom_ref {
color: #ccc !important;
}
.het {
color: #999 !important;
}
.hom_alt {
color: #555 !important;
}
.unknown {
color: yellow !important;
}
.red {
color: red !important;
}
h1 {
font-size: 1.5em;
}
</style>
</head>
<body>
<div id="output"></div>
<hr>
<pre>
Enter javascript to evaluate (open your javascript console to see errors).
Report good test-cases (especially ones that fail) <a href="https://github.com/brentp/slivar/issues">here</a>.
Available functions are <b>segregating_denovo</b>, <b>segregating_dominant</b> and <b>segregating_recessive</b>
in addition to the X-linked version of those that end with '_x', e.g.: <b>segregating_denovo_x</b>.
As an example, adjust the setup below to change kid to HET and affected to ensure that a transmitted de novo is allowed.
Then try setting the kid to HOM_ALT to ensure this does not pass.
</pre>
<div id="testing_output"></div>
<p><button id="evaluate">Evaluate Expression</button></p>
<textarea id="testing", rows=10 cols=90>
var gma = Sample(HOM_REF, {sex:"female"})
var gpa = Sample(HOM_REF,{sex:"male"})
var mom = Sample(HET, {sex:"female", affected: true})
var dad = Sample(HOM_REF, {sex:"male"})
var kid = Sample(HOM_REF, {sex:"male"})
as_trio(mom, gpa, gma) //this sets mom.mom=gma, mom.dad=gpa, gpa.kids=[mom]...
as_trio(kid, dad, mom)
fam = [gma, gpa, mom, dad, kid]
// note that fam is an array and we pass it in another array.
assert(segregating_denovo, [fam], "denovo in mom")
</textarea>
<script>
var output = document.getElementById('output');
var hom_ref = "<span class='hom_ref'>0/0</span>"
var het = "<span class='het'>0/1</span>"
var hom_alt = "<span class='hom_alt'>1/1</span>"
var unknown = "<span class='unknown'>./.</span>"
function SECTION(name) {
var p = document.createElement("h1")
p.appendChild(document.createTextNode(name))
output.appendChild(p)
}
document.getElementById("evaluate").onclick = function() {
output.innerHTML = ""
SECTION("testing output")
var code = document.getElementById("testing").value
var result = eval(code)
}
function stringify(o) {
// json.STRINGIFY, but get affected status and HET/HOM_REF/HOM_ALT
var s = Object.assign({}, o)
if(s.kids != undefined) {
var kids = [];
for(var i=0; i<s.kids.length;i++) {
kids.push(s.kids[i].id)
}
s.kids = kids;
}
if(s.mom != undefined) { s.mom = s.mom.id }
if(s.dad != undefined) { s.dad = s.dad.id }
var result = (s.hom_ref ? hom_ref : s.het ? het : s.hom_alt ? hom_alt : uknown)
if(s.affected){ result += "<b class='red'>*</b>"; } else { result += " " }
return result + JSON.stringify(s)
}
function assert_(fn, samples, description, expect) {
var outcome
if(Array.isArray(samples[0])) {
samples = samples[0]
outcome = samples.every(fn)
} else {
outcome = fn.apply(null, samples)
}
outcome = outcome == expect
var det = document.createElement('details');
var sum = document.createElement("summary")
sum.className = outcome ? 'pass' : 'fail';
det.appendChild(sum)
sum.appendChild( document.createTextNode((expect ? '(+) ' : '(-) ') + description ) );
var div = document.createElement("div");
div.className = "json";
div.innerHTML += "<summary><b>" + fn.name + "()<details><pre>" + fn.toString() + "</pre></details></summary><br>"
samples.forEach(sample => div.innerHTML += stringify(sample) + "<br>")
det.appendChild(div)
output.appendChild(det);
};
function assert(fn, samples, description) {
return assert_(fn, samples, description, true)
}
function assert_not(fn, samples, description) {
return assert_(fn, samples, description, false)
}
function add_defaults(s) {
if(s.GQ === undefined) { s.GQ = 50 }
if(s.AD === undefined) {
if(s.hom_ref) {s.AD = [20, 0]}
if(s.het) {s.AD = [10, 10]}
if(s.hom_alt) {s.AD = [0, 20]}
}
if(s.DP === undefined) {
s.DP = s.AD[0] + s.AD[1]
}
if(s.AB === undefined) {
s.AB = s.AD[1] / Math.max(1, s.AD[1] + s.AD[0])
}
if(s.affected === undefined) { s.affected = false }
if(s.kids === undefined) { s.kids = [] }
}
var HET = 1, HOM_REF = 0, HOM_ALT = 2, UNKNOWN = -1
function Sample(gt, s) {
if(gt == 0) { s.hom_ref = true;s.het = false;s.hom_alt = false;s.unknown = false , s.AB = 0.0}
else if(gt == 1) {s.hom_ref = false;s.het = true;s.hom_alt = false;s.unknown = false, s.AB = 0.5}
else if(gt == 2) {s.hom_ref = false;s.het = false;s.hom_alt = true;s.unknown = false, s.AB = 1.0}
else {s.hom_ref = false;s.het = false;s.hom_alt = false;s.unknown = true}
add_defaults(s)
return s
}
function as_trio(kid, dad, mom) {
kid.dad = dad
kid.mom = mom
if(mom.kids === undefined) { mom.kids = [] }
if(dad.kids === undefined) { dad.kids = [] }
mom.kids.push(kid)
dad.kids.push(kid)
}
</script>
<script src="slivar-functions.js"></script>
<script>
SECTION("trio-based tests")
var mom = {id: "mom", sex: 'female', hom_ref: true, het: false, hom_alt: false, unknown: false, GQ: 20, AB: 0.0, DP: 8, AD: [8, 0], affected: false}
var dad = {id: "dad", sex: 'male', hom_ref: true, het: false, hom_alt: false, unknown: false, GQ: 20, AB: 0.0, DP: 8, AD: [8, 0], affected: false}
var kid = {id: "kid", sex: 'female', hom_ref: false, het: true, hom_alt: false, unknown: false, GQ: 20, AB: 0.5, DP: 8, AD: [4, 4], affected: true}
assert(denovo, [kid, dad, mom], "standard de novo")
assert_not(denovo, [dad, dad, mom], "not de novo")
var variant = {"CHROM": "chr1"}
SECTION("family-based tests")
assert(segregating_dominant, [[kid, dad, mom]], "trio with segregating dominant")
assert_not(segregating_recessive, [[kid, dad, mom]], "trio with-out segregating recessive")
var mom = {id: "mom", sex: 'female', hom_ref: false, het: true, hom_alt: false, unknown: false, GQ: 20, AB: 0.5, DP: 8, AD: [8, 8], affected: true}
var dad = {id: "dad", sex: 'male', hom_ref: true, het: false, hom_alt: false, unknown: false, GQ: 20, AB: 0.0, DP: 8, AD: [8, 0], affected: false}
var kid = {id: "kid", sex: 'female', hom_ref: false, het: true, hom_alt: false, unknown: false, GQ: 20, AB: 0.5, DP: 8, AD: [8, 8], affected: true}
as_trio(kid, dad, mom)
kid.mom = mom; kid.dad = dad
assert_not(segregating_denovo, [[kid, dad, mom]], "affected mom is not segregating de novo")
var k1 = Sample(HET, {id:"kid", sex:"female", affected: true})
var m1 = Sample(HOM_REF, {id:"mom", sex:"female", affected: false})
var d1 = Sample(HOM_REF, {id:"dad", sex:"female", affected: false})
as_trio(k1, m1, d1)
assert(segregating_denovo, [[k1, d1, m1]], "segregating de novo")
k1.AB = 0.1
assert_not(segregating_denovo, [[k1, d1, m1]], "segregating de novo with bad allele balance")
k1.AB = 0.5
k1.affected = false;
assert_not(segregating_denovo, [[k1, d1, m1]], "segregating de novo with het unaffected")
k1.affected = true;
m1.affected = true
assert_not(segregating_denovo, [[k1, d1, m1]], "segregating de novo with affected mom")
// inherited de novo
var gma = Sample(HOM_REF, {id:"gma", sex:"female"})
var gpa = Sample(HOM_REF,{id:"gpa", sex:"male"})
var mom = Sample(HET, {id:"mom", sex:"female", affected: true})
var dad = Sample(HOM_REF, {id:"dad", sex:"male"})
var kid = Sample(HOM_REF, {id:"kid", sex:"male"})
as_trio(kid, dad, mom)
as_trio(mom, gma, gpa)
var fam = [gma, gpa, mom, dad, kid]
// only mom is het and afffected
assert(segregating_denovo, [fam], "inheritied de novo")
kid.affected = true
kid = Sample(HET, kid)
// mom and kid are het and affected
assert(segregating_denovo, [fam], "inheritied de novo adding affected, het kid")
mom.affected = true
mom = Sample(HOM_REF, mom)
// NOT: mom is affected, but hom-ref, kid is affected and het
assert_not(segregating_denovo, [fam], "de novo in 3rd gen with affected hom-ref mom")
SECTION("segregating X denovo")
var gma = Sample(HOM_REF, {id:"gma", sex:"female"})
var gpa = Sample(HOM_REF,{id:"gpa", sex:"male"})
var mom = Sample(HET, {id:"mom", sex:"female", affected: true})
var dad = Sample(HOM_REF, {id:"dad", sex:"male"})
var kid = Sample(HOM_REF, {id:"kid", sex:"male"})
var distant = Sample(HOM_REF, {id:"kid", sex:"male"})
as_trio(kid, dad, mom)
as_trio(mom, gma, gpa)
var fam = [gma, gpa, mom, dad, kid]
assert(segregating_denovo_x, [fam], "x-linked denovo not transmitted")
kid = Sample(HOM_ALT, kid); kid.affected = true
assert(segregating_denovo_x, [fam], "x-linked denovo transmitted")
// NOTE that as implemented, having a uncle who also has the de novo will still pass.
var uncle = Sample(HET, {id:"uncle", sex:"male", affected: true})
as_trio(uncle, gma, gpa)
fam.push(uncle)
assert(segregating_denovo_x, [fam], "x-linked denovo in mom+uncle, transmitted to kid")
SECTION("segregating denovo")
assert_not(segregating_denovo, [fam], "autosomal denovo but kid is hom_alt")
kid = Sample(HET, kid)
assert(segregating_denovo, [fam], "denovo kid is het not transmitted")
var mom = Sample(HET, {id:"mom", sex:"female", affected: true})
var dad = Sample(HOM_REF, {id:"dad", sex:"male"})
var kid = Sample(HET, {id:"kid", sex:"male", affected: true})
as_trio(kid, dad, mom)
assert_not(segregating_denovo, [[kid, dad, mom]], "not denovo with affected, het mom")
var mom = Sample(HOM_REF, {id:"mom", sex:"female"})
var dad = Sample(HOM_REF, {id:"dad", sex:"male"})
var kid = Sample(HET, {id:"kid", sex:"male", affected: true})
var sib = Sample(HET, {id:"kid", sex:"male", affected: true})
as_trio(kid, dad, mom)
as_trio(sib, dad, mom)
assert(segregating_denovo, [[kid, dad, mom, sib]], "denovo in both affected, het sibs")
var mom = Sample(HOM_REF, {id:"mom", sex:"female"})
var dad = Sample(HOM_REF, {id:"dad", sex:"male"})
var kid = Sample(HOM_ALT, {id:"kid", sex:"male", affected: true})
assert(segregating_recessive, [[kid, dad, mom]], "hom-alt denovo is segregating recessive")
SECTION("segregating x dominant")
var mom = Sample(HOM_REF, {id:"mom", sex:"female", GQ: 20, DP: 20})
var dad = Sample(HOM_ALT, {id:"dad", sex:"male", affected: true, GQ: 9, DP: 20})
var kid = Sample(HOM_ALT, {id:"kid", sex:"male", affected: true, GQ: 20, DP: 20})
kid.mom = mom
kid.dad = dad
dad.kids = [kid]
mom.kids = [kid]
assert_not(segregating_dominant_x, [[kid, dad, mom]], "dad has too low GQ")
dad.GQ = 10
assert(segregating_dominant_x, [[kid, dad, mom]], "dad has 10 GQ")
output = document.getElementById("testing_output")
</script>
</body>
</html>