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 * traverse existing hidden ranges and insert / amend / append as
532 for (int i = 0; i < hiddenColumns.size(); i++)
534 int[] region = hiddenColumns.get(i);
536 if (end < region[0] - 1)
539 * insert discontiguous preceding range
541 hiddenColumns.add(i, new int[] { start, end });
545 if (end <= region[1])
548 * new range overlaps existing, or is contiguous preceding it - adjust
551 region[0] = Math.min(region[0], start);
555 if (start <= region[1] + 1)
558 * new range overlaps existing, or is contiguous following it - adjust
559 * start and end columns
561 region[0] = Math.min(region[0], start);
562 region[1] = Math.max(region[1], end);
565 * also update or remove any subsequent ranges
566 * that are overlapped
568 while (i < hiddenColumns.size() - 1)
570 int[] nextRegion = hiddenColumns.get(i + 1);
571 if (nextRegion[0] > end + 1)
574 * gap to next hidden range - no more to update
578 region[1] = Math.max(nextRegion[1], end);
579 hiddenColumns.remove(i + 1);
586 * remaining case is that the new range follows everything else
588 hiddenColumns.add(new int[] { start, end });
591 if (!wasAlreadyLocked)
593 LOCK.writeLock().unlock();
598 public boolean isVisible(int column)
602 LOCK.readLock().lock();
604 if (hiddenColumns != null)
606 for (int[] region : hiddenColumns)
608 if (column >= region[0] && column <= region[1])
618 LOCK.readLock().unlock();
622 private ArrayList<int[]> copyHiddenRegionsToArrayList(int startIndex)
625 if (hiddenColumns != null)
627 size = hiddenColumns.size();
629 ArrayList<int[]> copy = new ArrayList<>(size);
631 for (int i = startIndex, j = size; i < j; i++)
635 rh = hiddenColumns.get(i);
638 cp = new int[rh.length];
639 System.arraycopy(rh, 0, cp, 0, rh.length);
647 public String[] getVisibleSequenceStrings(int start, int end,
652 LOCK.readLock().lock();
653 int iSize = seqs.length;
654 String[] selections = new String[iSize];
655 if (hiddenColumns != null && hiddenColumns.size() > 0)
657 for (int i = 0; i < iSize; i++)
659 StringBuffer visibleSeq = new StringBuffer();
661 Iterator<int[]> blocks = new VisibleBlocksIterator(start,
663 while (blocks.hasNext())
665 int[] block = blocks.next();
666 visibleSeq.append(seqs[i].getSequence(block[0], block[1]));
669 selections[i] = visibleSeq.toString();
674 for (int i = 0; i < iSize; i++)
676 selections[i] = seqs[i].getSequenceAsString(start, end);
683 LOCK.readLock().unlock();
688 * Locate the first position visible for this sequence. If seq isn't visible
689 * then return the position of the left side of the hidden boundary region.
692 * sequence to find position for
693 * @return visible start position
695 public int locateVisibleStartOfSequence(SequenceI seq)
699 LOCK.readLock().lock();
702 if (hiddenColumns == null || hiddenColumns.size() == 0)
704 return seq.findIndex(seq.getStart()) - 1;
707 // Simply walk along the sequence whilst watching for hidden column
709 Iterator<int[]> regions = iterator();
710 int hideStart = seq.getLength();
714 boolean foundStart = false;
716 // step through the non-gapped positions of the sequence
717 for (int i = seq.getStart(); i <= seq.getEnd() && (!foundStart); i++)
719 // get alignment position of this residue in the sequence
720 int p = seq.findIndex(i) - 1;
722 // update hidden region start/end
723 while (hideEnd < p && regions.hasNext())
725 int[] region = regions.next();
727 visNext += region[0] - visPrev;
728 hideStart = region[0];
733 hideStart = seq.getLength();
735 // update visible boundary for sequence
745 return findColumnPosition(start);
747 // otherwise, sequence was completely hidden
751 LOCK.readLock().unlock();
756 * delete any columns in alignmentAnnotation that are hidden (including
757 * sequence associated annotation).
759 * @param alignmentAnnotation
761 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
763 makeVisibleAnnotation(0, alignmentAnnotation.annotations.length,
764 alignmentAnnotation);
768 * delete any columns in alignmentAnnotation that are hidden (including
769 * sequence associated annotation).
772 * remove any annotation to the right of this column
774 * remove any annotation to the left of this column
775 * @param alignmentAnnotation
776 * the annotation to operate on
778 public void makeVisibleAnnotation(int start, int end,
779 AlignmentAnnotation alignmentAnnotation)
783 LOCK.readLock().lock();
785 int startFrom = start;
788 if (alignmentAnnotation.annotations != null)
790 if (hiddenColumns != null && hiddenColumns.size() > 0)
792 removeHiddenAnnotation(startFrom, endAt, alignmentAnnotation);
796 alignmentAnnotation.restrict(startFrom, endAt);
801 LOCK.readLock().unlock();
805 private void removeHiddenAnnotation(int start, int end,
806 AlignmentAnnotation alignmentAnnotation)
808 // mangle the alignmentAnnotation annotation array
809 Vector<Annotation[]> annels = new Vector<>();
810 Annotation[] els = null;
814 Iterator<int[]> blocks = new VisibleBlocksIterator(start, end,
817 int annotationLength;
818 while (blocks.hasNext())
820 int[] block = blocks.next();
822 if (blocks.hasNext())
824 annotationLength = block[1] - block[0];
825 // copy just the visible segment of the annotation row
826 copylength = annotationLength;
830 annotationLength = block[1] - block[0] + 1;
831 if (annotationLength + block[0] <= alignmentAnnotation.annotations.length)
833 // copy just the visible segment of the annotation row
834 copylength = annotationLength;
838 // copy to the end of the annotation row
839 copylength = alignmentAnnotation.annotations.length - block[0];
843 els = new Annotation[annotationLength];
844 annels.addElement(els);
845 System.arraycopy(alignmentAnnotation.annotations, block[0], els, 0,
847 w += annotationLength;
852 alignmentAnnotation.annotations = new Annotation[w];
855 for (Annotation[] chnk : annels)
857 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
866 * @return true if there are columns hidden
868 public boolean hasHiddenColumns()
872 LOCK.readLock().lock();
873 return hiddenColumns != null && hiddenColumns.size() > 0;
876 LOCK.readLock().unlock();
882 * @return true if there are more than one set of columns hidden
884 public boolean hasManyHiddenColumns()
888 LOCK.readLock().lock();
889 return hiddenColumns != null && hiddenColumns.size() > 1;
892 LOCK.readLock().unlock();
897 * mark the columns corresponding to gap characters as hidden in the column
902 public void hideInsertionsFor(SequenceI sr)
906 LOCK.writeLock().lock();
907 List<int[]> inserts = sr.getInsertions();
908 for (int[] r : inserts)
910 hideColumns(r[0], r[1]);
914 LOCK.writeLock().unlock();
919 * Unhides, and adds to the selection list, all hidden columns
921 public void revealAllHiddenColumns(ColumnSelection sel)
925 LOCK.writeLock().lock();
926 if (hiddenColumns != null)
928 for (int i = 0; i < hiddenColumns.size(); i++)
930 int[] region = hiddenColumns.get(i);
931 for (int j = region[0]; j < region[1] + 1; j++)
938 hiddenColumns = null;
941 LOCK.writeLock().unlock();
946 * Reveals, and marks as selected, the hidden column range with the given
951 public void revealHiddenColumns(int start, ColumnSelection sel)
955 LOCK.writeLock().lock();
956 for (int i = 0; i < hiddenColumns.size(); i++)
958 int[] region = hiddenColumns.get(i);
959 if (start == region[0])
961 for (int j = region[0]; j < region[1] + 1; j++)
966 hiddenColumns.remove(region);
970 if (hiddenColumns.size() == 0)
972 hiddenColumns = null;
976 LOCK.writeLock().unlock();
981 * Add gaps into the sequences aligned to profileseq under the given
986 * - alignment to have gaps inserted into it
988 * - alignment view where sequence corresponding to profileseq is
990 * @return new HiddenColumns for new alignment view, with insertions into
991 * profileseq marked as hidden.
993 public static HiddenColumns propagateInsertions(SequenceI profileseq,
994 AlignmentI al, AlignmentView input)
998 char gc = al.getGapCharacter();
999 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
1000 HiddenColumns nview = (HiddenColumns) alandhidden[1];
1001 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
1002 nview.propagateInsertions(profileseq, al, origseq);
1009 * - sequence in al which corresponds to origseq
1011 * - alignment which is to have gaps inserted into it
1013 * - sequence corresponding to profileseq which defines gap map for
1016 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
1021 LOCK.writeLock().lock();
1023 char gc = al.getGapCharacter();
1025 // take the set of hidden columns, and the set of gaps in origseq,
1026 // and remove all the hidden gaps from hiddenColumns
1028 // first get the gaps as a Bitset
1029 BitSet gaps = origseq.gapBitset();
1031 // now calculate hidden ^ not(gap)
1032 BitSet hidden = new BitSet();
1033 markHiddenRegions(hidden);
1034 hidden.andNot(gaps);
1035 hiddenColumns = null;
1036 this.hideMarkedBits(hidden);
1038 // for each sequence in the alignment, except the profile sequence,
1039 // insert gaps corresponding to each hidden region
1040 // but where each hidden column region is shifted backwards by the number
1042 // preceding visible gaps
1043 // update hidden columns at the same time
1044 Iterator<int[]> regions = iterator();
1045 ArrayList<int[]> newhidden = new ArrayList<>();
1047 int numGapsBefore = 0;
1048 int gapPosition = 0;
1049 while (regions.hasNext())
1051 // get region coordinates accounting for gaps
1052 // we can rely on gaps not being *in* hidden regions because we already
1054 int[] region = regions.next();
1055 while (gapPosition < region[0])
1058 if (gaps.get(gapPosition))
1064 int left = region[0] - numGapsBefore;
1065 int right = region[1] - numGapsBefore;
1066 newhidden.add(new int[] { left, right });
1068 // make a string with number of gaps = length of hidden region
1069 StringBuffer sb = new StringBuffer();
1070 for (int s = 0; s < right - left + 1; s++)
1074 padGaps(sb, left, profileseq, al);
1077 hiddenColumns = newhidden;
1080 LOCK.writeLock().unlock();
1085 * Pad gaps in all sequences in alignment except profileseq
1088 * gap string to insert
1090 * position to insert at
1092 * sequence not to pad
1094 * alignment to pad sequences in
1096 private void padGaps(StringBuffer sb, int pos, SequenceI profileseq,
1099 // loop over the sequences and pad with gaps where required
1100 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1102 SequenceI sqobj = al.getSequenceAt(s);
1103 if (sqobj != profileseq)
1105 String sq = al.getSequenceAt(s).getSequenceAsString();
1106 if (sq.length() <= pos)
1109 int diff = pos - sq.length() - 1;
1114 while ((diff = pos - sq.length() - 1) > 0)
1116 if (diff >= sb.length())
1118 sq += sb.toString();
1122 char[] buf = new char[diff];
1123 sb.getChars(0, diff, buf, 0);
1124 sq += buf.toString();
1128 sq += sb.toString();
1132 al.getSequenceAt(s).setSequence(
1133 sq.substring(0, pos) + sb.toString() + sq.substring(pos));
1140 * Returns a hashCode built from hidden column ranges
1143 public int hashCode()
1147 LOCK.readLock().lock();
1149 if (hiddenColumns != null)
1151 for (int[] hidden : hiddenColumns)
1153 hashCode = HASH_MULTIPLIER * hashCode + hidden[0];
1154 hashCode = HASH_MULTIPLIER * hashCode + hidden[1];
1160 LOCK.readLock().unlock();
1165 * Hide columns corresponding to the marked bits
1168 * - columns map to bits starting from zero
1170 public void hideMarkedBits(BitSet inserts)
1174 LOCK.writeLock().lock();
1175 for (int firstSet = inserts
1176 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1177 .nextSetBit(lastSet))
1179 lastSet = inserts.nextClearBit(firstSet);
1180 hideColumns(firstSet, lastSet - 1);
1184 LOCK.writeLock().unlock();
1191 * BitSet where hidden columns will be marked
1193 public void markHiddenRegions(BitSet inserts)
1197 LOCK.readLock().lock();
1198 if (hiddenColumns == null)
1202 for (int[] range : hiddenColumns)
1204 inserts.set(range[0], range[1] + 1);
1208 LOCK.readLock().unlock();
1213 * Calculate the visible start and end index of an alignment.
1216 * full alignment width
1217 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1219 public int[] getVisibleStartAndEndIndex(int width)
1223 LOCK.readLock().lock();
1224 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1225 int startPos = alignmentStartEnd[0];
1226 int endPos = alignmentStartEnd[1];
1228 int[] lowestRange = new int[] { -1, -1 };
1229 int[] higestRange = new int[] { -1, -1 };
1231 if (hiddenColumns == null)
1233 return new int[] { startPos, endPos };
1236 for (int[] hiddenCol : hiddenColumns)
1238 lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
1239 higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
1242 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1244 startPos = alignmentStartEnd[0];
1248 startPos = lowestRange[1] + 1;
1251 if (higestRange[0] == -1 && higestRange[1] == -1)
1253 endPos = alignmentStartEnd[1];
1257 endPos = higestRange[0] - 1;
1259 return new int[] { startPos, endPos };
1262 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;
1282 if (hiddenColumns != null)
1284 for (int[] region : hiddenColumns)
1286 if (adjres + 1 == region[0] || adjres - 1 == region[1])
1296 LOCK.readLock().unlock();
1301 * Return an iterator over the hidden regions
1303 public Iterator<int[]> iterator()
1305 if (hiddenColumns != null)
1307 int last = hiddenColumns.get(hiddenColumns.size() - 1)[1];
1308 return new BoundedHiddenColsIterator(0, last, true);
1312 return new BoundedHiddenColsIterator(0, 0, true);
1317 * Return a bounded iterator over the hidden regions
1320 * position to start from (inclusive, absolute column position)
1322 * position to end at (inclusive, absolute column position)
1325 public Iterator<int[]> getBoundedIterator(int start, int end)
1327 return new BoundedHiddenColsIterator(start, end, true);
1331 * Return a bounded iterator over the *visible* start positions of hidden
1335 * position to start from (inclusive, visible column position)
1337 * position to end at (inclusive, visible column position)
1339 public Iterator<Integer> getBoundedStartIterator(int start, int end)
1341 return new BoundedStartRegionIterator(start, end, true);
1345 * Return an iterator over visible columns between the given start and end
1349 * first column (inclusive)
1351 * last column (inclusive)
1353 public Iterator<Integer> getVisibleColsIterator(int start, int end)
1355 return new VisibleColsIterator(start, end, true);
1359 * return an iterator over visible segments between the given start and end
1363 * (first column inclusive from 0)
1365 * (last column - not inclusive)
1367 public Iterator<int[]> getVisContigsIterator(int start, int end)
1369 return new VisibleContigsIterator(start, end, true);
1373 * return an iterator over visible segments between the given start and end
1377 * (first column - inclusive from 0)
1379 * (last column - inclusive)
1380 * @param useVisibleCoords
1381 * if true, start and end are visible column positions, not absolute
1384 public Iterator<int[]> getVisibleBlocksIterator(int start, int end,
1385 boolean useVisibleCoords)
1387 if (useVisibleCoords)
1390 // we should really just convert start and end here with
1391 // adjustForHiddenColumns
1392 // and then create a VisibleBlocksIterator
1393 // but without a cursor this will be horribly slow in some situations
1394 // ... so until then...
1395 return new VisibleBlocksVisBoundsIterator(start, end, true);
1399 return new VisibleBlocksIterator(start, end, true);
1404 * An iterator which iterates over hidden column regions in a range.
1406 private class BoundedHiddenColsIterator implements Iterator<int[]>
1408 private int start; // start position to iterate from
1410 private int end; // end position to iterate to
1412 // current index in hiddenColumns
1413 private int currentPosition = 0;
1415 // current column in hiddenColumns
1416 private int[] currentRegion;
1418 // whether to make a local copy of hiddenColumns
1419 private final boolean useCopy;
1421 // local copy or reference to hiddenColumns
1422 private List<int[]> localHidden;
1425 * Construct an iterator over hiddenColums bounded at
1426 * [lowerBound,upperBound]
1429 * lower bound to iterate from
1431 * upper bound to iterate to
1432 * @param useCopyCols
1433 * whether to make a local copy of hiddenColumns for iteration (set
1434 * to true if calling from outwith the HiddenColumns class)
1436 BoundedHiddenColsIterator(int lowerBound, int upperBound,
1437 boolean useCopyCols)
1441 useCopy = useCopyCols;
1447 // assume that if useCopy is false the calling code has locked
1449 LOCK.readLock().lock();
1452 if (hiddenColumns != null)
1454 localHidden = new ArrayList<>();
1456 // iterate until a region overlaps with [start,end]
1458 while ((i < hiddenColumns.size())
1459 && (hiddenColumns.get(i)[1] < start))
1464 // iterate from start to end, adding each hidden region. Positions are
1465 // absolute, and all regions which *overlap* [start,end] are added.
1466 while (i < hiddenColumns.size()
1467 && (hiddenColumns.get(i)[0] <= end))
1471 rh = hiddenColumns.get(i);
1474 cp = new int[rh.length];
1475 System.arraycopy(rh, 0, cp, 0, rh.length);
1476 localHidden.add(cp);
1486 LOCK.readLock().unlock();
1492 public boolean hasNext()
1494 return (localHidden != null)
1495 && (currentPosition < localHidden.size());
1501 currentRegion = localHidden.get(currentPosition);
1503 return currentRegion;
1508 * An iterator which iterates over visible start positions of hidden column
1509 * regions in a range.
1511 private class BoundedStartRegionIterator implements Iterator<Integer>
1513 // start position to iterate from
1516 // end position to iterate to
1519 // current index in hiddenColumns
1520 private int currentPosition = 0;
1522 // local copy or reference to hiddenColumns
1523 private List<Integer> positions = null;
1526 * Construct an iterator over hiddenColums bounded at
1527 * [lowerBound,upperBound]
1530 * lower bound to iterate from
1532 * upper bound to iterate to
1533 * @param useCopyCols
1534 * whether to make a local copy of hiddenColumns for iteration (set
1535 * to true if calling from outwith the HiddenColumns class)
1537 BoundedStartRegionIterator(int lowerBound, int upperBound,
1547 // assume that if useCopy is false the calling code has locked
1549 LOCK.readLock().lock();
1552 if (hiddenColumns != null)
1554 positions = new ArrayList<>(hiddenColumns.size());
1556 // navigate to start, keeping count of hidden columns
1558 int hiddenSoFar = 0;
1559 while ((i < hiddenColumns.size())
1560 && (hiddenColumns.get(i)[0] < start + hiddenSoFar))
1562 int[] region = hiddenColumns.get(i);
1563 hiddenSoFar += region[1] - region[0] + 1;
1567 // iterate from start to end, adding start positions of each
1568 // hidden region. Positions are visible columns count, not absolute
1569 while (i < hiddenColumns.size()
1570 && (hiddenColumns.get(i)[0] <= end + hiddenSoFar))
1572 int[] region = hiddenColumns.get(i);
1573 positions.add(region[0] - hiddenSoFar);
1574 hiddenSoFar += region[1] - region[0] + 1;
1580 positions = new ArrayList<>();
1586 LOCK.readLock().unlock();
1592 public boolean hasNext()
1594 return (currentPosition < positions.size());
1598 * Get next hidden region start position
1600 * @return the start position in *visible* coordinates
1603 public Integer next()
1605 int result = positions.get(currentPosition);
1611 private class VisibleColsIterator implements Iterator<Integer>
1615 private int current;
1619 private List<int[]> localHidden = new ArrayList<>();
1621 private int nexthiddenregion;
1623 VisibleColsIterator(int firstcol, int lastcol, boolean useCopy)
1628 nexthiddenregion = 0;
1634 // assume that if useCopy is false the calling code has locked
1636 LOCK.readLock().lock();
1639 if (hiddenColumns != null)
1642 for (i = 0; i < hiddenColumns.size()
1643 && (current <= hiddenColumns.get(i)[0]); ++i)
1645 if (current >= hiddenColumns.get(i)[0]
1646 && current <= hiddenColumns.get(i)[1])
1648 // current is hidden, move to right
1649 current = hiddenColumns.get(i)[1] + 1;
1651 nexthiddenregion = i + 1;
1655 for (i = hiddenColumns.size() - 1; i >= 0
1656 && (last >= hiddenColumns.get(i)[1]); --i)
1658 if (last >= hiddenColumns.get(i)[0]
1659 && last <= hiddenColumns.get(i)[1])
1661 // last is hidden, move to left
1662 last = hiddenColumns.get(i)[0] - 1;
1666 // make a local copy of the bit we need
1667 i = nexthiddenregion;
1668 while (i < hiddenColumns.size()
1669 && hiddenColumns.get(i)[0] <= last)
1671 int[] region = new int[] { hiddenColumns.get(i)[0],
1672 hiddenColumns.get(i)[1] };
1673 localHidden.add(region);
1681 LOCK.readLock().unlock();
1687 public boolean hasNext()
1689 return next <= last;
1693 public Integer next()
1697 throw new NoSuchElementException();
1700 if ((localHidden != null)
1701 && (nexthiddenregion < localHidden.size()))
1703 // still some more hidden regions
1704 if (next + 1 < localHidden.get(nexthiddenregion)[0])
1706 // next+1 is still before the next hidden region
1709 else if ((next + 1 >= localHidden.get(nexthiddenregion)[0])
1710 && (next + 1 <= localHidden.get(nexthiddenregion)[1]))
1712 // next + 1 is in the next hidden region
1713 next = localHidden.get(nexthiddenregion)[1] + 1;
1719 // finished with hidden regions, just increment normally
1726 public void remove()
1728 throw new UnsupportedOperationException();
1733 * An iterator which iterates over visible regions in a range.
1735 private class VisibleContigsIterator implements Iterator<int[]>
1737 private List<int[]> vcontigs = new ArrayList<>();
1739 private int currentPosition = 0;
1741 VisibleContigsIterator(int start, int end, boolean usecopy)
1747 LOCK.readLock().lock();
1750 if (hiddenColumns != null && hiddenColumns.size() > 0)
1756 for (int[] region : hiddenColumns)
1758 hideStart = region[0];
1759 hideEnd = region[1];
1761 // navigate to start
1762 if (hideEnd < vstart)
1766 if (hideStart > vstart)
1768 int[] contig = new int[] { vstart, hideStart - 1 };
1769 vcontigs.add(contig);
1771 vstart = hideEnd + 1;
1773 // exit if we're past the end
1782 int[] contig = new int[] { vstart, end - 1 };
1783 vcontigs.add(contig);
1788 int[] contig = new int[] { start, end - 1 };
1789 vcontigs.add(contig);
1795 LOCK.readLock().unlock();
1801 public boolean hasNext()
1803 return (currentPosition < vcontigs.size());
1809 int[] result = vcontigs.get(currentPosition);
1816 * An iterator which iterates over visible regions in a range.
1818 private class VisibleBlocksIterator implements Iterator<int[]>
1820 private List<int[]> vcontigs = new ArrayList<>();
1822 private int currentPosition = 0;
1824 VisibleBlocksIterator(int start, int end, boolean usecopy)
1830 LOCK.readLock().lock();
1833 if (hiddenColumns != null && hiddenColumns.size() > 0)
1835 int blockStart = start;
1838 // iterate until a region overlaps with [start,end]
1840 while ((i < hiddenColumns.size())
1841 && (hiddenColumns.get(i)[1] < start))
1846 // iterate from start to end, adding each hidden region. Positions are
1847 // absolute, and all regions which *overlap* [start,end] are used.
1848 while (i < hiddenColumns.size()
1849 && (hiddenColumns.get(i)[0] <= end))
1851 int[] region = hiddenColumns.get(i);
1853 blockStart = Math.min(blockStart, region[1] + 1);
1854 blockEnd = Math.min(blockEnd, region[0]);
1856 int[] contig = new int[] { blockStart, blockEnd };
1857 vcontigs.add(contig);
1859 blockStart = region[1] + 1;
1865 if (end > blockStart)
1867 int[] contig = new int[] { blockStart, end };
1868 vcontigs.add(contig);
1873 int[] contig = new int[] { start, end };
1874 vcontigs.add(contig);
1880 LOCK.readLock().unlock();
1886 public boolean hasNext()
1888 return (currentPosition < vcontigs.size());
1894 int[] result = vcontigs.get(currentPosition);
1901 * An iterator which iterates over visible regions in a range. The range is
1902 * specified in terms of visible column positions. Provides a special
1903 * "endsAtHidden" indicator to allow callers to determine if the final visible
1904 * column is adjacent to a hidden region.
1906 public class VisibleBlocksVisBoundsIterator implements Iterator<int[]>
1908 private List<int[]> vcontigs = new ArrayList<>();
1910 private int currentPosition = 0;
1912 private boolean endsAtHidden = false;
1915 * Constructor for iterator over visible regions in a range.
1918 * start position in terms of visible column position
1920 * end position in terms of visible column position
1922 * whether to use a local copy of hidden columns
1924 VisibleBlocksVisBoundsIterator(int start, int end, boolean usecopy)
1926 /* actually this implementation always uses a local copy but this may change in future */
1931 LOCK.readLock().lock();
1934 if (hiddenColumns != null && hiddenColumns.size() > 0)
1936 int blockStart = start;
1938 int hiddenSoFar = 0;
1941 // iterate until a region begins within (start,end]
1943 while ((i < hiddenColumns.size())
1944 && (hiddenColumns.get(i)[0] <= blockStart + hiddenSoFar))
1946 hiddenSoFar += hiddenColumns.get(i)[1] - hiddenColumns.get(i)[0]
1951 blockStart += hiddenSoFar; // convert start to absolute position
1952 blockEnd += hiddenSoFar; // convert end to absolute position
1954 // iterate from start to end, adding each visible region. Positions
1956 // absolute, and all hidden regions which overlap [start,end] are
1958 while (i < hiddenColumns.size()
1959 && (hiddenColumns.get(i)[0] <= blockEnd))
1961 int[] region = hiddenColumns.get(i);
1963 // end position of this visible region is either just before the
1964 // start of the next hidden region, or the absolute position of
1965 // 'end', whichever is lowest
1966 blockEnd = Math.min(blockEnd, region[0] - 1);
1968 vcontigs.add(new int[] { blockStart, blockEnd });
1970 visSoFar += blockEnd - blockStart + 1;
1972 // next visible region starts after this hidden region
1973 blockStart = region[1] + 1;
1975 hiddenSoFar += region[1] - region[0] + 1;
1977 // reset blockEnd to absolute position of 'end', assuming we've now
1978 // passed all hidden regions before end
1979 blockEnd = end + hiddenSoFar;
1983 if (visSoFar < end - start)
1985 // the number of visible columns we've accounted for is less than
1986 // the number specified by end-start; work out the end position of
1987 // the last visible region
1988 blockEnd = blockStart + end - start - visSoFar;
1989 vcontigs.add(new int[] { blockStart, blockEnd });
1991 // if the last visible region ends at the next hidden region, set
1992 // endsAtHidden=true
1993 if (i < hiddenColumns.size()
1994 && hiddenColumns.get(i)[0] - 1 == blockEnd)
1996 endsAtHidden = true;
2002 // there are no hidden columns, return a single visible contig
2003 vcontigs.add(new int[] { start, end });
2004 endsAtHidden = false;
2010 LOCK.readLock().unlock();
2016 public boolean hasNext()
2018 return (currentPosition < vcontigs.size());
2024 int[] result = vcontigs.get(currentPosition);
2029 public boolean endsAtHidden()
2031 return endsAtHidden;