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 Iterator<int[]> it = hiddenColumns.iterator();
331 int[] region = it.next();
332 for (int j = region[0]; j < region[1] + 1; j++)
337 hiddenColumns.clear();
338 cursor.resetCursor(hiddenColumns);
343 LOCK.writeLock().unlock();
348 * Reveals, and marks as selected, the hidden column range with the given
352 * the start column to look for
354 * the column selection to add the hidden column range to
356 public void revealHiddenColumns(int start, ColumnSelection sel)
360 LOCK.writeLock().lock();
362 if (!hiddenColumns.isEmpty())
364 int regionIndex = cursor.findRegionForColumn(start)
367 if (regionIndex != -1 && regionIndex != hiddenColumns.size())
369 // regionIndex is the region which either contains start
370 // or lies to the right of start
371 int[] region = hiddenColumns.get(regionIndex);
372 if (start == region[0])
374 for (int j = region[0]; j < region[1] + 1; j++)
378 int colsToRemove = region[1] - region[0] + 1;
379 hiddenColumns.remove(regionIndex);
381 if (hiddenColumns.isEmpty())
383 hiddenColumns.clear();
388 numColumns -= colsToRemove;
390 cursor.updateForDeletedRegion(hiddenColumns);
396 LOCK.writeLock().unlock();
401 * Add gaps into the sequences aligned to profileseq under the given
405 * sequence in al which sequences are aligned to
407 * alignment to have gaps inserted into it
409 * alignment view where sequence corresponding to profileseq is first
411 * @return new HiddenColumns for new alignment view, with insertions into
412 * profileseq marked as hidden.
414 public static HiddenColumns propagateInsertions(SequenceI profileseq,
415 AlignmentI al, AlignmentView input)
419 char gc = al.getGapCharacter();
420 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
421 HiddenColumns nview = (HiddenColumns) alandhidden[1];
422 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
423 nview.propagateInsertions(profileseq, al, origseq);
430 * sequence in al which corresponds to origseq
432 * alignment which is to have gaps inserted into it
434 * sequence corresponding to profileseq which defines gap map for
437 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
442 LOCK.writeLock().lock();
444 char gc = al.getGapCharacter();
446 // take the set of hidden columns, and the set of gaps in origseq,
447 // and remove all the hidden gaps from hiddenColumns
449 // first get the gaps as a Bitset
450 BitSet gaps = origseq.gapBitset();
452 // now calculate hidden ^ not(gap)
453 BitSet hidden = new BitSet();
454 markHiddenRegions(hidden);
456 hiddenColumns.clear();
457 this.hideColumns(hidden);
459 // for each sequence in the alignment, except the profile sequence,
460 // insert gaps corresponding to each hidden region but where each hidden
461 // column region is shifted backwards by the number of preceding visible
462 // gaps update hidden columns at the same time
463 Iterator<int[]> regions = hiddenColumns.iterator();
464 List<int[]> newhidden = new ArrayList<>();
466 int numGapsBefore = 0;
468 while (regions.hasNext())
470 // get region coordinates accounting for gaps
471 // we can rely on gaps not being *in* hidden regions because we already
473 int[] region = regions.next();
474 while (gapPosition < region[0])
477 if (gaps.get(gapPosition))
483 int left = region[0] - numGapsBefore;
484 int right = region[1] - numGapsBefore;
485 newhidden.add(new int[] { left, right });
487 // make a string with number of gaps = length of hidden region
488 StringBuffer sb = new StringBuffer();
489 for (int s = 0; s < right - left + 1; s++)
493 padGaps(sb, left, profileseq, al);
496 hiddenColumns = newhidden;
497 cursor.resetCursor(hiddenColumns);
501 LOCK.writeLock().unlock();
506 * Pad gaps in all sequences in alignment except profileseq
509 * gap string to insert
511 * position to insert at
513 * sequence not to pad
515 * alignment to pad sequences in
517 private void padGaps(StringBuffer sb, int pos, SequenceI profileseq,
520 // loop over the sequences and pad with gaps where required
521 for (int s = 0, ns = al.getHeight(); s < ns; s++)
523 SequenceI sqobj = al.getSequenceAt(s);
524 if (sqobj != profileseq)
526 String sq = al.getSequenceAt(s).getSequenceAsString();
527 if (sq.length() <= pos)
530 int diff = pos - sq.length() - 1;
535 while ((diff = pos - sq.length() - 1) > 0)
537 if (diff >= sb.length())
543 char[] buf = new char[diff];
544 sb.getChars(0, diff, buf, 0);
545 sq += buf.toString();
553 al.getSequenceAt(s).setSequence(
554 sq.substring(0, pos) + sb.toString() + sq.substring(pos));
561 * Methods which only need read access to the hidden columns collection.
562 * These methods should use a readLock to prevent other threads changing
563 * the hidden columns collection while it is in use.
567 * Output regions data as a string. String is in the format:
568 * reg0[0]<between>reg0[1]<delimiter>reg1[0]<between>reg1[1] ... regn[1]
571 * string to delimit regions
572 * @param betweenstring
573 * to put between start and end region values
574 * @return regions formatted according to delimiter and between strings
576 public String regionsToString(String delimiter, String between)
580 LOCK.readLock().lock();
581 StringBuilder regionBuilder = new StringBuilder();
583 Iterator<int[]> it = hiddenColumns.iterator();
586 int[] range = it.next();
587 regionBuilder.append(delimiter).append(range[0]).append(between)
591 regionBuilder.deleteCharAt(0);
595 return regionBuilder.toString();
598 LOCK.readLock().unlock();
603 * Find the number of hidden columns
605 * @return number of hidden columns
611 LOCK.readLock().lock();
615 // numColumns is out of date, so recalculate
618 Iterator<int[]> it = hiddenColumns.iterator();
621 int[] range = it.next();
622 size += range[1] - range[0] + 1;
631 LOCK.readLock().unlock();
636 * Get the number of distinct hidden regions
638 * @return number of regions
640 public int getNumberOfRegions()
644 LOCK.readLock().lock();
646 if (hasHiddenColumns())
648 num = hiddenColumns.size();
653 LOCK.readLock().unlock();
658 public boolean equals(Object obj)
662 LOCK.readLock().lock();
664 if (!(obj instanceof HiddenColumns))
668 HiddenColumns that = (HiddenColumns) obj;
671 * check hidden columns are either both null, or match
674 if (that.hiddenColumns.size() != this.hiddenColumns.size())
679 Iterator<int[]> it = hiddenColumns.iterator();
680 Iterator<int[]> thatit = that.iterator();
683 int[] thisRange = it.next();
684 int[] thatRange = thatit.next();
685 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
693 LOCK.readLock().unlock();
698 * Return absolute column index for a visible column index
701 * int column index in alignment view (count from zero)
702 * @return alignment column index for column
704 public int visibleToAbsoluteColumn(int column)
708 LOCK.readLock().lock();
711 if (!hiddenColumns.isEmpty())
713 result += cursor.findRegionForVisColumn(column).getHiddenSoFar();
719 LOCK.readLock().unlock();
724 * Use this method to find out where a column will appear in the visible
725 * alignment when hidden columns exist. If the column is not visible, then the
726 * index of the next visible column on the left will be returned (or 0 if
727 * there is no visible column on the left)
729 * @param hiddenColumn
730 * the column index in the full alignment including hidden columns
731 * @return the position of the column in the visible alignment
733 public int absoluteToVisibleColumn(int hiddenColumn)
737 LOCK.readLock().lock();
738 int result = hiddenColumn;
740 if (!hiddenColumns.isEmpty())
742 HiddenCursorPosition cursorPos = cursor
743 .findRegionForColumn(hiddenColumn);
744 int index = cursorPos.getRegionIndex();
745 int hiddenBeforeCol = cursorPos.getHiddenSoFar();
747 // just subtract hidden cols count - this works fine if column is
749 result = hiddenColumn - hiddenBeforeCol;
751 // now check in case column is hidden - it will be in the returned
753 if (index < hiddenColumns.size())
755 int[] region = hiddenColumns.get(index);
756 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
758 // actually col is hidden, return region[0]-1
759 // unless region[0]==0 in which case return 0
766 result = region[0] - 1 - hiddenBeforeCol;
772 return result; // return the shifted position after removing hidden
776 LOCK.readLock().unlock();
781 * Find the visible column which is a given visible number of columns to the
782 * left of another visible column. i.e. for a startColumn x, the column which
783 * is distance 1 away will be column x-1.
785 * @param visibleDistance
786 * the number of visible columns to offset by
788 * the column to start from
789 * @return the position of the column in the visible alignment
791 public int subtractVisibleColumns(int visibleDistance, int startColumn)
795 LOCK.readLock().lock();
796 int distance = visibleDistance;
798 // in case startColumn is in a hidden region, move it to the left
799 int start = visibleToAbsoluteColumn(absoluteToVisibleColumn(startColumn));
801 Iterator<int[]> it = new ReverseRegionsIterator(0, start,
804 while (it.hasNext() && (distance > 0))
806 int[] region = it.next();
808 if (start > region[1])
810 // subtract the gap to right of region from distance
811 if (start - region[1] <= distance)
813 distance -= start - region[1];
814 start = region[0] - 1;
818 start = start - distance;
823 return start - distance;
827 LOCK.readLock().unlock();
832 * This method returns the rightmost limit of a region of an alignment with
833 * hidden columns. In otherwords, the next hidden column.
836 * the absolute (visible) alignmentPosition to find the next hidden
838 * @return the index of the next hidden column, or alPos if there is no next
841 public int getHiddenBoundaryRight(int alPos)
845 LOCK.readLock().lock();
846 if (!hiddenColumns.isEmpty())
848 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
849 if (index < hiddenColumns.size())
851 int[] region = hiddenColumns.get(index);
852 if (alPos < region[0])
856 else if ((alPos <= region[1])
857 && (index + 1 < hiddenColumns.size()))
859 // alPos is within a hidden region, return the next one
861 region = hiddenColumns.get(index + 1);
869 LOCK.readLock().unlock();
874 * This method returns the leftmost limit of a region of an alignment with
875 * hidden columns. In otherwords, the previous hidden column.
878 * the absolute (visible) alignmentPosition to find the previous
881 public int getHiddenBoundaryLeft(int alPos)
885 LOCK.readLock().lock();
887 if (!hiddenColumns.isEmpty())
889 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
893 int[] region = hiddenColumns.get(index - 1);
900 LOCK.readLock().unlock();
906 * Answers if a column in the alignment is visible
909 * absolute position of column in the alignment
910 * @return true if column is visible
912 public boolean isVisible(int column)
916 LOCK.readLock().lock();
918 int regionindex = cursor.findRegionForColumn(column).getRegionIndex();
919 if (regionindex > -1 && regionindex < hiddenColumns.size())
921 int[] region = hiddenColumns.get(regionindex);
922 // already know that column <= region[1] as cursor returns containing
923 // region or region to right
924 if (column >= region[0])
933 LOCK.readLock().unlock();
938 * Get the visible sections of a set of sequences
941 * sequence position to start from
943 * sequence position to end at
945 * an array of sequences
946 * @return an array of strings encoding the visible parts of each sequence
948 public String[] getVisibleSequenceStrings(int start, int end,
953 LOCK.readLock().lock();
954 int iSize = seqs.length;
955 String[] selections = new String[iSize];
956 if (!hiddenColumns.isEmpty())
958 for (int i = 0; i < iSize; i++)
960 StringBuffer visibleSeq = new StringBuffer();
962 Iterator<int[]> blocks = new VisibleContigsIterator(start,
963 end + 1, hiddenColumns);
965 while (blocks.hasNext())
967 int[] block = blocks.next();
968 if (blocks.hasNext())
971 .append(seqs[i].getSequence(block[0], block[1] + 1));
976 .append(seqs[i].getSequence(block[0], block[1]));
980 selections[i] = visibleSeq.toString();
985 for (int i = 0; i < iSize; i++)
987 selections[i] = seqs[i].getSequenceAsString(start, end);
994 LOCK.readLock().unlock();
999 * Locate the first position visible for this sequence. If seq isn't visible
1000 * then return the position of the left side of the hidden boundary region.
1003 * sequence to find position for
1004 * @return visible start position
1006 public int locateVisibleStartOfSequence(SequenceI seq)
1010 LOCK.readLock().lock();
1013 if (hiddenColumns.isEmpty())
1015 return seq.findIndex(seq.getStart()) - 1;
1018 // Simply walk along the sequence whilst watching for hidden column
1020 Iterator<int[]> regions = hiddenColumns.iterator();
1021 int hideStart = seq.getLength();
1025 boolean foundStart = false;
1027 // step through the non-gapped positions of the sequence
1028 for (int i = seq.getStart(); i <= seq.getEnd() && (!foundStart); i++)
1030 // get alignment position of this residue in the sequence
1031 int p = seq.findIndex(i) - 1;
1033 // update hidden region start/end
1034 while (hideEnd < p && regions.hasNext())
1036 int[] region = regions.next();
1038 visNext += region[0] - visPrev;
1039 hideStart = region[0];
1040 hideEnd = region[1];
1044 hideStart = seq.getLength();
1046 // update visible boundary for sequence
1056 return absoluteToVisibleColumn(start);
1058 // otherwise, sequence was completely hidden
1062 LOCK.readLock().unlock();
1067 * delete any columns in alignmentAnnotation that are hidden (including
1068 * sequence associated annotation).
1070 * @param alignmentAnnotation
1072 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
1074 if (alignmentAnnotation != null
1075 && alignmentAnnotation.annotations != null)
1077 makeVisibleAnnotation(0, alignmentAnnotation.annotations.length,
1078 alignmentAnnotation);
1083 * delete any columns in alignmentAnnotation that are hidden (including
1084 * sequence associated annotation).
1087 * remove any annotation to the right of this column
1089 * remove any annotation to the left of this column
1090 * @param alignmentAnnotation
1091 * the annotation to operate on
1093 public void makeVisibleAnnotation(int start, int end,
1094 AlignmentAnnotation alignmentAnnotation)
1098 LOCK.readLock().lock();
1100 int startFrom = start;
1103 if (alignmentAnnotation != null
1104 && alignmentAnnotation.annotations != null)
1106 if (!hiddenColumns.isEmpty())
1108 removeHiddenAnnotation(startFrom, endAt, alignmentAnnotation);
1112 alignmentAnnotation.restrict(startFrom, endAt);
1117 LOCK.readLock().unlock();
1121 private void removeHiddenAnnotation(int start, int end,
1122 AlignmentAnnotation alignmentAnnotation)
1124 // mangle the alignmentAnnotation annotation array
1125 ArrayList<Annotation[]> annels = new ArrayList<>();
1126 Annotation[] els = null;
1130 Iterator<int[]> blocks = new VisibleContigsIterator(start, end + 1,
1134 int annotationLength;
1135 while (blocks.hasNext())
1137 int[] block = blocks.next();
1138 annotationLength = block[1] - block[0] + 1;
1140 if (blocks.hasNext())
1142 // copy just the visible segment of the annotation row
1143 copylength = annotationLength;
1147 if (annotationLength + block[0] <= alignmentAnnotation.annotations.length)
1149 // copy just the visible segment of the annotation row
1150 copylength = annotationLength;
1154 // copy to the end of the annotation row
1155 copylength = alignmentAnnotation.annotations.length - block[0];
1159 els = new Annotation[annotationLength];
1161 System.arraycopy(alignmentAnnotation.annotations, block[0], els, 0,
1163 w += annotationLength;
1168 alignmentAnnotation.annotations = new Annotation[w];
1171 for (Annotation[] chnk : annels)
1173 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1182 * @return true if there are columns hidden
1184 public boolean hasHiddenColumns()
1188 LOCK.readLock().lock();
1190 // we don't use getSize()>0 here because it has to iterate over
1191 // the full hiddenColumns collection and so will be much slower
1192 return (!hiddenColumns.isEmpty());
1195 LOCK.readLock().unlock();
1201 * @return true if there is more than one hidden column region
1203 public boolean hasMultiHiddenColumnRegions()
1207 LOCK.readLock().lock();
1208 return !hiddenColumns.isEmpty() && hiddenColumns.size() > 1;
1211 LOCK.readLock().unlock();
1217 * Returns a hashCode built from hidden column ranges
1220 public int hashCode()
1224 LOCK.readLock().lock();
1226 Iterator<int[]> it = hiddenColumns.iterator();
1227 while (it.hasNext())
1229 int[] hidden = it.next();
1230 hashCode = HASH_MULTIPLIER * hashCode + hidden[0];
1231 hashCode = HASH_MULTIPLIER * hashCode + hidden[1];
1236 LOCK.readLock().unlock();
1241 * Hide columns corresponding to the marked bits
1244 * - columns map to bits starting from zero
1246 public void hideColumns(BitSet inserts)
1250 LOCK.writeLock().lock();
1251 for (int firstSet = inserts
1252 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1253 .nextSetBit(lastSet))
1255 lastSet = inserts.nextClearBit(firstSet);
1256 hideColumns(firstSet, lastSet - 1);
1258 cursor.resetCursor(hiddenColumns);
1262 LOCK.writeLock().unlock();
1269 * BitSet where hidden columns will be marked
1271 public void markHiddenRegions(BitSet inserts)
1275 LOCK.readLock().lock();
1277 Iterator<int[]> it = hiddenColumns.iterator();
1278 while (it.hasNext())
1280 int[] range = it.next();
1281 inserts.set(range[0], range[1] + 1);
1285 LOCK.readLock().unlock();
1290 * Calculate the visible start and end index of an alignment.
1293 * full alignment width
1294 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1296 public int[] getVisibleStartAndEndIndex(int width)
1300 LOCK.readLock().lock();
1301 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1302 int startPos = alignmentStartEnd[0];
1303 int endPos = alignmentStartEnd[1];
1305 int[] lowestRange = new int[] { -1, -1 };
1306 int[] higestRange = new int[] { -1, -1 };
1308 if (hiddenColumns.isEmpty())
1310 return new int[] { startPos, endPos };
1313 Iterator<int[]> it = hiddenColumns.iterator();
1314 while (it.hasNext())
1316 int[] range = it.next();
1317 lowestRange = (range[0] <= startPos) ? range : lowestRange;
1318 higestRange = (range[1] >= endPos) ? range : higestRange;
1321 if (lowestRange[0] == -1) // includes (lowestRange[1] == -1)
1323 startPos = alignmentStartEnd[0];
1327 startPos = lowestRange[1] + 1;
1330 if (higestRange[0] == -1) // includes (higestRange[1] == -1)
1332 endPos = alignmentStartEnd[1];
1336 endPos = higestRange[0] - 1;
1338 return new int[] { startPos, endPos };
1341 LOCK.readLock().unlock();
1346 * Finds the hidden region (if any) which starts or ends at res
1349 * visible residue position, unadjusted for hidden columns
1350 * @return region as [start,end] or null if no matching region is found. If
1351 * res is adjacent to two regions, returns the left region.
1353 public int[] getRegionWithEdgeAtRes(int res)
1357 LOCK.readLock().lock();
1358 int adjres = visibleToAbsoluteColumn(res);
1360 int[] reveal = null;
1362 if (!hiddenColumns.isEmpty())
1364 // look for a region ending just before adjres
1365 int regionindex = cursor.findRegionForColumn(adjres - 1)
1367 if (regionindex < hiddenColumns.size()
1368 && hiddenColumns.get(regionindex)[1] == adjres - 1)
1370 reveal = hiddenColumns.get(regionindex);
1372 // check if the region ends just after adjres
1373 else if (regionindex < hiddenColumns.size()
1374 && hiddenColumns.get(regionindex)[0] == adjres + 1)
1376 reveal = hiddenColumns.get(regionindex);
1383 LOCK.readLock().unlock();
1388 * Return an iterator over the hidden regions
1390 public Iterator<int[]> iterator()
1394 LOCK.readLock().lock();
1395 return new HiddenColsIterator(hiddenColumns);
1398 LOCK.readLock().unlock();
1403 * Return a bounded iterator over the hidden regions
1406 * position to start from (inclusive, absolute column position)
1408 * position to end at (inclusive, absolute column position)
1411 public Iterator<int[]> getBoundedIterator(int start, int end)
1415 LOCK.readLock().lock();
1416 return new HiddenColsIterator(start, end, hiddenColumns);
1419 LOCK.readLock().unlock();
1424 * Return a bounded iterator over the *visible* start positions of hidden
1428 * position to start from (inclusive, visible column position)
1430 * position to end at (inclusive, visible column position)
1432 public Iterator<Integer> getBoundedStartIterator(int start, int end)
1436 LOCK.readLock().lock();
1438 // get absolute position of column in alignment
1439 int absoluteStart = visibleToAbsoluteColumn(start);
1441 // Get cursor position and supply it to the iterator:
1442 // Since we want visible region start, we look for a cursor for the
1443 // (absoluteStart-1), then if absoluteStart is the start of a visible
1444 // region we'll get the cursor pointing to the region before, which is
1446 HiddenCursorPosition pos = cursor
1447 .findRegionForColumn(absoluteStart - 1);
1449 return new BoundedStartRegionIterator(pos, start, end,
1453 LOCK.readLock().unlock();
1458 * Return an iterator over visible *columns* (not regions) between the given
1459 * start and end boundaries
1462 * first column (inclusive)
1464 * last column (inclusive)
1466 public Iterator<Integer> getVisibleColsIterator(int start, int end)
1470 LOCK.readLock().lock();
1471 return new VisibleColsIterator(start, end, hiddenColumns);
1474 LOCK.readLock().unlock();
1479 * return an iterator over visible segments between the given start and end
1483 * first column, inclusive from 0
1485 * last column - not inclusive
1486 * @param useVisibleCoords
1487 * if true, start and end are visible column positions, not absolute
1490 public VisibleContigsIterator getVisContigsIterator(int start,
1492 boolean useVisibleCoords)
1494 int adjstart = start;
1496 if (useVisibleCoords)
1498 adjstart = visibleToAbsoluteColumn(start);
1499 adjend = visibleToAbsoluteColumn(end);
1504 LOCK.readLock().lock();
1505 return new VisibleContigsIterator(adjstart, adjend, hiddenColumns);
1508 LOCK.readLock().unlock();