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.BitSet;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.concurrent.locks.ReentrantReadWriteLock;
30 * This class manages the collection of hidden columns associated with an
31 * alignment. To iterate over the collection, or over visible columns/regions,
32 * use an iterator obtained from one of:
34 * - getBoundedIterator: iterates over the hidden regions, within some bounds,
35 * returning absolute positions
37 * - getBoundedStartIterator: iterates over the start positions of hidden
38 * regions, within some bounds, returning visible positions
40 * - getVisContigsIterator: iterates over visible regions in a range, returning
43 * - getVisibleColsIterator: iterates over the visible *columns*
45 * For performance reasons, provide bounds where possible. Note that column
46 * numbering begins at 0 throughout this class.
51 public class HiddenColumns
53 private static final int HASH_MULTIPLIER = 31;
55 private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock();
58 * Cursor which tracks the last used hidden columns region, and the number
59 * of hidden columns up to (but not including) that region.
61 private HiddenColumnsCursor cursor = new HiddenColumnsCursor();
64 * cache of the number of hidden columns
66 private int numColumns = 0;
69 * list of hidden column [start, end] ranges; the list is maintained in
70 * ascending start column order
72 private List<int[]> hiddenColumns;
77 public HiddenColumns()
82 * Methods which change the hiddenColumns collection. These methods should
83 * use a writeLock to prevent other threads accessing the hiddenColumns
84 * collection while changes are being made. They should also reset the hidden
85 * columns cursor, and either update the hidden columns count, or set it to 0
86 * (so that it will later be updated when needed).
93 * the HiddenColumns object to copy from
95 public HiddenColumns(HiddenColumns copy)
97 this(copy, Integer.MIN_VALUE, Integer.MAX_VALUE, 0);
101 * Copy constructor within bounds and with offset. Copies hidden column
102 * regions fully contained between start and end, and offsets positions by
103 * subtracting offset.
106 * HiddenColumns instance to copy from
108 * lower bound to copy from
110 * upper bound to copy to
112 * offset to subtract from each region boundary position
115 public HiddenColumns(HiddenColumns copy, int start, int end, int offset)
119 LOCK.writeLock().lock();
122 hiddenColumns = new ArrayList<>();
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)
156 boolean wasAlreadyLocked = false;
159 // check if the write lock was already locked by this thread,
160 // as this method can be called internally in loops within HiddenColumns
161 if (!LOCK.isWriteLockedByCurrentThread())
163 LOCK.writeLock().lock();
167 wasAlreadyLocked = true;
171 int prevHiddenCount = 0;
173 if (hiddenColumns == null)
175 hiddenColumns = new ArrayList<>();
179 // set up cursor reset values
180 HiddenCursorPosition cursorPos = cursor.findRegionForColumn(start);
181 regionindex = cursorPos.getRegionIndex();
185 // get previous index and hidden count for updating the cursor later
186 previndex = regionindex - 1;
187 int[] prevRegion = hiddenColumns.get(previndex);
188 prevHiddenCount = cursorPos.getHiddenSoFar()
189 - (prevRegion[1] - prevRegion[0] + 1);
194 * new range follows everything else; check first to avoid looping over whole hiddenColumns collection
196 if (hiddenColumns.isEmpty()
197 || start > hiddenColumns.get(hiddenColumns.size() - 1)[1])
199 hiddenColumns.add(new int[] { start, end });
204 * traverse existing hidden ranges and insert / amend / append as
207 boolean added = false;
210 added = insertRangeAtRegion(regionindex - 1, start, end);
212 if (!added && regionindex < hiddenColumns.size())
214 insertRangeAtRegion(regionindex, start, end);
218 // reset the cursor to just before our insertion point: this saves
219 // a lot of reprocessing in large alignments
220 cursor.resetCursor(hiddenColumns, previndex, prevHiddenCount);
222 // reset the number of columns so they will be recounted
227 if (!wasAlreadyLocked)
229 LOCK.writeLock().unlock();
235 * Insert [start, range] at the region at index i in hiddenColumns, if
241 * start of range to insert
243 * end of range to insert
244 * @return true if range was successfully inserted
246 private boolean insertRangeAtRegion(int i, int start, int end)
248 boolean added = false;
250 int[] region = hiddenColumns.get(i);
251 if (end < region[0] - 1)
254 * insert discontiguous preceding range
256 hiddenColumns.add(i, new int[] { start, end });
259 else if (end <= region[1])
262 * new range overlaps existing, or is contiguous preceding it - adjust
265 region[0] = Math.min(region[0], start);
268 else if (start <= region[1] + 1)
271 * new range overlaps existing, or is contiguous following it - adjust
272 * start and end columns
274 region[0] = Math.min(region[0], start);
275 region[1] = Math.max(region[1], end);
278 * also update or remove any subsequent ranges
279 * that are overlapped
281 while (i < hiddenColumns.size() - 1)
283 int[] nextRegion = hiddenColumns.get(i + 1);
284 if (nextRegion[0] > end + 1)
287 * gap to next hidden range - no more to update
291 region[1] = Math.max(nextRegion[1], end);
293 // in theory this is faster than hiddenColumns.remove(i+1)
294 // benchmarking results a bit ambivalent
295 hiddenColumns.subList(i + 1, i + 2).clear();
303 * hide a list of ranges
307 public void hideList(List<int[]> ranges)
311 LOCK.writeLock().lock();
312 for (int[] r : ranges)
314 hideColumns(r[0], r[1]);
316 cursor.resetCursor(hiddenColumns);
320 LOCK.writeLock().unlock();
325 * Unhides, and adds to the selection list, all hidden columns
327 public void revealAllHiddenColumns(ColumnSelection sel)
331 LOCK.writeLock().lock();
332 if (hiddenColumns != null)
334 Iterator<int[]> it = hiddenColumns.iterator();
337 int[] region = it.next();
338 for (int j = region[0]; j < region[1] + 1; j++)
343 hiddenColumns = null;
344 cursor.resetCursor(hiddenColumns);
349 LOCK.writeLock().unlock();
354 * Reveals, and marks as selected, the hidden column range with the given
358 * the start column to look for
360 * the column selection to add the hidden column range to
362 public void revealHiddenColumns(int start, ColumnSelection sel)
366 LOCK.writeLock().lock();
368 if (hiddenColumns != null)
370 int regionIndex = cursor.findRegionForColumn(start)
373 if (regionIndex != -1 && regionIndex != hiddenColumns.size())
375 // regionIndex is the region which either contains start
376 // or lies to the right of start
377 int[] region = hiddenColumns.get(regionIndex);
378 if (start == region[0])
380 for (int j = region[0]; j < region[1] + 1; j++)
384 int colsToRemove = region[1] - region[0] + 1;
385 hiddenColumns.remove(regionIndex);
387 if (hiddenColumns.isEmpty())
389 hiddenColumns = null;
394 numColumns -= colsToRemove;
396 cursor.updateForDeletedRegion(hiddenColumns);
402 LOCK.writeLock().unlock();
407 * Add gaps into the sequences aligned to profileseq under the given
411 * sequence in al which sequences are aligned to
413 * alignment to have gaps inserted into it
415 * alignment view where sequence corresponding to profileseq is first
417 * @return new HiddenColumns for new alignment view, with insertions into
418 * profileseq marked as hidden.
420 public static HiddenColumns propagateInsertions(SequenceI profileseq,
421 AlignmentI al, AlignmentView input)
425 char gc = al.getGapCharacter();
426 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
427 HiddenColumns nview = (HiddenColumns) alandhidden[1];
428 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
429 nview.propagateInsertions(profileseq, al, origseq);
436 * sequence in al which corresponds to origseq
438 * alignment which is to have gaps inserted into it
440 * sequence corresponding to profileseq which defines gap map for
443 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
448 LOCK.writeLock().lock();
450 char gc = al.getGapCharacter();
452 // take the set of hidden columns, and the set of gaps in origseq,
453 // and remove all the hidden gaps from hiddenColumns
455 // first get the gaps as a Bitset
456 BitSet gaps = origseq.gapBitset();
458 // now calculate hidden ^ not(gap)
459 BitSet hidden = new BitSet();
460 markHiddenRegions(hidden);
462 hiddenColumns = null;
463 this.hideColumns(hidden);
465 // for each sequence in the alignment, except the profile sequence,
466 // insert gaps corresponding to each hidden region but where each hidden
467 // column region is shifted backwards by the number of preceding visible
468 // gaps update hidden columns at the same time
469 Iterator<int[]> regions = hiddenColumns.iterator();
470 List<int[]> newhidden = new ArrayList<>();
472 int numGapsBefore = 0;
474 while (regions.hasNext())
476 // get region coordinates accounting for gaps
477 // we can rely on gaps not being *in* hidden regions because we already
479 int[] region = regions.next();
480 while (gapPosition < region[0])
483 if (gaps.get(gapPosition))
489 int left = region[0] - numGapsBefore;
490 int right = region[1] - numGapsBefore;
491 newhidden.add(new int[] { left, right });
493 // make a string with number of gaps = length of hidden region
494 StringBuffer sb = new StringBuffer();
495 for (int s = 0; s < right - left + 1; s++)
499 padGaps(sb, left, profileseq, al);
502 hiddenColumns = newhidden;
503 cursor.resetCursor(hiddenColumns);
507 LOCK.writeLock().unlock();
512 * Pad gaps in all sequences in alignment except profileseq
515 * gap string to insert
517 * position to insert at
519 * sequence not to pad
521 * alignment to pad sequences in
523 private void padGaps(StringBuffer sb, int pos, SequenceI profileseq,
526 // loop over the sequences and pad with gaps where required
527 for (int s = 0, ns = al.getHeight(); s < ns; s++)
529 SequenceI sqobj = al.getSequenceAt(s);
530 if (sqobj != profileseq)
532 String sq = al.getSequenceAt(s).getSequenceAsString();
533 if (sq.length() <= pos)
536 int diff = pos - sq.length() - 1;
541 while ((diff = pos - sq.length() - 1) > 0)
543 if (diff >= sb.length())
549 char[] buf = new char[diff];
550 sb.getChars(0, diff, buf, 0);
551 sq += buf.toString();
559 al.getSequenceAt(s).setSequence(
560 sq.substring(0, pos) + sb.toString() + sq.substring(pos));
567 * Methods which only need read access to the hidden columns collection.
568 * These methods should use a readLock to prevent other threads changing
569 * the hidden columns collection while it is in use.
573 * Output regions data as a string. String is in the format:
574 * reg0[0]<between>reg0[1]<delimiter>reg1[0]<between>reg1[1] ... regn[1]
577 * string to delimit regions
578 * @param betweenstring
579 * to put between start and end region values
580 * @return regions formatted according to delimiter and between strings
582 public String regionsToString(String delimiter, String between)
586 LOCK.readLock().lock();
587 StringBuilder regionBuilder = new StringBuilder();
588 if (hiddenColumns != null)
590 Iterator<int[]> it = hiddenColumns.iterator();
593 int[] range = it.next();
594 regionBuilder.append(delimiter).append(range[0]).append(between)
598 regionBuilder.deleteCharAt(0);
602 return regionBuilder.toString();
605 LOCK.readLock().unlock();
610 * Find the number of hidden columns
612 * @return number of hidden columns
618 LOCK.readLock().lock();
620 if (numColumns == 0 && hiddenColumns != null)
622 // numColumns is out of date, so recalculate
625 Iterator<int[]> it = hiddenColumns.iterator();
628 int[] range = it.next();
629 size += range[1] - range[0] + 1;
638 LOCK.readLock().unlock();
643 * Get the number of distinct hidden regions
645 * @return number of regions
647 public int getNumberOfRegions()
651 LOCK.readLock().lock();
653 if (hasHiddenColumns())
655 num = hiddenColumns.size();
660 LOCK.readLock().unlock();
665 public boolean equals(Object obj)
669 LOCK.readLock().lock();
671 if (!(obj instanceof HiddenColumns))
675 HiddenColumns that = (HiddenColumns) obj;
678 * check hidden columns are either both null, or match
680 if (this.hiddenColumns == null)
682 return (that.hiddenColumns == null);
684 if (that.hiddenColumns == null
685 || that.hiddenColumns.size() != this.hiddenColumns.size())
690 Iterator<int[]> it = hiddenColumns.iterator();
691 Iterator<int[]> thatit = that.iterator();
694 int[] thisRange = it.next();
695 int[] thatRange = thatit.next();
696 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
704 LOCK.readLock().unlock();
709 * Return absolute column index for a visible column index
712 * int column index in alignment view (count from zero)
713 * @return alignment column index for column
715 public int visibleToAbsoluteColumn(int column)
719 LOCK.readLock().lock();
722 if (hiddenColumns != null)
724 result += cursor.findRegionForVisColumn(column).getHiddenSoFar();
730 LOCK.readLock().unlock();
735 * Use this method to find out where a column will appear in the visible
736 * alignment when hidden columns exist. If the column is not visible, then the
737 * index of the next visible column on the left will be returned (or 0 if
738 * there is no visible column on the left)
740 * @param hiddenColumn
741 * the column index in the full alignment including hidden columns
742 * @return the position of the column in the visible alignment
744 public int absoluteToVisibleColumn(int hiddenColumn)
748 LOCK.readLock().lock();
749 int result = hiddenColumn;
751 if (hiddenColumns != null)
753 HiddenCursorPosition cursorPos = cursor
754 .findRegionForColumn(hiddenColumn);
755 int index = cursorPos.getRegionIndex();
756 int hiddenBeforeCol = cursorPos.getHiddenSoFar();
758 // just subtract hidden cols count - this works fine if column is
760 result = hiddenColumn - hiddenBeforeCol;
762 // now check in case column is hidden - it will be in the returned
764 if (index < hiddenColumns.size())
766 int[] region = hiddenColumns.get(index);
767 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
769 // actually col is hidden, return region[0]-1
770 // unless region[0]==0 in which case return 0
777 result = region[0] - 1 - hiddenBeforeCol;
783 return result; // return the shifted position after removing hidden
787 LOCK.readLock().unlock();
792 * Find the visible column which is a given visible number of columns to the
793 * left of another visible column. i.e. for a startColumn x, the column which
794 * is distance 1 away will be column x-1.
796 * @param visibleDistance
797 * the number of visible columns to offset by
799 * the column to start from
800 * @return the position of the column in the visible alignment
802 public int subtractVisibleColumns(int visibleDistance, int startColumn)
806 LOCK.readLock().lock();
807 int distance = visibleDistance;
809 // in case startColumn is in a hidden region, move it to the left
810 int start = visibleToAbsoluteColumn(absoluteToVisibleColumn(startColumn));
812 Iterator<int[]> it = new ReverseRegionsIterator(0, start,
815 while (it.hasNext() && (distance > 0))
817 int[] region = it.next();
819 if (start > region[1])
821 // subtract the gap to right of region from distance
822 if (start - region[1] <= distance)
824 distance -= start - region[1];
825 start = region[0] - 1;
829 start = start - distance;
834 return start - distance;
838 LOCK.readLock().unlock();
843 * This method returns the rightmost limit of a region of an alignment with
844 * hidden columns. In otherwords, the next hidden column.
847 * the absolute (visible) alignmentPosition to find the next hidden
849 * @return the index of the next hidden column, or alPos if there is no next
852 public int getHiddenBoundaryRight(int alPos)
856 LOCK.readLock().lock();
857 if (hiddenColumns != null)
859 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
860 if (index < hiddenColumns.size())
862 int[] region = hiddenColumns.get(index);
863 if (alPos < region[0])
867 else if ((alPos <= region[1])
868 && (index + 1 < hiddenColumns.size()))
870 // alPos is within a hidden region, return the next one
872 region = hiddenColumns.get(index + 1);
880 LOCK.readLock().unlock();
885 * This method returns the leftmost limit of a region of an alignment with
886 * hidden columns. In otherwords, the previous hidden column.
889 * the absolute (visible) alignmentPosition to find the previous
892 public int getHiddenBoundaryLeft(int alPos)
896 LOCK.readLock().lock();
898 if (hiddenColumns != null)
900 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
904 int[] region = hiddenColumns.get(index - 1);
911 LOCK.readLock().unlock();
917 * Answers if a column in the alignment is visible
920 * absolute position of column in the alignment
921 * @return true if column is visible
923 public boolean isVisible(int column)
927 LOCK.readLock().lock();
929 int regionindex = cursor.findRegionForColumn(column).getRegionIndex();
930 if (regionindex > -1 && regionindex < hiddenColumns.size())
932 int[] region = hiddenColumns.get(regionindex);
933 // already know that column <= region[1] as cursor returns containing
934 // region or region to right
935 if (column >= region[0])
944 LOCK.readLock().unlock();
949 * Get the visible sections of a set of sequences
952 * sequence position to start from
954 * sequence position to end at
956 * an array of sequences
957 * @return an array of strings encoding the visible parts of each sequence
959 public String[] getVisibleSequenceStrings(int start, int end,
964 LOCK.readLock().lock();
965 int iSize = seqs.length;
966 String[] selections = new String[iSize];
967 if (hiddenColumns != null && hiddenColumns.size() > 0)
969 for (int i = 0; i < iSize; i++)
971 StringBuffer visibleSeq = new StringBuffer();
973 Iterator<int[]> blocks = new VisibleContigsIterator(start,
974 end + 1, hiddenColumns);
976 while (blocks.hasNext())
978 int[] block = blocks.next();
979 if (blocks.hasNext())
982 .append(seqs[i].getSequence(block[0], block[1] + 1));
987 .append(seqs[i].getSequence(block[0], block[1]));
991 selections[i] = visibleSeq.toString();
996 for (int i = 0; i < iSize; i++)
998 selections[i] = seqs[i].getSequenceAsString(start, end);
1005 LOCK.readLock().unlock();
1010 * Locate the first position visible for this sequence. If seq isn't visible
1011 * then return the position of the left side of the hidden boundary region.
1014 * sequence to find position for
1015 * @return visible start position
1017 public int locateVisibleStartOfSequence(SequenceI seq)
1021 LOCK.readLock().lock();
1024 if (hiddenColumns == null || hiddenColumns.size() == 0)
1026 return seq.findIndex(seq.getStart()) - 1;
1029 // Simply walk along the sequence whilst watching for hidden column
1031 Iterator<int[]> regions = hiddenColumns.iterator();
1032 int hideStart = seq.getLength();
1036 boolean foundStart = false;
1038 // step through the non-gapped positions of the sequence
1039 for (int i = seq.getStart(); i <= seq.getEnd() && (!foundStart); i++)
1041 // get alignment position of this residue in the sequence
1042 int p = seq.findIndex(i) - 1;
1044 // update hidden region start/end
1045 while (hideEnd < p && regions.hasNext())
1047 int[] region = regions.next();
1049 visNext += region[0] - visPrev;
1050 hideStart = region[0];
1051 hideEnd = region[1];
1055 hideStart = seq.getLength();
1057 // update visible boundary for sequence
1067 return absoluteToVisibleColumn(start);
1069 // otherwise, sequence was completely hidden
1073 LOCK.readLock().unlock();
1078 * delete any columns in alignmentAnnotation that are hidden (including
1079 * sequence associated annotation).
1081 * @param alignmentAnnotation
1083 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
1085 if (alignmentAnnotation != null
1086 && alignmentAnnotation.annotations != null)
1088 makeVisibleAnnotation(0, alignmentAnnotation.annotations.length,
1089 alignmentAnnotation);
1094 * delete any columns in alignmentAnnotation that are hidden (including
1095 * sequence associated annotation).
1098 * remove any annotation to the right of this column
1100 * remove any annotation to the left of this column
1101 * @param alignmentAnnotation
1102 * the annotation to operate on
1104 public void makeVisibleAnnotation(int start, int end,
1105 AlignmentAnnotation alignmentAnnotation)
1109 LOCK.readLock().lock();
1111 int startFrom = start;
1114 if (alignmentAnnotation != null
1115 && alignmentAnnotation.annotations != null)
1117 if (hiddenColumns != null && hiddenColumns.size() > 0)
1119 removeHiddenAnnotation(startFrom, endAt, alignmentAnnotation);
1123 alignmentAnnotation.restrict(startFrom, endAt);
1128 LOCK.readLock().unlock();
1132 private void removeHiddenAnnotation(int start, int end,
1133 AlignmentAnnotation alignmentAnnotation)
1135 // mangle the alignmentAnnotation annotation array
1136 ArrayList<Annotation[]> annels = new ArrayList<>();
1137 Annotation[] els = null;
1141 Iterator<int[]> blocks = new VisibleContigsIterator(start, end + 1,
1145 int annotationLength;
1146 while (blocks.hasNext())
1148 int[] block = blocks.next();
1149 annotationLength = block[1] - block[0] + 1;
1151 if (blocks.hasNext())
1153 // copy just the visible segment of the annotation row
1154 copylength = annotationLength;
1158 if (annotationLength + block[0] <= alignmentAnnotation.annotations.length)
1160 // copy just the visible segment of the annotation row
1161 copylength = annotationLength;
1165 // copy to the end of the annotation row
1166 copylength = alignmentAnnotation.annotations.length - block[0];
1170 els = new Annotation[annotationLength];
1172 System.arraycopy(alignmentAnnotation.annotations, block[0], els, 0,
1174 w += annotationLength;
1179 alignmentAnnotation.annotations = new Annotation[w];
1182 for (Annotation[] chnk : annels)
1184 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1193 * @return true if there are columns hidden
1195 public boolean hasHiddenColumns()
1199 LOCK.readLock().lock();
1201 // we don't use getSize()>0 here because it has to iterate over
1202 // the full hiddenColumns collection and so will be much slower
1203 return hiddenColumns != null && hiddenColumns.size() > 0;
1206 LOCK.readLock().unlock();
1212 * @return true if there is more than one hidden column region
1214 public boolean hasMultiHiddenColumnRegions()
1218 LOCK.readLock().lock();
1219 return hiddenColumns != null && hiddenColumns.size() > 1;
1222 LOCK.readLock().unlock();
1228 * Returns a hashCode built from hidden column ranges
1231 public int hashCode()
1235 LOCK.readLock().lock();
1237 Iterator<int[]> it = hiddenColumns.iterator();
1238 while (it.hasNext())
1240 int[] hidden = it.next();
1241 hashCode = HASH_MULTIPLIER * hashCode + hidden[0];
1242 hashCode = HASH_MULTIPLIER * hashCode + hidden[1];
1247 LOCK.readLock().unlock();
1252 * Hide columns corresponding to the marked bits
1255 * - columns map to bits starting from zero
1257 public void hideColumns(BitSet inserts)
1261 LOCK.writeLock().lock();
1262 for (int firstSet = inserts
1263 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1264 .nextSetBit(lastSet))
1266 lastSet = inserts.nextClearBit(firstSet);
1267 hideColumns(firstSet, lastSet - 1);
1269 cursor.resetCursor(hiddenColumns);
1273 LOCK.writeLock().unlock();
1280 * BitSet where hidden columns will be marked
1282 public void markHiddenRegions(BitSet inserts)
1286 LOCK.readLock().lock();
1287 if (hiddenColumns == null)
1291 Iterator<int[]> it = hiddenColumns.iterator();
1292 while (it.hasNext())
1294 int[] range = it.next();
1295 inserts.set(range[0], range[1] + 1);
1299 LOCK.readLock().unlock();
1304 * Calculate the visible start and end index of an alignment.
1307 * full alignment width
1308 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1310 public int[] getVisibleStartAndEndIndex(int width)
1314 LOCK.readLock().lock();
1315 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1316 int startPos = alignmentStartEnd[0];
1317 int endPos = alignmentStartEnd[1];
1319 int[] lowestRange = new int[] { -1, -1 };
1320 int[] higestRange = new int[] { -1, -1 };
1322 if (hiddenColumns == null)
1324 return new int[] { startPos, endPos };
1327 Iterator<int[]> it = hiddenColumns.iterator();
1328 while (it.hasNext())
1330 int[] range = it.next();
1331 lowestRange = (range[0] <= startPos) ? range : lowestRange;
1332 higestRange = (range[1] >= endPos) ? range : higestRange;
1335 if (lowestRange[0] == -1) // includes (lowestRange[1] == -1)
1337 startPos = alignmentStartEnd[0];
1341 startPos = lowestRange[1] + 1;
1344 if (higestRange[0] == -1) // includes (higestRange[1] == -1)
1346 endPos = alignmentStartEnd[1];
1350 endPos = higestRange[0] - 1;
1352 return new int[] { startPos, endPos };
1355 LOCK.readLock().unlock();
1360 * Finds the hidden region (if any) which starts or ends at res
1363 * visible residue position, unadjusted for hidden columns
1364 * @return region as [start,end] or null if no matching region is found. If
1365 * res is adjacent to two regions, returns the left region.
1367 public int[] getRegionWithEdgeAtRes(int res)
1371 LOCK.readLock().lock();
1372 int adjres = visibleToAbsoluteColumn(res);
1374 int[] reveal = null;
1376 if (hiddenColumns != null)
1378 // look for a region ending just before adjres
1379 int regionindex = cursor.findRegionForColumn(adjres - 1)
1381 if (regionindex < hiddenColumns.size()
1382 && hiddenColumns.get(regionindex)[1] == adjres - 1)
1384 reveal = hiddenColumns.get(regionindex);
1386 // check if the region ends just after adjres
1387 else if (regionindex < hiddenColumns.size()
1388 && hiddenColumns.get(regionindex)[0] == adjres + 1)
1390 reveal = hiddenColumns.get(regionindex);
1397 LOCK.readLock().unlock();
1402 * Return an iterator over the hidden regions
1404 public Iterator<int[]> iterator()
1408 LOCK.readLock().lock();
1409 return new HiddenColsIterator(hiddenColumns);
1412 LOCK.readLock().unlock();
1417 * Return a bounded iterator over the hidden regions
1420 * position to start from (inclusive, absolute column position)
1422 * position to end at (inclusive, absolute column position)
1425 public Iterator<int[]> getBoundedIterator(int start, int end)
1429 LOCK.readLock().lock();
1430 return new HiddenColsIterator(start, end, hiddenColumns);
1433 LOCK.readLock().unlock();
1438 * Return a bounded iterator over the *visible* start positions of hidden
1442 * position to start from (inclusive, visible column position)
1444 * position to end at (inclusive, visible column position)
1446 public Iterator<Integer> getBoundedStartIterator(int start, int end)
1450 LOCK.readLock().lock();
1452 // get absolute position of column in alignment
1453 int absoluteStart = visibleToAbsoluteColumn(start);
1455 // Get cursor position and supply it to the iterator:
1456 // Since we want visible region start, we look for a cursor for the
1457 // (absoluteStart-1), then if absoluteStart is the start of a visible
1458 // region we'll get the cursor pointing to the region before, which is
1460 HiddenCursorPosition pos = cursor
1461 .findRegionForColumn(absoluteStart - 1);
1463 return new BoundedStartRegionIterator(pos, start, end,
1467 LOCK.readLock().unlock();
1472 * Return an iterator over visible *columns* (not regions) between the given
1473 * start and end boundaries
1476 * first column (inclusive)
1478 * last column (inclusive)
1480 public Iterator<Integer> getVisibleColsIterator(int start, int end)
1484 LOCK.readLock().lock();
1485 return new VisibleColsIterator(start, end, hiddenColumns);
1488 LOCK.readLock().unlock();
1493 * return an iterator over visible segments between the given start and end
1497 * first column, inclusive from 0
1499 * last column - not inclusive
1500 * @param useVisibleCoords
1501 * if true, start and end are visible column positions, not absolute
1504 public VisibleContigsIterator getVisContigsIterator(int start,
1506 boolean useVisibleCoords)
1508 int adjstart = start;
1510 if (useVisibleCoords)
1512 adjstart = visibleToAbsoluteColumn(start);
1513 adjend = visibleToAbsoluteColumn(end);
1518 LOCK.readLock().lock();
1519 return new VisibleContigsIterator(adjstart, adjend, hiddenColumns);
1522 LOCK.readLock().unlock();