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#clone()
36 protected Object clone() throws CloneNotSupportedException {
37 // TODO Auto-generated method stub
41 * @see java.lang.Object#equals(java.lang.Object)
43 public boolean equals(MapList obj) {
46 if (obj!=null && obj.fromRatio==fromRatio && obj.toRatio==toRatio
47 && obj.fromShifts!=null && obj.toShifts!=null) {
49 for (i=fromShifts.iterator(),j=obj.fromShifts.iterator(); i.hasNext();) {
50 int[] mi=(int[]) i.next();
53 int[] mj=(int[]) j.next();
54 if (mi[0]!=mj[0] || mi[1]!=mj[1])
59 for (i=toShifts.iterator(),j=obj.toShifts.iterator(); i.hasNext();) {
60 int[] mi=(int[]) i.next();
63 int[] mj=(int[]) j.next();
64 if (mi[0]!=mj[0] || mi[1]!=mj[1])
73 public Vector fromShifts;
74 public Vector toShifts;
75 int fromRatio; // number of steps in fromShifts to one toRatio unit
76 int toRatio; // number of steps in toShifts to one fromRatio
78 * lowest and highest value in the from Map
82 * lowest and highest value in the to Map
85 public int getFromRatio()
89 public int getToRatio()
93 public int getFromLowest() {
96 public int getFromHighest() {
99 public int getToLowest() {
102 public int getToHighest() {
105 private void ensureRange(int[] limits, int pos) {
111 public MapList(int from[], int to[], int fromRatio, int toRatio)
113 fromRange=new int[] { from[0],from[1] };
114 toRange=new int[] { to[0],to[1] };
116 fromShifts = new Vector();
117 for (int i=0;i<from.length; i+=2)
119 ensureRange(fromRange, from[i]);
120 ensureRange(fromRange, from[i+1]);
122 fromShifts.add(new int[]
123 {from[i], from[i + 1]});
125 toShifts = new Vector();
126 for (int i=0;i<to.length; i+=2)
128 ensureRange(toRange, to[i]);
129 ensureRange(toRange, to[i+1]);
130 toShifts.add(new int[]
133 this.fromRatio=fromRatio;
134 this.toRatio=toRatio;
137 * get all mapped positions from 'from' to 'to'
138 * @return int[][] { int[] { fromStart, fromFinish, toStart, toFinish }, int [fromFinish-fromStart+2] { toStart..toFinish mappings}}
140 public int[][] makeFromMap()
142 return posMap(fromShifts, fromRatio, toShifts, toRatio);
145 * get all mapped positions from 'to' to 'from'
146 * @return int[to position]=position mapped in from
148 public int[][] makeToMap()
150 return posMap(toShifts,toRatio, fromShifts, fromRatio);
153 * construct an int map for intervals in intVals
155 * @return int[] { from, to pos in range }, int[range.to-range.from+1] returning mapped position
157 private int[][] posMap(Vector intVals, int ratio, Vector toIntVals,
160 Iterator iv = intVals.iterator();
165 int[] intv=(int[]) iv.next();
166 int from=intv[0],to=intv[1];
174 intv = (int[]) iv.next();
193 int mp[][] = new int[to-from+2][];
194 for (int i = 0; i < mp.length; i++)
196 int[] m = shift(i+from,intVals,ratio,toIntVals, toRatio);
217 int[][] map = new int[][]
221 from, to, tF, tT}, new int[to - from + 2]};
226 for (int i = 0; i < mp.length; i++)
230 map[1][i] = mp[i][0]-tF;
234 map[1][i] = -1; // indicates an out of range mapping
241 * @param pos start position for shift (in original reference frame)
242 * @param shift length of shift
244 public void addShift(int pos, int shift)
248 while (sidx<shifts.size() && (rshift=(int[]) shifts.elementAt(sidx))[0]<pos)
250 if (sidx==shifts.size())
251 shifts.insertElementAt(new int[] { pos, shift}, sidx);
257 * shift from pos to To(pos)
260 * @return int shifted position in To, frameshift in From, direction of mapped symbol in To
262 public int[] shiftFrom(int pos)
264 return shift(pos, fromShifts, fromRatio, toShifts, toRatio);
268 * inverse of shiftFrom - maps pos in To to a position in From
270 * @return shifted position in From, frameshift in To, direction of mapped symbol in From
272 public int[] shiftTo(int pos)
274 return shift(pos, toShifts, toRatio, fromShifts, fromRatio);
284 private int[] shift(int pos, Vector fromShifts, int fromRatio,
285 Vector toShifts, int toRatio)
287 int[] fromCount = countPos(fromShifts.iterator(), pos);
292 int fromRemainder=(fromCount[0]-1) % fromRatio;
293 int toCount = 1+(((fromCount[0]-1) / fromRatio) * toRatio);
294 int[] toPos = countToPos(toShifts.iterator(), toCount);
297 return null; // throw new Error("Bad Mapping!");
299 //System.out.println(fromCount[0]+" "+fromCount[1]+" "+toCount);
302 toPos[0], fromRemainder, toPos[1]};
305 * count how many positions pos is along the series of intervals.
308 * @return number of positions or null if pos is not within intervals
310 private int[] countPos(Iterator intVals, int pos)
313 while (intVals.hasNext())
315 intv = (int[])intVals.next();
316 if (intv[0] <= intv[1])
318 if (pos >= intv[0] && pos <= intv[1])
322 count + pos - intv[0] + 1, +1};
326 count+=intv[1]-intv[0]+1;
331 if (pos >= intv[1] && pos <= intv[0])
335 count + intv[0] - pos + 1, -1};
339 count+=intv[0]-intv[1]+1;
346 * count out pos positions into a series of intervals and return the position
349 * @return position pos in interval set
351 private int[] countToPos(Iterator intVals, int pos)
353 int count = 0, diff = 0, intv[] =
356 while (intVals.hasNext())
358 intv = (int[])intVals.next();
359 diff = intv[1]-intv[0];
362 if (pos <= count + 1 + diff)
366 pos - count - 1 + intv[0], +1};
375 if (pos <= count + 1 - diff)
379 intv[0] - (pos - count - 1), -1};
387 return null;//(diff<0) ? (intv[1]-1) : (intv[0]+1);
390 * find series of intervals mapping from start-end in the From map.
391 * @param start position in to map
392 * @param end position in to map
393 * @return series of ranges in from map
395 public int[] locateInFrom(int start, int end) {
396 // inefficient implementation
397 int fromStart[] = shiftTo(start);
398 int fromEnd[] = shiftTo(end);
399 if (fromStart==null || fromEnd==null)
401 int iv[] = getIntervals(fromShifts, fromStart, fromEnd,fromRatio);
406 * find series of intervals mapping from start-end in the to map.
407 * @param start position in from map
408 * @param end position in from map
409 * @return series of ranges in to map
411 public int[] locateInTo(int start, int end) {
412 // inefficient implementation
413 int toStart[] = shiftFrom(start);
414 int toEnd[] = shiftFrom(end);
415 if (toStart==null || toEnd==null)
417 int iv[] = getIntervals(toShifts, toStart, toEnd, toRatio);
421 * like shift - except returns the intervals in the given vector of shifts which were spanned
422 * in traversing fromStart to fromEnd
429 private int[] getIntervals(Vector fromShifts2, int[] fromStart, int[] fromEnd, int fromRatio2)
431 // correct for word direction for start and end
432 int startpos = fromStart[0]+fromStart[2]*(fromRatio2-1);
433 int endpos = fromEnd[0]+fromEnd[2]*(fromRatio2-1);
434 Iterator intv = fromShifts2.iterator();
435 int iv[],i=0,fs=-1,fe=-1; // containing intervals
436 while (intv.hasNext() && (fs==-1 || fe==-1)) {
437 iv = (int[]) intv.next();
439 if (fs==-1 && startpos>=iv[0] && startpos<=iv[1]) {
442 if (fe==-1 && endpos>=iv[0] && endpos<=iv[1]) {
446 if (fs==-1 && startpos<=iv[0] && startpos>=iv[1]) {
449 if (fe==-1 && endpos<=iv[0] && endpos>=iv[1]) {
455 if (fs==fe && fe==-1)
457 Vector ranges=new Vector();
459 intv = fromShifts2.iterator();
465 // truncate initial interval
466 iv = (int[]) intv.next();
467 iv = new int[] { iv[0], iv[1]};// clone
471 ranges.addElement(iv); // add initial range
472 iv = (int[]) intv.next(); // get next interval
473 iv = new int[] { iv[0], iv[1]};// clone
478 ranges.addElement(iv); // add only - or final range
480 // walk from end of interval.
481 i=fromShifts2.size()-1;
485 iv = (int[]) fromShifts2.elementAt(i);
486 iv = new int[] { iv[1], iv[0]};// reverse and clone
487 // truncate initial interval
493 ranges.addElement(iv); // add (truncated) reversed interval
494 iv = (int[]) fromShifts2.elementAt(--i);
495 iv = new int[] { iv[1], iv[0] }; // reverse and clone
498 // interval is already reversed
501 ranges.addElement(iv); // add only - or final range
503 // create array of start end intervals.
505 if (ranges!=null && ranges.size()>0)
507 range = new int[ranges.size()*2];
508 intv = ranges.iterator();
510 while (intv.hasNext())
512 iv = (int[]) intv.next();
521 * test routine. not incremental.
526 public static void testMap(MapList ml, int fromS, int fromE)
528 for (int from = 1; from <= 25; from++)
530 int[] too=ml.shiftFrom(from);
531 System.out.print("ShiftFrom("+from+")==");
534 System.out.print("NaN\n");
538 System.out.print(too[0]+" % "+too[1]+" ("+too[2]+")");
539 System.out.print("\t+--+\t");
540 int[] toofrom=ml.shiftTo(too[0]);
543 if (toofrom[0]!=from)
545 System.err.println("Mapping not reflexive:" + from + " " + too[0] +
548 System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0] + " % " +
549 toofrom[1]+" ("+toofrom[2]+")");
553 System.out.println("ShiftTo(" + too[0] + ")==" +
554 "NaN! - not Bijective Mapping!");
558 int mmap[][] = ml.makeFromMap();
559 System.out.println("FromMap : (" + mmap[0][0] + " " + mmap[0][1] + " " +
560 mmap[0][2] + " " + mmap[0][3] + " ");
561 for (int i = 1; i <= mmap[1].length; i++)
563 if (mmap[1][i - 1] == -1)
565 System.out.print(i+"=XXX");
570 System.out.print(i+"="+(mmap[0][2]+mmap[1][i-1]));
574 System.out.print("\n");
578 System.out.print(",");
581 //test range function
582 System.out.print("\nTest locateInFrom\n");
584 int f=mmap[0][2],t=mmap[0][3];
586 System.out.println("Range "+f+" to "+t);
587 int rng[] = ml.locateInFrom(f,t);
590 for (int i=0; i<rng.length; i++) {
591 System.out.print(rng[i]+((i%2==0) ? "," : ";"));
596 System.out.println("No range!");
598 System.out.print("\nReversed\n");
599 rng = ml.locateInFrom(t,f);
602 for (int i=0; i<rng.length; i++) {
603 System.out.print(rng[i]+((i%2==0) ? "," : ";"));
608 System.out.println("No range!");
610 System.out.print("\n");
614 System.out.print("\n");
615 mmap = ml.makeToMap();
616 System.out.println("ToMap : (" + mmap[0][0] + " " + mmap[0][1] + " " +
617 mmap[0][2] + " " + mmap[0][3] + " ");
618 for (int i = 1; i <= mmap[1].length; i++)
620 if (mmap[1][i - 1] == -1)
622 System.out.print(i+"=XXX");
627 System.out.print(i+"="+(mmap[0][2]+mmap[1][i-1]));
631 System.out.print("\n");
635 System.out.print(",");
638 System.out.print("\n");
639 //test range function
640 System.out.print("\nTest locateInTo\n");
642 int f=mmap[0][2],t=mmap[0][3];
644 System.out.println("Range "+f+" to "+t);
645 int rng[] = ml.locateInTo(f,t);
647 for (int i=0; i<rng.length; i++) {
648 System.out.print(rng[i]+((i%2==0) ? "," : ";"));
653 System.out.println("No range!");
655 System.out.print("\nReversed\n");
656 rng = ml.locateInTo(t,f);
659 for (int i=0; i<rng.length; i++) {
660 System.out.print(rng[i]+((i%2==0) ? "," : ";"));
665 System.out.println("No range!");
668 System.out.print("\n");
674 public static void main(String argv[])
676 MapList ml = new MapList(new int[]
677 {1, 5, 10, 15, 25, 20},
680 MapList ml1 = new MapList(new int[]
684 MapList ml2 = new MapList(new int[] { 1, 60 },
685 new int[] { 1, 20 }, 3, 1);
686 // test internal consistency
687 int to[] = new int[51];
688 MapList.testMap(ml, 1, 60);
690 for (int from=1; from<=51; from++) {
691 int[] too=ml.shiftTo(from);
692 int[] toofrom=ml.shiftFrom(too[0]);
693 System.out.println("ShiftFrom("+from+")=="+too[0]+" % "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]);
695 System.out.print("Success?\n"); // if we get here - something must be working!