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)
42 * Subregions (base 1) described as { [start1, end1], [start2, end2], ...}
44 private List<int[]> fromShifts = new ArrayList<int[]>();
47 * Same format as fromShifts, for the 'mapped to' sequence
49 private List<int[]> toShifts = new ArrayList<int[]>();
52 * number of steps in fromShifts to one toRatio unit
54 private int fromRatio;
57 * number of steps in toShifts to one fromRatio
62 * lowest and highest value in the from Map
64 private int fromLowest;
66 private int fromHighest;
69 * lowest and highest value in the to Map
73 private int toHighest;
76 * Two MapList objects are equal if they are the same object, or they both
77 * have populated shift ranges and all values are the same.
80 public boolean equals(Object o)
82 if (o == null || !(o instanceof MapList))
87 MapList obj = (MapList) o;
92 if (obj.fromRatio != fromRatio || obj.toRatio != toRatio
93 || obj.fromShifts == null || obj.toShifts == null)
98 .deepEquals(fromShifts.toArray(), obj.fromShifts.toArray())
100 .deepEquals(toShifts.toArray(), obj.toShifts.toArray());
104 * Returns the 'from' ranges as {[start1, end1], [start2, end2], ...}
108 public List<int[]> getFromRanges()
114 * Returns the 'to' ranges as {[start1, end1], [start2, end2], ...}
118 public List<int[]> getToRanges()
124 * Flattens a list of [start, end] into a single [start1, end1, start2,
130 protected static int[] getRanges(List<int[]> shifts)
132 int[] rnges = new int[2 * shifts.size()];
134 for (int[] r : shifts)
144 * @return length of mapped phrase in from
146 public int getFromRatio()
153 * @return length of mapped phrase in to
155 public int getToRatio()
160 public int getFromLowest()
165 public int getFromHighest()
170 public int getToLowest()
175 public int getToHighest()
184 * contiguous regions as [start1, end1, start2, end2, ...]
186 * same format as 'from'
188 * phrase length in 'from' (e.g. 3 for dna)
190 * phrase length in 'to' (e.g. 1 for protein)
192 public MapList(int from[], int to[], int fromRatio, int toRatio)
194 this.fromRatio = fromRatio;
195 this.toRatio = toRatio;
196 fromLowest = from[0];
197 fromHighest = from[1];
198 for (int i = 0; i < from.length; i += 2)
200 fromLowest = Math.min(fromLowest, from[i]);
201 fromHighest = Math.max(fromHighest, from[i + 1]);
203 fromShifts.add(new int[]
204 { from[i], from[i + 1] });
209 for (int i = 0; i < to.length; i += 2)
211 toLowest = Math.min(toLowest, to[i]);
212 toHighest = Math.max(toHighest, to[i + 1]);
213 toShifts.add(new int[]
214 { to[i], to[i + 1] });
219 * Copy constructor. Creates an identical mapping.
223 public MapList(MapList map)
225 // TODO not used - remove?
226 this.fromLowest = map.fromLowest;
227 this.fromHighest = map.fromHighest;
228 this.toLowest = map.toLowest;
229 this.toHighest = map.toHighest;
231 this.fromRatio = map.fromRatio;
232 this.toRatio = map.toRatio;
233 if (map.fromShifts != null)
235 for (int[] r : map.fromShifts)
237 fromShifts.add(new int[]
241 if (map.toShifts != null)
243 for (int[] r : map.toShifts)
245 toShifts.add(new int[]
252 * Constructor given ranges as lists of [start, end] positions
259 public MapList(List<int[]> fromRange, List<int[]> toRange,
260 int fromRatio, int toRatio)
262 this.fromShifts = fromRange;
263 this.toShifts = toRange;
264 this.fromRatio = fromRatio;
265 this.toRatio = toRatio;
267 fromLowest = Integer.MAX_VALUE;
269 for (int[] range : fromRange) {
270 fromLowest = Math.min(fromLowest, range[0]);
271 fromHighest = Math.max(fromHighest, range[1]);
274 toLowest = Integer.MAX_VALUE;
276 for (int[] range : toRange)
278 toLowest = Math.min(toLowest, range[0]);
279 toHighest = Math.max(toHighest, range[1]);
284 * get all mapped positions from 'from' to 'to'
286 * @return int[][] { int[] { fromStart, fromFinish, toStart, toFinish }, int
287 * [fromFinish-fromStart+2] { toStart..toFinish mappings}}
289 protected int[][] makeFromMap()
291 // TODO not used - remove??
292 return posMap(fromShifts, fromRatio, toShifts, toRatio);
296 * get all mapped positions from 'to' to 'from'
298 * @return int[to position]=position mapped in from
300 protected int[][] makeToMap()
302 // TODO not used - remove??
303 return posMap(toShifts, toRatio, fromShifts, fromRatio);
307 * construct an int map for intervals in intVals
310 * @return int[] { from, to pos in range }, int[range.to-range.from+1]
311 * returning mapped position
313 private int[][] posMap(List<int[]> shiftTo, int ratio,
314 List<int[]> shiftFrom,
317 // TODO not used - remove??
318 int iv = 0, ivSize = shiftTo.size();
323 int[] intv = shiftTo.get(iv++);
324 int from = intv[0], to = intv[1];
332 intv = shiftTo.get(iv++);
351 int mp[][] = new int[to - from + 2][];
352 for (int i = 0; i < mp.length; i++)
354 int[] m = shift(i + from, shiftTo, ratio, shiftFrom, toRatio);
375 int[][] map = new int[][]
377 { from, to, tF, tT }, new int[to - from + 2] };
382 for (int i = 0; i < mp.length; i++)
386 map[1][i] = mp[i][0] - tF;
390 map[1][i] = -1; // indicates an out of range mapping
400 * start position for shift (in original reference frame)
404 * public void addShift(int pos, int shift) { int sidx = 0; int[]
405 * rshift=null; while (sidx<shifts.size() && (rshift=(int[])
406 * shifts.elementAt(sidx))[0]<pos) sidx++; if (sidx==shifts.size())
407 * shifts.insertElementAt(new int[] { pos, shift}, sidx); else
408 * rshift[1]+=shift; }
412 * shift from pos to To(pos)
416 * @return int shifted position in To, frameshift in From, direction of mapped
419 public int[] shiftFrom(int pos)
421 return shift(pos, fromShifts, fromRatio, toShifts, toRatio);
425 * inverse of shiftFrom - maps pos in To to a position in From
429 * @return shifted position in From, frameshift in To, direction of mapped
432 public int[] shiftTo(int pos)
434 return shift(pos, toShifts, toRatio, fromShifts, fromRatio);
445 protected static int[] shift(int pos, List<int[]> shiftTo, int fromRatio,
446 List<int[]> shiftFrom, int toRatio)
448 // TODO: javadoc; tests
449 int[] fromCount = countPos(shiftTo, pos);
450 if (fromCount == null)
454 int fromRemainder = (fromCount[0] - 1) % fromRatio;
455 int toCount = 1 + (((fromCount[0] - 1) / fromRatio) * toRatio);
456 int[] toPos = countToPos(shiftFrom, toCount);
459 return null; // throw new Error("Bad Mapping!");
461 // System.out.println(fromCount[0]+" "+fromCount[1]+" "+toCount);
463 { toPos[0], fromRemainder, toPos[1] };
467 * count how many positions pos is along the series of intervals.
471 * @return number of positions or null if pos is not within intervals
473 protected static int[] countPos(List<int[]> shiftTo, int pos)
475 int count = 0, intv[], iv = 0, ivSize = shiftTo.size();
478 intv = shiftTo.get(iv++);
479 if (intv[0] <= intv[1])
481 if (pos >= intv[0] && pos <= intv[1])
484 { count + pos - intv[0] + 1, +1 };
488 count += intv[1] - intv[0] + 1;
493 if (pos >= intv[1] && pos <= intv[0])
496 { count + intv[0] - pos + 1, -1 };
500 count += intv[0] - intv[1] + 1;
508 * count out pos positions into a series of intervals and return the position
512 * @return position pos in interval set
514 protected static int[] countToPos(List<int[]> shiftFrom, int pos)
516 int count = 0, diff = 0, iv = 0, ivSize = shiftFrom.size();
521 intv = shiftFrom.get(iv++);
522 diff = intv[1] - intv[0];
525 if (pos <= count + 1 + diff)
528 { pos - count - 1 + intv[0], +1 };
537 if (pos <= count + 1 - diff)
540 { intv[0] - (pos - count - 1), -1 };
548 return null;// (diff<0) ? (intv[1]-1) : (intv[0]+1);
552 * find series of intervals mapping from start-end in the From map.
555 * position mapped 'to'
557 * position mapped 'to'
558 * @return series of [start, end] ranges in sequence mapped 'from'
560 public int[] locateInFrom(int start, int end)
562 // inefficient implementation
563 int fromStart[] = shiftTo(start);
564 // needs to be inclusive of end of symbol position
565 int fromEnd[] = shiftTo(end);
567 return getIntervals(fromShifts, fromStart, fromEnd, fromRatio);
571 * find series of intervals mapping from start-end in the to map.
574 * position mapped 'from'
576 * position mapped 'from'
577 * @return series of [start, end] ranges in sequence mapped 'to'
579 public int[] locateInTo(int start, int end)
581 int toStart[] = shiftFrom(start);
582 int toEnd[] = shiftFrom(end);
583 return getIntervals(toShifts, toStart, toEnd, toRatio);
587 * like shift - except returns the intervals in the given vector of shifts
588 * which were spanned in traversing fromStart to fromEnd
594 * @return series of from,to intervals from from first position of starting
595 * region to final position of ending region inclusive
597 protected static int[] getIntervals(List<int[]> shiftFrom,
599 int[] fromEnd, int fromRatio2)
601 if (fromStart == null || fromEnd == null)
605 int startpos, endpos;
606 startpos = fromStart[0]; // first position in fromStart
607 endpos = fromEnd[0]; // last position in fromEnd
608 int endindx = (fromRatio2 - 1); // additional positions to get to last
609 // position from endpos
610 int intv = 0, intvSize = shiftFrom.size();
611 int iv[], i = 0, fs = -1, fe_s = -1, fe = -1; // containing intervals
612 // search intervals to locate ones containing startpos and count endindx
613 // positions on from endpos
614 while (intv < intvSize && (fs == -1 || fe == -1))
616 iv = shiftFrom.get(intv++);
619 endpos = iv[0]; // start counting from beginning of interval
620 endindx--; // inclusive of endpos
624 if (fs == -1 && startpos >= iv[0] && startpos <= iv[1])
628 if (endpos >= iv[0] && endpos <= iv[1])
636 if (endpos + endindx <= iv[1])
639 endpos = endpos + endindx; // end of end token is within this
644 endindx -= iv[1] - endpos; // skip all this interval too
651 if (fs == -1 && startpos <= iv[0] && startpos >= iv[1])
655 if (endpos <= iv[0] && endpos >= iv[1])
663 if (endpos - endindx >= iv[1])
666 endpos = endpos - endindx; // end of end token is within this
671 endindx -= endpos - iv[1]; // skip all this interval too
678 if (fs == fe && fe == -1)
682 List<int[]> ranges = new ArrayList<int[]>();
687 // truncate initial interval
688 iv = shiftFrom.get(intv++);
690 { iv[0], iv[1] };// clone
697 ranges.add(iv); // add initial range
698 iv = shiftFrom.get(intv++); // get next interval
700 { iv[0], iv[1] };// clone
707 ranges.add(iv); // add only - or final range
711 // walk from end of interval.
712 i = shiftFrom.size() - 1;
717 iv = shiftFrom.get(i);
719 { iv[1], iv[0] };// reverse and clone
720 // truncate initial interval
726 { // fix apparent logic bug when fe==-1
727 ranges.add(iv); // add (truncated) reversed interval
728 iv = shiftFrom.get(i);
730 { iv[1], iv[0] }; // reverse and clone
734 // interval is already reversed
737 ranges.add(iv); // add only - or final range
739 // create array of start end intervals.
741 if (ranges != null && ranges.size() > 0)
743 range = new int[ranges.size() * 2];
745 intvSize = ranges.size();
747 while (intv < intvSize)
749 iv = ranges.get(intv);
752 ranges.set(intv++, null); // remove
759 * get the 'initial' position of mpos in To
763 * @return position of first word in to reference frame
765 public int getToPosition(int mpos)
767 // TODO not used - remove??
768 int[] mp = shiftTo(mpos);
777 * get range of positions in To frame for the mpos word in From
781 * @return null or int[] first position in To for mpos, last position in to
784 public int[] getToWord(int mpos)
786 int[] mp = shiftTo(mpos);
790 { mp[0], mp[0] + mp[2] * (getFromRatio() - 1) };
796 * get From position in the associated reference frame for position pos in the
797 * associated sequence.
802 public int getMappedPosition(int pos)
804 // TODO not used - remove??
805 int[] mp = shiftFrom(pos);
813 public int[] getMappedWord(int pos)
815 // TODO not used - remove??
816 int[] mp = shiftFrom(pos);
820 { mp[0], mp[0] + mp[2] * (getToRatio() - 1) };
827 * @return a MapList whose From range is this maplist's To Range, and vice
830 public MapList getInverse()
832 return new MapList(getToRanges(), getFromRanges(), getToRatio(),
837 * test for containment rather than equivalence to another mapping
840 * to be tested for containment
841 * @return true if local or mapped range map contains or is contained by this
844 public boolean containsEither(boolean local, MapList map)
846 // TODO not used - remove?
849 return ((getFromLowest() >= map.getFromLowest() && getFromHighest() <= map
850 .getFromHighest()) || (getFromLowest() <= map.getFromLowest() && getFromHighest() >= map
855 return ((getToLowest() >= map.getToLowest() && getToHighest() <= map
856 .getToHighest()) || (getToLowest() <= map.getToLowest() && getToHighest() >= map