update author list in license for (JAL-826)
[jalview.git] / src / jalview / util / ShiftList.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)\r
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle\r
4  * \r
5  * This file is part of Jalview.\r
6  * \r
7  * Jalview is free software: you can redistribute it and/or\r
8  * modify it under the terms of the GNU General Public License \r
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.\r
10  * \r
11  * Jalview is distributed in the hope that it will be useful, but \r
12  * WITHOUT ANY WARRANTY; without even the implied warranty \r
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR \r
14  * PURPOSE.  See the GNU General Public License for more details.\r
15  * \r
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.\r
17  */\r
18 package jalview.util;\r
19 \r
20 import java.util.*;\r
21 \r
22 /**\r
23  * ShiftList Simple way of mapping a linear series to a new linear range with\r
24  * new points introduced. Use at your own risk! Now growing to be used for\r
25  * interval ranges (position, offset) storing deletions/insertions\r
26  */\r
27 public class ShiftList\r
28 {\r
29   public Vector shifts;\r
30 \r
31   public ShiftList()\r
32   {\r
33     shifts = new Vector();\r
34   }\r
35 \r
36   /**\r
37    * addShift\r
38    * \r
39    * @param pos\r
40    *          start position for shift (in original reference frame)\r
41    * @param shift\r
42    *          length of shift\r
43    */\r
44   public void addShift(int pos, int shift)\r
45   {\r
46     int sidx = 0;\r
47     int[] rshift = null;\r
48     while (sidx < shifts.size()\r
49             && (rshift = (int[]) shifts.elementAt(sidx))[0] < pos)\r
50     {\r
51       sidx++;\r
52     }\r
53     if (sidx == shifts.size())\r
54     {\r
55       shifts.insertElementAt(new int[]\r
56       { pos, shift }, sidx);\r
57     }\r
58     else\r
59     {\r
60       rshift[1] += shift;\r
61     }\r
62   }\r
63 \r
64   /**\r
65    * shift\r
66    * \r
67    * @param pos\r
68    *          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     {\r
75       return pos;\r
76     }\r
77     int shifted = pos;\r
78     int sidx = 0;\r
79     int rshift[];\r
80     while (sidx < shifts.size()\r
81             && (rshift = ((int[]) shifts.elementAt(sidx++)))[0] <= pos)\r
82     {\r
83       shifted += rshift[1];\r
84     }\r
85     return shifted;\r
86   }\r
87 \r
88   /**\r
89    * clear all shifts\r
90    */\r
91   public void clear()\r
92   {\r
93     shifts.removeAllElements();\r
94   }\r
95 \r
96   /**\r
97    * invert the shifts\r
98    * \r
99    * @return ShiftList with inverse shift operations\r
100    */\r
101   public ShiftList getInverse()\r
102   {\r
103     ShiftList inverse = new ShiftList();\r
104     if (shifts != null)\r
105     {\r
106       for (int i = 0, j = shifts.size(); i < j; i++)\r
107       {\r
108         int[] sh = (int[]) shifts.elementAt(i);\r
109         if (sh != null)\r
110         {\r
111           inverse.shifts.addElement(new int[]\r
112           { sh[0], -sh[1] });\r
113         }\r
114       }\r
115     }\r
116     return inverse;\r
117   }\r
118 \r
119   /**\r
120    * parse a 1d map of position 1<i<n to L<pos[i]<N such as that returned from\r
121    * SequenceI.gapMap()\r
122    * \r
123    * @param gapMap\r
124    * @return shifts from map index to mapped position\r
125    */\r
126   public static ShiftList parseMap(int[] gapMap)\r
127   {\r
128     ShiftList shiftList = null;\r
129     if (gapMap != null && gapMap.length > 0)\r
130     {\r
131       shiftList = new ShiftList();\r
132       for (int i = 0, p = 0; i < gapMap.length; p++, i++)\r
133       {\r
134         if (p != gapMap[i])\r
135         {\r
136           shiftList.addShift(p, gapMap[i] - p);\r
137           p = gapMap[i];\r
138         }\r
139       }\r
140     }\r
141     return shiftList;\r
142   }\r
143 }\r