0c78ca13d74f0db7bfd9fbdc88f3bdaa5527d042
[jalview.git] / src / jalview / util / ShiftList.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
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.
9  *
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.
14  *
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
18  */
19 package jalview.util;
20
21 import jalview.datamodel.SequenceI;
22
23 import java.util.*;
24
25 /**
26  * ShiftList
27  * Simple way of mapping a linear series to a new linear range with new points introduced.
28  * Use at your own risk!
29  * Now growing to be used for interval ranges (position, offset) storing deletions/insertions
30  */
31 public class ShiftList
32 {
33     public Vector shifts;
34   public ShiftList()
35   {
36     shifts = new Vector();
37   }
38
39   /**
40    * addShift
41    * @param pos start position for shift (in original reference frame)
42    * @param shift length of shift
43    */
44   public void addShift(int pos, int shift)
45   {
46     int sidx = 0;
47     int[] rshift=null;
48     while (sidx<shifts.size() && (rshift=(int[]) shifts.elementAt(sidx))[0]<pos)
49       sidx++;
50     if (sidx==shifts.size())
51       shifts.insertElementAt(new int[] { pos, shift}, sidx);
52     else
53       rshift[1]+=shift;
54   }
55
56   /**
57    * shift
58    *
59    * @param pos int
60    * @return int shifted position
61    */
62   public int shift(int pos)
63   {
64     if (shifts.size()==0)
65       return pos;
66     int shifted=pos;
67     int sidx=0;
68     int rshift[];
69     while (sidx<shifts.size()
70            &&
71            (rshift=((int[]) shifts.elementAt(sidx++)))[0]<=pos) {
72       shifted += rshift[1];
73     }
74     return shifted;
75   }
76
77   /**
78    * clear all shifts
79    */
80   public void clear()
81   {
82     shifts.removeAllElements();
83   }
84   /**
85    * invert the shifts
86    * @return ShiftList with inverse shift operations
87    */
88   public ShiftList getInverse() {
89     ShiftList inverse=new ShiftList();
90     if (shifts!=null) {
91       for (int i=0,j=shifts.size(); i<j; i++) {
92         int[] sh=(int[]) shifts.get(i);
93         if (sh!=null)
94           inverse.shifts.add(new int[] {sh[0], -sh[1]});
95       }
96     }
97     return inverse;
98   }
99
100   /**
101    * parse a 1d map of position 1<i<n to L<pos[i]<N
102    * such as that returned from SequenceI.gapMap()
103    * @param gapMap
104    * @return shifts from map index to mapped position
105    */
106   public static ShiftList parseMap(int[] gapMap) {
107     ShiftList shiftList = null;
108     if (gapMap!=null && gapMap.length>0) {
109       shiftList=new ShiftList();
110       for (int i=0,p=0; i<gapMap.length; p++,i++) {
111         if (p!=gapMap[i]) {
112           shiftList.addShift(p, gapMap[i]-p);
113           p=gapMap[i];
114         }
115       }
116     }
117     return shiftList;
118   }
119 }