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