-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.js
191 lines (158 loc) · 5.92 KB
/
Test.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
/********************************************************************
Bricodopt
Copyright (C) 2015 Andreas Huemer
This file is part of Bricodopt.
Bricodopt is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
Bricodopt is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
********************************************************************/
QUnit.test("Frequencies", function(assert) {
var checkFrequency = function(frequencies, codon, expected) {
assert.equal(Frequencies.getFrequency(frequencies, codon), expected);
};
var checkSum = function(frequencies, expected) {
assert.equal(Frequencies.sum(frequencies), expected);
};
var frequencies0 = Frequencies.none();
var frequencies1 = Frequencies.add(frequencies0, "ATG", 5);
var frequencies2 = Frequencies.add(undefined, "GTG", 7);
var frequencies3 = Frequencies.add(frequencies1, "GTG", 7);
var frequencies4 = Frequencies.add(frequencies3, "ATG", 3);
checkFrequency(frequencies0, "ATG", 0);
checkSum(frequencies0, 0);
checkFrequency(frequencies1, "ATG", 5);
checkFrequency(frequencies1, "GTG", 0);
checkSum(frequencies1, 5);
checkFrequency(frequencies2, "ATG", 0);
checkFrequency(frequencies2, "GTG", 7);
checkSum(frequencies2, 7);
checkFrequency(frequencies3, "ATG", 5);
checkFrequency(frequencies3, "GTG", 7);
checkSum(frequencies3, 12);
checkFrequency(frequencies4, "ATG", 8);
checkFrequency(frequencies4, "GTG", 7);
checkSum(frequencies4, 15);
checkFrequency(undefined, "ATG", 0);
checkSum(undefined, 0);
});
QUnit.test("Frequencies.forAllCodons", function(assert) {
var frequencies = Frequencies.add(Frequencies.add(Frequencies.none(), "ATG", 5), "GTG", 7);
var count = 0;
Frequencies.forAllCodons(frequencies, function(codon) {
count++;
});
assert.equal(count, 2);
});
QUnit.test("Frequencies.findCodon", function(assert) {
var check = function(frequencies, p, expected) {
assert.equal(Frequencies.findCodon(frequencies, p), expected);
};
var frequencies = Frequencies.add(Frequencies.add(Frequencies.none(), "ATG", 5), "GTG", 7);
check(frequencies, function(codon) {
return codon === "ATG";
}, "ATG");
check(frequencies, function(codon) {
return codon === "GTG";
}, "GTG");
check(frequencies, function(codon) {
return codon === "TAA";
}, undefined);
});
QUnit.test("Frequencies.normalise", function(assert) {
var checkFrequency = function(frequencies, codon, expected) {
assert.equal(Frequencies.getFrequency(frequencies, codon), expected);
};
var checkSum = function(frequencies, expected) {
assert.equal(Frequencies.sum(frequencies), expected);
};
var frequencies = Frequencies.add(Frequencies.add(Frequencies.none(), "ATG", 5), "GTG", 7);
var normalised = Frequencies.normalise(frequencies);
checkFrequency(normalised, "ATG", 5/12);
checkFrequency(normalised, "GTG", 7/12);
checkSum(normalised, 1);
checkSum(Frequencies.normalise(Frequencies.none()), 0);
});
QUnit.test("Sequence.normalise", function(assert) {
var check = function(input, expected) {
assert.equal(Sequence.normalise(input), expected);
};
check("", "");
check("ATGGTGAGCTAA", "ATGGTGAGCTAA");
check("ATG GTG AGC TAA", "ATGGTGAGCTAA");
check(" ATGGTGAGCTAA ", "ATGGTGAGCTAA");
check("\tATGGTG\tAGCTAA\t", "ATGGTGAGCTAA");
check("\nATG GTG\tAGC TAA\n", "ATGGTGAGCTAA");
});
QUnit.test("Sequence.getAminoAcids", function(assert) {
var check = function(input, expected) {
assert.equal(Sequence.getAminoAcids(input), expected);
};
check("", "");
check("GCTGCCGCAGCG", "AAAA");
check("TGTTGC", "CC");
check("GATGAC", "DD");
check("GAAGAG", "EE");
check("TTTTTC", "FF");
check("GGTGGCGGAGGG", "GGGG");
check("CATCAC", "HH");
check("ATTATCATA", "III");
check("AAAAAG", "KK");
check("TTATTGCTTCTCCTACTG", "LLLLLL");
check("ATG", "M");
check("AATAAC", "NN");
check("CCTCCCCCACCG", "PPPP");
check("CAACAG", "QQ");
check("CGTCGCCGACGGAGAAGG", "RRRRRR");
check("TCTTCCTCATCGAGTAGC", "SSSSSS");
check("ACTACCACAACG", "TTTT");
check("GTTGTCGTAGTG", "VVVV");
check("TGG", "W");
check("TATTAC", "YY");
check("TAATGATAG", "...");
check("XXX", "?");
check("XX", "");
check("XXXX", "?");
check("AACABCAB", "N?");
});
var randomAminoAcids = function() {
var aminoAcids = "";
var length = Math.floor(Math.random() * 200);
var options = "ACDEFGHIKLMNPQRSTVWY.";
var optionsCount = options.length;
for (var i = 0; i < length; i++) {
var sel = Math.floor(Math.random() * optionsCount);
aminoAcids = aminoAcids.concat(options.charAt(sel));
}
return aminoAcids;
};
QUnit.test("SequenceFactory.probabilisticSequenceFactory", function(assert) {
var check = function(aminoAcids, sequenceFactory) {
var optimised = SequenceFactory.getSequence(aminoAcids, sequenceFactory);
assert.equal(Sequence.getAminoAcids(optimised), aminoAcids);
};
var frequencyMap = FrequencyMap.parse(Example.frequencies);
var sequenceFactory = SequenceFactory.probabilisticSequenceFactory(frequencyMap);
check("", sequenceFactory);
for (var i = 0; i < 50; i++) {
check(randomAminoAcids(), sequenceFactory);
}
});
QUnit.test("SequenceFactory.deterministicSequenceFactory", function(assert) {
var check = function(aminoAcids, sequenceFactory) {
var optimised = SequenceFactory.getSequence(aminoAcids, sequenceFactory);
assert.equal(Sequence.getAminoAcids(optimised), aminoAcids);
};
var frequencyMap = FrequencyMap.parse(Example.frequencies);
var sequenceFactory = SequenceFactory.deterministicSequenceFactory(frequencyMap);
check("", sequenceFactory);
for (var i = 0; i < 50; i++) {
check(randomAminoAcids(), sequenceFactory);
}
});