Commit 6784d1e3 authored by Mikaël Salson's avatar Mikaël Salson Committed by Mathieu Giraud

tools.js: get_codons must keep the same number of chars as the input

Otherwise it induces unvoluntary frameshifts.  Therefore the total number of
characters returned in both lists must be equal and must also be equal to the
number of characters in the original sequences.

See #2056
parent ac17de9b
......@@ -12,8 +12,10 @@ var DEL="deletion";
* 0: first codon starts at first position, etc.
* @return an object containing a property ref and a property seq whose values
* are a list of codons. Some “codons” from seq may have a length ≠ 3.
* All codons from ref have 3 nucleotides (apart from the first/last codons), but may
* All codons from ref have 0 or 3 nucleotides (apart from the first/last codons), but may
* contain dashes.
* The total number of characters in both lists are equal and are also
* equal to the number of characters in the original sequences.
*/
function get_codons(ref, seq, frame) {
var codons_ref = [];
......@@ -32,12 +34,10 @@ function get_codons(ref, seq, frame) {
if (ref[pos] != '-') {
if (frame == 0)
break;
current_codon_ref += ref[pos];
frame--;
}
if (seq[pos] != '-') {
current_codon_seq += seq[pos];
}
current_codon_seq += seq[pos];
current_codon_ref += ref[pos];
}
if (current_codon_seq != '' || current_codon_ref != '') {
......@@ -60,10 +60,8 @@ function get_codons(ref, seq, frame) {
}
if (ref[pos] == '-') {
if (seq[pos] != '-') {
current_codon_seq += seq[pos];
current_codon_ref += '-';
}
current_codon_seq += seq[pos];
current_codon_ref += '-';
} else {
current_codon_ref += ref[pos];
current_codon_seq += seq[pos];
......
......@@ -27,6 +27,22 @@ QUnit.test("test get_codons", function(assert) {
codons = get_codons(r, s, 2);
assert.deepEqual(codons, {ref : ['AT', 'G--AT', 'AGA', 'CAG'],
seq : ['AA', 'ACCCG', '-GG', 'GTT']});
r = '-----ATG--ATAGACAG';
s = '--GACAAACCCG-GGGTT';
codons = get_codons(r, s, 0);
assert.deepEqual(codons, {ref : ['-----', 'ATG', '--', 'ATA', 'GAC', 'AG'],
seq : ['--GAC', 'AAA', 'CC', 'CG-', 'GGG', 'TT']});
codons = get_codons(r, s, 1);
assert.deepEqual(codons, {ref : ['-----A', 'TG--A', 'TAG', 'ACA', 'G'],
seq : ['--GACA', 'AACCC', 'G-G', 'GGT', 'T']});
codons = get_codons(r, s, 2);
assert.deepEqual(codons, {ref : ['-----AT', 'G--AT', 'AGA', 'CAG'],
seq : ['--GACAA', 'ACCCG', '-GG', 'GTT']});
});
QUnit.test("test get_mutations", function(assert) {
......@@ -82,21 +98,21 @@ QUnit.test("test get_mutations", function(assert) {
// AG-T > cGAT
mutations = get_mutations(r, s, 0);
assert.equal(Object.keys(mutations).length, 3, "Three mutations");
assert.deepEqual(mutations, {3 : DEL, 6 : SUBST, 8 : INS});
assert.deepEqual(mutations, {3 : DEL, 7 : SUBST, 9 : INS});
// ATA > ATC (I)
mutations = get_mutations(r, s, 1);
assert.equal(Object.keys(mutations).length, 3, "Three mutations");
assert.deepEqual(mutations, {3 : DEL, 6 : SILENT, 8 : INS});
assert.deepEqual(mutations, {3 : DEL, 7 : SILENT, 9 : INS});
// TAG > TcG (* > S)
mutations = get_mutations(r, s, 2);
assert.equal(Object.keys(mutations).length, 3, "Three mutations");
assert.deepEqual(mutations, {3 : DEL, 6 : SUBST, 8 : INS});
assert.deepEqual(mutations, {3 : DEL, 7 : SUBST, 9 : INS});
mutations = get_mutations(r, s);
assert.equal(Object.keys(mutations).length, 3, "Three mutations without phase");
assert.deepEqual(mutations, {3 : DEL, 6 : SUBST, 8 : INS});
assert.deepEqual(mutations, {3 : DEL, 7 : SUBST, 9 : INS});
});
QUnit.test("test nth_ocurrence", function(assert) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment