JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / site / j2s / jalview / analysis / Dna.js
1 Clazz.declarePackage ("jalview.analysis");\r
2 Clazz.load (["jalview.analysis.CodonComparator"], "jalview.analysis.Dna", ["jalview.datamodel.AlignedCodon", "$.AlignedCodonFrame", "$.Alignment", "$.AlignmentAnnotation", "$.Annotation", "$.DBRefSource", "$.FeatureProperties", "$.GraphLine", "$.Sequence", "jalview.schemes.ResidueProperties", "jalview.util.Comparison", "$.DBRefUtils", "$.MapList", "$.ShiftList", "java.lang.IllegalStateException", "$.StringBuilder", "java.util.ArrayList", "$.Arrays"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.selection = null;\r
5 this.seqstring = null;\r
6 this.contigs = null;\r
7 this.gapChar = '\0';\r
8 this.annotations = null;\r
9 this.dnaWidth = 0;\r
10 this.dataset = null;\r
11 this.aaWidth = 0;\r
12 this.alignedCodons = null;\r
13 Clazz.instantialize (this, arguments);\r
14 }, jalview.analysis, "Dna");\r
15 Clazz.makeConstructor (c$, \r
16 function (viewport, visibleContigs) {\r
17 this.selection = java.util.Arrays.asList (viewport.getSequenceSelection ());\r
18 this.seqstring = viewport.getViewAsString (true);\r
19 this.contigs = visibleContigs;\r
20 this.gapChar = viewport.getGapCharacter ();\r
21 this.annotations = viewport.getAlignment ().getAlignmentAnnotation ();\r
22 this.dnaWidth = viewport.getAlignment ().getWidth ();\r
23 this.dataset = viewport.getAlignment ().getDataset ();\r
24 }, "jalview.api.AlignViewportI,~A");\r
25 c$.compareCodonPos = Clazz.defineMethod (c$, "compareCodonPos", \r
26 function (ac1, ac2) {\r
27 return jalview.analysis.Dna.comparator.compare (ac1, ac2);\r
28 }, "jalview.datamodel.AlignedCodon,jalview.datamodel.AlignedCodon");\r
29 Clazz.defineMethod (c$, "translateCdna", \r
30 function () {\r
31 var acf =  new jalview.datamodel.AlignedCodonFrame ();\r
32 this.alignedCodons =  new Array (this.dnaWidth);\r
33 var s;\r
34 var sSize = this.selection.size ();\r
35 var pepseqs =  new java.util.ArrayList ();\r
36 for (s = 0; s < sSize; s++) {\r
37 var newseq = this.translateCodingRegion (this.selection.get (s), this.seqstring[s], acf, pepseqs);\r
38 if (newseq != null) {\r
39 pepseqs.add (newseq);\r
40 var ds = newseq;\r
41 if (this.dataset != null) {\r
42 while (ds.getDatasetSequence () != null) {\r
43 ds = ds.getDatasetSequence ();\r
44 }\r
45 this.dataset.addSequence (ds);\r
46 }}}\r
47 var newseqs = pepseqs.toArray ( new Array (pepseqs.size ()));\r
48 var al =  new jalview.datamodel.Alignment (newseqs);\r
49 al.padGaps ();\r
50 al.setDataset (this.dataset);\r
51 this.translateAlignedAnnotations (al, acf);\r
52 al.addCodonFrame (acf);\r
53 return al;\r
54 });\r
55 c$.canTranslate = Clazz.defineMethod (c$, "canTranslate", \r
56 function (selection, viscontigs) {\r
57 for (var gd = 0; gd < selection.length; gd++) {\r
58 var dna = selection[gd];\r
59 var dnarefs = jalview.util.DBRefUtils.selectRefs (dna.getDBRef (), jalview.datamodel.DBRefSource.DNACODINGDBS);\r
60 if (dnarefs != null) {\r
61 var mappedrefs =  new java.util.ArrayList ();\r
62 var refs = dna.getDBRef ();\r
63 for (var d = 0; d < refs.length; d++) {\r
64 if (refs[d].getMap () != null && refs[d].getMap ().getMap () != null && refs[d].getMap ().getMap ().getFromRatio () == 3 && refs[d].getMap ().getMap ().getToRatio () == 1) {\r
65 mappedrefs.add (refs[d]);\r
66 }}\r
67 dnarefs = mappedrefs.toArray ( new Array (mappedrefs.size ()));\r
68 for (var d = 0; d < dnarefs.length; d++) {\r
69 var mp = dnarefs[d].getMap ();\r
70 if (mp != null) {\r
71 for (var vc = 0; vc < viscontigs.length; vc += 2) {\r
72 var mpr = mp.locateMappedRange (viscontigs[vc], viscontigs[vc + 1]);\r
73 if (mpr != null) {\r
74 return true;\r
75 }}\r
76 }}\r
77 }}\r
78 return false;\r
79 }, "~A,~A");\r
80 Clazz.defineMethod (c$, "translateAlignedAnnotations", \r
81 function (al, acf) {\r
82 if (this.annotations != null) {\r
83 for (var annotation, $annotation = 0, $$annotation = this.annotations; $annotation < $$annotation.length && ((annotation = $$annotation[$annotation]) || true); $annotation++) {\r
84 if (annotation.autoCalculated || !annotation.visible || annotation.isRNA ()) {\r
85 continue;\r
86 }var aSize = this.aaWidth;\r
87 var anots = (annotation.annotations == null) ? null :  new Array (aSize);\r
88 if (anots != null) {\r
89 for (var a = 0; a < aSize; a++) {\r
90 if (a < this.alignedCodons.length && this.alignedCodons[a] != null && this.alignedCodons[a].pos1 == (this.alignedCodons[a].pos3 - 2)) {\r
91 anots[a] = jalview.analysis.Dna.getCodonAnnotation (this.alignedCodons[a], annotation.annotations);\r
92 }}\r
93 }var aa =  new jalview.datamodel.AlignmentAnnotation (annotation.label, annotation.description, anots);\r
94 aa.graph = annotation.graph;\r
95 aa.graphGroup = annotation.graphGroup;\r
96 aa.graphHeight = annotation.graphHeight;\r
97 if (annotation.getThreshold () != null) {\r
98 aa.setThreshold ( new jalview.datamodel.GraphLine (annotation.getThreshold ()));\r
99 }if (annotation.$hasScore) {\r
100 aa.setScore (annotation.getScore ());\r
101 }var seqRef = annotation.sequenceRef;\r
102 if (seqRef != null) {\r
103 var aaSeq = acf.getAaForDnaSeq (seqRef);\r
104 if (aaSeq != null) {\r
105 aa.setSequenceRef (aaSeq);\r
106 aa.createSequenceMapping (aaSeq, aaSeq.getStart (), true);\r
107 aa.adjustForAlignment ();\r
108 aaSeq.addAlignmentAnnotation (aa);\r
109 }}al.addAnnotation (aa);\r
110 }\r
111 }}, "jalview.datamodel.AlignmentI,jalview.datamodel.AlignedCodonFrame");\r
112 c$.getCodonAnnotation = Clazz.defineMethod (c$, "getCodonAnnotation", \r
113  function (is, annotations) {\r
114 var contrib = 0;\r
115 var annot = null;\r
116 for (var p = 1; p <= 3; p++) {\r
117 var dnaCol = is.getBaseColumn (p);\r
118 if (annotations[dnaCol] != null) {\r
119 if (annot == null) {\r
120 annot =  new jalview.datamodel.Annotation (annotations[dnaCol]);\r
121 contrib = 1;\r
122 } else {\r
123 var cpy =  new jalview.datamodel.Annotation (annotations[dnaCol]);\r
124 if (annot.colour == null) {\r
125 annot.colour = cpy.colour;\r
126 }if (annot.description == null || annot.description.length == 0) {\r
127 annot.description = cpy.description;\r
128 }if (annot.displayCharacter == null) {\r
129 annot.displayCharacter = cpy.displayCharacter;\r
130 }if (annot.secondaryStructure.charCodeAt (0) == 0) {\r
131 annot.secondaryStructure = cpy.secondaryStructure;\r
132 }annot.value += cpy.value;\r
133 contrib++;\r
134 }}}\r
135 if (contrib > 1) {\r
136 annot.value /= contrib;\r
137 }return annot;\r
138 }, "jalview.datamodel.AlignedCodon,~A");\r
139 Clazz.defineMethod (c$, "translateCodingRegion", \r
140 function (selection, seqstring, acf, proteinSeqs) {\r
141 var skip =  new java.util.ArrayList ();\r
142 var skipint = null;\r
143 var vismapping =  new jalview.util.ShiftList ();\r
144 var vc;\r
145 var scontigs =  Clazz.newIntArray (this.contigs.length, 0);\r
146 var npos = 0;\r
147 for (vc = 0; vc < this.contigs.length; vc += 2) {\r
148 if (vc == 0) {\r
149 vismapping.addShift (npos, this.contigs[vc]);\r
150 } else {\r
151 vismapping.addShift (npos, this.contigs[vc] - this.contigs[vc - 1] + 1);\r
152 }scontigs[vc] = this.contigs[vc];\r
153 scontigs[vc + 1] = this.contigs[vc + 1];\r
154 }\r
155 var protein =  new StringBuilder (Clazz.doubleToInt (seqstring.length / 2));\r
156 var seq = seqstring.$replace ('U', 'T').$replace ('u', 'T');\r
157 var codon =  Clazz.newCharArray (3, '\0');\r
158 var cdp =  Clazz.newIntArray (3, 0);\r
159 var rf = 0;\r
160 var lastnpos = 0;\r
161 var nend;\r
162 var aspos = 0;\r
163 var resSize = 0;\r
164 for (npos = 0, nend = seq.length; npos < nend; npos++) {\r
165 if (!jalview.util.Comparison.isGap (seq.charAt (npos))) {\r
166 cdp[rf] = npos;\r
167 codon[rf++] = seq.charAt (npos);\r
168 }if (rf == 3) {\r
169 var alignedCodon =  new jalview.datamodel.AlignedCodon (cdp[0], cdp[1], cdp[2]);\r
170 var aa = jalview.schemes.ResidueProperties.codonTranslate ( String.instantialize (codon));\r
171 rf = 0;\r
172 var gapString = String.valueOf (this.gapChar);\r
173 if (aa == null) {\r
174 aa = gapString;\r
175 if (skipint == null) {\r
176 skipint =  Clazz.newIntArray (-1, [alignedCodon.pos1, alignedCodon.pos3]);\r
177 }skipint[1] = alignedCodon.pos3;\r
178 } else {\r
179 if (skipint != null) {\r
180 skipint[0] = vismapping.shift (skipint[0]);\r
181 skipint[1] = vismapping.shift (skipint[1]);\r
182 for (vc = 0; vc < scontigs.length; ) {\r
183 if (scontigs[vc + 1] < skipint[0]) {\r
184 vc += 2;\r
185 continue;\r
186 }if (scontigs[vc] > skipint[1]) {\r
187 break;\r
188 }var t;\r
189 if (scontigs[vc] <= skipint[0]) {\r
190 if (skipint[0] == scontigs[vc]) {\r
191 if (scontigs[vc + 1] > skipint[1]) {\r
192 scontigs[vc] = skipint[1];\r
193 vc += 2;\r
194 } else {\r
195 if (scontigs[vc + 1] == skipint[1]) {\r
196 t =  Clazz.newIntArray (scontigs.length - 2, 0);\r
197 if (vc > 0) {\r
198 System.arraycopy (scontigs, 0, t, 0, vc - 1);\r
199 }if (vc + 2 < t.length) {\r
200 System.arraycopy (scontigs, vc + 2, t, vc, t.length - vc + 2);\r
201 }scontigs = t;\r
202 } else {\r
203 scontigs[vc + 1] = skipint[0] - 1;\r
204 vc += 2;\r
205 }}} else {\r
206 if (scontigs[vc + 1] < skipint[1]) {\r
207 scontigs[vc + 1] = skipint[0] - 1;\r
208 vc += 2;\r
209 } else {\r
210 t =  Clazz.newIntArray (scontigs.length + 2, 0);\r
211 System.arraycopy (scontigs, 0, t, 0, vc + 1);\r
212 t[vc + 1] = skipint[0];\r
213 t[vc + 2] = skipint[1];\r
214 System.arraycopy (scontigs, vc + 1, t, vc + 3, scontigs.length - (vc + 1));\r
215 scontigs = t;\r
216 vc += 4;\r
217 }}}}\r
218 skip.add (skipint);\r
219 skipint = null;\r
220 }if (aa.equals ("STOP")) {\r
221 aa = "X";\r
222 }resSize++;\r
223 }var findpos = true;\r
224 while (findpos) {\r
225 var compareCodonPos = jalview.analysis.Dna.compareCodonPos (alignedCodon, this.alignedCodons[aspos]);\r
226 switch (compareCodonPos) {\r
227 case -1:\r
228 this.insertAAGap (aspos, proteinSeqs);\r
229 findpos = false;\r
230 break;\r
231 case 1:\r
232 aa = gapString + aa;\r
233 aspos++;\r
234 break;\r
235 case 0:\r
236 findpos = false;\r
237 }\r
238 }\r
239 protein.append (aa);\r
240 lastnpos = npos;\r
241 if (this.alignedCodons[aspos] == null) {\r
242 this.alignedCodons[aspos] = alignedCodon;\r
243 } else if (!this.alignedCodons[aspos].equals (alignedCodon)) {\r
244 throw  new IllegalStateException ("Tried to coalign " + this.alignedCodons[aspos].toString () + " with " + alignedCodon.toString ());\r
245 }if (aspos >= this.aaWidth) {\r
246 this.aaWidth = aspos;\r
247 }aspos++;\r
248 }}\r
249 if (resSize > 0) {\r
250 var newseq =  new jalview.datamodel.Sequence (selection.getName (), protein.toString ());\r
251 if (rf != 0) {\r
252 var errMsg = "trimming contigs for incomplete terminal codon.";\r
253 System.err.println ("trimming contigs for incomplete terminal codon.");\r
254 vc = scontigs.length - 1;\r
255 lastnpos = vismapping.shift (lastnpos);\r
256 while (vc >= 0 && scontigs[vc] > lastnpos) {\r
257 if (vc > 0 && scontigs[vc - 1] > lastnpos) {\r
258 vc -= 2;\r
259 } else {\r
260 scontigs[vc] = lastnpos;\r
261 }}\r
262 if (vc > 0 && (vc + 1) < scontigs.length) {\r
263 var t =  Clazz.newIntArray (vc + 1, 0);\r
264 System.arraycopy (scontigs, 0, t, 0, vc + 1);\r
265 scontigs = t;\r
266 }if (vc <= 0) {\r
267 scontigs = null;\r
268 }}if (scontigs != null) {\r
269 npos = 0;\r
270 for (vc = 0; vc < scontigs.length; vc += 2) {\r
271 scontigs[vc] = selection.findPosition (scontigs[vc]);\r
272 scontigs[vc + 1] = selection.findPosition (scontigs[vc + 1]);\r
273 if (scontigs[vc + 1] == selection.getEnd ()) {\r
274 break;\r
275 }}\r
276 if ((vc + 2) < scontigs.length) {\r
277 var t =  Clazz.newIntArray (vc + 2, 0);\r
278 System.arraycopy (scontigs, 0, t, 0, vc + 2);\r
279 scontigs = t;\r
280 }var map =  new jalview.util.MapList (scontigs,  Clazz.newIntArray (-1, [1, resSize]), 3, 1);\r
281 jalview.analysis.Dna.transferCodedFeatures (selection, newseq, map, null, null);\r
282 var rseq = newseq.deriveSequence ();\r
283 acf.addMap (selection, rseq, map);\r
284 return rseq;\r
285 }}return null;\r
286 }, "jalview.datamodel.SequenceI,~S,jalview.datamodel.AlignedCodonFrame,java.util.List");\r
287 Clazz.defineMethod (c$, "insertAAGap", \r
288 function (pos, proteinSeqs) {\r
289 this.aaWidth++;\r
290 for (var seq, $seq = proteinSeqs.iterator (); $seq.hasNext () && ((seq = $seq.next ()) || true);) {\r
291 seq.insertCharAt (pos, this.gapChar);\r
292 }\r
293 this.checkCodonFrameWidth ();\r
294 if (pos < this.aaWidth) {\r
295 this.aaWidth++;\r
296 System.arraycopy (this.alignedCodons, pos, this.alignedCodons, pos + 1, this.alignedCodons.length - pos - 1);\r
297 this.alignedCodons[pos] = null;\r
298 }}, "~N,java.util.List");\r
299 Clazz.defineMethod (c$, "checkCodonFrameWidth", \r
300 function () {\r
301 if (this.alignedCodons[this.alignedCodons.length - 1] != null) {\r
302 var c =  new Array (this.alignedCodons.length + 10);\r
303 System.arraycopy (this.alignedCodons, 0, c, 0, this.alignedCodons.length);\r
304 this.alignedCodons = c;\r
305 }});\r
306 c$.transferCodedFeatures = Clazz.defineMethod (c$, "transferCodedFeatures", \r
307  function (dna, pep, map, featureTypes, featureGroups) {\r
308 var sfs = dna.getSequenceFeatures ();\r
309 var fgstate;\r
310 var dnarefs = jalview.util.DBRefUtils.selectRefs (dna.getDBRef (), jalview.datamodel.DBRefSource.DNACODINGDBS);\r
311 if (dnarefs != null) {\r
312 for (var d = 0; d < dnarefs.length; d++) {\r
313 var mp = dnarefs[d].getMap ();\r
314 if (mp != null) {\r
315 }}\r
316 }if (sfs != null) {\r
317 for (var sf, $sf = 0, $$sf = sfs; $sf < $$sf.length && ((sf = $$sf[$sf]) || true); $sf++) {\r
318 fgstate = (featureGroups == null) ? null : featureGroups.get (sf.featureGroup);\r
319 if ((featureTypes == null || featureTypes.containsKey (sf.getType ())) && (fgstate == null || fgstate.booleanValue ())) {\r
320 if (jalview.datamodel.FeatureProperties.isCodingFeature (null, sf.getType ())) {\r
321 {\r
322 }}}}\r
323 }}, "jalview.datamodel.SequenceI,jalview.datamodel.SequenceI,jalview.util.MapList,java.util.Map,java.util.Map");\r
324 Clazz.defineStatics (c$,\r
325 "STOP_X", "X");\r
326 c$.comparator = c$.prototype.comparator =  new jalview.analysis.CodonComparator ();\r
327 });\r