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 visibleToAbsoluteColumn(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 * index of the next visible column on the left will be returned (or 0 if
761 * there is no visible column on the left)
763 * @param hiddenColumn
764 * the column index in the full alignment including hidden columns
765 * @return the position of the column in the visible alignment
767 public int absoluteToVisibleColumn(int hiddenColumn)
771 LOCK.readLock().lock();
772 int result = hiddenColumn;
774 if (hiddenColumns != null)
776 HiddenCursorPosition cursorPos = cursor
777 .findRegionForColumn(hiddenColumn);
778 int index = cursorPos.getRegionIndex();
779 int hiddenBeforeCol = cursorPos.getHiddenSoFar();
781 // just subtract hidden cols count - this works fine if column is
783 result = hiddenColumn - hiddenBeforeCol;
785 // now check in case column is hidden - it will be in the returned
787 if (index < hiddenColumns.size())
789 int[] region = hiddenColumns.get(index);
790 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
792 // actually col is hidden, return region[0]-1
793 // unless region[0]==0 in which case return 0
800 result = region[0] - 1 - hiddenBeforeCol;
806 return result; // return the shifted position after removing hidden
810 LOCK.readLock().unlock();
815 * Find the visible column which is a given visible number of columns to the
816 * left of another visible column. i.e. for a startColumn x, the column which
817 * is distance 1 away will be column x-1.
819 * @param visibleDistance
820 * the number of visible columns to offset by
822 * the column to start from
823 * @return the position of the column in the visible alignment
825 public int subtractVisibleColumns(int visibleDistance, int startColumn)
829 LOCK.readLock().lock();
830 int distance = visibleDistance;
832 // in case startColumn is in a hidden region, move it to the left
833 int start = visibleToAbsoluteColumn(absoluteToVisibleColumn(startColumn));
835 Iterator<int[]> it = new ReverseRegionsIterator(0, start,
838 while (it.hasNext() && (distance > 0))
840 int[] region = it.next();
842 if (start > region[1])
844 // subtract the gap to right of region from distance
845 if (start - region[1] <= distance)
847 distance -= start - region[1];
848 start = region[0] - 1;
852 start = start - distance;
857 return start - distance;
861 LOCK.readLock().unlock();
866 * This method returns the rightmost limit of a region of an alignment with
867 * hidden columns. In otherwords, the next hidden column.
870 * the absolute (visible) alignmentPosition to find the next hidden
872 * @return the index of the next hidden column, or alPos if there is no next
875 public int getHiddenBoundaryRight(int alPos)
879 LOCK.readLock().lock();
880 if (hiddenColumns != null)
882 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
883 if (index < hiddenColumns.size())
885 int[] region = hiddenColumns.get(index);
886 if (alPos < region[0])
890 else if ((alPos <= region[1])
891 && (index + 1 < hiddenColumns.size()))
893 // alPos is within a hidden region, return the next one
895 region = hiddenColumns.get(index + 1);
903 LOCK.readLock().unlock();
908 * This method returns the leftmost limit of a region of an alignment with
909 * hidden columns. In otherwords, the previous hidden column.
912 * the absolute (visible) alignmentPosition to find the previous
915 public int getHiddenBoundaryLeft(int alPos)
919 LOCK.readLock().lock();
921 if (hiddenColumns != null)
923 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
927 int[] region = hiddenColumns.get(index - 1);
934 LOCK.readLock().unlock();
940 * Answers if a column in the alignment is visible
943 * absolute position of column in the alignment
944 * @return true if column is visible
946 public boolean isVisible(int column)
950 LOCK.readLock().lock();
952 int regionindex = cursor.findRegionForColumn(column).getRegionIndex();
953 if (regionindex > -1 && regionindex < hiddenColumns.size())
955 int[] region = hiddenColumns.get(regionindex);
956 // already know that column <= region[1] as cursor returns containing
957 // region or region to right
958 if (column >= region[0])
967 LOCK.readLock().unlock();
972 * Get the visible sections of a set of sequences
975 * sequence position to start from
977 * sequence position to end at
979 * an array of sequences
980 * @return an array of strings encoding the visible parts of each sequence
982 public String[] getVisibleSequenceStrings(int start, int end,
987 LOCK.readLock().lock();
988 int iSize = seqs.length;
989 String[] selections = new String[iSize];
990 if (hiddenColumns != null && hiddenColumns.size() > 0)
992 for (int i = 0; i < iSize; i++)
994 StringBuffer visibleSeq = new StringBuffer();
996 Iterator<int[]> blocks = new VisibleContigsIterator(start,
997 end + 1, hiddenColumns);
999 while (blocks.hasNext())
1001 int[] block = blocks.next();
1002 if (blocks.hasNext())
1005 .append(seqs[i].getSequence(block[0], block[1] + 1));
1010 .append(seqs[i].getSequence(block[0], block[1]));
1014 selections[i] = visibleSeq.toString();
1019 for (int i = 0; i < iSize; i++)
1021 selections[i] = seqs[i].getSequenceAsString(start, end);
1028 LOCK.readLock().unlock();
1033 * Locate the first position visible for this sequence. If seq isn't visible
1034 * then return the position of the left side of the hidden boundary region.
1037 * sequence to find position for
1038 * @return visible start position
1040 public int locateVisibleStartOfSequence(SequenceI seq)
1044 LOCK.readLock().lock();
1047 if (hiddenColumns == null || hiddenColumns.size() == 0)
1049 return seq.findIndex(seq.getStart()) - 1;
1052 // Simply walk along the sequence whilst watching for hidden column
1054 Iterator<int[]> regions = hiddenColumns.iterator();
1055 int hideStart = seq.getLength();
1059 boolean foundStart = false;
1061 // step through the non-gapped positions of the sequence
1062 for (int i = seq.getStart(); i <= seq.getEnd() && (!foundStart); i++)
1064 // get alignment position of this residue in the sequence
1065 int p = seq.findIndex(i) - 1;
1067 // update hidden region start/end
1068 while (hideEnd < p && regions.hasNext())
1070 int[] region = regions.next();
1072 visNext += region[0] - visPrev;
1073 hideStart = region[0];
1074 hideEnd = region[1];
1078 hideStart = seq.getLength();
1080 // update visible boundary for sequence
1090 return absoluteToVisibleColumn(start);
1092 // otherwise, sequence was completely hidden
1096 LOCK.readLock().unlock();
1101 * delete any columns in alignmentAnnotation that are hidden (including
1102 * sequence associated annotation).
1104 * @param alignmentAnnotation
1106 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
1108 if (alignmentAnnotation != null
1109 && alignmentAnnotation.annotations != null)
1111 makeVisibleAnnotation(0, alignmentAnnotation.annotations.length,
1112 alignmentAnnotation);
1117 * delete any columns in alignmentAnnotation that are hidden (including
1118 * sequence associated annotation).
1121 * remove any annotation to the right of this column
1123 * remove any annotation to the left of this column
1124 * @param alignmentAnnotation
1125 * the annotation to operate on
1127 public void makeVisibleAnnotation(int start, int end,
1128 AlignmentAnnotation alignmentAnnotation)
1132 LOCK.readLock().lock();
1134 int startFrom = start;
1137 if (alignmentAnnotation != null
1138 && alignmentAnnotation.annotations != null)
1140 if (hiddenColumns != null && hiddenColumns.size() > 0)
1142 removeHiddenAnnotation(startFrom, endAt, alignmentAnnotation);
1146 alignmentAnnotation.restrict(startFrom, endAt);
1151 LOCK.readLock().unlock();
1155 private void removeHiddenAnnotation(int start, int end,
1156 AlignmentAnnotation alignmentAnnotation)
1158 // mangle the alignmentAnnotation annotation array
1159 ArrayList<Annotation[]> annels = new ArrayList<>();
1160 Annotation[] els = null;
1164 Iterator<int[]> blocks = new VisibleContigsIterator(start, end + 1,
1168 int annotationLength;
1169 while (blocks.hasNext())
1171 int[] block = blocks.next();
1172 annotationLength = block[1] - block[0] + 1;
1174 if (blocks.hasNext())
1176 // copy just the visible segment of the annotation row
1177 copylength = annotationLength;
1181 if (annotationLength + block[0] <= alignmentAnnotation.annotations.length)
1183 // copy just the visible segment of the annotation row
1184 copylength = annotationLength;
1188 // copy to the end of the annotation row
1189 copylength = alignmentAnnotation.annotations.length - block[0];
1193 els = new Annotation[annotationLength];
1195 System.arraycopy(alignmentAnnotation.annotations, block[0], els, 0,
1197 w += annotationLength;
1202 alignmentAnnotation.annotations = new Annotation[w];
1205 for (Annotation[] chnk : annels)
1207 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1216 * @return true if there are columns hidden
1218 public boolean hasHiddenColumns()
1222 LOCK.readLock().lock();
1224 // we don't use getSize()>0 here because it has to iterate over
1225 // the full hiddenColumns collection and so will be much slower
1226 return hiddenColumns != null && hiddenColumns.size() > 0;
1229 LOCK.readLock().unlock();
1235 * @return true if there is more than one hidden column region
1237 public boolean hasMultiHiddenColumnRegions()
1241 LOCK.readLock().lock();
1242 return hiddenColumns != null && hiddenColumns.size() > 1;
1245 LOCK.readLock().unlock();
1251 * Returns a hashCode built from hidden column ranges
1254 public int hashCode()
1258 LOCK.readLock().lock();
1260 Iterator<int[]> it = hiddenColumns.iterator();
1261 while (it.hasNext())
1263 int[] hidden = it.next();
1264 hashCode = HASH_MULTIPLIER * hashCode + hidden[0];
1265 hashCode = HASH_MULTIPLIER * hashCode + hidden[1];
1270 LOCK.readLock().unlock();
1275 * Hide columns corresponding to the marked bits
1278 * - columns map to bits starting from zero
1280 public void hideMarkedBits(BitSet inserts)
1284 LOCK.writeLock().lock();
1285 for (int firstSet = inserts
1286 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1287 .nextSetBit(lastSet))
1289 lastSet = inserts.nextClearBit(firstSet);
1290 hideColumns(firstSet, lastSet - 1);
1292 cursor.resetCursor(hiddenColumns);
1296 LOCK.writeLock().unlock();
1303 * BitSet where hidden columns will be marked
1305 public void markHiddenRegions(BitSet inserts)
1309 LOCK.readLock().lock();
1310 if (hiddenColumns == null)
1314 Iterator<int[]> it = hiddenColumns.iterator();
1315 while (it.hasNext())
1317 int[] range = it.next();
1318 inserts.set(range[0], range[1] + 1);
1322 LOCK.readLock().unlock();
1327 * Calculate the visible start and end index of an alignment.
1330 * full alignment width
1331 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1333 public int[] getVisibleStartAndEndIndex(int width)
1337 LOCK.readLock().lock();
1338 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1339 int startPos = alignmentStartEnd[0];
1340 int endPos = alignmentStartEnd[1];
1342 int[] lowestRange = new int[] { -1, -1 };
1343 int[] higestRange = new int[] { -1, -1 };
1345 if (hiddenColumns == null)
1347 return new int[] { startPos, endPos };
1350 Iterator<int[]> it = hiddenColumns.iterator();
1351 while (it.hasNext())
1353 int[] range = it.next();
1354 lowestRange = (range[0] <= startPos) ? range : lowestRange;
1355 higestRange = (range[1] >= endPos) ? range : higestRange;
1358 if (lowestRange[0] == -1) // includes (lowestRange[1] == -1)
1360 startPos = alignmentStartEnd[0];
1364 startPos = lowestRange[1] + 1;
1367 if (higestRange[0] == -1) // includes (higestRange[1] == -1)
1369 endPos = alignmentStartEnd[1];
1373 endPos = higestRange[0] - 1;
1375 return new int[] { startPos, endPos };
1378 LOCK.readLock().unlock();
1383 * Finds the hidden region (if any) which starts or ends at res
1386 * visible residue position, unadjusted for hidden columns
1387 * @return region as [start,end] or null if no matching region is found
1389 public int[] getRegionWithEdgeAtRes(int res)
1393 LOCK.readLock().lock();
1394 int adjres = visibleToAbsoluteColumn(res);
1396 int[] reveal = null;
1398 if (hiddenColumns != null)
1400 // look for a region ending just before adjres
1401 int regionindex = cursor.findRegionForColumn(adjres - 1)
1403 if (regionindex < hiddenColumns.size()
1404 && hiddenColumns.get(regionindex)[1] == adjres - 1)
1406 reveal = hiddenColumns.get(regionindex);
1408 // check if the region ends just after adjres
1409 else if (regionindex < hiddenColumns.size()
1410 && hiddenColumns.get(regionindex)[0] == adjres + 1)
1412 reveal = hiddenColumns.get(regionindex);
1419 LOCK.readLock().unlock();
1424 * Return an iterator over the hidden regions
1426 public Iterator<int[]> iterator()
1430 LOCK.readLock().lock();
1431 return new HiddenColsIterator(hiddenColumns);
1434 LOCK.readLock().unlock();
1439 * Return a bounded iterator over the hidden regions
1442 * position to start from (inclusive, absolute column position)
1444 * position to end at (inclusive, absolute column position)
1447 public Iterator<int[]> getBoundedIterator(int start, int end)
1451 LOCK.readLock().lock();
1452 return new HiddenColsIterator(start, end, hiddenColumns);
1455 LOCK.readLock().unlock();
1460 * Return a bounded iterator over the *visible* start positions of hidden
1464 * position to start from (inclusive, visible column position)
1466 * position to end at (inclusive, visible column position)
1468 public Iterator<Integer> getBoundedStartIterator(int start, int end)
1472 LOCK.readLock().lock();
1474 // get absolute position of column in alignment
1475 int absoluteStart = visibleToAbsoluteColumn(start);
1477 // Get cursor position and supply it to the iterator:
1478 // Since we want visible region start, we look for a cursor for the
1479 // (absoluteStart-1), then if absoluteStart is the start of a visible
1480 // region we'll get the cursor pointing to the region before, which is
1482 HiddenCursorPosition pos = cursor
1483 .findRegionForColumn(absoluteStart - 1);
1485 return new BoundedStartRegionIterator(pos, start, end,
1489 LOCK.readLock().unlock();
1494 * Return an iterator over visible *columns* (not regions) between the given
1495 * start and end boundaries
1498 * first column (inclusive)
1500 * last column (inclusive)
1502 public Iterator<Integer> getVisibleColsIterator(int start, int end)
1506 LOCK.readLock().lock();
1507 return new VisibleColsIterator(start, end, hiddenColumns);
1510 LOCK.readLock().unlock();
1515 * return an iterator over visible segments between the given start and end
1519 * first column, inclusive from 0
1521 * last column - not inclusive
1522 * @param useVisibleCoords
1523 * if true, start and end are visible column positions, not absolute
1526 public Iterator<int[]> getVisContigsIterator(int start, int end,
1527 boolean useVisibleCoords)
1529 int adjstart = start;
1531 if (useVisibleCoords)
1533 adjstart = visibleToAbsoluteColumn(start);
1534 adjend = visibleToAbsoluteColumn(end);
1539 LOCK.readLock().lock();
1540 return new VisibleContigsIterator(adjstart, adjend, hiddenColumns);
1543 LOCK.readLock().unlock();