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