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.Arrays;
25 import java.util.BitSet;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.concurrent.locks.ReentrantReadWriteLock;
31 * This class manages the collection of hidden columns associated with an
32 * alignment. To iterate over the collection, or over visible columns/regions,
33 * use an iterator obtained from one of:
35 * - getBoundedIterator: iterates over the hidden regions, within some bounds,
36 * returning absolute positions
38 * - getBoundedStartIterator: iterates over the start positions of hidden
39 * regions, within some bounds, returning visible positions
41 * - getVisContigsIterator: iterates over visible regions in a range, returning
44 * - getVisibleColsIterator: iterates over the visible *columns*
46 * For performance reasons, provide bounds where possible. Note that column
47 * numbering begins at 0 throughout this class.
52 public class HiddenColumns
54 private static final int HASH_MULTIPLIER = 31;
56 private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock();
59 * Cursor which tracks the last used hidden columns region, and the number
60 * of hidden columns up to (but not including) that region.
62 private HiddenColumnsCursor cursor = new HiddenColumnsCursor();
65 * cache of the number of hidden columns
67 private int numColumns = 0;
70 * list of hidden column [start, end] ranges; the list is maintained in
71 * ascending start column order
73 private List<int[]> hiddenColumns = new ArrayList<>();
78 public HiddenColumns()
83 * Methods which change the hiddenColumns collection. These methods should
84 * use a writeLock to prevent other threads accessing the hiddenColumns
85 * collection while changes are being made. They should also reset the hidden
86 * columns cursor, and either update the hidden columns count, or set it to 0
87 * (so that it will later be updated when needed).
94 * the HiddenColumns object to copy from
96 public HiddenColumns(HiddenColumns copy)
98 this(copy, Integer.MIN_VALUE, Integer.MAX_VALUE, 0);
102 * Copy constructor within bounds and with offset. Copies hidden column
103 * regions fully contained between start and end, and offsets positions by
104 * subtracting offset.
107 * HiddenColumns instance to copy from
109 * lower bound to copy from
111 * upper bound to copy to
113 * offset to subtract from each region boundary position
116 public HiddenColumns(HiddenColumns copy, int start, int end, int offset)
120 LOCK.writeLock().lock();
124 Iterator<int[]> it = copy.getBoundedIterator(start, end);
127 int[] region = it.next();
128 // still need to check boundaries because iterator returns
129 // all overlapping regions and we need contained regions
130 if (region[0] >= start && region[1] <= end)
134 { region[0] - offset, region[1] - offset });
135 numColumns += region[1] - region[0] + 1;
138 cursor.resetCursor(hiddenColumns);
142 LOCK.writeLock().unlock();
147 * Adds the specified column range to the hidden columns collection
150 * start of range to add (absolute position in alignment)
152 * end of range to add (absolute position in alignment)
154 public void hideColumns(int start, int end)
156 boolean wasAlreadyLocked = false;
159 // check if the write lock was already locked by this thread,
160 // as this method can be called internally in loops within HiddenColumns
161 if (!LOCK.isWriteLockedByCurrentThread())
163 LOCK.writeLock().lock();
167 wasAlreadyLocked = true;
171 int prevHiddenCount = 0;
173 if (!hiddenColumns.isEmpty())
175 // set up cursor reset values
176 HiddenCursorPosition cursorPos = cursor.findRegionForColumn(start);
177 regionindex = cursorPos.getRegionIndex();
181 // get previous index and hidden count for updating the cursor later
182 previndex = regionindex - 1;
183 int[] prevRegion = hiddenColumns.get(previndex);
184 prevHiddenCount = cursorPos.getHiddenSoFar()
185 - (prevRegion[1] - prevRegion[0] + 1);
190 * new range follows everything else; check first to avoid looping over whole hiddenColumns collection
192 if (hiddenColumns.isEmpty()
193 || start > hiddenColumns.get(hiddenColumns.size() - 1)[1])
195 hiddenColumns.add(new int[] { start, end });
200 * traverse existing hidden ranges and insert / amend / append as
203 boolean added = false;
206 added = insertRangeAtRegion(regionindex - 1, start, end);
208 if (!added && regionindex < hiddenColumns.size())
210 insertRangeAtRegion(regionindex, start, end);
214 // reset the cursor to just before our insertion point: this saves
215 // a lot of reprocessing in large alignments
216 cursor.resetCursor(hiddenColumns, previndex, prevHiddenCount);
218 // reset the number of columns so they will be recounted
223 if (!wasAlreadyLocked)
225 LOCK.writeLock().unlock();
231 * Insert [start, range] at the region at index i in hiddenColumns, if
237 * start of range to insert
239 * end of range to insert
240 * @return true if range was successfully inserted
242 private boolean insertRangeAtRegion(int i, int start, int end)
244 boolean added = false;
246 int[] region = hiddenColumns.get(i);
247 if (end < region[0] - 1)
250 * insert discontiguous preceding range
252 hiddenColumns.add(i, new int[] { start, end });
255 else if (end <= region[1])
258 * new range overlaps existing, or is contiguous preceding it - adjust
261 region[0] = Math.min(region[0], start);
264 else if (start <= region[1] + 1)
267 * new range overlaps existing, or is contiguous following it - adjust
268 * start and end columns
270 region[0] = Math.min(region[0], start);
271 region[1] = Math.max(region[1], end);
274 * also update or remove any subsequent ranges
275 * that are overlapped
277 while (i < hiddenColumns.size() - 1)
279 int[] nextRegion = hiddenColumns.get(i + 1);
280 if (nextRegion[0] > end + 1)
283 * gap to next hidden range - no more to update
287 region[1] = Math.max(nextRegion[1], end);
289 // in theory this is faster than hiddenColumns.remove(i+1)
290 // benchmarking results a bit ambivalent
291 hiddenColumns.subList(i + 1, i + 2).clear();
299 * hide a list of ranges
303 public void hideList(List<int[]> ranges)
307 LOCK.writeLock().lock();
308 for (int[] r : ranges)
310 hideColumns(r[0], r[1]);
312 cursor.resetCursor(hiddenColumns);
316 LOCK.writeLock().unlock();
321 * Unhides, and adds to the selection list, all hidden columns
323 public void revealAllHiddenColumns(ColumnSelection sel)
327 LOCK.writeLock().lock();
329 for (int[] region : hiddenColumns)
331 for (int j = region[0]; j < region[1] + 1; j++)
336 hiddenColumns.clear();
337 cursor.resetCursor(hiddenColumns);
342 LOCK.writeLock().unlock();
347 * Reveals, and marks as selected, the hidden column range with the given
351 * the start column to look for
353 * the column selection to add the hidden column range to
355 public void revealHiddenColumns(int start, ColumnSelection sel)
359 LOCK.writeLock().lock();
361 if (!hiddenColumns.isEmpty())
363 int regionIndex = cursor.findRegionForColumn(start)
366 if (regionIndex != -1 && regionIndex != hiddenColumns.size())
368 // regionIndex is the region which either contains start
369 // or lies to the right of start
370 int[] region = hiddenColumns.get(regionIndex);
371 if (start == region[0])
373 for (int j = region[0]; j < region[1] + 1; j++)
377 int colsToRemove = region[1] - region[0] + 1;
378 hiddenColumns.remove(regionIndex);
380 if (hiddenColumns.isEmpty())
382 hiddenColumns.clear();
387 numColumns -= colsToRemove;
389 cursor.updateForDeletedRegion(hiddenColumns);
395 LOCK.writeLock().unlock();
400 * Add gaps into the sequences aligned to profileseq under the given
404 * sequence in al which sequences are aligned to
406 * alignment to have gaps inserted into it
408 * alignment view where sequence corresponding to profileseq is first
410 * @return new HiddenColumns for new alignment view, with insertions into
411 * profileseq marked as hidden.
413 public static HiddenColumns propagateInsertions(SequenceI profileseq,
414 AlignmentI al, AlignmentView input)
418 char gc = al.getGapCharacter();
419 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
420 HiddenColumns nview = (HiddenColumns) alandhidden[1];
421 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
422 nview.propagateInsertions(profileseq, al, origseq);
429 * sequence in al which corresponds to origseq
431 * alignment which is to have gaps inserted into it
433 * sequence corresponding to profileseq which defines gap map for
436 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
441 LOCK.writeLock().lock();
443 char gc = al.getGapCharacter();
445 // take the set of hidden columns, and the set of gaps in origseq,
446 // and remove all the hidden gaps from hiddenColumns
448 // first get the non-gaps as a Bitset
449 // then calculate hidden ^ not(gap)
450 BitSet gaps = origseq.gapBitset();
453 // for each sequence in the alignment, except the profile sequence,
454 // insert gaps corresponding to each hidden region but where each hidden
455 // column region is shifted backwards by the number of preceding visible
456 // gaps update hidden columns at the same time
457 List<int[]> newhidden = new ArrayList<>();
459 int numGapsBefore = 0;
461 for (int[] region : hiddenColumns)
463 // get region coordinates accounting for gaps
464 // we can rely on gaps not being *in* hidden regions because we already
466 while (gapPosition < region[0])
469 if (gaps.get(gapPosition))
475 int left = region[0] - numGapsBefore;
476 int right = region[1] - numGapsBefore;
477 newhidden.add(new int[] { left, right });
479 // make a string with number of gaps = length of hidden region
480 StringBuilder sb = new StringBuilder();
481 for (int s = 0; s < right - left + 1; s++)
485 padGaps(sb, left, profileseq, al);
488 hiddenColumns = newhidden;
489 cursor.resetCursor(hiddenColumns);
493 LOCK.writeLock().unlock();
498 * Pad gaps in all sequences in alignment except profileseq
501 * gap string to insert
503 * position to insert at
505 * sequence not to pad
507 * alignment to pad sequences in
509 private void padGaps(StringBuilder sb, int pos, SequenceI profileseq,
512 // loop over the sequences and pad with gaps where required
513 for (int s = 0, ns = al.getHeight(); s < ns; s++)
515 SequenceI sqobj = al.getSequenceAt(s);
516 if (sqobj != profileseq)
518 String sq = al.getSequenceAt(s).getSequenceAsString();
519 if (sq.length() <= pos)
522 int diff = pos - sq.length() - 1;
527 while ((diff = pos - sq.length() - 1) > 0)
529 if (diff >= sb.length())
535 char[] buf = new char[diff];
536 sb.getChars(0, diff, buf, 0);
537 sq += buf.toString();
545 al.getSequenceAt(s).setSequence(
546 sq.substring(0, pos) + sb.toString() + sq.substring(pos));
553 * Methods which only need read access to the hidden columns collection.
554 * These methods should use a readLock to prevent other threads changing
555 * the hidden columns collection while it is in use.
559 * Output regions data as a string. String is in the format:
560 * reg0[0]<between>reg0[1]<delimiter>reg1[0]<between>reg1[1] ... regn[1]
563 * string to delimit regions
564 * @param betweenstring
565 * to put between start and end region values
566 * @return regions formatted according to delimiter and between strings
568 public String regionsToString(String delimiter, String between)
572 LOCK.readLock().lock();
573 StringBuilder regionBuilder = new StringBuilder();
575 boolean first = true;
576 for (int[] range : hiddenColumns)
580 regionBuilder.append(delimiter);
586 regionBuilder.append(range[0]).append(between).append(range[1]);
590 return regionBuilder.toString();
593 LOCK.readLock().unlock();
598 * Find the number of hidden columns
600 * @return number of hidden columns
606 LOCK.readLock().lock();
610 // numColumns is out of date, so recalculate
613 for (int[] range : hiddenColumns)
615 size += range[1] - range[0] + 1;
624 LOCK.readLock().unlock();
629 * Get the number of distinct hidden regions
631 * @return number of regions
633 public int getNumberOfRegions()
637 LOCK.readLock().lock();
638 return hiddenColumns.size();
641 LOCK.readLock().unlock();
646 public boolean equals(Object obj)
650 LOCK.readLock().lock();
652 if (!(obj instanceof HiddenColumns))
656 HiddenColumns that = (HiddenColumns) obj;
659 * check hidden columns are either both null, or match
662 if (that.hiddenColumns.size() != this.hiddenColumns.size())
667 Iterator<int[]> it = this.iterator();
668 Iterator<int[]> thatit = that.iterator();
671 if (!(Arrays.equals(it.next(), thatit.next())))
680 LOCK.readLock().unlock();
685 * Return absolute column index for a visible column index
688 * int column index in alignment view (count from zero)
689 * @return alignment column index for column
691 public int visibleToAbsoluteColumn(int column)
695 LOCK.readLock().lock();
698 if (!hiddenColumns.isEmpty())
700 result += cursor.findRegionForVisColumn(column).getHiddenSoFar();
706 LOCK.readLock().unlock();
711 * Use this method to find out where a column will appear in the visible
712 * alignment when hidden columns exist. If the column is not visible, then the
713 * index of the next visible column on the left will be returned (or 0 if
714 * there is no visible column on the left)
716 * @param hiddenColumn
717 * the column index in the full alignment including hidden columns
718 * @return the position of the column in the visible alignment
720 public int absoluteToVisibleColumn(int hiddenColumn)
724 LOCK.readLock().lock();
725 int result = hiddenColumn;
727 if (!hiddenColumns.isEmpty())
729 HiddenCursorPosition cursorPos = cursor
730 .findRegionForColumn(hiddenColumn);
731 int index = cursorPos.getRegionIndex();
732 int hiddenBeforeCol = cursorPos.getHiddenSoFar();
734 // just subtract hidden cols count - this works fine if column is
736 result = hiddenColumn - hiddenBeforeCol;
738 // now check in case column is hidden - it will be in the returned
740 if (index < hiddenColumns.size())
742 int[] region = hiddenColumns.get(index);
743 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
745 // actually col is hidden, return region[0]-1
746 // unless region[0]==0 in which case return 0
753 result = region[0] - 1 - hiddenBeforeCol;
759 return result; // return the shifted position after removing hidden
763 LOCK.readLock().unlock();
768 * Find the visible column which is a given visible number of columns to the
769 * left (negative visibleDistance) or right (positive visibleDistance) of
770 * startColumn. If startColumn is not visible, we use the visible column at
771 * the left boundary of the hidden region containing startColumn.
773 * @param visibleDistance
774 * the number of visible columns to offset by (left offset = negative
775 * value; right offset = positive value)
777 * the position of the column to start from (absolute position)
778 * @return the position of the column which is <visibleDistance> away
779 * (absolute position)
781 public int offsetByVisibleColumns(int visibleDistance, int startColumn)
785 LOCK.readLock().lock();
786 int start = absoluteToVisibleColumn(startColumn);
787 return visibleToAbsoluteColumn(start + visibleDistance);
791 LOCK.readLock().unlock();
796 * This method returns the rightmost limit of a region of an alignment with
797 * hidden columns. In otherwords, the next hidden column.
800 * the absolute (visible) alignmentPosition to find the next hidden
802 * @return the index of the next hidden column, or alPos if there is no next
805 public int getHiddenBoundaryRight(int alPos)
809 LOCK.readLock().lock();
810 if (!hiddenColumns.isEmpty())
812 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
813 if (index < hiddenColumns.size())
815 int[] region = hiddenColumns.get(index);
816 if (alPos < region[0])
820 else if ((alPos <= region[1])
821 && (index + 1 < hiddenColumns.size()))
823 // alPos is within a hidden region, return the next one
825 region = hiddenColumns.get(index + 1);
833 LOCK.readLock().unlock();
838 * This method returns the leftmost limit of a region of an alignment with
839 * hidden columns. In otherwords, the previous hidden column.
842 * the absolute (visible) alignmentPosition to find the previous
845 public int getHiddenBoundaryLeft(int alPos)
849 LOCK.readLock().lock();
851 if (!hiddenColumns.isEmpty())
853 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
857 int[] region = hiddenColumns.get(index - 1);
864 LOCK.readLock().unlock();
870 * Answers if a column in the alignment is visible
873 * absolute position of column in the alignment
874 * @return true if column is visible
876 public boolean isVisible(int column)
880 LOCK.readLock().lock();
882 int regionindex = cursor.findRegionForColumn(column).getRegionIndex();
883 if (regionindex > -1 && regionindex < hiddenColumns.size())
885 int[] region = hiddenColumns.get(regionindex);
886 // already know that column <= region[1] as cursor returns containing
887 // region or region to right
888 if (column >= region[0])
897 LOCK.readLock().unlock();
902 * Get the visible sections of a set of sequences
905 * sequence position to start from
907 * sequence position to end at
909 * an array of sequences
910 * @return an array of strings encoding the visible parts of each sequence
912 public String[] getVisibleSequenceStrings(int start, int end,
917 LOCK.readLock().lock();
918 int iSize = seqs.length;
919 String[] selections = new String[iSize];
920 if (!hiddenColumns.isEmpty())
922 for (int i = 0; i < iSize; i++)
924 StringBuilder visibleSeq = new StringBuilder();
926 Iterator<int[]> blocks = new VisibleContigsIterator(start,
927 end + 1, hiddenColumns);
929 while (blocks.hasNext())
931 int[] block = blocks.next();
932 if (blocks.hasNext())
935 .append(seqs[i].getSequence(block[0], block[1] + 1));
940 .append(seqs[i].getSequence(block[0], block[1]));
944 selections[i] = visibleSeq.toString();
949 for (int i = 0; i < iSize; i++)
951 selections[i] = seqs[i].getSequenceAsString(start, end);
958 LOCK.readLock().unlock();
963 * Locate the first position visible for this sequence. If seq isn't visible
964 * then return the position of the left side of the hidden boundary region.
967 * sequence to find position for
968 * @return visible start position
970 public int locateVisibleStartOfSequence(SequenceI seq)
974 LOCK.readLock().lock();
977 if (hiddenColumns.isEmpty())
979 return seq.findIndex(seq.getStart()) - 1;
982 // Simply walk along the sequence whilst watching for hidden column
984 Iterator<int[]> regions = hiddenColumns.iterator();
985 int hideStart = seq.getLength();
989 boolean foundStart = false;
991 // step through the non-gapped positions of the sequence
992 for (int i = seq.getStart(); i <= seq.getEnd() && (!foundStart); i++)
994 // get alignment position of this residue in the sequence
995 int p = seq.findIndex(i) - 1;
997 // update hidden region start/end
998 while (hideEnd < p && regions.hasNext())
1000 int[] region = regions.next();
1002 visNext += region[0] - visPrev;
1003 hideStart = region[0];
1004 hideEnd = region[1];
1008 hideStart = seq.getLength();
1010 // update visible boundary for sequence
1020 return absoluteToVisibleColumn(start);
1022 // otherwise, sequence was completely hidden
1026 LOCK.readLock().unlock();
1033 * @return true if there are columns hidden
1035 public boolean hasHiddenColumns()
1039 LOCK.readLock().lock();
1041 // we don't use getSize()>0 here because it has to iterate over
1042 // the full hiddenColumns collection and so will be much slower
1043 return (!hiddenColumns.isEmpty());
1046 LOCK.readLock().unlock();
1052 * @return true if there is more than one hidden column region
1054 public boolean hasMultiHiddenColumnRegions()
1058 LOCK.readLock().lock();
1059 return !hiddenColumns.isEmpty() && hiddenColumns.size() > 1;
1062 LOCK.readLock().unlock();
1068 * Returns a hashCode built from hidden column ranges
1071 public int hashCode()
1075 LOCK.readLock().lock();
1077 for (int[] hidden : hiddenColumns)
1079 hashCode = HASH_MULTIPLIER * hashCode + hidden[0];
1080 hashCode = HASH_MULTIPLIER * hashCode + hidden[1];
1085 LOCK.readLock().unlock();
1090 * Hide columns corresponding to the marked bits
1093 * - columns mapped to bits starting from zero
1095 public void hideColumns(BitSet inserts)
1099 LOCK.writeLock().lock();
1100 for (int firstSet = inserts
1101 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1102 .nextSetBit(lastSet))
1104 lastSet = inserts.nextClearBit(firstSet);
1105 hideColumns(firstSet, lastSet - 1);
1107 cursor.resetCursor(hiddenColumns);
1111 LOCK.writeLock().unlock();
1116 * Hide columns corresponding to the marked bits, within the range
1117 * [start,end]. Entries in tohide which are outside [start,end] are ignored.
1120 * columns mapped to bits starting from zero
1122 * start of range to hide columns within
1124 * end of range to hide columns within
1126 public void hideColumns(BitSet tohide, int start, int end)
1128 clearHiddenColumnsInRange(start, end);
1130 // make sure only bits between start and end are set
1131 if (!tohide.isEmpty())
1133 tohide.clear(0, start);
1134 tohide.clear(Math.min(end + 1, tohide.length() + 1),
1135 tohide.length() + 1);
1138 hideColumns(tohide);
1142 * Make all columns in the range [start,end] visible
1145 * start of range to show columns
1147 * end of range to show columns
1149 private void clearHiddenColumnsInRange(int start, int end)
1153 LOCK.writeLock().lock();
1155 if (!hiddenColumns.isEmpty())
1157 HiddenCursorPosition pos = cursor.findRegionForColumn(start);
1158 int index = pos.getRegionIndex();
1159 int startindex = index; // first index in hiddenColumns to remove
1161 if (index != -1 && index != hiddenColumns.size())
1163 // regionIndex is the region which either contains start
1164 // or lies to the right of start
1165 int[] region = hiddenColumns.get(index);
1166 if (region[0] < start && region[1] >= start)
1168 // region contains start, truncate so that it ends just before start
1169 region[1] = start - 1;
1174 pos = cursor.findRegionForColumn(end);
1175 index = pos.getRegionIndex();
1176 int endindex = index - 1; // last index in hiddenColumns to remove
1178 if (index != -1 && index != hiddenColumns.size())
1180 // regionIndex is the region which either contains end
1181 // or lies to the right of end
1182 int[] region = hiddenColumns.get(index);
1183 if (region[0] <= end && region[1] > end)
1185 // region contains end, truncate so that it starts just after end
1186 region[0] = end + 1;
1190 hiddenColumns.subList(startindex, endindex + 1).clear();
1191 cursor.resetCursor(hiddenColumns);
1196 LOCK.writeLock().unlock();
1203 * BitSet where hidden columns will be marked
1205 private void andNot(BitSet updates)
1209 LOCK.readLock().lock();
1211 BitSet hiddenBitSet = new BitSet();
1212 for (int[] range : hiddenColumns)
1214 hiddenBitSet.set(range[0], range[1] + 1);
1216 hiddenBitSet.andNot(updates);
1217 hiddenColumns.clear();
1218 hideColumns(hiddenBitSet);
1221 LOCK.readLock().unlock();
1226 * Calculate the visible start and end index of an alignment.
1229 * full alignment width
1230 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1232 public int[] getVisibleStartAndEndIndex(int width)
1236 LOCK.readLock().lock();
1237 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1238 int startPos = alignmentStartEnd[0];
1239 int endPos = alignmentStartEnd[1];
1241 int[] lowestRange = new int[] { -1, -1 };
1242 int[] higestRange = new int[] { -1, -1 };
1244 if (hiddenColumns.isEmpty())
1246 return new int[] { startPos, endPos };
1249 for (int[] range : hiddenColumns)
1251 lowestRange = (range[0] <= startPos) ? range : lowestRange;
1252 higestRange = (range[1] >= endPos) ? range : higestRange;
1255 if (lowestRange[0] == -1) // includes (lowestRange[1] == -1)
1257 startPos = alignmentStartEnd[0];
1261 startPos = lowestRange[1] + 1;
1264 if (higestRange[0] == -1) // includes (higestRange[1] == -1)
1266 endPos = alignmentStartEnd[1];
1270 endPos = higestRange[0] - 1;
1272 return new int[] { startPos, endPos };
1275 LOCK.readLock().unlock();
1280 * Finds the hidden region (if any) which starts or ends at res
1283 * visible residue position, unadjusted for hidden columns
1284 * @return region as [start,end] or null if no matching region is found. If
1285 * res is adjacent to two regions, returns the left region.
1287 public int[] getRegionWithEdgeAtRes(int res)
1291 LOCK.readLock().lock();
1292 int adjres = visibleToAbsoluteColumn(res);
1294 int[] reveal = null;
1296 if (!hiddenColumns.isEmpty())
1298 // look for a region ending just before adjres
1299 int regionindex = cursor.findRegionForColumn(adjres - 1)
1301 if (regionindex < hiddenColumns.size()
1302 && hiddenColumns.get(regionindex)[1] == adjres - 1)
1304 reveal = hiddenColumns.get(regionindex);
1306 // check if the region ends just after adjres
1307 else if (regionindex < hiddenColumns.size()
1308 && hiddenColumns.get(regionindex)[0] == adjres + 1)
1310 reveal = hiddenColumns.get(regionindex);
1317 LOCK.readLock().unlock();
1322 * Return an iterator over the hidden regions
1324 public Iterator<int[]> iterator()
1328 LOCK.readLock().lock();
1329 return new HiddenColsIterator(hiddenColumns);
1332 LOCK.readLock().unlock();
1337 * Return a bounded iterator over the hidden regions
1340 * position to start from (inclusive, absolute column position)
1342 * position to end at (inclusive, absolute column position)
1345 public Iterator<int[]> getBoundedIterator(int start, int end)
1349 LOCK.readLock().lock();
1350 return new HiddenColsIterator(start, end, hiddenColumns);
1353 LOCK.readLock().unlock();
1358 * Return a bounded iterator over the *visible* start positions of hidden
1362 * position to start from (inclusive, visible column position)
1364 * position to end at (inclusive, visible column position)
1366 public Iterator<Integer> getBoundedStartIterator(int start, int end)
1370 LOCK.readLock().lock();
1372 // get absolute position of column in alignment
1373 int absoluteStart = visibleToAbsoluteColumn(start);
1375 // Get cursor position and supply it to the iterator:
1376 // Since we want visible region start, we look for a cursor for the
1377 // (absoluteStart-1), then if absoluteStart is the start of a visible
1378 // region we'll get the cursor pointing to the region before, which is
1380 HiddenCursorPosition pos = cursor
1381 .findRegionForColumn(absoluteStart - 1);
1383 return new BoundedStartRegionIterator(pos, start, end,
1387 LOCK.readLock().unlock();
1392 * Return an iterator over visible *columns* (not regions) between the given
1393 * start and end boundaries
1396 * first column (inclusive)
1398 * last column (inclusive)
1400 public Iterator<Integer> getVisibleColsIterator(int start, int end)
1404 LOCK.readLock().lock();
1405 return new VisibleColsIterator(start, end, hiddenColumns);
1408 LOCK.readLock().unlock();
1413 * return an iterator over visible segments between the given start and end
1417 * first column, inclusive from 0
1419 * last column - not inclusive
1420 * @param useVisibleCoords
1421 * if true, start and end are visible column positions, not absolute
1424 public VisibleContigsIterator getVisContigsIterator(int start,
1426 boolean useVisibleCoords)
1428 int adjstart = start;
1430 if (useVisibleCoords)
1432 adjstart = visibleToAbsoluteColumn(start);
1433 adjend = visibleToAbsoluteColumn(end);
1438 LOCK.readLock().lock();
1439 return new VisibleContigsIterator(adjstart, adjend, hiddenColumns);
1442 LOCK.readLock().unlock();