-
Notifications
You must be signed in to change notification settings - Fork 16
/
nussinovmatrix.js
1851 lines (1532 loc) · 64 KB
/
nussinovmatrix.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file Main file containing backend algorithms for RNA-algorithms-JS project.
Main Items contains in theis file are:
-Matrix class
-Nussinov algorithms
-Traceback algorithms
* @authors "Mostafa Mahmoud Mohamed", "Syed Mohsin Ali", "Martin Mann"
*/
"use strict";
/**
* Utility class that covers RNA specific functions.
*/
var EPS = 1e-4; // floating point precision error
var RnaUtil = {
/**
* checks base pair complementary of two nucleotides
* @param {string} nt1 first nucleotide
* @param {string} nt2 second nucleotide
* @returns {boolean} true if complementary; false otherwise
*/
areComplementary: function (nt1, nt2) {
//console.log("areComp:", nt1, nt2);
var complementary =
(nt1 === "A" && nt2 === "U") || (nt1 === "U" && nt2 === "A") ||
(nt1 === "G" && nt2 === "C") || (nt1 === "C" && nt2 === "G") ||
(nt1 === "G" && nt2 === "U") || (nt1 === "U" && nt2 === "G");
return complementary;
},
/**
* checks whether or not the sequence is a valid RNA sequence
* @param sequence
*/
isRnaSequence: function (sequence) {
var isValid =
// check if sequence given
(sequence !== null)
// check RNA alphabet
&& sequence.match("^[ACGU]+$");
return isValid;
}
};
/**
* data stored within a cell of a nussinov matrix
*/
var NussinovCell = {
// row
i: -1,
// column
j: -1,
// value
value: null,
logValue: null,
// traces for the current value
traces: null,
/**
* Initialize a cell with the given data and sets traces to an empty list
* @param i the row of the cell
* @param j the column of the cell
* @param value the value of the cell
* @return this : cell access for chaining
*/
init: function (i, j, value) {
// init data
this.i = i;
this.j = j;
this.value = value;
this.traces = [];
this.logValue = null;
// this access for chaining
return this;
}
};
/**
* Ancestor information for a certain traceback
*/
var NussinovCellTrace = {
// list of parent cells
parents: null,
// list of base pairs added
bps: null,
/**
* initializes the object
* @param {object} parents the parents to set
* @param {object} bps the base pairs to set
* @returns {NussinovCellTrace} this for call chaining
*/
init: function (parents, bps) {
this.parents = null;
this.bps = null;
// input check
if ((parents !== null && bps === null) || (parents === null && bps !== null)) {
console.log("ERROR : NussinovCellTrace.init : only one value null");
return this;
}
// store sane data
this.parents = parents;
this.bps = bps;
return this;
}
};
/**
* Nussinov matrix object, stores Sequence and Table. Contains all the utility functionalities for the tables.
*/
var NussinovMatrix = {
/**
* Access to the sequence for this matrix
*/
sequence: null,
/**
* Sequence length
* */
seq_length: 0,
/**
* Access name of recursion used
*/
name: null,
/**
* Minimal loop length within computation
*/
minLoopLength: 0,
/**
* cells of the matrix
*/
cells: [],
/**
* The latex representation of the formula computing the matrix.
*/
latex_representation: "$$",
/**
* The dimensions of the matrix.
* */
tablesDimension: 2,
/**
* initialize a matrix of dim = (n + 1) x n, where n is the length of the provided sequence
* @param {string} sequence the RNA sequence (not null or empty)
* @returns {NussinovMatrix} this
*/
init: function(sequence, name) {
// reset data
this.sequence = null;
this.name = null;
this.cells = [];
// check input
if (sequence == null || sequence === "" || name == null) {
console.log("Matrix init failed for sequence (", sequence, ")");
return this;
}
// store sequence
this.sequence = sequence;
this.name = name;
this.seq_length = sequence.length;
// create matrix cells
for (var i = 0; i <= this.seq_length; i++) {
this.cells[i] = [];
for (var j = 0; j <= this.seq_length; j++) {
// create new cell and initialize
this.cells[i][j] = Object.create(NussinovCell).init(i, j, null);
}
}
return this;
},
/**
* Check if a given tuple is an invalid state or not
* @param i row #
* @param j column #
* @returns {boolean}
* */
isInvalidState: function(i, j) {
if (i < 0 || j < 0 || i > this.seq_length || j > this.seq_length || i > j + 1) {
return true;
} else {
return false;
}
},
/**
* Compute the cell at a given state in the matrix.
* It's recommended to make the implementation use the method "updateCell",
* if it's computing the tracebacks in an optimization problem.
* TODO: this function has to be overwritten by the instances, before calling computeMatrix/computeAllCells
* @param i row #
* @param j column #
* @returns {NussinovCell} The computed cell.
*/
computeCell: function(i, j) {
// updateCell(i, j);
return Object.create(NussinovCell).init(i, j, null);
},
/**
* Access a cell at a given state in the matrix. If the cell is null or has no value,
* then it will be computed using the "computeCell" method.
* TODO: Implement computeCell
* @param i row #
* @param j column #
* @returns {NussinovCell} The cell or null if it's an invalid state
*/
getCell: function (i, j) {
// check border cases {
if (this.isInvalidState(i, j)) {
return null;
}
if (this.cells[i][j] === null || this.cells[i][j].value == null) {
this.cells[i][j] = this.computeCell(i, j);
}
return this.cells[i][j];
},
/**
* returns traces of cell (i,j), i.e. ancestor cells and the basepairs they form
* @param {int} i row #.
* @param {int} j column #.
* @returns {object} ancestor's object Eg. {parents:[],bPs:[]} or null if not available
*/
getTraces: function (i, j) { //get traceback info for each cell
// access cell at location (i,j) in the matrix
var cell = this.getCell(i, j);
// check if valid cell
if (cell === null) {
return null;
}
return cell.traces;
},
/**
* Access the value at a given state in the matrix. If the cell is null or has no value,
* then the cell will be computed using the "computeCell" method.
* TODO: Implement computeCell
* @param i row #
* @param j column #
* @returns {float} Cell value or null if invalid cell
*/
getValue: function (i, j) {
var cell = this.getCell(i, j);
if (cell === null) {
return null;
}
return parseFloat(cell.value);
},
/**
* Updates the ancestor list of a given cell if the curVal is higher or
* equal to the current value within the cell.
* If the value is equal, curAncestor is added to the list.
* If the value is smaller than curVal, curAncestor will be set to be the
* only list entry.
* TODO: this function can be overwritten by the instances
* @param curCell The current cell to be updated.
* @param curAncestor A list of the 4dTraces of the tracebacks at this state.
*/
updateCell: function (curCell, curAncestor) {
// check if something to update
if (curCell === null) {
return;
}
// init value with number of additional base pairs
var curVal = curAncestor.bps.length;
// add scores of ancestor cells
for (var x = 0; x < curAncestor.parents.length; x++) {
curVal += this.getValue(curAncestor.parents[x][0], curAncestor.parents[x][1]);
}
// check if we have to update
if (curCell.value <= curVal) {
// check for new maximal value
if (curCell.value < curVal) {
// reset ancestor list
curCell.traces = [];
// store new maximum
curCell.value = curVal;
}
// store this ancestor
curCell.traces.push(curAncestor);
}
},
/**
* Compute all the cells of the matrix.
* TODO: Implement computeCell
*/
computeAllCells: function() {
for (var i = 0; i <= this.seq_length; ++i) {
for (var j = 0; j <= this.seq_length; ++j) {
this.getCell(i, j);
}
}
},
/**
* Fills the matrix according to the recursion.
* TODO: Implement computeCell
* TODO: this function has to be overwritten by the instances.
*
* @param {input} A Dictionary of the input for the matrix. Should contain all the arguments
* needed for initalizing the matrix input properly. Minimally this will be the sequence.
* @returns {NussinovMatrix} this for call chaining
*/
computeMatrix: function (input) {
console.log("WARNING: computeMatrix() not implemented in NussinovMatrix superclass; overwrite in subclass!");
// resize and init matrix
this.init(input.sequence(), "Default name");
// set minimal loop length
this.minLoopLength = parseInt(input.loopLength());
this.computeAllCells();
return this;
},
/**
* Access to the recursion's representation in LaTeX, that is used in by this matrix.
* @returns {string} latex encoding of the recursion
*/
getRecursionInLatex: function () {
return "$$" + this.latex_representation + "$$";
},
/**
* creates a string representation of the matrix
* @returns {string} matrix as string
*/
toString: function () {
var str = this.minLoopLength + " ";
for (var i = 0; i < this.seq_length; i++) {
str += this.sequence[i] + " ";
}
str += "\n";
for (var i = 1; i <= this.seq_length; i++) {
// print sequence
str += this.sequence[i - 1] + " ";
// print values
for (var j = 0; j <= this.seq_length; j++) {
if (j !== 0) {
str += ", ";
}
str += this.getValue(i, j);
}
str += "\n";
}
return str;
},
/**
* Compute an Sprime instance that can be used wuchty.
* Given a trace of a cell @cell and the value of the trace at this cell @NSprime and the optimal value at this
* cell @Nmax. and returns the SPrime for this trace.
* It assumes that NSprime >= Nmax - delta (The error in this trace is less than the remaining potential error)
* @NSprime: The value of this trace.
* @NMax: The optimal value at this cell.
* @cell: The current cell
* @trace: The current trace being considered
* @sigma: Old sigma to be cloned
* @delta: The remaining error potential
* @P: The basepair structure so far.
* @traces: A list of pairs of cells and their parents.
* @returns S_prime for this trace.
* */
getSPrime: function(NSprime, Nmax, cell, trace, sigma, delta, P, traces) {
var sigma_prime = JSON.stringify(sigma);
sigma_prime = JSON.parse(sigma_prime);
//console.log(trace);
for (var i = 0; i < trace.parents.length; ++i) {
sigma_prime.push(trace.parents[i]);
}
var tmp_P = JSON.stringify(P);
tmp_P = JSON.parse(tmp_P);
for (var i = 0; i < trace.bps.length; ++i) {
tmp_P.push(trace.bps[i]);
}
var tmp_traces = JSON.stringify(traces);
tmp_traces = JSON.parse(tmp_traces);
tmp_traces.unshift([cell, trace.parents]);
var S_prime = {};
S_prime.sigma = sigma_prime;
S_prime.P = tmp_P;
S_prime.traces = tmp_traces;
S_prime.potential = delta - (Nmax - NSprime);
return S_prime;
},
/**
* countBasepairs(for wuchty)
*/
countBasepairs: function (bps, sigma) {
var NSprime = bps.length;
for (var s in sigma) {
var i = sigma[s][0];
var j = sigma[s][1];
NSprime += this.getValue(i, j);
}
return NSprime;
},
/**
* Construct a one string representing the matching base pairs in the sequence.
* @param x A list of pairs of indices, that represent the matching base pairs.
* @returns {string} A representation of the matching base pairs in the matching.
*/
conv_str: function (x, length) {
var str = "";
for (var l = 0; l < length; l++) {
str += ".";
}
var linked = this.sequence.indexOf("X");
if(linked == -1){
for (var i in x) {
str = str.substr(0, x[i][0] - 1) + "(" + str.substr(x[i][0], str.length);
str = str.substr(0, x[i][1] - 1) + ")" + str.substr(x[i][1], str.length);
}
return str;
} else {
for (var i in x) {
if(x[i][0] <= linked && x[i][1] >= linked + 1 + this.minLoopLength) {
str = str.substr(0, x[i][0] - 1) + "[" + str.substr(x[i][0], str.length);
str = str.substr(0, x[i][1] - 1) + "]" + str.substr(x[i][1], str.length);
}
else{
str = str.substr(0, x[i][0] - 1) + "(" + str.substr(x[i][0], str.length);
str = str.substr(0, x[i][1] - 1) + ")" + str.substr(x[i][1], str.length);
}
}
var st = "";
for (var l = 0; l < this.minLoopLength + 1; ++l) {
st += "X";
}
str = str.substr(0, linked) + st + str.substr(linked + this.minLoopLength + 1, str.length);
return str;
}
},
};
/**
* Dynamic programming algorithm.
*
* DP Algorithms will work by memoization usually. If so, there's a function getCell/getValue for each of the tables,
* that computes a value and memoize it if it's not computed(default null cells), and returns the memoized value.
*
* The computation is done through the computeCell in each of the tables, this function should be overriden for each
* table depending on how an entry in the table is computed. The function should include the base case, and should use
* the other tables by the getValue function and not by accessing the tables directly, in order to ensure the
* correctness of the memoization.
*
* The tables should be usable after invoking the computeMatrix method, this method should be overriden to set the tables
* parameters and compute all the dynamic programming. Unless there's a special way to do this, it can usually
* (with memoization) be done by invoking getCell for all the entries of all the tables (or computeAllCells for each table).
*
* TODO: How To Use:
* * Create the DPAlgorithm instance
* * Create new Tables Array, and push the needed tables(NussinovMatrix/NussinovMatrix4d)
* * Override latex_representation for each table.
* * Override computeCell for each table, and/or isInvalidState, and/or updateCell (In case of storing traceback)
* * Remember to use getCell/getValue instead of accessing the cell directly, to preserve the memoization.
* * If a given state satisfies isInvalidState, then it should return an invalid default value. Like INF in
* minimization algorithms, 0 in counting algorithms, and -INF in maximization algorithms.
* * Override getSubstructures (In case of computing tracebacks)
* * Override ComputeMatrix, which is the main interface for computing all the tables.
* * Can use the instance of other algorithms and clone their tables. (Clone also the needed methods like getCell)
* * Calling computeAllCells for each Table should be sufficient most of the time.
*
*
* @type {{Description: Algorithm description,
* Tables: Array of tables for all the recursive formulas,
* computeMatrix: Compute All the Tables
* getRecursionInLatex: Get Latex String describing the recursive equations for all the tables.}}
*/
var DPAlgorithm = {
/**
* Algorithm description.
*/
Description: "Algorithm",
/**
* A list of the Tables NussinovMatrix (or NussinovMatrix4d) for each table used by the DP algorithms.
* Note: Create a new array for each instance.
*/
Tables: [], // create new array
/**
* TODO: Has to be overriden by the instances.
* @param input A dictionary of the input arguments.
*/
computeMatrix: function (input) { },
/**
* @returns {string} An aligned latex array that contains the latex formula of each table(seperated with empty lines).
*/
getRecursionInLatex: function () {
var formula = " \\begin{array} ";
for (var i = 0; i < this.Tables.length; ++i) {
formula += " \\\\ \\\\ " + this.Tables[i].latex_representation;
}
formula += " \\end{array} ";
return formula;
},
};
var NussinovDPAlgorithm_Ambiguous = Object.create(DPAlgorithm);
NussinovDPAlgorithm_Ambiguous.Description = "Ambiguous recursion";
NussinovDPAlgorithm_Ambiguous.Tables = new Array();
NussinovDPAlgorithm_Ambiguous.Tables.push(Object.create(NussinovMatrix));
NussinovDPAlgorithm_Ambiguous.Tables[0].latex_representation = "D(i,j) = \\max \\begin{cases} D(i+1,j) & S_i \\text{ unpaired} \\\\ D(i,j-1) & S_j \\text{ unpaired} \\\\ D(i+1,j-1)+1 & \\text{if } S_i,S_j \\text{ compl. base pair and } i+ l< j \\\\ \\max_{i< k< (j-1)} D(i,k)+D(k+1,j) & \\text{ decomposition} \\end{cases}";
NussinovDPAlgorithm_Ambiguous.Tables[0].computeCell = function(i, j) {
var curCell = Object.create(NussinovCell).init(i, j, 0);
if (this.isInvalidState(i, j)) {
return curCell;
}
// i unpaired
this.updateCell(curCell, Object.create(NussinovCellTrace).init([[i + 1, j]], []));
// j unpaired
this.updateCell(curCell, Object.create(NussinovCellTrace).init([[i, j - 1]], []));
// check (i,j) base pair
if ((j - i > this.minLoopLength) && RnaUtil.areComplementary(this.sequence[i - 1], this.sequence[j - 1])) {
// get value for base pair
this.updateCell(curCell, Object.create(NussinovCellTrace).init([[i + 1, j - 1]], [[i, j]]));
}
// check decomposition into substructures (minLength==2)
for (var k = i + 1; k < (j - 1); k++) {
// get decomposition value
this.updateCell(curCell, Object.create(NussinovCellTrace).init([[i, k], [k + 1, j]], []));
}
return curCell;
};
NussinovDPAlgorithm_Ambiguous.computeMatrix = function (input) {
// resize and initialize matrix
this.Tables[0].init(input.sequence(), "ambiguous");
// store minimal loop length
this.Tables[0].minLoopLength = parseInt(input.loopLength());
this.Tables[0].computeAllCells();
return this.Tables;
};
NussinovDPAlgorithm_Ambiguous.Tables[0].getSubstructures = function (sigma, P, traces, delta, maxLengthR) {
var Nmax = this.getValue(1, this.sequence.length);
var R = [];
var ij = sigma.pop();
//console.log(ij);
// check for sane interval
// if i>j dont continue
if (ij[0] >= ij[1]) {
//console.log("ij[0] > ij[1]", ij[0], ij[1]);
var S_prime = {};
var sigma_prime = JSON.stringify(sigma);
sigma_prime = JSON.parse(sigma_prime);
var P_prime = JSON.stringify(P);
P_prime = JSON.parse(P_prime);
var tmp_traces = JSON.stringify(traces);
tmp_traces = JSON.parse(tmp_traces);
S_prime.sigma = sigma_prime;
S_prime.P = P_prime;
S_prime.traces = tmp_traces;
R.push(S_prime);
//console.log("returning R:", JSON.stringify(R));
return R;
}
// if (i,j) == (i+1,j-1) + bp(ij)
{
if (ij[1] - ij[0] > this.minLoopLength) {
//console.log(this.sequence);
//console.log(this.sequence[ij[0] - 1], this.sequence[ij[1] - 1]);
if (RnaUtil.areComplementary(this.sequence[ij[0] - 1], this.sequence[ij[1] - 1])) {
var sigma_prime = JSON.stringify(sigma);
sigma_prime = JSON.parse(sigma_prime);
sigma_prime.push([ij[0] + 1, ij[1] - 1]);
var P_prime = JSON.stringify(P);
P_prime = JSON.parse(P_prime);
P_prime.push([ij[0], ij[1]]);
var tmp_traces = JSON.stringify(traces);
tmp_traces = JSON.parse(tmp_traces);
var NSprime = this.countBasepairs(P_prime, sigma_prime);
if (NSprime >= Nmax - delta) {
var S_prime = {};
S_prime.sigma = sigma_prime;
S_prime.P = P_prime;
tmp_traces.unshift([ij, [[ij[0] + 1, ij[1] - 1]]]);
S_prime.traces = tmp_traces;
//console.log("i+1,j-1:", JSON.stringify(S_prime));
// push to the front to keep base pair most prominent to refine
R.unshift(S_prime);
}
}
}
// check if enough structures found so far
if (R.length >= maxLengthR) {
//console.log("returning R:", JSON.stringify(R));
return R;
}
}
// if (i,j) == (i+1,j)
{
var sigma_prime = JSON.stringify(sigma);
sigma_prime = JSON.parse(sigma_prime);
sigma_prime.unshift([ij[0] + 1, ij[1]]);
var P_prime = JSON.stringify(P);
P_prime = JSON.parse(P_prime);
var tmp_traces = JSON.stringify(traces);
tmp_traces = JSON.parse(tmp_traces);
var NSprime = this.countBasepairs(P_prime, sigma_prime);
if (NSprime >= Nmax - delta) {
var S_prime = {};
S_prime.sigma = sigma_prime;
S_prime.P = P_prime;
tmp_traces.unshift([ij, [[ij[0] + 1, ij[1]]]]);
S_prime.traces = tmp_traces;
//console.log("i+1,j:", JSON.stringify(S_prime));
// push to the front to keep base pair most prominent to refine
R.unshift(S_prime);
}
// check if enough structures found so far
if (R.length >= maxLengthR) {
//console.log("returning R:", JSON.stringify(R));
return R;
}
}
// if (i,j) == (i,j-1)
{
var sigma_prime = JSON.stringify(sigma);
sigma_prime = JSON.parse(sigma_prime);
sigma_prime.unshift([ij[0], ij[1] - 1]);
var P_prime = JSON.stringify(P);
P_prime = JSON.parse(P_prime);
var tmp_traces = JSON.stringify(traces);
tmp_traces = JSON.parse(tmp_traces);
var NSprime = this.countBasepairs(P_prime, sigma_prime);
if (NSprime >= Nmax - delta) {
var S_prime = {};
S_prime.sigma = sigma_prime;
S_prime.P = P_prime;
tmp_traces.unshift([ij, [[ij[0], ij[1] - 1]]]);
S_prime.traces = tmp_traces;
//console.log("i,j-1:", JSON.stringify(S_prime));
// push to the front to keep base pair most prominent to refine
R.unshift(S_prime);
}
// check if enough structures found so far
if (R.length >= maxLengthR) {
//console.log("returning R:", JSON.stringify(R));
return R;
}
}
// if (i,j) == (i,l) + (l+1, j)
for (var l = ij[0] + 1; l < ij[1] - 1; l++) {
var sigma_prime = JSON.stringify(sigma);
sigma_prime = JSON.parse(sigma_prime);
sigma_prime.push([ij[0], l]);
sigma_prime.push([l + 1, ij[1]]);
var P_prime = JSON.stringify(P);
P_prime = JSON.parse(P_prime);
var tmp_traces = JSON.stringify(traces);
tmp_traces = JSON.parse(tmp_traces);
var NSprime = this.countBasepairs(P_prime, sigma_prime);
if (NSprime >= Nmax - delta) {
var S_prime = {};
S_prime.sigma = sigma_prime;
S_prime.P = P_prime;
tmp_traces.unshift([ij, [[ij[0], l], [l + 1, ij[1]]]]);
S_prime.traces = tmp_traces;
//console.log("ilj:", JSON.stringify(S_prime));
// push to the front to keep base pair most prominent to refine
R.unshift(S_prime);
}
// check if enough structures found so far
if (R.length >= maxLengthR) {
//console.log("returning R:", JSON.stringify(R));
return R;
}
}
//console.log("returning R:", JSON.stringify(R));
return R;
}
;
var NussinovDPAlgorithm_Unique = Object.create(DPAlgorithm);
NussinovDPAlgorithm_Unique.Description = "Recursion by Nussinov et al. (1978) with unique decomposition";
NussinovDPAlgorithm_Unique.Tables = new Array();
NussinovDPAlgorithm_Unique.Tables.push(Object.create(NussinovMatrix));
NussinovDPAlgorithm_Unique.Tables[0].latex_representation = "D(i,j) = \\max \\begin{cases} D(i,j-1) & S_j \\text{ unpaired} \\\\ \\max_{i\\leq k< (j-l)} D(i,k-1)+D(k+1,j-1)+1 & \\text{if } S_k,S_j \\text{ compl. base pair} \\end{cases}";
NussinovDPAlgorithm_Unique.Tables[0].computeCell = function(i, j) {
var curCell = Object.create(NussinovCell).init(i, j, 0);
if (this.isInvalidState(i, j)) {
return curCell;
}
// j unpaired
this.updateCell(curCell, Object.create(NussinovCellTrace).init([[i, j - 1]], []));
// check base pair based decomposition : (k,j) base pair
for (var k = i; k + this.minLoopLength < j; k++) {
// check if sequence positions are compatible
if (RnaUtil.areComplementary(this.sequence[k - 1], this.sequence[j - 1])) {
this.updateCell(curCell, Object.create(NussinovCellTrace).init([[i, k - 1], [k + 1, j - 1]], [[k, j]]));
}
}
return curCell;
};
NussinovDPAlgorithm_Unique.computeMatrix = function (input) {
// resize and initialize matrix
this.Tables[0].init(input.sequence(), "unique");
// store minimal loop length
this.Tables[0].minLoopLength = parseInt(input.loopLength());
this.Tables[0].computeAllCells();
return this.Tables;
};
NussinovDPAlgorithm_Unique.Tables[0].getSubstructures = function (sigma, P, traces, delta, maxLengthR) {
var Nmax = this.getValue(1, this.sequence.length);
var R = [];
var ij = sigma.pop();
// if i>j dont countinue
if (ij[0] > ij[1]) {
//console.log("ij[0] > ij[1]", ij[0], ij[1]);
var S_prime = {};
S_prime.sigma = sigma;
S_prime.P = P;
S_prime.traces = traces;
R.push(S_prime);
//console.log("returning R:", JSON.stringify(R));
return R;
}
// [<[(i, l - 1], (l + 1, j - 1), -1,>, <>, ....]
// if (i,j) == (i,l-1) + (l+1, j-1) + 1
for (var l = ij[0]; l <= ij[1] - 1; l++) {
if (ij[1] - l > this.minLoopLength) {
if (RnaUtil.areComplementary(this.sequence[l - 1], this.sequence[ij[1] - 1])) {
var sigma_prime = JSON.stringify(sigma);
sigma_prime = JSON.parse(sigma_prime);
sigma_prime.push([ij[0], l - 1]);
sigma_prime.push([l + 1, ij[1] - 1]);
var tmp_P = JSON.stringify(P);
tmp_P = JSON.parse(tmp_P);
tmp_P.push([l, ij[1]]);
var tmp_traces = JSON.stringify(traces);
tmp_traces = JSON.parse(tmp_traces);
var NSprime = this.countBasepairs(tmp_P, sigma_prime);
if (NSprime >= Nmax - delta) {
var S_prime = {};
S_prime.sigma = sigma_prime;
S_prime.P = tmp_P;
tmp_traces.unshift([ij, [[ij[0], l - 1], [l + 1, ij[1] - 1]]]);
S_prime.traces = tmp_traces;
//console.log("ilj:", JSON.stringify(S_prime));
// push to the back to keep base pair most prominent to refine
R.push(S_prime);
}
// check if enough structures found so far
if (R.length >= maxLengthR) {
//console.log("returning R:", JSON.stringify(R));
return R;
}
}
}
}
// if (i,j) == (i,j-1)
{
var sigma_prime = JSON.stringify(sigma);
sigma_prime = JSON.parse(sigma_prime);
sigma_prime.unshift([ij[0], ij[1] - 1]);
var tmp_P = JSON.stringify(P);
tmp_P = JSON.parse(tmp_P);
var tmp_traces = JSON.stringify(traces);
tmp_traces = JSON.parse(tmp_traces);
var NSprime = this.countBasepairs(tmp_P, sigma_prime);
if (NSprime >= Nmax - delta) {
var S_prime = {};
S_prime.sigma = sigma_prime;
S_prime.P = tmp_P;
tmp_traces.unshift([ij, [[ij[0], ij[1] - 1]]]);
S_prime.traces = tmp_traces;
//console.log("ij-1:", JSON.stringify(S_prime));
// push to the front to keep base pair most prominent to refine
R.unshift(S_prime);
}
// check if enough structures found so far
if (R.length >= maxLengthR) {
//console.log("returning R:", JSON.stringify(R));
return R;
}
}
//console.log("returning R:", JSON.stringify(R));
return R;
}
/**
* nussinov recursion
* N(i,j) = max(0, N(i+1,j-1)+1 if bp(i,j), max_{i<=k<j} : N(i,k)+N(k+1,j))
* @type {DPAlgorithm}
*/
var NussinovDPAlgorithm_Ambiguous2 = Object.create(DPAlgorithm);
NussinovDPAlgorithm_Ambiguous2.Description = "Recursion by Nussinov et al. (1978) with Ambiguous2 decomposition";
NussinovDPAlgorithm_Ambiguous2.Tables = new Array();
NussinovDPAlgorithm_Ambiguous2.Tables.push(Object.create(NussinovMatrix));
NussinovDPAlgorithm_Ambiguous2.Tables[0].latex_representation = "D(i,j) = \\max \\begin{cases} D(i+1,j-1)+1 & \\text{if } S_i,S_j \\text{ compl. base pair and } i+ l< j \\\\ \\max_{i\\leq k< j} D(i,k)+D(k+1,j) \\end{cases}";
NussinovDPAlgorithm_Ambiguous2.Tables[0].computeCell = function(i, j) {
var curCell = Object.create(NussinovCell).init(i, j, 0);
if (this.isInvalidState(i, j)) {
return curCell;
}
// check (i,j) base pair
if ((j - i > this.minLoopLength) && RnaUtil.areComplementary(this.sequence[i - 1], this.sequence[j - 1])) {
// get value for base pair
this.updateCell(curCell, Object.create(NussinovCellTrace).init([[i + 1, j - 1]], [[i, j]]));
}
// check decomposition into substructures (minLength==2)
for (var k = i; k < j; k++) {
// get decomposition value
this.updateCell(curCell, Object.create(NussinovCellTrace).init([[i, k], [k + 1, j]], []));
}
return curCell;
};
NussinovDPAlgorithm_Ambiguous2.computeMatrix = function (input) {
// resize and initialize matrix
this.Tables[0].init(input.sequence(), "Ambiguous2");
// store minimal loop length
this.Tables[0].minLoopLength = parseInt(input.loopLength());
this.Tables[0].computeAllCells();
return this.Tables;
};
NussinovDPAlgorithm_Ambiguous2.Tables[0].getSubstructures = function (sigma, P, traces, delta, maxLengthR) {
var Nmax = this.getValue(1, this.sequence.length);
var R = [];
var ij = sigma.pop();
//console.log(ij);
// check for sane interval
// if i>j dont continue
if (ij[0] >= ij[1]) {
//console.log("ij[0] > ij[1]", ij[0], ij[1]);
var S_prime = {};
var sigma_prime = JSON.stringify(sigma);
sigma_prime = JSON.parse(sigma_prime);
var tmp_P = JSON.stringify(P);
tmp_P = JSON.parse(tmp_P);
var tmp_traces = JSON.stringify(traces);
tmp_traces = JSON.parse(tmp_traces);
S_prime.sigma = sigma_prime;
S_prime.P = tmp_P;
S_prime.traces = tmp_traces;
R.push(S_prime);
//console.log("returning R:", JSON.stringify(R));
return R;
}
// if (i,j) == (i+1,j-1) + bp(ij)
{
if (ij[1] - ij[0] > this.minLoopLength) {
//console.log(this.sequence);
//console.log(this.sequence[ij[0] - 1], this.sequence[ij[1] - 1]);
if (RnaUtil.areComplementary(this.sequence[ij[0] - 1], this.sequence[ij[1] - 1])) {
var sigma_prime = JSON.stringify(sigma);
sigma_prime = JSON.parse(sigma_prime);
sigma_prime.push([ij[0] + 1, ij[1] - 1]);
var tmp_P = JSON.stringify(P);
tmp_P = JSON.parse(tmp_P);
tmp_P.push([ij[0], ij[1]]);
var tmp_traces = JSON.stringify(traces);
tmp_traces = JSON.parse(tmp_traces);
var NSprime = this.countBasepairs(tmp_P, sigma_prime);
if (NSprime >= Nmax - delta) {
var S_prime = {};
S_prime.sigma = sigma_prime;