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); // needs to be inclusive of end of symbol position
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
449 * @return series of from,to intervals from from first position of starting region to final position of ending region inclusive
451 private int[] getIntervals(Vector fromShifts2, int[] fromStart, int[] fromEnd, int fromRatio2)
454 startpos = fromStart[0]; // first position in fromStart
455 endpos = fromEnd[0]+fromEnd[2]*(fromRatio2-1); // last position in fromEnd
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 * get the 'initial' position of mpos in To
541 * @param mpos position in from
542 * @return position of first word in to reference frame
544 public int getToPosition(int mpos)
546 int[] mp = shiftTo(mpos);
554 * get range of positions in To frame for the mpos word in From
555 * @param mpos position in From
556 * @return null or int[] first position in To for mpos, last position in to for Mpos
558 public int[] getToWord(int mpos) {
559 int[] mp=shiftTo(mpos);
561 return new int[] {mp[0], mp[0]+mp[2]*(getFromRatio()-1)};
566 * get From position in the associated
567 * reference frame for position pos in the
568 * associated sequence.
572 public int getMappedPosition(int pos) {
573 int[] mp = shiftFrom(pos);
580 public int[] getMappedWord(int pos) {
581 int[] mp = shiftFrom(pos);
584 return new int[] { mp[0], mp[0]+mp[2]*(getToRatio()-1)};
590 * test routine. not incremental.
595 public static void testMap(MapList ml, int fromS, int fromE)
597 for (int from = 1; from <= 25; from++)
599 int[] too=ml.shiftFrom(from);
600 System.out.print("ShiftFrom("+from+")==");
603 System.out.print("NaN\n");
607 System.out.print(too[0]+" % "+too[1]+" ("+too[2]+")");
608 System.out.print("\t+--+\t");
609 int[] toofrom=ml.shiftTo(too[0]);
612 if (toofrom[0]!=from)
614 System.err.println("Mapping not reflexive:" + from + " " + too[0] +
617 System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0] + " % " +
618 toofrom[1]+" ("+toofrom[2]+")");
622 System.out.println("ShiftTo(" + too[0] + ")==" +
623 "NaN! - not Bijective Mapping!");
627 int mmap[][] = ml.makeFromMap();
628 System.out.println("FromMap : (" + mmap[0][0] + " " + mmap[0][1] + " " +
629 mmap[0][2] + " " + mmap[0][3] + " ");
630 for (int i = 1; i <= mmap[1].length; i++)
632 if (mmap[1][i - 1] == -1)
634 System.out.print(i+"=XXX");
639 System.out.print(i+"="+(mmap[0][2]+mmap[1][i-1]));
643 System.out.print("\n");
647 System.out.print(",");
650 //test range function
651 System.out.print("\nTest locateInFrom\n");
653 int f=mmap[0][2],t=mmap[0][3];
655 System.out.println("Range "+f+" to "+t);
656 int rng[] = ml.locateInFrom(f,t);
659 for (int i=0; i<rng.length; i++) {
660 System.out.print(rng[i]+((i%2==0) ? "," : ";"));
665 System.out.println("No range!");
667 System.out.print("\nReversed\n");
668 rng = ml.locateInFrom(t,f);
671 for (int i=0; i<rng.length; i++) {
672 System.out.print(rng[i]+((i%2==0) ? "," : ";"));
677 System.out.println("No range!");
679 System.out.print("\n");
683 System.out.print("\n");
684 mmap = ml.makeToMap();
685 System.out.println("ToMap : (" + mmap[0][0] + " " + mmap[0][1] + " " +
686 mmap[0][2] + " " + mmap[0][3] + " ");
687 for (int i = 1; i <= mmap[1].length; i++)
689 if (mmap[1][i - 1] == -1)
691 System.out.print(i+"=XXX");
696 System.out.print(i+"="+(mmap[0][2]+mmap[1][i-1]));
700 System.out.print("\n");
704 System.out.print(",");
707 System.out.print("\n");
708 //test range function
709 System.out.print("\nTest locateInTo\n");
711 int f=mmap[0][2],t=mmap[0][3];
713 System.out.println("Range "+f+" to "+t);
714 int rng[] = ml.locateInTo(f,t);
716 for (int i=0; i<rng.length; i++) {
717 System.out.print(rng[i]+((i%2==0) ? "," : ";"));
722 System.out.println("No range!");
724 System.out.print("\nReversed\n");
725 rng = ml.locateInTo(t,f);
728 for (int i=0; i<rng.length; i++) {
729 System.out.print(rng[i]+((i%2==0) ? "," : ";"));
734 System.out.println("No range!");
737 System.out.print("\n");
743 public static void main(String argv[])
745 MapList ml = new MapList(new int[]
746 {1, 5, 10, 15, 25, 20},
749 MapList ml1 = new MapList(new int[]
753 MapList ml2 = new MapList(new int[] { 1, 60 },
754 new int[] { 1, 20 }, 3, 1);
755 // test internal consistency
756 int to[] = new int[51];
757 MapList.testMap(ml, 1, 60);
759 for (int from=1; from<=51; from++) {
760 int[] too=ml.shiftTo(from);
761 int[] toofrom=ml.shiftFrom(too[0]);
762 System.out.println("ShiftFrom("+from+")=="+too[0]+" % "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]);
764 System.out.print("Success?\n"); // if we get here - something must be working!