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.Collections;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.NoSuchElementException;
31 import java.util.Vector;
32 import java.util.concurrent.locks.ReentrantReadWriteLock;
34 public class HiddenColumns
36 private static final int HASH_MULTIPLIER = 31;
38 private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock();
41 * list of hidden column [start, end] ranges; the list is maintained in
42 * ascending start column order
44 private ArrayList<int[]> hiddenColumns;
49 public HiddenColumns()
58 public HiddenColumns(HiddenColumns copy)
62 LOCK.writeLock().lock();
65 if (copy.hiddenColumns != null)
67 hiddenColumns = copy.copyHiddenRegionsToArrayList(0);
72 LOCK.writeLock().unlock();
77 * Copy constructor within bounds and with offset. Copies hidden column
78 * regions fully contained between start and end, and offsets positions by
82 * HiddenColumns instance to copy from
84 * lower bound to copy from
86 * upper bound to copy to
88 * offset to subtract from each region boundary position
91 public HiddenColumns(HiddenColumns copy, int start, int end, int offset)
95 LOCK.writeLock().lock();
98 hiddenColumns = new ArrayList<>();
99 Iterator<int[]> it = copy.getBoundedIterator(start, end);
102 int[] region = it.next();
103 // still need to check boundaries because iterator returns
104 // all overlapping regions and we need contained regions
105 if (region[0] >= start && region[1] <= end)
109 { region[0] - offset, region[1] - offset });
115 LOCK.writeLock().unlock();
120 * This method is used to return all the HiddenColumn regions and is intended
121 * to remain private. External callers which need a copy of the regions can
122 * call getHiddenColumnsCopyAsList.
124 * @return empty list or List of hidden column intervals
126 private List<int[]> getHiddenRegions()
128 return hiddenColumns == null ? Collections.<int[]> emptyList()
133 * Output regions data as a string. String is in the format:
134 * reg0[0]<between>reg0[1]<delimiter>reg1[0]<between>reg1[1] ... regn[1]
137 * string to delimit regions
138 * @param betweenstring
139 * to put between start and end region values
140 * @return regions formatted according to delimiter and between strings
142 public String regionsToString(String delimiter, String between)
146 LOCK.readLock().lock();
147 StringBuilder regionBuilder = new StringBuilder();
148 if (hiddenColumns != null)
150 for (int[] range : hiddenColumns)
152 regionBuilder.append(delimiter).append(range[0]).append(between)
156 regionBuilder.deleteCharAt(0);
158 return regionBuilder.toString();
161 LOCK.readLock().unlock();
166 * Find the number of hidden columns
168 * @return number of hidden columns
174 LOCK.readLock().lock();
176 if (hasHiddenColumns())
178 for (int[] range : hiddenColumns)
180 size += range[1] - range[0] + 1;
186 LOCK.readLock().unlock();
191 * Get the number of distinct hidden regions
193 * @return number of regions
195 public int getNumberOfRegions()
199 LOCK.readLock().lock();
201 if (hasHiddenColumns())
203 num = hiddenColumns.size();
208 LOCK.readLock().unlock();
213 public boolean equals(Object obj)
217 LOCK.readLock().lock();
219 if (!(obj instanceof HiddenColumns))
223 HiddenColumns that = (HiddenColumns) obj;
226 * check hidden columns are either both null, or match
228 if (this.hiddenColumns == null)
230 return (that.hiddenColumns == null);
232 if (that.hiddenColumns == null
233 || that.hiddenColumns.size() != this.hiddenColumns.size())
238 for (int[] thisRange : hiddenColumns)
240 int[] thatRange = that.hiddenColumns.get(i++);
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();
266 if (hiddenColumns != null)
268 for (int i = 0; i < hiddenColumns.size(); i++)
270 int[] region = hiddenColumns.get(i);
271 if (result >= region[0])
273 result += region[1] - region[0] + 1;
280 LOCK.readLock().unlock();
285 * Use this method to find out where a column will appear in the visible
286 * alignment when hidden columns exist. If the column is not visible, then the
287 * left-most visible column will always be returned.
289 * @param hiddenColumn
290 * the column index in the full alignment including hidden columns
291 * @return the position of the column in the visible alignment
293 public int findColumnPosition(int hiddenColumn)
297 LOCK.readLock().lock();
298 int result = hiddenColumn;
299 if (hiddenColumns != null)
305 region = hiddenColumns.get(index++);
306 if (hiddenColumn > region[1])
308 result -= region[1] + 1 - region[0];
310 } while ((hiddenColumn > region[1])
311 && (index < hiddenColumns.size()));
313 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
315 // Here the hidden column is within a region, so
316 // we want to return the position of region[0]-1, adjusted for any
317 // earlier hidden columns.
318 // Calculate the difference between the actual hidden col position
319 // and region[0]-1, and then subtract from result to convert result
321 // the adjusted hiddenColumn value to the adjusted region[0]-1 value
323 // However, if the region begins at 0 we cannot return region[0]-1
331 return result - (hiddenColumn - region[0] + 1);
335 return result; // return the shifted position after removing hidden
339 LOCK.readLock().unlock();
344 * Find the visible column which is a given visible number of columns to the
345 * left of another visible column. i.e. for a startColumn x, the column which
346 * is distance 1 away will be column x-1.
348 * @param visibleDistance
349 * the number of visible columns to offset by
351 * the column to start from
352 * @return the position of the column in the visible alignment
354 public int subtractVisibleColumns(int visibleDistance, int startColumn)
359 LOCK.readLock().lock();
360 int distance = visibleDistance;
362 // in case startColumn is in a hidden region, move it to the left
363 int start = adjustForHiddenColumns(findColumnPosition(startColumn));
365 // get index of hidden region to left of start
366 int index = getHiddenIndexLeft(start);
369 // no hidden regions to left of startColumn
370 return start - distance;
373 // walk backwards through the alignment subtracting the counts of visible
374 // columns from distance
377 int nextstart = start;
379 while ((index > -1) && (distance - gap > 0))
381 // subtract the gap to right of region from distance
385 // calculate the next gap
386 region = hiddenColumns.get(index);
387 gap = start - region[1];
389 // set start to just to left of current region
390 nextstart = region[0] - 1;
394 if (distance - gap > 0)
396 // fell out of loop because there are no more hidden regions
398 return nextstart - distance;
400 return start - distance;
403 LOCK.readLock().unlock();
409 * Use this method to determine the set of hiddenRegion start positions
410 * between absolute position <start> and absolute position <end>
413 * absolute residue to start from
415 * absolute residue to end at
417 * @return list of column numbers in *visible* view where hidden regions start
419 public List<Integer> findHiddenRegionPositions(int start, int end)
423 LOCK.readLock().lock();
424 List<Integer> positions = null;
426 if (hiddenColumns != null)
428 positions = new ArrayList<>(hiddenColumns.size());
430 // navigate to start, keeping count of hidden columns
433 while ((i < hiddenColumns.size())
434 && (hiddenColumns.get(i)[0] < start))
436 int[] region = hiddenColumns.get(i);
437 hiddenSoFar += region[1] - region[0] + 1;
441 // iterate from start to end, adding start positions of each
442 // hidden region. Positions are visible columns count, not absolute
443 while (i < hiddenColumns.size()
444 && (hiddenColumns.get(i)[0] < end))
446 int[] region = hiddenColumns.get(i);
447 positions.add(region[0] - hiddenSoFar);
448 hiddenSoFar += region[1] - region[0] + 1;
454 positions = new ArrayList<>();
460 LOCK.readLock().unlock();
465 * This method returns the rightmost limit of a region of an alignment with
466 * hidden columns. In otherwords, the next hidden column.
471 public int getHiddenBoundaryRight(int alPos)
475 LOCK.readLock().lock();
476 if (hiddenColumns != null)
481 int[] region = hiddenColumns.get(index);
482 if (alPos < region[0])
488 } while (index < hiddenColumns.size());
494 LOCK.readLock().unlock();
500 * This method returns the leftmost limit of a region of an alignment with
501 * hidden columns. In otherwords, the previous hidden column.
506 public int getHiddenBoundaryLeft(int alPos)
510 LOCK.readLock().lock();
512 if (hiddenColumns != null)
514 int index = hiddenColumns.size() - 1;
517 int[] region = hiddenColumns.get(index);
518 if (alPos > region[1])
524 } while (index > -1);
530 LOCK.readLock().unlock();
535 * This method returns the index of the hidden region to the left of a column
536 * position. If the column is in a hidden region it returns the index of the
537 * region to the left. If there is no hidden region to the left it returns -1.
542 private int getHiddenIndexLeft(int pos)
547 LOCK.readLock().lock();
548 if (hiddenColumns != null)
550 int index = hiddenColumns.size() - 1;
553 int[] region = hiddenColumns.get(index);
560 } while (index > -1);
566 LOCK.readLock().unlock();
572 * Adds the specified column range to the hidden columns
577 public void hideColumns(int start, int end)
579 boolean wasAlreadyLocked = false;
582 // check if the write lock was already locked by this thread,
583 // as this method can be called internally in loops within HiddenColumns
584 if (!LOCK.isWriteLockedByCurrentThread())
586 LOCK.writeLock().lock();
590 wasAlreadyLocked = true;
593 if (hiddenColumns == null)
595 hiddenColumns = new ArrayList<>();
599 * traverse existing hidden ranges and insert / amend / append as
602 for (int i = 0; i < hiddenColumns.size(); i++)
604 int[] region = hiddenColumns.get(i);
606 if (end < region[0] - 1)
609 * insert discontiguous preceding range
611 hiddenColumns.add(i, new int[] { start, end });
615 if (end <= region[1])
618 * new range overlaps existing, or is contiguous preceding it - adjust
621 region[0] = Math.min(region[0], start);
625 if (start <= region[1] + 1)
628 * new range overlaps existing, or is contiguous following it - adjust
629 * start and end columns
631 region[0] = Math.min(region[0], start);
632 region[1] = Math.max(region[1], end);
635 * also update or remove any subsequent ranges
636 * that are overlapped
638 while (i < hiddenColumns.size() - 1)
640 int[] nextRegion = hiddenColumns.get(i + 1);
641 if (nextRegion[0] > end + 1)
644 * gap to next hidden range - no more to update
648 region[1] = Math.max(nextRegion[1], end);
649 hiddenColumns.remove(i + 1);
656 * remaining case is that the new range follows everything else
658 hiddenColumns.add(new int[] { start, end });
661 if (!wasAlreadyLocked)
663 LOCK.writeLock().unlock();
668 public boolean isVisible(int column)
672 LOCK.readLock().lock();
674 if (hiddenColumns != null)
676 for (int[] region : hiddenColumns)
678 if (column >= region[0] && column <= region[1])
688 LOCK.readLock().unlock();
692 private ArrayList<int[]> copyHiddenRegionsToArrayList(int startIndex)
695 if (hiddenColumns != null)
697 size = hiddenColumns.size();
699 ArrayList<int[]> copy = new ArrayList<>(size);
701 for (int i = startIndex, j = size; i < j; i++)
705 rh = hiddenColumns.get(i);
708 cp = new int[rh.length];
709 System.arraycopy(rh, 0, cp, 0, rh.length);
717 public String[] getVisibleSequenceStrings(int start, int end,
722 LOCK.readLock().lock();
723 int iSize = seqs.length;
724 String[] selections = new String[iSize];
725 if (hiddenColumns != null && hiddenColumns.size() > 0)
727 for (int i = 0; i < iSize; i++)
729 StringBuffer visibleSeq = new StringBuffer();
731 int blockStart = start;
734 Iterator<int[]> regions = new BoundedHiddenColsIterator(start,
736 while (regions.hasNext())
738 int[] region = regions.next();
739 blockStart = Math.min(blockStart, region[1] + 1);
740 blockEnd = Math.min(blockEnd, region[0]);
742 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
744 blockStart = region[1] + 1;
748 if (end > blockStart)
750 visibleSeq.append(seqs[i].getSequence(blockStart, end));
753 selections[i] = visibleSeq.toString();
758 for (int i = 0; i < iSize; i++)
760 selections[i] = seqs[i].getSequenceAsString(start, end);
767 LOCK.readLock().unlock();
772 * Locate the first and last position visible for this sequence. if seq isn't
773 * visible then return the position of the left and right of the hidden
774 * boundary region, and the corresponding alignment column indices for the
775 * extent of the sequence
778 * @return int[] { visible start, visible end, first seqpos, last seqpos,
779 * alignment index for seq start, alignment index for seq end }
781 public int[] locateVisibleBoundsOfSequence(SequenceI seq)
785 LOCK.readLock().lock();
786 int fpos = seq.getStart();
787 int lpos = seq.getEnd();
790 if (hiddenColumns == null || hiddenColumns.size() == 0)
792 int ifpos = seq.findIndex(fpos) - 1;
793 int ilpos = seq.findIndex(lpos) - 1;
794 return new int[] { ifpos, ifpos, ilpos };
797 // Simply walk along the sequence whilst watching for hidden column
799 List<int[]> regions = getHiddenRegions();
802 int hideStart = seq.getLength();
808 boolean foundStart = false;
809 for (int p = 0, pLen = seq.getLength(); spos <= seq.getEnd()
812 if (!Comparison.isGap(seq.getCharAt(p)))
814 // keep track of first/last column
815 // containing sequence data regardless of visibility
821 // update hidden region start/end
822 while (hideEnd < p && rcount < regions.size())
824 int[] region = regions.get(rcount++);
826 visNext += region[0] - visPrev;
827 hideStart = region[0];
832 hideStart = seq.getLength();
834 // update visible boundary for sequence
845 // look for next sequence position
851 return new int[] { findColumnPosition(start), firstP, lastP };
853 // otherwise, sequence was completely hidden
854 return new int[] { visPrev, firstP, lastP };
857 LOCK.readLock().unlock();
862 * delete any columns in alignmentAnnotation that are hidden (including
863 * sequence associated annotation).
865 * @param alignmentAnnotation
867 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
869 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
873 * delete any columns in alignmentAnnotation that are hidden (including
874 * sequence associated annotation).
877 * remove any annotation to the right of this column
879 * remove any annotation to the left of this column
880 * @param alignmentAnnotation
881 * the annotation to operate on
883 public void makeVisibleAnnotation(int start, int end,
884 AlignmentAnnotation alignmentAnnotation)
888 LOCK.readLock().lock();
890 int startFrom = start;
893 if (alignmentAnnotation.annotations != null)
895 if (start == end && end == -1)
898 endAt = alignmentAnnotation.annotations.length;
900 if (hiddenColumns != null && hiddenColumns.size() > 0)
902 removeHiddenAnnotation(startFrom, endAt, alignmentAnnotation);
906 alignmentAnnotation.restrict(startFrom, endAt);
911 LOCK.readLock().unlock();
915 private void removeHiddenAnnotation(int start, int end,
916 AlignmentAnnotation alignmentAnnotation)
918 // mangle the alignmentAnnotation annotation array
919 Vector<Annotation[]> annels = new Vector<>();
920 Annotation[] els = null;
921 int blockStart = start;
925 Iterator<int[]> regions = new BoundedHiddenColsIterator(start, end,
927 while (regions.hasNext())
929 int[] region = regions.next();
930 blockStart = Math.min(blockStart, region[1] + 1);
931 blockEnd = Math.min(blockEnd, region[0]);
933 els = new Annotation[blockEnd - blockStart];
934 annels.addElement(els);
935 System.arraycopy(alignmentAnnotation.annotations, blockStart, els, 0,
939 blockStart = region[1] + 1;
943 if (end > blockStart)
945 els = new Annotation[end - blockStart + 1];
946 annels.addElement(els);
948 + blockStart) <= alignmentAnnotation.annotations.length)
950 // copy just the visible segment of the annotation row
951 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
956 // copy to the end of the annotation row
957 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
958 0, (alignmentAnnotation.annotations.length - blockStart));
965 alignmentAnnotation.annotations = new Annotation[w];
968 for (Annotation[] chnk : annels)
970 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
979 * @return true if there are columns hidden
981 public boolean hasHiddenColumns()
985 LOCK.readLock().lock();
986 return hiddenColumns != null && hiddenColumns.size() > 0;
989 LOCK.readLock().unlock();
995 * @return true if there are more than one set of columns hidden
997 public boolean hasManyHiddenColumns()
1001 LOCK.readLock().lock();
1002 return hiddenColumns != null && hiddenColumns.size() > 1;
1005 LOCK.readLock().unlock();
1010 * mark the columns corresponding to gap characters as hidden in the column
1015 public void hideInsertionsFor(SequenceI sr)
1019 LOCK.writeLock().lock();
1020 List<int[]> inserts = sr.getInsertions();
1021 for (int[] r : inserts)
1023 hideColumns(r[0], r[1]);
1027 LOCK.writeLock().unlock();
1032 * Unhides, and adds to the selection list, all hidden columns
1034 public void revealAllHiddenColumns(ColumnSelection sel)
1038 LOCK.writeLock().lock();
1039 if (hiddenColumns != null)
1041 for (int i = 0; i < hiddenColumns.size(); i++)
1043 int[] region = hiddenColumns.get(i);
1044 for (int j = region[0]; j < region[1] + 1; j++)
1051 hiddenColumns = null;
1054 LOCK.writeLock().unlock();
1059 * Reveals, and marks as selected, the hidden column range with the given
1064 public void revealHiddenColumns(int start, ColumnSelection sel)
1068 LOCK.writeLock().lock();
1069 for (int i = 0; i < hiddenColumns.size(); i++)
1071 int[] region = hiddenColumns.get(i);
1072 if (start == region[0])
1074 for (int j = region[0]; j < region[1] + 1; j++)
1079 hiddenColumns.remove(region);
1083 if (hiddenColumns.size() == 0)
1085 hiddenColumns = null;
1089 LOCK.writeLock().unlock();
1094 * Add gaps into the sequences aligned to profileseq under the given
1099 * - alignment to have gaps inserted into it
1101 * - alignment view where sequence corresponding to profileseq is
1103 * @return new HiddenColumns for new alignment view, with insertions into
1104 * profileseq marked as hidden.
1106 public static HiddenColumns propagateInsertions(SequenceI profileseq,
1107 AlignmentI al, AlignmentView input)
1111 char gc = al.getGapCharacter();
1112 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
1113 HiddenColumns nview = (HiddenColumns) alandhidden[1];
1114 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
1115 nview.propagateInsertions(profileseq, al, origseq);
1122 * - sequence in al which corresponds to origseq
1124 * - alignment which is to have gaps inserted into it
1126 * - sequence corresponding to profileseq which defines gap map for
1129 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
1132 char gc = al.getGapCharacter();
1134 // take the set of hidden columns, and the set of gaps in origseq,
1135 // and remove all the hidden gaps from hiddenColumns
1137 // first get the gaps as a Bitset
1138 BitSet gaps = origseq.gapBitset();
1140 // now calculate hidden ^ not(gap)
1141 BitSet hidden = new BitSet();
1142 markHiddenRegions(hidden);
1143 hidden.andNot(gaps);
1144 hiddenColumns = null;
1145 this.hideMarkedBits(hidden);
1147 // for each sequence in the alignment, except the profile sequence,
1148 // insert gaps corresponding to each hidden region
1149 // but where each hidden column region is shifted backwards by the number of
1150 // preceding visible gaps
1151 // update hidden columns at the same time
1152 Iterator<int[]> regions = iterator();
1153 ArrayList<int[]> newhidden = new ArrayList<>();
1155 int numGapsBefore = 0;
1156 int gapPosition = 0;
1157 while (regions.hasNext())
1159 // get region coordinates accounting for gaps
1160 // we can rely on gaps not being *in* hidden regions because we already
1162 int[] region = regions.next();
1163 while (gapPosition < region[0])
1166 if (gaps.get(gapPosition))
1172 int left = region[0] - numGapsBefore;
1173 int right = region[1] - numGapsBefore;
1174 newhidden.add(new int[] { left, right });
1176 // make a string with number of gaps = length of hidden region
1177 StringBuffer sb = new StringBuffer();
1178 for (int s = 0; s < right - left + 1; s++)
1182 padGaps(sb, left, profileseq, al);
1185 hiddenColumns = newhidden;
1189 * Pad gaps in all sequences in alignment except profileseq
1192 * gap string to insert
1194 * position to insert at
1196 * sequence not to pad
1198 * alignment to pad sequences in
1200 private void padGaps(StringBuffer sb, int pos, SequenceI profileseq,
1203 // loop over the sequences and pad with gaps where required
1204 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1206 SequenceI sqobj = al.getSequenceAt(s);
1207 if (sqobj != profileseq)
1209 String sq = al.getSequenceAt(s).getSequenceAsString();
1210 if (sq.length() <= pos)
1213 int diff = pos - sq.length() - 1;
1218 while ((diff = pos - sq.length() - 1) > 0)
1220 if (diff >= sb.length())
1222 sq += sb.toString();
1226 char[] buf = new char[diff];
1227 sb.getChars(0, diff, buf, 0);
1228 sq += buf.toString();
1232 sq += sb.toString();
1236 al.getSequenceAt(s).setSequence(
1237 sq.substring(0, pos) + sb.toString() + sq.substring(pos));
1244 * Returns a hashCode built from hidden column ranges
1247 public int hashCode()
1251 LOCK.readLock().lock();
1253 if (hiddenColumns != null)
1255 for (int[] hidden : hiddenColumns)
1257 hashCode = HASH_MULTIPLIER * hashCode + hidden[0];
1258 hashCode = HASH_MULTIPLIER * hashCode + hidden[1];
1264 LOCK.readLock().unlock();
1269 * Hide columns corresponding to the marked bits
1272 * - columns map to bits starting from zero
1274 public void hideMarkedBits(BitSet inserts)
1278 LOCK.writeLock().lock();
1279 for (int firstSet = inserts
1280 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1281 .nextSetBit(lastSet))
1283 lastSet = inserts.nextClearBit(firstSet);
1284 hideColumns(firstSet, lastSet - 1);
1288 LOCK.writeLock().unlock();
1295 * BitSet where hidden columns will be marked
1297 public void markHiddenRegions(BitSet inserts)
1301 LOCK.readLock().lock();
1302 if (hiddenColumns == null)
1306 for (int[] range : hiddenColumns)
1308 inserts.set(range[0], range[1] + 1);
1312 LOCK.readLock().unlock();
1317 * Calculate the visible start and end index of an alignment.
1320 * full alignment width
1321 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1323 public int[] getVisibleStartAndEndIndex(int width)
1327 LOCK.readLock().lock();
1328 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1329 int startPos = alignmentStartEnd[0];
1330 int endPos = alignmentStartEnd[1];
1332 int[] lowestRange = new int[] { -1, -1 };
1333 int[] higestRange = new int[] { -1, -1 };
1335 if (hiddenColumns == null)
1337 return new int[] { startPos, endPos };
1340 for (int[] hiddenCol : hiddenColumns)
1342 lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
1343 higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
1346 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1348 startPos = alignmentStartEnd[0];
1352 startPos = lowestRange[1] + 1;
1355 if (higestRange[0] == -1 && higestRange[1] == -1)
1357 endPos = alignmentStartEnd[1];
1361 endPos = higestRange[0] - 1;
1363 return new int[] { startPos, endPos };
1366 LOCK.readLock().unlock();
1372 * Finds the hidden region (if any) which starts or ends at res
1375 * visible residue position, unadjusted for hidden columns
1376 * @return region as [start,end] or null if no matching region is found
1378 public int[] getRegionWithEdgeAtRes(int res)
1382 LOCK.readLock().lock();
1383 int adjres = adjustForHiddenColumns(res);
1385 int[] reveal = null;
1386 if (hiddenColumns != null)
1388 for (int[] region : hiddenColumns)
1390 if (adjres + 1 == region[0] || adjres - 1 == region[1])
1400 LOCK.readLock().unlock();
1405 * Return an iterator over the hidden regions
1407 public Iterator<int[]> iterator()
1409 if (hiddenColumns != null)
1411 int last = hiddenColumns.get(hiddenColumns.size() - 1)[1];
1412 return new BoundedHiddenColsIterator(0, last, true);
1416 return new BoundedHiddenColsIterator(0, 0, true);
1421 * Return a bounded iterator over the hidden regions
1424 * position to start from (inclusive, absolute column position)
1426 * position to end at (inclusive, absolute column position)
1429 public Iterator<int[]> getBoundedIterator(int start, int end)
1431 return new BoundedHiddenColsIterator(start, end, true);
1435 * Return a bounded iterator over the *visible* start positions of hidden
1439 * position to start from (inclusive, visible column position)
1441 * position to end at (inclusive, visible column position)
1443 public Iterator<Integer> getBoundedStartIterator(int start, int end)
1445 return new BoundedStartRegionIterator(start, end, true);
1449 * Return an iterator over visible columns between the given start and end
1453 * first column (inclusive)
1455 * last column (inclusive)
1457 public Iterator<Integer> getVisibleColsIterator(int start, int end)
1459 return new VisibleColsIterator(start, end, true);
1463 * return an iterator over visible segments between the given start and end
1467 * (first column inclusive from 0)
1469 * (last column - not inclusive)
1471 public Iterator<int[]> getVisContigsIterator(int start, int end)
1473 return new VisibleContigsIterator(start, end, true);
1477 * An iterator which iterates over hidden column regions in a range.
1479 private class BoundedHiddenColsIterator implements Iterator<int[]>
1481 private int start; // start position to iterate from
1483 private int end; // end position to iterate to
1485 // current index in hiddenColumns
1486 private int currentPosition = 0;
1488 // current column in hiddenColumns
1489 private int[] currentRegion;
1491 // whether to make a local copy of hiddenColumns
1492 private final boolean useCopy;
1494 // local copy or reference to hiddenColumns
1495 private List<int[]> localHidden;
1498 * Construct an iterator over hiddenColums bounded at
1499 * [lowerBound,upperBound]
1502 * lower bound to iterate from
1504 * upper bound to iterate to
1505 * @param useCopyCols
1506 * whether to make a local copy of hiddenColumns for iteration (set
1507 * to true if calling from outwith the HiddenColumns class)
1509 BoundedHiddenColsIterator(int lowerBound, int upperBound,
1510 boolean useCopyCols)
1514 useCopy = useCopyCols;
1520 // assume that if useCopy is false the calling code has locked
1522 LOCK.readLock().lock();
1525 if (hiddenColumns != null)
1527 localHidden = new ArrayList<>();
1529 // iterate until a region overlaps with [start,end]
1531 while ((i < hiddenColumns.size())
1532 && (hiddenColumns.get(i)[1] < start))
1537 // iterate from start to end, adding each hidden region. Positions are
1538 // absolute, and all regions which *overlap* [start,end] are added.
1539 while (i < hiddenColumns.size()
1540 && (hiddenColumns.get(i)[0] <= end))
1544 rh = hiddenColumns.get(i);
1547 cp = new int[rh.length];
1548 System.arraycopy(rh, 0, cp, 0, rh.length);
1549 localHidden.add(cp);
1559 LOCK.readLock().unlock();
1565 public boolean hasNext()
1567 return (localHidden != null)
1568 && (currentPosition < localHidden.size());
1574 currentRegion = localHidden.get(currentPosition);
1576 return currentRegion;
1581 * An iterator which iterates over visible start positions of hidden column
1582 * regions in a range.
1584 private class BoundedStartRegionIterator implements Iterator<Integer>
1586 // start position to iterate from
1589 // end position to iterate to
1592 // current index in hiddenColumns
1593 private int currentPosition = 0;
1595 // local copy or reference to hiddenColumns
1596 private List<Integer> positions = null;
1599 * Construct an iterator over hiddenColums bounded at
1600 * [lowerBound,upperBound]
1603 * lower bound to iterate from
1605 * upper bound to iterate to
1606 * @param useCopyCols
1607 * whether to make a local copy of hiddenColumns for iteration (set
1608 * to true if calling from outwith the HiddenColumns class)
1610 BoundedStartRegionIterator(int lowerBound, int upperBound,
1620 // assume that if useCopy is false the calling code has locked
1622 LOCK.readLock().lock();
1625 if (hiddenColumns != null)
1627 positions = new ArrayList<>(hiddenColumns.size());
1629 // navigate to start, keeping count of hidden columns
1631 int hiddenSoFar = 0;
1632 while ((i < hiddenColumns.size())
1633 && (hiddenColumns.get(i)[0] < start + hiddenSoFar))
1635 int[] region = hiddenColumns.get(i);
1636 hiddenSoFar += region[1] - region[0] + 1;
1640 // iterate from start to end, adding start positions of each
1641 // hidden region. Positions are visible columns count, not absolute
1642 while (i < hiddenColumns.size()
1643 && (hiddenColumns.get(i)[0] <= end + hiddenSoFar))
1645 int[] region = hiddenColumns.get(i);
1646 positions.add(region[0] - hiddenSoFar);
1647 hiddenSoFar += region[1] - region[0] + 1;
1653 positions = new ArrayList<>();
1659 LOCK.readLock().unlock();
1665 public boolean hasNext()
1667 return (currentPosition < positions.size());
1671 * Get next hidden region start position
1673 * @return the start position in *visible* coordinates
1676 public Integer next()
1678 int result = positions.get(currentPosition);
1684 private class VisibleColsIterator implements Iterator<Integer>
1688 private int current;
1692 private List<int[]> localHidden = new ArrayList<>();
1694 private int lasthiddenregion;
1696 VisibleColsIterator(int firstcol, int lastcol, boolean useCopy)
1701 lasthiddenregion = -1;
1707 // assume that if useCopy is false the calling code has locked
1709 LOCK.readLock().lock();
1712 if (hiddenColumns != null)
1715 for (i = 0; i < hiddenColumns.size(); ++i)
1717 if (current >= hiddenColumns.get(i)[0]
1718 && current <= hiddenColumns.get(i)[1])
1720 // current is hidden, move to right
1721 current = hiddenColumns.get(i)[1] + 1;
1724 if (current < hiddenColumns.get(i)[0])
1730 lasthiddenregion = i - 1;
1732 for (i = hiddenColumns.size() - 1; i >= 0; --i)
1734 if (last >= hiddenColumns.get(i)[0]
1735 && last <= hiddenColumns.get(i)[1])
1737 // last is hidden, move to left
1738 last = hiddenColumns.get(i)[0] - 1;
1740 if (last > hiddenColumns.get(i)[1])
1746 // make a local copy of the bit we need
1747 i = lasthiddenregion + 1;
1748 while (i < hiddenColumns.size()
1749 && hiddenColumns.get(i)[0] <= last)
1751 int[] region = new int[] { hiddenColumns.get(i)[0],
1752 hiddenColumns.get(i)[1] };
1753 localHidden.add(region);
1756 lasthiddenregion = -1;
1762 LOCK.readLock().unlock();
1768 public boolean hasNext()
1770 return next <= last;
1774 public Integer next()
1778 throw new NoSuchElementException();
1781 if ((localHidden != null)
1782 && (lasthiddenregion + 1 < localHidden.size()))
1784 // still some more hidden regions
1785 if (next + 1 < localHidden.get(lasthiddenregion + 1)[0])
1787 // next+1 is still before the next hidden region
1790 else if ((next + 1 >= localHidden.get(lasthiddenregion + 1)[0])
1791 && (next + 1 <= localHidden.get(lasthiddenregion + 1)[1]))
1793 // next + 1 is in the next hidden region
1794 next = localHidden.get(lasthiddenregion + 1)[1] + 1;
1800 // finished with hidden regions, just increment normally
1807 public void remove()
1809 throw new UnsupportedOperationException();
1814 * An iterator which iterates over visible regions in a range.
1816 private class VisibleContigsIterator implements Iterator<int[]>
1818 private List<int[]> vcontigs = new ArrayList<>();
1820 private int currentPosition = 0;
1822 VisibleContigsIterator(int start, int end, boolean usecopy)
1828 LOCK.readLock().lock();
1831 if (hiddenColumns != null && hiddenColumns.size() > 0)
1837 for (int[] region : hiddenColumns)
1839 hideStart = region[0];
1840 hideEnd = region[1];
1842 // navigate to start
1843 if (hideEnd < vstart)
1847 if (hideStart > vstart)
1849 int[] contig = new int[] { vstart, hideStart - 1 };
1850 vcontigs.add(contig);
1852 vstart = hideEnd + 1;
1854 // exit if we're past the end
1863 int[] contig = new int[] { vstart, end - 1 };
1864 vcontigs.add(contig);
1869 int[] contig = new int[] { start, end - 1 };
1870 vcontigs.add(contig);
1876 LOCK.readLock().unlock();
1882 public boolean hasNext()
1884 return (currentPosition < vcontigs.size());
1890 int[] result = vcontigs.get(currentPosition);