5c1d572256ae9fcdfe58b7033466992ca84848dd
[jalview.git] / src / jalview / util / ShiftList.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 package jalview.util;\r
20 \r
21 import jalview.datamodel.SequenceI;\r
22 \r
23 import java.util.*;\r
24 \r
25 /**\r
26  * ShiftList\r
27  * Simple way of mapping a linear series to a new linear range with new points introduced.\r
28  * Use at your own risk!\r
29  * <p>Title: ShiftList</p>\r
30  *\r
31  * <p>Description: </p>\r
32  *\r
33  * <p>Copyright: Copyright (c) 2004</p>\r
34  *\r
35  * <p>Company: Dundee University</p>\r
36  *\r
37  * @author not attributable\r
38  * @version 1.0\r
39  */\r
40 public class ShiftList\r
41 {\r
42     public Vector shifts;\r
43   public ShiftList()\r
44   {\r
45     shifts = new Vector();\r
46   }\r
47 \r
48   /**\r
49    * addShift\r
50    * @param pos start position for shift (in original reference frame)\r
51    * @param shift length of shift\r
52    */\r
53   public void addShift(int pos, int shift)\r
54   {\r
55     int sidx = 0;\r
56     int[] rshift=null;\r
57     while (sidx<shifts.size() && (rshift=(int[]) shifts.elementAt(sidx))[0]<pos)\r
58       sidx++;\r
59     if (sidx==shifts.size())\r
60       shifts.insertElementAt(new int[] { pos, shift}, sidx);\r
61     else\r
62       rshift[1]+=shift;\r
63   }\r
64 \r
65   /**\r
66    * shift\r
67    *\r
68    * @param pos int\r
69    * @return int shifted position\r
70    */\r
71   public int shift(int pos)\r
72   {\r
73     if (shifts.size()==0)\r
74       return pos;\r
75     int shifted=pos;\r
76     int sidx=0;\r
77     int rshift[];\r
78     while (sidx<shifts.size()\r
79            &&\r
80            (rshift=((int[]) shifts.elementAt(sidx++)))[0]<=pos) {\r
81       shifted += rshift[1];\r
82     }\r
83     return shifted;\r
84   }\r
85 \r
86   /**\r
87    * clear all shifts\r
88    */\r
89   public void clear()\r
90   {\r
91     shifts.removeAllElements();\r
92   }\r
93   /**\r
94    * invert the shifts\r
95    * @return ShiftList with inverse shift operations\r
96    */\r
97   public ShiftList getInverse() {\r
98     ShiftList inverse=new ShiftList();\r
99     if (shifts!=null) {\r
100       for (int i=0,j=shifts.size(); i<j; i++) {\r
101         int[] sh=(int[]) shifts.elementAt(i);\r
102         if (sh!=null)\r
103           inverse.shifts.addElement(new int[] {sh[0], -sh[1]});\r
104       }\r
105     }\r
106     return inverse;\r
107   }\r
108 \r
109   /**\r
110    * parse a 1d map of position 1<i<n to L<pos[i]<N\r
111    * such as that returned from SequenceI.gapMap()\r
112    * @param gapMap\r
113    * @return\r
114    */\r
115   public static ShiftList parseMap(int[] gapMap) {\r
116     ShiftList shiftList = null;\r
117     if (gapMap!=null && gapMap.length>0) {\r
118       shiftList=new ShiftList();\r
119 \r
120       for (int i=0,p=0; i<gapMap.length; p++,i++) {\r
121         if (p!=gapMap[i]) {\r
122           shiftList.addShift(p, gapMap[i]-p);\r
123           p=gapMap[i];\r
124         }\r
125       }\r
126     }\r
127     return shiftList;\r
128   }\r
129 }\r