2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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.
33 public Vector fromShifts;
34 public Vector toShifts;
35 int fromRatio; // number of steps in fromShifts to one toRatio unit
36 int toRatio; // number of steps in toShifts to one fromRatio
37 public MapList(int from[], int to[], int fromRatio, int toRatio)
39 fromShifts = new Vector();
40 for (int i=0;i<from.length; i+=2)
41 fromShifts.add(new int[] { from[i], from[i+1]});
42 toShifts = new Vector();
43 for (int i=0;i<to.length; i+=2)
44 toShifts.add(new int[] { to[i], to[i+1]});
45 this.fromRatio=fromRatio;
49 * get all mapped positions from 'from' to 'to'
50 * @return int[][] { int[] { fromStart, fromFinish, toStart, toFinish }, int [fromFinish-fromStart+2] { toStart..toFinish mappings}}
52 public int[][] makeFromMap() {
53 return posMap(fromShifts, fromRatio, toShifts, toRatio);
56 * get all mapped positions from 'to' to 'from'
57 * @return int[to position]=position mapped in from
59 public int[][] makeToMap() {
60 return posMap(toShifts,toRatio, fromShifts, fromRatio);
63 * construct an int map for intervals in intVals
65 * @return int[] { from, to pos in range }, int[range.to-range.from+1] returning mapped position
67 private int[][] posMap(Vector intVals, int ratio, Vector toIntVals, int toRatio) {
68 Iterator iv = intVals.iterator();
71 int[] intv=(int[]) iv.next();
72 int from=intv[0],to=intv[1];
77 while (iv.hasNext()) {
78 intv = (int[]) iv.next();
89 int mp[][] = new int[to-from+2][];
90 for (int i=0;i<mp.length; i++) {
91 int[] m = shift(i+from,intVals,ratio,toIntVals, toRatio);
106 int[][] map=new int[][] { new int[]{from, to, tF, tT}, new int[to-from+2]};
111 for (int i=0;i<mp.length; i++) {
113 map[1][i] = mp[i][0]-tF;
115 map[1][i] = -1; // indicates an out of range mapping
122 * @param pos start position for shift (in original reference frame)
123 * @param shift length of shift
125 public void addShift(int pos, int shift)
129 while (sidx<shifts.size() && (rshift=(int[]) shifts.elementAt(sidx))[0]<pos)
131 if (sidx==shifts.size())
132 shifts.insertElementAt(new int[] { pos, shift}, sidx);
138 * shift from pos to To(pos)
141 * @return int shifted position in To, frameshift in From
143 public int[] shiftFrom(int pos)
145 return shift(pos, fromShifts, fromRatio, toShifts, toRatio);
149 * inverse of shiftFrom - maps pos in To to a position in From
151 * @return shifted position in From, frameshift in To
153 public int[] shiftTo(int pos) {
154 return shift(pos, toShifts, toRatio, fromShifts, fromRatio);
164 private int[] shift(int pos, Vector fromShifts, int fromRatio, Vector toShifts, int toRatio) {
165 int[] fromCount = countPos(fromShifts.iterator(), pos);
168 int fromRemainder=(fromCount[0]-1) % fromRatio;
169 int toCount = 1+(((fromCount[0]-1) / fromRatio) * toRatio);
170 int[] toPos = countToPos(toShifts.iterator(), toCount);
172 return null; // throw new Error("Bad Mapping!");
173 //System.out.println(fromCount[0]+" "+fromCount[1]+" "+toCount);
174 return new int[] {toPos[0], fromRemainder};
177 * count how many positions pos is along the series of intervals.
180 * @return number of positions or null if pos is not within intervals
182 private int[] countPos(Iterator intVals, int pos) {
184 while (intVals.hasNext()) {
185 intv = (int[])intVals.next();
186 if (intv[0] <= intv[1]) {
187 if (pos>=intv[0] && pos<=intv[1]) {
188 return new int[] { count+pos-intv[0]+1, +1 };
190 count+=intv[1]-intv[0]+1;
193 if (pos>=intv[1] && pos<=intv[0]) {
194 return new int[] { count+intv[0]-pos+1, -1 };
196 count+=intv[0]-intv[1]+1;
203 * count out pos positions into a series of intervals and return the position
206 * @return position pos in interval set
208 private int[] countToPos(Iterator intVals, int pos) {
209 int count=0,diff=0,intv[]={0,0};
210 while (intVals.hasNext()) {
211 intv = (int[])intVals.next();
212 diff = intv[1]-intv[0];
214 if (pos<=count+1+diff) {
215 return new int[] { pos-count-1+intv[0],+1 };
220 if (pos<=count+1-diff) {
221 return new int[] { intv[0]-(pos-count-1),-1 };
227 return null;//(diff<0) ? (intv[1]-1) : (intv[0]+1);
229 public static void testMap(MapList ml, int fromS,int fromE) {
230 for (int from=1; from<=25; from++) {
231 int[] too=ml.shiftFrom(from);
232 System.out.print("ShiftFrom("+from+")==");
234 System.out.print("NaN\n");
236 System.out.print(too[0]+" % "+too[1]);
237 System.out.print("\t+--+\t");
238 int[] toofrom=ml.shiftTo(too[0]);
240 if (toofrom[0]!=from)
241 System.err.println("Mapping not reflexive:"+from+" "+too[0]+"->"+toofrom[0]);
242 System.out.println("ShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]);
244 System.out.println("ShiftTo("+too[0]+")=="+"NaN! - not Bijective Mapping!");
247 int mmap[][] = ml.makeFromMap();
248 System.out.println("FromMap : ("+mmap[0][0]+" "+mmap[0][1]+" "+mmap[0][2]+" "+mmap[0][3]+" ");
249 for (int i=1;i<=mmap[1].length; i++) {
250 if (mmap[1][i-1]==-1) {
251 System.out.print(i+"=XXX");
254 System.out.print(i+"="+(mmap[0][2]+mmap[1][i-1]));
257 System.out.print("\n");
259 System.out.print(",");
261 System.out.print("\n");
263 public static void main(String argv[]) {
264 MapList ml=new MapList(new int[] { 1,5,10,15,25,20},
265 new int[] { 51,1}, 1, 3);
266 MapList ml1=new MapList(new int[] { 1,3,17,4},
267 new int[] { 51,1}, 1, 3);
269 // test internal consistency
270 int to[] = new int[51];
271 MapList.testMap(ml, 1, 25);
273 for (int from=1; from<=51; from++) {
274 int[] too=ml.shiftTo(from);
275 int[] toofrom=ml.shiftFrom(too[0]);
276 System.out.println("ShiftFrom("+from+")=="+too[0]+" % "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]);
278 System.out.print("Success?\n"); // if we get here - something must be working!