2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
28 * A simple way of bijectively mapping a non-contiguous linear range to another
29 * non-contiguous linear range.
31 * Use at your own risk!
33 * TODO: efficient implementation of private posMap method
35 * TODO: test/ensure that sense of from and to ratio start position is conserved
36 * (codon start position recovery)
41 public static final int POS = 0;
42 public static final int POS_FROM = 1; // for countPos
43 public static final int DIR_FROM = 2; // for countPos
44 public static final int POS_TO = 3; // for countToPos
45 public static final int DIR_TO = 4; // for countToPos
46 private static final int FROM_REMAINDER = 5;
47 public static final int LEN = 6;
49 * Subregions (base 1) described as { [start1, end1], [start2, end2], ...}
51 private List<int[]> fromShifts;
54 * Same format as fromShifts, for the 'mapped to' sequence
56 private List<int[]> toShifts;
59 * number of steps in fromShifts to one toRatio unit
61 private int fromRatio;
64 * number of steps in toShifts to one fromRatio
69 * lowest and highest value in the from Map
71 private int fromLowest;
73 private int fromHighest;
76 * lowest and highest value in the to Map
80 private int toHighest;
87 fromShifts = new ArrayList<>();
88 toShifts = new ArrayList<>();
92 * Two MapList objects are equal if they are the same object, or they both
93 * have populated shift ranges and all values are the same.
96 public boolean equals(Object o)
98 if (o == null || !(o instanceof MapList))
103 MapList obj = (MapList) o;
108 if (obj.fromRatio != fromRatio || obj.toRatio != toRatio
109 || obj.fromShifts == null || obj.toShifts == null)
113 return Arrays.deepEquals(fromShifts.toArray(), obj.fromShifts.toArray())
114 && Arrays.deepEquals(toShifts.toArray(),
115 obj.toShifts.toArray());
119 * Returns a hashcode made from the fromRatio, toRatio, and from/to ranges
122 public int hashCode()
124 int hashCode = 31 * fromRatio;
125 hashCode = 31 * hashCode + toRatio;
126 for (int[] shift : fromShifts)
128 hashCode = 31 * hashCode + shift[0];
129 hashCode = 31 * hashCode + shift[1];
131 for (int[] shift : toShifts)
133 hashCode = 31 * hashCode + shift[0];
134 hashCode = 31 * hashCode + shift[1];
141 * Returns the 'from' ranges as {[start1, end1], [start2, end2], ...}
145 public List<int[]> getFromRanges()
151 * Returns the 'to' ranges as {[start1, end1], [start2, end2], ...}
155 public List<int[]> getToRanges()
161 * Flattens a list of [start, end] into a single [start1, end1, start2,
167 protected static int[] getRanges(List<int[]> shifts)
169 int[] rnges = new int[2 * shifts.size()];
171 for (int[] r : shifts)
181 * @return length of mapped phrase in from
183 public int getFromRatio()
190 * @return length of mapped phrase in to
192 public int getToRatio()
197 public int getFromLowest()
202 public int getFromHighest()
207 public int getToLowest()
212 public int getToHighest()
218 * Constructor given from and to ranges as [start1, end1, start2, end2,...].
219 * If any end is equal to the next start, the ranges will be merged. There is
220 * no validation check that the ranges do not overlap each other.
223 * contiguous regions as [start1, end1, start2, end2, ...]
225 * same format as 'from'
227 * phrase length in 'from' (e.g. 3 for dna)
229 * phrase length in 'to' (e.g. 1 for protein)
231 public MapList(int from[], int to[], int fromRatio, int toRatio)
234 this.fromRatio = fromRatio;
235 this.toRatio = toRatio;
236 fromLowest = Integer.MAX_VALUE;
237 fromHighest = Integer.MIN_VALUE;
240 for (int i = 0; i < from.length; i += 2)
243 * note lowest and highest values - bearing in mind the
244 * direction may be reversed
246 fromLowest = Math.min(fromLowest, Math.min(from[i], from[i + 1]));
247 fromHighest = Math.max(fromHighest, Math.max(from[i], from[i + 1]));
248 if (added > 0 && from[i] == fromShifts.get(added - 1)[1])
251 * this range starts where the last ended - just extend it
253 fromShifts.get(added - 1)[1] = from[i + 1];
257 fromShifts.add(new int[] { from[i], from[i + 1] });
262 toLowest = Integer.MAX_VALUE;
263 toHighest = Integer.MIN_VALUE;
265 for (int i = 0; i < to.length; i += 2)
267 toLowest = Math.min(toLowest, Math.min(to[i], to[i + 1]));
268 toHighest = Math.max(toHighest, Math.max(to[i], to[i + 1]));
269 if (added > 0 && to[i] == toShifts.get(added - 1)[1])
271 toShifts.get(added - 1)[1] = to[i + 1];
275 toShifts.add(new int[] { to[i], to[i + 1] });
282 * Copy constructor. Creates an identical mapping.
286 public MapList(MapList map)
289 // TODO not used - remove?
290 this.fromLowest = map.fromLowest;
291 this.fromHighest = map.fromHighest;
292 this.toLowest = map.toLowest;
293 this.toHighest = map.toHighest;
295 this.fromRatio = map.fromRatio;
296 this.toRatio = map.toRatio;
297 if (map.fromShifts != null)
299 for (int[] r : map.fromShifts)
301 fromShifts.add(new int[] { r[0], r[1] });
304 if (map.toShifts != null)
306 for (int[] r : map.toShifts)
308 toShifts.add(new int[] { r[0], r[1] });
314 * Constructor given ranges as lists of [start, end] positions. There is no
315 * validation check that the ranges do not overlap each other.
322 public MapList(List<int[]> fromRange, List<int[]> toRange, int fromRatio,
326 fromRange = coalesceRanges(fromRange);
327 toRange = coalesceRanges(toRange);
328 this.fromShifts = fromRange;
329 this.toShifts = toRange;
330 this.fromRatio = fromRatio;
331 this.toRatio = toRatio;
333 fromLowest = Integer.MAX_VALUE;
334 fromHighest = Integer.MIN_VALUE;
335 for (int[] range : fromRange)
337 if (range.length != 2)
339 // throw new IllegalArgumentException(range);
341 "Invalid format for fromRange " + Arrays.toString(range)
342 + " may cause errors");
344 fromLowest = Math.min(fromLowest, Math.min(range[0], range[1]));
345 fromHighest = Math.max(fromHighest, Math.max(range[0], range[1]));
348 toLowest = Integer.MAX_VALUE;
349 toHighest = Integer.MIN_VALUE;
350 for (int[] range : toRange)
352 if (range.length != 2)
354 // throw new IllegalArgumentException(range);
355 System.err.println("Invalid format for toRange "
356 + Arrays.toString(range)
357 + " may cause errors");
359 toLowest = Math.min(toLowest, Math.min(range[0], range[1]));
360 toHighest = Math.max(toHighest, Math.max(range[0], range[1]));
365 * Consolidates a list of ranges so that any contiguous ranges are merged.
366 * This assumes the ranges are already in start order (does not sort them).
369 * @return the same list (if unchanged), else a new merged list, leaving the
370 * input list unchanged
372 public static List<int[]> coalesceRanges(final List<int[]> ranges)
374 if (ranges == null || ranges.size() < 2)
379 boolean changed = false;
380 List<int[]> merged = new ArrayList<>();
381 int[] lastRange = ranges.get(0);
382 int lastDirection = lastRange[1] >= lastRange[0] ? 1 : -1;
383 lastRange = new int[] { lastRange[0], lastRange[1] };
384 merged.add(lastRange);
385 boolean first = true;
387 for (final int[] range : ranges)
394 if (range[0] == lastRange[0] && range[1] == lastRange[1])
396 // drop duplicate range
402 * drop this range if it lies within the last range
404 if ((lastDirection == 1 && range[0] >= lastRange[0]
405 && range[0] <= lastRange[1] && range[1] >= lastRange[0]
406 && range[1] <= lastRange[1])
407 || (lastDirection == -1 && range[0] <= lastRange[0]
408 && range[0] >= lastRange[1]
409 && range[1] <= lastRange[0]
410 && range[1] >= lastRange[1]))
416 int direction = range[1] >= range[0] ? 1 : -1;
419 * if next range is in the same direction as last and contiguous,
420 * just update the end position of the last range
422 boolean sameDirection = range[1] == range[0]
423 || direction == lastDirection;
424 boolean extending = range[0] == lastRange[1] + lastDirection;
425 boolean overlapping = (lastDirection == 1 && range[0] >= lastRange[0]
426 && range[0] <= lastRange[1])
427 || (lastDirection == -1 && range[0] <= lastRange[0]
428 && range[0] >= lastRange[1]);
429 if (sameDirection && (overlapping || extending))
431 lastRange[1] = range[1];
436 lastRange = new int[] { range[0], range[1] };
437 merged.add(lastRange);
438 // careful: merging [5, 5] after [7, 6] should keep negative direction
439 lastDirection = (range[1] == range[0]) ? lastDirection : direction;
443 return changed ? merged : ranges;
447 // * get all mapped positions from 'from' to 'to'
449 // * @return int[][] { int[] { fromStart, fromFinish, toStart, toFinish }, int
450 // * [fromFinish-fromStart+2] { toStart..toFinish mappings}}
452 // protected int[][] makeFromMap()
454 // // TODO not used - remove??
455 // return posMap(fromShifts, fromRatio, toShifts, toRatio);
459 // * get all mapped positions from 'to' to 'from'
461 // * @return int[to position]=position mapped in from
463 // protected int[][] makeToMap()
465 // // TODO not used - remove??
466 // return posMap(toShifts, toRatio, fromShifts, fromRatio);
470 // * construct an int map for intervals in intVals
473 // * @return int[] { from, to pos in range }, int[range.to-range.from+1]
474 // * returning mapped position
476 // private int[][] posMap(List<int[]> shiftTo, int ratio,
477 // List<int[]> shiftFrom, int toRatio)
479 // // TODO not used - remove??
481 // int[] reg = new int[LEN];
483 // int iv = 0, ivSize = shiftTo.size();
488 // int[] intv = shiftTo.get(iv++);
489 // int from = intv[0], to = intv[1];
495 // while (iv < ivSize)
497 // intv = shiftTo.get(iv++);
498 // if (intv[0] < from)
502 // if (intv[1] < from)
515 // int tF = 0, tT = 0;
516 // int mp[][] = new int[to - from + 2][];
517 // for (int i = 0; i < mp.length; i++)
519 // reg[POS] = i + from;
520 // int[] m = shift(reg, shiftTo, ratio, shiftFrom, toRatio);
541 // int[][] map = new int[][] { new int[] { from, to, tF, tT },
542 // new int[to - from + 2] };
547 // for (int i = 0; i < mp.length; i++)
549 // if (mp[i] != null)
551 // map[1][i] = mp[i][0] - tF;
555 // map[1][i] = -1; // indicates an out of range mapping
565 * start position for shift (in original reference frame)
569 * public void addShift(int pos, int shift) { int sidx = 0; int[]
570 * rshift=null; while (sidx<shifts.size() && (rshift=(int[])
571 * shifts.elementAt(sidx))[0]<pos) sidx++; if (sidx==shifts.size())
572 * shifts.insertElementAt(new int[] { pos, shift}, sidx); else
573 * rshift[1]+=shift; }
577 * shift from pos to To(pos)
581 * @return int[] reg[POS_TO:shifted position in To,
582 * FROM_REMAINDER: frameshift in From,
583 * DIR_TO: direction of mapped symbol in To]
585 public int[] shiftFrom(int[] reg)
587 return shift(reg, fromShifts, fromRatio, toShifts, toRatio);
591 * inverse of shiftFrom - maps pos in To to a position in From
595 * @return shifted position in From, frameshift in To, direction of mapped
598 public int[] shiftTo(int[] reg)
600 return shift(reg, toShifts, toRatio, fromShifts, fromRatio);
610 * @return reg[COUNT_TO, FROM_REMAINDER, DIR_TO]
612 protected static int[] shift(int[] reg, List<int[]> shiftTo, int fromRatio,
613 List<int[]> shiftFrom, int toRatio)
615 // TODO: javadoc; tests
616 reg = countPos(shiftTo, reg);
621 reg[FROM_REMAINDER] = (reg[POS_FROM] - 1) % fromRatio;
622 reg[POS] = 1 + (((reg[POS_FROM] - 1) / fromRatio) * toRatio); // toCount
623 reg = countToPos(shiftFrom, reg);
626 return null; // throw new Error("Bad Mapping!");
629 // System.out.println(fromCount[0]+" "+fromCount[1]+" "+toCount);
630 // ret3[0] = toPos[0];
631 // ret3[1] = fromRemainder;
632 // ret3[2] = toPos[1];
637 * count how many positions pos is along the series of intervals.
641 * @return number of positions or null if pos is not within intervals
643 protected static int[] countPos(List<int[]> shiftTo, int[] reg)
646 int count = 0, iv = 0, ivSize = shiftTo.size();
649 int[] intv = shiftTo.get(iv++);
650 if (intv[0] <= intv[1])
652 if (pos >= intv[0] && pos <= intv[1])
654 reg[POS_FROM] = count + pos - intv[0] + 1;
660 count += intv[1] - intv[0] + 1;
665 if (pos >= intv[1] && pos <= intv[0])
667 reg[POS_FROM] = count - pos + intv[0] + 1;
673 count += intv[0] - intv[1] + 1;
681 * count out pos positions into a series of intervals and return the position
685 * @return position pos in interval set
687 protected static int[] countToPos(List<int[]> shiftFrom, int[] reg)
689 int count = 0, diff = 0, iv = 0, ivSize = shiftFrom.size();
693 int[] intv = shiftFrom.get(iv++);
694 diff = intv[1] - intv[0];
697 if (pos <= count + 1 + diff)
699 reg[POS_TO] = intv[0] + pos - count - 1;
710 if (pos <= count + 1 - diff)
712 reg[POS_TO] = intv[0] - (pos - count - 1);
722 return null;// (diff<0) ? (intv[1]-1) : (intv[0]+1);
726 * find series of intervals mapping from start-end in the From map.
729 * position mapped 'to'
731 * position mapped 'to'
732 * @return series of [start, end] ranges in sequence mapped 'from'
734 public int[] locateInFrom(int start, int end)
736 int[] reg = new int[LEN];
737 // inefficient implementation
742 // needs to be inclusive of end of symbol position
749 return getIntervals(fromShifts, start, end, fromRatio);
753 * find series of intervals mapping from start-end in the to map.
756 * position mapped 'from'
758 * position mapped 'from'
759 * @return series of [start, end] ranges in sequence mapped 'to'
761 public int[] locateInTo(int start, int end)
763 int[] reg = new int[LEN];
765 reg = shiftFrom(reg);
768 start = reg[POS_FROM];
770 reg = shiftFrom(reg);
774 return getIntervals(toShifts, start, end, toRatio);
778 * like shift - except returns the intervals in the given vector of shifts
779 * which were spanned in traversing fromStart to fromEnd
785 * @return series of from,to intervals from from first position of starting
786 * region to final position of ending region inclusive
788 protected static int[] getIntervals(List<int[]> shiftFrom,
789 int startpos, int endpos, int fromRatio2)
791 // if (fromStart == null || fromEnd == null)
795 // int startpos, endpos;
796 // startpos = fromStart[0]; // first position in fromStart
797 // endpos = fromEnd[0]; // last position in fromEnd
798 int endindx = (fromRatio2 - 1); // additional positions to get to last
799 // position from endpos
800 int intv = 0, intvSize = shiftFrom.size();
801 int iv[], i = 0, fs = -1, fe_s = -1, fe = -1; // containing intervals
802 // search intervals to locate ones containing startpos and count endindx
803 // positions on from endpos
804 while (intv < intvSize && (fs == -1 || fe == -1))
806 iv = shiftFrom.get(intv++);
809 endpos = iv[0]; // start counting from beginning of interval
810 endindx--; // inclusive of endpos
814 if (fs == -1 && startpos >= iv[0] && startpos <= iv[1])
818 if (endpos >= iv[0] && endpos <= iv[1])
826 if (endpos + endindx <= iv[1])
829 endpos = endpos + endindx; // end of end token is within this
834 endindx -= iv[1] - endpos; // skip all this interval too
841 if (fs == -1 && startpos <= iv[0] && startpos >= iv[1])
845 if (endpos <= iv[0] && endpos >= iv[1])
853 if (endpos - endindx >= iv[1])
856 endpos = endpos - endindx; // end of end token is within this
861 endindx -= endpos - iv[1]; // skip all this interval too
868 if (fs == fe && fe == -1)
872 List<int[]> ranges = new ArrayList<>();
877 // truncate initial interval
878 iv = shiftFrom.get(intv++);
879 iv = new int[] { iv[0], iv[1] };// clone
886 ranges.add(iv); // add initial range
887 iv = shiftFrom.get(intv++); // get next interval
888 iv = new int[] { iv[0], iv[1] };// clone
895 ranges.add(iv); // add only - or final range
899 // walk from end of interval.
900 i = shiftFrom.size() - 1;
905 iv = shiftFrom.get(i);
906 iv = new int[] { iv[1], iv[0] };// reverse and clone
907 // truncate initial interval
913 { // fix apparent logic bug when fe==-1
914 ranges.add(iv); // add (truncated) reversed interval
915 iv = shiftFrom.get(i);
916 iv = new int[] { iv[1], iv[0] }; // reverse and clone
920 // interval is already reversed
923 ranges.add(iv); // add only - or final range
925 // create array of start end intervals.
927 if (ranges != null && ranges.size() > 0)
929 range = new int[ranges.size() * 2];
931 intvSize = ranges.size();
933 while (intv < intvSize)
935 iv = ranges.get(intv);
938 ranges.set(intv++, null); // remove
945 // * get the 'initial' position of mpos in To
948 // * position in from
949 // * @return position of first word in to reference frame
951 // public int getToPosition(int[])
953 // // TODO not used - remove??
954 // int[] mp = shiftTo(mpos);
963 // * get range of positions in To frame for the mpos word in From
966 // * position in From
967 // * @return null or int[] first position in To for mpos, last position in to
970 // public int[] getToWord(int mpos)
973 // int[] mp = shiftTo(mpos);
976 // return new int[] { mp[0], mp[0] + mp[2] * (getFromRatio() - 1) };
982 * get From position in the associated reference frame for position pos in the
983 * associated sequence.
988 public int getMappedPosition(int[] reg)
990 // TODO not used - remove??
992 reg = shiftFrom(reg);
1000 // public int[] getMappedWord(int[] reg)
1002 // // TODO not used - remove??
1003 // reg = shiftFrom(reg);
1006 // return new int[] { mp[0], mp[0] + mp[2] * (getToRatio() - 1) };
1013 * @return a MapList whose From range is this maplist's To Range, and vice
1016 public MapList getInverse()
1018 return new MapList(getToRanges(), getFromRanges(), getToRatio(),
1023 * test for containment rather than equivalence to another mapping
1026 * to be tested for containment
1027 * @return true if local or mapped range map contains or is contained by this
1030 public boolean containsEither(boolean local, MapList map)
1032 // TODO not used - remove?
1035 return ((getFromLowest() >= map.getFromLowest()
1036 && getFromHighest() <= map.getFromHighest())
1037 || (getFromLowest() <= map.getFromLowest()
1038 && getFromHighest() >= map.getFromHighest()));
1042 return ((getToLowest() >= map.getToLowest()
1043 && getToHighest() <= map.getToHighest())
1044 || (getToLowest() <= map.getToLowest()
1045 && getToHighest() >= map.getToHighest()));
1050 * String representation - for debugging, not guaranteed not to change
1053 public String toString()
1055 StringBuilder sb = new StringBuilder(64);
1057 for (int[] shift : fromShifts)
1059 sb.append(" ").append(Arrays.toString(shift));
1062 sb.append(fromRatio).append(":").append(toRatio);
1064 for (int[] shift : toShifts)
1066 sb.append(" ").append(Arrays.toString(shift));
1069 return sb.toString();
1073 * Extend this map list by adding the given map's ranges. There is no
1074 * validation check that the ranges do not overlap existing ranges (or each
1075 * other), but contiguous ranges are merged.
1079 public void addMapList(MapList map)
1081 if (this.equals(map))
1085 this.fromLowest = Math.min(fromLowest, map.fromLowest);
1086 this.toLowest = Math.min(toLowest, map.toLowest);
1087 this.fromHighest = Math.max(fromHighest, map.fromHighest);
1088 this.toHighest = Math.max(toHighest, map.toHighest);
1090 for (int[] range : map.getFromRanges())
1092 addRange(range, fromShifts);
1094 for (int[] range : map.getToRanges())
1096 addRange(range, toShifts);
1101 * Adds the given range to a list of ranges. If the new range just extends
1102 * existing ranges, the current endpoint is updated instead.
1107 static void addRange(int[] range, List<int[]> addTo)
1110 * list is empty - add to it!
1112 if (addTo.size() == 0)
1118 int[] last = addTo.get(addTo.size() - 1);
1119 boolean lastForward = last[1] >= last[0];
1120 boolean newForward = range[1] >= range[0];
1123 * contiguous range in the same direction - just update endpoint
1125 if (lastForward == newForward && last[1] == range[0])
1132 * next range starts at +1 in forward sense - update endpoint
1134 if (lastForward && newForward && range[0] == last[1] + 1)
1141 * next range starts at -1 in reverse sense - update endpoint
1143 if (!lastForward && !newForward && range[0] == last[1] - 1)
1150 * just add the new range
1156 * Returns true if mapping is from forward strand, false if from reverse
1157 * strand. Result is just based on the first 'from' range that is not a single
1158 * position. Default is true unless proven to be false. Behaviour is not well
1159 * defined if the mapping has a mixture of forward and reverse ranges.
1163 public boolean isFromForwardStrand()
1165 return isForwardStrand(getFromRanges());
1169 * Returns true if mapping is to forward strand, false if to reverse strand.
1170 * Result is just based on the first 'to' range that is not a single position.
1171 * Default is true unless proven to be false. Behaviour is not well defined if
1172 * the mapping has a mixture of forward and reverse ranges.
1176 public boolean isToForwardStrand()
1178 return isForwardStrand(getToRanges());
1182 * A helper method that returns true unless at least one range has start > end.
1183 * Behaviour is undefined for a mixture of forward and reverse ranges.
1188 private boolean isForwardStrand(List<int[]> ranges)
1190 boolean forwardStrand = true;
1191 for (int[] range : ranges)
1193 if (range[1] > range[0])
1195 break; // forward strand confirmed
1197 else if (range[1] < range[0])
1199 forwardStrand = false;
1200 break; // reverse strand confirmed
1203 return forwardStrand;
1208 * @return true if from, or to is a three to 1 mapping
1210 public boolean isTripletMap()
1212 return (toRatio == 3 && fromRatio == 1)
1213 || (fromRatio == 3 && toRatio == 1);
1217 * Returns a map which is the composite of this one and the input map. That
1218 * is, the output map has the fromRanges of this map, and its toRanges are the
1219 * toRanges of this map as transformed by the input map.
1221 * Returns null if the mappings cannot be traversed (not all toRanges of this
1222 * map correspond to fromRanges of the input), or if this.toRatio does not
1223 * match map.fromRatio.
1227 * this: from [1-100] to [501-600]
1228 * input: from [10-40] to [60-90]
1229 * output: from [10-40] to [560-590]
1230 * Example 2 ('reverse strand exons'):
1231 * this: from [1-100] to [2000-1951], [1000-951] // transcript to loci
1232 * input: from [1-50] to [41-90] // CDS to transcript
1233 * output: from [10-40] to [1960-1951], [1000-971] // CDS to gene loci
1239 public MapList traverse(MapList map)
1247 * compound the ratios by this rule:
1248 * A:B with M:N gives A*M:B*N
1249 * reduced by greatest common divisor
1250 * so 1:3 with 3:3 is 3:9 or 1:3
1251 * 1:3 with 3:1 is 3:3 or 1:1
1252 * 1:3 with 1:3 is 1:9
1253 * 2:5 with 3:7 is 6:35
1255 int outFromRatio = getFromRatio() * map.getFromRatio();
1256 int outToRatio = getToRatio() * map.getToRatio();
1257 int gcd = MathUtils.gcd(outFromRatio, outToRatio);
1258 outFromRatio /= gcd;
1261 List<int[]> toRanges = new ArrayList<>();
1262 List<int[]> ranges = getToRanges();
1264 for (int ir = 0, nr = ranges.size(); ir < nr; ir++)
1266 int[] range = ranges.get(ir);
1267 int[] transferred = map.locateInTo(range[0], range[1]);
1268 if (transferred == null || transferred.length % 2 != 0)
1274 * convert [start1, end1, start2, end2, ...]
1275 * to [[start1, end1], [start2, end2], ...]
1277 for (int i = 0, n = transferred.length; i < n; i += 2)
1279 toRanges.add(new int[] { transferred[i], transferred[i + 1] });
1283 return new MapList(getFromRanges(), toRanges, outFromRatio, outToRatio);