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