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.
50 public class HiddenColumns
52 private static final int HASH_MULTIPLIER = 31;
54 private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock();
57 * Cursor which tracks the last used hidden columns region, and the number
58 * of hidden columns up to (but not including) that region.
60 private HiddenColumnsCursor cursor = new HiddenColumnsCursor();
63 * cache of the number of hidden columns
65 private int numColumns = 0;
68 * list of hidden column [start, end] ranges; the list is maintained in
69 * ascending start column order
71 private ArrayList<int[]> hiddenColumns;
76 public HiddenColumns()
81 * Methods which change the hiddenColumns collection. These methods should
82 * use a writeLock to prevent other threads accessing the hiddenColumns
83 * collection while changes are being made. They should also reset the hidden
84 * columns cursor, and either update the hidden columns count, or set it to 0
85 * (so that it will later be updated when needed).
92 * the HiddenColumns object to copy from
94 public HiddenColumns(HiddenColumns copy)
98 LOCK.writeLock().lock();
102 if (copy.hiddenColumns != null)
104 hiddenColumns = new ArrayList<>();
105 Iterator<int[]> it = copy.iterator();
108 int[] region = it.next();
109 hiddenColumns.add(region);
110 numColumns += region[1] - region[0] + 1;
112 cursor.resetCursor(hiddenColumns);
117 LOCK.writeLock().unlock();
122 * Copy constructor within bounds and with offset. Copies hidden column
123 * regions fully contained between start and end, and offsets positions by
124 * subtracting offset.
127 * HiddenColumns instance to copy from
129 * lower bound to copy from
131 * upper bound to copy to
133 * offset to subtract from each region boundary position
136 public HiddenColumns(HiddenColumns copy, int start, int end, int offset)
140 LOCK.writeLock().lock();
143 hiddenColumns = new ArrayList<>();
145 Iterator<int[]> it = copy.getBoundedIterator(start, end);
148 int[] region = it.next();
149 // still need to check boundaries because iterator returns
150 // all overlapping regions and we need contained regions
151 if (region[0] >= start && region[1] <= end)
155 { region[0] - offset, region[1] - offset });
156 numColumns += region[1] - region[0] + 1;
159 cursor.resetCursor(hiddenColumns);
163 LOCK.writeLock().unlock();
168 * Adds the specified column range to the hidden columns collection
171 * start of range to add (absolute position in alignment)
173 * end of range to add (absolute position in alignment)
175 public void hideColumns(int start, int end)
177 boolean wasAlreadyLocked = false;
180 // check if the write lock was already locked by this thread,
181 // as this method can be called internally in loops within HiddenColumns
182 if (!LOCK.isWriteLockedByCurrentThread())
184 LOCK.writeLock().lock();
188 wasAlreadyLocked = true;
191 if (hiddenColumns == null)
193 hiddenColumns = new ArrayList<>();
197 * new range follows everything else; check first to avoid looping over whole hiddenColumns collection
199 if (hiddenColumns.isEmpty()
200 || start > hiddenColumns.get(hiddenColumns.size() - 1)[1])
202 hiddenColumns.add(new int[] { start, end });
207 * traverse existing hidden ranges and insert / amend / append as
210 boolean added = false;
211 for (int i = 0; !added && i < hiddenColumns.size(); i++)
213 added = insertRangeAtRegion(i, start, end);
216 if (!wasAlreadyLocked)
218 cursor.resetCursor(hiddenColumns);
220 // reset the number of columns so they will be recounted
225 if (!wasAlreadyLocked)
227 LOCK.writeLock().unlock();
233 * Insert [start, range] at the region at index i in hiddenColumns, if
239 * start of range to insert
241 * end of range to insert
242 * @return true if range was successfully inserted
244 private boolean insertRangeAtRegion(int i, int start, int end)
246 boolean added = false;
248 int[] region = hiddenColumns.get(i);
249 if (end < region[0] - 1)
252 * insert discontiguous preceding range
254 hiddenColumns.add(i, new int[] { start, end });
257 else if (end <= region[1])
260 * new range overlaps existing, or is contiguous preceding it - adjust
263 region[0] = Math.min(region[0], start);
266 else if (start <= region[1] + 1)
269 * new range overlaps existing, or is contiguous following it - adjust
270 * start and end columns
272 region[0] = Math.min(region[0], start);
273 region[1] = Math.max(region[1], end);
276 * also update or remove any subsequent ranges
277 * that are overlapped
279 while (i < hiddenColumns.size() - 1)
281 int[] nextRegion = hiddenColumns.get(i + 1);
282 if (nextRegion[0] > end + 1)
285 * gap to next hidden range - no more to update
289 region[1] = Math.max(nextRegion[1], end);
290 hiddenColumns.subList(i + 1, i + 2).clear();
298 * mark the columns corresponding to gap characters as hidden in the column
303 public void hideInsertionsFor(SequenceI sr)
307 LOCK.writeLock().lock();
308 List<int[]> inserts = sr.getInsertions();
309 for (int[] r : inserts)
311 hideColumns(r[0], r[1]);
313 cursor.resetCursor(hiddenColumns);
317 LOCK.writeLock().unlock();
322 * Unhides, and adds to the selection list, all hidden columns
324 public void revealAllHiddenColumns(ColumnSelection sel)
328 LOCK.writeLock().lock();
329 if (hiddenColumns != null)
331 Iterator<int[]> it = hiddenColumns.iterator();
334 int[] region = it.next();
335 for (int j = region[0]; j < region[1] + 1; j++)
340 hiddenColumns = null;
341 cursor.resetCursor(hiddenColumns);
346 LOCK.writeLock().unlock();
351 * Reveals, and marks as selected, the hidden column range with the given
355 * the start column to look for
357 * the column selection to add the hidden column range to
359 public void revealHiddenColumns(int start, ColumnSelection sel)
363 LOCK.writeLock().lock();
365 if (hiddenColumns != null)
367 int regionIndex = cursor.findRegionForColumn(start)
370 if (regionIndex != -1 && regionIndex != hiddenColumns.size())
372 // regionIndex is the region which either contains start
373 // or lies to the right of start
374 int[] region = hiddenColumns.get(regionIndex);
375 if (start == region[0])
377 for (int j = region[0]; j < region[1] + 1; j++)
381 int colsToRemove = region[1] - region[0] + 1;
382 hiddenColumns.remove(regionIndex);
384 if (hiddenColumns.isEmpty())
386 hiddenColumns = null;
391 numColumns -= colsToRemove;
393 cursor.updateForDeletedRegion(hiddenColumns);
399 LOCK.writeLock().unlock();
404 * Add gaps into the sequences aligned to profileseq under the given
408 * sequence in al which sequences are aligned to
410 * alignment to have gaps inserted into it
412 * alignment view where sequence corresponding to profileseq is first
414 * @return new HiddenColumns for new alignment view, with insertions into
415 * profileseq marked as hidden.
417 public static HiddenColumns propagateInsertions(SequenceI profileseq,
418 AlignmentI al, AlignmentView input)
422 char gc = al.getGapCharacter();
423 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
424 HiddenColumns nview = (HiddenColumns) alandhidden[1];
425 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
426 nview.propagateInsertions(profileseq, al, origseq);
433 * sequence in al which corresponds to origseq
435 * alignment which is to have gaps inserted into it
437 * sequence corresponding to profileseq which defines gap map for
440 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
445 LOCK.writeLock().lock();
447 char gc = al.getGapCharacter();
449 // take the set of hidden columns, and the set of gaps in origseq,
450 // and remove all the hidden gaps from hiddenColumns
452 // first get the gaps as a Bitset
453 BitSet gaps = origseq.gapBitset();
455 // now calculate hidden ^ not(gap)
456 BitSet hidden = new BitSet();
457 markHiddenRegions(hidden);
459 hiddenColumns = null;
460 this.hideMarkedBits(hidden);
462 // for each sequence in the alignment, except the profile sequence,
463 // insert gaps corresponding to each hidden region but where each hidden
464 // column region is shifted backwards by the number of preceding visible
465 // gaps update hidden columns at the same time
466 Iterator<int[]> regions = hiddenColumns.iterator();
467 ArrayList<int[]> newhidden = new ArrayList<>();
469 int numGapsBefore = 0;
471 while (regions.hasNext())
473 // get region coordinates accounting for gaps
474 // we can rely on gaps not being *in* hidden regions because we already
476 int[] region = regions.next();
477 while (gapPosition < region[0])
480 if (gaps.get(gapPosition))
486 int left = region[0] - numGapsBefore;
487 int right = region[1] - numGapsBefore;
488 newhidden.add(new int[] { left, right });
490 // make a string with number of gaps = length of hidden region
491 StringBuffer sb = new StringBuffer();
492 for (int s = 0; s < right - left + 1; s++)
496 padGaps(sb, left, profileseq, al);
499 hiddenColumns = newhidden;
500 cursor.resetCursor(hiddenColumns);
504 LOCK.writeLock().unlock();
509 * Pad gaps in all sequences in alignment except profileseq
512 * gap string to insert
514 * position to insert at
516 * sequence not to pad
518 * alignment to pad sequences in
520 private void padGaps(StringBuffer sb, int pos, SequenceI profileseq,
523 // loop over the sequences and pad with gaps where required
524 for (int s = 0, ns = al.getHeight(); s < ns; s++)
526 SequenceI sqobj = al.getSequenceAt(s);
527 if (sqobj != profileseq)
529 String sq = al.getSequenceAt(s).getSequenceAsString();
530 if (sq.length() <= pos)
533 int diff = pos - sq.length() - 1;
538 while ((diff = pos - sq.length() - 1) > 0)
540 if (diff >= sb.length())
546 char[] buf = new char[diff];
547 sb.getChars(0, diff, buf, 0);
548 sq += buf.toString();
556 al.getSequenceAt(s).setSequence(
557 sq.substring(0, pos) + sb.toString() + sq.substring(pos));
564 * Methods which only need read access to the hidden columns collection.
565 * These methods should use a readLock to prevent other threads changing
566 * the hidden columns collection while it is in use.
570 * Output regions data as a string. String is in the format:
571 * reg0[0]<between>reg0[1]<delimiter>reg1[0]<between>reg1[1] ... regn[1]
574 * string to delimit regions
575 * @param betweenstring
576 * to put between start and end region values
577 * @return regions formatted according to delimiter and between strings
579 public String regionsToString(String delimiter, String between)
583 LOCK.readLock().lock();
584 StringBuilder regionBuilder = new StringBuilder();
585 if (hiddenColumns != null)
587 Iterator<int[]> it = hiddenColumns.iterator();
590 int[] range = it.next();
591 regionBuilder.append(delimiter).append(range[0]).append(between)
595 regionBuilder.deleteCharAt(0);
599 return regionBuilder.toString();
602 LOCK.readLock().unlock();
607 * Find the number of hidden columns
609 * @return number of hidden columns
615 LOCK.readLock().lock();
617 if (numColumns == 0 && hiddenColumns != null)
619 // numColumns is out of date, so recalculate
622 Iterator<int[]> it = hiddenColumns.iterator();
625 int[] range = it.next();
626 size += range[1] - range[0] + 1;
635 LOCK.readLock().unlock();
640 * Get the number of distinct hidden regions
642 * @return number of regions
644 public int getNumberOfRegions()
648 LOCK.readLock().lock();
650 if (hasHiddenColumns())
652 num = hiddenColumns.size();
657 LOCK.readLock().unlock();
662 public boolean equals(Object obj)
666 LOCK.readLock().lock();
668 if (!(obj instanceof HiddenColumns))
672 HiddenColumns that = (HiddenColumns) obj;
675 * check hidden columns are either both null, or match
677 if (this.hiddenColumns == null)
679 return (that.hiddenColumns == null);
681 if (that.hiddenColumns == null
682 || that.hiddenColumns.size() != this.hiddenColumns.size())
687 Iterator<int[]> it = hiddenColumns.iterator();
688 Iterator<int[]> thatit = that.iterator();
691 int[] thisRange = it.next();
692 int[] thatRange = thatit.next();
693 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
701 LOCK.readLock().unlock();
706 * Return absolute column index for a visible column index
709 * int column index in alignment view (count from zero)
710 * @return alignment column index for column
712 public int adjustForHiddenColumns(int column)
716 LOCK.readLock().lock();
719 if (hiddenColumns != null)
721 result += cursor.getHiddenOffset(column).getHiddenSoFar();
727 LOCK.readLock().unlock();
732 * Use this method to find out where a column will appear in the visible
733 * alignment when hidden columns exist. If the column is not visible, then the
734 * left-most visible column will always be returned.
736 * @param hiddenColumn
737 * the column index in the full alignment including hidden columns
738 * @return the position of the column in the visible alignment
740 public int findColumnPosition(int hiddenColumn)
744 LOCK.readLock().lock();
745 int result = hiddenColumn;
747 if (hiddenColumns != null)
749 HiddenCursorPosition cursorPos = cursor
750 .findRegionForColumn(hiddenColumn);
751 int index = cursorPos.getRegionIndex();
752 int hiddenBeforeCol = cursorPos.getHiddenSoFar();
754 // just subtract hidden cols count - this works fine if column is
756 result = hiddenColumn - hiddenBeforeCol;
758 // now check in case column is hidden - it will be in the returned
760 if (index < hiddenColumns.size())
762 int[] region = hiddenColumns.get(index);
763 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
765 // actually col is hidden, return region[0]-1
766 // unless region[0]==0 in which case return 0
773 result = region[0] - 1 - hiddenBeforeCol;
779 return result; // return the shifted position after removing hidden
783 LOCK.readLock().unlock();
788 * Find the visible column which is a given visible number of columns to the
789 * left of another visible column. i.e. for a startColumn x, the column which
790 * is distance 1 away will be column x-1.
792 * @param visibleDistance
793 * the number of visible columns to offset by
795 * the column to start from
796 * @return the position of the column in the visible alignment
798 public int subtractVisibleColumns(int visibleDistance, int startColumn)
802 LOCK.readLock().lock();
803 int distance = visibleDistance;
805 // in case startColumn is in a hidden region, move it to the left
806 int start = adjustForHiddenColumns(findColumnPosition(startColumn));
808 Iterator<int[]> it = new ReverseRegionsIterator(0, start,
811 while (it.hasNext() && (distance > 0))
813 int[] region = it.next();
815 if (start > region[1])
817 // subtract the gap to right of region from distance
818 if (start - region[1] <= distance)
820 distance -= start - region[1];
821 start = region[0] - 1;
825 start = start - distance;
830 return start - distance;
834 LOCK.readLock().unlock();
839 * This method returns the rightmost limit of a region of an alignment with
840 * hidden columns. In otherwords, the next hidden column.
843 * the absolute (visible) alignmentPosition to find the next hidden
846 public int getHiddenBoundaryRight(int alPos)
850 LOCK.readLock().lock();
851 if (hiddenColumns != null)
853 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
854 if (index < hiddenColumns.size())
856 int[] region = hiddenColumns.get(index);
857 if (alPos < region[0])
861 else if ((alPos <= region[1])
862 && (index + 1 < hiddenColumns.size()))
864 // alPos is within a hidden region, return the next one
866 region = hiddenColumns.get(index + 1);
874 LOCK.readLock().unlock();
879 * This method returns the leftmost limit of a region of an alignment with
880 * hidden columns. In otherwords, the previous hidden column.
883 * the absolute (visible) alignmentPosition to find the previous
886 public int getHiddenBoundaryLeft(int alPos)
890 LOCK.readLock().lock();
892 if (hiddenColumns != null)
894 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
898 int[] region = hiddenColumns.get(index - 1);
905 LOCK.readLock().unlock();
911 * Answers if a column in the alignment is visible
914 * absolute position of column in the alignment
915 * @return true if column is visible
917 public boolean isVisible(int column)
921 LOCK.readLock().lock();
923 int regionindex = cursor.findRegionForColumn(column).getRegionIndex();
924 if (regionindex > -1 && regionindex < hiddenColumns.size())
926 int[] region = hiddenColumns.get(regionindex);
927 // already know that column <= region[1] as cursor returns containing
928 // region or region to right
929 if (column >= region[0])
938 LOCK.readLock().unlock();
943 * Get the visible sections of a set of sequences
946 * sequence position to start from
948 * sequence position to end at
950 * an array of sequences
951 * @return an array of strings encoding the visible parts of each sequence
953 public String[] getVisibleSequenceStrings(int start, int end,
958 LOCK.readLock().lock();
959 int iSize = seqs.length;
960 String[] selections = new String[iSize];
961 if (hiddenColumns != null && hiddenColumns.size() > 0)
963 for (int i = 0; i < iSize; i++)
965 StringBuffer visibleSeq = new StringBuffer();
967 Iterator<int[]> blocks = new VisibleContigsIterator(start,
968 end + 1, hiddenColumns);
970 while (blocks.hasNext())
972 int[] block = blocks.next();
973 if (blocks.hasNext())
976 .append(seqs[i].getSequence(block[0], block[1] + 1));
981 .append(seqs[i].getSequence(block[0], block[1]));
985 selections[i] = visibleSeq.toString();
990 for (int i = 0; i < iSize; i++)
992 selections[i] = seqs[i].getSequenceAsString(start, end);
999 LOCK.readLock().unlock();
1004 * Locate the first position visible for this sequence. If seq isn't visible
1005 * then return the position of the left side of the hidden boundary region.
1008 * sequence to find position for
1009 * @return visible start position
1011 public int locateVisibleStartOfSequence(SequenceI seq)
1015 LOCK.readLock().lock();
1018 if (hiddenColumns == null || hiddenColumns.size() == 0)
1020 return seq.findIndex(seq.getStart()) - 1;
1023 // Simply walk along the sequence whilst watching for hidden column
1025 Iterator<int[]> regions = hiddenColumns.iterator();
1026 int hideStart = seq.getLength();
1030 boolean foundStart = false;
1032 // step through the non-gapped positions of the sequence
1033 for (int i = seq.getStart(); i <= seq.getEnd() && (!foundStart); i++)
1035 // get alignment position of this residue in the sequence
1036 int p = seq.findIndex(i) - 1;
1038 // update hidden region start/end
1039 while (hideEnd < p && regions.hasNext())
1041 int[] region = regions.next();
1043 visNext += region[0] - visPrev;
1044 hideStart = region[0];
1045 hideEnd = region[1];
1049 hideStart = seq.getLength();
1051 // update visible boundary for sequence
1061 return findColumnPosition(start);
1063 // otherwise, sequence was completely hidden
1067 LOCK.readLock().unlock();
1072 * delete any columns in alignmentAnnotation that are hidden (including
1073 * sequence associated annotation).
1075 * @param alignmentAnnotation
1077 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
1079 if (alignmentAnnotation != null
1080 && alignmentAnnotation.annotations != null)
1082 makeVisibleAnnotation(0, alignmentAnnotation.annotations.length,
1083 alignmentAnnotation);
1088 * delete any columns in alignmentAnnotation that are hidden (including
1089 * sequence associated annotation).
1092 * remove any annotation to the right of this column
1094 * remove any annotation to the left of this column
1095 * @param alignmentAnnotation
1096 * the annotation to operate on
1098 public void makeVisibleAnnotation(int start, int end,
1099 AlignmentAnnotation alignmentAnnotation)
1103 LOCK.readLock().lock();
1105 int startFrom = start;
1108 if (alignmentAnnotation != null
1109 && alignmentAnnotation.annotations != null)
1111 if (hiddenColumns != null && hiddenColumns.size() > 0)
1113 removeHiddenAnnotation(startFrom, endAt, alignmentAnnotation);
1117 alignmentAnnotation.restrict(startFrom, endAt);
1122 LOCK.readLock().unlock();
1126 private void removeHiddenAnnotation(int start, int end,
1127 AlignmentAnnotation alignmentAnnotation)
1129 // mangle the alignmentAnnotation annotation array
1130 ArrayList<Annotation[]> annels = new ArrayList<>();
1131 Annotation[] els = null;
1135 Iterator<int[]> blocks = new VisibleContigsIterator(start, end + 1,
1139 int annotationLength;
1140 while (blocks.hasNext())
1142 int[] block = blocks.next();
1143 annotationLength = block[1] - block[0] + 1;
1145 if (blocks.hasNext())
1147 // copy just the visible segment of the annotation row
1148 copylength = annotationLength;
1152 if (annotationLength + block[0] <= alignmentAnnotation.annotations.length)
1154 // copy just the visible segment of the annotation row
1155 copylength = annotationLength;
1159 // copy to the end of the annotation row
1160 copylength = alignmentAnnotation.annotations.length - block[0];
1164 els = new Annotation[annotationLength];
1166 System.arraycopy(alignmentAnnotation.annotations, block[0], els, 0,
1168 w += annotationLength;
1173 alignmentAnnotation.annotations = new Annotation[w];
1176 for (Annotation[] chnk : annels)
1178 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1187 * @return true if there are columns hidden
1189 public boolean hasHiddenColumns()
1193 LOCK.readLock().lock();
1194 return hiddenColumns != null && hiddenColumns.size() > 0;
1197 LOCK.readLock().unlock();
1203 * @return true if there are more than one set of columns hidden
1205 public boolean hasManyHiddenColumns()
1209 LOCK.readLock().lock();
1210 return hiddenColumns != null && hiddenColumns.size() > 1;
1213 LOCK.readLock().unlock();
1219 * Returns a hashCode built from hidden column ranges
1222 public int hashCode()
1226 LOCK.readLock().lock();
1228 Iterator<int[]> it = hiddenColumns.iterator();
1229 while (it.hasNext())
1231 int[] hidden = it.next();
1232 hashCode = HASH_MULTIPLIER * hashCode + hidden[0];
1233 hashCode = HASH_MULTIPLIER * hashCode + hidden[1];
1238 LOCK.readLock().unlock();
1243 * Hide columns corresponding to the marked bits
1246 * - columns map to bits starting from zero
1248 public void hideMarkedBits(BitSet inserts)
1252 LOCK.writeLock().lock();
1253 for (int firstSet = inserts
1254 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1255 .nextSetBit(lastSet))
1257 lastSet = inserts.nextClearBit(firstSet);
1258 hideColumns(firstSet, lastSet - 1);
1260 cursor.resetCursor(hiddenColumns);
1264 LOCK.writeLock().unlock();
1271 * BitSet where hidden columns will be marked
1273 public void markHiddenRegions(BitSet inserts)
1277 LOCK.readLock().lock();
1278 if (hiddenColumns == null)
1282 Iterator<int[]> it = hiddenColumns.iterator();
1283 while (it.hasNext())
1285 int[] range = it.next();
1286 inserts.set(range[0], range[1] + 1);
1290 LOCK.readLock().unlock();
1295 * Calculate the visible start and end index of an alignment.
1298 * full alignment width
1299 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1301 public int[] getVisibleStartAndEndIndex(int width)
1305 LOCK.readLock().lock();
1306 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1307 int startPos = alignmentStartEnd[0];
1308 int endPos = alignmentStartEnd[1];
1310 int[] lowestRange = new int[] { -1, -1 };
1311 int[] higestRange = new int[] { -1, -1 };
1313 if (hiddenColumns == null)
1315 return new int[] { startPos, endPos };
1318 Iterator<int[]> it = hiddenColumns.iterator();
1319 while (it.hasNext())
1321 int[] range = it.next();
1322 lowestRange = (range[0] <= startPos) ? range : lowestRange;
1323 higestRange = (range[1] >= endPos) ? range : higestRange;
1326 if (lowestRange[0] == -1) // includes (lowestRange[1] == -1)
1328 startPos = alignmentStartEnd[0];
1332 startPos = lowestRange[1] + 1;
1335 if (higestRange[0] == -1) // includes (higestRange[1] == -1)
1337 endPos = alignmentStartEnd[1];
1341 endPos = higestRange[0] - 1;
1343 return new int[] { startPos, endPos };
1346 LOCK.readLock().unlock();
1351 * Finds the hidden region (if any) which starts or ends at res
1354 * visible residue position, unadjusted for hidden columns
1355 * @return region as [start,end] or null if no matching region is found
1357 public int[] getRegionWithEdgeAtRes(int res)
1361 LOCK.readLock().lock();
1362 int adjres = adjustForHiddenColumns(res);
1364 int[] reveal = null;
1366 if (hiddenColumns != null)
1368 // look for a region ending just before adjres
1369 int regionindex = cursor.findRegionForColumn(adjres - 1)
1371 if (regionindex < hiddenColumns.size()
1372 && hiddenColumns.get(regionindex)[1] == adjres - 1)
1374 reveal = hiddenColumns.get(regionindex);
1376 // check if the region ends just after adjres
1377 else if (regionindex < hiddenColumns.size()
1378 && hiddenColumns.get(regionindex)[0] == adjres + 1)
1380 reveal = hiddenColumns.get(regionindex);
1387 LOCK.readLock().unlock();
1392 * Return an iterator over the hidden regions
1394 public Iterator<int[]> iterator()
1398 LOCK.readLock().lock();
1399 return new BoundedHiddenColsIterator(hiddenColumns);
1402 LOCK.readLock().unlock();
1407 * Return a bounded iterator over the hidden regions
1410 * position to start from (inclusive, absolute column position)
1412 * position to end at (inclusive, absolute column position)
1415 public Iterator<int[]> getBoundedIterator(int start, int end)
1419 LOCK.readLock().lock();
1420 return new BoundedHiddenColsIterator(start, end, hiddenColumns);
1423 LOCK.readLock().unlock();
1428 * Return a bounded iterator over the *visible* start positions of hidden
1432 * position to start from (inclusive, visible column position)
1434 * position to end at (inclusive, visible column position)
1436 public Iterator<Integer> getBoundedStartIterator(int start, int end)
1440 LOCK.readLock().lock();
1442 // get absolute position of column in alignment
1443 int absoluteStart = adjustForHiddenColumns(start);
1445 // Get cursor position and supply it to the iterator:
1446 // Since we want visible region start, we look for a cursor for the
1447 // (absoluteStart-1), then if absoluteStart is the start of a visible
1448 // region we'll get the cursor pointing to the region before, which is
1450 HiddenCursorPosition pos = cursor
1451 .findRegionForColumn(absoluteStart - 1);
1453 return new BoundedStartRegionIterator(pos, start, end,
1457 LOCK.readLock().unlock();
1462 * Return an iterator over visible *columns* (not regions) between the given
1463 * start and end boundaries
1466 * first column (inclusive)
1468 * last column (inclusive)
1470 public Iterator<Integer> getVisibleColsIterator(int start, int end)
1474 LOCK.readLock().lock();
1475 return new VisibleColsIterator(start, end, hiddenColumns);
1478 LOCK.readLock().unlock();
1483 * return an iterator over visible segments between the given start and end
1487 * first column, inclusive from 0
1489 * last column - not inclusive
1490 * @param useVisibleCoords
1491 * if true, start and end are visible column positions, not absolute
1494 public Iterator<int[]> getVisContigsIterator(int start, int end,
1495 boolean useVisibleCoords)
1497 int adjstart = start;
1499 if (useVisibleCoords)
1501 adjstart = adjustForHiddenColumns(start);
1502 adjend = adjustForHiddenColumns(end);
1507 LOCK.readLock().lock();
1508 return new VisibleContigsIterator(adjstart, adjend, hiddenColumns);
1511 LOCK.readLock().unlock();