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
69 * lowest and highest value in the from Map
73 * lowest and highest value in the to Map
76 public int getFromRatio()
80 public int getToRatio()
84 public int getFromLowest() {
87 public int getFromHighest() {
90 public int getToLowest() {
93 public int getToHighest() {
96 private void ensureRange(int[] limits, int pos) {
102 public MapList(int from[], int to[], int fromRatio, int toRatio)
104 fromRange=new int[] { from[0],from[1] };
105 toRange=new int[] { to[0],to[1] };
107 fromShifts = new Vector();
108 for (int i=0;i<from.length; i+=2)
110 ensureRange(fromRange, from[i]);
111 ensureRange(fromRange, from[i+1]);
113 fromShifts.addElement(new int[]
114 {from[i], from[i + 1]});
116 toShifts = new Vector();
117 for (int i=0;i<to.length; i+=2)
119 ensureRange(toRange, to[i]);
120 ensureRange(toRange, to[i+1]);
121 toShifts.addElement(new int[]
124 this.fromRatio=fromRatio;
125 this.toRatio=toRatio;
127 public MapList(MapList map)
129 this.fromRange = new int[]
130 { map.fromRange[0], map.fromRange[1] };
131 this.toRange = new int[]
132 { map.toRange[0], map.toRange[1] };
133 this.fromRatio = map.fromRatio;
134 this.toRatio = map.toRatio;
135 if (map.fromShifts != null)
137 this.fromShifts = new Vector();
138 Enumeration e = map.fromShifts.elements();
139 while (e.hasMoreElements())
141 int[] el = (int[]) e.nextElement();
142 fromShifts.addElement(new int[]
146 if (map.toShifts != null)
148 this.toShifts = new Vector();
149 Enumeration e = map.toShifts.elements();
150 while (e.hasMoreElements())
152 int[] el = (int[]) e.nextElement();
153 toShifts.addElement(new int[]
159 * get all mapped positions from 'from' to 'to'
160 * @return int[][] { int[] { fromStart, fromFinish, toStart, toFinish }, int [fromFinish-fromStart+2] { toStart..toFinish mappings}}
162 public int[][] makeFromMap()
164 return posMap(fromShifts, fromRatio, toShifts, toRatio);
167 * get all mapped positions from 'to' to 'from'
168 * @return int[to position]=position mapped in from
170 public int[][] makeToMap()
172 return posMap(toShifts,toRatio, fromShifts, fromRatio);
175 * construct an int map for intervals in intVals
177 * @return int[] { from, to pos in range }, int[range.to-range.from+1] returning mapped position
179 private int[][] posMap(Vector intVals, int ratio, Vector toIntVals,
182 int iv=0,ivSize = intVals.size();
187 int[] intv=(int[]) intVals.elementAt(iv++);
188 int from=intv[0],to=intv[1];
196 intv = (int[]) intVals.elementAt(iv++);
215 int mp[][] = new int[to-from+2][];
216 for (int i = 0; i < mp.length; i++)
218 int[] m = shift(i+from,intVals,ratio,toIntVals, toRatio);
239 int[][] map = new int[][]
243 from, to, tF, tT}, new int[to - from + 2]};
248 for (int i = 0; i < mp.length; i++)
252 map[1][i] = mp[i][0]-tF;
256 map[1][i] = -1; // indicates an out of range mapping
263 * @param pos start position for shift (in original reference frame)
264 * @param shift length of shift
266 public void addShift(int pos, int shift)
270 while (sidx<shifts.size() && (rshift=(int[]) shifts.elementAt(sidx))[0]<pos)
272 if (sidx==shifts.size())
273 shifts.insertElementAt(new int[] { pos, shift}, sidx);
279 * shift from pos to To(pos)
282 * @return int shifted position in To, frameshift in From, direction of mapped symbol in To
284 public int[] shiftFrom(int pos)
286 return shift(pos, fromShifts, fromRatio, toShifts, toRatio);
290 * inverse of shiftFrom - maps pos in To to a position in From
292 * @return shifted position in From, frameshift in To, direction of mapped symbol in From
294 public int[] shiftTo(int pos)
296 return shift(pos, toShifts, toRatio, fromShifts, fromRatio);
306 private int[] shift(int pos, Vector fromShifts, int fromRatio,
307 Vector toShifts, int toRatio)
309 int[] fromCount = countPos(fromShifts, pos);
314 int fromRemainder=(fromCount[0]-1) % fromRatio;
315 int toCount = 1+(((fromCount[0]-1) / fromRatio) * toRatio);
316 int[] toPos = countToPos(toShifts, toCount);
319 return null; // throw new Error("Bad Mapping!");
321 //System.out.println(fromCount[0]+" "+fromCount[1]+" "+toCount);
324 toPos[0], fromRemainder, toPos[1]};
327 * count how many positions pos is along the series of intervals.
330 * @return number of positions or null if pos is not within intervals
332 private int[] countPos(Vector intVals, int pos)
334 int count=0,intv[],iv=0,ivSize=intVals.size();
337 intv = (int[])intVals.elementAt(iv++);
338 if (intv[0] <= intv[1])
340 if (pos >= intv[0] && pos <= intv[1])
344 count + pos - intv[0] + 1, +1};
348 count+=intv[1]-intv[0]+1;
353 if (pos >= intv[1] && pos <= intv[0])
357 count + intv[0] - pos + 1, -1};
361 count+=intv[0]-intv[1]+1;
368 * count out pos positions into a series of intervals and return the position
371 * @return position pos in interval set
373 private int[] countToPos(Vector intVals, int pos)
375 int count = 0, diff = 0, iv=0,ivSize=intVals.size(), intv[] =
380 intv = (int[])intVals.elementAt(iv++);
381 diff = intv[1]-intv[0];
384 if (pos <= count + 1 + diff)
388 pos - count - 1 + intv[0], +1};
397 if (pos <= count + 1 - diff)
401 intv[0] - (pos - count - 1), -1};
409 return null;//(diff<0) ? (intv[1]-1) : (intv[0]+1);
412 * find series of intervals mapping from start-end in the From map.
413 * @param start position in to map
414 * @param end position in to map
415 * @return series of ranges in from map
417 public int[] locateInFrom(int start, int end) {
418 // inefficient implementation
419 int fromStart[] = shiftTo(start);
420 int fromEnd[] = shiftTo(end);
421 if (fromStart==null || fromEnd==null)
423 int iv[] = getIntervals(fromShifts, fromStart, fromEnd,fromRatio);
428 * find series of intervals mapping from start-end in the to map.
429 * @param start position in from map
430 * @param end position in from map
431 * @return series of ranges in to map
433 public int[] locateInTo(int start, int end) {
434 // inefficient implementation
435 int toStart[] = shiftFrom(start);
436 int toEnd[] = shiftFrom(end);
437 if (toStart==null || toEnd==null)
439 int iv[] = getIntervals(toShifts, toStart, toEnd, toRatio);
443 * like shift - except returns the intervals in the given vector of shifts which were spanned
444 * in traversing fromStart to fromEnd
451 private int[] getIntervals(Vector fromShifts2, int[] fromStart, int[] fromEnd, int fromRatio2)
453 // correct for word direction for start and end
454 int startpos = fromStart[0]+fromStart[2]*(fromRatio2-1);
455 int endpos = fromEnd[0]+fromEnd[2]*(fromRatio2-1);
456 int intv=0,intvSize= fromShifts2.size();
457 int iv[],i=0,fs=-1,fe=-1; // containing intervals
458 while (intv<intvSize && (fs==-1 || fe==-1)) {
459 iv = (int[]) fromShifts2.elementAt(intv++);
461 if (fs==-1 && startpos>=iv[0] && startpos<=iv[1]) {
464 if (fe==-1 && endpos>=iv[0] && endpos<=iv[1]) {
468 if (fs==-1 && startpos<=iv[0] && startpos>=iv[1]) {
471 if (fe==-1 && endpos<=iv[0] && endpos>=iv[1]) {
477 if (fs==fe && fe==-1)
479 Vector ranges=new Vector();
483 // truncate initial interval
484 iv = (int[]) fromShifts2.elementAt(intv++);
485 iv = new int[] { iv[0], iv[1]};// clone
489 ranges.addElement(iv); // add initial range
490 iv = (int[]) fromShifts2.elementAt(intv++); // get next interval
491 iv = new int[] { iv[0], iv[1]};// clone
496 ranges.addElement(iv); // add only - or final range
498 // walk from end of interval.
499 i=fromShifts2.size()-1;
503 iv = (int[]) fromShifts2.elementAt(i);
504 iv = new int[] { iv[1], iv[0]};// reverse and clone
505 // truncate initial interval
511 ranges.addElement(iv); // add (truncated) reversed interval
512 iv = (int[]) fromShifts2.elementAt(--i);
513 iv = new int[] { iv[1], iv[0] }; // reverse and clone
516 // interval is already reversed
519 ranges.addElement(iv); // add only - or final range
521 // create array of start end intervals.
523 if (ranges!=null && ranges.size()>0)
525 range = new int[ranges.size()*2];
527 intvSize=ranges.size();
529 while (intv<intvSize)
531 iv = (int[]) ranges.elementAt(intv);
534 ranges.setElementAt(null, intv++); // remove
540 * test routine. not incremental.
545 public static void testMap(MapList ml, int fromS, int fromE)
547 for (int from = 1; from <= 25; from++)
549 int[] too=ml.shiftFrom(from);
550 System.out.print("ShiftFrom("+from+")==");
553 System.out.print("NaN\n");
557 System.out.print(too[0]+" % "+too[1]+" ("+too[2]+")");
558 System.out.print("\t+--+\t");
559 int[] toofrom=ml.shiftTo(too[0]);
562 if (toofrom[0]!=from)
564 System.err.println("Mapping not reflexive:" + from + " " + too[0] +
567 System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0] + " % " +
568 toofrom[1]+" ("+toofrom[2]+")");
572 System.out.println("ShiftTo(" + too[0] + ")==" +
573 "NaN! - not Bijective Mapping!");
577 int mmap[][] = ml.makeFromMap();
578 System.out.println("FromMap : (" + mmap[0][0] + " " + mmap[0][1] + " " +
579 mmap[0][2] + " " + mmap[0][3] + " ");
580 for (int i = 1; i <= mmap[1].length; i++)
582 if (mmap[1][i - 1] == -1)
584 System.out.print(i+"=XXX");
589 System.out.print(i+"="+(mmap[0][2]+mmap[1][i-1]));
593 System.out.print("\n");
597 System.out.print(",");
600 //test range function
601 System.out.print("\nTest locateInFrom\n");
603 int f=mmap[0][2],t=mmap[0][3];
605 System.out.println("Range "+f+" to "+t);
606 int rng[] = ml.locateInFrom(f,t);
609 for (int i=0; i<rng.length; i++) {
610 System.out.print(rng[i]+((i%2==0) ? "," : ";"));
615 System.out.println("No range!");
617 System.out.print("\nReversed\n");
618 rng = ml.locateInFrom(t,f);
621 for (int i=0; i<rng.length; i++) {
622 System.out.print(rng[i]+((i%2==0) ? "," : ";"));
627 System.out.println("No range!");
629 System.out.print("\n");
633 System.out.print("\n");
634 mmap = ml.makeToMap();
635 System.out.println("ToMap : (" + mmap[0][0] + " " + mmap[0][1] + " " +
636 mmap[0][2] + " " + mmap[0][3] + " ");
637 for (int i = 1; i <= mmap[1].length; i++)
639 if (mmap[1][i - 1] == -1)
641 System.out.print(i+"=XXX");
646 System.out.print(i+"="+(mmap[0][2]+mmap[1][i-1]));
650 System.out.print("\n");
654 System.out.print(",");
657 System.out.print("\n");
658 //test range function
659 System.out.print("\nTest locateInTo\n");
661 int f=mmap[0][2],t=mmap[0][3];
663 System.out.println("Range "+f+" to "+t);
664 int rng[] = ml.locateInTo(f,t);
666 for (int i=0; i<rng.length; i++) {
667 System.out.print(rng[i]+((i%2==0) ? "," : ";"));
672 System.out.println("No range!");
674 System.out.print("\nReversed\n");
675 rng = ml.locateInTo(t,f);
678 for (int i=0; i<rng.length; i++) {
679 System.out.print(rng[i]+((i%2==0) ? "," : ";"));
684 System.out.println("No range!");
687 System.out.print("\n");
693 public static void main(String argv[])
695 MapList ml = new MapList(new int[]
696 {1, 5, 10, 15, 25, 20},
699 MapList ml1 = new MapList(new int[]
703 MapList ml2 = new MapList(new int[] { 1, 60 },
704 new int[] { 1, 20 }, 3, 1);
705 // test internal consistency
706 int to[] = new int[51];
707 MapList.testMap(ml, 1, 60);
709 for (int from=1; from<=51; from++) {
710 int[] too=ml.shiftTo(from);
711 int[] toofrom=ml.shiftFrom(too[0]);
712 System.out.println("ShiftFrom("+from+")=="+too[0]+" % "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]);
714 System.out.print("Success?\n"); // if we get here - something must be working!