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 gaps as a Bitset
448 BitSet gaps = origseq.gapBitset();
450 // now calculate hidden ^ not(gap)
451 BitSet hidden = new BitSet();
452 markHiddenRegions(hidden);
454 hiddenColumns.clear();
455 this.hideColumns(hidden);
457 // for each sequence in the alignment, except the profile sequence,
458 // insert gaps corresponding to each hidden region but where each hidden
459 // column region is shifted backwards by the number of preceding visible
460 // gaps update hidden columns at the same time
461 List<int[]> newhidden = new ArrayList<>();
463 int numGapsBefore = 0;
465 for (int[] region : hiddenColumns)
467 // get region coordinates accounting for gaps
468 // we can rely on gaps not being *in* hidden regions because we already
470 while (gapPosition < region[0])
473 if (gaps.get(gapPosition))
479 int left = region[0] - numGapsBefore;
480 int right = region[1] - numGapsBefore;
481 newhidden.add(new int[] { left, right });
483 // make a string with number of gaps = length of hidden region
484 StringBuilder sb = new StringBuilder();
485 for (int s = 0; s < right - left + 1; s++)
489 padGaps(sb, left, profileseq, al);
492 hiddenColumns = newhidden;
493 cursor.resetCursor(hiddenColumns);
497 LOCK.writeLock().unlock();
502 * Pad gaps in all sequences in alignment except profileseq
505 * gap string to insert
507 * position to insert at
509 * sequence not to pad
511 * alignment to pad sequences in
513 private void padGaps(StringBuilder sb, int pos, SequenceI profileseq,
516 // loop over the sequences and pad with gaps where required
517 for (int s = 0, ns = al.getHeight(); s < ns; s++)
519 SequenceI sqobj = al.getSequenceAt(s);
520 if (sqobj != profileseq)
522 String sq = al.getSequenceAt(s).getSequenceAsString();
523 if (sq.length() <= pos)
526 int diff = pos - sq.length() - 1;
531 while ((diff = pos - sq.length() - 1) > 0)
533 if (diff >= sb.length())
539 char[] buf = new char[diff];
540 sb.getChars(0, diff, buf, 0);
541 sq += buf.toString();
549 al.getSequenceAt(s).setSequence(
550 sq.substring(0, pos) + sb.toString() + sq.substring(pos));
557 * Methods which only need read access to the hidden columns collection.
558 * These methods should use a readLock to prevent other threads changing
559 * the hidden columns collection while it is in use.
563 * Output regions data as a string. String is in the format:
564 * reg0[0]<between>reg0[1]<delimiter>reg1[0]<between>reg1[1] ... regn[1]
567 * string to delimit regions
568 * @param betweenstring
569 * to put between start and end region values
570 * @return regions formatted according to delimiter and between strings
572 public String regionsToString(String delimiter, String between)
576 LOCK.readLock().lock();
577 StringBuilder regionBuilder = new StringBuilder();
579 boolean first = true;
580 for (int[] range : hiddenColumns)
584 regionBuilder.append(delimiter);
590 regionBuilder.append(range[0]).append(between).append(range[1]);
594 return regionBuilder.toString();
597 LOCK.readLock().unlock();
602 * Find the number of hidden columns
604 * @return number of hidden columns
610 LOCK.readLock().lock();
614 // numColumns is out of date, so recalculate
617 for (int[] range : hiddenColumns)
619 size += range[1] - range[0] + 1;
628 LOCK.readLock().unlock();
633 * Get the number of distinct hidden regions
635 * @return number of regions
637 public int getNumberOfRegions()
641 LOCK.readLock().lock();
643 if (hasHiddenColumns())
645 num = hiddenColumns.size();
650 LOCK.readLock().unlock();
655 public boolean equals(Object obj)
659 LOCK.readLock().lock();
661 if (!(obj instanceof HiddenColumns))
665 HiddenColumns that = (HiddenColumns) obj;
668 * check hidden columns are either both null, or match
671 if (that.hiddenColumns.size() != this.hiddenColumns.size())
676 Iterator<int[]> thatit = that.iterator();
677 for (int[] thisRange : hiddenColumns)
679 int[] thatRange = thatit.next();
680 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
688 LOCK.readLock().unlock();
693 * Return absolute column index for a visible column index
696 * int column index in alignment view (count from zero)
697 * @return alignment column index for column
699 public int visibleToAbsoluteColumn(int column)
703 LOCK.readLock().lock();
706 if (!hiddenColumns.isEmpty())
708 result += cursor.findRegionForVisColumn(column).getHiddenSoFar();
714 LOCK.readLock().unlock();
719 * Use this method to find out where a column will appear in the visible
720 * alignment when hidden columns exist. If the column is not visible, then the
721 * index of the next visible column on the left will be returned (or 0 if
722 * there is no visible column on the left)
724 * @param hiddenColumn
725 * the column index in the full alignment including hidden columns
726 * @return the position of the column in the visible alignment
728 public int absoluteToVisibleColumn(int hiddenColumn)
732 LOCK.readLock().lock();
733 int result = hiddenColumn;
735 if (!hiddenColumns.isEmpty())
737 HiddenCursorPosition cursorPos = cursor
738 .findRegionForColumn(hiddenColumn);
739 int index = cursorPos.getRegionIndex();
740 int hiddenBeforeCol = cursorPos.getHiddenSoFar();
742 // just subtract hidden cols count - this works fine if column is
744 result = hiddenColumn - hiddenBeforeCol;
746 // now check in case column is hidden - it will be in the returned
748 if (index < hiddenColumns.size())
750 int[] region = hiddenColumns.get(index);
751 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
753 // actually col is hidden, return region[0]-1
754 // unless region[0]==0 in which case return 0
761 result = region[0] - 1 - hiddenBeforeCol;
767 return result; // return the shifted position after removing hidden
771 LOCK.readLock().unlock();
776 * Find the visible column which is a given visible number of columns to the
777 * left of another visible column. i.e. for a startColumn x, the column which
778 * is distance 1 away will be column x-1.
780 * @param visibleDistance
781 * the number of visible columns to offset by
783 * the column to start from
784 * @return the position of the column in the visible alignment
786 public int subtractVisibleColumns(int visibleDistance, int startColumn)
790 LOCK.readLock().lock();
791 int distance = visibleDistance;
793 // in case startColumn is in a hidden region, move it to the left
794 int start = visibleToAbsoluteColumn(absoluteToVisibleColumn(startColumn));
796 Iterator<int[]> it = new ReverseRegionsIterator(0, start,
799 while (it.hasNext() && (distance > 0))
801 int[] region = it.next();
803 if (start > region[1])
805 // subtract the gap to right of region from distance
806 if (start - region[1] <= distance)
808 distance -= start - region[1];
809 start = region[0] - 1;
813 start = start - distance;
818 return start - distance;
822 LOCK.readLock().unlock();
827 * This method returns the rightmost limit of a region of an alignment with
828 * hidden columns. In otherwords, the next hidden column.
831 * the absolute (visible) alignmentPosition to find the next hidden
833 * @return the index of the next hidden column, or alPos if there is no next
836 public int getHiddenBoundaryRight(int alPos)
840 LOCK.readLock().lock();
841 if (!hiddenColumns.isEmpty())
843 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
844 if (index < hiddenColumns.size())
846 int[] region = hiddenColumns.get(index);
847 if (alPos < region[0])
851 else if ((alPos <= region[1])
852 && (index + 1 < hiddenColumns.size()))
854 // alPos is within a hidden region, return the next one
856 region = hiddenColumns.get(index + 1);
864 LOCK.readLock().unlock();
869 * This method returns the leftmost limit of a region of an alignment with
870 * hidden columns. In otherwords, the previous hidden column.
873 * the absolute (visible) alignmentPosition to find the previous
876 public int getHiddenBoundaryLeft(int alPos)
880 LOCK.readLock().lock();
882 if (!hiddenColumns.isEmpty())
884 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
888 int[] region = hiddenColumns.get(index - 1);
895 LOCK.readLock().unlock();
901 * Answers if a column in the alignment is visible
904 * absolute position of column in the alignment
905 * @return true if column is visible
907 public boolean isVisible(int column)
911 LOCK.readLock().lock();
913 int regionindex = cursor.findRegionForColumn(column).getRegionIndex();
914 if (regionindex > -1 && regionindex < hiddenColumns.size())
916 int[] region = hiddenColumns.get(regionindex);
917 // already know that column <= region[1] as cursor returns containing
918 // region or region to right
919 if (column >= region[0])
928 LOCK.readLock().unlock();
933 * Get the visible sections of a set of sequences
936 * sequence position to start from
938 * sequence position to end at
940 * an array of sequences
941 * @return an array of strings encoding the visible parts of each sequence
943 public String[] getVisibleSequenceStrings(int start, int end,
948 LOCK.readLock().lock();
949 int iSize = seqs.length;
950 String[] selections = new String[iSize];
951 if (!hiddenColumns.isEmpty())
953 for (int i = 0; i < iSize; i++)
955 StringBuilder visibleSeq = new StringBuilder();
957 Iterator<int[]> blocks = new VisibleContigsIterator(start,
958 end + 1, hiddenColumns);
960 while (blocks.hasNext())
962 int[] block = blocks.next();
963 if (blocks.hasNext())
966 .append(seqs[i].getSequence(block[0], block[1] + 1));
971 .append(seqs[i].getSequence(block[0], block[1]));
975 selections[i] = visibleSeq.toString();
980 for (int i = 0; i < iSize; i++)
982 selections[i] = seqs[i].getSequenceAsString(start, end);
989 LOCK.readLock().unlock();
994 * Locate the first position visible for this sequence. If seq isn't visible
995 * then return the position of the left side of the hidden boundary region.
998 * sequence to find position for
999 * @return visible start position
1001 public int locateVisibleStartOfSequence(SequenceI seq)
1005 LOCK.readLock().lock();
1008 if (hiddenColumns.isEmpty())
1010 return seq.findIndex(seq.getStart()) - 1;
1013 // Simply walk along the sequence whilst watching for hidden column
1015 Iterator<int[]> regions = hiddenColumns.iterator();
1016 int hideStart = seq.getLength();
1020 boolean foundStart = false;
1022 // step through the non-gapped positions of the sequence
1023 for (int i = seq.getStart(); i <= seq.getEnd() && (!foundStart); i++)
1025 // get alignment position of this residue in the sequence
1026 int p = seq.findIndex(i) - 1;
1028 // update hidden region start/end
1029 while (hideEnd < p && regions.hasNext())
1031 int[] region = regions.next();
1033 visNext += region[0] - visPrev;
1034 hideStart = region[0];
1035 hideEnd = region[1];
1039 hideStart = seq.getLength();
1041 // update visible boundary for sequence
1051 return absoluteToVisibleColumn(start);
1053 // otherwise, sequence was completely hidden
1057 LOCK.readLock().unlock();
1064 * @return true if there are columns hidden
1066 public boolean hasHiddenColumns()
1070 LOCK.readLock().lock();
1072 // we don't use getSize()>0 here because it has to iterate over
1073 // the full hiddenColumns collection and so will be much slower
1074 return (!hiddenColumns.isEmpty());
1077 LOCK.readLock().unlock();
1083 * @return true if there is more than one hidden column region
1085 public boolean hasMultiHiddenColumnRegions()
1089 LOCK.readLock().lock();
1090 return !hiddenColumns.isEmpty() && hiddenColumns.size() > 1;
1093 LOCK.readLock().unlock();
1099 * Returns a hashCode built from hidden column ranges
1102 public int hashCode()
1106 LOCK.readLock().lock();
1108 for (int[] hidden : hiddenColumns)
1110 hashCode = HASH_MULTIPLIER * hashCode + hidden[0];
1111 hashCode = HASH_MULTIPLIER * hashCode + hidden[1];
1116 LOCK.readLock().unlock();
1121 * Hide columns corresponding to the marked bits
1124 * - columns mapped to bits starting from zero
1126 public void hideColumns(BitSet inserts)
1130 LOCK.writeLock().lock();
1131 for (int firstSet = inserts
1132 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1133 .nextSetBit(lastSet))
1135 lastSet = inserts.nextClearBit(firstSet);
1136 hideColumns(firstSet, lastSet - 1);
1138 cursor.resetCursor(hiddenColumns);
1142 LOCK.writeLock().unlock();
1147 * Hide columns corresponding to the marked bits, within the range
1148 * [start,end]. Entries in tohide which are outside [start,end] are ignored.
1151 * columns mapped to bits starting from zero
1153 * start of range to hide columns within
1155 * end of range to hide columns within
1157 public void hideColumns(BitSet tohide, int start, int end)
1159 clearHiddenColumnsInRange(start, end);
1161 // make sure only bits between start and end are set
1162 tohide.clear(0, start - 1);
1163 tohide.clear(Math.min(end + 1, tohide.length() - 1),
1164 tohide.length() - 1);
1166 hideColumns(tohide);
1170 * Make all columns in the range [start,end] visible
1173 * start of range to show columns
1175 * end of range to show columns
1177 private void clearHiddenColumnsInRange(int start, int end)
1181 LOCK.writeLock().lock();
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);
1221 LOCK.writeLock().unlock();
1228 * BitSet where hidden columns will be marked
1230 public void markHiddenRegions(BitSet inserts)
1234 LOCK.readLock().lock();
1236 for (int[] range : hiddenColumns)
1238 inserts.set(range[0], range[1] + 1);
1242 LOCK.readLock().unlock();
1247 * Calculate the visible start and end index of an alignment.
1250 * full alignment width
1251 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1253 public int[] getVisibleStartAndEndIndex(int width)
1257 LOCK.readLock().lock();
1258 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1259 int startPos = alignmentStartEnd[0];
1260 int endPos = alignmentStartEnd[1];
1262 int[] lowestRange = new int[] { -1, -1 };
1263 int[] higestRange = new int[] { -1, -1 };
1265 if (hiddenColumns.isEmpty())
1267 return new int[] { startPos, endPos };
1270 for (int[] range : hiddenColumns)
1272 lowestRange = (range[0] <= startPos) ? range : lowestRange;
1273 higestRange = (range[1] >= endPos) ? range : higestRange;
1276 if (lowestRange[0] == -1) // includes (lowestRange[1] == -1)
1278 startPos = alignmentStartEnd[0];
1282 startPos = lowestRange[1] + 1;
1285 if (higestRange[0] == -1) // includes (higestRange[1] == -1)
1287 endPos = alignmentStartEnd[1];
1291 endPos = higestRange[0] - 1;
1293 return new int[] { startPos, endPos };
1296 LOCK.readLock().unlock();
1301 * Finds the hidden region (if any) which starts or ends at res
1304 * visible residue position, unadjusted for hidden columns
1305 * @return region as [start,end] or null if no matching region is found. If
1306 * res is adjacent to two regions, returns the left region.
1308 public int[] getRegionWithEdgeAtRes(int res)
1312 LOCK.readLock().lock();
1313 int adjres = visibleToAbsoluteColumn(res);
1315 int[] reveal = null;
1317 if (!hiddenColumns.isEmpty())
1319 // look for a region ending just before adjres
1320 int regionindex = cursor.findRegionForColumn(adjres - 1)
1322 if (regionindex < hiddenColumns.size()
1323 && hiddenColumns.get(regionindex)[1] == adjres - 1)
1325 reveal = hiddenColumns.get(regionindex);
1327 // check if the region ends just after adjres
1328 else if (regionindex < hiddenColumns.size()
1329 && hiddenColumns.get(regionindex)[0] == adjres + 1)
1331 reveal = hiddenColumns.get(regionindex);
1338 LOCK.readLock().unlock();
1343 * Return an iterator over the hidden regions
1345 public Iterator<int[]> iterator()
1349 LOCK.readLock().lock();
1350 return new HiddenColsIterator(hiddenColumns);
1353 LOCK.readLock().unlock();
1358 * Return a bounded iterator over the hidden regions
1361 * position to start from (inclusive, absolute column position)
1363 * position to end at (inclusive, absolute column position)
1366 public Iterator<int[]> getBoundedIterator(int start, int end)
1370 LOCK.readLock().lock();
1371 return new HiddenColsIterator(start, end, hiddenColumns);
1374 LOCK.readLock().unlock();
1379 * Return a bounded iterator over the *visible* start positions of hidden
1383 * position to start from (inclusive, visible column position)
1385 * position to end at (inclusive, visible column position)
1387 public Iterator<Integer> getBoundedStartIterator(int start, int end)
1391 LOCK.readLock().lock();
1393 // get absolute position of column in alignment
1394 int absoluteStart = visibleToAbsoluteColumn(start);
1396 // Get cursor position and supply it to the iterator:
1397 // Since we want visible region start, we look for a cursor for the
1398 // (absoluteStart-1), then if absoluteStart is the start of a visible
1399 // region we'll get the cursor pointing to the region before, which is
1401 HiddenCursorPosition pos = cursor
1402 .findRegionForColumn(absoluteStart - 1);
1404 return new BoundedStartRegionIterator(pos, start, end,
1408 LOCK.readLock().unlock();
1413 * Return an iterator over visible *columns* (not regions) between the given
1414 * start and end boundaries
1417 * first column (inclusive)
1419 * last column (inclusive)
1421 public Iterator<Integer> getVisibleColsIterator(int start, int end)
1425 LOCK.readLock().lock();
1426 return new VisibleColsIterator(start, end, hiddenColumns);
1429 LOCK.readLock().unlock();
1434 * return an iterator over visible segments between the given start and end
1438 * first column, inclusive from 0
1440 * last column - not inclusive
1441 * @param useVisibleCoords
1442 * if true, start and end are visible column positions, not absolute
1445 public VisibleContigsIterator getVisContigsIterator(int start,
1447 boolean useVisibleCoords)
1449 int adjstart = start;
1451 if (useVisibleCoords)
1453 adjstart = visibleToAbsoluteColumn(start);
1454 adjend = visibleToAbsoluteColumn(end);
1459 LOCK.readLock().lock();
1460 return new VisibleContigsIterator(adjstart, adjend, hiddenColumns);
1463 LOCK.readLock().unlock();