Clover icon

Coverage Report

  1. Project Clover database Thu Nov 27 2025 16:51:35 GMT
  2. Package jalview.util

File ShiftList.java

 

Coverage histogram

../../img/srcFileCovDistChart8.png
21% of files have more coverage

Code metrics

24
47
8
1
187
116
26
0.55
5.88
8
3.25

Classes

Class Line # Actions
ShiftList 31 47 26
0.746835574.7%
 

Contributing tests

This file is covered by 18 tests. .

Source view

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  40 toggle public ShiftList()
36    {
37  40 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  36 toggle public void addShift(int pos, int shift)
49    {
50  36 synchronized (shifts)
51    {
52  36 int sidx = 0;
53  36 int[] rshift = null;
54  ? while (sidx < shifts.size() && (rshift = shifts.get(sidx))[0] < pos)
55    {
56  8 sidx++;
57    }
58  36 if (sidx == shifts.size() || pos!=rshift[0])
59    {
60  33 shifts.add(sidx, new int[] { pos, shift });
61    }
62    else
63    {
64  3 rshift[1] += shift;
65    }
66    }
67    }
68   
69    /**
70    * update the shiftList with a new insertion.
71    * If a shift already exists at that position the shift will be extended if this one is bigger.
72    * @param pos
73    * @param shift
74    * @return true if the shiftList was modified
75    */
 
76  599 toggle public boolean extendShift(int pos, int shift)
77    {
78  599 boolean modified=false;
79  599 synchronized (shifts)
80    {
81  599 int sidx = 0;
82  599 int[] rshift = null;
83  ? while (sidx < shifts.size() && (rshift = shifts.get(sidx))[0] < pos)
84    {
85  15008 sidx++;
86    }
87  599 if (sidx == shifts.size() || pos!=rshift[0])
88    {
89  225 modified=true;
90  225 shifts.add(sidx, new int[] { pos, shift });
91    }
92    else
93    {
94  374 if (rshift[1]<shift) {
95  12 modified=true;
96  12 rshift[1] = shift;
97    }
98    }
99    }
100  599 return modified;
101    }
102    /**
103    * get shifted position for pos
104    *
105    * @param pos
106    * int
107    * @return int shifted position
108    */
 
109  211 toggle public int shift(int pos)
110    {
111  211 if (shifts.size() == 0)
112    {
113  3 return pos;
114    }
115  208 int shifted = pos;
116  208 int sidx = 0;
117  208 int rshift[];
118  ? while (sidx < shifts.size()
119    && (rshift = (shifts.get(sidx++)))[0] <= pos)
120    {
121  212 shifted += rshift[1];
122    }
123  208 return shifted;
124    }
125   
126    /**
127    * clear all shifts
128    */
 
129  0 toggle public synchronized void clear()
130    {
131  0 shifts.clear();
132    }
133   
134    /**
135    * invert the shifts
136    *
137    * @return ShiftList with inverse shift operations
138    */
 
139  0 toggle public ShiftList getInverse()
140    {
141  0 ShiftList inverse = new ShiftList();
142  0 synchronized (shifts)
143    {
144  0 if (shifts != null)
145    {
146  0 for (int[] sh : shifts)
147    {
148  0 if (sh != null)
149    {
150  0 inverse.shifts.add(new int[] { sh[0], -sh[1] });
151    }
152    }
153    }
154    }
155  0 return inverse;
156    }
157   
158    /**
159    * parse a 1d map of position 1&lt;i&lt;n to L&lt;pos[i]&lt;N such as that
160    * returned from SequenceI.gapMap()
161    *
162    * @param gapMap
163    * @return shifts from map index to mapped position
164    */
 
165  3 toggle public static ShiftList parseMap(int[] gapMap)
166    {
167  3 ShiftList shiftList = null;
168  3 if (gapMap != null && gapMap.length > 0)
169    {
170  1 shiftList = new ShiftList();
171  8 for (int i = 0, p = 0; i < gapMap.length; p++, i++)
172    {
173  7 if (p != gapMap[i])
174    {
175  4 shiftList.addShift(p, gapMap[i] - p);
176  4 p = gapMap[i];
177    }
178    }
179    }
180  3 return shiftList;
181    }
182   
 
183  87 toggle public List<int[]> getShifts()
184    {
185  87 return shifts;
186    }
187    }