1 Clazz.declarePackage ("jalview.analysis");
2 Clazz.load (["jalview.schemes.ResidueProperties", "java.lang.StringBuffer"], "jalview.analysis.AlignSeq", ["jalview.datamodel.Mapping", "$.Sequence", "jalview.util.Comparison", "$.Format", "$.MapList", "$.MessageManager", "java.awt.Color", "java.lang.Error", "$.StringBuilder", "java.util.ArrayList", "$.Arrays", "$.StringTokenizer"], function () {
3 c$ = Clazz.decorateAsClass (function () {
35 this.charToInt = null;
36 Clazz.instantialize (this, arguments);
37 }, jalview.analysis, "AlignSeq");
38 Clazz.prepareFields (c$, function () {
39 this.lookup = jalview.schemes.ResidueProperties.getBLOSUM62 ();
40 this.intToStr = jalview.analysis.AlignSeq.pep;
41 this.output = new StringBuffer ();
43 Clazz.makeConstructor (c$,
44 function (s1, s2, type) {
45 this.SeqInit (s1, s1.getSequenceAsString (), s2, s2.getSequenceAsString (), type);
46 }, "jalview.datamodel.SequenceI,jalview.datamodel.SequenceI,~S");
47 Clazz.makeConstructor (c$,
48 function (s1, string1, s2, string2, type) {
49 this.SeqInit (s1, string1.toUpperCase (), s2, string2.toUpperCase (), type);
50 }, "jalview.datamodel.SequenceI,~S,jalview.datamodel.SequenceI,~S,~S");
51 Clazz.defineMethod (c$, "getMaxScore",
55 Clazz.defineMethod (c$, "getSeq2Start",
57 return this.seq2start;
59 Clazz.defineMethod (c$, "getSeq2End",
63 Clazz.defineMethod (c$, "getSeq1Start",
65 return this.seq1start;
67 Clazz.defineMethod (c$, "getSeq1End",
71 Clazz.defineMethod (c$, "getOutput",
73 return this.output.toString ();
75 Clazz.defineMethod (c$, "getAStr1",
79 Clazz.defineMethod (c$, "getAStr2",
83 Clazz.defineMethod (c$, "getASeq1",
87 Clazz.defineMethod (c$, "getASeq2",
91 Clazz.defineMethod (c$, "getS1",
95 Clazz.defineMethod (c$, "getS2",
99 Clazz.defineMethod (c$, "getAlignedSeq1",
101 var alSeq1 = new jalview.datamodel.Sequence (this.s1.getName (), this.getAStr1 ());
102 alSeq1.setStart (this.s1.getStart () + this.getSeq1Start () - 1);
103 alSeq1.setEnd (this.s1.getStart () + this.getSeq1End () - 1);
104 alSeq1.setDatasetSequence (this.s1.getDatasetSequence () == null ? this.s1 : this.s1.getDatasetSequence ());
107 Clazz.defineMethod (c$, "getAlignedSeq2",
109 var alSeq2 = new jalview.datamodel.Sequence (this.s2.getName (), this.getAStr2 ());
110 alSeq2.setStart (this.s2.getStart () + this.getSeq2Start () - 1);
111 alSeq2.setEnd (this.s2.getStart () + this.getSeq2End () - 1);
112 alSeq2.setDatasetSequence (this.s2.getDatasetSequence () == null ? this.s2 : this.s2.getDatasetSequence ());
115 Clazz.defineMethod (c$, "SeqInit",
116 function (s1, string1, s2, string2, type) {
119 this.setDefaultParams (type);
120 this.SeqInit (string1, string2);
121 }, "jalview.datamodel.SequenceI,~S,jalview.datamodel.SequenceI,~S,~S");
122 Clazz.defineMethod (c$, "SeqInit",
123 function (s1, string1, s2, string2, scoreMatrix) {
126 this.setType (scoreMatrix.isDNA () ? "dna" : "pep");
127 this.lookup = scoreMatrix.getMatrix ();
128 }, "jalview.datamodel.SequenceI,~S,jalview.datamodel.SequenceI,~S,jalview.schemes.ScoreMatrix");
129 Clazz.defineMethod (c$, "SeqInit",
130 function (string1, string2) {
131 this.s1str = jalview.analysis.AlignSeq.extractGaps (jalview.util.Comparison.GapChars, string1);
132 this.s2str = jalview.analysis.AlignSeq.extractGaps (jalview.util.Comparison.GapChars, string2);
133 if (this.s1str.length == 0 || this.s2str.length == 0) {
134 this.output.append ("ALL GAPS: " + (this.s1str.length == 0 ? this.s1.getName () : " ") + (this.s2str.length == 0 ? this.s2.getName () : ""));
136 }this.seq1 = Clazz.newIntArray (this.s1str.length, 0);
137 this.seq2 = Clazz.newIntArray (this.s2str.length, 0);
138 this.score = Clazz.newIntArray (this.s1str.length, this.s2str.length, 0);
139 this.E = Clazz.newIntArray (this.s1str.length, this.s2str.length, 0);
140 this.F = Clazz.newIntArray (this.s1str.length, this.s2str.length, 0);
141 this.traceback = Clazz.newIntArray (this.s1str.length, this.s2str.length, 0);
142 this.seq1 = this.stringToInt (this.s1str, this.type);
143 this.seq2 = this.stringToInt (this.s2str, this.type);
145 Clazz.defineMethod (c$, "setDefaultParams",
148 if (type.equals ("pep")) {
149 this.lookup = jalview.schemes.ResidueProperties.getDefaultPeptideMatrix ();
150 } else if (type.equals ("dna")) {
151 this.lookup = jalview.schemes.ResidueProperties.getDefaultDnaMatrix ();
153 Clazz.defineMethod (c$, "setType",
156 if (this.type.equals ("pep")) {
157 this.intToStr = jalview.analysis.AlignSeq.pep;
158 this.charToInt = jalview.schemes.ResidueProperties.aaIndex;
160 } else if (this.type.equals ("dna")) {
161 this.intToStr = jalview.analysis.AlignSeq.dna;
162 this.charToInt = jalview.schemes.ResidueProperties.nucleotideIndex;
165 this.output.append ("Wrong type = dna or pep only");
166 throw new Error (jalview.util.MessageManager.formatMessage ("error.unknown_type_dna_or_pep", Clazz.newArray (-1, [type2])));
168 Clazz.defineMethod (c$, "traceAlignment",
171 for (var i = 0; i < this.seq1.length; i++) {
172 if (this.score[i][this.seq2.length - 1] > max) {
173 max = this.score[i][this.seq2.length - 1];
175 this.maxj = this.seq2.length - 1;
177 for (var j = 0; j < this.seq2.length; j++) {
178 if (this.score[this.seq1.length - 1][j] > max) {
179 max = this.score[this.seq1.length - 1][j];
180 this.maxi = this.seq1.length - 1;
186 this.maxscore = Clazz.doubleToInt (this.score[i][j] / 10);
187 this.seq1end = this.maxi + 1;
188 this.seq2end = this.maxj + 1;
189 this.aseq1 = Clazz.newIntArray (this.seq1.length + this.seq2.length, 0);
190 this.aseq2 = Clazz.newIntArray (this.seq1.length + this.seq2.length, 0);
191 this.count = (this.seq1.length + this.seq2.length) - 1;
192 while ((i > 0) && (j > 0)) {
193 if ((this.aseq1[this.count] != this.defInt) && (i >= 0)) {
194 this.aseq1[this.count] = this.seq1[i];
195 this.astr1 = this.s1str.charAt (i) + this.astr1;
196 }if ((this.aseq2[this.count] != this.defInt) && (j > 0)) {
197 this.aseq2[this.count] = this.seq2[j];
198 this.astr2 = this.s2str.charAt (j) + this.astr2;
199 }trace = this.findTrace (i, j);
203 } else if (trace == 1) {
205 this.aseq1[this.count] = this.defInt;
206 this.astr1 = "-" + this.astr1.substring (1);
207 } else if (trace == -1) {
209 this.aseq2[this.count] = this.defInt;
210 this.astr2 = "-" + this.astr2.substring (1);
213 this.seq1start = i + 1;
214 this.seq2start = j + 1;
215 if (this.aseq1[this.count] != this.defInt) {
216 this.aseq1[this.count] = this.seq1[i];
217 this.astr1 = this.s1str.charAt (i) + this.astr1;
218 }if (this.aseq2[this.count] != this.defInt) {
219 this.aseq2[this.count] = this.seq2[j];
220 this.astr2 = this.s2str.charAt (j) + this.astr2;
222 Clazz.defineMethod (c$, "printAlignment",
224 var s1id = this.s1.getName ();
225 var s2id = this.s2.getName ();
226 var maxid = this.s1.getName ().length;
227 if (this.s2.getName ().length > maxid) {
228 maxid = this.s2.getName ().length;
231 if (this.s1.getName ().length > maxid) {
232 s1id = this.s1.getName ().substring (0, 30);
233 }if (this.s2.getName ().length > maxid) {
234 s2id = this.s2.getName ().substring (0, 30);
235 }}var len = 72 - maxid - 1;
236 var nochunks = (Clazz.doubleToInt ((this.aseq1.length - this.count) / len)) + 1;
238 this.output.append ("Score = ").append ("" + this.score[this.maxi][this.maxj]).append (jalview.analysis.AlignSeq.NEWLINE);
239 this.output.append ("Length of alignment = ").append (String.valueOf (this.aseq1.length - this.count)).append (jalview.analysis.AlignSeq.NEWLINE);
240 this.output.append ("Sequence ");
241 this.output.append ( new jalview.util.Format ("%" + maxid + "s").form (this.s1.getName ()));
242 this.output.append (" : ").append (String.valueOf (this.s1.getStart ())).append (" - ").append (String.valueOf (this.s1.getEnd ()));
243 this.output.append (" (Sequence length = ").append (String.valueOf (this.s1str.length)).append (")").append (jalview.analysis.AlignSeq.NEWLINE);
244 this.output.append ("Sequence ");
245 this.output.append ( new jalview.util.Format ("%" + maxid + "s").form (this.s2.getName ()));
246 this.output.append (" : ").append (String.valueOf (this.s2.getStart ())).append (" - ").append (String.valueOf (this.s2.getEnd ()));
247 this.output.append (" (Sequence length = ").append (String.valueOf (this.s2str.length)).append (")").append (jalview.analysis.AlignSeq.NEWLINE).append (jalview.analysis.AlignSeq.NEWLINE);
248 for (var j = 0; j < nochunks; j++) {
249 this.output.append ( new jalview.util.Format ("%" + (maxid) + "s").form (s1id)).append (" ");
250 for (var i = 0; i < len; i++) {
251 if ((i + (j * len)) < this.astr1.length) {
252 this.output.append (this.astr1.charAt (i + (j * len)));
254 this.output.append (jalview.analysis.AlignSeq.NEWLINE);
255 this.output.append ( new jalview.util.Format ("%" + (maxid) + "s").form (" ")).append (" ");
256 for (var i = 0; i < len; i++) {
257 if ((i + (j * len)) < this.astr1.length) {
258 if (this.astr1.charAt (i + (j * len)) == this.astr2.charAt (i + (j * len)) && !jalview.util.Comparison.isGap (this.astr1.charAt (i + (j * len)))) {
260 this.output.append ("|");
261 } else if (this.type.equals ("pep")) {
262 if (jalview.schemes.ResidueProperties.getPAM250 (this.astr1.charAt (i + (j * len)), this.astr2.charAt (i + (j * len))) > 0) {
263 this.output.append (".");
265 this.output.append (" ");
267 this.output.append (" ");
269 this.output = this.output.append (jalview.analysis.AlignSeq.NEWLINE);
270 this.output = this.output.append ( new jalview.util.Format ("%" + (maxid) + "s").form (s2id)).append (" ");
271 for (var i = 0; i < len; i++) {
272 if ((i + (j * len)) < this.astr2.length) {
273 this.output.append (this.astr2.charAt (i + (j * len)));
275 this.output.append (jalview.analysis.AlignSeq.NEWLINE).append (jalview.analysis.AlignSeq.NEWLINE);
277 this.pid = this.pid / (this.aseq1.length - this.count) * 100;
278 this.output = this.output.append ( new jalview.util.Format ("Percentage ID = %2.2f\n\n").formDouble (this.pid));
280 os.print (this.output.toString ());
282 if (Clazz.exceptionOf (ex, Exception)) {
287 }, "java.io.PrintStream");
288 Clazz.defineMethod (c$, "printScoreMatrix",
290 var n = this.seq1.length;
291 var m = this.seq2.length;
292 for (var i = 0; i < n; i++) {
294 jalview.util.Format.print (System.out, "%8s", this.s2str.substring (0, 1));
295 for (var jj = 1; jj < m; jj++) {
296 jalview.util.Format.print (System.out, "%5s", this.s2str.substring (jj, jj + 1));
298 System.out.println ();
299 }for (var j = 0; j < m; j++) {
301 jalview.util.Format.print (System.out, "%3s", this.s1str.substring (i, i + 1));
302 }jalview.util.Format.printLong (System.out, "%3d ", Clazz.doubleToInt (mat[i][j] / 10));
304 System.out.println ();
307 Clazz.defineMethod (c$, "findTrace",
310 var max = this.score[i - 1][j - 1] + (this.lookup[this.seq1[i]][this.seq2[j]] * 10);
311 if (this.F[i][j] > max) {
314 } else if (this.F[i][j] == max) {
315 if (this.prev == -1) {
318 }}if (this.E[i][j] >= max) {
321 } else if (this.E[i][j] == max) {
322 if (this.prev == 1) {
328 Clazz.defineMethod (c$, "calcScoreMatrix",
330 var n = this.seq1.length;
331 var m = this.seq2.length;
332 this.score[0][0] = this.lookup[this.seq1[0]][this.seq2[0]] * 10;
333 this.E[0][0] = -this.gapExtend;
335 for (var j = 1; j < m; j++) {
336 this.E[0][j] = this.max (this.score[0][j - 1] - this.gapOpen, this.E[0][j - 1] - this.gapExtend);
337 this.F[0][j] = -this.gapExtend;
338 this.score[0][j] = this.max (this.lookup[this.seq1[0]][this.seq2[j]] * 10, -this.gapOpen, -this.gapExtend);
339 this.traceback[0][j] = 1;
341 for (var i = 1; i < n; i++) {
342 this.E[i][0] = -this.gapOpen;
343 this.F[i][0] = this.max (this.score[i - 1][0] - this.gapOpen, this.F[i - 1][0] - this.gapExtend);
344 this.score[i][0] = this.max (this.lookup[this.seq1[i]][this.seq2[0]] * 10, this.E[i][0], this.F[i][0]);
345 this.traceback[i][0] = -1;
347 for (var i = 1; i < n; i++) {
348 for (var j = 1; j < m; j++) {
349 this.E[i][j] = this.max (this.score[i][j - 1] - this.gapOpen, this.E[i][j - 1] - this.gapExtend);
350 this.F[i][j] = this.max (this.score[i - 1][j] - this.gapOpen, this.F[i - 1][j] - this.gapExtend);
351 this.score[i][j] = this.max (this.score[i - 1][j - 1] + (this.lookup[this.seq1[i]][this.seq2[j]] * 10), this.E[i][j], this.F[i][j]);
352 this.traceback[i][j] = this.findTrace (i, j);
356 c$.extractGaps = Clazz.defineMethod (c$, "extractGaps",
357 function (gapChars, seq) {
358 if (gapChars == null || seq == null) {
360 }var str = new java.util.StringTokenizer (seq, gapChars);
361 var newString = new StringBuilder (seq.length);
362 while (str.hasMoreTokens ()) {
363 newString.append (str.nextToken ());
365 return newString.toString ();
367 Clazz.defineMethod (c$, "max",
368 function (i1, i2, i3) {
376 Clazz.defineMethod (c$, "max",
383 Clazz.defineMethod (c$, "stringToInt",
385 var seq1 = Clazz.newIntArray (s.length, 0);
386 for (var i = 0; i < s.length; i++) {
387 var c = s.charAt (i);
388 if ('a' <= c && c <= 'z') {
389 c = String.fromCharCode (c.charCodeAt (0) - (32));
391 seq1[i] = this.charToInt[c.charCodeAt (0)];
392 if (seq1[i] < 0 || seq1[i] > this.defInt) {
393 seq1[i] = this.defInt;
395 if (Clazz.exceptionOf (e, Exception)) {
396 seq1[i] = this.defInt;
404 c$.displayMatrix = Clazz.defineMethod (c$, "displayMatrix",
405 function (g, mat, n, m, psize) {
408 for (var i = 0; i < n; i++) {
409 for (var j = 0; j < m; j++) {
410 if (mat[i][j] >= max) {
412 }if (mat[i][j] <= min) {
416 System.out.println (max + " " + min);
417 for (var i = 0; i < n; i++) {
418 for (var j = 0; j < m; j++) {
421 var score = (mat[i][j] - min) / (max - min);
422 g.setColor ( new java.awt.Color (score, 0, 0));
423 g.fillRect (x, y, psize, psize);
426 }, "java.awt.Graphics,~A,~N,~N,~N");
427 c$.doGlobalNWAlignment = Clazz.defineMethod (c$, "doGlobalNWAlignment",
428 function (s1, s2, type) {
429 var as = new jalview.analysis.AlignSeq (s1, s2, type);
430 as.calcScoreMatrix ();
431 as.traceAlignment ();
433 }, "jalview.datamodel.SequenceI,jalview.datamodel.SequenceI,~S");
434 Clazz.defineMethod (c$, "getMappingFromS1",
435 function (allowmismatch) {
436 var as1 = new java.util.ArrayList ();
437 var as2 = new java.util.ArrayList ();
438 var pdbpos = this.s2.getStart () + this.getSeq2Start () - 2;
439 var alignpos = this.s1.getStart () + this.getSeq1Start () - 2;
440 var lp2 = pdbpos - 3;
441 var lp1 = alignpos - 3;
442 var lastmatch = false;
443 for (var i = 0; i < this.astr1.length; i++) {
444 var c1 = this.astr1.charAt (i);
445 var c2 = this.astr2.charAt (i);
450 }if (allowmismatch || c1 == c2) {
451 if (lp1 + 1 != alignpos || lp2 + 1 != pdbpos) {
452 as1.add (Integer.$valueOf (alignpos));
453 as2.add (Integer.$valueOf (pdbpos));
459 as1.add (Integer.$valueOf (lp1));
460 as2.add (Integer.$valueOf (lp2));
463 var mapseq1 = Clazz.newIntArray (as1.size () + (lastmatch ? 1 : 0), 0);
464 var mapseq2 = Clazz.newIntArray (as2.size () + (lastmatch ? 1 : 0), 0);
466 for (var ip, $ip = as1.iterator (); $ip.hasNext () && ((ip = $ip.next ()) || true);) {
467 mapseq1[i++] = (ip).intValue ();
470 for (var ip, $ip = as2.iterator (); $ip.hasNext () && ((ip = $ip.next ()) || true);) {
471 mapseq2[i++] = (ip).intValue ();
474 mapseq1[mapseq1.length - 1] = alignpos;
475 mapseq2[mapseq2.length - 1] = pdbpos;
476 }var map = new jalview.util.MapList (mapseq1, mapseq2, 1, 1);
477 var mapping = new jalview.datamodel.Mapping (map);
478 mapping.setTo (this.s2);
481 c$.replaceMatchingSeqsWith = Clazz.defineMethod (c$, "replaceMatchingSeqsWith",
482 function (seqs, annotations, ochains, al, dnaOrProtein, removeOldAnnots) {
483 var orig = new java.util.ArrayList ();
484 var repl = new java.util.ArrayList ();
485 var aligs = new java.util.ArrayList ();
486 if (al != null && al.getHeight () > 0) {
487 var matches = new java.util.ArrayList ();
488 var aligns = new java.util.ArrayList ();
489 for (var sq, $sq = ochains.iterator (); $sq.hasNext () && ((sq = $sq.next ()) || true);) {
493 for (var msq, $msq = al.getSequences ().iterator (); $msq.hasNext () && ((msq = $msq.next ()) || true);) {
494 var aseq = jalview.analysis.AlignSeq.doGlobalNWAlignment (msq, sq, dnaOrProtein);
495 if (bestm == null || aseq.getMaxScore () > bestscore) {
496 bestscore = aseq.getMaxScore ();
500 System.out.println ("Best Score for " + (matches.size () + 1) + " :" + bestscore);
502 aligns.add (bestaseq);
503 al.deleteSequence (bestm);
505 for (var p = 0, pSize = seqs.size (); p < pSize; p++) {
507 var sp = seqs.get (p);
509 if ((q = ochains.indexOf (sp)) > -1) {
510 seqs.set (p, sq = matches.get (q));
513 sq.setName (sp.getName ());
514 sq.setDescription (sp.getDescription ());
516 sq.transferAnnotation (sp, sp2sq = aligns.get (q).getMappingFromS1 (false));
517 aligs.add (aligns.get (q));
519 for (var ap = 0; ap < annotations.size (); ) {
520 if (annotations.get (ap).sequenceRef === sp) {
523 }if (removeOldAnnots) {
524 annotations.remove (ap);
526 var alan = annotations.remove (ap);
527 alan.liftOver (sq, sp2sq);
528 alan.setSequenceRef (sq);
529 sq.addAlignmentAnnotation (alan);
533 if (sq.getAnnotation () != null && sq.getAnnotation ().length > 0) {
534 annotations.addAll (inspos == -1 ? annotations.size () : inspos, java.util.Arrays.asList (sq.getAnnotation ()));
536 }return java.util.Arrays.asList ([orig, repl, aligs]);
537 }, "java.util.List,java.util.List,java.util.List,jalview.datamodel.AlignmentI,~S,~B");
538 c$.computeRedundancyMatrix = Clazz.defineMethod (c$, "computeRedundancyMatrix",
539 function (originalSequences, omitHidden, start, end, ungapped) {
540 var height = originalSequences.length;
541 var redundancy = Clazz.newFloatArray (height, 0);
542 var lngth = Clazz.newIntArray (height, 0);
543 for (var i = 0; i < height; i++) {
550 for (var i = 0; i < height; i++) {
551 for (var j = 0; j < i; j++) {
554 }if (omitHidden == null) {
555 seqi = originalSequences[i].getSequenceAsString (start, end);
556 seqj = originalSequences[j].getSequenceAsString (start, end);
558 seqi = omitHidden[i];
559 seqj = omitHidden[j];
560 }if (lngth[i] == -1) {
561 var ug = jalview.analysis.AlignSeq.extractGaps (jalview.util.Comparison.GapChars, seqi);
562 lngth[i] = ug.length;
565 }}if (lngth[j] == -1) {
566 var ug = jalview.analysis.AlignSeq.extractGaps (jalview.util.Comparison.GapChars, seqj);
567 lngth[j] = ug.length;
570 }}pid = jalview.util.Comparison.PID (seqi, seqj);
571 if (lngth[j] < lngth[i]) {
572 redundancy[j] = Math.max (pid, redundancy[j]);
574 redundancy[i] = Math.max (pid, redundancy[i]);
578 }, "~A,~A,~N,~N,~B");
579 Clazz.defineStatics (c$,
582 c$.NEWLINE = c$.prototype.NEWLINE = System.lineSeparator ();
583 Clazz.defineStatics (c$,
584 "dna", Clazz.newArray (-1, ["A", "C", "G", "T", "-"]),
585 "pep", Clazz.newArray (-1, ["A", "R", "N", "D", "C", "Q", "E", "G", "H", "I", "L", "K", "M", "F", "P", "S", "T", "W", "Y", "V", "B", "Z", "X", "-"]));