2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 * Simple way of bijectively mapping a non-contiguous linear range to another non-contiguous linear range
26 * Use at your own risk!
27 * TODO: efficient implementation of private posMap method
28 * TODO: test/ensure that sense of from and to ratio start position is conserved (codon start position recovery)
29 * TODO: optimize to use int[][] arrays rather than vectors.
34 * @see java.lang.Object#equals(java.lang.Object)
36 public boolean equals(MapList obj) {
39 if (obj!=null && obj.fromRatio==fromRatio && obj.toRatio==toRatio
40 && obj.fromShifts!=null && obj.toShifts!=null) {
41 int i,iSize=fromShifts.size(),j,jSize=obj.fromShifts.size();
44 for (i=0,iSize=fromShifts.size(),j=0, jSize=obj.fromShifts.size(); i<iSize;) {
45 int[] mi=(int[]) fromShifts.elementAt(i++);
46 int[] mj=(int[]) obj.fromShifts.elementAt(j++);
47 if (mi[0]!=mj[0] || mi[1]!=mj[1])
50 iSize=toShifts.size();
51 jSize=obj.toShifts.size();
54 for (i=0,j=0; i<iSize;) {
55 int[] mi=(int[]) toShifts.elementAt(i++);
56 int[] mj=(int[]) obj.toShifts.elementAt(j++);
57 if (mi[0]!=mj[0] || mi[1]!=mj[1])
64 public Vector fromShifts;
65 public Vector toShifts;
66 int fromRatio; // number of steps in fromShifts to one toRatio unit
67 int toRatio; // number of steps in toShifts to one fromRatio
71 * @return series of intervals mapped in from
73 public int[] getFromRanges()
75 return getRanges(fromShifts);
77 public int[] getToRanges()
79 return getRanges(toShifts);
82 private int[] getRanges(Vector shifts)
84 int[] rnges = new int[2*shifts.size()];
85 Enumeration e = shifts.elements();
87 while (e.hasMoreElements())
89 int r[] = (int[]) e.nextElement();
96 * lowest and highest value in the from Map
100 * lowest and highest value in the to Map
105 * @return length of mapped phrase in from
107 public int getFromRatio()
113 * @return length of mapped phrase in to
115 public int getToRatio()
119 public int getFromLowest() {
122 public int getFromHighest() {
125 public int getToLowest() {
128 public int getToHighest() {
131 private void ensureRange(int[] limits, int pos) {
137 public MapList(int from[], int to[], int fromRatio, int toRatio)
139 fromRange=new int[] { from[0],from[1] };
140 toRange=new int[] { to[0],to[1] };
142 fromShifts = new Vector();
143 for (int i=0;i<from.length; i+=2)
145 ensureRange(fromRange, from[i]);
146 ensureRange(fromRange, from[i+1]);
148 fromShifts.addElement(new int[]
149 {from[i], from[i + 1]});
151 toShifts = new Vector();
152 for (int i=0;i<to.length; i+=2)
154 ensureRange(toRange, to[i]);
155 ensureRange(toRange, to[i+1]);
156 toShifts.addElement(new int[]
159 this.fromRatio=fromRatio;
160 this.toRatio=toRatio;
162 public MapList(MapList map)
164 this.fromRange = new int[]
165 { map.fromRange[0], map.fromRange[1] };
166 this.toRange = new int[]
167 { map.toRange[0], map.toRange[1] };
168 this.fromRatio = map.fromRatio;
169 this.toRatio = map.toRatio;
170 if (map.fromShifts != null)
172 this.fromShifts = new Vector();
173 Enumeration e = map.fromShifts.elements();
174 while (e.hasMoreElements())
176 int[] el = (int[]) e.nextElement();
177 fromShifts.addElement(new int[]
181 if (map.toShifts != null)
183 this.toShifts = new Vector();
184 Enumeration e = map.toShifts.elements();
185 while (e.hasMoreElements())
187 int[] el = (int[]) e.nextElement();
188 toShifts.addElement(new int[]
194 * get all mapped positions from 'from' to 'to'
195 * @return int[][] { int[] { fromStart, fromFinish, toStart, toFinish }, int [fromFinish-fromStart+2] { toStart..toFinish mappings}}
197 public int[][] makeFromMap()
199 return posMap(fromShifts, fromRatio, toShifts, toRatio);
202 * get all mapped positions from 'to' to 'from'
203 * @return int[to position]=position mapped in from
205 public int[][] makeToMap()
207 return posMap(toShifts,toRatio, fromShifts, fromRatio);
210 * construct an int map for intervals in intVals
212 * @return int[] { from, to pos in range }, int[range.to-range.from+1] returning mapped position
214 private int[][] posMap(Vector intVals, int ratio, Vector toIntVals,
217 int iv=0,ivSize = intVals.size();
222 int[] intv=(int[]) intVals.elementAt(iv++);
223 int from=intv[0],to=intv[1];
231 intv = (int[]) intVals.elementAt(iv++);
250 int mp[][] = new int[to-from+2][];
251 for (int i = 0; i < mp.length; i++)
253 int[] m = shift(i+from,intVals,ratio,toIntVals, toRatio);
274 int[][] map = new int[][]
278 from, to, tF, tT}, new int[to - from + 2]};
283 for (int i = 0; i < mp.length; i++)
287 map[1][i] = mp[i][0]-tF;
291 map[1][i] = -1; // indicates an out of range mapping
298 * @param pos start position for shift (in original reference frame)
299 * @param shift length of shift
301 public void addShift(int pos, int shift)
305 while (sidx<shifts.size() && (rshift=(int[]) shifts.elementAt(sidx))[0]<pos)
307 if (sidx==shifts.size())
308 shifts.insertElementAt(new int[] { pos, shift}, sidx);
314 * shift from pos to To(pos)
317 * @return int shifted position in To, frameshift in From, direction of mapped symbol in To
319 public int[] shiftFrom(int pos)
321 return shift(pos, fromShifts, fromRatio, toShifts, toRatio);
325 * inverse of shiftFrom - maps pos in To to a position in From
327 * @return shifted position in From, frameshift in To, direction of mapped symbol in From
329 public int[] shiftTo(int pos)
331 return shift(pos, toShifts, toRatio, fromShifts, fromRatio);
341 private int[] shift(int pos, Vector fromShifts, int fromRatio,
342 Vector toShifts, int toRatio)
344 int[] fromCount = countPos(fromShifts, pos);
349 int fromRemainder=(fromCount[0]-1) % fromRatio;
350 int toCount = 1+(((fromCount[0]-1) / fromRatio) * toRatio);
351 int[] toPos = countToPos(toShifts, toCount);
354 return null; // throw new Error("Bad Mapping!");
356 //System.out.println(fromCount[0]+" "+fromCount[1]+" "+toCount);
359 toPos[0], fromRemainder, toPos[1]};
362 * count how many positions pos is along the series of intervals.
365 * @return number of positions or null if pos is not within intervals
367 private int[] countPos(Vector intVals, int pos)
369 int count=0,intv[],iv=0,ivSize=intVals.size();
372 intv = (int[])intVals.elementAt(iv++);
373 if (intv[0] <= intv[1])
375 if (pos >= intv[0] && pos <= intv[1])
379 count + pos - intv[0] + 1, +1};
383 count+=intv[1]-intv[0]+1;
388 if (pos >= intv[1] && pos <= intv[0])
392 count + intv[0] - pos + 1, -1};
396 count+=intv[0]-intv[1]+1;
403 * count out pos positions into a series of intervals and return the position
406 * @return position pos in interval set
408 private int[] countToPos(Vector intVals, int pos)
410 int count = 0, diff = 0, iv=0,ivSize=intVals.size(), intv[] =
415 intv = (int[])intVals.elementAt(iv++);
416 diff = intv[1]-intv[0];
419 if (pos <= count + 1 + diff)
423 pos - count - 1 + intv[0], +1};
432 if (pos <= count + 1 - diff)
436 intv[0] - (pos - count - 1), -1};
444 return null;//(diff<0) ? (intv[1]-1) : (intv[0]+1);
447 * find series of intervals mapping from start-end in the From map.
448 * @param start position in to map
449 * @param end position in to map
450 * @return series of ranges in from map
452 public int[] locateInFrom(int start, int end) {
453 // inefficient implementation
454 int fromStart[] = shiftTo(start);
455 int fromEnd[] = shiftTo(end); // needs to be inclusive of end of symbol position
456 if (fromStart==null || fromEnd==null)
458 int iv[] = getIntervals(fromShifts, fromStart, fromEnd,fromRatio);
463 * find series of intervals mapping from start-end in the to map.
464 * @param start position in from map
465 * @param end position in from map
466 * @return series of ranges in to map
468 public int[] locateInTo(int start, int end) {
469 // inefficient implementation
470 int toStart[] = shiftFrom(start);
471 int toEnd[] = shiftFrom(end);
472 if (toStart==null || toEnd==null)
474 int iv[] = getIntervals(toShifts, toStart, toEnd, toRatio);
478 * like shift - except returns the intervals in the given vector of shifts which were spanned
479 * in traversing fromStart to fromEnd
484 * @return series of from,to intervals from from first position of starting region to final position of ending region inclusive
486 private int[] getIntervals(Vector fromShifts2, int[] fromStart, int[] fromEnd, int fromRatio2)
489 startpos = fromStart[0]; // first position in fromStart
490 endpos = fromEnd[0]+fromEnd[2]*(fromRatio2-1); // last position in fromEnd
491 int intv=0,intvSize= fromShifts2.size();
492 int iv[],i=0,fs=-1,fe=-1; // containing intervals
493 while (intv<intvSize && (fs==-1 || fe==-1)) {
494 iv = (int[]) fromShifts2.elementAt(intv++);
496 if (fs==-1 && startpos>=iv[0] && startpos<=iv[1]) {
499 if (fe==-1 && endpos>=iv[0] && endpos<=iv[1]) {
503 if (fs==-1 && startpos<=iv[0] && startpos>=iv[1]) {
506 if (fe==-1 && endpos<=iv[0] && endpos>=iv[1]) {
512 if (fs==fe && fe==-1)
514 Vector ranges=new Vector();
518 // truncate initial interval
519 iv = (int[]) fromShifts2.elementAt(intv++);
520 iv = new int[] { iv[0], iv[1]};// clone
524 ranges.addElement(iv); // add initial range
525 iv = (int[]) fromShifts2.elementAt(intv++); // get next interval
526 iv = new int[] { iv[0], iv[1]};// clone
531 ranges.addElement(iv); // add only - or final range
533 // walk from end of interval.
534 i=fromShifts2.size()-1;
538 iv = (int[]) fromShifts2.elementAt(i);
539 iv = new int[] { iv[1], iv[0]};// reverse and clone
540 // truncate initial interval
545 while (--i!=fe) { // fix apparent logic bug when fe==-1
546 ranges.addElement(iv); // add (truncated) reversed interval
547 iv = (int[]) fromShifts2.elementAt(i);
548 iv = new int[] { iv[1], iv[0] }; // reverse and clone
551 // interval is already reversed
554 ranges.addElement(iv); // add only - or final range
556 // create array of start end intervals.
558 if (ranges!=null && ranges.size()>0)
560 range = new int[ranges.size()*2];
562 intvSize=ranges.size();
564 while (intv<intvSize)
566 iv = (int[]) ranges.elementAt(intv);
569 ranges.setElementAt(null, intv++); // remove
575 * get the 'initial' position of mpos in To
576 * @param mpos position in from
577 * @return position of first word in to reference frame
579 public int getToPosition(int mpos)
581 int[] mp = shiftTo(mpos);
589 * get range of positions in To frame for the mpos word in From
590 * @param mpos position in From
591 * @return null or int[] first position in To for mpos, last position in to for Mpos
593 public int[] getToWord(int mpos) {
594 int[] mp=shiftTo(mpos);
596 return new int[] {mp[0], mp[0]+mp[2]*(getFromRatio()-1)};
601 * get From position in the associated
602 * reference frame for position pos in the
603 * associated sequence.
607 public int getMappedPosition(int pos) {
608 int[] mp = shiftFrom(pos);
615 public int[] getMappedWord(int pos) {
616 int[] mp = shiftFrom(pos);
619 return new int[] { mp[0], mp[0]+mp[2]*(getToRatio()-1)};
625 * test routine. not incremental.
630 public static void testMap(MapList ml, int fromS, int fromE)
632 for (int from = 1; from <= 25; from++)
634 int[] too=ml.shiftFrom(from);
635 System.out.print("ShiftFrom("+from+")==");
638 System.out.print("NaN\n");
642 System.out.print(too[0]+" % "+too[1]+" ("+too[2]+")");
643 System.out.print("\t+--+\t");
644 int[] toofrom=ml.shiftTo(too[0]);
647 if (toofrom[0]!=from)
649 System.err.println("Mapping not reflexive:" + from + " " + too[0] +
652 System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0] + " % " +
653 toofrom[1]+" ("+toofrom[2]+")");
657 System.out.println("ShiftTo(" + too[0] + ")==" +
658 "NaN! - not Bijective Mapping!");
662 int mmap[][] = ml.makeFromMap();
663 System.out.println("FromMap : (" + mmap[0][0] + " " + mmap[0][1] + " " +
664 mmap[0][2] + " " + mmap[0][3] + " ");
665 for (int i = 1; i <= mmap[1].length; i++)
667 if (mmap[1][i - 1] == -1)
669 System.out.print(i+"=XXX");
674 System.out.print(i+"="+(mmap[0][2]+mmap[1][i-1]));
678 System.out.print("\n");
682 System.out.print(",");
685 //test range function
686 System.out.print("\nTest locateInFrom\n");
688 int f=mmap[0][2],t=mmap[0][3];
690 System.out.println("Range "+f+" to "+t);
691 int rng[] = ml.locateInFrom(f,t);
694 for (int i=0; i<rng.length; i++) {
695 System.out.print(rng[i]+((i%2==0) ? "," : ";"));
700 System.out.println("No range!");
702 System.out.print("\nReversed\n");
703 rng = ml.locateInFrom(t,f);
706 for (int i=0; i<rng.length; i++) {
707 System.out.print(rng[i]+((i%2==0) ? "," : ";"));
712 System.out.println("No range!");
714 System.out.print("\n");
718 System.out.print("\n");
719 mmap = ml.makeToMap();
720 System.out.println("ToMap : (" + mmap[0][0] + " " + mmap[0][1] + " " +
721 mmap[0][2] + " " + mmap[0][3] + " ");
722 for (int i = 1; i <= mmap[1].length; i++)
724 if (mmap[1][i - 1] == -1)
726 System.out.print(i+"=XXX");
731 System.out.print(i+"="+(mmap[0][2]+mmap[1][i-1]));
735 System.out.print("\n");
739 System.out.print(",");
742 System.out.print("\n");
743 //test range function
744 System.out.print("\nTest locateInTo\n");
746 int f=mmap[0][2],t=mmap[0][3];
748 System.out.println("Range "+f+" to "+t);
749 int rng[] = ml.locateInTo(f,t);
751 for (int i=0; i<rng.length; i++) {
752 System.out.print(rng[i]+((i%2==0) ? "," : ";"));
757 System.out.println("No range!");
759 System.out.print("\nReversed\n");
760 rng = ml.locateInTo(t,f);
763 for (int i=0; i<rng.length; i++) {
764 System.out.print(rng[i]+((i%2==0) ? "," : ";"));
769 System.out.println("No range!");
772 System.out.print("\n");
778 public static void main(String argv[])
780 MapList ml = new MapList(new int[]
781 {1, 5, 10, 15, 25, 20},
784 MapList ml1 = new MapList(new int[]
788 MapList ml2 = new MapList(new int[] { 1, 60 },
789 new int[] { 1, 20 }, 3, 1);
790 // test internal consistency
791 int to[] = new int[51];
792 MapList.testMap(ml, 1, 60);
794 for (int from=1; from<=51; from++) {
795 int[] too=ml.shiftTo(from);
796 int[] toofrom=ml.shiftFrom(too[0]);
797 System.out.println("ShiftFrom("+from+")=="+too[0]+" % "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]);
799 System.out.print("Success?\n"); // if we get here - something must be working!
803 * @return a MapList whose From range is this maplist's To Range, and vice versa
805 public MapList getInverse()
807 return new MapList(getToRanges(), getFromRanges(), getToRatio(), getFromRatio());