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.
50 public class HiddenColumns
52 private static final int HASH_MULTIPLIER = 31;
54 private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock();
57 * Cursor which tracks the last used hidden columns region, and the number
58 * of hidden columns up to (but not including) that region.
60 private HiddenColumnsCursor cursor = new HiddenColumnsCursor();
63 * cache of the number of hidden columns
65 private int numColumns = 0;
68 * list of hidden column [start, end] ranges; the list is maintained in
69 * ascending start column order
71 private ArrayList<int[]> hiddenColumns;
76 public HiddenColumns()
81 * Methods which change the hiddenColumns collection. These methods should
82 * use a writeLock to prevent other threads accessing the hiddenColumns
83 * collection while changes are being made. They should also reset the hidden
84 * columns cursor, and either update the hidden columns count, or set it to 0
85 * (so that it will later be updated when needed).
92 * the HiddenColumns object to copy from
94 public HiddenColumns(HiddenColumns copy)
98 LOCK.writeLock().lock();
102 if (copy.hiddenColumns != null)
104 hiddenColumns = new ArrayList<>();
105 Iterator<int[]> it = copy.iterator();
108 int[] region = it.next();
109 hiddenColumns.add(region);
110 numColumns += region[1] - region[0] + 1;
112 cursor.resetCursor(hiddenColumns);
117 LOCK.writeLock().unlock();
122 * Copy constructor within bounds and with offset. Copies hidden column
123 * regions fully contained between start and end, and offsets positions by
124 * subtracting offset.
127 * HiddenColumns instance to copy from
129 * lower bound to copy from
131 * upper bound to copy to
133 * offset to subtract from each region boundary position
136 public HiddenColumns(HiddenColumns copy, int start, int end, int offset)
140 LOCK.writeLock().lock();
143 hiddenColumns = new ArrayList<>();
145 Iterator<int[]> it = copy.getBoundedIterator(start, end);
148 int[] region = it.next();
149 // still need to check boundaries because iterator returns
150 // all overlapping regions and we need contained regions
151 if (region[0] >= start && region[1] <= end)
155 { region[0] - offset, region[1] - offset });
156 numColumns += region[1] - region[0] + 1;
159 cursor.resetCursor(hiddenColumns);
163 LOCK.writeLock().unlock();
168 * Adds the specified column range to the hidden columns collection
171 * start of range to add (absolute position in alignment)
173 * end of range to add (absolute position in alignment)
175 public void hideColumns(int start, int end)
177 boolean wasAlreadyLocked = false;
180 // check if the write lock was already locked by this thread,
181 // as this method can be called internally in loops within HiddenColumns
182 if (!LOCK.isWriteLockedByCurrentThread())
184 LOCK.writeLock().lock();
188 wasAlreadyLocked = true;
192 int prevHiddenCount = 0;
194 if (hiddenColumns == null)
196 hiddenColumns = new ArrayList<>();
200 // set up cursor reset values
201 HiddenCursorPosition cursorPos = cursor.findRegionForColumn(start);
202 regionindex = cursorPos.getRegionIndex();
206 // get previous index and hidden count for updating the cursor later
207 previndex = regionindex - 1;
208 int[] prevRegion = hiddenColumns.get(previndex);
209 prevHiddenCount = cursorPos.getHiddenSoFar()
210 - (prevRegion[1] - prevRegion[0] + 1);
215 * new range follows everything else; check first to avoid looping over whole hiddenColumns collection
217 if (hiddenColumns.isEmpty()
218 || start > hiddenColumns.get(hiddenColumns.size() - 1)[1])
220 hiddenColumns.add(new int[] { start, end });
225 * traverse existing hidden ranges and insert / amend / append as
228 boolean added = false;
231 added = insertRangeAtRegion(regionindex - 1, start, end);
233 if (!added && regionindex < hiddenColumns.size())
235 insertRangeAtRegion(regionindex, start, end);
239 // reset the cursor to just before our insertion point: this saves
240 // a lot of reprocessing in large alignments
241 cursor.resetCursor(hiddenColumns, previndex, prevHiddenCount);
243 // reset the number of columns so they will be recounted
248 if (!wasAlreadyLocked)
250 LOCK.writeLock().unlock();
256 * Insert [start, range] at the region at index i in hiddenColumns, if
262 * start of range to insert
264 * end of range to insert
265 * @return true if range was successfully inserted
267 private boolean insertRangeAtRegion(int i, int start, int end)
269 boolean added = false;
271 int[] region = hiddenColumns.get(i);
272 if (end < region[0] - 1)
275 * insert discontiguous preceding range
277 hiddenColumns.add(i, new int[] { start, end });
280 else if (end <= region[1])
283 * new range overlaps existing, or is contiguous preceding it - adjust
286 region[0] = Math.min(region[0], start);
289 else if (start <= region[1] + 1)
292 * new range overlaps existing, or is contiguous following it - adjust
293 * start and end columns
295 region[0] = Math.min(region[0], start);
296 region[1] = Math.max(region[1], end);
299 * also update or remove any subsequent ranges
300 * that are overlapped
302 while (i < hiddenColumns.size() - 1)
304 int[] nextRegion = hiddenColumns.get(i + 1);
305 if (nextRegion[0] > end + 1)
308 * gap to next hidden range - no more to update
312 region[1] = Math.max(nextRegion[1], end);
314 // in theory this is faster than hiddenColumns.remove(i+1)
315 // benchmarking results a bit ambivalent
316 hiddenColumns.subList(i + 1, i + 2).clear();
324 * mark the columns corresponding to gap characters as hidden in the column
329 public void hideInsertionsFor(SequenceI sr)
333 LOCK.writeLock().lock();
334 List<int[]> inserts = sr.getInsertions();
335 for (int[] r : inserts)
337 hideColumns(r[0], r[1]);
339 cursor.resetCursor(hiddenColumns);
343 LOCK.writeLock().unlock();
348 * Unhides, and adds to the selection list, all hidden columns
350 public void revealAllHiddenColumns(ColumnSelection sel)
354 LOCK.writeLock().lock();
355 if (hiddenColumns != null)
357 Iterator<int[]> it = hiddenColumns.iterator();
360 int[] region = it.next();
361 for (int j = region[0]; j < region[1] + 1; j++)
366 hiddenColumns = null;
367 cursor.resetCursor(hiddenColumns);
372 LOCK.writeLock().unlock();
377 * Reveals, and marks as selected, the hidden column range with the given
381 * the start column to look for
383 * the column selection to add the hidden column range to
385 public void revealHiddenColumns(int start, ColumnSelection sel)
389 LOCK.writeLock().lock();
391 if (hiddenColumns != null)
393 int regionIndex = cursor.findRegionForColumn(start)
396 if (regionIndex != -1 && regionIndex != hiddenColumns.size())
398 // regionIndex is the region which either contains start
399 // or lies to the right of start
400 int[] region = hiddenColumns.get(regionIndex);
401 if (start == region[0])
403 for (int j = region[0]; j < region[1] + 1; j++)
407 int colsToRemove = region[1] - region[0] + 1;
408 hiddenColumns.remove(regionIndex);
410 if (hiddenColumns.isEmpty())
412 hiddenColumns = null;
417 numColumns -= colsToRemove;
419 cursor.updateForDeletedRegion(hiddenColumns);
425 LOCK.writeLock().unlock();
430 * Add gaps into the sequences aligned to profileseq under the given
434 * sequence in al which sequences are aligned to
436 * alignment to have gaps inserted into it
438 * alignment view where sequence corresponding to profileseq is first
440 * @return new HiddenColumns for new alignment view, with insertions into
441 * profileseq marked as hidden.
443 public static HiddenColumns propagateInsertions(SequenceI profileseq,
444 AlignmentI al, AlignmentView input)
448 char gc = al.getGapCharacter();
449 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
450 HiddenColumns nview = (HiddenColumns) alandhidden[1];
451 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
452 nview.propagateInsertions(profileseq, al, origseq);
459 * sequence in al which corresponds to origseq
461 * alignment which is to have gaps inserted into it
463 * sequence corresponding to profileseq which defines gap map for
466 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
471 LOCK.writeLock().lock();
473 char gc = al.getGapCharacter();
475 // take the set of hidden columns, and the set of gaps in origseq,
476 // and remove all the hidden gaps from hiddenColumns
478 // first get the gaps as a Bitset
479 BitSet gaps = origseq.gapBitset();
481 // now calculate hidden ^ not(gap)
482 BitSet hidden = new BitSet();
483 markHiddenRegions(hidden);
485 hiddenColumns = null;
486 this.hideMarkedBits(hidden);
488 // for each sequence in the alignment, except the profile sequence,
489 // insert gaps corresponding to each hidden region but where each hidden
490 // column region is shifted backwards by the number of preceding visible
491 // gaps update hidden columns at the same time
492 Iterator<int[]> regions = hiddenColumns.iterator();
493 ArrayList<int[]> newhidden = new ArrayList<>();
495 int numGapsBefore = 0;
497 while (regions.hasNext())
499 // get region coordinates accounting for gaps
500 // we can rely on gaps not being *in* hidden regions because we already
502 int[] region = regions.next();
503 while (gapPosition < region[0])
506 if (gaps.get(gapPosition))
512 int left = region[0] - numGapsBefore;
513 int right = region[1] - numGapsBefore;
514 newhidden.add(new int[] { left, right });
516 // make a string with number of gaps = length of hidden region
517 StringBuffer sb = new StringBuffer();
518 for (int s = 0; s < right - left + 1; s++)
522 padGaps(sb, left, profileseq, al);
525 hiddenColumns = newhidden;
526 cursor.resetCursor(hiddenColumns);
530 LOCK.writeLock().unlock();
535 * Pad gaps in all sequences in alignment except profileseq
538 * gap string to insert
540 * position to insert at
542 * sequence not to pad
544 * alignment to pad sequences in
546 private void padGaps(StringBuffer sb, int pos, SequenceI profileseq,
549 // loop over the sequences and pad with gaps where required
550 for (int s = 0, ns = al.getHeight(); s < ns; s++)
552 SequenceI sqobj = al.getSequenceAt(s);
553 if (sqobj != profileseq)
555 String sq = al.getSequenceAt(s).getSequenceAsString();
556 if (sq.length() <= pos)
559 int diff = pos - sq.length() - 1;
564 while ((diff = pos - sq.length() - 1) > 0)
566 if (diff >= sb.length())
572 char[] buf = new char[diff];
573 sb.getChars(0, diff, buf, 0);
574 sq += buf.toString();
582 al.getSequenceAt(s).setSequence(
583 sq.substring(0, pos) + sb.toString() + sq.substring(pos));
590 * Methods which only need read access to the hidden columns collection.
591 * These methods should use a readLock to prevent other threads changing
592 * the hidden columns collection while it is in use.
596 * Output regions data as a string. String is in the format:
597 * reg0[0]<between>reg0[1]<delimiter>reg1[0]<between>reg1[1] ... regn[1]
600 * string to delimit regions
601 * @param betweenstring
602 * to put between start and end region values
603 * @return regions formatted according to delimiter and between strings
605 public String regionsToString(String delimiter, String between)
609 LOCK.readLock().lock();
610 StringBuilder regionBuilder = new StringBuilder();
611 if (hiddenColumns != null)
613 Iterator<int[]> it = hiddenColumns.iterator();
616 int[] range = it.next();
617 regionBuilder.append(delimiter).append(range[0]).append(between)
621 regionBuilder.deleteCharAt(0);
625 return regionBuilder.toString();
628 LOCK.readLock().unlock();
633 * Find the number of hidden columns
635 * @return number of hidden columns
641 LOCK.readLock().lock();
643 if (numColumns == 0 && hiddenColumns != null)
645 // numColumns is out of date, so recalculate
648 Iterator<int[]> it = hiddenColumns.iterator();
651 int[] range = it.next();
652 size += range[1] - range[0] + 1;
661 LOCK.readLock().unlock();
666 * Get the number of distinct hidden regions
668 * @return number of regions
670 public int getNumberOfRegions()
674 LOCK.readLock().lock();
676 if (hasHiddenColumns())
678 num = hiddenColumns.size();
683 LOCK.readLock().unlock();
688 public boolean equals(Object obj)
692 LOCK.readLock().lock();
694 if (!(obj instanceof HiddenColumns))
698 HiddenColumns that = (HiddenColumns) obj;
701 * check hidden columns are either both null, or match
703 if (this.hiddenColumns == null)
705 return (that.hiddenColumns == null);
707 if (that.hiddenColumns == null
708 || that.hiddenColumns.size() != this.hiddenColumns.size())
713 Iterator<int[]> it = hiddenColumns.iterator();
714 Iterator<int[]> thatit = that.iterator();
717 int[] thisRange = it.next();
718 int[] thatRange = thatit.next();
719 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
727 LOCK.readLock().unlock();
732 * Return absolute column index for a visible column index
735 * int column index in alignment view (count from zero)
736 * @return alignment column index for column
738 public int adjustForHiddenColumns(int column)
742 LOCK.readLock().lock();
745 if (hiddenColumns != null)
747 result += cursor.findRegionForVisColumn(column).getHiddenSoFar();
753 LOCK.readLock().unlock();
758 * Use this method to find out where a column will appear in the visible
759 * alignment when hidden columns exist. If the column is not visible, then the
760 * left-most visible column will always be returned.
762 * @param hiddenColumn
763 * the column index in the full alignment including hidden columns
764 * @return the position of the column in the visible alignment
766 public int findColumnPosition(int hiddenColumn)
770 LOCK.readLock().lock();
771 int result = hiddenColumn;
773 if (hiddenColumns != null)
775 HiddenCursorPosition cursorPos = cursor
776 .findRegionForColumn(hiddenColumn);
777 int index = cursorPos.getRegionIndex();
778 int hiddenBeforeCol = cursorPos.getHiddenSoFar();
780 // just subtract hidden cols count - this works fine if column is
782 result = hiddenColumn - hiddenBeforeCol;
784 // now check in case column is hidden - it will be in the returned
786 if (index < hiddenColumns.size())
788 int[] region = hiddenColumns.get(index);
789 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
791 // actually col is hidden, return region[0]-1
792 // unless region[0]==0 in which case return 0
799 result = region[0] - 1 - hiddenBeforeCol;
805 return result; // return the shifted position after removing hidden
809 LOCK.readLock().unlock();
814 * Find the visible column which is a given visible number of columns to the
815 * left of another visible column. i.e. for a startColumn x, the column which
816 * is distance 1 away will be column x-1.
818 * @param visibleDistance
819 * the number of visible columns to offset by
821 * the column to start from
822 * @return the position of the column in the visible alignment
824 public int subtractVisibleColumns(int visibleDistance, int startColumn)
828 LOCK.readLock().lock();
829 int distance = visibleDistance;
831 // in case startColumn is in a hidden region, move it to the left
832 int start = adjustForHiddenColumns(findColumnPosition(startColumn));
834 Iterator<int[]> it = new ReverseRegionsIterator(0, start,
837 while (it.hasNext() && (distance > 0))
839 int[] region = it.next();
841 if (start > region[1])
843 // subtract the gap to right of region from distance
844 if (start - region[1] <= distance)
846 distance -= start - region[1];
847 start = region[0] - 1;
851 start = start - distance;
856 return start - distance;
860 LOCK.readLock().unlock();
865 * This method returns the rightmost limit of a region of an alignment with
866 * hidden columns. In otherwords, the next hidden column.
869 * the absolute (visible) alignmentPosition to find the next hidden
871 * @return the index of the next hidden column, or alPos if there is no next
874 public int getHiddenBoundaryRight(int alPos)
878 LOCK.readLock().lock();
879 if (hiddenColumns != null)
881 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
882 if (index < hiddenColumns.size())
884 int[] region = hiddenColumns.get(index);
885 if (alPos < region[0])
889 else if ((alPos <= region[1])
890 && (index + 1 < hiddenColumns.size()))
892 // alPos is within a hidden region, return the next one
894 region = hiddenColumns.get(index + 1);
902 LOCK.readLock().unlock();
907 * This method returns the leftmost limit of a region of an alignment with
908 * hidden columns. In otherwords, the previous hidden column.
911 * the absolute (visible) alignmentPosition to find the previous
914 public int getHiddenBoundaryLeft(int alPos)
918 LOCK.readLock().lock();
920 if (hiddenColumns != null)
922 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
926 int[] region = hiddenColumns.get(index - 1);
933 LOCK.readLock().unlock();
939 * Answers if a column in the alignment is visible
942 * absolute position of column in the alignment
943 * @return true if column is visible
945 public boolean isVisible(int column)
949 LOCK.readLock().lock();
951 int regionindex = cursor.findRegionForColumn(column).getRegionIndex();
952 if (regionindex > -1 && regionindex < hiddenColumns.size())
954 int[] region = hiddenColumns.get(regionindex);
955 // already know that column <= region[1] as cursor returns containing
956 // region or region to right
957 if (column >= region[0])
966 LOCK.readLock().unlock();
971 * Get the visible sections of a set of sequences
974 * sequence position to start from
976 * sequence position to end at
978 * an array of sequences
979 * @return an array of strings encoding the visible parts of each sequence
981 public String[] getVisibleSequenceStrings(int start, int end,
986 LOCK.readLock().lock();
987 int iSize = seqs.length;
988 String[] selections = new String[iSize];
989 if (hiddenColumns != null && hiddenColumns.size() > 0)
991 for (int i = 0; i < iSize; i++)
993 StringBuffer visibleSeq = new StringBuffer();
995 Iterator<int[]> blocks = new VisibleContigsIterator(start,
996 end + 1, hiddenColumns);
998 while (blocks.hasNext())
1000 int[] block = blocks.next();
1001 if (blocks.hasNext())
1004 .append(seqs[i].getSequence(block[0], block[1] + 1));
1009 .append(seqs[i].getSequence(block[0], block[1]));
1013 selections[i] = visibleSeq.toString();
1018 for (int i = 0; i < iSize; i++)
1020 selections[i] = seqs[i].getSequenceAsString(start, end);
1027 LOCK.readLock().unlock();
1032 * Locate the first position visible for this sequence. If seq isn't visible
1033 * then return the position of the left side of the hidden boundary region.
1036 * sequence to find position for
1037 * @return visible start position
1039 public int locateVisibleStartOfSequence(SequenceI seq)
1043 LOCK.readLock().lock();
1046 if (hiddenColumns == null || hiddenColumns.size() == 0)
1048 return seq.findIndex(seq.getStart()) - 1;
1051 // Simply walk along the sequence whilst watching for hidden column
1053 Iterator<int[]> regions = hiddenColumns.iterator();
1054 int hideStart = seq.getLength();
1058 boolean foundStart = false;
1060 // step through the non-gapped positions of the sequence
1061 for (int i = seq.getStart(); i <= seq.getEnd() && (!foundStart); i++)
1063 // get alignment position of this residue in the sequence
1064 int p = seq.findIndex(i) - 1;
1066 // update hidden region start/end
1067 while (hideEnd < p && regions.hasNext())
1069 int[] region = regions.next();
1071 visNext += region[0] - visPrev;
1072 hideStart = region[0];
1073 hideEnd = region[1];
1077 hideStart = seq.getLength();
1079 // update visible boundary for sequence
1089 return findColumnPosition(start);
1091 // otherwise, sequence was completely hidden
1095 LOCK.readLock().unlock();
1100 * delete any columns in alignmentAnnotation that are hidden (including
1101 * sequence associated annotation).
1103 * @param alignmentAnnotation
1105 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
1107 if (alignmentAnnotation != null
1108 && alignmentAnnotation.annotations != null)
1110 makeVisibleAnnotation(0, alignmentAnnotation.annotations.length,
1111 alignmentAnnotation);
1116 * delete any columns in alignmentAnnotation that are hidden (including
1117 * sequence associated annotation).
1120 * remove any annotation to the right of this column
1122 * remove any annotation to the left of this column
1123 * @param alignmentAnnotation
1124 * the annotation to operate on
1126 public void makeVisibleAnnotation(int start, int end,
1127 AlignmentAnnotation alignmentAnnotation)
1131 LOCK.readLock().lock();
1133 int startFrom = start;
1136 if (alignmentAnnotation != null
1137 && alignmentAnnotation.annotations != null)
1139 if (hiddenColumns != null && hiddenColumns.size() > 0)
1141 removeHiddenAnnotation(startFrom, endAt, alignmentAnnotation);
1145 alignmentAnnotation.restrict(startFrom, endAt);
1150 LOCK.readLock().unlock();
1154 private void removeHiddenAnnotation(int start, int end,
1155 AlignmentAnnotation alignmentAnnotation)
1157 // mangle the alignmentAnnotation annotation array
1158 ArrayList<Annotation[]> annels = new ArrayList<>();
1159 Annotation[] els = null;
1163 Iterator<int[]> blocks = new VisibleContigsIterator(start, end + 1,
1167 int annotationLength;
1168 while (blocks.hasNext())
1170 int[] block = blocks.next();
1171 annotationLength = block[1] - block[0] + 1;
1173 if (blocks.hasNext())
1175 // copy just the visible segment of the annotation row
1176 copylength = annotationLength;
1180 if (annotationLength + block[0] <= alignmentAnnotation.annotations.length)
1182 // copy just the visible segment of the annotation row
1183 copylength = annotationLength;
1187 // copy to the end of the annotation row
1188 copylength = alignmentAnnotation.annotations.length - block[0];
1192 els = new Annotation[annotationLength];
1194 System.arraycopy(alignmentAnnotation.annotations, block[0], els, 0,
1196 w += annotationLength;
1201 alignmentAnnotation.annotations = new Annotation[w];
1204 for (Annotation[] chnk : annels)
1206 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1215 * @return true if there are columns hidden
1217 public boolean hasHiddenColumns()
1221 LOCK.readLock().lock();
1223 // we don't use getSize()>0 here because it has to iterate over
1224 // the full hiddenColumns collection and so will be much slower
1225 return hiddenColumns != null && hiddenColumns.size() > 0;
1228 LOCK.readLock().unlock();
1234 * @return true if there is more than one hidden column region
1236 public boolean hasMultiHiddenColumnRegions()
1240 LOCK.readLock().lock();
1241 return hiddenColumns != null && hiddenColumns.size() > 1;
1244 LOCK.readLock().unlock();
1250 * Returns a hashCode built from hidden column ranges
1253 public int hashCode()
1257 LOCK.readLock().lock();
1259 Iterator<int[]> it = hiddenColumns.iterator();
1260 while (it.hasNext())
1262 int[] hidden = it.next();
1263 hashCode = HASH_MULTIPLIER * hashCode + hidden[0];
1264 hashCode = HASH_MULTIPLIER * hashCode + hidden[1];
1269 LOCK.readLock().unlock();
1274 * Hide columns corresponding to the marked bits
1277 * - columns map to bits starting from zero
1279 public void hideMarkedBits(BitSet inserts)
1283 LOCK.writeLock().lock();
1284 for (int firstSet = inserts
1285 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1286 .nextSetBit(lastSet))
1288 lastSet = inserts.nextClearBit(firstSet);
1289 hideColumns(firstSet, lastSet - 1);
1291 cursor.resetCursor(hiddenColumns);
1295 LOCK.writeLock().unlock();
1302 * BitSet where hidden columns will be marked
1304 public void markHiddenRegions(BitSet inserts)
1308 LOCK.readLock().lock();
1309 if (hiddenColumns == null)
1313 Iterator<int[]> it = hiddenColumns.iterator();
1314 while (it.hasNext())
1316 int[] range = it.next();
1317 inserts.set(range[0], range[1] + 1);
1321 LOCK.readLock().unlock();
1326 * Calculate the visible start and end index of an alignment.
1329 * full alignment width
1330 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1332 public int[] getVisibleStartAndEndIndex(int width)
1336 LOCK.readLock().lock();
1337 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1338 int startPos = alignmentStartEnd[0];
1339 int endPos = alignmentStartEnd[1];
1341 int[] lowestRange = new int[] { -1, -1 };
1342 int[] higestRange = new int[] { -1, -1 };
1344 if (hiddenColumns == null)
1346 return new int[] { startPos, endPos };
1349 Iterator<int[]> it = hiddenColumns.iterator();
1350 while (it.hasNext())
1352 int[] range = it.next();
1353 lowestRange = (range[0] <= startPos) ? range : lowestRange;
1354 higestRange = (range[1] >= endPos) ? range : higestRange;
1357 if (lowestRange[0] == -1) // includes (lowestRange[1] == -1)
1359 startPos = alignmentStartEnd[0];
1363 startPos = lowestRange[1] + 1;
1366 if (higestRange[0] == -1) // includes (higestRange[1] == -1)
1368 endPos = alignmentStartEnd[1];
1372 endPos = higestRange[0] - 1;
1374 return new int[] { startPos, endPos };
1377 LOCK.readLock().unlock();
1382 * Finds the hidden region (if any) which starts or ends at res
1385 * visible residue position, unadjusted for hidden columns
1386 * @return region as [start,end] or null if no matching region is found
1388 public int[] getRegionWithEdgeAtRes(int res)
1392 LOCK.readLock().lock();
1393 int adjres = adjustForHiddenColumns(res);
1395 int[] reveal = null;
1397 if (hiddenColumns != null)
1399 // look for a region ending just before adjres
1400 int regionindex = cursor.findRegionForColumn(adjres - 1)
1402 if (regionindex < hiddenColumns.size()
1403 && hiddenColumns.get(regionindex)[1] == adjres - 1)
1405 reveal = hiddenColumns.get(regionindex);
1407 // check if the region ends just after adjres
1408 else if (regionindex < hiddenColumns.size()
1409 && hiddenColumns.get(regionindex)[0] == adjres + 1)
1411 reveal = hiddenColumns.get(regionindex);
1418 LOCK.readLock().unlock();
1423 * Return an iterator over the hidden regions
1425 public Iterator<int[]> iterator()
1429 LOCK.readLock().lock();
1430 return new BoundedHiddenColsIterator(hiddenColumns);
1433 LOCK.readLock().unlock();
1438 * Return a bounded iterator over the hidden regions
1441 * position to start from (inclusive, absolute column position)
1443 * position to end at (inclusive, absolute column position)
1446 public Iterator<int[]> getBoundedIterator(int start, int end)
1450 LOCK.readLock().lock();
1451 return new BoundedHiddenColsIterator(start, end, hiddenColumns);
1454 LOCK.readLock().unlock();
1459 * Return a bounded iterator over the *visible* start positions of hidden
1463 * position to start from (inclusive, visible column position)
1465 * position to end at (inclusive, visible column position)
1467 public Iterator<Integer> getBoundedStartIterator(int start, int end)
1471 LOCK.readLock().lock();
1473 // get absolute position of column in alignment
1474 int absoluteStart = adjustForHiddenColumns(start);
1476 // Get cursor position and supply it to the iterator:
1477 // Since we want visible region start, we look for a cursor for the
1478 // (absoluteStart-1), then if absoluteStart is the start of a visible
1479 // region we'll get the cursor pointing to the region before, which is
1481 HiddenCursorPosition pos = cursor
1482 .findRegionForColumn(absoluteStart - 1);
1484 return new BoundedStartRegionIterator(pos, start, end,
1488 LOCK.readLock().unlock();
1493 * Return an iterator over visible *columns* (not regions) between the given
1494 * start and end boundaries
1497 * first column (inclusive)
1499 * last column (inclusive)
1501 public Iterator<Integer> getVisibleColsIterator(int start, int end)
1505 LOCK.readLock().lock();
1506 return new VisibleColsIterator(start, end, hiddenColumns);
1509 LOCK.readLock().unlock();
1514 * return an iterator over visible segments between the given start and end
1518 * first column, inclusive from 0
1520 * last column - not inclusive
1521 * @param useVisibleCoords
1522 * if true, start and end are visible column positions, not absolute
1525 public Iterator<int[]> getVisContigsIterator(int start, int end,
1526 boolean useVisibleCoords)
1528 int adjstart = start;
1530 if (useVisibleCoords)
1532 adjstart = adjustForHiddenColumns(start);
1533 adjend = adjustForHiddenColumns(end);
1538 LOCK.readLock().lock();
1539 return new VisibleContigsIterator(adjstart, adjend, hiddenColumns);
1542 LOCK.readLock().unlock();