2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.datamodel;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.BitSet;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.concurrent.locks.ReentrantReadWriteLock;
31 * This class manages the collection of hidden columns associated with an
32 * alignment. To iterate over the collection, or over visible columns/regions,
33 * use an iterator obtained from one of:
35 * - getBoundedIterator: iterates over the hidden regions, within some bounds,
36 * returning absolute positions
38 * - getBoundedStartIterator: iterates over the start positions of hidden
39 * regions, within some bounds, returning visible positions
41 * - getVisContigsIterator: iterates over visible regions in a range, returning
44 * - getVisibleColsIterator: iterates over the visible *columns*
46 * For performance reasons, provide bounds where possible. Note that column
47 * numbering begins at 0 throughout this class.
52 public class HiddenColumns
54 private static final int HASH_MULTIPLIER = 31;
56 private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock();
59 * Cursor which tracks the last used hidden columns region, and the number
60 * of hidden columns up to (but not including) that region.
62 private HiddenColumnsCursor cursor = new HiddenColumnsCursor();
65 * cache of the number of hidden columns
67 private int numColumns = 0;
70 * list of hidden column [start, end] ranges; the list is maintained in
71 * ascending start column order
73 private List<int[]> hiddenColumns = new ArrayList<>();
78 public HiddenColumns()
83 * Methods which change the hiddenColumns collection. These methods should
84 * use a writeLock to prevent other threads accessing the hiddenColumns
85 * collection while changes are being made. They should also reset the hidden
86 * columns cursor, and either update the hidden columns count, or set it to 0
87 * (so that it will later be updated when needed).
94 * the HiddenColumns object to copy from
96 public HiddenColumns(HiddenColumns copy)
98 this(copy, Integer.MIN_VALUE, Integer.MAX_VALUE, 0);
102 * Copy constructor within bounds and with offset. Copies hidden column
103 * regions fully contained between start and end, and offsets positions by
104 * subtracting offset.
107 * HiddenColumns instance to copy from
109 * lower bound to copy from
111 * upper bound to copy to
113 * offset to subtract from each region boundary position
116 public HiddenColumns(HiddenColumns copy, int start, int end, int offset)
120 LOCK.writeLock().lock();
124 Iterator<int[]> it = copy.getBoundedIterator(start, end);
127 int[] region = it.next();
128 // still need to check boundaries because iterator returns
129 // all overlapping regions and we need contained regions
130 if (region[0] >= start && region[1] <= end)
134 { region[0] - offset, region[1] - offset });
135 numColumns += region[1] - region[0] + 1;
138 cursor.resetCursor(hiddenColumns);
142 LOCK.writeLock().unlock();
147 * Adds the specified column range to the hidden columns collection
150 * start of range to add (absolute position in alignment)
152 * end of range to add (absolute position in alignment)
154 public void hideColumns(int start, int end)
158 LOCK.writeLock().lock();
161 int prevHiddenCount = 0;
163 if (!hiddenColumns.isEmpty())
165 // set up cursor reset values
166 HiddenCursorPosition cursorPos = cursor.findRegionForColumn(start);
167 regionindex = cursorPos.getRegionIndex();
171 // get previous index and hidden count for updating the cursor later
172 previndex = regionindex - 1;
173 int[] prevRegion = hiddenColumns.get(previndex);
174 prevHiddenCount = cursorPos.getHiddenSoFar()
175 - (prevRegion[1] - prevRegion[0] + 1);
180 * new range follows everything else; check first to avoid looping over whole hiddenColumns collection
182 if (hiddenColumns.isEmpty()
183 || start > hiddenColumns.get(hiddenColumns.size() - 1)[1])
185 hiddenColumns.add(new int[] { start, end });
190 * traverse existing hidden ranges and insert / amend / append as
193 boolean added = false;
196 added = insertRangeAtRegion(regionindex - 1, start, end);
198 if (!added && regionindex < hiddenColumns.size())
200 insertRangeAtRegion(regionindex, start, end);
204 // reset the cursor to just before our insertion point: this saves
205 // a lot of reprocessing in large alignments
206 cursor.resetCursor(hiddenColumns, previndex, prevHiddenCount);
208 // reset the number of columns so they will be recounted
213 LOCK.writeLock().unlock();
218 * Insert [start, range] at the region at index i in hiddenColumns, if
224 * start of range to insert
226 * end of range to insert
227 * @return true if range was successfully inserted
229 private boolean insertRangeAtRegion(int i, int start, int end)
231 boolean added = false;
233 int[] region = hiddenColumns.get(i);
234 if (end < region[0] - 1)
237 * insert discontiguous preceding range
239 hiddenColumns.add(i, new int[] { start, end });
242 else if (end <= region[1])
245 * new range overlaps existing, or is contiguous preceding it - adjust
248 region[0] = Math.min(region[0], start);
251 else if (start <= region[1] + 1)
254 * new range overlaps existing, or is contiguous following it - adjust
255 * start and end columns
257 region[0] = Math.min(region[0], start);
258 region[1] = Math.max(region[1], end);
261 * also update or remove any subsequent ranges
262 * that are overlapped
264 while (i < hiddenColumns.size() - 1)
266 int[] nextRegion = hiddenColumns.get(i + 1);
267 if (nextRegion[0] > end + 1)
270 * gap to next hidden range - no more to update
274 region[1] = Math.max(nextRegion[1], end);
276 // in theory this is faster than hiddenColumns.remove(i+1)
277 // benchmarking results a bit ambivalent
278 hiddenColumns.subList(i + 1, i + 2).clear();
286 * hide a list of ranges
290 public void hideList(List<int[]> ranges)
294 LOCK.writeLock().lock();
295 for (int[] r : ranges)
297 hideColumns(r[0], r[1]);
299 cursor.resetCursor(hiddenColumns);
303 LOCK.writeLock().unlock();
308 * Unhides, and adds to the selection list, all hidden columns
310 public void revealAllHiddenColumns(ColumnSelection sel)
314 LOCK.writeLock().lock();
316 for (int[] region : hiddenColumns)
318 for (int j = region[0]; j < region[1] + 1; j++)
323 hiddenColumns.clear();
324 cursor.resetCursor(hiddenColumns);
329 LOCK.writeLock().unlock();
334 * Reveals, and marks as selected, the hidden column range with the given
338 * the start column to look for
340 * the column selection to add the hidden column range to
342 public void revealHiddenColumns(int start, ColumnSelection sel)
346 LOCK.writeLock().lock();
348 if (!hiddenColumns.isEmpty())
350 int regionIndex = cursor.findRegionForColumn(start)
353 if (regionIndex != -1 && regionIndex != hiddenColumns.size())
355 // regionIndex is the region which either contains start
356 // or lies to the right of start
357 int[] region = hiddenColumns.get(regionIndex);
358 if (start == region[0])
360 for (int j = region[0]; j < region[1] + 1; j++)
364 int colsToRemove = region[1] - region[0] + 1;
365 hiddenColumns.remove(regionIndex);
367 if (hiddenColumns.isEmpty())
369 hiddenColumns.clear();
374 numColumns -= colsToRemove;
376 cursor.updateForDeletedRegion(hiddenColumns);
382 LOCK.writeLock().unlock();
390 * Methods which only need read access to the hidden columns collection.
391 * These methods should use a readLock to prevent other threads changing
392 * the hidden columns collection while it is in use.
396 * Output regions data as a string. String is in the format:
397 * reg0[0]<between>reg0[1]<delimiter>reg1[0]<between>reg1[1] ... regn[1]
400 * string to delimit regions
401 * @param betweenstring
402 * to put between start and end region values
403 * @return regions formatted according to delimiter and between strings
405 public String regionsToString(String delimiter, String between)
409 LOCK.readLock().lock();
410 StringBuilder regionBuilder = new StringBuilder();
412 boolean first = true;
413 for (int[] range : hiddenColumns)
417 regionBuilder.append(delimiter);
423 regionBuilder.append(range[0]).append(between).append(range[1]);
427 return regionBuilder.toString();
430 LOCK.readLock().unlock();
435 * Find the number of hidden columns
437 * @return number of hidden columns
443 LOCK.readLock().lock();
447 // numColumns is out of date, so recalculate
450 for (int[] range : hiddenColumns)
452 size += range[1] - range[0] + 1;
461 LOCK.readLock().unlock();
466 * Get the number of distinct hidden regions
468 * @return number of regions
470 public int getNumberOfRegions()
474 LOCK.readLock().lock();
475 return hiddenColumns.size();
478 LOCK.readLock().unlock();
483 public boolean equals(Object obj)
487 LOCK.readLock().lock();
489 if (!(obj instanceof HiddenColumns))
493 HiddenColumns that = (HiddenColumns) obj;
496 * check hidden columns are either both null, or match
499 if (that.hiddenColumns.size() != this.hiddenColumns.size())
504 Iterator<int[]> it = this.iterator();
505 Iterator<int[]> thatit = that.iterator();
508 if (!(Arrays.equals(it.next(), thatit.next())))
517 LOCK.readLock().unlock();
522 * Return absolute column index for a visible column index
525 * int column index in alignment view (count from zero)
526 * @return alignment column index for column
528 public int visibleToAbsoluteColumn(int column)
532 LOCK.readLock().lock();
535 if (!hiddenColumns.isEmpty())
537 result += cursor.findRegionForVisColumn(column).getHiddenSoFar();
543 LOCK.readLock().unlock();
548 * Use this method to find out where a column will appear in the visible
549 * alignment when hidden columns exist. If the column is not visible, then the
550 * index of the next visible column on the left will be returned (or 0 if
551 * there is no visible column on the left)
553 * @param hiddenColumn
554 * the column index in the full alignment including hidden columns
555 * @return the position of the column in the visible alignment
557 public int absoluteToVisibleColumn(int hiddenColumn)
561 LOCK.readLock().lock();
562 int result = hiddenColumn;
564 if (!hiddenColumns.isEmpty())
566 HiddenCursorPosition cursorPos = cursor
567 .findRegionForColumn(hiddenColumn);
568 int index = cursorPos.getRegionIndex();
569 int hiddenBeforeCol = cursorPos.getHiddenSoFar();
571 // just subtract hidden cols count - this works fine if column is
573 result = hiddenColumn - hiddenBeforeCol;
575 // now check in case column is hidden - it will be in the returned
577 if (index < hiddenColumns.size())
579 int[] region = hiddenColumns.get(index);
580 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
582 // actually col is hidden, return region[0]-1
583 // unless region[0]==0 in which case return 0
590 result = region[0] - 1 - hiddenBeforeCol;
596 return result; // return the shifted position after removing hidden
600 LOCK.readLock().unlock();
605 * Find the visible column which is a given visible number of columns to the
606 * left (negative visibleDistance) or right (positive visibleDistance) of
607 * startColumn. If startColumn is not visible, we use the visible column at
608 * the left boundary of the hidden region containing startColumn.
610 * @param visibleDistance
611 * the number of visible columns to offset by (left offset = negative
612 * value; right offset = positive value)
614 * the position of the column to start from (absolute position)
615 * @return the position of the column which is <visibleDistance> away
616 * (absolute position)
618 public int offsetByVisibleColumns(int visibleDistance, int startColumn)
622 LOCK.readLock().lock();
623 int start = absoluteToVisibleColumn(startColumn);
624 return visibleToAbsoluteColumn(start + visibleDistance);
628 LOCK.readLock().unlock();
633 * This method returns the rightmost limit of a region of an alignment with
634 * hidden columns. In otherwords, the next hidden column.
637 * the absolute (visible) alignmentPosition to find the next hidden
639 * @return the index of the next hidden column, or alPos if there is no next
642 public int getNextHiddenBoundary(boolean left, int alPos)
646 LOCK.readLock().lock();
647 if (!hiddenColumns.isEmpty())
649 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
651 if (left && index > 0)
653 int[] region = hiddenColumns.get(index - 1);
656 else if (!left && index < hiddenColumns.size())
658 int[] region = hiddenColumns.get(index);
659 if (alPos < region[0])
663 else if ((alPos <= region[1])
664 && (index + 1 < hiddenColumns.size()))
666 // alPos is within a hidden region, return the next one
668 region = hiddenColumns.get(index + 1);
676 LOCK.readLock().unlock();
681 * Answers if a column in the alignment is visible
684 * absolute position of column in the alignment
685 * @return true if column is visible
687 public boolean isVisible(int column)
691 LOCK.readLock().lock();
693 int regionindex = cursor.findRegionForColumn(column).getRegionIndex();
694 if (regionindex > -1 && regionindex < hiddenColumns.size())
696 int[] region = hiddenColumns.get(regionindex);
697 // already know that column <= region[1] as cursor returns containing
698 // region or region to right
699 if (column >= region[0])
708 LOCK.readLock().unlock();
713 * Locate the first position visible for this sequence. If seq isn't visible
714 * then return the position of the left side of the hidden boundary region.
717 * sequence to find position for
718 * @return visible start position
720 public int locateVisibleStartOfSequence(SequenceI seq)
724 LOCK.readLock().lock();
727 if (hiddenColumns.isEmpty())
729 return seq.findIndex(seq.getStart()) - 1;
732 // Simply walk along the sequence whilst watching for hidden column
734 Iterator<int[]> regions = hiddenColumns.iterator();
735 int hideStart = seq.getLength();
739 boolean foundStart = false;
741 // step through the non-gapped positions of the sequence
742 for (int i = seq.getStart(); i <= seq.getEnd() && (!foundStart); i++)
744 // get alignment position of this residue in the sequence
745 int p = seq.findIndex(i) - 1;
747 // update hidden region start/end
748 while (hideEnd < p && regions.hasNext())
750 int[] region = regions.next();
752 visNext += region[0] - visPrev;
753 hideStart = region[0];
758 hideStart = seq.getLength();
760 // update visible boundary for sequence
770 return absoluteToVisibleColumn(start);
772 // otherwise, sequence was completely hidden
776 LOCK.readLock().unlock();
783 * @return true if there are columns hidden
785 public boolean hasHiddenColumns()
789 LOCK.readLock().lock();
791 // we don't use getSize()>0 here because it has to iterate over
792 // the full hiddenColumns collection and so will be much slower
793 return (!hiddenColumns.isEmpty());
796 LOCK.readLock().unlock();
802 * @return true if there is more than one hidden column region
804 public boolean hasMultiHiddenColumnRegions()
808 LOCK.readLock().lock();
809 return !hiddenColumns.isEmpty() && hiddenColumns.size() > 1;
812 LOCK.readLock().unlock();
818 * Returns a hashCode built from hidden column ranges
821 public int hashCode()
825 LOCK.readLock().lock();
827 for (int[] hidden : hiddenColumns)
829 hashCode = HASH_MULTIPLIER * hashCode + hidden[0];
830 hashCode = HASH_MULTIPLIER * hashCode + hidden[1];
835 LOCK.readLock().unlock();
840 * Hide columns corresponding to the marked bits
843 * - columns mapped to bits starting from zero
845 public void hideColumns(BitSet inserts)
849 LOCK.writeLock().lock();
850 for (int firstSet = inserts
851 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
852 .nextSetBit(lastSet))
854 lastSet = inserts.nextClearBit(firstSet);
855 hideColumns(firstSet, lastSet - 1);
857 cursor.resetCursor(hiddenColumns);
861 LOCK.writeLock().unlock();
866 * Hide columns corresponding to the marked bits, within the range
867 * [start,end]. Entries in tohide which are outside [start,end] are ignored.
870 * columns mapped to bits starting from zero
872 * start of range to hide columns within
874 * end of range to hide columns within
876 public void hideColumns(BitSet tohide, int start, int end)
878 clearHiddenColumnsInRange(start, end);
880 // make sure only bits between start and end are set
881 if (!tohide.isEmpty())
883 tohide.clear(0, start);
884 tohide.clear(Math.min(end + 1, tohide.length() + 1),
885 tohide.length() + 1);
892 * Make all columns in the range [start,end] visible
895 * start of range to show columns
897 * end of range to show columns
899 private void clearHiddenColumnsInRange(int start, int end)
903 LOCK.writeLock().lock();
905 if (!hiddenColumns.isEmpty())
907 HiddenCursorPosition pos = cursor.findRegionForColumn(start);
908 int index = pos.getRegionIndex();
909 int startindex = index; // first index in hiddenColumns to remove
911 if (index != -1 && index != hiddenColumns.size())
913 // regionIndex is the region which either contains start
914 // or lies to the right of start
915 int[] region = hiddenColumns.get(index);
916 if (region[0] < start && region[1] >= start)
918 // region contains start, truncate so that it ends just before start
919 region[1] = start - 1;
924 pos = cursor.findRegionForColumn(end);
925 index = pos.getRegionIndex();
926 int endindex = index - 1; // last index in hiddenColumns to remove
928 if (index != -1 && index != hiddenColumns.size())
930 // regionIndex is the region which either contains end
931 // or lies to the right of end
932 int[] region = hiddenColumns.get(index);
933 if (region[0] <= end && region[1] > end)
935 // region contains end, truncate so that it starts just after end
940 hiddenColumns.subList(startindex, endindex + 1).clear();
941 cursor.resetCursor(hiddenColumns);
946 LOCK.writeLock().unlock();
953 * BitSet where hidden columns will be marked
955 protected void andNot(BitSet updates)
959 LOCK.writeLock().lock();
961 BitSet hiddenBitSet = new BitSet();
962 for (int[] range : hiddenColumns)
964 hiddenBitSet.set(range[0], range[1] + 1);
966 hiddenBitSet.andNot(updates);
967 hiddenColumns.clear();
968 hideColumns(hiddenBitSet);
971 LOCK.writeLock().unlock();
976 * Calculate the visible start and end index of an alignment.
979 * full alignment width
980 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
982 public int[] getVisibleStartAndEndIndex(int width)
986 LOCK.readLock().lock();
988 int firstVisible = 0;
989 int lastVisible = width - 1;
991 if (!hiddenColumns.isEmpty())
993 // first visible col with index 0, convert to absolute index
994 firstVisible = visibleToAbsoluteColumn(0);
996 // last visible column is either immediately to left of
997 // last hidden region, or is just the last column in the alignment
998 int[] lastregion = hiddenColumns.get(hiddenColumns.size() - 1);
999 if (lastregion[1] == width - 1)
1001 // last region is at very end of alignment
1002 // last visible column immediately precedes it
1003 lastVisible = lastregion[0] - 1;
1006 return new int[] { firstVisible, lastVisible };
1010 LOCK.readLock().unlock();
1015 * Finds the hidden region (if any) which starts or ends at res
1018 * visible residue position, unadjusted for hidden columns
1019 * @return region as [start,end] or null if no matching region is found. If
1020 * res is adjacent to two regions, returns the left region.
1022 public int[] getRegionWithEdgeAtRes(int res)
1026 LOCK.readLock().lock();
1027 int adjres = visibleToAbsoluteColumn(res);
1029 int[] reveal = null;
1031 if (!hiddenColumns.isEmpty())
1033 // look for a region ending just before adjres
1034 int regionindex = cursor.findRegionForColumn(adjres - 1)
1036 if (regionindex < hiddenColumns.size()
1037 && hiddenColumns.get(regionindex)[1] == adjres - 1)
1039 reveal = hiddenColumns.get(regionindex);
1041 // check if the region ends just after adjres
1042 else if (regionindex < hiddenColumns.size()
1043 && hiddenColumns.get(regionindex)[0] == adjres + 1)
1045 reveal = hiddenColumns.get(regionindex);
1052 LOCK.readLock().unlock();
1057 * Return an iterator over the hidden regions
1059 public Iterator<int[]> iterator()
1063 LOCK.readLock().lock();
1064 return new HiddenColsIterator(hiddenColumns);
1067 LOCK.readLock().unlock();
1072 * Return a bounded iterator over the hidden regions
1075 * position to start from (inclusive, absolute column position)
1077 * position to end at (inclusive, absolute column position)
1080 public Iterator<int[]> getBoundedIterator(int start, int end)
1084 LOCK.readLock().lock();
1085 return new HiddenColsIterator(start, end, hiddenColumns);
1088 LOCK.readLock().unlock();
1093 * Return a bounded iterator over the *visible* start positions of hidden
1097 * position to start from (inclusive, visible column position)
1099 * position to end at (inclusive, visible column position)
1101 public Iterator<Integer> getBoundedStartIterator(int start, int end)
1105 LOCK.readLock().lock();
1107 // get absolute position of column in alignment
1108 int absoluteStart = visibleToAbsoluteColumn(start);
1110 // Get cursor position and supply it to the iterator:
1111 // Since we want visible region start, we look for a cursor for the
1112 // (absoluteStart-1), then if absoluteStart is the start of a visible
1113 // region we'll get the cursor pointing to the region before, which is
1115 HiddenCursorPosition pos = cursor
1116 .findRegionForColumn(absoluteStart - 1);
1118 return new BoundedStartRegionIterator(pos, start, end,
1122 LOCK.readLock().unlock();
1127 * Return an iterator over visible *columns* (not regions) between the given
1128 * start and end boundaries
1131 * first column (inclusive)
1133 * last column (inclusive)
1135 public Iterator<Integer> getVisibleColsIterator(int start, int end)
1139 LOCK.readLock().lock();
1140 return new VisibleColsIterator(start, end, hiddenColumns);
1143 LOCK.readLock().unlock();
1148 * return an iterator over visible segments between the given start and end
1152 * first column, inclusive from 0
1154 * last column - not inclusive
1155 * @param useVisibleCoords
1156 * if true, start and end are visible column positions, not absolute
1159 public VisibleContigsIterator getVisContigsIterator(int start,
1161 boolean useVisibleCoords)
1163 int adjstart = start;
1165 if (useVisibleCoords)
1167 adjstart = visibleToAbsoluteColumn(start);
1168 adjend = visibleToAbsoluteColumn(end);
1173 LOCK.readLock().lock();
1174 return new VisibleContigsIterator(adjstart, adjend, hiddenColumns);
1177 LOCK.readLock().unlock();