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;
29 public class HiddenColumns
31 private static final int HASH_MULTIPLIER = 31;
33 private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock();
36 * Cursor which tracks the last used hidden columns region, and the number
37 * of hidden columns up to (but not including) that region.
39 private HiddenColumnsCursor cursor = new HiddenColumnsCursor();
42 * cache of the number of hidden columns
44 private int numColumns = 0;
47 * list of hidden column [start, end] ranges; the list is maintained in
48 * ascending start column order
50 private ArrayList<int[]> hiddenColumns;
55 public HiddenColumns()
60 * Methods which change the hiddenColumns collection. These methods should
61 * use a writeLock to prevent other threads accessing the hiddenColumns
62 * collection while changes are being made. They should also reset the hidden
63 * columns cursor, and either update the hidden columns count, or set it to 0.
70 * the HiddenColumns object to copy from
72 public HiddenColumns(HiddenColumns copy)
76 LOCK.writeLock().lock();
80 if (copy.hiddenColumns != null)
82 hiddenColumns = new ArrayList<>();
83 Iterator<int[]> it = copy.iterator();
86 int[] region = it.next();
87 hiddenColumns.add(region);
88 numColumns += region[1] - region[0] + 1;
90 cursor.resetCursor(hiddenColumns);
95 LOCK.writeLock().unlock();
100 * Copy constructor within bounds and with offset. Copies hidden column
101 * regions fully contained between start and end, and offsets positions by
102 * subtracting offset.
105 * HiddenColumns instance to copy from
107 * lower bound to copy from
109 * upper bound to copy to
111 * offset to subtract from each region boundary position
114 public HiddenColumns(HiddenColumns copy, int start, int end, int offset)
118 LOCK.writeLock().lock();
121 hiddenColumns = new ArrayList<>();
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;
169 if (hiddenColumns == null)
171 hiddenColumns = new ArrayList<>();
175 * new range follows everything else; check first to avoid looping over whole hiddenColumns collection
177 if (hiddenColumns.isEmpty()
178 || start > hiddenColumns.get(hiddenColumns.size() - 1)[1])
180 hiddenColumns.add(new int[] { start, end });
185 * traverse existing hidden ranges and insert / amend / append as
188 boolean added = false;
189 for (int i = 0; !added && i < hiddenColumns.size(); i++)
191 added = insertRangeAtRegion(i, start, end);
194 if (!wasAlreadyLocked)
196 cursor.resetCursor(hiddenColumns);
198 // reset the number of columns so they will be recounted
203 if (!wasAlreadyLocked)
205 LOCK.writeLock().unlock();
211 * Insert [start, range] at the region at index i in hiddenColumns, if
217 * start of range to insert
219 * end of range to insert
220 * @return true if range was successfully inserted
222 private boolean insertRangeAtRegion(int i, int start, int end)
224 boolean added = false;
226 int[] region = hiddenColumns.get(i);
227 if (end < region[0] - 1)
230 * insert discontiguous preceding range
232 hiddenColumns.add(i, new int[] { start, end });
235 else if (end <= region[1])
238 * new range overlaps existing, or is contiguous preceding it - adjust
241 region[0] = Math.min(region[0], start);
244 else if (start <= region[1] + 1)
247 * new range overlaps existing, or is contiguous following it - adjust
248 * start and end columns
250 region[0] = Math.min(region[0], start);
251 region[1] = Math.max(region[1], end);
254 * also update or remove any subsequent ranges
255 * that are overlapped
257 while (i < hiddenColumns.size() - 1)
259 int[] nextRegion = hiddenColumns.get(i + 1);
260 if (nextRegion[0] > end + 1)
263 * gap to next hidden range - no more to update
267 region[1] = Math.max(nextRegion[1], end);
268 hiddenColumns.subList(i + 1, i + 2).clear();
276 * mark the columns corresponding to gap characters as hidden in the column
281 public void hideInsertionsFor(SequenceI sr)
285 LOCK.writeLock().lock();
286 List<int[]> inserts = sr.getInsertions();
287 for (int[] r : inserts)
289 hideColumns(r[0], r[1]);
291 cursor.resetCursor(hiddenColumns);
295 LOCK.writeLock().unlock();
300 * Unhides, and adds to the selection list, all hidden columns
302 public void revealAllHiddenColumns(ColumnSelection sel)
306 LOCK.writeLock().lock();
307 if (hiddenColumns != null)
309 Iterator<int[]> it = hiddenColumns.iterator();
312 int[] region = it.next();
313 for (int j = region[0]; j < region[1] + 1; j++)
318 hiddenColumns = null;
319 cursor.resetCursor(hiddenColumns);
324 LOCK.writeLock().unlock();
329 * Reveals, and marks as selected, the hidden column range with the given
333 * the start column to look for
335 * the column selection to add the hidden column range to
337 public void revealHiddenColumns(int start, ColumnSelection sel)
341 LOCK.writeLock().lock();
343 if (hiddenColumns != null)
345 int regionIndex = cursor.findRegionForColumn(start)
348 if (regionIndex != -1 && regionIndex != hiddenColumns.size())
350 // regionIndex is the region which either contains start
351 // or lies to the right of start
352 int[] region = hiddenColumns.get(regionIndex);
353 if (start == region[0])
355 for (int j = region[0]; j < region[1] + 1; j++)
359 int colsToRemove = region[1] - region[0] + 1;
360 hiddenColumns.remove(regionIndex);
362 if (hiddenColumns.isEmpty())
364 hiddenColumns = null;
369 numColumns -= colsToRemove;
371 cursor.updateForDeletedRegion(hiddenColumns);
377 LOCK.writeLock().unlock();
382 * Add gaps into the sequences aligned to profileseq under the given
386 * sequence in al which sequences are aligned to
388 * alignment to have gaps inserted into it
390 * alignment view where sequence corresponding to profileseq is first
392 * @return new HiddenColumns for new alignment view, with insertions into
393 * profileseq marked as hidden.
395 public static HiddenColumns propagateInsertions(SequenceI profileseq,
396 AlignmentI al, AlignmentView input)
400 char gc = al.getGapCharacter();
401 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
402 HiddenColumns nview = (HiddenColumns) alandhidden[1];
403 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
404 nview.propagateInsertions(profileseq, al, origseq);
411 * sequence in al which corresponds to origseq
413 * alignment which is to have gaps inserted into it
415 * sequence corresponding to profileseq which defines gap map for
418 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
423 LOCK.writeLock().lock();
425 char gc = al.getGapCharacter();
427 // take the set of hidden columns, and the set of gaps in origseq,
428 // and remove all the hidden gaps from hiddenColumns
430 // first get the gaps as a Bitset
431 BitSet gaps = origseq.gapBitset();
433 // now calculate hidden ^ not(gap)
434 BitSet hidden = new BitSet();
435 markHiddenRegions(hidden);
437 hiddenColumns = null;
438 this.hideMarkedBits(hidden);
440 // for each sequence in the alignment, except the profile sequence,
441 // insert gaps corresponding to each hidden region but where each hidden
442 // column region is shifted backwards by the number of preceding visible
443 // gaps update hidden columns at the same time
444 Iterator<int[]> regions = hiddenColumns.iterator();
445 ArrayList<int[]> newhidden = new ArrayList<>();
447 int numGapsBefore = 0;
449 while (regions.hasNext())
451 // get region coordinates accounting for gaps
452 // we can rely on gaps not being *in* hidden regions because we already
454 int[] region = regions.next();
455 while (gapPosition < region[0])
458 if (gaps.get(gapPosition))
464 int left = region[0] - numGapsBefore;
465 int right = region[1] - numGapsBefore;
466 newhidden.add(new int[] { left, right });
468 // make a string with number of gaps = length of hidden region
469 StringBuffer sb = new StringBuffer();
470 for (int s = 0; s < right - left + 1; s++)
474 padGaps(sb, left, profileseq, al);
477 hiddenColumns = newhidden;
478 cursor.resetCursor(hiddenColumns);
482 LOCK.writeLock().unlock();
487 * Pad gaps in all sequences in alignment except profileseq
490 * gap string to insert
492 * position to insert at
494 * sequence not to pad
496 * alignment to pad sequences in
498 private void padGaps(StringBuffer sb, int pos, SequenceI profileseq,
501 // loop over the sequences and pad with gaps where required
502 for (int s = 0, ns = al.getHeight(); s < ns; s++)
504 SequenceI sqobj = al.getSequenceAt(s);
505 if (sqobj != profileseq)
507 String sq = al.getSequenceAt(s).getSequenceAsString();
508 if (sq.length() <= pos)
511 int diff = pos - sq.length() - 1;
516 while ((diff = pos - sq.length() - 1) > 0)
518 if (diff >= sb.length())
524 char[] buf = new char[diff];
525 sb.getChars(0, diff, buf, 0);
526 sq += buf.toString();
534 al.getSequenceAt(s).setSequence(
535 sq.substring(0, pos) + sb.toString() + sq.substring(pos));
542 * Methods which only need read access to the hidden columns collection.
543 * These methods should use a readLock to prevent other threads changing
544 * the hidden columns collection while it is in use.
548 * Output regions data as a string. String is in the format:
549 * reg0[0]<between>reg0[1]<delimiter>reg1[0]<between>reg1[1] ... regn[1]
552 * string to delimit regions
553 * @param betweenstring
554 * to put between start and end region values
555 * @return regions formatted according to delimiter and between strings
557 public String regionsToString(String delimiter, String between)
561 LOCK.readLock().lock();
562 StringBuilder regionBuilder = new StringBuilder();
563 if (hiddenColumns != null)
565 Iterator<int[]> it = hiddenColumns.iterator();
568 int[] range = it.next();
569 regionBuilder.append(delimiter).append(range[0]).append(between)
573 regionBuilder.deleteCharAt(0);
577 return regionBuilder.toString();
580 LOCK.readLock().unlock();
585 * Find the number of hidden columns
587 * @return number of hidden columns
593 LOCK.readLock().lock();
595 if (numColumns == 0 && hiddenColumns != null)
597 // numColumns is out of date, so recalculate
599 if (hiddenColumns != null)
601 Iterator<int[]> it = hiddenColumns.iterator();
604 int[] range = it.next();
605 size += range[1] - range[0] + 1;
614 LOCK.readLock().unlock();
619 * Get the number of distinct hidden regions
621 * @return number of regions
623 public int getNumberOfRegions()
627 LOCK.readLock().lock();
629 if (hasHiddenColumns())
631 num = hiddenColumns.size();
636 LOCK.readLock().unlock();
641 public boolean equals(Object obj)
645 LOCK.readLock().lock();
647 if (!(obj instanceof HiddenColumns))
651 HiddenColumns that = (HiddenColumns) obj;
654 * check hidden columns are either both null, or match
656 if (this.hiddenColumns == null)
658 return (that.hiddenColumns == null);
660 if (that.hiddenColumns == null
661 || that.hiddenColumns.size() != this.hiddenColumns.size())
666 Iterator<int[]> it = hiddenColumns.iterator();
667 Iterator<int[]> thatit = that.iterator();
670 int[] thisRange = it.next();
671 int[] thatRange = thatit.next();
672 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
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 adjustForHiddenColumns(int column)
695 LOCK.readLock().lock();
698 if (hiddenColumns != null)
700 result += cursor.getHiddenOffset(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 * left-most visible column will always be returned.
715 * @param hiddenColumn
716 * the column index in the full alignment including hidden columns
717 * @return the position of the column in the visible alignment
719 public int findColumnPosition(int hiddenColumn)
723 LOCK.readLock().lock();
724 int result = hiddenColumn;
726 if (hiddenColumns != null)
728 HiddenCursorPosition cursorPos = cursor
729 .findRegionForColumn(hiddenColumn);
730 int index = cursorPos.getRegionIndex();
731 int hiddenBeforeCol = cursorPos.getHiddenSoFar();
733 // just subtract hidden cols count - this works fine if column is
735 result = hiddenColumn - hiddenBeforeCol;
737 // now check in case column is hidden - it will be in the returned
739 if (index < hiddenColumns.size())
741 int[] region = hiddenColumns.get(index);
742 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
744 // actually col is hidden, return region[0]-1
745 // unless region[0]==0 in which case return 0
752 result = region[0] - 1 - hiddenBeforeCol;
758 return result; // return the shifted position after removing hidden
762 LOCK.readLock().unlock();
767 * Find the visible column which is a given visible number of columns to the
768 * left of another visible column. i.e. for a startColumn x, the column which
769 * is distance 1 away will be column x-1.
771 * @param visibleDistance
772 * the number of visible columns to offset by
774 * the column to start from
775 * @return the position of the column in the visible alignment
777 public int subtractVisibleColumns(int visibleDistance, int startColumn)
781 LOCK.readLock().lock();
782 int distance = visibleDistance;
784 // in case startColumn is in a hidden region, move it to the left
785 int start = adjustForHiddenColumns(findColumnPosition(startColumn));
787 Iterator<int[]> it = new ReverseRegionsIterator(0, start,
790 while (it.hasNext() && (distance > 0))
792 int[] region = it.next();
794 if (start > region[1])
796 // subtract the gap to right of region from distance
797 if (start - region[1] <= distance)
799 distance -= start - region[1];
800 start = region[0] - 1;
804 start = start - distance;
809 return start - distance;
813 LOCK.readLock().unlock();
818 * This method returns the rightmost limit of a region of an alignment with
819 * hidden columns. In otherwords, the next hidden column.
822 * the absolute (visible) alignmentPosition to find the next hidden
825 public int getHiddenBoundaryRight(int alPos)
829 LOCK.readLock().lock();
830 if (hiddenColumns != null)
832 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
833 if (index < hiddenColumns.size())
835 int[] region = hiddenColumns.get(index);
836 if (alPos < region[0])
840 else if ((alPos <= region[1])
841 && (index + 1 < hiddenColumns.size()))
843 // alPos is within a hidden region, return the next one
845 region = hiddenColumns.get(index + 1);
853 LOCK.readLock().unlock();
858 * This method returns the leftmost limit of a region of an alignment with
859 * hidden columns. In otherwords, the previous hidden column.
862 * the absolute (visible) alignmentPosition to find the previous
865 public int getHiddenBoundaryLeft(int alPos)
869 LOCK.readLock().lock();
871 if (hiddenColumns != null)
873 int index = cursor.findRegionForColumn(alPos).getRegionIndex();
877 int[] region = hiddenColumns.get(index - 1);
884 LOCK.readLock().unlock();
890 * Answers if a column in the alignment is visible
893 * absolute position of column in the alignment
894 * @return true if column is visible
896 public boolean isVisible(int column)
900 LOCK.readLock().lock();
902 int regionindex = cursor.findRegionForColumn(column).getRegionIndex();
903 if (regionindex > -1 && regionindex < hiddenColumns.size())
905 int[] region = hiddenColumns.get(regionindex);
906 if (column >= region[0] && column <= region[1])
915 LOCK.readLock().unlock();
920 * Get the visible sections of a set of sequences
923 * sequence position to start from
925 * sequence position to end at
927 * an array of sequences
928 * @return an array of strings encoding the visible parts of each sequence
930 public String[] getVisibleSequenceStrings(int start, int end,
935 LOCK.readLock().lock();
936 int iSize = seqs.length;
937 String[] selections = new String[iSize];
938 if (hiddenColumns != null && hiddenColumns.size() > 0)
940 for (int i = 0; i < iSize; i++)
942 StringBuffer visibleSeq = new StringBuffer();
944 Iterator<int[]> blocks = new VisibleContigsIterator(start,
945 end + 1, hiddenColumns);
947 while (blocks.hasNext())
949 int[] block = blocks.next();
950 if (blocks.hasNext())
953 .append(seqs[i].getSequence(block[0], block[1] + 1));
958 .append(seqs[i].getSequence(block[0], block[1]));
962 selections[i] = visibleSeq.toString();
967 for (int i = 0; i < iSize; i++)
969 selections[i] = seqs[i].getSequenceAsString(start, end);
976 LOCK.readLock().unlock();
981 * Locate the first position visible for this sequence. If seq isn't visible
982 * then return the position of the left side of the hidden boundary region.
985 * sequence to find position for
986 * @return visible start position
988 public int locateVisibleStartOfSequence(SequenceI seq)
992 LOCK.readLock().lock();
995 if (hiddenColumns == null || hiddenColumns.size() == 0)
997 return seq.findIndex(seq.getStart()) - 1;
1000 // Simply walk along the sequence whilst watching for hidden column
1002 Iterator<int[]> regions = hiddenColumns.iterator();
1003 int hideStart = seq.getLength();
1007 boolean foundStart = false;
1009 // step through the non-gapped positions of the sequence
1010 for (int i = seq.getStart(); i <= seq.getEnd() && (!foundStart); i++)
1012 // get alignment position of this residue in the sequence
1013 int p = seq.findIndex(i) - 1;
1015 // update hidden region start/end
1016 while (hideEnd < p && regions.hasNext())
1018 int[] region = regions.next();
1020 visNext += region[0] - visPrev;
1021 hideStart = region[0];
1022 hideEnd = region[1];
1026 hideStart = seq.getLength();
1028 // update visible boundary for sequence
1038 return findColumnPosition(start);
1040 // otherwise, sequence was completely hidden
1044 LOCK.readLock().unlock();
1049 * delete any columns in alignmentAnnotation that are hidden (including
1050 * sequence associated annotation).
1052 * @param alignmentAnnotation
1054 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
1056 makeVisibleAnnotation(0, alignmentAnnotation.annotations.length,
1057 alignmentAnnotation);
1061 * delete any columns in alignmentAnnotation that are hidden (including
1062 * sequence associated annotation).
1065 * remove any annotation to the right of this column
1067 * remove any annotation to the left of this column
1068 * @param alignmentAnnotation
1069 * the annotation to operate on
1071 public void makeVisibleAnnotation(int start, int end,
1072 AlignmentAnnotation alignmentAnnotation)
1076 LOCK.readLock().lock();
1078 int startFrom = start;
1081 if (alignmentAnnotation.annotations != null)
1083 if (hiddenColumns != null && hiddenColumns.size() > 0)
1085 removeHiddenAnnotation(startFrom, endAt, alignmentAnnotation);
1089 alignmentAnnotation.restrict(startFrom, endAt);
1094 LOCK.readLock().unlock();
1098 private void removeHiddenAnnotation(int start, int end,
1099 AlignmentAnnotation alignmentAnnotation)
1101 // mangle the alignmentAnnotation annotation array
1102 ArrayList<Annotation[]> annels = new ArrayList<>();
1103 Annotation[] els = null;
1107 Iterator<int[]> blocks = new VisibleContigsIterator(start, end + 1,
1111 int annotationLength;
1112 while (blocks.hasNext())
1114 int[] block = blocks.next();
1115 annotationLength = block[1] - block[0] + 1;
1117 if (blocks.hasNext())
1119 // copy just the visible segment of the annotation row
1120 copylength = annotationLength;
1124 if (annotationLength + block[0] <= alignmentAnnotation.annotations.length)
1126 // copy just the visible segment of the annotation row
1127 copylength = annotationLength;
1131 // copy to the end of the annotation row
1132 copylength = alignmentAnnotation.annotations.length - block[0];
1136 els = new Annotation[annotationLength];
1138 System.arraycopy(alignmentAnnotation.annotations, block[0], els, 0,
1140 w += annotationLength;
1145 alignmentAnnotation.annotations = new Annotation[w];
1148 for (Annotation[] chnk : annels)
1150 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1159 * @return true if there are columns hidden
1161 public boolean hasHiddenColumns()
1165 LOCK.readLock().lock();
1166 return hiddenColumns != null && hiddenColumns.size() > 0;
1169 LOCK.readLock().unlock();
1175 * @return true if there are more than one set of columns hidden
1177 public boolean hasManyHiddenColumns()
1181 LOCK.readLock().lock();
1182 return hiddenColumns != null && hiddenColumns.size() > 1;
1185 LOCK.readLock().unlock();
1191 * Returns a hashCode built from hidden column ranges
1194 public int hashCode()
1198 LOCK.readLock().lock();
1200 Iterator<int[]> it = hiddenColumns.iterator();
1201 while (it.hasNext())
1203 int[] hidden = it.next();
1204 hashCode = HASH_MULTIPLIER * hashCode + hidden[0];
1205 hashCode = HASH_MULTIPLIER * hashCode + hidden[1];
1210 LOCK.readLock().unlock();
1215 * Hide columns corresponding to the marked bits
1218 * - columns map to bits starting from zero
1220 public void hideMarkedBits(BitSet inserts)
1224 LOCK.writeLock().lock();
1225 for (int firstSet = inserts
1226 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1227 .nextSetBit(lastSet))
1229 lastSet = inserts.nextClearBit(firstSet);
1230 hideColumns(firstSet, lastSet - 1);
1232 cursor.resetCursor(hiddenColumns);
1236 LOCK.writeLock().unlock();
1243 * BitSet where hidden columns will be marked
1245 public void markHiddenRegions(BitSet inserts)
1249 LOCK.readLock().lock();
1250 if (hiddenColumns == null)
1254 Iterator<int[]> it = hiddenColumns.iterator();
1255 while (it.hasNext())
1257 int[] range = it.next();
1258 inserts.set(range[0], range[1] + 1);
1262 LOCK.readLock().unlock();
1267 * Calculate the visible start and end index of an alignment.
1270 * full alignment width
1271 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1273 public int[] getVisibleStartAndEndIndex(int width)
1277 LOCK.readLock().lock();
1278 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1279 int startPos = alignmentStartEnd[0];
1280 int endPos = alignmentStartEnd[1];
1282 int[] lowestRange = new int[] { -1, -1 };
1283 int[] higestRange = new int[] { -1, -1 };
1285 if (hiddenColumns == null)
1287 return new int[] { startPos, endPos };
1290 Iterator<int[]> it = hiddenColumns.iterator();
1291 while (it.hasNext())
1293 int[] range = it.next();
1294 lowestRange = (range[0] <= startPos) ? range : lowestRange;
1295 higestRange = (range[1] >= endPos) ? range : higestRange;
1298 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1300 startPos = alignmentStartEnd[0];
1304 startPos = lowestRange[1] + 1;
1307 if (higestRange[0] == -1 && higestRange[1] == -1)
1309 endPos = alignmentStartEnd[1];
1313 endPos = higestRange[0] - 1;
1315 return new int[] { startPos, endPos };
1318 LOCK.readLock().unlock();
1323 * Finds the hidden region (if any) which starts or ends at res
1326 * visible residue position, unadjusted for hidden columns
1327 * @return region as [start,end] or null if no matching region is found
1329 public int[] getRegionWithEdgeAtRes(int res)
1333 LOCK.readLock().lock();
1334 int adjres = adjustForHiddenColumns(res);
1336 int[] reveal = null;
1338 if (hiddenColumns != null)
1340 // look for a region ending just before adjres
1341 int regionindex = cursor.findRegionForColumn(adjres - 1)
1343 if (regionindex < hiddenColumns.size()
1344 && hiddenColumns.get(regionindex)[1] == adjres - 1)
1346 reveal = hiddenColumns.get(regionindex);
1348 // check if the region ends just after adjres
1349 else if (regionindex < hiddenColumns.size()
1350 && hiddenColumns.get(regionindex)[0] == adjres + 1)
1352 reveal = hiddenColumns.get(regionindex);
1354 // or try the next region
1358 if (regionindex < hiddenColumns.size()
1359 && hiddenColumns.get(regionindex)[0] == adjres + 1)
1361 reveal = hiddenColumns.get(regionindex);
1369 LOCK.readLock().unlock();
1374 * Return an iterator over the hidden regions
1376 public Iterator<int[]> iterator()
1380 LOCK.readLock().lock();
1381 return new BoundedHiddenColsIterator(hiddenColumns);
1384 LOCK.readLock().unlock();
1389 * Return a bounded iterator over the hidden regions
1392 * position to start from (inclusive, absolute column position)
1394 * position to end at (inclusive, absolute column position)
1397 public Iterator<int[]> getBoundedIterator(int start, int end)
1401 LOCK.readLock().lock();
1402 return new BoundedHiddenColsIterator(start, end, hiddenColumns);
1405 LOCK.readLock().unlock();
1410 * Return a bounded iterator over the *visible* start positions of hidden
1414 * position to start from (inclusive, visible column position)
1416 * position to end at (inclusive, visible column position)
1418 public Iterator<Integer> getBoundedStartIterator(int start, int end)
1422 LOCK.readLock().lock();
1424 // get absolute position of column in alignment
1425 int absoluteStart = adjustForHiddenColumns(start);
1427 // Get cursor position and supply it to the iterator:
1428 // Since we want visible region start, we look for a cursor for the
1429 // (absoluteStart-1), then if absoluteStart is the start of a visible
1430 // region we'll get the cursor pointing to the region before, which is
1432 HiddenCursorPosition pos = cursor
1433 .findRegionForColumn(absoluteStart - 1);
1435 return new BoundedStartRegionIterator(pos, start, end,
1439 LOCK.readLock().unlock();
1444 * Return an iterator over visible *columns* (not regions) between the given
1445 * start and end boundaries
1448 * first column (inclusive)
1450 * last column (inclusive)
1452 public Iterator<Integer> getVisibleColsIterator(int start, int end)
1456 LOCK.readLock().lock();
1457 return new VisibleColsIterator(start, end, hiddenColumns);
1460 LOCK.readLock().unlock();
1465 * return an iterator over visible segments between the given start and end
1469 * first column, inclusive from 0, absolute value
1471 * last column - not inclusive, absolute value
1473 public Iterator<int[]> getVisContigsIterator(int start, int end)
1477 LOCK.readLock().lock();
1478 return new VisibleContigsIterator(start, end, hiddenColumns);
1481 LOCK.readLock().unlock();
1486 * return an iterator over visible segments between the given start and end
1490 * (first column - inclusive from 0)
1492 * (last column - inclusive)
1493 * @param useVisibleCoords
1494 * if true, start and end are visible column positions, not absolute
1497 public Iterator<int[]> getVisibleBlocksIterator(int start, int end,
1498 boolean useVisibleCoords)
1500 int adjstart = start;
1502 if (useVisibleCoords)
1504 adjstart = adjustForHiddenColumns(start);
1505 adjend = adjustForHiddenColumns(end);
1510 LOCK.readLock().lock();
1512 return new VisibleContigsIterator(adjstart, adjend + 1,
1516 LOCK.readLock().unlock();