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(SequenceI sr)
312 LOCK.writeLock().lock();
313 List<int[]> inserts = sr.getInsertions();
314 for (int[] r : inserts)
316 hideColumns(r[0], r[1]);
318 cursor.resetCursor(hiddenColumns);
322 LOCK.writeLock().unlock();
327 * Unhides, and adds to the selection list, all hidden columns
329 public void revealAllHiddenColumns(ColumnSelection sel)
333 LOCK.writeLock().lock();
334 if (hiddenColumns != null)
336 Iterator<int[]> it = hiddenColumns.iterator();
339 int[] region = it.next();
340 for (int j = region[0]; j < region[1] + 1; j++)
345 hiddenColumns = null;
346 cursor.resetCursor(hiddenColumns);
351 LOCK.writeLock().unlock();
356 * Reveals, and marks as selected, the hidden column range with the given
360 * the start column to look for
362 * the column selection to add the hidden column range to
364 public void revealHiddenColumns(int start, ColumnSelection sel)
368 LOCK.writeLock().lock();
370 if (hiddenColumns != null)
372 int regionIndex = cursor.findRegionForColumn(start)
375 if (regionIndex != -1 && regionIndex != hiddenColumns.size())
377 // regionIndex is the region which either contains start
378 // or lies to the right of start
379 int[] region = hiddenColumns.get(regionIndex);
380 if (start == region[0])
382 for (int j = region[0]; j < region[1] + 1; j++)
386 int colsToRemove = region[1] - region[0] + 1;
387 hiddenColumns.remove(regionIndex);
389 if (hiddenColumns.isEmpty())
391 hiddenColumns = null;
396 numColumns -= colsToRemove;
398 cursor.updateForDeletedRegion(hiddenColumns);
404 LOCK.writeLock().unlock();
409 * Add gaps into the sequences aligned to profileseq under the given
413 * sequence in al which sequences are aligned to
415 * alignment to have gaps inserted into it
417 * alignment view where sequence corresponding to profileseq is first
419 * @return new HiddenColumns for new alignment view, with insertions into
420 * profileseq marked as hidden.
422 public static HiddenColumns propagateInsertions(SequenceI profileseq,
423 AlignmentI al, AlignmentView input)
427 char gc = al.getGapCharacter();
428 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
429 HiddenColumns nview = (HiddenColumns) alandhidden[1];
430 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
431 nview.propagateInsertions(profileseq, al, origseq);
438 * sequence in al which corresponds to origseq
440 * alignment which is to have gaps inserted into it
442 * sequence corresponding to profileseq which defines gap map for
445 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
450 LOCK.writeLock().lock();
452 char gc = al.getGapCharacter();
454 // take the set of hidden columns, and the set of gaps in origseq,
455 // and remove all the hidden gaps from hiddenColumns
457 // first get the gaps as a Bitset
458 BitSet gaps = origseq.gapBitset();
460 // now calculate hidden ^ not(gap)
461 BitSet hidden = new BitSet();
462 markHiddenRegions(hidden);
464 hiddenColumns = null;
465 this.hideColumns(hidden);
467 // for each sequence in the alignment, except the profile sequence,
468 // insert gaps corresponding to each hidden region but where each hidden
469 // column region is shifted backwards by the number of preceding visible
470 // gaps update hidden columns at the same time
471 Iterator<int[]> regions = hiddenColumns.iterator();
472 List<int[]> newhidden = new ArrayList<>();
474 int numGapsBefore = 0;
476 while (regions.hasNext())
478 // get region coordinates accounting for gaps
479 // we can rely on gaps not being *in* hidden regions because we already
481 int[] region = regions.next();
482 while (gapPosition < region[0])
485 if (gaps.get(gapPosition))
491 int left = region[0] - numGapsBefore;
492 int right = region[1] - numGapsBefore;
493 newhidden.add(new int[] { left, right });
495 // make a string with number of gaps = length of hidden region
496 StringBuffer sb = new StringBuffer();
497 for (int s = 0; s < right - left + 1; s++)
501 padGaps(sb, left, profileseq, al);
504 hiddenColumns = newhidden;
505 cursor.resetCursor(hiddenColumns);
509 LOCK.writeLock().unlock();
514 * Pad gaps in all sequences in alignment except profileseq
517 * gap string to insert
519 * position to insert at
521 * sequence not to pad
523 * alignment to pad sequences in
525 private void padGaps(StringBuffer sb, int pos, SequenceI profileseq,
528 // loop over the sequences and pad with gaps where required
529 for (int s = 0, ns = al.getHeight(); s < ns; s++)
531 SequenceI sqobj = al.getSequenceAt(s);
532 if (sqobj != profileseq)
534 String sq = al.getSequenceAt(s).getSequenceAsString();
535 if (sq.length() <= pos)
538 int diff = pos - sq.length() - 1;
543 while ((diff = pos - sq.length() - 1) > 0)
545 if (diff >= sb.length())
551 char[] buf = new char[diff];
552 sb.getChars(0, diff, buf, 0);
553 sq += buf.toString();
561 al.getSequenceAt(s).setSequence(
562 sq.substring(0, pos) + sb.toString() + sq.substring(pos));
569 * Methods which only need read access to the hidden columns collection.
570 * These methods should use a readLock to prevent other threads changing
571 * the hidden columns collection while it is in use.
575 * Output regions data as a string. String is in the format:
576 * reg0[0]<between>reg0[1]<delimiter>reg1[0]<between>reg1[1] ... regn[1]
579 * string to delimit regions
580 * @param betweenstring
581 * to put between start and end region values
582 * @return regions formatted according to delimiter and between strings
584 public String regionsToString(String delimiter, String between)
588 LOCK.readLock().lock();
589 StringBuilder regionBuilder = new StringBuilder();
590 if (hiddenColumns != null)
592 Iterator<int[]> it = hiddenColumns.iterator();
595 int[] range = it.next();
596 regionBuilder.append(delimiter).append(range[0]).append(between)
600 regionBuilder.deleteCharAt(0);
604 return regionBuilder.toString();
607 LOCK.readLock().unlock();
612 * Find the number of hidden columns
614 * @return number of hidden columns
620 LOCK.readLock().lock();
622 if (numColumns == 0 && hiddenColumns != null)
624 // numColumns is out of date, so recalculate
627 Iterator<int[]> it = hiddenColumns.iterator();
630 int[] range = it.next();
631 size += range[1] - range[0] + 1;
640 LOCK.readLock().unlock();
645 * Get the number of distinct hidden regions
647 * @return number of regions
649 public int getNumberOfRegions()
653 LOCK.readLock().lock();
655 if (hasHiddenColumns())
657 num = hiddenColumns.size();
662 LOCK.readLock().unlock();
667 public boolean equals(Object obj)
671 LOCK.readLock().lock();
673 if (!(obj instanceof HiddenColumns))
677 HiddenColumns that = (HiddenColumns) obj;
680 * check hidden columns are either both null, or match
682 if (this.hiddenColumns == null)
684 return (that.hiddenColumns == null);
686 if (that.hiddenColumns == null
687 || that.hiddenColumns.size() != this.hiddenColumns.size())
692 Iterator<int[]> it = hiddenColumns.iterator();
693 Iterator<int[]> thatit = that.iterator();
696 int[] thisRange = it.next();
697 int[] thatRange = thatit.next();
698 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
706 LOCK.readLock().unlock();
711 * Return absolute column index for a visible column index
714 * int column index in alignment view (count from zero)
715 * @return alignment column index for column
717 public int visibleToAbsoluteColumn(int column)
721 LOCK.readLock().lock();
724 if (hiddenColumns != null)
726 result += cursor.findRegionForVisColumn(column).getHiddenSoFar();
732 LOCK.readLock().unlock();
737 * Use this method to find out where a column will appear in the visible
738 * alignment when hidden columns exist. If the column is not visible, then the
739 * index of the next visible column on the left will be returned (or 0 if
740 * there is no visible column on the left)
742 * @param hiddenColumn
743 * the column index in the full alignment including hidden columns
744 * @return the position of the column in the visible alignment
746 public int absoluteToVisibleColumn(int hiddenColumn)
750 LOCK.readLock().lock();
751 int result = hiddenColumn;
753 if (hiddenColumns != null)
755 HiddenCursorPosition cursorPos = cursor
756 .findRegionForColumn(hiddenColumn);
757 int index = cursorPos.getRegionIndex();
758 int hiddenBeforeCol = cursorPos.getHiddenSoFar();
760 // just subtract hidden cols count - this works fine if column is
762 result = hiddenColumn - hiddenBeforeCol;
764 // now check in case column is hidden - it will be in the returned
766 if (index < hiddenColumns.size())
768 int[] region = hiddenColumns.get(index);
769 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
771 // actually col is hidden, return region[0]-1
772 // unless region[0]==0 in which case return 0
779 result = region[0] - 1 - hiddenBeforeCol;
785 return result; // return the shifted position after removing hidden
789 LOCK.readLock().unlock();
794 * Find the visible column which is a given visible number of columns to the
795 * left of another visible column. i.e. for a startColumn x, the column which
796 * is distance 1 away will be column x-1.
798 * @param visibleDistance
799 * the number of visible columns to offset by
801 * the column to start from
802 * @return the position of the column in the visible alignment
804 public int subtractVisibleColumns(int visibleDistance, int startColumn)
808 LOCK.readLock().lock();
809 int distance = visibleDistance;
811 // in case startColumn is in a hidden region, move it to the left
812 int start = visibleToAbsoluteColumn(absoluteToVisibleColumn(startColumn));
814 Iterator<int[]> it = new ReverseRegionsIterator(0, start,
817 while (it.hasNext() && (distance > 0))
819 int[] region = it.next();
821 if (start > region[1])
823 // subtract the gap to right of region from distance
824 if (start - region[1] <= distance)
826 distance -= start - region[1];
827 start = region[0] - 1;
831 start = start - distance;
836 return start - distance;
840 LOCK.readLock().unlock();
845 * This method returns the rightmost limit of a region of an alignment with
846 * hidden columns. In otherwords, the next hidden column.
849 * the absolute (visible) alignmentPosition to find the next hidden
851 * @return the index of the next hidden column, or alPos if there is no next
854 public int getHiddenBoundaryRight(int alPos)
858 LOCK.readLock().lock();
859 if (hiddenColumns != null)
861 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
862 if (index < hiddenColumns.size())
864 int[] region = hiddenColumns.get(index);
865 if (alPos < region[0])
869 else if ((alPos <= region[1])
870 && (index + 1 < hiddenColumns.size()))
872 // alPos is within a hidden region, return the next one
874 region = hiddenColumns.get(index + 1);
882 LOCK.readLock().unlock();
887 * This method returns the leftmost limit of a region of an alignment with
888 * hidden columns. In otherwords, the previous hidden column.
891 * the absolute (visible) alignmentPosition to find the previous
894 public int getHiddenBoundaryLeft(int alPos)
898 LOCK.readLock().lock();
900 if (hiddenColumns != null)
902 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
906 int[] region = hiddenColumns.get(index - 1);
913 LOCK.readLock().unlock();
919 * Answers if a column in the alignment is visible
922 * absolute position of column in the alignment
923 * @return true if column is visible
925 public boolean isVisible(int column)
929 LOCK.readLock().lock();
931 int regionindex = cursor.findRegionForColumn(column).getRegionIndex();
932 if (regionindex > -1 && regionindex < hiddenColumns.size())
934 int[] region = hiddenColumns.get(regionindex);
935 // already know that column <= region[1] as cursor returns containing
936 // region or region to right
937 if (column >= region[0])
946 LOCK.readLock().unlock();
951 * Get the visible sections of a set of sequences
954 * sequence position to start from
956 * sequence position to end at
958 * an array of sequences
959 * @return an array of strings encoding the visible parts of each sequence
961 public String[] getVisibleSequenceStrings(int start, int end,
966 LOCK.readLock().lock();
967 int iSize = seqs.length;
968 String[] selections = new String[iSize];
969 if (hiddenColumns != null && hiddenColumns.size() > 0)
971 for (int i = 0; i < iSize; i++)
973 StringBuffer visibleSeq = new StringBuffer();
975 Iterator<int[]> blocks = new VisibleContigsIterator(start,
976 end + 1, hiddenColumns);
978 while (blocks.hasNext())
980 int[] block = blocks.next();
981 if (blocks.hasNext())
984 .append(seqs[i].getSequence(block[0], block[1] + 1));
989 .append(seqs[i].getSequence(block[0], block[1]));
993 selections[i] = visibleSeq.toString();
998 for (int i = 0; i < iSize; i++)
1000 selections[i] = seqs[i].getSequenceAsString(start, end);
1007 LOCK.readLock().unlock();
1012 * Locate the first position visible for this sequence. If seq isn't visible
1013 * then return the position of the left side of the hidden boundary region.
1016 * sequence to find position for
1017 * @return visible start position
1019 public int locateVisibleStartOfSequence(SequenceI seq)
1023 LOCK.readLock().lock();
1026 if (hiddenColumns == null || hiddenColumns.size() == 0)
1028 return seq.findIndex(seq.getStart()) - 1;
1031 // Simply walk along the sequence whilst watching for hidden column
1033 Iterator<int[]> regions = hiddenColumns.iterator();
1034 int hideStart = seq.getLength();
1038 boolean foundStart = false;
1040 // step through the non-gapped positions of the sequence
1041 for (int i = seq.getStart(); i <= seq.getEnd() && (!foundStart); i++)
1043 // get alignment position of this residue in the sequence
1044 int p = seq.findIndex(i) - 1;
1046 // update hidden region start/end
1047 while (hideEnd < p && regions.hasNext())
1049 int[] region = regions.next();
1051 visNext += region[0] - visPrev;
1052 hideStart = region[0];
1053 hideEnd = region[1];
1057 hideStart = seq.getLength();
1059 // update visible boundary for sequence
1069 return absoluteToVisibleColumn(start);
1071 // otherwise, sequence was completely hidden
1075 LOCK.readLock().unlock();
1080 * delete any columns in alignmentAnnotation that are hidden (including
1081 * sequence associated annotation).
1083 * @param alignmentAnnotation
1085 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
1087 if (alignmentAnnotation != null
1088 && alignmentAnnotation.annotations != null)
1090 makeVisibleAnnotation(0, alignmentAnnotation.annotations.length,
1091 alignmentAnnotation);
1096 * delete any columns in alignmentAnnotation that are hidden (including
1097 * sequence associated annotation).
1100 * remove any annotation to the right of this column
1102 * remove any annotation to the left of this column
1103 * @param alignmentAnnotation
1104 * the annotation to operate on
1106 public void makeVisibleAnnotation(int start, int end,
1107 AlignmentAnnotation alignmentAnnotation)
1111 LOCK.readLock().lock();
1113 int startFrom = start;
1116 if (alignmentAnnotation != null
1117 && alignmentAnnotation.annotations != null)
1119 if (hiddenColumns != null && hiddenColumns.size() > 0)
1121 removeHiddenAnnotation(startFrom, endAt, alignmentAnnotation);
1125 alignmentAnnotation.restrict(startFrom, endAt);
1130 LOCK.readLock().unlock();
1134 private void removeHiddenAnnotation(int start, int end,
1135 AlignmentAnnotation alignmentAnnotation)
1137 // mangle the alignmentAnnotation annotation array
1138 ArrayList<Annotation[]> annels = new ArrayList<>();
1139 Annotation[] els = null;
1143 Iterator<int[]> blocks = new VisibleContigsIterator(start, end + 1,
1147 int annotationLength;
1148 while (blocks.hasNext())
1150 int[] block = blocks.next();
1151 annotationLength = block[1] - block[0] + 1;
1153 if (blocks.hasNext())
1155 // copy just the visible segment of the annotation row
1156 copylength = annotationLength;
1160 if (annotationLength + block[0] <= alignmentAnnotation.annotations.length)
1162 // copy just the visible segment of the annotation row
1163 copylength = annotationLength;
1167 // copy to the end of the annotation row
1168 copylength = alignmentAnnotation.annotations.length - block[0];
1172 els = new Annotation[annotationLength];
1174 System.arraycopy(alignmentAnnotation.annotations, block[0], els, 0,
1176 w += annotationLength;
1181 alignmentAnnotation.annotations = new Annotation[w];
1184 for (Annotation[] chnk : annels)
1186 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1195 * @return true if there are columns hidden
1197 public boolean hasHiddenColumns()
1201 LOCK.readLock().lock();
1203 // we don't use getSize()>0 here because it has to iterate over
1204 // the full hiddenColumns collection and so will be much slower
1205 return hiddenColumns != null && hiddenColumns.size() > 0;
1208 LOCK.readLock().unlock();
1214 * @return true if there is more than one hidden column region
1216 public boolean hasMultiHiddenColumnRegions()
1220 LOCK.readLock().lock();
1221 return hiddenColumns != null && hiddenColumns.size() > 1;
1224 LOCK.readLock().unlock();
1230 * Returns a hashCode built from hidden column ranges
1233 public int hashCode()
1237 LOCK.readLock().lock();
1239 Iterator<int[]> it = hiddenColumns.iterator();
1240 while (it.hasNext())
1242 int[] hidden = it.next();
1243 hashCode = HASH_MULTIPLIER * hashCode + hidden[0];
1244 hashCode = HASH_MULTIPLIER * hashCode + hidden[1];
1249 LOCK.readLock().unlock();
1254 * Hide columns corresponding to the marked bits
1257 * - columns map to bits starting from zero
1259 public void hideColumns(BitSet inserts)
1263 LOCK.writeLock().lock();
1264 for (int firstSet = inserts
1265 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1266 .nextSetBit(lastSet))
1268 lastSet = inserts.nextClearBit(firstSet);
1269 hideColumns(firstSet, lastSet - 1);
1271 cursor.resetCursor(hiddenColumns);
1275 LOCK.writeLock().unlock();
1282 * BitSet where hidden columns will be marked
1284 public void markHiddenRegions(BitSet inserts)
1288 LOCK.readLock().lock();
1289 if (hiddenColumns == null)
1293 Iterator<int[]> it = hiddenColumns.iterator();
1294 while (it.hasNext())
1296 int[] range = it.next();
1297 inserts.set(range[0], range[1] + 1);
1301 LOCK.readLock().unlock();
1306 * Calculate the visible start and end index of an alignment.
1309 * full alignment width
1310 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1312 public int[] getVisibleStartAndEndIndex(int width)
1316 LOCK.readLock().lock();
1317 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1318 int startPos = alignmentStartEnd[0];
1319 int endPos = alignmentStartEnd[1];
1321 int[] lowestRange = new int[] { -1, -1 };
1322 int[] higestRange = new int[] { -1, -1 };
1324 if (hiddenColumns == null)
1326 return new int[] { startPos, endPos };
1329 Iterator<int[]> it = hiddenColumns.iterator();
1330 while (it.hasNext())
1332 int[] range = it.next();
1333 lowestRange = (range[0] <= startPos) ? range : lowestRange;
1334 higestRange = (range[1] >= endPos) ? range : higestRange;
1337 if (lowestRange[0] == -1) // includes (lowestRange[1] == -1)
1339 startPos = alignmentStartEnd[0];
1343 startPos = lowestRange[1] + 1;
1346 if (higestRange[0] == -1) // includes (higestRange[1] == -1)
1348 endPos = alignmentStartEnd[1];
1352 endPos = higestRange[0] - 1;
1354 return new int[] { startPos, endPos };
1357 LOCK.readLock().unlock();
1362 * Finds the hidden region (if any) which starts or ends at res
1365 * visible residue position, unadjusted for hidden columns
1366 * @return region as [start,end] or null if no matching region is found. If
1367 * res is adjacent to two regions, returns the left region.
1369 public int[] getRegionWithEdgeAtRes(int res)
1373 LOCK.readLock().lock();
1374 int adjres = visibleToAbsoluteColumn(res);
1376 int[] reveal = null;
1378 if (hiddenColumns != null)
1380 // look for a region ending just before adjres
1381 int regionindex = cursor.findRegionForColumn(adjres - 1)
1383 if (regionindex < hiddenColumns.size()
1384 && hiddenColumns.get(regionindex)[1] == adjres - 1)
1386 reveal = hiddenColumns.get(regionindex);
1388 // check if the region ends just after adjres
1389 else if (regionindex < hiddenColumns.size()
1390 && hiddenColumns.get(regionindex)[0] == adjres + 1)
1392 reveal = hiddenColumns.get(regionindex);
1399 LOCK.readLock().unlock();
1404 * Return an iterator over the hidden regions
1406 public Iterator<int[]> iterator()
1410 LOCK.readLock().lock();
1411 return new HiddenColsIterator(hiddenColumns);
1414 LOCK.readLock().unlock();
1419 * Return a bounded iterator over the hidden regions
1422 * position to start from (inclusive, absolute column position)
1424 * position to end at (inclusive, absolute column position)
1427 public Iterator<int[]> getBoundedIterator(int start, int end)
1431 LOCK.readLock().lock();
1432 return new HiddenColsIterator(start, end, hiddenColumns);
1435 LOCK.readLock().unlock();
1440 * Return a bounded iterator over the *visible* start positions of hidden
1444 * position to start from (inclusive, visible column position)
1446 * position to end at (inclusive, visible column position)
1448 public Iterator<Integer> getBoundedStartIterator(int start, int end)
1452 LOCK.readLock().lock();
1454 // get absolute position of column in alignment
1455 int absoluteStart = visibleToAbsoluteColumn(start);
1457 // Get cursor position and supply it to the iterator:
1458 // Since we want visible region start, we look for a cursor for the
1459 // (absoluteStart-1), then if absoluteStart is the start of a visible
1460 // region we'll get the cursor pointing to the region before, which is
1462 HiddenCursorPosition pos = cursor
1463 .findRegionForColumn(absoluteStart - 1);
1465 return new BoundedStartRegionIterator(pos, start, end,
1469 LOCK.readLock().unlock();
1474 * Return an iterator over visible *columns* (not regions) between the given
1475 * start and end boundaries
1478 * first column (inclusive)
1480 * last column (inclusive)
1482 public Iterator<Integer> getVisibleColsIterator(int start, int end)
1486 LOCK.readLock().lock();
1487 return new VisibleColsIterator(start, end, hiddenColumns);
1490 LOCK.readLock().unlock();
1495 * return an iterator over visible segments between the given start and end
1499 * first column, inclusive from 0
1501 * last column - not inclusive
1502 * @param useVisibleCoords
1503 * if true, start and end are visible column positions, not absolute
1506 public Iterator<int[]> getVisContigsIterator(int start, int end,
1507 boolean useVisibleCoords)
1509 int adjstart = start;
1511 if (useVisibleCoords)
1513 adjstart = visibleToAbsoluteColumn(start);
1514 adjend = visibleToAbsoluteColumn(end);
1519 LOCK.readLock().lock();
1520 return new VisibleContigsIterator(adjstart, adjend, hiddenColumns);
1523 LOCK.readLock().unlock();