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;
47 * Same format as fromShifts, for the 'mapped to' sequence
49 private List<int[]> toShifts;
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;
80 fromShifts = new ArrayList<int[]>();
81 toShifts = new ArrayList<int[]>();
85 * Two MapList objects are equal if they are the same object, or they both
86 * have populated shift ranges and all values are the same.
89 public boolean equals(Object o)
91 // TODO should also override hashCode to ensure equal objects have equal
93 if (o == null || !(o instanceof MapList))
98 MapList obj = (MapList) o;
103 if (obj.fromRatio != fromRatio || obj.toRatio != toRatio
104 || obj.fromShifts == null || obj.toShifts == null)
109 .deepEquals(fromShifts.toArray(), obj.fromShifts.toArray())
111 .deepEquals(toShifts.toArray(), obj.toShifts.toArray());
115 * Returns the 'from' ranges as {[start1, end1], [start2, end2], ...}
119 public List<int[]> getFromRanges()
125 * Returns the 'to' ranges as {[start1, end1], [start2, end2], ...}
129 public List<int[]> getToRanges()
135 * Flattens a list of [start, end] into a single [start1, end1, start2,
141 protected static int[] getRanges(List<int[]> shifts)
143 int[] rnges = new int[2 * shifts.size()];
145 for (int[] r : shifts)
155 * @return length of mapped phrase in from
157 public int getFromRatio()
164 * @return length of mapped phrase in to
166 public int getToRatio()
171 public int getFromLowest()
176 public int getFromHighest()
181 public int getToLowest()
186 public int getToHighest()
192 * Constructor given from and to ranges as [start1, end1, start2, end2,...].
193 * If any end is equal to the next start, the ranges will be merged.
196 * contiguous regions as [start1, end1, start2, end2, ...]
198 * same format as 'from'
200 * phrase length in 'from' (e.g. 3 for dna)
202 * phrase length in 'to' (e.g. 1 for protein)
204 public MapList(int from[], int to[], int fromRatio, int toRatio)
207 this.fromRatio = fromRatio;
208 this.toRatio = toRatio;
209 fromLowest = from[0];
210 fromHighest = from[1];
213 for (int i = 0; i < from.length; i += 2)
215 fromLowest = Math.min(fromLowest, from[i]);
216 fromHighest = Math.max(fromHighest, from[i + 1]);
217 if (added > 0 && from[i] == fromShifts.get(added - 1)[1])
220 * this range starts where the last ended - just extend it
222 fromShifts.get(added - 1)[1] = from[i + 1];
226 fromShifts.add(new int[] { from[i], from[i + 1] });
234 for (int i = 0; i < to.length; i += 2)
236 toLowest = Math.min(toLowest, to[i]);
237 toHighest = Math.max(toHighest, to[i + 1]);
238 if (added > 0 && to[i] == toShifts.get(added - 1)[1])
240 toShifts.get(added - 1)[1] = to[i + 1];
244 toShifts.add(new int[] { to[i], to[i + 1] });
251 * Copy constructor. Creates an identical mapping.
255 public MapList(MapList map)
258 // TODO not used - remove?
259 this.fromLowest = map.fromLowest;
260 this.fromHighest = map.fromHighest;
261 this.toLowest = map.toLowest;
262 this.toHighest = map.toHighest;
264 this.fromRatio = map.fromRatio;
265 this.toRatio = map.toRatio;
266 if (map.fromShifts != null)
268 for (int[] r : map.fromShifts)
270 fromShifts.add(new int[] { r[0], r[1] });
273 if (map.toShifts != null)
275 for (int[] r : map.toShifts)
277 toShifts.add(new int[] { r[0], r[1] });
283 * Constructor given ranges as lists of [start, end] positions
290 public MapList(List<int[]> fromRange, List<int[]> toRange, int fromRatio,
294 this.fromShifts = fromRange;
295 this.toShifts = toRange;
296 this.fromRatio = fromRatio;
297 this.toRatio = toRatio;
299 fromLowest = Integer.MAX_VALUE;
301 for (int[] range : fromRange)
303 fromLowest = Math.min(fromLowest, range[0]);
304 fromHighest = Math.max(fromHighest, range[1]);
307 toLowest = Integer.MAX_VALUE;
309 for (int[] range : toRange)
311 toLowest = Math.min(toLowest, range[0]);
312 toHighest = Math.max(toHighest, range[1]);
317 * get all mapped positions from 'from' to 'to'
319 * @return int[][] { int[] { fromStart, fromFinish, toStart, toFinish }, int
320 * [fromFinish-fromStart+2] { toStart..toFinish mappings}}
322 protected int[][] makeFromMap()
324 // TODO not used - remove??
325 return posMap(fromShifts, fromRatio, toShifts, toRatio);
329 * get all mapped positions from 'to' to 'from'
331 * @return int[to position]=position mapped in from
333 protected int[][] makeToMap()
335 // TODO not used - remove??
336 return posMap(toShifts, toRatio, fromShifts, fromRatio);
340 * construct an int map for intervals in intVals
343 * @return int[] { from, to pos in range }, int[range.to-range.from+1]
344 * returning mapped position
346 private int[][] posMap(List<int[]> shiftTo, int ratio,
347 List<int[]> shiftFrom, int toRatio)
349 // TODO not used - remove??
350 int iv = 0, ivSize = shiftTo.size();
355 int[] intv = shiftTo.get(iv++);
356 int from = intv[0], to = intv[1];
364 intv = shiftTo.get(iv++);
383 int mp[][] = new int[to - from + 2][];
384 for (int i = 0; i < mp.length; i++)
386 int[] m = shift(i + from, shiftTo, ratio, shiftFrom, toRatio);
407 int[][] map = new int[][] { new int[] { from, to, tF, tT },
408 new int[to - from + 2] };
413 for (int i = 0; i < mp.length; i++)
417 map[1][i] = mp[i][0] - tF;
421 map[1][i] = -1; // indicates an out of range mapping
431 * start position for shift (in original reference frame)
435 * public void addShift(int pos, int shift) { int sidx = 0; int[]
436 * rshift=null; while (sidx<shifts.size() && (rshift=(int[])
437 * shifts.elementAt(sidx))[0]<pos) sidx++; if (sidx==shifts.size())
438 * shifts.insertElementAt(new int[] { pos, shift}, sidx); else
439 * rshift[1]+=shift; }
443 * shift from pos to To(pos)
447 * @return int shifted position in To, frameshift in From, direction of mapped
450 public int[] shiftFrom(int pos)
452 return shift(pos, fromShifts, fromRatio, toShifts, toRatio);
456 * inverse of shiftFrom - maps pos in To to a position in From
460 * @return shifted position in From, frameshift in To, direction of mapped
463 public int[] shiftTo(int pos)
465 return shift(pos, toShifts, toRatio, fromShifts, fromRatio);
476 protected static int[] shift(int pos, List<int[]> shiftTo, int fromRatio,
477 List<int[]> shiftFrom, int toRatio)
479 // TODO: javadoc; tests
480 int[] fromCount = countPos(shiftTo, pos);
481 if (fromCount == null)
485 int fromRemainder = (fromCount[0] - 1) % fromRatio;
486 int toCount = 1 + (((fromCount[0] - 1) / fromRatio) * toRatio);
487 int[] toPos = countToPos(shiftFrom, toCount);
490 return null; // throw new Error("Bad Mapping!");
492 // System.out.println(fromCount[0]+" "+fromCount[1]+" "+toCount);
493 return new int[] { toPos[0], fromRemainder, toPos[1] };
497 * count how many positions pos is along the series of intervals.
501 * @return number of positions or null if pos is not within intervals
503 protected static int[] countPos(List<int[]> shiftTo, int pos)
505 int count = 0, intv[], iv = 0, ivSize = shiftTo.size();
508 intv = shiftTo.get(iv++);
509 if (intv[0] <= intv[1])
511 if (pos >= intv[0] && pos <= intv[1])
513 return new int[] { count + pos - intv[0] + 1, +1 };
517 count += intv[1] - intv[0] + 1;
522 if (pos >= intv[1] && pos <= intv[0])
524 return new int[] { count + intv[0] - pos + 1, -1 };
528 count += intv[0] - intv[1] + 1;
536 * count out pos positions into a series of intervals and return the position
540 * @return position pos in interval set
542 protected static int[] countToPos(List<int[]> shiftFrom, int pos)
544 int count = 0, diff = 0, iv = 0, ivSize = shiftFrom.size();
545 int[] intv = { 0, 0 };
548 intv = shiftFrom.get(iv++);
549 diff = intv[1] - intv[0];
552 if (pos <= count + 1 + diff)
554 return new int[] { pos - count - 1 + intv[0], +1 };
563 if (pos <= count + 1 - diff)
565 return new int[] { intv[0] - (pos - count - 1), -1 };
573 return null;// (diff<0) ? (intv[1]-1) : (intv[0]+1);
577 * find series of intervals mapping from start-end in the From map.
580 * position mapped 'to'
582 * position mapped 'to'
583 * @return series of [start, end] ranges in sequence mapped 'from'
585 public int[] locateInFrom(int start, int end)
587 // inefficient implementation
588 int fromStart[] = shiftTo(start);
589 // needs to be inclusive of end of symbol position
590 int fromEnd[] = shiftTo(end);
592 return getIntervals(fromShifts, fromStart, fromEnd, fromRatio);
596 * find series of intervals mapping from start-end in the to map.
599 * position mapped 'from'
601 * position mapped 'from'
602 * @return series of [start, end] ranges in sequence mapped 'to'
604 public int[] locateInTo(int start, int end)
606 int toStart[] = shiftFrom(start);
607 int toEnd[] = shiftFrom(end);
608 return getIntervals(toShifts, toStart, toEnd, toRatio);
612 * like shift - except returns the intervals in the given vector of shifts
613 * which were spanned in traversing fromStart to fromEnd
619 * @return series of from,to intervals from from first position of starting
620 * region to final position of ending region inclusive
622 protected static int[] getIntervals(List<int[]> shiftFrom,
623 int[] fromStart, int[] fromEnd, int fromRatio2)
625 if (fromStart == null || fromEnd == null)
629 int startpos, endpos;
630 startpos = fromStart[0]; // first position in fromStart
631 endpos = fromEnd[0]; // last position in fromEnd
632 int endindx = (fromRatio2 - 1); // additional positions to get to last
633 // position from endpos
634 int intv = 0, intvSize = shiftFrom.size();
635 int iv[], i = 0, fs = -1, fe_s = -1, fe = -1; // containing intervals
636 // search intervals to locate ones containing startpos and count endindx
637 // positions on from endpos
638 while (intv < intvSize && (fs == -1 || fe == -1))
640 iv = shiftFrom.get(intv++);
643 endpos = iv[0]; // start counting from beginning of interval
644 endindx--; // inclusive of endpos
648 if (fs == -1 && startpos >= iv[0] && startpos <= iv[1])
652 if (endpos >= iv[0] && endpos <= iv[1])
660 if (endpos + endindx <= iv[1])
663 endpos = endpos + endindx; // end of end token is within this
668 endindx -= iv[1] - endpos; // skip all this interval too
675 if (fs == -1 && startpos <= iv[0] && startpos >= iv[1])
679 if (endpos <= iv[0] && endpos >= iv[1])
687 if (endpos - endindx >= iv[1])
690 endpos = endpos - endindx; // end of end token is within this
695 endindx -= endpos - iv[1]; // skip all this interval too
702 if (fs == fe && fe == -1)
706 List<int[]> ranges = new ArrayList<int[]>();
711 // truncate initial interval
712 iv = shiftFrom.get(intv++);
713 iv = new int[] { iv[0], iv[1] };// clone
720 ranges.add(iv); // add initial range
721 iv = shiftFrom.get(intv++); // get next interval
722 iv = new int[] { iv[0], iv[1] };// clone
729 ranges.add(iv); // add only - or final range
733 // walk from end of interval.
734 i = shiftFrom.size() - 1;
739 iv = shiftFrom.get(i);
740 iv = new int[] { iv[1], iv[0] };// reverse and clone
741 // truncate initial interval
747 { // fix apparent logic bug when fe==-1
748 ranges.add(iv); // add (truncated) reversed interval
749 iv = shiftFrom.get(i);
750 iv = new int[] { iv[1], iv[0] }; // reverse and clone
754 // interval is already reversed
757 ranges.add(iv); // add only - or final range
759 // create array of start end intervals.
761 if (ranges != null && ranges.size() > 0)
763 range = new int[ranges.size() * 2];
765 intvSize = ranges.size();
767 while (intv < intvSize)
769 iv = ranges.get(intv);
772 ranges.set(intv++, null); // remove
779 * get the 'initial' position of mpos in To
783 * @return position of first word in to reference frame
785 public int getToPosition(int mpos)
787 // TODO not used - remove??
788 int[] mp = shiftTo(mpos);
797 * get range of positions in To frame for the mpos word in From
801 * @return null or int[] first position in To for mpos, last position in to
804 public int[] getToWord(int mpos)
806 int[] mp = shiftTo(mpos);
809 return new int[] { mp[0], mp[0] + mp[2] * (getFromRatio() - 1) };
815 * get From position in the associated reference frame for position pos in the
816 * associated sequence.
821 public int getMappedPosition(int pos)
823 // TODO not used - remove??
824 int[] mp = shiftFrom(pos);
832 public int[] getMappedWord(int pos)
834 // TODO not used - remove??
835 int[] mp = shiftFrom(pos);
838 return new int[] { mp[0], mp[0] + mp[2] * (getToRatio() - 1) };
845 * @return a MapList whose From range is this maplist's To Range, and vice
848 public MapList getInverse()
850 return new MapList(getToRanges(), getFromRanges(), getToRatio(),
855 * test for containment rather than equivalence to another mapping
858 * to be tested for containment
859 * @return true if local or mapped range map contains or is contained by this
862 public boolean containsEither(boolean local, MapList map)
864 // TODO not used - remove?
867 return ((getFromLowest() >= map.getFromLowest() && getFromHighest() <= map
868 .getFromHighest()) || (getFromLowest() <= map.getFromLowest() && getFromHighest() >= map
873 return ((getToLowest() >= map.getToLowest() && getToHighest() <= map
874 .getToHighest()) || (getToLowest() <= map.getToLowest() && getToHighest() >= map
880 * String representation - for debugging, not guaranteed not to change
883 public String toString()
885 StringBuilder sb = new StringBuilder(64);
886 sb.append("From (").append(fromRatio).append(":").append(toRatio)
888 for (int[] shift : fromShifts)
890 sb.append(" ").append(Arrays.toString(shift));
892 sb.append(" ] To [");
893 for (int[] shift : toShifts)
895 sb.append(" ").append(Arrays.toString(shift));
898 return sb.toString();