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)
99 LOCK.writeLock().lock();
103 if (copy.hiddenColumns != null)
105 hiddenColumns = new ArrayList<>();
106 Iterator<int[]> it = copy.iterator();
109 int[] region = it.next();
110 hiddenColumns.add(region);
111 numColumns += region[1] - region[0] + 1;
113 cursor.resetCursor(hiddenColumns);
118 LOCK.writeLock().unlock();
123 * Copy constructor within bounds and with offset. Copies hidden column
124 * regions fully contained between start and end, and offsets positions by
125 * subtracting offset.
128 * HiddenColumns instance to copy from
130 * lower bound to copy from
132 * upper bound to copy to
134 * offset to subtract from each region boundary position
137 public HiddenColumns(HiddenColumns copy, int start, int end, int offset)
141 LOCK.writeLock().lock();
144 hiddenColumns = new ArrayList<>();
146 Iterator<int[]> it = copy.getBoundedIterator(start, end);
149 int[] region = it.next();
150 // still need to check boundaries because iterator returns
151 // all overlapping regions and we need contained regions
152 if (region[0] >= start && region[1] <= end)
156 { region[0] - offset, region[1] - offset });
157 numColumns += region[1] - region[0] + 1;
160 cursor.resetCursor(hiddenColumns);
164 LOCK.writeLock().unlock();
169 * Adds the specified column range to the hidden columns collection
172 * start of range to add (absolute position in alignment)
174 * end of range to add (absolute position in alignment)
176 public void hideColumns(int start, int end)
178 boolean wasAlreadyLocked = false;
181 // check if the write lock was already locked by this thread,
182 // as this method can be called internally in loops within HiddenColumns
183 if (!LOCK.isWriteLockedByCurrentThread())
185 LOCK.writeLock().lock();
189 wasAlreadyLocked = true;
193 int prevHiddenCount = 0;
195 if (hiddenColumns == null)
197 hiddenColumns = new ArrayList<>();
201 // set up cursor reset values
202 HiddenCursorPosition cursorPos = cursor.findRegionForColumn(start);
203 regionindex = cursorPos.getRegionIndex();
207 // get previous index and hidden count for updating the cursor later
208 previndex = regionindex - 1;
209 int[] prevRegion = hiddenColumns.get(previndex);
210 prevHiddenCount = cursorPos.getHiddenSoFar()
211 - (prevRegion[1] - prevRegion[0] + 1);
216 * new range follows everything else; check first to avoid looping over whole hiddenColumns collection
218 if (hiddenColumns.isEmpty()
219 || start > hiddenColumns.get(hiddenColumns.size() - 1)[1])
221 hiddenColumns.add(new int[] { start, end });
226 * traverse existing hidden ranges and insert / amend / append as
229 boolean added = false;
232 added = insertRangeAtRegion(regionindex - 1, start, end);
234 if (!added && regionindex < hiddenColumns.size())
236 insertRangeAtRegion(regionindex, start, end);
240 // reset the cursor to just before our insertion point: this saves
241 // a lot of reprocessing in large alignments
242 cursor.resetCursor(hiddenColumns, previndex, prevHiddenCount);
244 // reset the number of columns so they will be recounted
249 if (!wasAlreadyLocked)
251 LOCK.writeLock().unlock();
257 * Insert [start, range] at the region at index i in hiddenColumns, if
263 * start of range to insert
265 * end of range to insert
266 * @return true if range was successfully inserted
268 private boolean insertRangeAtRegion(int i, int start, int end)
270 boolean added = false;
272 int[] region = hiddenColumns.get(i);
273 if (end < region[0] - 1)
276 * insert discontiguous preceding range
278 hiddenColumns.add(i, new int[] { start, end });
281 else if (end <= region[1])
284 * new range overlaps existing, or is contiguous preceding it - adjust
287 region[0] = Math.min(region[0], start);
290 else if (start <= region[1] + 1)
293 * new range overlaps existing, or is contiguous following it - adjust
294 * start and end columns
296 region[0] = Math.min(region[0], start);
297 region[1] = Math.max(region[1], end);
300 * also update or remove any subsequent ranges
301 * that are overlapped
303 while (i < hiddenColumns.size() - 1)
305 int[] nextRegion = hiddenColumns.get(i + 1);
306 if (nextRegion[0] > end + 1)
309 * gap to next hidden range - no more to update
313 region[1] = Math.max(nextRegion[1], end);
315 // in theory this is faster than hiddenColumns.remove(i+1)
316 // benchmarking results a bit ambivalent
317 hiddenColumns.subList(i + 1, i + 2).clear();
325 * mark the columns corresponding to gap characters as hidden in the column
330 public void hideInsertionsFor(SequenceI sr)
334 LOCK.writeLock().lock();
335 List<int[]> inserts = sr.getInsertions();
336 for (int[] r : inserts)
338 hideColumns(r[0], r[1]);
340 cursor.resetCursor(hiddenColumns);
344 LOCK.writeLock().unlock();
349 * Unhides, and adds to the selection list, all hidden columns
351 public void revealAllHiddenColumns(ColumnSelection sel)
355 LOCK.writeLock().lock();
356 if (hiddenColumns != null)
358 Iterator<int[]> it = hiddenColumns.iterator();
361 int[] region = it.next();
362 for (int j = region[0]; j < region[1] + 1; j++)
367 hiddenColumns = null;
368 cursor.resetCursor(hiddenColumns);
373 LOCK.writeLock().unlock();
378 * Reveals, and marks as selected, the hidden column range with the given
382 * the start column to look for
384 * the column selection to add the hidden column range to
386 public void revealHiddenColumns(int start, ColumnSelection sel)
390 LOCK.writeLock().lock();
392 if (hiddenColumns != null)
394 int regionIndex = cursor.findRegionForColumn(start)
397 if (regionIndex != -1 && regionIndex != hiddenColumns.size())
399 // regionIndex is the region which either contains start
400 // or lies to the right of start
401 int[] region = hiddenColumns.get(regionIndex);
402 if (start == region[0])
404 for (int j = region[0]; j < region[1] + 1; j++)
408 int colsToRemove = region[1] - region[0] + 1;
409 hiddenColumns.remove(regionIndex);
411 if (hiddenColumns.isEmpty())
413 hiddenColumns = null;
418 numColumns -= colsToRemove;
420 cursor.updateForDeletedRegion(hiddenColumns);
426 LOCK.writeLock().unlock();
431 * Add gaps into the sequences aligned to profileseq under the given
435 * sequence in al which sequences are aligned to
437 * alignment to have gaps inserted into it
439 * alignment view where sequence corresponding to profileseq is first
441 * @return new HiddenColumns for new alignment view, with insertions into
442 * profileseq marked as hidden.
444 public static HiddenColumns propagateInsertions(SequenceI profileseq,
445 AlignmentI al, AlignmentView input)
449 char gc = al.getGapCharacter();
450 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
451 HiddenColumns nview = (HiddenColumns) alandhidden[1];
452 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
453 nview.propagateInsertions(profileseq, al, origseq);
460 * sequence in al which corresponds to origseq
462 * alignment which is to have gaps inserted into it
464 * sequence corresponding to profileseq which defines gap map for
467 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
472 LOCK.writeLock().lock();
474 char gc = al.getGapCharacter();
476 // take the set of hidden columns, and the set of gaps in origseq,
477 // and remove all the hidden gaps from hiddenColumns
479 // first get the gaps as a Bitset
480 BitSet gaps = origseq.gapBitset();
482 // now calculate hidden ^ not(gap)
483 BitSet hidden = new BitSet();
484 markHiddenRegions(hidden);
486 hiddenColumns = null;
487 this.hideColumns(hidden);
489 // for each sequence in the alignment, except the profile sequence,
490 // insert gaps corresponding to each hidden region but where each hidden
491 // column region is shifted backwards by the number of preceding visible
492 // gaps update hidden columns at the same time
493 Iterator<int[]> regions = hiddenColumns.iterator();
494 List<int[]> newhidden = new ArrayList<>();
496 int numGapsBefore = 0;
498 while (regions.hasNext())
500 // get region coordinates accounting for gaps
501 // we can rely on gaps not being *in* hidden regions because we already
503 int[] region = regions.next();
504 while (gapPosition < region[0])
507 if (gaps.get(gapPosition))
513 int left = region[0] - numGapsBefore;
514 int right = region[1] - numGapsBefore;
515 newhidden.add(new int[] { left, right });
517 // make a string with number of gaps = length of hidden region
518 StringBuffer sb = new StringBuffer();
519 for (int s = 0; s < right - left + 1; s++)
523 padGaps(sb, left, profileseq, al);
526 hiddenColumns = newhidden;
527 cursor.resetCursor(hiddenColumns);
531 LOCK.writeLock().unlock();
536 * Pad gaps in all sequences in alignment except profileseq
539 * gap string to insert
541 * position to insert at
543 * sequence not to pad
545 * alignment to pad sequences in
547 private void padGaps(StringBuffer sb, int pos, SequenceI profileseq,
550 // loop over the sequences and pad with gaps where required
551 for (int s = 0, ns = al.getHeight(); s < ns; s++)
553 SequenceI sqobj = al.getSequenceAt(s);
554 if (sqobj != profileseq)
556 String sq = al.getSequenceAt(s).getSequenceAsString();
557 if (sq.length() <= pos)
560 int diff = pos - sq.length() - 1;
565 while ((diff = pos - sq.length() - 1) > 0)
567 if (diff >= sb.length())
573 char[] buf = new char[diff];
574 sb.getChars(0, diff, buf, 0);
575 sq += buf.toString();
583 al.getSequenceAt(s).setSequence(
584 sq.substring(0, pos) + sb.toString() + sq.substring(pos));
591 * Methods which only need read access to the hidden columns collection.
592 * These methods should use a readLock to prevent other threads changing
593 * the hidden columns collection while it is in use.
597 * Output regions data as a string. String is in the format:
598 * reg0[0]<between>reg0[1]<delimiter>reg1[0]<between>reg1[1] ... regn[1]
601 * string to delimit regions
602 * @param betweenstring
603 * to put between start and end region values
604 * @return regions formatted according to delimiter and between strings
606 public String regionsToString(String delimiter, String between)
610 LOCK.readLock().lock();
611 StringBuilder regionBuilder = new StringBuilder();
612 if (hiddenColumns != null)
614 Iterator<int[]> it = hiddenColumns.iterator();
617 int[] range = it.next();
618 regionBuilder.append(delimiter).append(range[0]).append(between)
622 regionBuilder.deleteCharAt(0);
626 return regionBuilder.toString();
629 LOCK.readLock().unlock();
634 * Find the number of hidden columns
636 * @return number of hidden columns
642 LOCK.readLock().lock();
644 if (numColumns == 0 && hiddenColumns != null)
646 // numColumns is out of date, so recalculate
649 Iterator<int[]> it = hiddenColumns.iterator();
652 int[] range = it.next();
653 size += range[1] - range[0] + 1;
662 LOCK.readLock().unlock();
667 * Get the number of distinct hidden regions
669 * @return number of regions
671 public int getNumberOfRegions()
675 LOCK.readLock().lock();
677 if (hasHiddenColumns())
679 num = hiddenColumns.size();
684 LOCK.readLock().unlock();
689 public boolean equals(Object obj)
693 LOCK.readLock().lock();
695 if (!(obj instanceof HiddenColumns))
699 HiddenColumns that = (HiddenColumns) obj;
702 * check hidden columns are either both null, or match
704 if (this.hiddenColumns == null)
706 return (that.hiddenColumns == null);
708 if (that.hiddenColumns == null
709 || that.hiddenColumns.size() != this.hiddenColumns.size())
714 Iterator<int[]> it = hiddenColumns.iterator();
715 Iterator<int[]> thatit = that.iterator();
718 int[] thisRange = it.next();
719 int[] thatRange = thatit.next();
720 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
728 LOCK.readLock().unlock();
733 * Return absolute column index for a visible column index
736 * int column index in alignment view (count from zero)
737 * @return alignment column index for column
739 public int visibleToAbsoluteColumn(int column)
743 LOCK.readLock().lock();
746 if (hiddenColumns != null)
748 result += cursor.findRegionForVisColumn(column).getHiddenSoFar();
754 LOCK.readLock().unlock();
759 * Use this method to find out where a column will appear in the visible
760 * alignment when hidden columns exist. If the column is not visible, then the
761 * index of the next visible column on the left will be returned (or 0 if
762 * there is no visible column on the left)
764 * @param hiddenColumn
765 * the column index in the full alignment including hidden columns
766 * @return the position of the column in the visible alignment
768 public int absoluteToVisibleColumn(int hiddenColumn)
772 LOCK.readLock().lock();
773 int result = hiddenColumn;
775 if (hiddenColumns != null)
777 HiddenCursorPosition cursorPos = cursor
778 .findRegionForColumn(hiddenColumn);
779 int index = cursorPos.getRegionIndex();
780 int hiddenBeforeCol = cursorPos.getHiddenSoFar();
782 // just subtract hidden cols count - this works fine if column is
784 result = hiddenColumn - hiddenBeforeCol;
786 // now check in case column is hidden - it will be in the returned
788 if (index < hiddenColumns.size())
790 int[] region = hiddenColumns.get(index);
791 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
793 // actually col is hidden, return region[0]-1
794 // unless region[0]==0 in which case return 0
801 result = region[0] - 1 - hiddenBeforeCol;
807 return result; // return the shifted position after removing hidden
811 LOCK.readLock().unlock();
816 * Find the visible column which is a given visible number of columns to the
817 * left of another visible column. i.e. for a startColumn x, the column which
818 * is distance 1 away will be column x-1.
820 * @param visibleDistance
821 * the number of visible columns to offset by
823 * the column to start from
824 * @return the position of the column in the visible alignment
826 public int subtractVisibleColumns(int visibleDistance, int startColumn)
830 LOCK.readLock().lock();
831 int distance = visibleDistance;
833 // in case startColumn is in a hidden region, move it to the left
834 int start = visibleToAbsoluteColumn(absoluteToVisibleColumn(startColumn));
836 Iterator<int[]> it = new ReverseRegionsIterator(0, start,
839 while (it.hasNext() && (distance > 0))
841 int[] region = it.next();
843 if (start > region[1])
845 // subtract the gap to right of region from distance
846 if (start - region[1] <= distance)
848 distance -= start - region[1];
849 start = region[0] - 1;
853 start = start - distance;
858 return start - distance;
862 LOCK.readLock().unlock();
867 * This method returns the rightmost limit of a region of an alignment with
868 * hidden columns. In otherwords, the next hidden column.
871 * the absolute (visible) alignmentPosition to find the next hidden
873 * @return the index of the next hidden column, or alPos if there is no next
876 public int getHiddenBoundaryRight(int alPos)
880 LOCK.readLock().lock();
881 if (hiddenColumns != null)
883 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
884 if (index < hiddenColumns.size())
886 int[] region = hiddenColumns.get(index);
887 if (alPos < region[0])
891 else if ((alPos <= region[1])
892 && (index + 1 < hiddenColumns.size()))
894 // alPos is within a hidden region, return the next one
896 region = hiddenColumns.get(index + 1);
904 LOCK.readLock().unlock();
909 * This method returns the leftmost limit of a region of an alignment with
910 * hidden columns. In otherwords, the previous hidden column.
913 * the absolute (visible) alignmentPosition to find the previous
916 public int getHiddenBoundaryLeft(int alPos)
920 LOCK.readLock().lock();
922 if (hiddenColumns != null)
924 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
928 int[] region = hiddenColumns.get(index - 1);
935 LOCK.readLock().unlock();
941 * Answers if a column in the alignment is visible
944 * absolute position of column in the alignment
945 * @return true if column is visible
947 public boolean isVisible(int column)
951 LOCK.readLock().lock();
953 int regionindex = cursor.findRegionForColumn(column).getRegionIndex();
954 if (regionindex > -1 && regionindex < hiddenColumns.size())
956 int[] region = hiddenColumns.get(regionindex);
957 // already know that column <= region[1] as cursor returns containing
958 // region or region to right
959 if (column >= region[0])
968 LOCK.readLock().unlock();
973 * Get the visible sections of a set of sequences
976 * sequence position to start from
978 * sequence position to end at
980 * an array of sequences
981 * @return an array of strings encoding the visible parts of each sequence
983 public String[] getVisibleSequenceStrings(int start, int end,
988 LOCK.readLock().lock();
989 int iSize = seqs.length;
990 String[] selections = new String[iSize];
991 if (hiddenColumns != null && hiddenColumns.size() > 0)
993 for (int i = 0; i < iSize; i++)
995 StringBuffer visibleSeq = new StringBuffer();
997 Iterator<int[]> blocks = new VisibleContigsIterator(start,
998 end + 1, hiddenColumns);
1000 while (blocks.hasNext())
1002 int[] block = blocks.next();
1003 if (blocks.hasNext())
1006 .append(seqs[i].getSequence(block[0], block[1] + 1));
1011 .append(seqs[i].getSequence(block[0], block[1]));
1015 selections[i] = visibleSeq.toString();
1020 for (int i = 0; i < iSize; i++)
1022 selections[i] = seqs[i].getSequenceAsString(start, end);
1029 LOCK.readLock().unlock();
1034 * Locate the first position visible for this sequence. If seq isn't visible
1035 * then return the position of the left side of the hidden boundary region.
1038 * sequence to find position for
1039 * @return visible start position
1041 public int locateVisibleStartOfSequence(SequenceI seq)
1045 LOCK.readLock().lock();
1048 if (hiddenColumns == null || hiddenColumns.size() == 0)
1050 return seq.findIndex(seq.getStart()) - 1;
1053 // Simply walk along the sequence whilst watching for hidden column
1055 Iterator<int[]> regions = hiddenColumns.iterator();
1056 int hideStart = seq.getLength();
1060 boolean foundStart = false;
1062 // step through the non-gapped positions of the sequence
1063 for (int i = seq.getStart(); i <= seq.getEnd() && (!foundStart); i++)
1065 // get alignment position of this residue in the sequence
1066 int p = seq.findIndex(i) - 1;
1068 // update hidden region start/end
1069 while (hideEnd < p && regions.hasNext())
1071 int[] region = regions.next();
1073 visNext += region[0] - visPrev;
1074 hideStart = region[0];
1075 hideEnd = region[1];
1079 hideStart = seq.getLength();
1081 // update visible boundary for sequence
1091 return absoluteToVisibleColumn(start);
1093 // otherwise, sequence was completely hidden
1097 LOCK.readLock().unlock();
1102 * delete any columns in alignmentAnnotation that are hidden (including
1103 * sequence associated annotation).
1105 * @param alignmentAnnotation
1107 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
1109 if (alignmentAnnotation != null
1110 && alignmentAnnotation.annotations != null)
1112 makeVisibleAnnotation(0, alignmentAnnotation.annotations.length,
1113 alignmentAnnotation);
1118 * delete any columns in alignmentAnnotation that are hidden (including
1119 * sequence associated annotation).
1122 * remove any annotation to the right of this column
1124 * remove any annotation to the left of this column
1125 * @param alignmentAnnotation
1126 * the annotation to operate on
1128 public void makeVisibleAnnotation(int start, int end,
1129 AlignmentAnnotation alignmentAnnotation)
1133 LOCK.readLock().lock();
1135 int startFrom = start;
1138 if (alignmentAnnotation != null
1139 && alignmentAnnotation.annotations != null)
1141 if (hiddenColumns != null && hiddenColumns.size() > 0)
1143 removeHiddenAnnotation(startFrom, endAt, alignmentAnnotation);
1147 alignmentAnnotation.restrict(startFrom, endAt);
1152 LOCK.readLock().unlock();
1156 private void removeHiddenAnnotation(int start, int end,
1157 AlignmentAnnotation alignmentAnnotation)
1159 // mangle the alignmentAnnotation annotation array
1160 ArrayList<Annotation[]> annels = new ArrayList<>();
1161 Annotation[] els = null;
1165 Iterator<int[]> blocks = new VisibleContigsIterator(start, end + 1,
1169 int annotationLength;
1170 while (blocks.hasNext())
1172 int[] block = blocks.next();
1173 annotationLength = block[1] - block[0] + 1;
1175 if (blocks.hasNext())
1177 // copy just the visible segment of the annotation row
1178 copylength = annotationLength;
1182 if (annotationLength + block[0] <= alignmentAnnotation.annotations.length)
1184 // copy just the visible segment of the annotation row
1185 copylength = annotationLength;
1189 // copy to the end of the annotation row
1190 copylength = alignmentAnnotation.annotations.length - block[0];
1194 els = new Annotation[annotationLength];
1196 System.arraycopy(alignmentAnnotation.annotations, block[0], els, 0,
1198 w += annotationLength;
1203 alignmentAnnotation.annotations = new Annotation[w];
1206 for (Annotation[] chnk : annels)
1208 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1217 * @return true if there are columns hidden
1219 public boolean hasHiddenColumns()
1223 LOCK.readLock().lock();
1225 // we don't use getSize()>0 here because it has to iterate over
1226 // the full hiddenColumns collection and so will be much slower
1227 return hiddenColumns != null && hiddenColumns.size() > 0;
1230 LOCK.readLock().unlock();
1236 * @return true if there is more than one hidden column region
1238 public boolean hasMultiHiddenColumnRegions()
1242 LOCK.readLock().lock();
1243 return hiddenColumns != null && hiddenColumns.size() > 1;
1246 LOCK.readLock().unlock();
1252 * Returns a hashCode built from hidden column ranges
1255 public int hashCode()
1259 LOCK.readLock().lock();
1261 Iterator<int[]> it = hiddenColumns.iterator();
1262 while (it.hasNext())
1264 int[] hidden = it.next();
1265 hashCode = HASH_MULTIPLIER * hashCode + hidden[0];
1266 hashCode = HASH_MULTIPLIER * hashCode + hidden[1];
1271 LOCK.readLock().unlock();
1276 * Hide columns corresponding to the marked bits
1279 * - columns map to bits starting from zero
1281 public void hideColumns(BitSet inserts)
1285 LOCK.writeLock().lock();
1286 for (int firstSet = inserts
1287 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1288 .nextSetBit(lastSet))
1290 lastSet = inserts.nextClearBit(firstSet);
1291 hideColumns(firstSet, lastSet - 1);
1293 cursor.resetCursor(hiddenColumns);
1297 LOCK.writeLock().unlock();
1304 * BitSet where hidden columns will be marked
1306 public void markHiddenRegions(BitSet inserts)
1310 LOCK.readLock().lock();
1311 if (hiddenColumns == null)
1315 Iterator<int[]> it = hiddenColumns.iterator();
1316 while (it.hasNext())
1318 int[] range = it.next();
1319 inserts.set(range[0], range[1] + 1);
1323 LOCK.readLock().unlock();
1328 * Calculate the visible start and end index of an alignment.
1331 * full alignment width
1332 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1334 public int[] getVisibleStartAndEndIndex(int width)
1338 LOCK.readLock().lock();
1339 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1340 int startPos = alignmentStartEnd[0];
1341 int endPos = alignmentStartEnd[1];
1343 int[] lowestRange = new int[] { -1, -1 };
1344 int[] higestRange = new int[] { -1, -1 };
1346 if (hiddenColumns == null)
1348 return new int[] { startPos, endPos };
1351 Iterator<int[]> it = hiddenColumns.iterator();
1352 while (it.hasNext())
1354 int[] range = it.next();
1355 lowestRange = (range[0] <= startPos) ? range : lowestRange;
1356 higestRange = (range[1] >= endPos) ? range : higestRange;
1359 if (lowestRange[0] == -1) // includes (lowestRange[1] == -1)
1361 startPos = alignmentStartEnd[0];
1365 startPos = lowestRange[1] + 1;
1368 if (higestRange[0] == -1) // includes (higestRange[1] == -1)
1370 endPos = alignmentStartEnd[1];
1374 endPos = higestRange[0] - 1;
1376 return new int[] { startPos, endPos };
1379 LOCK.readLock().unlock();
1384 * Finds the hidden region (if any) which starts or ends at res
1387 * visible residue position, unadjusted for hidden columns
1388 * @return region as [start,end] or null if no matching region is found. If
1389 * res is adjacent to two regions, returns the left region.
1391 public int[] getRegionWithEdgeAtRes(int res)
1395 LOCK.readLock().lock();
1396 int adjres = visibleToAbsoluteColumn(res);
1398 int[] reveal = null;
1400 if (hiddenColumns != null)
1402 // look for a region ending just before adjres
1403 int regionindex = cursor.findRegionForColumn(adjres - 1)
1405 if (regionindex < hiddenColumns.size()
1406 && hiddenColumns.get(regionindex)[1] == adjres - 1)
1408 reveal = hiddenColumns.get(regionindex);
1410 // check if the region ends just after adjres
1411 else if (regionindex < hiddenColumns.size()
1412 && hiddenColumns.get(regionindex)[0] == adjres + 1)
1414 reveal = hiddenColumns.get(regionindex);
1421 LOCK.readLock().unlock();
1426 * Return an iterator over the hidden regions
1428 public Iterator<int[]> iterator()
1432 LOCK.readLock().lock();
1433 return new HiddenColsIterator(hiddenColumns);
1436 LOCK.readLock().unlock();
1441 * Return a bounded iterator over the hidden regions
1444 * position to start from (inclusive, absolute column position)
1446 * position to end at (inclusive, absolute column position)
1449 public Iterator<int[]> getBoundedIterator(int start, int end)
1453 LOCK.readLock().lock();
1454 return new HiddenColsIterator(start, end, hiddenColumns);
1457 LOCK.readLock().unlock();
1462 * Return a bounded iterator over the *visible* start positions of hidden
1466 * position to start from (inclusive, visible column position)
1468 * position to end at (inclusive, visible column position)
1470 public Iterator<Integer> getBoundedStartIterator(int start, int end)
1474 LOCK.readLock().lock();
1476 // get absolute position of column in alignment
1477 int absoluteStart = visibleToAbsoluteColumn(start);
1479 // Get cursor position and supply it to the iterator:
1480 // Since we want visible region start, we look for a cursor for the
1481 // (absoluteStart-1), then if absoluteStart is the start of a visible
1482 // region we'll get the cursor pointing to the region before, which is
1484 HiddenCursorPosition pos = cursor
1485 .findRegionForColumn(absoluteStart - 1);
1487 return new BoundedStartRegionIterator(pos, start, end,
1491 LOCK.readLock().unlock();
1496 * Return an iterator over visible *columns* (not regions) between the given
1497 * start and end boundaries
1500 * first column (inclusive)
1502 * last column (inclusive)
1504 public Iterator<Integer> getVisibleColsIterator(int start, int end)
1508 LOCK.readLock().lock();
1509 return new VisibleColsIterator(start, end, hiddenColumns);
1512 LOCK.readLock().unlock();
1517 * return an iterator over visible segments between the given start and end
1521 * first column, inclusive from 0
1523 * last column - not inclusive
1524 * @param useVisibleCoords
1525 * if true, start and end are visible column positions, not absolute
1528 public Iterator<int[]> getVisContigsIterator(int start, int end,
1529 boolean useVisibleCoords)
1531 int adjstart = start;
1533 if (useVisibleCoords)
1535 adjstart = visibleToAbsoluteColumn(start);
1536 adjend = visibleToAbsoluteColumn(end);
1541 LOCK.readLock().lock();
1542 return new VisibleContigsIterator(adjstart, adjend, hiddenColumns);
1545 LOCK.readLock().unlock();