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.NoSuchElementException;
28 import java.util.Vector;
29 import java.util.concurrent.locks.ReentrantReadWriteLock;
31 public class HiddenColumns
33 private static final int HASH_MULTIPLIER = 31;
35 private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock();
38 * list of hidden column [start, end] ranges; the list is maintained in
39 * ascending start column order
41 private ArrayList<int[]> hiddenColumns;
46 public HiddenColumns()
55 public HiddenColumns(HiddenColumns copy)
59 LOCK.writeLock().lock();
62 if (copy.hiddenColumns != null)
64 hiddenColumns = copy.copyHiddenRegionsToArrayList(0);
69 LOCK.writeLock().unlock();
74 * Copy constructor within bounds and with offset. Copies hidden column
75 * regions fully contained between start and end, and offsets positions by
79 * HiddenColumns instance to copy from
81 * lower bound to copy from
83 * upper bound to copy to
85 * offset to subtract from each region boundary position
88 public HiddenColumns(HiddenColumns copy, int start, int end, int offset)
92 LOCK.writeLock().lock();
95 hiddenColumns = new ArrayList<>();
96 Iterator<int[]> it = copy.getBoundedIterator(start, end);
99 int[] region = it.next();
100 // still need to check boundaries because iterator returns
101 // all overlapping regions and we need contained regions
102 if (region[0] >= start && region[1] <= end)
106 { region[0] - offset, region[1] - offset });
112 LOCK.writeLock().unlock();
117 * Output regions data as a string. String is in the format:
118 * reg0[0]<between>reg0[1]<delimiter>reg1[0]<between>reg1[1] ... regn[1]
121 * string to delimit regions
122 * @param betweenstring
123 * to put between start and end region values
124 * @return regions formatted according to delimiter and between strings
126 public String regionsToString(String delimiter, String between)
130 LOCK.readLock().lock();
131 StringBuilder regionBuilder = new StringBuilder();
132 if (hiddenColumns != null)
134 for (int[] range : hiddenColumns)
136 regionBuilder.append(delimiter).append(range[0]).append(between)
140 regionBuilder.deleteCharAt(0);
142 return regionBuilder.toString();
145 LOCK.readLock().unlock();
150 * Find the number of hidden columns
152 * @return number of hidden columns
158 LOCK.readLock().lock();
160 if (hasHiddenColumns())
162 for (int[] range : hiddenColumns)
164 size += range[1] - range[0] + 1;
170 LOCK.readLock().unlock();
175 * Get the number of distinct hidden regions
177 * @return number of regions
179 public int getNumberOfRegions()
183 LOCK.readLock().lock();
185 if (hasHiddenColumns())
187 num = hiddenColumns.size();
192 LOCK.readLock().unlock();
197 public boolean equals(Object obj)
201 LOCK.readLock().lock();
203 if (!(obj instanceof HiddenColumns))
207 HiddenColumns that = (HiddenColumns) obj;
210 * check hidden columns are either both null, or match
212 if (this.hiddenColumns == null)
214 return (that.hiddenColumns == null);
216 if (that.hiddenColumns == null
217 || that.hiddenColumns.size() != this.hiddenColumns.size())
222 for (int[] thisRange : hiddenColumns)
224 int[] thatRange = that.hiddenColumns.get(i++);
225 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
233 LOCK.readLock().unlock();
238 * Return absolute column index for a visible column index
241 * int column index in alignment view (count from zero)
242 * @return alignment column index for column
244 public int adjustForHiddenColumns(int column)
248 LOCK.readLock().lock();
250 if (hiddenColumns != null)
252 for (int i = 0; i < hiddenColumns.size(); i++)
254 int[] region = hiddenColumns.get(i);
255 if (result >= region[0])
257 result += region[1] - region[0] + 1;
264 LOCK.readLock().unlock();
269 * Use this method to find out where a column will appear in the visible
270 * alignment when hidden columns exist. If the column is not visible, then the
271 * left-most visible column will always be returned.
273 * @param hiddenColumn
274 * the column index in the full alignment including hidden columns
275 * @return the position of the column in the visible alignment
277 public int findColumnPosition(int hiddenColumn)
281 LOCK.readLock().lock();
282 int result = hiddenColumn;
283 if (hiddenColumns != null)
289 region = hiddenColumns.get(index++);
290 if (hiddenColumn > region[1])
292 result -= region[1] + 1 - region[0];
294 } while ((hiddenColumn > region[1])
295 && (index < hiddenColumns.size()));
297 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
299 // Here the hidden column is within a region, so
300 // we want to return the position of region[0]-1, adjusted for any
301 // earlier hidden columns.
302 // Calculate the difference between the actual hidden col position
303 // and region[0]-1, and then subtract from result to convert result
305 // the adjusted hiddenColumn value to the adjusted region[0]-1 value
307 // However, if the region begins at 0 we cannot return region[0]-1
315 return result - (hiddenColumn - region[0] + 1);
319 return result; // return the shifted position after removing hidden
323 LOCK.readLock().unlock();
328 * Find the visible column which is a given visible number of columns to the
329 * left of another visible column. i.e. for a startColumn x, the column which
330 * is distance 1 away will be column x-1.
332 * @param visibleDistance
333 * the number of visible columns to offset by
335 * the column to start from
336 * @return the position of the column in the visible alignment
338 public int subtractVisibleColumns(int visibleDistance, int startColumn)
343 LOCK.readLock().lock();
344 int distance = visibleDistance;
346 // in case startColumn is in a hidden region, move it to the left
347 int start = adjustForHiddenColumns(findColumnPosition(startColumn));
349 // get index of hidden region to left of start
350 int index = getHiddenIndexLeft(start);
353 // no hidden regions to left of startColumn
354 return start - distance;
357 // walk backwards through the alignment subtracting the counts of visible
358 // columns from distance
361 int nextstart = start;
363 while ((index > -1) && (distance - gap > 0))
365 // subtract the gap to right of region from distance
369 // calculate the next gap
370 region = hiddenColumns.get(index);
371 gap = start - region[1];
373 // set start to just to left of current region
374 nextstart = region[0] - 1;
378 if (distance - gap > 0)
380 // fell out of loop because there are no more hidden regions
382 return nextstart - distance;
384 return start - distance;
387 LOCK.readLock().unlock();
395 * This method returns the rightmost limit of a region of an alignment with
396 * hidden columns. In otherwords, the next hidden column.
401 public int getHiddenBoundaryRight(int alPos)
405 LOCK.readLock().lock();
406 if (hiddenColumns != null)
411 int[] region = hiddenColumns.get(index);
412 if (alPos < region[0])
418 } while (index < hiddenColumns.size());
424 LOCK.readLock().unlock();
430 * This method returns the leftmost limit of a region of an alignment with
431 * hidden columns. In otherwords, the previous hidden column.
436 public int getHiddenBoundaryLeft(int alPos)
440 LOCK.readLock().lock();
442 if (hiddenColumns != null)
444 int index = hiddenColumns.size() - 1;
447 int[] region = hiddenColumns.get(index);
448 if (alPos > region[1])
454 } while (index > -1);
460 LOCK.readLock().unlock();
465 * This method returns the index of the hidden region to the left of a column
466 * position. If the column is in a hidden region it returns the index of the
467 * region to the left. If there is no hidden region to the left it returns -1.
472 private int getHiddenIndexLeft(int pos)
477 LOCK.readLock().lock();
478 if (hiddenColumns != null)
480 int index = hiddenColumns.size() - 1;
483 int[] region = hiddenColumns.get(index);
490 } while (index > -1);
496 LOCK.readLock().unlock();
502 * Adds the specified column range to the hidden columns
507 public void hideColumns(int start, int end)
509 boolean wasAlreadyLocked = false;
512 // check if the write lock was already locked by this thread,
513 // as this method can be called internally in loops within HiddenColumns
514 if (!LOCK.isWriteLockedByCurrentThread())
516 LOCK.writeLock().lock();
520 wasAlreadyLocked = true;
523 if (hiddenColumns == null)
525 hiddenColumns = new ArrayList<>();
529 * new range follows everything else; check first to avoid looping over whole hiddenColumns collection
531 if (hiddenColumns.isEmpty()
532 || start > hiddenColumns.get(hiddenColumns.size() - 1)[1])
534 hiddenColumns.add(new int[] { start, end });
539 * traverse existing hidden ranges and insert / amend / append as
542 boolean added = false;
543 for (int i = 0; !added && i < hiddenColumns.size(); i++)
545 int[] region = hiddenColumns.get(i);
547 if (end < region[0] - 1)
550 * insert discontiguous preceding range
552 hiddenColumns.add(i, new int[] { start, end });
555 else if (end <= region[1])
558 * new range overlaps existing, or is contiguous preceding it - adjust
561 region[0] = Math.min(region[0], start);
564 else if (start <= region[1] + 1)
567 * new range overlaps existing, or is contiguous following it - adjust
568 * start and end columns
570 region[0] = Math.min(region[0], start);
571 region[1] = Math.max(region[1], end);
574 * also update or remove any subsequent ranges
575 * that are overlapped
577 while (i < hiddenColumns.size() - 1)
579 int[] nextRegion = hiddenColumns.get(i + 1);
580 if (nextRegion[0] > end + 1)
583 * gap to next hidden range - no more to update
587 region[1] = Math.max(nextRegion[1], end);
588 hiddenColumns.remove(i + 1);
596 if (!wasAlreadyLocked)
598 LOCK.writeLock().unlock();
603 public boolean isVisible(int column)
607 LOCK.readLock().lock();
609 if (hiddenColumns != null)
611 for (int[] region : hiddenColumns)
613 if (column >= region[0] && column <= region[1])
623 LOCK.readLock().unlock();
627 private ArrayList<int[]> copyHiddenRegionsToArrayList(int startIndex)
630 if (hiddenColumns != null)
632 size = hiddenColumns.size();
634 ArrayList<int[]> copy = new ArrayList<>(size);
636 for (int i = startIndex, j = size; i < j; i++)
640 rh = hiddenColumns.get(i);
643 cp = new int[rh.length];
644 System.arraycopy(rh, 0, cp, 0, rh.length);
652 public String[] getVisibleSequenceStrings(int start, int end,
657 LOCK.readLock().lock();
658 int iSize = seqs.length;
659 String[] selections = new String[iSize];
660 if (hiddenColumns != null && hiddenColumns.size() > 0)
662 for (int i = 0; i < iSize; i++)
664 StringBuffer visibleSeq = new StringBuffer();
666 Iterator<int[]> blocks = new VisibleBlocksIterator(start,
668 while (blocks.hasNext())
670 int[] block = blocks.next();
671 visibleSeq.append(seqs[i].getSequence(block[0], block[1]));
674 selections[i] = visibleSeq.toString();
679 for (int i = 0; i < iSize; i++)
681 selections[i] = seqs[i].getSequenceAsString(start, end);
688 LOCK.readLock().unlock();
693 * Locate the first position visible for this sequence. If seq isn't visible
694 * then return the position of the left side of the hidden boundary region.
697 * sequence to find position for
698 * @return visible start position
700 public int locateVisibleStartOfSequence(SequenceI seq)
704 LOCK.readLock().lock();
707 if (hiddenColumns == null || hiddenColumns.size() == 0)
709 return seq.findIndex(seq.getStart()) - 1;
712 // Simply walk along the sequence whilst watching for hidden column
714 Iterator<int[]> regions = iterator();
715 int hideStart = seq.getLength();
719 boolean foundStart = false;
721 // step through the non-gapped positions of the sequence
722 for (int i = seq.getStart(); i <= seq.getEnd() && (!foundStart); i++)
724 // get alignment position of this residue in the sequence
725 int p = seq.findIndex(i) - 1;
727 // update hidden region start/end
728 while (hideEnd < p && regions.hasNext())
730 int[] region = regions.next();
732 visNext += region[0] - visPrev;
733 hideStart = region[0];
738 hideStart = seq.getLength();
740 // update visible boundary for sequence
750 return findColumnPosition(start);
752 // otherwise, sequence was completely hidden
756 LOCK.readLock().unlock();
761 * delete any columns in alignmentAnnotation that are hidden (including
762 * sequence associated annotation).
764 * @param alignmentAnnotation
766 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
768 makeVisibleAnnotation(0, alignmentAnnotation.annotations.length,
769 alignmentAnnotation);
773 * delete any columns in alignmentAnnotation that are hidden (including
774 * sequence associated annotation).
777 * remove any annotation to the right of this column
779 * remove any annotation to the left of this column
780 * @param alignmentAnnotation
781 * the annotation to operate on
783 public void makeVisibleAnnotation(int start, int end,
784 AlignmentAnnotation alignmentAnnotation)
788 LOCK.readLock().lock();
790 int startFrom = start;
793 if (alignmentAnnotation.annotations != null)
795 if (hiddenColumns != null && hiddenColumns.size() > 0)
797 removeHiddenAnnotation(startFrom, endAt, alignmentAnnotation);
801 alignmentAnnotation.restrict(startFrom, endAt);
806 LOCK.readLock().unlock();
810 private void removeHiddenAnnotation(int start, int end,
811 AlignmentAnnotation alignmentAnnotation)
813 // mangle the alignmentAnnotation annotation array
814 Vector<Annotation[]> annels = new Vector<>();
815 Annotation[] els = null;
819 Iterator<int[]> blocks = new VisibleBlocksIterator(start, end,
822 int annotationLength;
823 while (blocks.hasNext())
825 int[] block = blocks.next();
827 if (blocks.hasNext())
829 annotationLength = block[1] - block[0];
830 // copy just the visible segment of the annotation row
831 copylength = annotationLength;
835 annotationLength = block[1] - block[0] + 1;
836 if (annotationLength + block[0] <= alignmentAnnotation.annotations.length)
838 // copy just the visible segment of the annotation row
839 copylength = annotationLength;
843 // copy to the end of the annotation row
844 copylength = alignmentAnnotation.annotations.length - block[0];
848 els = new Annotation[annotationLength];
849 annels.addElement(els);
850 System.arraycopy(alignmentAnnotation.annotations, block[0], els, 0,
852 w += annotationLength;
857 alignmentAnnotation.annotations = new Annotation[w];
860 for (Annotation[] chnk : annels)
862 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
871 * @return true if there are columns hidden
873 public boolean hasHiddenColumns()
877 LOCK.readLock().lock();
878 return hiddenColumns != null && hiddenColumns.size() > 0;
881 LOCK.readLock().unlock();
887 * @return true if there are more than one set of columns hidden
889 public boolean hasManyHiddenColumns()
893 LOCK.readLock().lock();
894 return hiddenColumns != null && hiddenColumns.size() > 1;
897 LOCK.readLock().unlock();
902 * mark the columns corresponding to gap characters as hidden in the column
907 public void hideInsertionsFor(SequenceI sr)
911 LOCK.writeLock().lock();
912 List<int[]> inserts = sr.getInsertions();
913 for (int[] r : inserts)
915 hideColumns(r[0], r[1]);
919 LOCK.writeLock().unlock();
924 * Unhides, and adds to the selection list, all hidden columns
926 public void revealAllHiddenColumns(ColumnSelection sel)
930 LOCK.writeLock().lock();
931 if (hiddenColumns != null)
933 for (int i = 0; i < hiddenColumns.size(); i++)
935 int[] region = hiddenColumns.get(i);
936 for (int j = region[0]; j < region[1] + 1; j++)
943 hiddenColumns = null;
946 LOCK.writeLock().unlock();
951 * Reveals, and marks as selected, the hidden column range with the given
956 public void revealHiddenColumns(int start, ColumnSelection sel)
960 LOCK.writeLock().lock();
961 for (int i = 0; i < hiddenColumns.size(); i++)
963 int[] region = hiddenColumns.get(i);
964 if (start == region[0])
966 for (int j = region[0]; j < region[1] + 1; j++)
971 hiddenColumns.remove(region);
975 if (hiddenColumns.size() == 0)
977 hiddenColumns = null;
981 LOCK.writeLock().unlock();
986 * Add gaps into the sequences aligned to profileseq under the given
991 * - alignment to have gaps inserted into it
993 * - alignment view where sequence corresponding to profileseq is
995 * @return new HiddenColumns for new alignment view, with insertions into
996 * profileseq marked as hidden.
998 public static HiddenColumns propagateInsertions(SequenceI profileseq,
999 AlignmentI al, AlignmentView input)
1003 char gc = al.getGapCharacter();
1004 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
1005 HiddenColumns nview = (HiddenColumns) alandhidden[1];
1006 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
1007 nview.propagateInsertions(profileseq, al, origseq);
1014 * - sequence in al which corresponds to origseq
1016 * - alignment which is to have gaps inserted into it
1018 * - sequence corresponding to profileseq which defines gap map for
1021 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
1026 LOCK.writeLock().lock();
1028 char gc = al.getGapCharacter();
1030 // take the set of hidden columns, and the set of gaps in origseq,
1031 // and remove all the hidden gaps from hiddenColumns
1033 // first get the gaps as a Bitset
1034 BitSet gaps = origseq.gapBitset();
1036 // now calculate hidden ^ not(gap)
1037 BitSet hidden = new BitSet();
1038 markHiddenRegions(hidden);
1039 hidden.andNot(gaps);
1040 hiddenColumns = null;
1041 this.hideMarkedBits(hidden);
1043 // for each sequence in the alignment, except the profile sequence,
1044 // insert gaps corresponding to each hidden region
1045 // but where each hidden column region is shifted backwards by the number
1047 // preceding visible gaps
1048 // update hidden columns at the same time
1049 Iterator<int[]> regions = iterator();
1050 ArrayList<int[]> newhidden = new ArrayList<>();
1052 int numGapsBefore = 0;
1053 int gapPosition = 0;
1054 while (regions.hasNext())
1056 // get region coordinates accounting for gaps
1057 // we can rely on gaps not being *in* hidden regions because we already
1059 int[] region = regions.next();
1060 while (gapPosition < region[0])
1063 if (gaps.get(gapPosition))
1069 int left = region[0] - numGapsBefore;
1070 int right = region[1] - numGapsBefore;
1071 newhidden.add(new int[] { left, right });
1073 // make a string with number of gaps = length of hidden region
1074 StringBuffer sb = new StringBuffer();
1075 for (int s = 0; s < right - left + 1; s++)
1079 padGaps(sb, left, profileseq, al);
1082 hiddenColumns = newhidden;
1085 LOCK.writeLock().unlock();
1090 * Pad gaps in all sequences in alignment except profileseq
1093 * gap string to insert
1095 * position to insert at
1097 * sequence not to pad
1099 * alignment to pad sequences in
1101 private void padGaps(StringBuffer sb, int pos, SequenceI profileseq,
1104 // loop over the sequences and pad with gaps where required
1105 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1107 SequenceI sqobj = al.getSequenceAt(s);
1108 if (sqobj != profileseq)
1110 String sq = al.getSequenceAt(s).getSequenceAsString();
1111 if (sq.length() <= pos)
1114 int diff = pos - sq.length() - 1;
1119 while ((diff = pos - sq.length() - 1) > 0)
1121 if (diff >= sb.length())
1123 sq += sb.toString();
1127 char[] buf = new char[diff];
1128 sb.getChars(0, diff, buf, 0);
1129 sq += buf.toString();
1133 sq += sb.toString();
1137 al.getSequenceAt(s).setSequence(
1138 sq.substring(0, pos) + sb.toString() + sq.substring(pos));
1145 * Returns a hashCode built from hidden column ranges
1148 public int hashCode()
1152 LOCK.readLock().lock();
1154 if (hiddenColumns != null)
1156 for (int[] hidden : hiddenColumns)
1158 hashCode = HASH_MULTIPLIER * hashCode + hidden[0];
1159 hashCode = HASH_MULTIPLIER * hashCode + hidden[1];
1165 LOCK.readLock().unlock();
1170 * Hide columns corresponding to the marked bits
1173 * - columns map to bits starting from zero
1175 public void hideMarkedBits(BitSet inserts)
1179 LOCK.writeLock().lock();
1180 for (int firstSet = inserts
1181 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1182 .nextSetBit(lastSet))
1184 lastSet = inserts.nextClearBit(firstSet);
1185 hideColumns(firstSet, lastSet - 1);
1189 LOCK.writeLock().unlock();
1196 * BitSet where hidden columns will be marked
1198 public void markHiddenRegions(BitSet inserts)
1202 LOCK.readLock().lock();
1203 if (hiddenColumns == null)
1207 for (int[] range : hiddenColumns)
1209 inserts.set(range[0], range[1] + 1);
1213 LOCK.readLock().unlock();
1218 * Calculate the visible start and end index of an alignment.
1221 * full alignment width
1222 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1224 public int[] getVisibleStartAndEndIndex(int width)
1228 LOCK.readLock().lock();
1229 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1230 int startPos = alignmentStartEnd[0];
1231 int endPos = alignmentStartEnd[1];
1233 int[] lowestRange = new int[] { -1, -1 };
1234 int[] higestRange = new int[] { -1, -1 };
1236 if (hiddenColumns == null)
1238 return new int[] { startPos, endPos };
1241 for (int[] hiddenCol : hiddenColumns)
1243 lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
1244 higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
1247 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1249 startPos = alignmentStartEnd[0];
1253 startPos = lowestRange[1] + 1;
1256 if (higestRange[0] == -1 && higestRange[1] == -1)
1258 endPos = alignmentStartEnd[1];
1262 endPos = higestRange[0] - 1;
1264 return new int[] { startPos, endPos };
1267 LOCK.readLock().unlock();
1273 * Finds the hidden region (if any) which starts or ends at res
1276 * visible residue position, unadjusted for hidden columns
1277 * @return region as [start,end] or null if no matching region is found
1279 public int[] getRegionWithEdgeAtRes(int res)
1283 LOCK.readLock().lock();
1284 int adjres = adjustForHiddenColumns(res);
1286 int[] reveal = null;
1287 if (hiddenColumns != null)
1289 for (int[] region : hiddenColumns)
1291 if (adjres + 1 == region[0] || adjres - 1 == region[1])
1301 LOCK.readLock().unlock();
1306 * Return an iterator over the hidden regions
1308 public Iterator<int[]> iterator()
1310 if (hiddenColumns != null)
1312 int last = hiddenColumns.get(hiddenColumns.size() - 1)[1];
1313 return new BoundedHiddenColsIterator(0, last, true);
1317 return new BoundedHiddenColsIterator(0, 0, true);
1322 * Return a bounded iterator over the hidden regions
1325 * position to start from (inclusive, absolute column position)
1327 * position to end at (inclusive, absolute column position)
1330 public Iterator<int[]> getBoundedIterator(int start, int end)
1332 return new BoundedHiddenColsIterator(start, end, true);
1336 * Return a bounded iterator over the *visible* start positions of hidden
1340 * position to start from (inclusive, visible column position)
1342 * position to end at (inclusive, visible column position)
1344 public Iterator<Integer> getBoundedStartIterator(int start, int end)
1346 return new BoundedStartRegionIterator(start, end, true);
1350 * Return an iterator over visible columns between the given start and end
1354 * first column (inclusive)
1356 * last column (inclusive)
1358 public Iterator<Integer> getVisibleColsIterator(int start, int end)
1360 return new VisibleColsIterator(start, end, true);
1364 * return an iterator over visible segments between the given start and end
1368 * (first column inclusive from 0)
1370 * (last column - not inclusive)
1372 public Iterator<int[]> getVisContigsIterator(int start, int end)
1374 return new VisibleContigsIterator(start, end, true);
1378 * return an iterator over visible segments between the given start and end
1382 * (first column - inclusive from 0)
1384 * (last column - inclusive)
1385 * @param useVisibleCoords
1386 * if true, start and end are visible column positions, not absolute
1389 public Iterator<int[]> getVisibleBlocksIterator(int start, int end,
1390 boolean useVisibleCoords)
1392 if (useVisibleCoords)
1395 // we should really just convert start and end here with
1396 // adjustForHiddenColumns
1397 // and then create a VisibleBlocksIterator
1398 // but without a cursor this will be horribly slow in some situations
1399 // ... so until then...
1400 return new VisibleBlocksVisBoundsIterator(start, end, true);
1404 return new VisibleBlocksIterator(start, end, true);
1409 * An iterator which iterates over hidden column regions in a range.
1411 private class BoundedHiddenColsIterator implements Iterator<int[]>
1413 private int start; // start position to iterate from
1415 private int end; // end position to iterate to
1417 // current index in hiddenColumns
1418 private int currentPosition = 0;
1420 // current column in hiddenColumns
1421 private int[] currentRegion;
1423 // whether to make a local copy of hiddenColumns
1424 private final boolean useCopy;
1426 // local copy or reference to hiddenColumns
1427 private List<int[]> localHidden;
1430 * Construct an iterator over hiddenColums bounded at
1431 * [lowerBound,upperBound]
1434 * lower bound to iterate from
1436 * upper bound to iterate to
1437 * @param useCopyCols
1438 * whether to make a local copy of hiddenColumns for iteration (set
1439 * to true if calling from outwith the HiddenColumns class)
1441 BoundedHiddenColsIterator(int lowerBound, int upperBound,
1442 boolean useCopyCols)
1446 useCopy = useCopyCols;
1452 // assume that if useCopy is false the calling code has locked
1454 LOCK.readLock().lock();
1457 if (hiddenColumns != null)
1459 localHidden = new ArrayList<>();
1461 // iterate until a region overlaps with [start,end]
1463 while ((i < hiddenColumns.size())
1464 && (hiddenColumns.get(i)[1] < start))
1469 // iterate from start to end, adding each hidden region. Positions are
1470 // absolute, and all regions which *overlap* [start,end] are added.
1471 while (i < hiddenColumns.size()
1472 && (hiddenColumns.get(i)[0] <= end))
1476 rh = hiddenColumns.get(i);
1479 cp = new int[rh.length];
1480 System.arraycopy(rh, 0, cp, 0, rh.length);
1481 localHidden.add(cp);
1491 LOCK.readLock().unlock();
1497 public boolean hasNext()
1499 return (localHidden != null)
1500 && (currentPosition < localHidden.size());
1506 currentRegion = localHidden.get(currentPosition);
1508 return currentRegion;
1513 * An iterator which iterates over visible start positions of hidden column
1514 * regions in a range.
1516 private class BoundedStartRegionIterator implements Iterator<Integer>
1518 // start position to iterate from
1521 // end position to iterate to
1524 // current index in hiddenColumns
1525 private int currentPosition = 0;
1527 // local copy or reference to hiddenColumns
1528 private List<Integer> positions = null;
1531 * Construct an iterator over hiddenColums bounded at
1532 * [lowerBound,upperBound]
1535 * lower bound to iterate from
1537 * upper bound to iterate to
1538 * @param useCopyCols
1539 * whether to make a local copy of hiddenColumns for iteration (set
1540 * to true if calling from outwith the HiddenColumns class)
1542 BoundedStartRegionIterator(int lowerBound, int upperBound,
1552 // assume that if useCopy is false the calling code has locked
1554 LOCK.readLock().lock();
1557 if (hiddenColumns != null)
1559 positions = new ArrayList<>(hiddenColumns.size());
1561 // navigate to start, keeping count of hidden columns
1563 int hiddenSoFar = 0;
1564 while ((i < hiddenColumns.size())
1565 && (hiddenColumns.get(i)[0] < start + hiddenSoFar))
1567 int[] region = hiddenColumns.get(i);
1568 hiddenSoFar += region[1] - region[0] + 1;
1572 // iterate from start to end, adding start positions of each
1573 // hidden region. Positions are visible columns count, not absolute
1574 while (i < hiddenColumns.size()
1575 && (hiddenColumns.get(i)[0] <= end + hiddenSoFar))
1577 int[] region = hiddenColumns.get(i);
1578 positions.add(region[0] - hiddenSoFar);
1579 hiddenSoFar += region[1] - region[0] + 1;
1585 positions = new ArrayList<>();
1591 LOCK.readLock().unlock();
1597 public boolean hasNext()
1599 return (currentPosition < positions.size());
1603 * Get next hidden region start position
1605 * @return the start position in *visible* coordinates
1608 public Integer next()
1610 int result = positions.get(currentPosition);
1616 private class VisibleColsIterator implements Iterator<Integer>
1620 private int current;
1624 private List<int[]> localHidden = new ArrayList<>();
1626 private int nexthiddenregion;
1628 VisibleColsIterator(int firstcol, int lastcol, boolean useCopy)
1633 nexthiddenregion = 0;
1639 // assume that if useCopy is false the calling code has locked
1641 LOCK.readLock().lock();
1644 if (hiddenColumns != null)
1647 for (i = 0; i < hiddenColumns.size()
1648 && (current <= hiddenColumns.get(i)[0]); ++i)
1650 if (current >= hiddenColumns.get(i)[0]
1651 && current <= hiddenColumns.get(i)[1])
1653 // current is hidden, move to right
1654 current = hiddenColumns.get(i)[1] + 1;
1656 nexthiddenregion = i + 1;
1660 for (i = hiddenColumns.size() - 1; i >= 0
1661 && (last >= hiddenColumns.get(i)[1]); --i)
1663 if (last >= hiddenColumns.get(i)[0]
1664 && last <= hiddenColumns.get(i)[1])
1666 // last is hidden, move to left
1667 last = hiddenColumns.get(i)[0] - 1;
1671 // make a local copy of the bit we need
1672 i = nexthiddenregion;
1673 while (i < hiddenColumns.size()
1674 && hiddenColumns.get(i)[0] <= last)
1676 int[] region = new int[] { hiddenColumns.get(i)[0],
1677 hiddenColumns.get(i)[1] };
1678 localHidden.add(region);
1686 LOCK.readLock().unlock();
1692 public boolean hasNext()
1694 return next <= last;
1698 public Integer next()
1702 throw new NoSuchElementException();
1705 if ((localHidden != null)
1706 && (nexthiddenregion < localHidden.size()))
1708 // still some more hidden regions
1709 if (next + 1 < localHidden.get(nexthiddenregion)[0])
1711 // next+1 is still before the next hidden region
1714 else if ((next + 1 >= localHidden.get(nexthiddenregion)[0])
1715 && (next + 1 <= localHidden.get(nexthiddenregion)[1]))
1717 // next + 1 is in the next hidden region
1718 next = localHidden.get(nexthiddenregion)[1] + 1;
1724 // finished with hidden regions, just increment normally
1731 public void remove()
1733 throw new UnsupportedOperationException();
1738 * An iterator which iterates over visible regions in a range.
1740 private class VisibleContigsIterator implements Iterator<int[]>
1742 private List<int[]> vcontigs = new ArrayList<>();
1744 private int currentPosition = 0;
1746 VisibleContigsIterator(int start, int end, boolean usecopy)
1752 LOCK.readLock().lock();
1755 if (hiddenColumns != null && hiddenColumns.size() > 0)
1761 for (int[] region : hiddenColumns)
1763 hideStart = region[0];
1764 hideEnd = region[1];
1766 // navigate to start
1767 if (hideEnd < vstart)
1771 if (hideStart > vstart)
1773 int[] contig = new int[] { vstart, hideStart - 1 };
1774 vcontigs.add(contig);
1776 vstart = hideEnd + 1;
1778 // exit if we're past the end
1787 int[] contig = new int[] { vstart, end - 1 };
1788 vcontigs.add(contig);
1793 int[] contig = new int[] { start, end - 1 };
1794 vcontigs.add(contig);
1800 LOCK.readLock().unlock();
1806 public boolean hasNext()
1808 return (currentPosition < vcontigs.size());
1814 int[] result = vcontigs.get(currentPosition);
1821 * An iterator which iterates over visible regions in a range.
1823 private class VisibleBlocksIterator implements Iterator<int[]>
1825 private List<int[]> vcontigs = new ArrayList<>();
1827 private int currentPosition = 0;
1829 VisibleBlocksIterator(int start, int end, boolean usecopy)
1835 LOCK.readLock().lock();
1838 if (hiddenColumns != null && hiddenColumns.size() > 0)
1840 int blockStart = start;
1843 // iterate until a region overlaps with [start,end]
1845 while ((i < hiddenColumns.size())
1846 && (hiddenColumns.get(i)[1] < start))
1851 // iterate from start to end, adding each hidden region. Positions are
1852 // absolute, and all regions which *overlap* [start,end] are used.
1853 while (i < hiddenColumns.size()
1854 && (hiddenColumns.get(i)[0] <= end))
1856 int[] region = hiddenColumns.get(i);
1858 blockStart = Math.min(blockStart, region[1] + 1);
1859 blockEnd = Math.min(blockEnd, region[0]);
1861 int[] contig = new int[] { blockStart, blockEnd };
1862 vcontigs.add(contig);
1864 blockStart = region[1] + 1;
1870 if (end > blockStart)
1872 int[] contig = new int[] { blockStart, end };
1873 vcontigs.add(contig);
1878 int[] contig = new int[] { start, end };
1879 vcontigs.add(contig);
1885 LOCK.readLock().unlock();
1891 public boolean hasNext()
1893 return (currentPosition < vcontigs.size());
1899 int[] result = vcontigs.get(currentPosition);
1906 * An iterator which iterates over visible regions in a range. The range is
1907 * specified in terms of visible column positions. Provides a special
1908 * "endsAtHidden" indicator to allow callers to determine if the final visible
1909 * column is adjacent to a hidden region.
1911 public class VisibleBlocksVisBoundsIterator implements Iterator<int[]>
1913 private List<int[]> vcontigs = new ArrayList<>();
1915 private int currentPosition = 0;
1917 private boolean endsAtHidden = false;
1920 * Constructor for iterator over visible regions in a range.
1923 * start position in terms of visible column position
1925 * end position in terms of visible column position
1927 * whether to use a local copy of hidden columns
1929 VisibleBlocksVisBoundsIterator(int start, int end, boolean usecopy)
1931 /* actually this implementation always uses a local copy but this may change in future */
1936 LOCK.readLock().lock();
1939 if (hiddenColumns != null && hiddenColumns.size() > 0)
1941 int blockStart = start;
1943 int hiddenSoFar = 0;
1946 // iterate until a region begins within (start,end]
1948 while ((i < hiddenColumns.size())
1949 && (hiddenColumns.get(i)[0] <= blockStart + hiddenSoFar))
1951 hiddenSoFar += hiddenColumns.get(i)[1] - hiddenColumns.get(i)[0]
1956 blockStart += hiddenSoFar; // convert start to absolute position
1957 blockEnd += hiddenSoFar; // convert end to absolute position
1959 // iterate from start to end, adding each visible region. Positions
1961 // absolute, and all hidden regions which overlap [start,end] are
1963 while (i < hiddenColumns.size()
1964 && (hiddenColumns.get(i)[0] <= blockEnd))
1966 int[] region = hiddenColumns.get(i);
1968 // end position of this visible region is either just before the
1969 // start of the next hidden region, or the absolute position of
1970 // 'end', whichever is lowest
1971 blockEnd = Math.min(blockEnd, region[0] - 1);
1973 vcontigs.add(new int[] { blockStart, blockEnd });
1975 visSoFar += blockEnd - blockStart + 1;
1977 // next visible region starts after this hidden region
1978 blockStart = region[1] + 1;
1980 hiddenSoFar += region[1] - region[0] + 1;
1982 // reset blockEnd to absolute position of 'end', assuming we've now
1983 // passed all hidden regions before end
1984 blockEnd = end + hiddenSoFar;
1988 if (visSoFar < end - start)
1990 // the number of visible columns we've accounted for is less than
1991 // the number specified by end-start; work out the end position of
1992 // the last visible region
1993 blockEnd = blockStart + end - start - visSoFar;
1994 vcontigs.add(new int[] { blockStart, blockEnd });
1996 // if the last visible region ends at the next hidden region, set
1997 // endsAtHidden=true
1998 if (i < hiddenColumns.size()
1999 && hiddenColumns.get(i)[0] - 1 == blockEnd)
2001 endsAtHidden = true;
2007 // there are no hidden columns, return a single visible contig
2008 vcontigs.add(new int[] { start, end });
2009 endsAtHidden = false;
2015 LOCK.readLock().unlock();
2021 public boolean hasNext()
2023 return (currentPosition < vcontigs.size());
2029 int[] result = vcontigs.get(currentPosition);
2034 public boolean endsAtHidden()
2036 return endsAtHidden;