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 = new ArrayList<>();
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();
123 Iterator<int[]> it = copy.getBoundedIterator(start, end);
126 int[] region = it.next();
127 // still need to check boundaries because iterator returns
128 // all overlapping regions and we need contained regions
129 if (region[0] >= start && region[1] <= end)
133 { region[0] - offset, region[1] - offset });
134 numColumns += region[1] - region[0] + 1;
137 cursor.resetCursor(hiddenColumns);
141 LOCK.writeLock().unlock();
146 * Adds the specified column range to the hidden columns collection
149 * start of range to add (absolute position in alignment)
151 * end of range to add (absolute position in alignment)
153 public void hideColumns(int start, int end)
155 boolean wasAlreadyLocked = false;
158 // check if the write lock was already locked by this thread,
159 // as this method can be called internally in loops within HiddenColumns
160 if (!LOCK.isWriteLockedByCurrentThread())
162 LOCK.writeLock().lock();
166 wasAlreadyLocked = true;
170 int prevHiddenCount = 0;
172 if (!hiddenColumns.isEmpty())
174 // set up cursor reset values
175 HiddenCursorPosition cursorPos = cursor.findRegionForColumn(start);
176 regionindex = cursorPos.getRegionIndex();
180 // get previous index and hidden count for updating the cursor later
181 previndex = regionindex - 1;
182 int[] prevRegion = hiddenColumns.get(previndex);
183 prevHiddenCount = cursorPos.getHiddenSoFar()
184 - (prevRegion[1] - prevRegion[0] + 1);
189 * new range follows everything else; check first to avoid looping over whole hiddenColumns collection
191 if (hiddenColumns.isEmpty()
192 || start > hiddenColumns.get(hiddenColumns.size() - 1)[1])
194 hiddenColumns.add(new int[] { start, end });
199 * traverse existing hidden ranges and insert / amend / append as
202 boolean added = false;
205 added = insertRangeAtRegion(regionindex - 1, start, end);
207 if (!added && regionindex < hiddenColumns.size())
209 insertRangeAtRegion(regionindex, start, end);
213 // reset the cursor to just before our insertion point: this saves
214 // a lot of reprocessing in large alignments
215 cursor.resetCursor(hiddenColumns, previndex, prevHiddenCount);
217 // reset the number of columns so they will be recounted
222 if (!wasAlreadyLocked)
224 LOCK.writeLock().unlock();
230 * Insert [start, range] at the region at index i in hiddenColumns, if
236 * start of range to insert
238 * end of range to insert
239 * @return true if range was successfully inserted
241 private boolean insertRangeAtRegion(int i, int start, int end)
243 boolean added = false;
245 int[] region = hiddenColumns.get(i);
246 if (end < region[0] - 1)
249 * insert discontiguous preceding range
251 hiddenColumns.add(i, new int[] { start, end });
254 else if (end <= region[1])
257 * new range overlaps existing, or is contiguous preceding it - adjust
260 region[0] = Math.min(region[0], start);
263 else if (start <= region[1] + 1)
266 * new range overlaps existing, or is contiguous following it - adjust
267 * start and end columns
269 region[0] = Math.min(region[0], start);
270 region[1] = Math.max(region[1], end);
273 * also update or remove any subsequent ranges
274 * that are overlapped
276 while (i < hiddenColumns.size() - 1)
278 int[] nextRegion = hiddenColumns.get(i + 1);
279 if (nextRegion[0] > end + 1)
282 * gap to next hidden range - no more to update
286 region[1] = Math.max(nextRegion[1], end);
288 // in theory this is faster than hiddenColumns.remove(i+1)
289 // benchmarking results a bit ambivalent
290 hiddenColumns.subList(i + 1, i + 2).clear();
298 * hide a list of ranges
302 public void hideList(List<int[]> ranges)
306 LOCK.writeLock().lock();
307 for (int[] r : ranges)
309 hideColumns(r[0], r[1]);
311 cursor.resetCursor(hiddenColumns);
315 LOCK.writeLock().unlock();
320 * Unhides, and adds to the selection list, all hidden columns
322 public void revealAllHiddenColumns(ColumnSelection sel)
326 LOCK.writeLock().lock();
328 for (int[] region : hiddenColumns)
330 for (int j = region[0]; j < region[1] + 1; j++)
335 hiddenColumns.clear();
336 cursor.resetCursor(hiddenColumns);
341 LOCK.writeLock().unlock();
346 * Reveals, and marks as selected, the hidden column range with the given
350 * the start column to look for
352 * the column selection to add the hidden column range to
354 public void revealHiddenColumns(int start, ColumnSelection sel)
358 LOCK.writeLock().lock();
360 if (!hiddenColumns.isEmpty())
362 int regionIndex = cursor.findRegionForColumn(start)
365 if (regionIndex != -1 && regionIndex != hiddenColumns.size())
367 // regionIndex is the region which either contains start
368 // or lies to the right of start
369 int[] region = hiddenColumns.get(regionIndex);
370 if (start == region[0])
372 for (int j = region[0]; j < region[1] + 1; j++)
376 int colsToRemove = region[1] - region[0] + 1;
377 hiddenColumns.remove(regionIndex);
379 if (hiddenColumns.isEmpty())
381 hiddenColumns.clear();
386 numColumns -= colsToRemove;
388 cursor.updateForDeletedRegion(hiddenColumns);
394 LOCK.writeLock().unlock();
399 * Add gaps into the sequences aligned to profileseq under the given
403 * sequence in al which sequences are aligned to
405 * alignment to have gaps inserted into it
407 * alignment view where sequence corresponding to profileseq is first
409 * @return new HiddenColumns for new alignment view, with insertions into
410 * profileseq marked as hidden.
412 public static HiddenColumns propagateInsertions(SequenceI profileseq,
413 AlignmentI al, AlignmentView input)
417 char gc = al.getGapCharacter();
418 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
419 HiddenColumns nview = (HiddenColumns) alandhidden[1];
420 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
421 nview.propagateInsertions(profileseq, al, origseq);
428 * sequence in al which corresponds to origseq
430 * alignment which is to have gaps inserted into it
432 * sequence corresponding to profileseq which defines gap map for
435 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
440 LOCK.writeLock().lock();
442 char gc = al.getGapCharacter();
444 // take the set of hidden columns, and the set of gaps in origseq,
445 // and remove all the hidden gaps from hiddenColumns
447 // first get the non-gaps as a Bitset
448 // then calculate hidden ^ not(gap)
449 BitSet gaps = origseq.gapBitset();
452 // for each sequence in the alignment, except the profile sequence,
453 // insert gaps corresponding to each hidden region but where each hidden
454 // column region is shifted backwards by the number of preceding visible
455 // gaps update hidden columns at the same time
456 List<int[]> newhidden = new ArrayList<>();
458 int numGapsBefore = 0;
460 for (int[] region : hiddenColumns)
462 // get region coordinates accounting for gaps
463 // we can rely on gaps not being *in* hidden regions because we already
465 while (gapPosition < region[0])
468 if (gaps.get(gapPosition))
474 int left = region[0] - numGapsBefore;
475 int right = region[1] - numGapsBefore;
476 newhidden.add(new int[] { left, right });
478 // make a string with number of gaps = length of hidden region
479 StringBuilder sb = new StringBuilder();
480 for (int s = 0; s < right - left + 1; s++)
484 padGaps(sb, left, profileseq, al);
487 hiddenColumns = newhidden;
488 cursor.resetCursor(hiddenColumns);
492 LOCK.writeLock().unlock();
497 * Pad gaps in all sequences in alignment except profileseq
500 * gap string to insert
502 * position to insert at
504 * sequence not to pad
506 * alignment to pad sequences in
508 private void padGaps(StringBuilder sb, int pos, SequenceI profileseq,
511 // loop over the sequences and pad with gaps where required
512 for (int s = 0, ns = al.getHeight(); s < ns; s++)
514 SequenceI sqobj = al.getSequenceAt(s);
515 if (sqobj != profileseq)
517 String sq = al.getSequenceAt(s).getSequenceAsString();
518 if (sq.length() <= pos)
521 int diff = pos - sq.length() - 1;
526 while ((diff = pos - sq.length() - 1) > 0)
528 if (diff >= sb.length())
534 char[] buf = new char[diff];
535 sb.getChars(0, diff, buf, 0);
536 sq += buf.toString();
544 al.getSequenceAt(s).setSequence(
545 sq.substring(0, pos) + sb.toString() + sq.substring(pos));
552 * Methods which only need read access to the hidden columns collection.
553 * These methods should use a readLock to prevent other threads changing
554 * the hidden columns collection while it is in use.
558 * Output regions data as a string. String is in the format:
559 * reg0[0]<between>reg0[1]<delimiter>reg1[0]<between>reg1[1] ... regn[1]
562 * string to delimit regions
563 * @param betweenstring
564 * to put between start and end region values
565 * @return regions formatted according to delimiter and between strings
567 public String regionsToString(String delimiter, String between)
571 LOCK.readLock().lock();
572 StringBuilder regionBuilder = new StringBuilder();
574 boolean first = true;
575 for (int[] range : hiddenColumns)
579 regionBuilder.append(delimiter);
585 regionBuilder.append(range[0]).append(between).append(range[1]);
589 return regionBuilder.toString();
592 LOCK.readLock().unlock();
597 * Find the number of hidden columns
599 * @return number of hidden columns
605 LOCK.readLock().lock();
609 // numColumns is out of date, so recalculate
612 for (int[] range : hiddenColumns)
614 size += range[1] - range[0] + 1;
623 LOCK.readLock().unlock();
628 * Get the number of distinct hidden regions
630 * @return number of regions
632 public int getNumberOfRegions()
636 LOCK.readLock().lock();
638 if (hasHiddenColumns())
640 num = hiddenColumns.size();
645 LOCK.readLock().unlock();
650 public boolean equals(Object obj)
654 LOCK.readLock().lock();
656 if (!(obj instanceof HiddenColumns))
660 HiddenColumns that = (HiddenColumns) obj;
663 * check hidden columns are either both null, or match
666 if (that.hiddenColumns.size() != this.hiddenColumns.size())
671 Iterator<int[]> thatit = that.iterator();
672 for (int[] thisRange : hiddenColumns)
674 int[] thatRange = thatit.next();
675 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
683 LOCK.readLock().unlock();
688 * Return absolute column index for a visible column index
691 * int column index in alignment view (count from zero)
692 * @return alignment column index for column
694 public int visibleToAbsoluteColumn(int column)
698 LOCK.readLock().lock();
701 if (!hiddenColumns.isEmpty())
703 result += cursor.findRegionForVisColumn(column).getHiddenSoFar();
709 LOCK.readLock().unlock();
714 * Use this method to find out where a column will appear in the visible
715 * alignment when hidden columns exist. If the column is not visible, then the
716 * index of the next visible column on the left will be returned (or 0 if
717 * there is no visible column on the left)
719 * @param hiddenColumn
720 * the column index in the full alignment including hidden columns
721 * @return the position of the column in the visible alignment
723 public int absoluteToVisibleColumn(int hiddenColumn)
727 LOCK.readLock().lock();
728 int result = hiddenColumn;
730 if (!hiddenColumns.isEmpty())
732 HiddenCursorPosition cursorPos = cursor
733 .findRegionForColumn(hiddenColumn);
734 int index = cursorPos.getRegionIndex();
735 int hiddenBeforeCol = cursorPos.getHiddenSoFar();
737 // just subtract hidden cols count - this works fine if column is
739 result = hiddenColumn - hiddenBeforeCol;
741 // now check in case column is hidden - it will be in the returned
743 if (index < hiddenColumns.size())
745 int[] region = hiddenColumns.get(index);
746 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
748 // actually col is hidden, return region[0]-1
749 // unless region[0]==0 in which case return 0
756 result = region[0] - 1 - hiddenBeforeCol;
762 return result; // return the shifted position after removing hidden
766 LOCK.readLock().unlock();
771 * Find the visible column which is a given visible number of columns to the
772 * left of another visible column. i.e. for a startColumn x, the column which
773 * is distance 1 away will be column x-1.
775 * @param visibleDistance
776 * the number of visible columns to offset by
778 * the column to start from
779 * @return the position of the column in the visible alignment
781 public int subtractVisibleColumns(int visibleDistance, int startColumn)
785 LOCK.readLock().lock();
786 int distance = visibleDistance;
788 // in case startColumn is in a hidden region, move it to the left
789 int start = visibleToAbsoluteColumn(absoluteToVisibleColumn(startColumn));
791 Iterator<int[]> it = new ReverseRegionsIterator(0, start,
794 while (it.hasNext() && (distance > 0))
796 int[] region = it.next();
798 if (start > region[1])
800 // subtract the gap to right of region from distance
801 if (start - region[1] <= distance)
803 distance -= start - region[1];
804 start = region[0] - 1;
808 start = start - distance;
813 return start - distance;
817 LOCK.readLock().unlock();
822 * This method returns the rightmost limit of a region of an alignment with
823 * hidden columns. In otherwords, the next hidden column.
826 * the absolute (visible) alignmentPosition to find the next hidden
828 * @return the index of the next hidden column, or alPos if there is no next
831 public int getHiddenBoundaryRight(int alPos)
835 LOCK.readLock().lock();
836 if (!hiddenColumns.isEmpty())
838 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
839 if (index < hiddenColumns.size())
841 int[] region = hiddenColumns.get(index);
842 if (alPos < region[0])
846 else if ((alPos <= region[1])
847 && (index + 1 < hiddenColumns.size()))
849 // alPos is within a hidden region, return the next one
851 region = hiddenColumns.get(index + 1);
859 LOCK.readLock().unlock();
864 * This method returns the leftmost limit of a region of an alignment with
865 * hidden columns. In otherwords, the previous hidden column.
868 * the absolute (visible) alignmentPosition to find the previous
871 public int getHiddenBoundaryLeft(int alPos)
875 LOCK.readLock().lock();
877 if (!hiddenColumns.isEmpty())
879 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
883 int[] region = hiddenColumns.get(index - 1);
890 LOCK.readLock().unlock();
896 * Answers if a column in the alignment is visible
899 * absolute position of column in the alignment
900 * @return true if column is visible
902 public boolean isVisible(int column)
906 LOCK.readLock().lock();
908 int regionindex = cursor.findRegionForColumn(column).getRegionIndex();
909 if (regionindex > -1 && regionindex < hiddenColumns.size())
911 int[] region = hiddenColumns.get(regionindex);
912 // already know that column <= region[1] as cursor returns containing
913 // region or region to right
914 if (column >= region[0])
923 LOCK.readLock().unlock();
928 * Get the visible sections of a set of sequences
931 * sequence position to start from
933 * sequence position to end at
935 * an array of sequences
936 * @return an array of strings encoding the visible parts of each sequence
938 public String[] getVisibleSequenceStrings(int start, int end,
943 LOCK.readLock().lock();
944 int iSize = seqs.length;
945 String[] selections = new String[iSize];
946 if (!hiddenColumns.isEmpty())
948 for (int i = 0; i < iSize; i++)
950 StringBuilder visibleSeq = new StringBuilder();
952 Iterator<int[]> blocks = new VisibleContigsIterator(start,
953 end + 1, hiddenColumns);
955 while (blocks.hasNext())
957 int[] block = blocks.next();
958 if (blocks.hasNext())
961 .append(seqs[i].getSequence(block[0], block[1] + 1));
966 .append(seqs[i].getSequence(block[0], block[1]));
970 selections[i] = visibleSeq.toString();
975 for (int i = 0; i < iSize; i++)
977 selections[i] = seqs[i].getSequenceAsString(start, end);
984 LOCK.readLock().unlock();
989 * Locate the first position visible for this sequence. If seq isn't visible
990 * then return the position of the left side of the hidden boundary region.
993 * sequence to find position for
994 * @return visible start position
996 public int locateVisibleStartOfSequence(SequenceI seq)
1000 LOCK.readLock().lock();
1003 if (hiddenColumns.isEmpty())
1005 return seq.findIndex(seq.getStart()) - 1;
1008 // Simply walk along the sequence whilst watching for hidden column
1010 Iterator<int[]> regions = hiddenColumns.iterator();
1011 int hideStart = seq.getLength();
1015 boolean foundStart = false;
1017 // step through the non-gapped positions of the sequence
1018 for (int i = seq.getStart(); i <= seq.getEnd() && (!foundStart); i++)
1020 // get alignment position of this residue in the sequence
1021 int p = seq.findIndex(i) - 1;
1023 // update hidden region start/end
1024 while (hideEnd < p && regions.hasNext())
1026 int[] region = regions.next();
1028 visNext += region[0] - visPrev;
1029 hideStart = region[0];
1030 hideEnd = region[1];
1034 hideStart = seq.getLength();
1036 // update visible boundary for sequence
1046 return absoluteToVisibleColumn(start);
1048 // otherwise, sequence was completely hidden
1052 LOCK.readLock().unlock();
1059 * @return true if there are columns hidden
1061 public boolean hasHiddenColumns()
1065 LOCK.readLock().lock();
1067 // we don't use getSize()>0 here because it has to iterate over
1068 // the full hiddenColumns collection and so will be much slower
1069 return (!hiddenColumns.isEmpty());
1072 LOCK.readLock().unlock();
1078 * @return true if there is more than one hidden column region
1080 public boolean hasMultiHiddenColumnRegions()
1084 LOCK.readLock().lock();
1085 return !hiddenColumns.isEmpty() && hiddenColumns.size() > 1;
1088 LOCK.readLock().unlock();
1094 * Returns a hashCode built from hidden column ranges
1097 public int hashCode()
1101 LOCK.readLock().lock();
1103 for (int[] hidden : hiddenColumns)
1105 hashCode = HASH_MULTIPLIER * hashCode + hidden[0];
1106 hashCode = HASH_MULTIPLIER * hashCode + hidden[1];
1111 LOCK.readLock().unlock();
1116 * Hide columns corresponding to the marked bits
1119 * - columns mapped to bits starting from zero
1121 public void hideColumns(BitSet inserts)
1125 LOCK.writeLock().lock();
1126 for (int firstSet = inserts
1127 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1128 .nextSetBit(lastSet))
1130 lastSet = inserts.nextClearBit(firstSet);
1131 hideColumns(firstSet, lastSet - 1);
1133 cursor.resetCursor(hiddenColumns);
1137 LOCK.writeLock().unlock();
1142 * Hide columns corresponding to the marked bits, within the range
1143 * [start,end]. Entries in tohide which are outside [start,end] are ignored.
1146 * columns mapped to bits starting from zero
1148 * start of range to hide columns within
1150 * end of range to hide columns within
1152 public void hideColumns(BitSet tohide, int start, int end)
1154 clearHiddenColumnsInRange(start, end);
1156 // make sure only bits between start and end are set
1157 if (!tohide.isEmpty())
1159 tohide.clear(0, start);
1160 tohide.clear(Math.min(end + 1, tohide.length() + 1),
1161 tohide.length() + 1);
1164 hideColumns(tohide);
1168 * Make all columns in the range [start,end] visible
1171 * start of range to show columns
1173 * end of range to show columns
1175 private void clearHiddenColumnsInRange(int start, int end)
1179 LOCK.writeLock().lock();
1181 if (!hiddenColumns.isEmpty())
1183 HiddenCursorPosition pos = cursor.findRegionForColumn(start);
1184 int index = pos.getRegionIndex();
1185 int startindex = index; // first index in hiddenColumns to remove
1187 if (index != -1 && index != hiddenColumns.size())
1189 // regionIndex is the region which either contains start
1190 // or lies to the right of start
1191 int[] region = hiddenColumns.get(index);
1192 if (region[0] < start && region[1] >= start)
1194 // region contains start, truncate so that it ends just before start
1195 region[1] = start - 1;
1200 pos = cursor.findRegionForColumn(end);
1201 index = pos.getRegionIndex();
1202 int endindex = index - 1; // last index in hiddenColumns to remove
1204 if (index != -1 && index != hiddenColumns.size())
1206 // regionIndex is the region which either contains end
1207 // or lies to the right of end
1208 int[] region = hiddenColumns.get(index);
1209 if (region[0] <= end && region[1] > end)
1211 // region contains end, truncate so that it starts just after end
1212 region[0] = end + 1;
1216 hiddenColumns.subList(startindex, endindex + 1).clear();
1217 cursor.resetCursor(hiddenColumns);
1222 LOCK.writeLock().unlock();
1229 * BitSet where hidden columns will be marked
1231 private void andNot(BitSet updates)
1235 LOCK.readLock().lock();
1237 BitSet hiddenBitSet = new BitSet();
1238 for (int[] range : hiddenColumns)
1240 hiddenBitSet.set(range[0], range[1] + 1);
1242 hiddenBitSet.andNot(updates);
1243 hiddenColumns.clear();
1244 hideColumns(hiddenBitSet);
1247 LOCK.readLock().unlock();
1252 * Calculate the visible start and end index of an alignment.
1255 * full alignment width
1256 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1258 public int[] getVisibleStartAndEndIndex(int width)
1262 LOCK.readLock().lock();
1263 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1264 int startPos = alignmentStartEnd[0];
1265 int endPos = alignmentStartEnd[1];
1267 int[] lowestRange = new int[] { -1, -1 };
1268 int[] higestRange = new int[] { -1, -1 };
1270 if (hiddenColumns.isEmpty())
1272 return new int[] { startPos, endPos };
1275 for (int[] range : hiddenColumns)
1277 lowestRange = (range[0] <= startPos) ? range : lowestRange;
1278 higestRange = (range[1] >= endPos) ? range : higestRange;
1281 if (lowestRange[0] == -1) // includes (lowestRange[1] == -1)
1283 startPos = alignmentStartEnd[0];
1287 startPos = lowestRange[1] + 1;
1290 if (higestRange[0] == -1) // includes (higestRange[1] == -1)
1292 endPos = alignmentStartEnd[1];
1296 endPos = higestRange[0] - 1;
1298 return new int[] { startPos, endPos };
1301 LOCK.readLock().unlock();
1306 * Finds the hidden region (if any) which starts or ends at res
1309 * visible residue position, unadjusted for hidden columns
1310 * @return region as [start,end] or null if no matching region is found. If
1311 * res is adjacent to two regions, returns the left region.
1313 public int[] getRegionWithEdgeAtRes(int res)
1317 LOCK.readLock().lock();
1318 int adjres = visibleToAbsoluteColumn(res);
1320 int[] reveal = null;
1322 if (!hiddenColumns.isEmpty())
1324 // look for a region ending just before adjres
1325 int regionindex = cursor.findRegionForColumn(adjres - 1)
1327 if (regionindex < hiddenColumns.size()
1328 && hiddenColumns.get(regionindex)[1] == adjres - 1)
1330 reveal = hiddenColumns.get(regionindex);
1332 // check if the region ends just after adjres
1333 else if (regionindex < hiddenColumns.size()
1334 && hiddenColumns.get(regionindex)[0] == adjres + 1)
1336 reveal = hiddenColumns.get(regionindex);
1343 LOCK.readLock().unlock();
1348 * Return an iterator over the hidden regions
1350 public Iterator<int[]> iterator()
1354 LOCK.readLock().lock();
1355 return new HiddenColsIterator(hiddenColumns);
1358 LOCK.readLock().unlock();
1363 * Return a bounded iterator over the hidden regions
1366 * position to start from (inclusive, absolute column position)
1368 * position to end at (inclusive, absolute column position)
1371 public Iterator<int[]> getBoundedIterator(int start, int end)
1375 LOCK.readLock().lock();
1376 return new HiddenColsIterator(start, end, hiddenColumns);
1379 LOCK.readLock().unlock();
1384 * Return a bounded iterator over the *visible* start positions of hidden
1388 * position to start from (inclusive, visible column position)
1390 * position to end at (inclusive, visible column position)
1392 public Iterator<Integer> getBoundedStartIterator(int start, int end)
1396 LOCK.readLock().lock();
1398 // get absolute position of column in alignment
1399 int absoluteStart = visibleToAbsoluteColumn(start);
1401 // Get cursor position and supply it to the iterator:
1402 // Since we want visible region start, we look for a cursor for the
1403 // (absoluteStart-1), then if absoluteStart is the start of a visible
1404 // region we'll get the cursor pointing to the region before, which is
1406 HiddenCursorPosition pos = cursor
1407 .findRegionForColumn(absoluteStart - 1);
1409 return new BoundedStartRegionIterator(pos, start, end,
1413 LOCK.readLock().unlock();
1418 * Return an iterator over visible *columns* (not regions) between the given
1419 * start and end boundaries
1422 * first column (inclusive)
1424 * last column (inclusive)
1426 public Iterator<Integer> getVisibleColsIterator(int start, int end)
1430 LOCK.readLock().lock();
1431 return new VisibleColsIterator(start, end, hiddenColumns);
1434 LOCK.readLock().unlock();
1439 * return an iterator over visible segments between the given start and end
1443 * first column, inclusive from 0
1445 * last column - not inclusive
1446 * @param useVisibleCoords
1447 * if true, start and end are visible column positions, not absolute
1450 public VisibleContigsIterator getVisContigsIterator(int start,
1452 boolean useVisibleCoords)
1454 int adjstart = start;
1456 if (useVisibleCoords)
1458 adjstart = visibleToAbsoluteColumn(start);
1459 adjend = visibleToAbsoluteColumn(end);
1464 LOCK.readLock().lock();
1465 return new VisibleContigsIterator(adjstart, adjend, hiddenColumns);
1468 LOCK.readLock().unlock();