Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / jalview / util / MapList.js
1 Clazz.declarePackage ("jalview.util");
2 Clazz.load (["java.util.ArrayList"], "jalview.util.MapList", ["java.lang.StringBuilder", "java.util.Arrays"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.fromShifts = null;
5 this.toShifts = null;
6 this.fromRatio = 0;
7 this.toRatio = 0;
8 this.fromLowest = 0;
9 this.fromHighest = 0;
10 this.toLowest = 0;
11 this.toHighest = 0;
12 Clazz.instantialize (this, arguments);
13 }, jalview.util, "MapList");
14 Clazz.prepareFields (c$, function () {
15 this.fromShifts =  new java.util.ArrayList ();
16 this.toShifts =  new java.util.ArrayList ();
17 });
18 Clazz.overrideMethod (c$, "equals", 
19 function (o) {
20 if (o == null || !(Clazz.instanceOf (o, jalview.util.MapList))) {
21 return false;
22 }var obj = o;
23 if (obj === this) {
24 return true;
25 }if (obj.fromRatio != this.fromRatio || obj.toRatio != this.toRatio || obj.fromShifts == null || obj.toShifts == null) {
26 return false;
27 }return java.util.Arrays.deepEquals (this.fromShifts.toArray (), obj.fromShifts.toArray ()) && java.util.Arrays.deepEquals (this.toShifts.toArray (), obj.toShifts.toArray ());
28 }, "~O");
29 Clazz.defineMethod (c$, "getFromRanges", 
30 function () {
31 return this.fromShifts;
32 });
33 Clazz.defineMethod (c$, "getToRanges", 
34 function () {
35 return this.toShifts;
36 });
37 c$.getRanges = Clazz.defineMethod (c$, "getRanges", 
38 function (shifts) {
39 var rnges =  Clazz.newIntArray (2 * shifts.size (), 0);
40 var i = 0;
41 for (var r, $r = shifts.iterator (); $r.hasNext () && ((r = $r.next ()) || true);) {
42 rnges[i++] = r[0];
43 rnges[i++] = r[1];
44 }
45 return rnges;
46 }, "java.util.List");
47 Clazz.defineMethod (c$, "getFromRatio", 
48 function () {
49 return this.fromRatio;
50 });
51 Clazz.defineMethod (c$, "getToRatio", 
52 function () {
53 return this.toRatio;
54 });
55 Clazz.defineMethod (c$, "getFromLowest", 
56 function () {
57 return this.fromLowest;
58 });
59 Clazz.defineMethod (c$, "getFromHighest", 
60 function () {
61 return this.fromHighest;
62 });
63 Clazz.defineMethod (c$, "getToLowest", 
64 function () {
65 return this.toLowest;
66 });
67 Clazz.defineMethod (c$, "getToHighest", 
68 function () {
69 return this.toHighest;
70 });
71 Clazz.makeConstructor (c$, 
72 function (from, to, fromRatio, toRatio) {
73 this.fromRatio = fromRatio;
74 this.toRatio = toRatio;
75 this.fromLowest = from[0];
76 this.fromHighest = from[1];
77 for (var i = 0; i < from.length; i += 2) {
78 this.fromLowest = Math.min (this.fromLowest, from[i]);
79 this.fromHighest = Math.max (this.fromHighest, from[i + 1]);
80 this.fromShifts.add ( Clazz.newIntArray (-1, [from[i], from[i + 1]]));
81 }
82 this.toLowest = to[0];
83 this.toHighest = to[1];
84 for (var i = 0; i < to.length; i += 2) {
85 this.toLowest = Math.min (this.toLowest, to[i]);
86 this.toHighest = Math.max (this.toHighest, to[i + 1]);
87 this.toShifts.add ( Clazz.newIntArray (-1, [to[i], to[i + 1]]));
88 }
89 }, "~A,~A,~N,~N");
90 Clazz.makeConstructor (c$, 
91 function (map) {
92 this.fromLowest = map.fromLowest;
93 this.fromHighest = map.fromHighest;
94 this.toLowest = map.toLowest;
95 this.toHighest = map.toHighest;
96 this.fromRatio = map.fromRatio;
97 this.toRatio = map.toRatio;
98 if (map.fromShifts != null) {
99 for (var r, $r = map.fromShifts.iterator (); $r.hasNext () && ((r = $r.next ()) || true);) {
100 this.fromShifts.add ( Clazz.newIntArray (-1, [r[0], r[1]]));
101 }
102 }if (map.toShifts != null) {
103 for (var r, $r = map.toShifts.iterator (); $r.hasNext () && ((r = $r.next ()) || true);) {
104 this.toShifts.add ( Clazz.newIntArray (-1, [r[0], r[1]]));
105 }
106 }}, "jalview.util.MapList");
107 Clazz.makeConstructor (c$, 
108 function (fromRange, toRange, fromRatio, toRatio) {
109 this.fromShifts = fromRange;
110 this.toShifts = toRange;
111 this.fromRatio = fromRatio;
112 this.toRatio = toRatio;
113 this.fromLowest = 2147483647;
114 this.fromHighest = 0;
115 for (var range, $range = fromRange.iterator (); $range.hasNext () && ((range = $range.next ()) || true);) {
116 this.fromLowest = Math.min (this.fromLowest, range[0]);
117 this.fromHighest = Math.max (this.fromHighest, range[1]);
118 }
119 this.toLowest = 2147483647;
120 this.toHighest = 0;
121 for (var range, $range = toRange.iterator (); $range.hasNext () && ((range = $range.next ()) || true);) {
122 this.toLowest = Math.min (this.toLowest, range[0]);
123 this.toHighest = Math.max (this.toHighest, range[1]);
124 }
125 }, "java.util.List,java.util.List,~N,~N");
126 Clazz.defineMethod (c$, "makeFromMap", 
127 function () {
128 return this.posMap (this.fromShifts, this.fromRatio, this.toShifts, this.toRatio);
129 });
130 Clazz.defineMethod (c$, "makeToMap", 
131 function () {
132 return this.posMap (this.toShifts, this.toRatio, this.fromShifts, this.fromRatio);
133 });
134 Clazz.defineMethod (c$, "posMap", 
135  function (shiftTo, ratio, shiftFrom, toRatio) {
136 var iv = 0;
137 var ivSize = shiftTo.size ();
138 if (iv >= ivSize) {
139 return null;
140 }var intv = shiftTo.get (iv++);
141 var from = intv[0];
142 var to = intv[1];
143 if (from > to) {
144 from = intv[1];
145 to = intv[0];
146 }while (iv < ivSize) {
147 intv = shiftTo.get (iv++);
148 if (intv[0] < from) {
149 from = intv[0];
150 }if (intv[1] < from) {
151 from = intv[1];
152 }if (intv[0] > to) {
153 to = intv[0];
154 }if (intv[1] > to) {
155 to = intv[1];
156 }}
157 var tF = 0;
158 var tT = 0;
159 var mp =  Clazz.newIntArray (to - from + 2, 0);
160 for (var i = 0; i < mp.length; i++) {
161 var m = jalview.util.MapList.shift (i + from, shiftTo, ratio, shiftFrom, toRatio);
162 if (m != null) {
163 if (i == 0) {
164 tF = tT = m[0];
165 } else {
166 if (m[0] < tF) {
167 tF = m[0];
168 }if (m[0] > tT) {
169 tT = m[0];
170 }}}mp[i] = m;
171 }
172 var map =  Clazz.newArray (-1, [ Clazz.newIntArray (-1, [from, to, tF, tT]),  Clazz.newIntArray (to - from + 2, 0)]);
173 map[0][2] = tF;
174 map[0][3] = tT;
175 for (var i = 0; i < mp.length; i++) {
176 if (mp[i] != null) {
177 map[1][i] = mp[i][0] - tF;
178 } else {
179 map[1][i] = -1;
180 }}
181 return map;
182 }, "java.util.List,~N,java.util.List,~N");
183 Clazz.defineMethod (c$, "shiftFrom", 
184 function (pos) {
185 return jalview.util.MapList.shift (pos, this.fromShifts, this.fromRatio, this.toShifts, this.toRatio);
186 }, "~N");
187 Clazz.defineMethod (c$, "shiftTo", 
188 function (pos) {
189 return jalview.util.MapList.shift (pos, this.toShifts, this.toRatio, this.fromShifts, this.fromRatio);
190 }, "~N");
191 c$.shift = Clazz.defineMethod (c$, "shift", 
192 function (pos, shiftTo, fromRatio, shiftFrom, toRatio) {
193 var fromCount = jalview.util.MapList.countPos (shiftTo, pos);
194 if (fromCount == null) {
195 return null;
196 }var fromRemainder = (fromCount[0] - 1) % fromRatio;
197 var toCount = 1 + ((Clazz.doubleToInt ((fromCount[0] - 1) / fromRatio)) * toRatio);
198 var toPos = jalview.util.MapList.countToPos (shiftFrom, toCount);
199 if (toPos == null) {
200 return null;
201 }return  Clazz.newIntArray (-1, [toPos[0], fromRemainder, toPos[1]]);
202 }, "~N,java.util.List,~N,java.util.List,~N");
203 c$.countPos = Clazz.defineMethod (c$, "countPos", 
204 function (shiftTo, pos) {
205 var count = 0;
206 var intv;
207 var iv = 0;
208 var ivSize = shiftTo.size ();
209 while (iv < ivSize) {
210 intv = shiftTo.get (iv++);
211 if (intv[0] <= intv[1]) {
212 if (pos >= intv[0] && pos <= intv[1]) {
213 return  Clazz.newIntArray (-1, [count + pos - intv[0] + 1, 1]);
214 } else {
215 count += intv[1] - intv[0] + 1;
216 }} else {
217 if (pos >= intv[1] && pos <= intv[0]) {
218 return  Clazz.newIntArray (-1, [count + intv[0] - pos + 1, -1]);
219 } else {
220 count += intv[0] - intv[1] + 1;
221 }}}
222 return null;
223 }, "java.util.List,~N");
224 c$.countToPos = Clazz.defineMethod (c$, "countToPos", 
225 function (shiftFrom, pos) {
226 var count = 0;
227 var diff = 0;
228 var iv = 0;
229 var ivSize = shiftFrom.size ();
230 var intv =  Clazz.newIntArray (-1, [0, 0]);
231 while (iv < ivSize) {
232 intv = shiftFrom.get (iv++);
233 diff = intv[1] - intv[0];
234 if (diff >= 0) {
235 if (pos <= count + 1 + diff) {
236 return  Clazz.newIntArray (-1, [pos - count - 1 + intv[0], 1]);
237 } else {
238 count += 1 + diff;
239 }} else {
240 if (pos <= count + 1 - diff) {
241 return  Clazz.newIntArray (-1, [intv[0] - (pos - count - 1), -1]);
242 } else {
243 count += 1 - diff;
244 }}}
245 return null;
246 }, "java.util.List,~N");
247 Clazz.defineMethod (c$, "locateInFrom", 
248 function (start, end) {
249 var fromStart = this.shiftTo (start);
250 var fromEnd = this.shiftTo (end);
251 return jalview.util.MapList.getIntervals (this.fromShifts, fromStart, fromEnd, this.fromRatio);
252 }, "~N,~N");
253 Clazz.defineMethod (c$, "locateInTo", 
254 function (start, end) {
255 var toStart = this.shiftFrom (start);
256 var toEnd = this.shiftFrom (end);
257 return jalview.util.MapList.getIntervals (this.toShifts, toStart, toEnd, this.toRatio);
258 }, "~N,~N");
259 c$.getIntervals = Clazz.defineMethod (c$, "getIntervals", 
260 function (shiftFrom, fromStart, fromEnd, fromRatio2) {
261 if (fromStart == null || fromEnd == null) {
262 return null;
263 }var startpos;
264 var endpos;
265 startpos = fromStart[0];
266 endpos = fromEnd[0];
267 var endindx = (fromRatio2 - 1);
268 var intv = 0;
269 var intvSize = shiftFrom.size ();
270 var iv;
271 var i = 0;
272 var fs = -1;
273 var fe_s = -1;
274 var fe = -1;
275 while (intv < intvSize && (fs == -1 || fe == -1)) {
276 iv = shiftFrom.get (intv++);
277 if (fe_s > -1) {
278 endpos = iv[0];
279 endindx--;
280 }if (iv[0] <= iv[1]) {
281 if (fs == -1 && startpos >= iv[0] && startpos <= iv[1]) {
282 fs = i;
283 }if (endpos >= iv[0] && endpos <= iv[1]) {
284 if (fe_s == -1) {
285 fe_s = i;
286 }if (fe_s != -1) {
287 if (endpos + endindx <= iv[1]) {
288 fe = i;
289 endpos = endpos + endindx;
290 } else {
291 endindx -= iv[1] - endpos;
292 }}}} else {
293 if (fs == -1 && startpos <= iv[0] && startpos >= iv[1]) {
294 fs = i;
295 }if (endpos <= iv[0] && endpos >= iv[1]) {
296 if (fe_s == -1) {
297 fe_s = i;
298 }if (fe_s != -1) {
299 if (endpos - endindx >= iv[1]) {
300 fe = i;
301 endpos = endpos - endindx;
302 } else {
303 endindx -= endpos - iv[1];
304 }}}}i++;
305 }
306 if (fs == fe && fe == -1) {
307 return null;
308 }var ranges =  new java.util.ArrayList ();
309 if (fs <= fe) {
310 intv = fs;
311 i = fs;
312 iv = shiftFrom.get (intv++);
313 iv =  Clazz.newIntArray (-1, [iv[0], iv[1]]);
314 if (i == fs) {
315 iv[0] = startpos;
316 }while (i != fe) {
317 ranges.add (iv);
318 iv = shiftFrom.get (intv++);
319 iv =  Clazz.newIntArray (-1, [iv[0], iv[1]]);
320 i++;
321 }
322 if (i == fe) {
323 iv[1] = endpos;
324 }ranges.add (iv);
325 } else {
326 i = shiftFrom.size () - 1;
327 while (i > fs) {
328 i--;
329 }
330 iv = shiftFrom.get (i);
331 iv =  Clazz.newIntArray (-1, [iv[1], iv[0]]);
332 if (i == fs) {
333 iv[0] = startpos;
334 }while (--i != fe) {
335 ranges.add (iv);
336 iv = shiftFrom.get (i);
337 iv =  Clazz.newIntArray (-1, [iv[1], iv[0]]);
338 }
339 if (i == fe) {
340 iv[1] = endpos;
341 }ranges.add (iv);
342 }var range = null;
343 if (ranges != null && ranges.size () > 0) {
344 range =  Clazz.newIntArray (ranges.size () * 2, 0);
345 intv = 0;
346 intvSize = ranges.size ();
347 i = 0;
348 while (intv < intvSize) {
349 iv = ranges.get (intv);
350 range[i++] = iv[0];
351 range[i++] = iv[1];
352 ranges.set (intv++, null);
353 }
354 }return range;
355 }, "java.util.List,~A,~A,~N");
356 Clazz.defineMethod (c$, "getToPosition", 
357 function (mpos) {
358 var mp = this.shiftTo (mpos);
359 if (mp != null) {
360 return mp[0];
361 }return mpos;
362 }, "~N");
363 Clazz.defineMethod (c$, "getToWord", 
364 function (mpos) {
365 var mp = this.shiftTo (mpos);
366 if (mp != null) {
367 return  Clazz.newIntArray (-1, [mp[0], mp[0] + mp[2] * (this.getFromRatio () - 1)]);
368 }return null;
369 }, "~N");
370 Clazz.defineMethod (c$, "getMappedPosition", 
371 function (pos) {
372 var mp = this.shiftFrom (pos);
373 if (mp != null) {
374 return mp[0];
375 }return pos;
376 }, "~N");
377 Clazz.defineMethod (c$, "getMappedWord", 
378 function (pos) {
379 var mp = this.shiftFrom (pos);
380 if (mp != null) {
381 return  Clazz.newIntArray (-1, [mp[0], mp[0] + mp[2] * (this.getToRatio () - 1)]);
382 }return null;
383 }, "~N");
384 Clazz.defineMethod (c$, "getInverse", 
385 function () {
386 return  new jalview.util.MapList (this.getToRanges (), this.getFromRanges (), this.getToRatio (), this.getFromRatio ());
387 });
388 Clazz.defineMethod (c$, "containsEither", 
389 function (local, map) {
390 if (local) {
391 return ((this.getFromLowest () >= map.getFromLowest () && this.getFromHighest () <= map.getFromHighest ()) || (this.getFromLowest () <= map.getFromLowest () && this.getFromHighest () >= map.getFromHighest ()));
392 } else {
393 return ((this.getToLowest () >= map.getToLowest () && this.getToHighest () <= map.getToHighest ()) || (this.getToLowest () <= map.getToLowest () && this.getToHighest () >= map.getToHighest ()));
394 }}, "~B,jalview.util.MapList");
395 Clazz.overrideMethod (c$, "toString", 
396 function () {
397 var sb =  new StringBuilder (64);
398 sb.append ("From (").append (this.fromRatio).append (":").append (this.toRatio).append (") [");
399 for (var shift, $shift = this.fromShifts.iterator (); $shift.hasNext () && ((shift = $shift.next ()) || true);) {
400 sb.append (" ").append (java.util.Arrays.toString (shift));
401 }
402 sb.append (" ] To [");
403 for (var shift, $shift = this.toShifts.iterator (); $shift.hasNext () && ((shift = $shift.next ()) || true);) {
404 sb.append (" ").append (java.util.Arrays.toString (shift));
405 }
406 sb.append (" ]");
407 return sb.toString ();
408 });
409 });