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 * mark the columns corresponding to gap characters as hidden in the column
308 public void hideInsertionsFor(List<int[]> inserts)
312 LOCK.writeLock().lock();
313 for (int[] r : inserts)
315 hideColumns(r[0], r[1]);
317 cursor.resetCursor(hiddenColumns);
321 LOCK.writeLock().unlock();
326 * Unhides, and adds to the selection list, all hidden columns
328 public void revealAllHiddenColumns(ColumnSelection sel)
332 LOCK.writeLock().lock();
333 if (hiddenColumns != null)
335 Iterator<int[]> it = hiddenColumns.iterator();
338 int[] region = it.next();
339 for (int j = region[0]; j < region[1] + 1; j++)
344 hiddenColumns = null;
345 cursor.resetCursor(hiddenColumns);
350 LOCK.writeLock().unlock();
355 * Reveals, and marks as selected, the hidden column range with the given
359 * the start column to look for
361 * the column selection to add the hidden column range to
363 public void revealHiddenColumns(int start, ColumnSelection sel)
367 LOCK.writeLock().lock();
369 if (hiddenColumns != null)
371 int regionIndex = cursor.findRegionForColumn(start)
374 if (regionIndex != -1 && regionIndex != hiddenColumns.size())
376 // regionIndex is the region which either contains start
377 // or lies to the right of start
378 int[] region = hiddenColumns.get(regionIndex);
379 if (start == region[0])
381 for (int j = region[0]; j < region[1] + 1; j++)
385 int colsToRemove = region[1] - region[0] + 1;
386 hiddenColumns.remove(regionIndex);
388 if (hiddenColumns.isEmpty())
390 hiddenColumns = null;
395 numColumns -= colsToRemove;
397 cursor.updateForDeletedRegion(hiddenColumns);
403 LOCK.writeLock().unlock();
408 * Add gaps into the sequences aligned to profileseq under the given
412 * sequence in al which sequences are aligned to
414 * alignment to have gaps inserted into it
416 * alignment view where sequence corresponding to profileseq is first
418 * @return new HiddenColumns for new alignment view, with insertions into
419 * profileseq marked as hidden.
421 public static HiddenColumns propagateInsertions(SequenceI profileseq,
422 AlignmentI al, AlignmentView input)
426 char gc = al.getGapCharacter();
427 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
428 HiddenColumns nview = (HiddenColumns) alandhidden[1];
429 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
430 nview.propagateInsertions(profileseq, al, origseq);
437 * sequence in al which corresponds to origseq
439 * alignment which is to have gaps inserted into it
441 * sequence corresponding to profileseq which defines gap map for
444 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
449 LOCK.writeLock().lock();
451 char gc = al.getGapCharacter();
453 // take the set of hidden columns, and the set of gaps in origseq,
454 // and remove all the hidden gaps from hiddenColumns
456 // first get the gaps as a Bitset
457 BitSet gaps = origseq.gapBitset();
459 // now calculate hidden ^ not(gap)
460 BitSet hidden = new BitSet();
461 markHiddenRegions(hidden);
463 hiddenColumns = null;
464 this.hideColumns(hidden);
466 // for each sequence in the alignment, except the profile sequence,
467 // insert gaps corresponding to each hidden region but where each hidden
468 // column region is shifted backwards by the number of preceding visible
469 // gaps update hidden columns at the same time
470 Iterator<int[]> regions = hiddenColumns.iterator();
471 List<int[]> newhidden = new ArrayList<>();
473 int numGapsBefore = 0;
475 while (regions.hasNext())
477 // get region coordinates accounting for gaps
478 // we can rely on gaps not being *in* hidden regions because we already
480 int[] region = regions.next();
481 while (gapPosition < region[0])
484 if (gaps.get(gapPosition))
490 int left = region[0] - numGapsBefore;
491 int right = region[1] - numGapsBefore;
492 newhidden.add(new int[] { left, right });
494 // make a string with number of gaps = length of hidden region
495 StringBuffer sb = new StringBuffer();
496 for (int s = 0; s < right - left + 1; s++)
500 padGaps(sb, left, profileseq, al);
503 hiddenColumns = newhidden;
504 cursor.resetCursor(hiddenColumns);
508 LOCK.writeLock().unlock();
513 * Pad gaps in all sequences in alignment except profileseq
516 * gap string to insert
518 * position to insert at
520 * sequence not to pad
522 * alignment to pad sequences in
524 private void padGaps(StringBuffer sb, int pos, SequenceI profileseq,
527 // loop over the sequences and pad with gaps where required
528 for (int s = 0, ns = al.getHeight(); s < ns; s++)
530 SequenceI sqobj = al.getSequenceAt(s);
531 if (sqobj != profileseq)
533 String sq = al.getSequenceAt(s).getSequenceAsString();
534 if (sq.length() <= pos)
537 int diff = pos - sq.length() - 1;
542 while ((diff = pos - sq.length() - 1) > 0)
544 if (diff >= sb.length())
550 char[] buf = new char[diff];
551 sb.getChars(0, diff, buf, 0);
552 sq += buf.toString();
560 al.getSequenceAt(s).setSequence(
561 sq.substring(0, pos) + sb.toString() + sq.substring(pos));
568 * Methods which only need read access to the hidden columns collection.
569 * These methods should use a readLock to prevent other threads changing
570 * the hidden columns collection while it is in use.
574 * Output regions data as a string. String is in the format:
575 * reg0[0]<between>reg0[1]<delimiter>reg1[0]<between>reg1[1] ... regn[1]
578 * string to delimit regions
579 * @param betweenstring
580 * to put between start and end region values
581 * @return regions formatted according to delimiter and between strings
583 public String regionsToString(String delimiter, String between)
587 LOCK.readLock().lock();
588 StringBuilder regionBuilder = new StringBuilder();
589 if (hiddenColumns != null)
591 Iterator<int[]> it = hiddenColumns.iterator();
594 int[] range = it.next();
595 regionBuilder.append(delimiter).append(range[0]).append(between)
599 regionBuilder.deleteCharAt(0);
603 return regionBuilder.toString();
606 LOCK.readLock().unlock();
611 * Find the number of hidden columns
613 * @return number of hidden columns
619 LOCK.readLock().lock();
621 if (numColumns == 0 && hiddenColumns != null)
623 // numColumns is out of date, so recalculate
626 Iterator<int[]> it = hiddenColumns.iterator();
629 int[] range = it.next();
630 size += range[1] - range[0] + 1;
639 LOCK.readLock().unlock();
644 * Get the number of distinct hidden regions
646 * @return number of regions
648 public int getNumberOfRegions()
652 LOCK.readLock().lock();
654 if (hasHiddenColumns())
656 num = hiddenColumns.size();
661 LOCK.readLock().unlock();
666 public boolean equals(Object obj)
670 LOCK.readLock().lock();
672 if (!(obj instanceof HiddenColumns))
676 HiddenColumns that = (HiddenColumns) obj;
679 * check hidden columns are either both null, or match
681 if (this.hiddenColumns == null)
683 return (that.hiddenColumns == null);
685 if (that.hiddenColumns == null
686 || that.hiddenColumns.size() != this.hiddenColumns.size())
691 Iterator<int[]> it = hiddenColumns.iterator();
692 Iterator<int[]> thatit = that.iterator();
695 int[] thisRange = it.next();
696 int[] thatRange = thatit.next();
697 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
705 LOCK.readLock().unlock();
710 * Return absolute column index for a visible column index
713 * int column index in alignment view (count from zero)
714 * @return alignment column index for column
716 public int visibleToAbsoluteColumn(int column)
720 LOCK.readLock().lock();
723 if (hiddenColumns != null)
725 result += cursor.findRegionForVisColumn(column).getHiddenSoFar();
731 LOCK.readLock().unlock();
736 * Use this method to find out where a column will appear in the visible
737 * alignment when hidden columns exist. If the column is not visible, then the
738 * index of the next visible column on the left will be returned (or 0 if
739 * there is no visible column on the left)
741 * @param hiddenColumn
742 * the column index in the full alignment including hidden columns
743 * @return the position of the column in the visible alignment
745 public int absoluteToVisibleColumn(int hiddenColumn)
749 LOCK.readLock().lock();
750 int result = hiddenColumn;
752 if (hiddenColumns != null)
754 HiddenCursorPosition cursorPos = cursor
755 .findRegionForColumn(hiddenColumn);
756 int index = cursorPos.getRegionIndex();
757 int hiddenBeforeCol = cursorPos.getHiddenSoFar();
759 // just subtract hidden cols count - this works fine if column is
761 result = hiddenColumn - hiddenBeforeCol;
763 // now check in case column is hidden - it will be in the returned
765 if (index < hiddenColumns.size())
767 int[] region = hiddenColumns.get(index);
768 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
770 // actually col is hidden, return region[0]-1
771 // unless region[0]==0 in which case return 0
778 result = region[0] - 1 - hiddenBeforeCol;
784 return result; // return the shifted position after removing hidden
788 LOCK.readLock().unlock();
793 * Find the visible column which is a given visible number of columns to the
794 * left of another visible column. i.e. for a startColumn x, the column which
795 * is distance 1 away will be column x-1.
797 * @param visibleDistance
798 * the number of visible columns to offset by
800 * the column to start from
801 * @return the position of the column in the visible alignment
803 public int subtractVisibleColumns(int visibleDistance, int startColumn)
807 LOCK.readLock().lock();
808 int distance = visibleDistance;
810 // in case startColumn is in a hidden region, move it to the left
811 int start = visibleToAbsoluteColumn(absoluteToVisibleColumn(startColumn));
813 Iterator<int[]> it = new ReverseRegionsIterator(0, start,
816 while (it.hasNext() && (distance > 0))
818 int[] region = it.next();
820 if (start > region[1])
822 // subtract the gap to right of region from distance
823 if (start - region[1] <= distance)
825 distance -= start - region[1];
826 start = region[0] - 1;
830 start = start - distance;
835 return start - distance;
839 LOCK.readLock().unlock();
844 * This method returns the rightmost limit of a region of an alignment with
845 * hidden columns. In otherwords, the next hidden column.
848 * the absolute (visible) alignmentPosition to find the next hidden
850 * @return the index of the next hidden column, or alPos if there is no next
853 public int getHiddenBoundaryRight(int alPos)
857 LOCK.readLock().lock();
858 if (hiddenColumns != null)
860 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
861 if (index < hiddenColumns.size())
863 int[] region = hiddenColumns.get(index);
864 if (alPos < region[0])
868 else if ((alPos <= region[1])
869 && (index + 1 < hiddenColumns.size()))
871 // alPos is within a hidden region, return the next one
873 region = hiddenColumns.get(index + 1);
881 LOCK.readLock().unlock();
886 * This method returns the leftmost limit of a region of an alignment with
887 * hidden columns. In otherwords, the previous hidden column.
890 * the absolute (visible) alignmentPosition to find the previous
893 public int getHiddenBoundaryLeft(int alPos)
897 LOCK.readLock().lock();
899 if (hiddenColumns != null)
901 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
905 int[] region = hiddenColumns.get(index - 1);
912 LOCK.readLock().unlock();
918 * Answers if a column in the alignment is visible
921 * absolute position of column in the alignment
922 * @return true if column is visible
924 public boolean isVisible(int column)
928 LOCK.readLock().lock();
930 int regionindex = cursor.findRegionForColumn(column).getRegionIndex();
931 if (regionindex > -1 && regionindex < hiddenColumns.size())
933 int[] region = hiddenColumns.get(regionindex);
934 // already know that column <= region[1] as cursor returns containing
935 // region or region to right
936 if (column >= region[0])
945 LOCK.readLock().unlock();
950 * Get the visible sections of a set of sequences
953 * sequence position to start from
955 * sequence position to end at
957 * an array of sequences
958 * @return an array of strings encoding the visible parts of each sequence
960 public String[] getVisibleSequenceStrings(int start, int end,
965 LOCK.readLock().lock();
966 int iSize = seqs.length;
967 String[] selections = new String[iSize];
968 if (hiddenColumns != null && hiddenColumns.size() > 0)
970 for (int i = 0; i < iSize; i++)
972 StringBuffer visibleSeq = new StringBuffer();
974 Iterator<int[]> blocks = new VisibleContigsIterator(start,
975 end + 1, hiddenColumns);
977 while (blocks.hasNext())
979 int[] block = blocks.next();
980 if (blocks.hasNext())
983 .append(seqs[i].getSequence(block[0], block[1] + 1));
988 .append(seqs[i].getSequence(block[0], block[1]));
992 selections[i] = visibleSeq.toString();
997 for (int i = 0; i < iSize; i++)
999 selections[i] = seqs[i].getSequenceAsString(start, end);
1006 LOCK.readLock().unlock();
1011 * Locate the first position visible for this sequence. If seq isn't visible
1012 * then return the position of the left side of the hidden boundary region.
1015 * sequence to find position for
1016 * @return visible start position
1018 public int locateVisibleStartOfSequence(SequenceI seq)
1022 LOCK.readLock().lock();
1025 if (hiddenColumns == null || hiddenColumns.size() == 0)
1027 return seq.findIndex(seq.getStart()) - 1;
1030 // Simply walk along the sequence whilst watching for hidden column
1032 Iterator<int[]> regions = hiddenColumns.iterator();
1033 int hideStart = seq.getLength();
1037 boolean foundStart = false;
1039 // step through the non-gapped positions of the sequence
1040 for (int i = seq.getStart(); i <= seq.getEnd() && (!foundStart); i++)
1042 // get alignment position of this residue in the sequence
1043 int p = seq.findIndex(i) - 1;
1045 // update hidden region start/end
1046 while (hideEnd < p && regions.hasNext())
1048 int[] region = regions.next();
1050 visNext += region[0] - visPrev;
1051 hideStart = region[0];
1052 hideEnd = region[1];
1056 hideStart = seq.getLength();
1058 // update visible boundary for sequence
1068 return absoluteToVisibleColumn(start);
1070 // otherwise, sequence was completely hidden
1074 LOCK.readLock().unlock();
1079 * delete any columns in alignmentAnnotation that are hidden (including
1080 * sequence associated annotation).
1082 * @param alignmentAnnotation
1084 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
1086 if (alignmentAnnotation != null
1087 && alignmentAnnotation.annotations != null)
1089 makeVisibleAnnotation(0, alignmentAnnotation.annotations.length,
1090 alignmentAnnotation);
1095 * delete any columns in alignmentAnnotation that are hidden (including
1096 * sequence associated annotation).
1099 * remove any annotation to the right of this column
1101 * remove any annotation to the left of this column
1102 * @param alignmentAnnotation
1103 * the annotation to operate on
1105 public void makeVisibleAnnotation(int start, int end,
1106 AlignmentAnnotation alignmentAnnotation)
1110 LOCK.readLock().lock();
1112 int startFrom = start;
1115 if (alignmentAnnotation != null
1116 && alignmentAnnotation.annotations != null)
1118 if (hiddenColumns != null && hiddenColumns.size() > 0)
1120 removeHiddenAnnotation(startFrom, endAt, alignmentAnnotation);
1124 alignmentAnnotation.restrict(startFrom, endAt);
1129 LOCK.readLock().unlock();
1133 private void removeHiddenAnnotation(int start, int end,
1134 AlignmentAnnotation alignmentAnnotation)
1136 // mangle the alignmentAnnotation annotation array
1137 ArrayList<Annotation[]> annels = new ArrayList<>();
1138 Annotation[] els = null;
1142 Iterator<int[]> blocks = new VisibleContigsIterator(start, end + 1,
1146 int annotationLength;
1147 while (blocks.hasNext())
1149 int[] block = blocks.next();
1150 annotationLength = block[1] - block[0] + 1;
1152 if (blocks.hasNext())
1154 // copy just the visible segment of the annotation row
1155 copylength = annotationLength;
1159 if (annotationLength + block[0] <= alignmentAnnotation.annotations.length)
1161 // copy just the visible segment of the annotation row
1162 copylength = annotationLength;
1166 // copy to the end of the annotation row
1167 copylength = alignmentAnnotation.annotations.length - block[0];
1171 els = new Annotation[annotationLength];
1173 System.arraycopy(alignmentAnnotation.annotations, block[0], els, 0,
1175 w += annotationLength;
1180 alignmentAnnotation.annotations = new Annotation[w];
1183 for (Annotation[] chnk : annels)
1185 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1194 * @return true if there are columns hidden
1196 public boolean hasHiddenColumns()
1200 LOCK.readLock().lock();
1202 // we don't use getSize()>0 here because it has to iterate over
1203 // the full hiddenColumns collection and so will be much slower
1204 return hiddenColumns != null && hiddenColumns.size() > 0;
1207 LOCK.readLock().unlock();
1213 * @return true if there is more than one hidden column region
1215 public boolean hasMultiHiddenColumnRegions()
1219 LOCK.readLock().lock();
1220 return hiddenColumns != null && hiddenColumns.size() > 1;
1223 LOCK.readLock().unlock();
1229 * Returns a hashCode built from hidden column ranges
1232 public int hashCode()
1236 LOCK.readLock().lock();
1238 Iterator<int[]> it = hiddenColumns.iterator();
1239 while (it.hasNext())
1241 int[] hidden = it.next();
1242 hashCode = HASH_MULTIPLIER * hashCode + hidden[0];
1243 hashCode = HASH_MULTIPLIER * hashCode + hidden[1];
1248 LOCK.readLock().unlock();
1253 * Hide columns corresponding to the marked bits
1256 * - columns map to bits starting from zero
1258 public void hideColumns(BitSet inserts)
1262 LOCK.writeLock().lock();
1263 for (int firstSet = inserts
1264 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1265 .nextSetBit(lastSet))
1267 lastSet = inserts.nextClearBit(firstSet);
1268 hideColumns(firstSet, lastSet - 1);
1270 cursor.resetCursor(hiddenColumns);
1274 LOCK.writeLock().unlock();
1281 * BitSet where hidden columns will be marked
1283 public void markHiddenRegions(BitSet inserts)
1287 LOCK.readLock().lock();
1288 if (hiddenColumns == null)
1292 Iterator<int[]> it = hiddenColumns.iterator();
1293 while (it.hasNext())
1295 int[] range = it.next();
1296 inserts.set(range[0], range[1] + 1);
1300 LOCK.readLock().unlock();
1305 * Calculate the visible start and end index of an alignment.
1308 * full alignment width
1309 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1311 public int[] getVisibleStartAndEndIndex(int width)
1315 LOCK.readLock().lock();
1316 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1317 int startPos = alignmentStartEnd[0];
1318 int endPos = alignmentStartEnd[1];
1320 int[] lowestRange = new int[] { -1, -1 };
1321 int[] higestRange = new int[] { -1, -1 };
1323 if (hiddenColumns == null)
1325 return new int[] { startPos, endPos };
1328 Iterator<int[]> it = hiddenColumns.iterator();
1329 while (it.hasNext())
1331 int[] range = it.next();
1332 lowestRange = (range[0] <= startPos) ? range : lowestRange;
1333 higestRange = (range[1] >= endPos) ? range : higestRange;
1336 if (lowestRange[0] == -1) // includes (lowestRange[1] == -1)
1338 startPos = alignmentStartEnd[0];
1342 startPos = lowestRange[1] + 1;
1345 if (higestRange[0] == -1) // includes (higestRange[1] == -1)
1347 endPos = alignmentStartEnd[1];
1351 endPos = higestRange[0] - 1;
1353 return new int[] { startPos, endPos };
1356 LOCK.readLock().unlock();
1361 * Finds the hidden region (if any) which starts or ends at res
1364 * visible residue position, unadjusted for hidden columns
1365 * @return region as [start,end] or null if no matching region is found. If
1366 * res is adjacent to two regions, returns the left region.
1368 public int[] getRegionWithEdgeAtRes(int res)
1372 LOCK.readLock().lock();
1373 int adjres = visibleToAbsoluteColumn(res);
1375 int[] reveal = null;
1377 if (hiddenColumns != null)
1379 // look for a region ending just before adjres
1380 int regionindex = cursor.findRegionForColumn(adjres - 1)
1382 if (regionindex < hiddenColumns.size()
1383 && hiddenColumns.get(regionindex)[1] == adjres - 1)
1385 reveal = hiddenColumns.get(regionindex);
1387 // check if the region ends just after adjres
1388 else if (regionindex < hiddenColumns.size()
1389 && hiddenColumns.get(regionindex)[0] == adjres + 1)
1391 reveal = hiddenColumns.get(regionindex);
1398 LOCK.readLock().unlock();
1403 * Return an iterator over the hidden regions
1405 public Iterator<int[]> iterator()
1409 LOCK.readLock().lock();
1410 return new HiddenColsIterator(hiddenColumns);
1413 LOCK.readLock().unlock();
1418 * Return a bounded iterator over the hidden regions
1421 * position to start from (inclusive, absolute column position)
1423 * position to end at (inclusive, absolute column position)
1426 public Iterator<int[]> getBoundedIterator(int start, int end)
1430 LOCK.readLock().lock();
1431 return new HiddenColsIterator(start, end, hiddenColumns);
1434 LOCK.readLock().unlock();
1439 * Return a bounded iterator over the *visible* start positions of hidden
1443 * position to start from (inclusive, visible column position)
1445 * position to end at (inclusive, visible column position)
1447 public Iterator<Integer> getBoundedStartIterator(int start, int end)
1451 LOCK.readLock().lock();
1453 // get absolute position of column in alignment
1454 int absoluteStart = visibleToAbsoluteColumn(start);
1456 // Get cursor position and supply it to the iterator:
1457 // Since we want visible region start, we look for a cursor for the
1458 // (absoluteStart-1), then if absoluteStart is the start of a visible
1459 // region we'll get the cursor pointing to the region before, which is
1461 HiddenCursorPosition pos = cursor
1462 .findRegionForColumn(absoluteStart - 1);
1464 return new BoundedStartRegionIterator(pos, start, end,
1468 LOCK.readLock().unlock();
1473 * Return an iterator over visible *columns* (not regions) between the given
1474 * start and end boundaries
1477 * first column (inclusive)
1479 * last column (inclusive)
1481 public Iterator<Integer> getVisibleColsIterator(int start, int end)
1485 LOCK.readLock().lock();
1486 return new VisibleColsIterator(start, end, hiddenColumns);
1489 LOCK.readLock().unlock();
1494 * return an iterator over visible segments between the given start and end
1498 * first column, inclusive from 0
1500 * last column - not inclusive
1501 * @param useVisibleCoords
1502 * if true, start and end are visible column positions, not absolute
1505 public Iterator<int[]> getVisContigsIterator(int start, int end,
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();