Formatting
[jalview.git] / src / jalview / util / ShiftList.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2007 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 java.util.*;\r
22 \r
23 /**\r
24  * ShiftList\r
25  * Simple way of mapping a linear series to a new linear range with new points introduced.\r
26  * Use at your own risk!\r
27  * Now growing to be used for interval ranges (position, offset) storing deletions/insertions\r
28  */\r
29 public class ShiftList\r
30 {\r
31   public Vector shifts;\r
32   public ShiftList()\r
33   {\r
34     shifts = new Vector();\r
35   }\r
36 \r
37   /**\r
38    * addShift\r
39    * @param pos start position for shift (in original reference frame)\r
40    * @param shift length of shift\r
41    */\r
42   public void addShift(int pos, int shift)\r
43   {\r
44     int sidx = 0;\r
45     int[] rshift = null;\r
46     while (sidx < shifts.size() &&\r
47            (rshift = (int[]) shifts.elementAt(sidx))[0] < pos)\r
48     {\r
49       sidx++;\r
50     }\r
51     if (sidx == shifts.size())\r
52     {\r
53       shifts.insertElementAt(new int[]\r
54                              {pos, shift}, sidx);\r
55     }\r
56     else\r
57     {\r
58       rshift[1] += shift;\r
59     }\r
60   }\r
61 \r
62   /**\r
63    * shift\r
64    *\r
65    * @param pos int\r
66    * @return int shifted position\r
67    */\r
68   public int shift(int pos)\r
69   {\r
70     if (shifts.size() == 0)\r
71     {\r
72       return pos;\r
73     }\r
74     int shifted = pos;\r
75     int sidx = 0;\r
76     int rshift[];\r
77     while (sidx < shifts.size()\r
78            &&\r
79            (rshift = ( (int[]) shifts.elementAt(sidx++)))[0] <= pos)\r
80     {\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   /**\r
95    * invert the shifts\r
96    * @return ShiftList with inverse shift operations\r
97    */\r
98   public ShiftList getInverse()\r
99   {\r
100     ShiftList inverse = new ShiftList();\r
101     if (shifts != null)\r
102     {\r
103       for (int i = 0, j = shifts.size(); i < j; i++)\r
104       {\r
105         int[] sh = (int[]) shifts.elementAt(i);\r
106         if (sh != null)\r
107         {\r
108           inverse.shifts.addElement(new int[]\r
109                                     {sh[0], -sh[1]});\r
110         }\r
111       }\r
112     }\r
113     return inverse;\r
114   }\r
115 \r
116   /**\r
117    * parse a 1d map of position 1<i<n to L<pos[i]<N\r
118    * such as that returned from SequenceI.gapMap()\r
119    * @param gapMap\r
120    * @return shifts from map index to mapped position\r
121    */\r
122   public static ShiftList parseMap(int[] gapMap)\r
123   {\r
124     ShiftList shiftList = null;\r
125     if (gapMap != null && gapMap.length > 0)\r
126     {\r
127       shiftList = new ShiftList();\r
128       for (int i = 0, p = 0; i < gapMap.length; p++, i++)\r
129       {\r
130         if (p != gapMap[i])\r
131         {\r
132           shiftList.addShift(p, gapMap[i] - p);\r
133           p = gapMap[i];\r
134         }\r
135       }\r
136     }\r
137     return shiftList;\r
138   }\r
139 }\r