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 getHiddenBoundaryRight(int alPos)
646 LOCK.readLock().lock();
647 if (!hiddenColumns.isEmpty())
649 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
650 if (index < hiddenColumns.size())
652 int[] region = hiddenColumns.get(index);
653 if (alPos < region[0])
657 else if ((alPos <= region[1])
658 && (index + 1 < hiddenColumns.size()))
660 // alPos is within a hidden region, return the next one
662 region = hiddenColumns.get(index + 1);
670 LOCK.readLock().unlock();
675 * This method returns the leftmost limit of a region of an alignment with
676 * hidden columns. In otherwords, the previous hidden column.
679 * the absolute (visible) alignmentPosition to find the previous
682 public int getHiddenBoundaryLeft(int alPos)
686 LOCK.readLock().lock();
688 if (!hiddenColumns.isEmpty())
690 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
694 int[] region = hiddenColumns.get(index - 1);
701 LOCK.readLock().unlock();
707 * Answers if a column in the alignment is visible
710 * absolute position of column in the alignment
711 * @return true if column is visible
713 public boolean isVisible(int column)
717 LOCK.readLock().lock();
719 int regionindex = cursor.findRegionForColumn(column).getRegionIndex();
720 if (regionindex > -1 && regionindex < hiddenColumns.size())
722 int[] region = hiddenColumns.get(regionindex);
723 // already know that column <= region[1] as cursor returns containing
724 // region or region to right
725 if (column >= region[0])
734 LOCK.readLock().unlock();
739 * Get the visible sections of a set of sequences
742 * sequence position to start from
744 * sequence position to end at
746 * an array of sequences
747 * @return an array of strings encoding the visible parts of each sequence
749 public String[] getVisibleSequenceStrings(int start, int end,
754 LOCK.readLock().lock();
755 int iSize = seqs.length;
756 String[] selections = new String[iSize];
757 if (!hiddenColumns.isEmpty())
759 for (int i = 0; i < iSize; i++)
761 StringBuilder visibleSeq = new StringBuilder();
763 Iterator<int[]> blocks = new VisibleContigsIterator(start,
764 end + 1, hiddenColumns);
766 while (blocks.hasNext())
768 int[] block = blocks.next();
769 if (blocks.hasNext())
772 .append(seqs[i].getSequence(block[0], block[1] + 1));
777 .append(seqs[i].getSequence(block[0], block[1]));
781 selections[i] = visibleSeq.toString();
786 for (int i = 0; i < iSize; i++)
788 selections[i] = seqs[i].getSequenceAsString(start, end);
795 LOCK.readLock().unlock();
800 * Locate the first position visible for this sequence. If seq isn't visible
801 * then return the position of the left side of the hidden boundary region.
804 * sequence to find position for
805 * @return visible start position
807 public int locateVisibleStartOfSequence(SequenceI seq)
811 LOCK.readLock().lock();
814 if (hiddenColumns.isEmpty())
816 return seq.findIndex(seq.getStart()) - 1;
819 // Simply walk along the sequence whilst watching for hidden column
821 Iterator<int[]> regions = hiddenColumns.iterator();
822 int hideStart = seq.getLength();
826 boolean foundStart = false;
828 // step through the non-gapped positions of the sequence
829 for (int i = seq.getStart(); i <= seq.getEnd() && (!foundStart); i++)
831 // get alignment position of this residue in the sequence
832 int p = seq.findIndex(i) - 1;
834 // update hidden region start/end
835 while (hideEnd < p && regions.hasNext())
837 int[] region = regions.next();
839 visNext += region[0] - visPrev;
840 hideStart = region[0];
845 hideStart = seq.getLength();
847 // update visible boundary for sequence
857 return absoluteToVisibleColumn(start);
859 // otherwise, sequence was completely hidden
863 LOCK.readLock().unlock();
870 * @return true if there are columns hidden
872 public boolean hasHiddenColumns()
876 LOCK.readLock().lock();
878 // we don't use getSize()>0 here because it has to iterate over
879 // the full hiddenColumns collection and so will be much slower
880 return (!hiddenColumns.isEmpty());
883 LOCK.readLock().unlock();
889 * @return true if there is more than one hidden column region
891 public boolean hasMultiHiddenColumnRegions()
895 LOCK.readLock().lock();
896 return !hiddenColumns.isEmpty() && hiddenColumns.size() > 1;
899 LOCK.readLock().unlock();
905 * Returns a hashCode built from hidden column ranges
908 public int hashCode()
912 LOCK.readLock().lock();
914 for (int[] hidden : hiddenColumns)
916 hashCode = HASH_MULTIPLIER * hashCode + hidden[0];
917 hashCode = HASH_MULTIPLIER * hashCode + hidden[1];
922 LOCK.readLock().unlock();
927 * Hide columns corresponding to the marked bits
930 * - columns mapped to bits starting from zero
932 public void hideColumns(BitSet inserts)
936 LOCK.writeLock().lock();
937 for (int firstSet = inserts
938 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
939 .nextSetBit(lastSet))
941 lastSet = inserts.nextClearBit(firstSet);
942 hideColumns(firstSet, lastSet - 1);
944 cursor.resetCursor(hiddenColumns);
948 LOCK.writeLock().unlock();
953 * Hide columns corresponding to the marked bits, within the range
954 * [start,end]. Entries in tohide which are outside [start,end] are ignored.
957 * columns mapped to bits starting from zero
959 * start of range to hide columns within
961 * end of range to hide columns within
963 public void hideColumns(BitSet tohide, int start, int end)
965 clearHiddenColumnsInRange(start, end);
967 // make sure only bits between start and end are set
968 if (!tohide.isEmpty())
970 tohide.clear(0, start);
971 tohide.clear(Math.min(end + 1, tohide.length() + 1),
972 tohide.length() + 1);
979 * Make all columns in the range [start,end] visible
982 * start of range to show columns
984 * end of range to show columns
986 private void clearHiddenColumnsInRange(int start, int end)
990 LOCK.writeLock().lock();
992 if (!hiddenColumns.isEmpty())
994 HiddenCursorPosition pos = cursor.findRegionForColumn(start);
995 int index = pos.getRegionIndex();
996 int startindex = index; // first index in hiddenColumns to remove
998 if (index != -1 && index != hiddenColumns.size())
1000 // regionIndex is the region which either contains start
1001 // or lies to the right of start
1002 int[] region = hiddenColumns.get(index);
1003 if (region[0] < start && region[1] >= start)
1005 // region contains start, truncate so that it ends just before start
1006 region[1] = start - 1;
1011 pos = cursor.findRegionForColumn(end);
1012 index = pos.getRegionIndex();
1013 int endindex = index - 1; // last index in hiddenColumns to remove
1015 if (index != -1 && index != hiddenColumns.size())
1017 // regionIndex is the region which either contains end
1018 // or lies to the right of end
1019 int[] region = hiddenColumns.get(index);
1020 if (region[0] <= end && region[1] > end)
1022 // region contains end, truncate so that it starts just after end
1023 region[0] = end + 1;
1027 hiddenColumns.subList(startindex, endindex + 1).clear();
1028 cursor.resetCursor(hiddenColumns);
1033 LOCK.writeLock().unlock();
1040 * BitSet where hidden columns will be marked
1042 protected void andNot(BitSet updates)
1046 LOCK.writeLock().lock();
1048 BitSet hiddenBitSet = new BitSet();
1049 for (int[] range : hiddenColumns)
1051 hiddenBitSet.set(range[0], range[1] + 1);
1053 hiddenBitSet.andNot(updates);
1054 hiddenColumns.clear();
1055 hideColumns(hiddenBitSet);
1058 LOCK.writeLock().unlock();
1063 * Calculate the visible start and end index of an alignment.
1066 * full alignment width
1067 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1069 public int[] getVisibleStartAndEndIndex(int width)
1073 LOCK.readLock().lock();
1075 int firstVisible = 0;
1076 int lastVisible = width - 1;
1078 if (!hiddenColumns.isEmpty())
1080 // first visible col with index 0, convert to absolute index
1081 firstVisible = visibleToAbsoluteColumn(0);
1083 // last visible column is either immediately to left of
1084 // last hidden region, or is just the last column in the alignment
1085 int[] lastregion = hiddenColumns.get(hiddenColumns.size() - 1);
1086 if (lastregion[1] == width - 1)
1088 // last region is at very end of alignment
1089 // last visible column immediately precedes it
1090 lastVisible = lastregion[0] - 1;
1093 return new int[] { firstVisible, lastVisible };
1097 LOCK.readLock().unlock();
1102 * Finds the hidden region (if any) which starts or ends at res
1105 * visible residue position, unadjusted for hidden columns
1106 * @return region as [start,end] or null if no matching region is found. If
1107 * res is adjacent to two regions, returns the left region.
1109 public int[] getRegionWithEdgeAtRes(int res)
1113 LOCK.readLock().lock();
1114 int adjres = visibleToAbsoluteColumn(res);
1116 int[] reveal = null;
1118 if (!hiddenColumns.isEmpty())
1120 // look for a region ending just before adjres
1121 int regionindex = cursor.findRegionForColumn(adjres - 1)
1123 if (regionindex < hiddenColumns.size()
1124 && hiddenColumns.get(regionindex)[1] == adjres - 1)
1126 reveal = hiddenColumns.get(regionindex);
1128 // check if the region ends just after adjres
1129 else if (regionindex < hiddenColumns.size()
1130 && hiddenColumns.get(regionindex)[0] == adjres + 1)
1132 reveal = hiddenColumns.get(regionindex);
1139 LOCK.readLock().unlock();
1144 * Return an iterator over the hidden regions
1146 public Iterator<int[]> iterator()
1150 LOCK.readLock().lock();
1151 return new HiddenColsIterator(hiddenColumns);
1154 LOCK.readLock().unlock();
1159 * Return a bounded iterator over the hidden regions
1162 * position to start from (inclusive, absolute column position)
1164 * position to end at (inclusive, absolute column position)
1167 public Iterator<int[]> getBoundedIterator(int start, int end)
1171 LOCK.readLock().lock();
1172 return new HiddenColsIterator(start, end, hiddenColumns);
1175 LOCK.readLock().unlock();
1180 * Return a bounded iterator over the *visible* start positions of hidden
1184 * position to start from (inclusive, visible column position)
1186 * position to end at (inclusive, visible column position)
1188 public Iterator<Integer> getBoundedStartIterator(int start, int end)
1192 LOCK.readLock().lock();
1194 // get absolute position of column in alignment
1195 int absoluteStart = visibleToAbsoluteColumn(start);
1197 // Get cursor position and supply it to the iterator:
1198 // Since we want visible region start, we look for a cursor for the
1199 // (absoluteStart-1), then if absoluteStart is the start of a visible
1200 // region we'll get the cursor pointing to the region before, which is
1202 HiddenCursorPosition pos = cursor
1203 .findRegionForColumn(absoluteStart - 1);
1205 return new BoundedStartRegionIterator(pos, start, end,
1209 LOCK.readLock().unlock();
1214 * Return an iterator over visible *columns* (not regions) between the given
1215 * start and end boundaries
1218 * first column (inclusive)
1220 * last column (inclusive)
1222 public Iterator<Integer> getVisibleColsIterator(int start, int end)
1226 LOCK.readLock().lock();
1227 return new VisibleColsIterator(start, end, hiddenColumns);
1230 LOCK.readLock().unlock();
1235 * return an iterator over visible segments between the given start and end
1239 * first column, inclusive from 0
1241 * last column - not inclusive
1242 * @param useVisibleCoords
1243 * if true, start and end are visible column positions, not absolute
1246 public VisibleContigsIterator getVisContigsIterator(int start,
1248 boolean useVisibleCoords)
1250 int adjstart = start;
1252 if (useVisibleCoords)
1254 adjstart = visibleToAbsoluteColumn(start);
1255 adjend = visibleToAbsoluteColumn(end);
1260 LOCK.readLock().lock();
1261 return new VisibleContigsIterator(adjstart, adjend, hiddenColumns);
1264 LOCK.readLock().unlock();