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();
35 private HiddenColumnsCursor cursor = new HiddenColumnsCursor();
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 = new ArrayList<>();
65 Iterator<int[]> it = copy.iterator();
68 hiddenColumns.add(it.next());
70 cursor.resetCursor(hiddenColumns);
75 LOCK.writeLock().unlock();
80 * Copy constructor within bounds and with offset. Copies hidden column
81 * regions fully contained between start and end, and offsets positions by
85 * HiddenColumns instance to copy from
87 * lower bound to copy from
89 * upper bound to copy to
91 * offset to subtract from each region boundary position
94 public HiddenColumns(HiddenColumns copy, int start, int end, int offset)
98 LOCK.writeLock().lock();
101 hiddenColumns = new ArrayList<>();
102 Iterator<int[]> it = copy.getBoundedIterator(start, end);
105 int[] region = it.next();
106 // still need to check boundaries because iterator returns
107 // all overlapping regions and we need contained regions
108 if (region[0] >= start && region[1] <= end)
112 { region[0] - offset, region[1] - offset });
115 cursor.resetCursor(hiddenColumns);
119 LOCK.writeLock().unlock();
124 * Output regions data as a string. String is in the format:
125 * reg0[0]<between>reg0[1]<delimiter>reg1[0]<between>reg1[1] ... regn[1]
128 * string to delimit regions
129 * @param betweenstring
130 * to put between start and end region values
131 * @return regions formatted according to delimiter and between strings
133 public String regionsToString(String delimiter, String between)
137 LOCK.readLock().lock();
138 StringBuilder regionBuilder = new StringBuilder();
139 if (hiddenColumns != null)
141 Iterator<int[]> it = hiddenColumns.iterator();
144 int[] range = it.next();
145 regionBuilder.append(delimiter).append(range[0]).append(between)
149 regionBuilder.deleteCharAt(0);
153 return regionBuilder.toString();
156 LOCK.readLock().unlock();
161 * Find the number of hidden columns
163 * @return number of hidden columns
169 LOCK.readLock().lock();
171 if (hiddenColumns != null)
173 Iterator<int[]> it = hiddenColumns.iterator();
176 int[] range = it.next();
177 size += range[1] - range[0] + 1;
183 LOCK.readLock().unlock();
188 * Get the number of distinct hidden regions
190 * @return number of regions
192 public int getNumberOfRegions()
196 LOCK.readLock().lock();
198 if (hasHiddenColumns())
200 num = hiddenColumns.size();
205 LOCK.readLock().unlock();
210 public boolean equals(Object obj)
214 LOCK.readLock().lock();
216 if (!(obj instanceof HiddenColumns))
220 HiddenColumns that = (HiddenColumns) obj;
223 * check hidden columns are either both null, or match
225 if (this.hiddenColumns == null)
227 return (that.hiddenColumns == null);
229 if (that.hiddenColumns == null
230 || that.hiddenColumns.size() != this.hiddenColumns.size())
235 Iterator<int[]> it = hiddenColumns.iterator();
236 Iterator<int[]> thatit = that.iterator();
239 int[] thisRange = it.next();
240 int[] thatRange = thatit.next();
241 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
249 LOCK.readLock().unlock();
254 * Return absolute column index for a visible column index
257 * int column index in alignment view (count from zero)
258 * @return alignment column index for column
260 public int adjustForHiddenColumns(int column)
264 LOCK.readLock().lock();
267 if (hiddenColumns != null)
269 result += cursor.getHiddenOffset(column);
275 LOCK.readLock().unlock();
280 * Use this method to find out where a column will appear in the visible
281 * alignment when hidden columns exist. If the column is not visible, then the
282 * left-most visible column will always be returned.
284 * @param hiddenColumn
285 * the column index in the full alignment including hidden columns
286 * @return the position of the column in the visible alignment
288 public int findColumnPosition(int hiddenColumn)
292 LOCK.readLock().lock();
293 int result = hiddenColumn;
294 // int[] region = null;
295 if (hiddenColumns != null)
297 int index = cursor.findRegionForColumn(hiddenColumn);
298 int hiddenBeforeCol = cursor.getHiddenSoFar();
300 // just subtract hidden cols count - this works fine if column is
302 result = hiddenColumn - hiddenBeforeCol;
304 // now check in case column is hidden - it will be in the returned
306 if (index < hiddenColumns.size())
308 int[] region = hiddenColumns.get(index);
309 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
311 // actually col is hidden, return region[0]-1
312 // unless region[0]==0 in which case return 0
319 result = region[0] - 1 - hiddenBeforeCol;
324 return result; // return the shifted position after removing hidden
328 LOCK.readLock().unlock();
333 * Find the visible column which is a given visible number of columns to the
334 * left of another visible column. i.e. for a startColumn x, the column which
335 * is distance 1 away will be column x-1.
337 * @param visibleDistance
338 * the number of visible columns to offset by
340 * the column to start from
341 * @return the position of the column in the visible alignment
343 public int subtractVisibleColumns(int visibleDistance, int startColumn)
347 LOCK.readLock().lock();
348 int distance = visibleDistance;
350 // in case startColumn is in a hidden region, move it to the left
351 int start = adjustForHiddenColumns(findColumnPosition(startColumn));
353 Iterator<int[]> it = new ReverseRegionsIterator(0, start,
356 while (it.hasNext() && (distance > 0))
358 int[] region = it.next();
360 if (start > region[1])
362 // subtract the gap to right of region from distance
363 if (start - region[1] <= distance)
365 distance -= start - region[1];
366 start = region[0] - 1;
370 start = start - distance;
376 return start - distance;
380 LOCK.readLock().unlock();
385 * This method returns the rightmost limit of a region of an alignment with
386 * hidden columns. In otherwords, the next hidden column.
389 * the absolute (visible) alignmentPosition to find the next hidden
392 public int getHiddenBoundaryRight(int alPos)
396 LOCK.readLock().lock();
397 if (hiddenColumns != null)
399 int index = cursor.findRegionForColumn(alPos);
400 if (index < hiddenColumns.size())
402 int[] region = hiddenColumns.get(index);
403 if (alPos < region[0])
407 else if ((alPos <= region[1])
408 && (index + 1 < hiddenColumns.size()))
410 // alPos is within a hidden region, return the next one
412 region = hiddenColumns.get(index + 1);
420 LOCK.readLock().unlock();
425 * This method returns the leftmost limit of a region of an alignment with
426 * hidden columns. In otherwords, the previous hidden column.
429 * the absolute (visible) alignmentPosition to find the previous
432 public int getHiddenBoundaryLeft(int alPos)
436 LOCK.readLock().lock();
438 if (hiddenColumns != null)
440 int index = cursor.findRegionForColumn(alPos);
444 int[] region = hiddenColumns.get(index - 1);
451 LOCK.readLock().unlock();
456 * Adds the specified column range to the hidden columns collection
459 * start of range to add (absolute position in alignment)
461 * end of range to add (absolute position in alignment)
463 public void hideColumns(int start, int end)
465 boolean wasAlreadyLocked = false;
468 // check if the write lock was already locked by this thread,
469 // as this method can be called internally in loops within HiddenColumns
470 if (!LOCK.isWriteLockedByCurrentThread())
472 LOCK.writeLock().lock();
476 wasAlreadyLocked = true;
479 if (hiddenColumns == null)
481 hiddenColumns = new ArrayList<>();
485 * new range follows everything else; check first to avoid looping over whole hiddenColumns collection
487 if (hiddenColumns.isEmpty()
488 || start > hiddenColumns.get(hiddenColumns.size() - 1)[1])
490 hiddenColumns.add(new int[] { start, end });
495 * traverse existing hidden ranges and insert / amend / append as
498 boolean added = false;
499 for (int i = 0; !added && i < hiddenColumns.size(); i++)
501 added = insertRangeAtRegion(i, start, end);
504 if (!wasAlreadyLocked)
506 cursor.resetCursor(hiddenColumns);
510 if (!wasAlreadyLocked)
512 LOCK.writeLock().unlock();
518 * Insert [start, range] at the region at index i in hiddenColumns, if
524 * start of range to insert
526 * end of range to insert
527 * @return true if range was successfully inserted
529 private boolean insertRangeAtRegion(int i, int start, int end)
531 boolean added = false;
533 int[] region = hiddenColumns.get(i);
534 if (end < region[0] - 1)
537 * insert discontiguous preceding range
539 hiddenColumns.add(i, new int[] { start, end });
542 else if (end <= region[1])
545 * new range overlaps existing, or is contiguous preceding it - adjust
548 region[0] = Math.min(region[0], start);
551 else if (start <= region[1] + 1)
554 * new range overlaps existing, or is contiguous following it - adjust
555 * start and end columns
557 region[0] = Math.min(region[0], start);
558 region[1] = Math.max(region[1], end);
561 * also update or remove any subsequent ranges
562 * that are overlapped
564 while (i < hiddenColumns.size() - 1)
566 int[] nextRegion = hiddenColumns.get(i + 1);
567 if (nextRegion[0] > end + 1)
570 * gap to next hidden range - no more to update
574 region[1] = Math.max(nextRegion[1], end);
575 hiddenColumns.subList(i + 1, i + 2).clear();
583 * Answers if a column in the alignment is visible
586 * absolute position of column in the alignment
587 * @return true if column is visible
589 public boolean isVisible(int column)
593 LOCK.readLock().lock();
595 Iterator<int[]> it = new RegionsIterator(column, column,
596 hiddenColumns, cursor);
599 int[] region = it.next();
600 if (column >= region[0] && column <= region[1])
609 LOCK.readLock().unlock();
614 * Get the visible sections of a set of sequences
617 * sequence position to start from
619 * sequence position to end at
621 * an array of sequences
622 * @return an array of strings encoding the visible parts of each sequence
624 public String[] getVisibleSequenceStrings(int start, int end,
629 LOCK.readLock().lock();
630 int iSize = seqs.length;
631 String[] selections = new String[iSize];
632 if (hiddenColumns != null && hiddenColumns.size() > 0)
634 for (int i = 0; i < iSize; i++)
636 StringBuffer visibleSeq = new StringBuffer();
638 Iterator<int[]> blocks = new VisibleContigsIterator(start,
639 end + 1, hiddenColumns);
641 while (blocks.hasNext())
643 int[] block = blocks.next();
644 if (blocks.hasNext())
647 .append(seqs[i].getSequence(block[0], block[1] + 1));
652 .append(seqs[i].getSequence(block[0], block[1]));
656 selections[i] = visibleSeq.toString();
661 for (int i = 0; i < iSize; i++)
663 selections[i] = seqs[i].getSequenceAsString(start, end);
670 LOCK.readLock().unlock();
675 * Locate the first position visible for this sequence. If seq isn't visible
676 * then return the position of the left side of the hidden boundary region.
679 * sequence to find position for
680 * @return visible start position
682 public int locateVisibleStartOfSequence(SequenceI seq)
686 LOCK.readLock().lock();
689 if (hiddenColumns == null || hiddenColumns.size() == 0)
691 return seq.findIndex(seq.getStart()) - 1;
694 // Simply walk along the sequence whilst watching for hidden column
696 Iterator<int[]> regions = hiddenColumns.iterator();
697 int hideStart = seq.getLength();
701 boolean foundStart = false;
703 // step through the non-gapped positions of the sequence
704 for (int i = seq.getStart(); i <= seq.getEnd() && (!foundStart); i++)
706 // get alignment position of this residue in the sequence
707 int p = seq.findIndex(i) - 1;
709 // update hidden region start/end
710 while (hideEnd < p && regions.hasNext())
712 int[] region = regions.next();
714 visNext += region[0] - visPrev;
715 hideStart = region[0];
720 hideStart = seq.getLength();
722 // update visible boundary for sequence
732 return findColumnPosition(start);
734 // otherwise, sequence was completely hidden
738 LOCK.readLock().unlock();
743 * delete any columns in alignmentAnnotation that are hidden (including
744 * sequence associated annotation).
746 * @param alignmentAnnotation
748 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
750 makeVisibleAnnotation(0, alignmentAnnotation.annotations.length,
751 alignmentAnnotation);
755 * delete any columns in alignmentAnnotation that are hidden (including
756 * sequence associated annotation).
759 * remove any annotation to the right of this column
761 * remove any annotation to the left of this column
762 * @param alignmentAnnotation
763 * the annotation to operate on
765 public void makeVisibleAnnotation(int start, int end,
766 AlignmentAnnotation alignmentAnnotation)
770 LOCK.readLock().lock();
772 int startFrom = start;
775 if (alignmentAnnotation.annotations != null)
777 if (hiddenColumns != null && hiddenColumns.size() > 0)
779 removeHiddenAnnotation(startFrom, endAt, alignmentAnnotation);
783 alignmentAnnotation.restrict(startFrom, endAt);
788 LOCK.readLock().unlock();
792 private void removeHiddenAnnotation(int start, int end,
793 AlignmentAnnotation alignmentAnnotation)
795 // mangle the alignmentAnnotation annotation array
796 ArrayList<Annotation[]> annels = new ArrayList<>();
797 Annotation[] els = null;
801 Iterator<int[]> blocks = new VisibleContigsIterator(start, end + 1,
805 int annotationLength;
806 while (blocks.hasNext())
808 int[] block = blocks.next();
809 annotationLength = block[1] - block[0] + 1;
811 if (blocks.hasNext())
813 // copy just the visible segment of the annotation row
814 copylength = annotationLength;
818 if (annotationLength + block[0] <= alignmentAnnotation.annotations.length)
820 // copy just the visible segment of the annotation row
821 copylength = annotationLength;
825 // copy to the end of the annotation row
826 copylength = alignmentAnnotation.annotations.length - block[0];
830 els = new Annotation[annotationLength];
832 System.arraycopy(alignmentAnnotation.annotations, block[0], els, 0,
834 w += annotationLength;
839 alignmentAnnotation.annotations = new Annotation[w];
842 for (Annotation[] chnk : annels)
844 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
853 * @return true if there are columns hidden
855 public boolean hasHiddenColumns()
859 LOCK.readLock().lock();
860 return hiddenColumns != null && hiddenColumns.size() > 0;
863 LOCK.readLock().unlock();
869 * @return true if there are more than one set of columns hidden
871 public boolean hasManyHiddenColumns()
875 LOCK.readLock().lock();
876 return hiddenColumns != null && hiddenColumns.size() > 1;
879 LOCK.readLock().unlock();
884 * mark the columns corresponding to gap characters as hidden in the column
889 public void hideInsertionsFor(SequenceI sr)
893 LOCK.writeLock().lock();
894 List<int[]> inserts = sr.getInsertions();
895 for (int[] r : inserts)
897 hideColumns(r[0], r[1]);
899 cursor.resetCursor(hiddenColumns);
902 LOCK.writeLock().unlock();
907 * Unhides, and adds to the selection list, all hidden columns
909 public void revealAllHiddenColumns(ColumnSelection sel)
913 LOCK.writeLock().lock();
914 if (hiddenColumns != null)
916 Iterator<int[]> it = hiddenColumns.iterator();
919 int[] region = it.next();
920 for (int j = region[0]; j < region[1] + 1; j++)
925 hiddenColumns = null;
926 cursor.resetCursor(hiddenColumns);
930 LOCK.writeLock().unlock();
935 * Reveals, and marks as selected, the hidden column range with the given
940 public void revealHiddenColumns(int start, ColumnSelection sel)
944 LOCK.writeLock().lock();
945 Iterator<int[]> it = new RegionsIterator(start, start, hiddenColumns,
949 int[] region = it.next();
950 if (start == region[0])
952 for (int j = region[0]; j < region[1] + 1; j++)
959 else if (start < region[0])
961 break; // passed all possible matching regions
965 if (hiddenColumns.size() == 0)
967 hiddenColumns = null;
969 cursor.resetCursor(hiddenColumns);
972 LOCK.writeLock().unlock();
977 * Add gaps into the sequences aligned to profileseq under the given
982 * - alignment to have gaps inserted into it
984 * - alignment view where sequence corresponding to profileseq is
986 * @return new HiddenColumns for new alignment view, with insertions into
987 * profileseq marked as hidden.
989 public static HiddenColumns propagateInsertions(SequenceI profileseq,
990 AlignmentI al, AlignmentView input)
994 char gc = al.getGapCharacter();
995 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
996 HiddenColumns nview = (HiddenColumns) alandhidden[1];
997 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
998 nview.propagateInsertions(profileseq, al, origseq);
1005 * - sequence in al which corresponds to origseq
1007 * - alignment which is to have gaps inserted into it
1009 * - sequence corresponding to profileseq which defines gap map for
1012 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
1017 LOCK.writeLock().lock();
1019 char gc = al.getGapCharacter();
1021 // take the set of hidden columns, and the set of gaps in origseq,
1022 // and remove all the hidden gaps from hiddenColumns
1024 // first get the gaps as a Bitset
1025 BitSet gaps = origseq.gapBitset();
1027 // now calculate hidden ^ not(gap)
1028 BitSet hidden = new BitSet();
1029 markHiddenRegions(hidden);
1030 hidden.andNot(gaps);
1031 hiddenColumns = null;
1032 this.hideMarkedBits(hidden);
1034 // for each sequence in the alignment, except the profile sequence,
1035 // insert gaps corresponding to each hidden region
1036 // but where each hidden column region is shifted backwards by the number
1038 // preceding visible gaps
1039 // update hidden columns at the same time
1040 Iterator<int[]> regions = hiddenColumns.iterator();
1041 ArrayList<int[]> newhidden = new ArrayList<>();
1043 int numGapsBefore = 0;
1044 int gapPosition = 0;
1045 while (regions.hasNext())
1047 // get region coordinates accounting for gaps
1048 // we can rely on gaps not being *in* hidden regions because we already
1050 int[] region = regions.next();
1051 while (gapPosition < region[0])
1054 if (gaps.get(gapPosition))
1060 int left = region[0] - numGapsBefore;
1061 int right = region[1] - numGapsBefore;
1062 newhidden.add(new int[] { left, right });
1064 // make a string with number of gaps = length of hidden region
1065 StringBuffer sb = new StringBuffer();
1066 for (int s = 0; s < right - left + 1; s++)
1070 padGaps(sb, left, profileseq, al);
1073 hiddenColumns = newhidden;
1074 cursor.resetCursor(hiddenColumns);
1077 LOCK.writeLock().unlock();
1082 * Pad gaps in all sequences in alignment except profileseq
1085 * gap string to insert
1087 * position to insert at
1089 * sequence not to pad
1091 * alignment to pad sequences in
1093 private void padGaps(StringBuffer sb, int pos, SequenceI profileseq,
1096 // loop over the sequences and pad with gaps where required
1097 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1099 SequenceI sqobj = al.getSequenceAt(s);
1100 if (sqobj != profileseq)
1102 String sq = al.getSequenceAt(s).getSequenceAsString();
1103 if (sq.length() <= pos)
1106 int diff = pos - sq.length() - 1;
1111 while ((diff = pos - sq.length() - 1) > 0)
1113 if (diff >= sb.length())
1115 sq += sb.toString();
1119 char[] buf = new char[diff];
1120 sb.getChars(0, diff, buf, 0);
1121 sq += buf.toString();
1125 sq += sb.toString();
1129 al.getSequenceAt(s).setSequence(
1130 sq.substring(0, pos) + sb.toString() + sq.substring(pos));
1137 * Returns a hashCode built from hidden column ranges
1140 public int hashCode()
1144 LOCK.readLock().lock();
1146 Iterator<int[]> it = hiddenColumns.iterator();
1147 while (it.hasNext())
1149 int[] hidden = it.next();
1150 hashCode = HASH_MULTIPLIER * hashCode + hidden[0];
1151 hashCode = HASH_MULTIPLIER * hashCode + hidden[1];
1156 LOCK.readLock().unlock();
1161 * Hide columns corresponding to the marked bits
1164 * - columns map to bits starting from zero
1166 public void hideMarkedBits(BitSet inserts)
1170 LOCK.writeLock().lock();
1171 for (int firstSet = inserts
1172 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1173 .nextSetBit(lastSet))
1175 lastSet = inserts.nextClearBit(firstSet);
1176 hideColumns(firstSet, lastSet - 1);
1178 cursor.resetCursor(hiddenColumns);
1181 LOCK.writeLock().unlock();
1188 * BitSet where hidden columns will be marked
1190 public void markHiddenRegions(BitSet inserts)
1194 LOCK.readLock().lock();
1195 if (hiddenColumns == null)
1199 Iterator<int[]> it = hiddenColumns.iterator();
1200 while (it.hasNext())
1202 int[] range = it.next();
1203 inserts.set(range[0], range[1] + 1);
1207 LOCK.readLock().unlock();
1212 * Calculate the visible start and end index of an alignment.
1215 * full alignment width
1216 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1218 public int[] getVisibleStartAndEndIndex(int width)
1222 LOCK.readLock().lock();
1223 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1224 int startPos = alignmentStartEnd[0];
1225 int endPos = alignmentStartEnd[1];
1227 int[] lowestRange = new int[] { -1, -1 };
1228 int[] higestRange = new int[] { -1, -1 };
1230 if (hiddenColumns == null)
1232 return new int[] { startPos, endPos };
1235 Iterator<int[]> it = hiddenColumns.iterator();
1236 while (it.hasNext())
1238 int[] range = it.next();
1239 lowestRange = (range[0] <= startPos) ? range : lowestRange;
1240 higestRange = (range[1] >= endPos) ? range : higestRange;
1243 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1245 startPos = alignmentStartEnd[0];
1249 startPos = lowestRange[1] + 1;
1252 if (higestRange[0] == -1 && higestRange[1] == -1)
1254 endPos = alignmentStartEnd[1];
1258 endPos = higestRange[0] - 1;
1260 return new int[] { startPos, endPos };
1263 LOCK.readLock().unlock();
1268 * Finds the hidden region (if any) which starts or ends at res
1271 * visible residue position, unadjusted for hidden columns
1272 * @return region as [start,end] or null if no matching region is found
1274 public int[] getRegionWithEdgeAtRes(int res)
1278 LOCK.readLock().lock();
1279 int adjres = adjustForHiddenColumns(res);
1281 int[] reveal = null;
1283 if (hiddenColumns != null)
1285 int regionindex = cursor.findRegionForColumn(adjres - 1);
1286 if (hiddenColumns.get(regionindex)[1] == adjres - 1)
1288 reveal = hiddenColumns.get(regionindex);
1292 regionindex = cursor.findRegionForColumn(adjres + 1);
1293 if (hiddenColumns.get(regionindex)[0] == adjres + 1)
1295 reveal = hiddenColumns.get(regionindex);
1303 LOCK.readLock().unlock();
1308 * Return an iterator over the hidden regions
1310 public Iterator<int[]> iterator()
1314 LOCK.readLock().lock();
1315 return new BoundedHiddenColsIterator(hiddenColumns);
1318 LOCK.readLock().unlock();
1323 * Return a bounded iterator over the hidden regions
1326 * position to start from (inclusive, absolute column position)
1328 * position to end at (inclusive, absolute column position)
1331 public Iterator<int[]> getBoundedIterator(int start, int end)
1335 LOCK.readLock().lock();
1336 return new BoundedHiddenColsIterator(start, end, hiddenColumns);
1339 LOCK.readLock().unlock();
1344 * Return a bounded iterator over the *visible* start positions of hidden
1348 * position to start from (inclusive, visible column position)
1350 * position to end at (inclusive, visible column position)
1352 public Iterator<Integer> getBoundedStartIterator(int start, int end)
1356 LOCK.readLock().lock();
1357 return new BoundedStartRegionIterator(start, end, hiddenColumns);
1360 LOCK.readLock().unlock();
1365 * Return an iterator over visible *columns* (not regions) between the given
1366 * start and end boundaries
1369 * first column (inclusive)
1371 * last column (inclusive)
1373 public Iterator<Integer> getVisibleColsIterator(int start, int end)
1377 LOCK.readLock().lock();
1378 return new VisibleColsIterator(start, end, hiddenColumns);
1381 LOCK.readLock().unlock();
1386 * return an iterator over visible segments between the given start and end
1390 * (first column inclusive from 0)
1392 * (last column - not inclusive)
1394 public Iterator<int[]> getVisContigsIterator(int start, int end)
1398 LOCK.readLock().lock();
1399 return new VisibleContigsIterator(start, end, hiddenColumns);
1402 LOCK.readLock().unlock();
1407 * return an iterator over visible segments between the given start and end
1411 * (first column - inclusive from 0)
1413 * (last column - inclusive)
1414 * @param useVisibleCoords
1415 * if true, start and end are visible column positions, not absolute
1418 public Iterator<int[]> getVisibleBlocksIterator(int start, int end,
1419 boolean useVisibleCoords)
1421 if (useVisibleCoords)
1424 // we should really just convert start and end here with
1425 // adjustForHiddenColumns
1426 // and then create a VisibleContigsIterator
1427 // but without a cursor this will be horribly slow in some situations
1428 // ... so until then...
1429 return new VisibleBlocksVisBoundsIterator(start, end, true);
1435 LOCK.readLock().lock();
1436 return new VisibleContigsIterator(start, end + 1, hiddenColumns);
1439 LOCK.readLock().unlock();
1445 * An iterator which iterates over visible regions in a range. The range is
1446 * specified in terms of visible column positions. Provides a special
1447 * "endsAtHidden" indicator to allow callers to determine if the final visible
1448 * column is adjacent to a hidden region.
1450 public class VisibleBlocksVisBoundsIterator implements Iterator<int[]>
1452 private List<int[]> vcontigs = new ArrayList<>();
1454 private int currentPosition = 0;
1456 private boolean endsAtHidden = false;
1459 * Constructor for iterator over visible regions in a range.
1462 * start position in terms of visible column position
1464 * end position in terms of visible column position
1466 * whether to use a local copy of hidden columns
1468 VisibleBlocksVisBoundsIterator(int start, int end, boolean usecopy)
1470 /* actually this implementation always uses a local copy but this may change in future */
1475 LOCK.readLock().lock();
1478 if (hiddenColumns != null && hiddenColumns.size() > 0)
1480 int blockStart = start;
1482 int hiddenSoFar = 0;
1485 // iterate until a region begins within (start,end]
1487 while ((i < hiddenColumns.size())
1488 && (hiddenColumns.get(i)[0] <= blockStart + hiddenSoFar))
1490 hiddenSoFar += hiddenColumns.get(i)[1] - hiddenColumns.get(i)[0]
1495 blockStart += hiddenSoFar; // convert start to absolute position
1496 blockEnd += hiddenSoFar; // convert end to absolute position
1498 // iterate from start to end, adding each visible region. Positions
1500 // absolute, and all hidden regions which overlap [start,end] are
1502 while (i < hiddenColumns.size()
1503 && (hiddenColumns.get(i)[0] <= blockEnd))
1505 int[] region = hiddenColumns.get(i);
1507 // end position of this visible region is either just before the
1508 // start of the next hidden region, or the absolute position of
1509 // 'end', whichever is lowest
1510 blockEnd = Math.min(blockEnd, region[0] - 1);
1512 vcontigs.add(new int[] { blockStart, blockEnd });
1514 visSoFar += blockEnd - blockStart + 1;
1516 // next visible region starts after this hidden region
1517 blockStart = region[1] + 1;
1519 hiddenSoFar += region[1] - region[0] + 1;
1521 // reset blockEnd to absolute position of 'end', assuming we've now
1522 // passed all hidden regions before end
1523 blockEnd = end + hiddenSoFar;
1527 if (visSoFar < end - start + 1)
1529 // the number of visible columns we've accounted for is less than
1530 // the number specified by end-start; work out the end position of
1531 // the last visible region
1532 blockEnd = blockStart + end - start - visSoFar;
1533 vcontigs.add(new int[] { blockStart, blockEnd });
1535 // if the last visible region ends at the next hidden region, set
1536 // endsAtHidden=true
1537 if (i < hiddenColumns.size()
1538 && hiddenColumns.get(i)[0] - 1 == blockEnd)
1540 endsAtHidden = true;
1546 // there are no hidden columns, return a single visible contig
1547 vcontigs.add(new int[] { start, end });
1548 endsAtHidden = false;
1554 LOCK.readLock().unlock();
1560 public boolean hasNext()
1562 return (currentPosition < vcontigs.size());
1568 int[] result = vcontigs.get(currentPosition);
1573 public boolean endsAtHidden()
1575 return endsAtHidden;