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 jalview.util.Comparison;
25 import java.util.ArrayList;
26 import java.util.BitSet;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.NoSuchElementException;
30 import java.util.Vector;
31 import java.util.concurrent.locks.ReentrantReadWriteLock;
33 public class HiddenColumns
35 private static final int HASH_MULTIPLIER = 31;
37 private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock();
40 * list of hidden column [start, end] ranges; the list is maintained in
41 * ascending start column order
43 private ArrayList<int[]> hiddenColumns;
48 public HiddenColumns()
57 public HiddenColumns(HiddenColumns copy)
61 LOCK.writeLock().lock();
64 if (copy.hiddenColumns != null)
66 hiddenColumns = copy.copyHiddenRegionsToArrayList(0);
71 LOCK.writeLock().unlock();
76 * Copy constructor within bounds and with offset. Copies hidden column
77 * regions fully contained between start and end, and offsets positions by
81 * HiddenColumns instance to copy from
83 * lower bound to copy from
85 * upper bound to copy to
87 * offset to subtract from each region boundary position
90 public HiddenColumns(HiddenColumns copy, int start, int end, int offset)
94 LOCK.writeLock().lock();
97 hiddenColumns = new ArrayList<>();
98 Iterator<int[]> it = copy.getBoundedIterator(start, end);
101 int[] region = it.next();
102 // still need to check boundaries because iterator returns
103 // all overlapping regions and we need contained regions
104 if (region[0] >= start && region[1] <= end)
108 { region[0] - offset, region[1] - offset });
114 LOCK.writeLock().unlock();
119 * Output regions data as a string. String is in the format:
120 * reg0[0]<between>reg0[1]<delimiter>reg1[0]<between>reg1[1] ... regn[1]
123 * string to delimit regions
124 * @param betweenstring
125 * to put between start and end region values
126 * @return regions formatted according to delimiter and between strings
128 public String regionsToString(String delimiter, String between)
132 LOCK.readLock().lock();
133 StringBuilder regionBuilder = new StringBuilder();
134 if (hiddenColumns != null)
136 for (int[] range : hiddenColumns)
138 regionBuilder.append(delimiter).append(range[0]).append(between)
142 regionBuilder.deleteCharAt(0);
144 return regionBuilder.toString();
147 LOCK.readLock().unlock();
152 * Find the number of hidden columns
154 * @return number of hidden columns
160 LOCK.readLock().lock();
162 if (hasHiddenColumns())
164 for (int[] range : hiddenColumns)
166 size += range[1] - range[0] + 1;
172 LOCK.readLock().unlock();
177 * Get the number of distinct hidden regions
179 * @return number of regions
181 public int getNumberOfRegions()
185 LOCK.readLock().lock();
187 if (hasHiddenColumns())
189 num = hiddenColumns.size();
194 LOCK.readLock().unlock();
199 public boolean equals(Object obj)
203 LOCK.readLock().lock();
205 if (!(obj instanceof HiddenColumns))
209 HiddenColumns that = (HiddenColumns) obj;
212 * check hidden columns are either both null, or match
214 if (this.hiddenColumns == null)
216 return (that.hiddenColumns == null);
218 if (that.hiddenColumns == null
219 || that.hiddenColumns.size() != this.hiddenColumns.size())
224 for (int[] thisRange : hiddenColumns)
226 int[] thatRange = that.hiddenColumns.get(i++);
227 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
235 LOCK.readLock().unlock();
240 * Return absolute column index for a visible column index
243 * int column index in alignment view (count from zero)
244 * @return alignment column index for column
246 public int adjustForHiddenColumns(int column)
250 LOCK.readLock().lock();
252 if (hiddenColumns != null)
254 for (int i = 0; i < hiddenColumns.size(); i++)
256 int[] region = hiddenColumns.get(i);
257 if (result >= region[0])
259 result += region[1] - region[0] + 1;
266 LOCK.readLock().unlock();
271 * Use this method to find out where a column will appear in the visible
272 * alignment when hidden columns exist. If the column is not visible, then the
273 * left-most visible column will always be returned.
275 * @param hiddenColumn
276 * the column index in the full alignment including hidden columns
277 * @return the position of the column in the visible alignment
279 public int findColumnPosition(int hiddenColumn)
283 LOCK.readLock().lock();
284 int result = hiddenColumn;
285 if (hiddenColumns != null)
291 region = hiddenColumns.get(index++);
292 if (hiddenColumn > region[1])
294 result -= region[1] + 1 - region[0];
296 } while ((hiddenColumn > region[1])
297 && (index < hiddenColumns.size()));
299 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
301 // Here the hidden column is within a region, so
302 // we want to return the position of region[0]-1, adjusted for any
303 // earlier hidden columns.
304 // Calculate the difference between the actual hidden col position
305 // and region[0]-1, and then subtract from result to convert result
307 // the adjusted hiddenColumn value to the adjusted region[0]-1 value
309 // However, if the region begins at 0 we cannot return region[0]-1
317 return result - (hiddenColumn - region[0] + 1);
321 return result; // return the shifted position after removing hidden
325 LOCK.readLock().unlock();
330 * Find the visible column which is a given visible number of columns to the
331 * left of another visible column. i.e. for a startColumn x, the column which
332 * is distance 1 away will be column x-1.
334 * @param visibleDistance
335 * the number of visible columns to offset by
337 * the column to start from
338 * @return the position of the column in the visible alignment
340 public int subtractVisibleColumns(int visibleDistance, int startColumn)
345 LOCK.readLock().lock();
346 int distance = visibleDistance;
348 // in case startColumn is in a hidden region, move it to the left
349 int start = adjustForHiddenColumns(findColumnPosition(startColumn));
351 // get index of hidden region to left of start
352 int index = getHiddenIndexLeft(start);
355 // no hidden regions to left of startColumn
356 return start - distance;
359 // walk backwards through the alignment subtracting the counts of visible
360 // columns from distance
363 int nextstart = start;
365 while ((index > -1) && (distance - gap > 0))
367 // subtract the gap to right of region from distance
371 // calculate the next gap
372 region = hiddenColumns.get(index);
373 gap = start - region[1];
375 // set start to just to left of current region
376 nextstart = region[0] - 1;
380 if (distance - gap > 0)
382 // fell out of loop because there are no more hidden regions
384 return nextstart - distance;
386 return start - distance;
389 LOCK.readLock().unlock();
397 * This method returns the rightmost limit of a region of an alignment with
398 * hidden columns. In otherwords, the next hidden column.
403 public int getHiddenBoundaryRight(int alPos)
407 LOCK.readLock().lock();
408 if (hiddenColumns != null)
413 int[] region = hiddenColumns.get(index);
414 if (alPos < region[0])
420 } while (index < hiddenColumns.size());
426 LOCK.readLock().unlock();
432 * This method returns the leftmost limit of a region of an alignment with
433 * hidden columns. In otherwords, the previous hidden column.
438 public int getHiddenBoundaryLeft(int alPos)
442 LOCK.readLock().lock();
444 if (hiddenColumns != null)
446 int index = hiddenColumns.size() - 1;
449 int[] region = hiddenColumns.get(index);
450 if (alPos > region[1])
456 } while (index > -1);
462 LOCK.readLock().unlock();
467 * This method returns the index of the hidden region to the left of a column
468 * position. If the column is in a hidden region it returns the index of the
469 * region to the left. If there is no hidden region to the left it returns -1.
474 private int getHiddenIndexLeft(int pos)
479 LOCK.readLock().lock();
480 if (hiddenColumns != null)
482 int index = hiddenColumns.size() - 1;
485 int[] region = hiddenColumns.get(index);
492 } while (index > -1);
498 LOCK.readLock().unlock();
504 * Adds the specified column range to the hidden columns
509 public void hideColumns(int start, int end)
511 boolean wasAlreadyLocked = false;
514 // check if the write lock was already locked by this thread,
515 // as this method can be called internally in loops within HiddenColumns
516 if (!LOCK.isWriteLockedByCurrentThread())
518 LOCK.writeLock().lock();
522 wasAlreadyLocked = true;
525 if (hiddenColumns == null)
527 hiddenColumns = new ArrayList<>();
531 * traverse existing hidden ranges and insert / amend / append as
534 for (int i = 0; i < hiddenColumns.size(); i++)
536 int[] region = hiddenColumns.get(i);
538 if (end < region[0] - 1)
541 * insert discontiguous preceding range
543 hiddenColumns.add(i, new int[] { start, end });
547 if (end <= region[1])
550 * new range overlaps existing, or is contiguous preceding it - adjust
553 region[0] = Math.min(region[0], start);
557 if (start <= region[1] + 1)
560 * new range overlaps existing, or is contiguous following it - adjust
561 * start and end columns
563 region[0] = Math.min(region[0], start);
564 region[1] = Math.max(region[1], end);
567 * also update or remove any subsequent ranges
568 * that are overlapped
570 while (i < hiddenColumns.size() - 1)
572 int[] nextRegion = hiddenColumns.get(i + 1);
573 if (nextRegion[0] > end + 1)
576 * gap to next hidden range - no more to update
580 region[1] = Math.max(nextRegion[1], end);
581 hiddenColumns.remove(i + 1);
588 * remaining case is that the new range follows everything else
590 hiddenColumns.add(new int[] { start, end });
593 if (!wasAlreadyLocked)
595 LOCK.writeLock().unlock();
600 public boolean isVisible(int column)
604 LOCK.readLock().lock();
606 if (hiddenColumns != null)
608 for (int[] region : hiddenColumns)
610 if (column >= region[0] && column <= region[1])
620 LOCK.readLock().unlock();
624 private ArrayList<int[]> copyHiddenRegionsToArrayList(int startIndex)
627 if (hiddenColumns != null)
629 size = hiddenColumns.size();
631 ArrayList<int[]> copy = new ArrayList<>(size);
633 for (int i = startIndex, j = size; i < j; i++)
637 rh = hiddenColumns.get(i);
640 cp = new int[rh.length];
641 System.arraycopy(rh, 0, cp, 0, rh.length);
649 public String[] getVisibleSequenceStrings(int start, int end,
654 LOCK.readLock().lock();
655 int iSize = seqs.length;
656 String[] selections = new String[iSize];
657 if (hiddenColumns != null && hiddenColumns.size() > 0)
659 for (int i = 0; i < iSize; i++)
661 StringBuffer visibleSeq = new StringBuffer();
663 Iterator<int[]> blocks = new VisibleBlocksIterator(start,
665 while (blocks.hasNext())
667 int[] block = blocks.next();
668 visibleSeq.append(seqs[i].getSequence(block[0], block[1]));
671 selections[i] = visibleSeq.toString();
676 for (int i = 0; i < iSize; i++)
678 selections[i] = seqs[i].getSequenceAsString(start, end);
685 LOCK.readLock().unlock();
690 * Locate the first and last position visible for this sequence. if seq isn't
691 * visible then return the position of the left and right of the hidden
692 * boundary region, and the corresponding alignment column indices for the
693 * extent of the sequence
696 * @return int[] { visible start, visible end, first seqpos, last seqpos,
697 * alignment index for seq start, alignment index for seq end }
699 public int[] locateVisibleBoundsOfSequence(SequenceI seq)
703 LOCK.readLock().lock();
704 int fpos = seq.getStart();
705 int lpos = seq.getEnd();
708 if (hiddenColumns == null || hiddenColumns.size() == 0)
710 int ifpos = seq.findIndex(fpos) - 1;
711 int ilpos = seq.findIndex(lpos) - 1;
712 return new int[] { ifpos, ifpos, ilpos };
715 // Simply walk along the sequence whilst watching for hidden column
717 Iterator<int[]> regions = iterator();
719 int hideStart = seq.getLength();
725 boolean foundStart = false;
726 for (int p = 0, pLen = seq.getLength(); spos <= seq.getEnd()
729 if (!Comparison.isGap(seq.getCharAt(p)))
731 // keep track of first/last column
732 // containing sequence data regardless of visibility
738 // update hidden region start/end
739 while (hideEnd < p && regions.hasNext())
741 int[] region = regions.next();
743 visNext += region[0] - visPrev;
744 hideStart = region[0];
749 hideStart = seq.getLength();
751 // update visible boundary for sequence
762 // look for next sequence position
768 return new int[] { findColumnPosition(start), firstP, lastP };
770 // otherwise, sequence was completely hidden
771 return new int[] { visPrev, firstP, lastP };
774 LOCK.readLock().unlock();
779 * delete any columns in alignmentAnnotation that are hidden (including
780 * sequence associated annotation).
782 * @param alignmentAnnotation
784 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
786 makeVisibleAnnotation(0, alignmentAnnotation.annotations.length,
787 alignmentAnnotation);
791 * delete any columns in alignmentAnnotation that are hidden (including
792 * sequence associated annotation).
795 * remove any annotation to the right of this column
797 * remove any annotation to the left of this column
798 * @param alignmentAnnotation
799 * the annotation to operate on
801 public void makeVisibleAnnotation(int start, int end,
802 AlignmentAnnotation alignmentAnnotation)
806 LOCK.readLock().lock();
808 int startFrom = start;
811 if (alignmentAnnotation.annotations != null)
813 if (hiddenColumns != null && hiddenColumns.size() > 0)
815 removeHiddenAnnotation(startFrom, endAt, alignmentAnnotation);
819 alignmentAnnotation.restrict(startFrom, endAt);
824 LOCK.readLock().unlock();
828 private void removeHiddenAnnotation(int start, int end,
829 AlignmentAnnotation alignmentAnnotation)
831 // mangle the alignmentAnnotation annotation array
832 Vector<Annotation[]> annels = new Vector<>();
833 Annotation[] els = null;
837 Iterator<int[]> blocks = new VisibleBlocksIterator(start, end,
840 int annotationLength;
841 while (blocks.hasNext())
843 int[] block = blocks.next();
845 if (blocks.hasNext())
847 annotationLength = block[1] - block[0];
848 // copy just the visible segment of the annotation row
849 copylength = annotationLength;
853 annotationLength = block[1] - block[0] + 1;
854 if (annotationLength + block[0] <= alignmentAnnotation.annotations.length)
856 // copy just the visible segment of the annotation row
857 copylength = annotationLength;
861 // copy to the end of the annotation row
862 copylength = alignmentAnnotation.annotations.length - block[0];
866 els = new Annotation[annotationLength];
867 annels.addElement(els);
868 System.arraycopy(alignmentAnnotation.annotations, block[0], els, 0,
870 w += annotationLength;
875 alignmentAnnotation.annotations = new Annotation[w];
878 for (Annotation[] chnk : annels)
880 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
889 * @return true if there are columns hidden
891 public boolean hasHiddenColumns()
895 LOCK.readLock().lock();
896 return hiddenColumns != null && hiddenColumns.size() > 0;
899 LOCK.readLock().unlock();
905 * @return true if there are more than one set of columns hidden
907 public boolean hasManyHiddenColumns()
911 LOCK.readLock().lock();
912 return hiddenColumns != null && hiddenColumns.size() > 1;
915 LOCK.readLock().unlock();
920 * mark the columns corresponding to gap characters as hidden in the column
925 public void hideInsertionsFor(SequenceI sr)
929 LOCK.writeLock().lock();
930 List<int[]> inserts = sr.getInsertions();
931 for (int[] r : inserts)
933 hideColumns(r[0], r[1]);
937 LOCK.writeLock().unlock();
942 * Unhides, and adds to the selection list, all hidden columns
944 public void revealAllHiddenColumns(ColumnSelection sel)
948 LOCK.writeLock().lock();
949 if (hiddenColumns != null)
951 for (int i = 0; i < hiddenColumns.size(); i++)
953 int[] region = hiddenColumns.get(i);
954 for (int j = region[0]; j < region[1] + 1; j++)
961 hiddenColumns = null;
964 LOCK.writeLock().unlock();
969 * Reveals, and marks as selected, the hidden column range with the given
974 public void revealHiddenColumns(int start, ColumnSelection sel)
978 LOCK.writeLock().lock();
979 for (int i = 0; i < hiddenColumns.size(); i++)
981 int[] region = hiddenColumns.get(i);
982 if (start == region[0])
984 for (int j = region[0]; j < region[1] + 1; j++)
989 hiddenColumns.remove(region);
993 if (hiddenColumns.size() == 0)
995 hiddenColumns = null;
999 LOCK.writeLock().unlock();
1004 * Add gaps into the sequences aligned to profileseq under the given
1009 * - alignment to have gaps inserted into it
1011 * - alignment view where sequence corresponding to profileseq is
1013 * @return new HiddenColumns for new alignment view, with insertions into
1014 * profileseq marked as hidden.
1016 public static HiddenColumns propagateInsertions(SequenceI profileseq,
1017 AlignmentI al, AlignmentView input)
1021 char gc = al.getGapCharacter();
1022 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
1023 HiddenColumns nview = (HiddenColumns) alandhidden[1];
1024 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
1025 nview.propagateInsertions(profileseq, al, origseq);
1032 * - sequence in al which corresponds to origseq
1034 * - alignment which is to have gaps inserted into it
1036 * - sequence corresponding to profileseq which defines gap map for
1039 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
1044 LOCK.writeLock().lock();
1046 char gc = al.getGapCharacter();
1048 // take the set of hidden columns, and the set of gaps in origseq,
1049 // and remove all the hidden gaps from hiddenColumns
1051 // first get the gaps as a Bitset
1052 BitSet gaps = origseq.gapBitset();
1054 // now calculate hidden ^ not(gap)
1055 BitSet hidden = new BitSet();
1056 markHiddenRegions(hidden);
1057 hidden.andNot(gaps);
1058 hiddenColumns = null;
1059 this.hideMarkedBits(hidden);
1061 // for each sequence in the alignment, except the profile sequence,
1062 // insert gaps corresponding to each hidden region
1063 // but where each hidden column region is shifted backwards by the number
1065 // preceding visible gaps
1066 // update hidden columns at the same time
1067 Iterator<int[]> regions = iterator();
1068 ArrayList<int[]> newhidden = new ArrayList<>();
1070 int numGapsBefore = 0;
1071 int gapPosition = 0;
1072 while (regions.hasNext())
1074 // get region coordinates accounting for gaps
1075 // we can rely on gaps not being *in* hidden regions because we already
1077 int[] region = regions.next();
1078 while (gapPosition < region[0])
1081 if (gaps.get(gapPosition))
1087 int left = region[0] - numGapsBefore;
1088 int right = region[1] - numGapsBefore;
1089 newhidden.add(new int[] { left, right });
1091 // make a string with number of gaps = length of hidden region
1092 StringBuffer sb = new StringBuffer();
1093 for (int s = 0; s < right - left + 1; s++)
1097 padGaps(sb, left, profileseq, al);
1100 hiddenColumns = newhidden;
1103 LOCK.writeLock().unlock();
1108 * Pad gaps in all sequences in alignment except profileseq
1111 * gap string to insert
1113 * position to insert at
1115 * sequence not to pad
1117 * alignment to pad sequences in
1119 private void padGaps(StringBuffer sb, int pos, SequenceI profileseq,
1122 // loop over the sequences and pad with gaps where required
1123 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1125 SequenceI sqobj = al.getSequenceAt(s);
1126 if (sqobj != profileseq)
1128 String sq = al.getSequenceAt(s).getSequenceAsString();
1129 if (sq.length() <= pos)
1132 int diff = pos - sq.length() - 1;
1137 while ((diff = pos - sq.length() - 1) > 0)
1139 if (diff >= sb.length())
1141 sq += sb.toString();
1145 char[] buf = new char[diff];
1146 sb.getChars(0, diff, buf, 0);
1147 sq += buf.toString();
1151 sq += sb.toString();
1155 al.getSequenceAt(s).setSequence(
1156 sq.substring(0, pos) + sb.toString() + sq.substring(pos));
1163 * Returns a hashCode built from hidden column ranges
1166 public int hashCode()
1170 LOCK.readLock().lock();
1172 if (hiddenColumns != null)
1174 for (int[] hidden : hiddenColumns)
1176 hashCode = HASH_MULTIPLIER * hashCode + hidden[0];
1177 hashCode = HASH_MULTIPLIER * hashCode + hidden[1];
1183 LOCK.readLock().unlock();
1188 * Hide columns corresponding to the marked bits
1191 * - columns map to bits starting from zero
1193 public void hideMarkedBits(BitSet inserts)
1197 LOCK.writeLock().lock();
1198 for (int firstSet = inserts
1199 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1200 .nextSetBit(lastSet))
1202 lastSet = inserts.nextClearBit(firstSet);
1203 hideColumns(firstSet, lastSet - 1);
1207 LOCK.writeLock().unlock();
1214 * BitSet where hidden columns will be marked
1216 public void markHiddenRegions(BitSet inserts)
1220 LOCK.readLock().lock();
1221 if (hiddenColumns == null)
1225 for (int[] range : hiddenColumns)
1227 inserts.set(range[0], range[1] + 1);
1231 LOCK.readLock().unlock();
1236 * Calculate the visible start and end index of an alignment.
1239 * full alignment width
1240 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1242 public int[] getVisibleStartAndEndIndex(int width)
1246 LOCK.readLock().lock();
1247 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1248 int startPos = alignmentStartEnd[0];
1249 int endPos = alignmentStartEnd[1];
1251 int[] lowestRange = new int[] { -1, -1 };
1252 int[] higestRange = new int[] { -1, -1 };
1254 if (hiddenColumns == null)
1256 return new int[] { startPos, endPos };
1259 for (int[] hiddenCol : hiddenColumns)
1261 lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
1262 higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
1265 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1267 startPos = alignmentStartEnd[0];
1271 startPos = lowestRange[1] + 1;
1274 if (higestRange[0] == -1 && higestRange[1] == -1)
1276 endPos = alignmentStartEnd[1];
1280 endPos = higestRange[0] - 1;
1282 return new int[] { startPos, endPos };
1285 LOCK.readLock().unlock();
1291 * Finds the hidden region (if any) which starts or ends at res
1294 * visible residue position, unadjusted for hidden columns
1295 * @return region as [start,end] or null if no matching region is found
1297 public int[] getRegionWithEdgeAtRes(int res)
1301 LOCK.readLock().lock();
1302 int adjres = adjustForHiddenColumns(res);
1304 int[] reveal = null;
1305 if (hiddenColumns != null)
1307 for (int[] region : hiddenColumns)
1309 if (adjres + 1 == region[0] || adjres - 1 == region[1])
1319 LOCK.readLock().unlock();
1324 * Return an iterator over the hidden regions
1326 public Iterator<int[]> iterator()
1328 if (hiddenColumns != null)
1330 int last = hiddenColumns.get(hiddenColumns.size() - 1)[1];
1331 return new BoundedHiddenColsIterator(0, last, true);
1335 return new BoundedHiddenColsIterator(0, 0, true);
1340 * Return a bounded iterator over the hidden regions
1343 * position to start from (inclusive, absolute column position)
1345 * position to end at (inclusive, absolute column position)
1348 public Iterator<int[]> getBoundedIterator(int start, int end)
1350 return new BoundedHiddenColsIterator(start, end, true);
1354 * Return a bounded iterator over the *visible* start positions of hidden
1358 * position to start from (inclusive, visible column position)
1360 * position to end at (inclusive, visible column position)
1362 public Iterator<Integer> getBoundedStartIterator(int start, int end)
1364 return new BoundedStartRegionIterator(start, end, true);
1368 * Return an iterator over visible columns between the given start and end
1372 * first column (inclusive)
1374 * last column (inclusive)
1376 public Iterator<Integer> getVisibleColsIterator(int start, int end)
1378 return new VisibleColsIterator(start, end, true);
1382 * return an iterator over visible segments between the given start and end
1386 * (first column inclusive from 0)
1388 * (last column - not inclusive)
1390 public Iterator<int[]> getVisContigsIterator(int start, int end)
1392 return new VisibleContigsIterator(start, end, true);
1396 * return an iterator over visible segments between the given start and end
1400 * (first column - inclusive from 0)
1402 * (last column - inclusive)
1403 * @param useVisibleCoords
1404 * if true, start and end are visible column positions, not absolute
1407 public Iterator<int[]> getVisibleBlocksIterator(int start, int end,
1408 boolean useVisibleCoords)
1410 if (useVisibleCoords)
1413 // we should really just convert start and end here with
1414 // adjustForHiddenColumns
1415 // and then create a VisibleBlocksIterator
1416 // but without a cursor this will be horribly slow in some situations
1417 // ... so until then...
1418 return new VisibleBlocksVisBoundsIterator(start, end, true);
1422 return new VisibleBlocksIterator(start, end, true);
1427 * An iterator which iterates over hidden column regions in a range.
1429 private class BoundedHiddenColsIterator implements Iterator<int[]>
1431 private int start; // start position to iterate from
1433 private int end; // end position to iterate to
1435 // current index in hiddenColumns
1436 private int currentPosition = 0;
1438 // current column in hiddenColumns
1439 private int[] currentRegion;
1441 // whether to make a local copy of hiddenColumns
1442 private final boolean useCopy;
1444 // local copy or reference to hiddenColumns
1445 private List<int[]> localHidden;
1448 * Construct an iterator over hiddenColums bounded at
1449 * [lowerBound,upperBound]
1452 * lower bound to iterate from
1454 * upper bound to iterate to
1455 * @param useCopyCols
1456 * whether to make a local copy of hiddenColumns for iteration (set
1457 * to true if calling from outwith the HiddenColumns class)
1459 BoundedHiddenColsIterator(int lowerBound, int upperBound,
1460 boolean useCopyCols)
1464 useCopy = useCopyCols;
1470 // assume that if useCopy is false the calling code has locked
1472 LOCK.readLock().lock();
1475 if (hiddenColumns != null)
1477 localHidden = new ArrayList<>();
1479 // iterate until a region overlaps with [start,end]
1481 while ((i < hiddenColumns.size())
1482 && (hiddenColumns.get(i)[1] < start))
1487 // iterate from start to end, adding each hidden region. Positions are
1488 // absolute, and all regions which *overlap* [start,end] are added.
1489 while (i < hiddenColumns.size()
1490 && (hiddenColumns.get(i)[0] <= end))
1494 rh = hiddenColumns.get(i);
1497 cp = new int[rh.length];
1498 System.arraycopy(rh, 0, cp, 0, rh.length);
1499 localHidden.add(cp);
1509 LOCK.readLock().unlock();
1515 public boolean hasNext()
1517 return (localHidden != null)
1518 && (currentPosition < localHidden.size());
1524 currentRegion = localHidden.get(currentPosition);
1526 return currentRegion;
1531 * An iterator which iterates over visible start positions of hidden column
1532 * regions in a range.
1534 private class BoundedStartRegionIterator implements Iterator<Integer>
1536 // start position to iterate from
1539 // end position to iterate to
1542 // current index in hiddenColumns
1543 private int currentPosition = 0;
1545 // local copy or reference to hiddenColumns
1546 private List<Integer> positions = null;
1549 * Construct an iterator over hiddenColums bounded at
1550 * [lowerBound,upperBound]
1553 * lower bound to iterate from
1555 * upper bound to iterate to
1556 * @param useCopyCols
1557 * whether to make a local copy of hiddenColumns for iteration (set
1558 * to true if calling from outwith the HiddenColumns class)
1560 BoundedStartRegionIterator(int lowerBound, int upperBound,
1570 // assume that if useCopy is false the calling code has locked
1572 LOCK.readLock().lock();
1575 if (hiddenColumns != null)
1577 positions = new ArrayList<>(hiddenColumns.size());
1579 // navigate to start, keeping count of hidden columns
1581 int hiddenSoFar = 0;
1582 while ((i < hiddenColumns.size())
1583 && (hiddenColumns.get(i)[0] < start + hiddenSoFar))
1585 int[] region = hiddenColumns.get(i);
1586 hiddenSoFar += region[1] - region[0] + 1;
1590 // iterate from start to end, adding start positions of each
1591 // hidden region. Positions are visible columns count, not absolute
1592 while (i < hiddenColumns.size()
1593 && (hiddenColumns.get(i)[0] <= end + hiddenSoFar))
1595 int[] region = hiddenColumns.get(i);
1596 positions.add(region[0] - hiddenSoFar);
1597 hiddenSoFar += region[1] - region[0] + 1;
1603 positions = new ArrayList<>();
1609 LOCK.readLock().unlock();
1615 public boolean hasNext()
1617 return (currentPosition < positions.size());
1621 * Get next hidden region start position
1623 * @return the start position in *visible* coordinates
1626 public Integer next()
1628 int result = positions.get(currentPosition);
1634 private class VisibleColsIterator implements Iterator<Integer>
1638 private int current;
1642 private List<int[]> localHidden = new ArrayList<>();
1644 private int lasthiddenregion;
1646 VisibleColsIterator(int firstcol, int lastcol, boolean useCopy)
1651 lasthiddenregion = -1;
1657 // assume that if useCopy is false the calling code has locked
1659 LOCK.readLock().lock();
1662 if (hiddenColumns != null)
1665 for (i = 0; i < hiddenColumns.size()
1666 && (current < hiddenColumns.get(i)[0]); ++i)
1668 if (current >= hiddenColumns.get(i)[0]
1669 && current <= hiddenColumns.get(i)[1])
1671 // current is hidden, move to right
1672 current = hiddenColumns.get(i)[1] + 1;
1677 lasthiddenregion = i - 1;
1679 for (i = hiddenColumns.size() - 1; i >= 0
1680 && (last > hiddenColumns.get(i)[1]); --i)
1682 if (last >= hiddenColumns.get(i)[0]
1683 && last <= hiddenColumns.get(i)[1])
1685 // last is hidden, move to left
1686 last = hiddenColumns.get(i)[0] - 1;
1690 // make a local copy of the bit we need
1691 i = lasthiddenregion + 1;
1692 while (i < hiddenColumns.size()
1693 && hiddenColumns.get(i)[0] <= last)
1695 int[] region = new int[] { hiddenColumns.get(i)[0],
1696 hiddenColumns.get(i)[1] };
1697 localHidden.add(region);
1700 lasthiddenregion = -1;
1706 LOCK.readLock().unlock();
1712 public boolean hasNext()
1714 return next <= last;
1718 public Integer next()
1722 throw new NoSuchElementException();
1725 if ((localHidden != null)
1726 && (lasthiddenregion + 1 < localHidden.size()))
1728 // still some more hidden regions
1729 if (next + 1 < localHidden.get(lasthiddenregion + 1)[0])
1731 // next+1 is still before the next hidden region
1734 else if ((next + 1 >= localHidden.get(lasthiddenregion + 1)[0])
1735 && (next + 1 <= localHidden.get(lasthiddenregion + 1)[1]))
1737 // next + 1 is in the next hidden region
1738 next = localHidden.get(lasthiddenregion + 1)[1] + 1;
1744 // finished with hidden regions, just increment normally
1751 public void remove()
1753 throw new UnsupportedOperationException();
1758 * An iterator which iterates over visible regions in a range.
1760 private class VisibleContigsIterator implements Iterator<int[]>
1762 private List<int[]> vcontigs = new ArrayList<>();
1764 private int currentPosition = 0;
1766 VisibleContigsIterator(int start, int end, boolean usecopy)
1772 LOCK.readLock().lock();
1775 if (hiddenColumns != null && hiddenColumns.size() > 0)
1781 for (int[] region : hiddenColumns)
1783 hideStart = region[0];
1784 hideEnd = region[1];
1786 // navigate to start
1787 if (hideEnd < vstart)
1791 if (hideStart > vstart)
1793 int[] contig = new int[] { vstart, hideStart - 1 };
1794 vcontigs.add(contig);
1796 vstart = hideEnd + 1;
1798 // exit if we're past the end
1807 int[] contig = new int[] { vstart, end - 1 };
1808 vcontigs.add(contig);
1813 int[] contig = new int[] { start, end - 1 };
1814 vcontigs.add(contig);
1820 LOCK.readLock().unlock();
1826 public boolean hasNext()
1828 return (currentPosition < vcontigs.size());
1834 int[] result = vcontigs.get(currentPosition);
1841 * An iterator which iterates over visible regions in a range.
1843 private class VisibleBlocksIterator implements Iterator<int[]>
1845 private List<int[]> vcontigs = new ArrayList<>();
1847 private int currentPosition = 0;
1849 VisibleBlocksIterator(int start, int end, boolean usecopy)
1855 LOCK.readLock().lock();
1858 if (hiddenColumns != null && hiddenColumns.size() > 0)
1860 int blockStart = start;
1863 // iterate until a region overlaps with [start,end]
1865 while ((i < hiddenColumns.size())
1866 && (hiddenColumns.get(i)[1] < start))
1871 // iterate from start to end, adding each hidden region. Positions are
1872 // absolute, and all regions which *overlap* [start,end] are used.
1873 while (i < hiddenColumns.size()
1874 && (hiddenColumns.get(i)[0] <= end))
1876 int[] region = hiddenColumns.get(i);
1878 blockStart = Math.min(blockStart, region[1] + 1);
1879 blockEnd = Math.min(blockEnd, region[0]);
1881 int[] contig = new int[] { blockStart, blockEnd };
1882 vcontigs.add(contig);
1884 blockStart = region[1] + 1;
1890 if (end > blockStart)
1892 int[] contig = new int[] { blockStart, end };
1893 vcontigs.add(contig);
1898 int[] contig = new int[] { start, end };
1899 vcontigs.add(contig);
1905 LOCK.readLock().unlock();
1911 public boolean hasNext()
1913 return (currentPosition < vcontigs.size());
1919 int[] result = vcontigs.get(currentPosition);
1926 * An iterator which iterates over visible regions in a range as visible
1929 public class VisibleBlocksVisBoundsIterator implements Iterator<int[]>
1931 private List<int[]> vcontigs = new ArrayList<>();
1933 private int currentPosition = 0;
1935 private boolean endsAtHidden = false;
1937 VisibleBlocksVisBoundsIterator(int start, int end, boolean usecopy)
1943 LOCK.readLock().lock();
1946 if (hiddenColumns != null && hiddenColumns.size() > 0)
1948 int blockStart = start;
1950 int hiddenSoFar = 0;
1952 int maxVisible = end - start;
1953 endsAtHidden = false;
1955 // iterate until a region begins within (start,end]
1957 while ((i < hiddenColumns.size())
1958 && (hiddenColumns.get(i)[0] <= blockStart + hiddenSoFar))
1960 hiddenSoFar += hiddenColumns.get(i)[1] - hiddenColumns.get(i)[0]
1965 blockStart += hiddenSoFar; // convert start to absolute position
1966 blockEnd += hiddenSoFar; // convert end too in
1968 // iterate from start to end, adding each hidden region. Positions are
1969 // absolute, and all regions which *overlap* [start,end] are used.
1970 while (i < hiddenColumns.size()
1971 && (hiddenColumns.get(i)[0] <= blockEnd))
1973 int[] region = hiddenColumns.get(i);
1975 blockEnd = Math.min(blockEnd, region[0] - 1);
1977 int[] contig = new int[] { blockStart, blockEnd };
1978 vcontigs.add(contig);
1980 visSoFar += blockEnd - blockStart + 1;
1981 blockStart = region[1] + 1;
1983 hiddenSoFar += region[1] - region[0] + 1;
1984 blockEnd = end + hiddenSoFar;
1988 if (visSoFar < maxVisible)
1990 blockEnd = blockStart + maxVisible - visSoFar;
1991 int[] contig = new int[] { blockStart, blockEnd };
1992 vcontigs.add(contig);
1994 if (i < hiddenColumns.size()
1995 && hiddenColumns.get(i)[0] - 1 == blockEnd)
1997 endsAtHidden = true;
2003 // there are no hidden columns, return a single visible contig
2004 int[] contig = new int[] { start, end };
2005 vcontigs.add(contig);
2006 endsAtHidden = false;
2012 LOCK.readLock().unlock();
2018 public boolean hasNext()
2020 return (currentPosition < vcontigs.size());
2026 int[] result = vcontigs.get(currentPosition);
2031 public boolean endsAtHidden()
2033 return endsAtHidden;