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);
718 * return all visible segments between the given start and end boundaries
721 * (first column inclusive from 0)
723 * (last column - not inclusive)
724 * @return List<int[]> {[i_start, i_end], ..} where intervals lie in
725 * start<=i_start<=i_end<end
727 public List<int[]> getVisibleContigs(int start, int end)
731 LOCK.readLock().lock();
732 List<int[]> vcontigs = new ArrayList<>();
733 if (hiddenColumns != null && hiddenColumns.size() > 0)
739 for (int[] region : hiddenColumns)
741 hideStart = region[0];
745 if (hideEnd < vstart)
749 if (hideStart > vstart)
751 int[] contig = new int[] { vstart, hideStart - 1 };
752 vcontigs.add(contig);
754 vstart = hideEnd + 1;
756 // exit if we're past the end
765 int[] contig = new int[] { vstart, end - 1 };
766 vcontigs.add(contig);
771 int[] contig = new int[] { start, end - 1 };
772 vcontigs.add(contig);
777 LOCK.readLock().unlock();
781 public String[] getVisibleSequenceStrings(int start, int end,
786 LOCK.readLock().lock();
787 int iSize = seqs.length;
788 String[] selections = new String[iSize];
789 if (hiddenColumns != null && hiddenColumns.size() > 0)
791 for (int i = 0; i < iSize; i++)
793 StringBuffer visibleSeq = new StringBuffer();
795 int blockStart = start;
800 for (int[] region : hiddenColumns)
802 hideStart = region[0];
805 if (hideStart < start)
810 blockStart = Math.min(blockStart, hideEnd + 1);
811 blockEnd = Math.min(blockEnd, hideStart);
813 if (blockStart > blockEnd)
818 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
820 blockStart = hideEnd + 1;
824 if (end > blockStart)
826 visibleSeq.append(seqs[i].getSequence(blockStart, end));
829 selections[i] = visibleSeq.toString();
834 for (int i = 0; i < iSize; i++)
836 selections[i] = seqs[i].getSequenceAsString(start, end);
843 LOCK.readLock().unlock();
848 * Locate the first and last position visible for this sequence. if seq isn't
849 * visible then return the position of the left and right of the hidden
850 * boundary region, and the corresponding alignment column indices for the
851 * extent of the sequence
854 * @return int[] { visible start, visible end, first seqpos, last seqpos,
855 * alignment index for seq start, alignment index for seq end }
857 public int[] locateVisibleBoundsOfSequence(SequenceI seq)
861 LOCK.readLock().lock();
862 int fpos = seq.getStart();
863 int lpos = seq.getEnd();
866 if (hiddenColumns == null || hiddenColumns.size() == 0)
868 int ifpos = seq.findIndex(fpos) - 1;
869 int ilpos = seq.findIndex(lpos) - 1;
870 return new int[] { ifpos, ifpos, ilpos };
873 // Simply walk along the sequence whilst watching for hidden column
875 List<int[]> regions = getHiddenRegions();
878 int hideStart = seq.getLength();
884 boolean foundStart = false;
885 for (int p = 0, pLen = seq.getLength(); spos <= seq.getEnd()
888 if (!Comparison.isGap(seq.getCharAt(p)))
890 // keep track of first/last column
891 // containing sequence data regardless of visibility
897 // update hidden region start/end
898 while (hideEnd < p && rcount < regions.size())
900 int[] region = regions.get(rcount++);
902 visNext += region[0] - visPrev;
903 hideStart = region[0];
908 hideStart = seq.getLength();
910 // update visible boundary for sequence
921 // look for next sequence position
927 return new int[] { findColumnPosition(start), firstP, lastP };
929 // otherwise, sequence was completely hidden
930 return new int[] { visPrev, firstP, lastP };
933 LOCK.readLock().unlock();
938 * delete any columns in alignmentAnnotation that are hidden (including
939 * sequence associated annotation).
941 * @param alignmentAnnotation
943 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
945 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
949 * delete any columns in alignmentAnnotation that are hidden (including
950 * sequence associated annotation).
953 * remove any annotation to the right of this column
955 * remove any annotation to the left of this column
956 * @param alignmentAnnotation
957 * the annotation to operate on
959 public void makeVisibleAnnotation(int start, int end,
960 AlignmentAnnotation alignmentAnnotation)
964 LOCK.readLock().lock();
965 if (alignmentAnnotation.annotations == null)
969 if (start == end && end == -1)
972 end = alignmentAnnotation.annotations.length;
974 if (hiddenColumns != null && hiddenColumns.size() > 0)
976 // then mangle the alignmentAnnotation annotation array
977 Vector<Annotation[]> annels = new Vector<>();
978 Annotation[] els = null;
979 int blockStart = start;
985 for (int[] region : hiddenColumns)
987 hideStart = region[0];
990 if (hideStart < start)
995 blockStart = Math.min(blockStart, hideEnd + 1);
996 blockEnd = Math.min(blockEnd, hideStart);
998 if (blockStart > blockEnd)
1003 els = new Annotation[blockEnd - blockStart];
1004 annels.addElement(els);
1005 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
1008 blockStart = hideEnd + 1;
1012 if (end > blockStart)
1014 els = new Annotation[end - blockStart + 1];
1015 annels.addElement(els);
1017 + blockStart) <= alignmentAnnotation.annotations.length)
1019 // copy just the visible segment of the annotation row
1020 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1021 els, 0, els.length);
1025 // copy to the end of the annotation row
1026 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1028 (alignmentAnnotation.annotations.length - blockStart));
1037 alignmentAnnotation.annotations = new Annotation[w];
1040 for (Annotation[] chnk : annels)
1042 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1049 alignmentAnnotation.restrict(start, end);
1053 LOCK.readLock().unlock();
1059 * @return true if there are columns hidden
1061 public boolean hasHiddenColumns()
1065 LOCK.readLock().lock();
1066 return hiddenColumns != null && hiddenColumns.size() > 0;
1069 LOCK.readLock().unlock();
1075 * @return true if there are more than one set of columns hidden
1077 public boolean hasManyHiddenColumns()
1081 LOCK.readLock().lock();
1082 return hiddenColumns != null && hiddenColumns.size() > 1;
1085 LOCK.readLock().unlock();
1090 * mark the columns corresponding to gap characters as hidden in the column
1095 public void hideInsertionsFor(SequenceI sr)
1099 LOCK.writeLock().lock();
1100 List<int[]> inserts = sr.getInsertions();
1101 for (int[] r : inserts)
1103 hideColumns(r[0], r[1]);
1107 LOCK.writeLock().unlock();
1112 * Unhides, and adds to the selection list, all hidden columns
1114 public void revealAllHiddenColumns(ColumnSelection sel)
1118 LOCK.writeLock().lock();
1119 if (hiddenColumns != null)
1121 for (int i = 0; i < hiddenColumns.size(); i++)
1123 int[] region = hiddenColumns.get(i);
1124 for (int j = region[0]; j < region[1] + 1; j++)
1131 hiddenColumns = null;
1134 LOCK.writeLock().unlock();
1139 * Reveals, and marks as selected, the hidden column range with the given
1144 public void revealHiddenColumns(int start, ColumnSelection sel)
1148 LOCK.writeLock().lock();
1149 for (int i = 0; i < hiddenColumns.size(); i++)
1151 int[] region = hiddenColumns.get(i);
1152 if (start == region[0])
1154 for (int j = region[0]; j < region[1] + 1; j++)
1159 hiddenColumns.remove(region);
1163 if (hiddenColumns.size() == 0)
1165 hiddenColumns = null;
1169 LOCK.writeLock().unlock();
1174 * Add gaps into the sequences aligned to profileseq under the given
1179 * - alignment to have gaps inserted into it
1181 * - alignment view where sequence corresponding to profileseq is
1183 * @return new HiddenColumns for new alignment view, with insertions into
1184 * profileseq marked as hidden.
1186 public static HiddenColumns propagateInsertions(SequenceI profileseq,
1187 AlignmentI al, AlignmentView input)
1191 char gc = al.getGapCharacter();
1192 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
1193 HiddenColumns nview = (HiddenColumns) alandhidden[1];
1194 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
1195 nview.propagateInsertions(profileseq, al, origseq);
1202 * - sequence in al which corresponds to origseq
1204 * - alignment which is to have gaps inserted into it
1206 * - sequence corresponding to profileseq which defines gap map for
1209 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
1212 char gc = al.getGapCharacter();
1214 // take the set of hidden columns, and the set of gaps in origseq,
1215 // and remove all the hidden gaps from hiddenColumns
1217 // first get the gaps as a Bitset
1218 BitSet gaps = origseq.gapBitset();
1220 // now calculate hidden ^ not(gap)
1221 BitSet hidden = new BitSet();
1222 markHiddenRegions(hidden);
1223 hidden.andNot(gaps);
1224 hiddenColumns = null;
1225 this.hideMarkedBits(hidden);
1227 // for each sequence in the alignment, except the profile sequence,
1228 // insert gaps corresponding to each hidden region
1229 // but where each hidden column region is shifted backwards by the number of
1230 // preceding visible gaps
1231 // update hidden columns at the same time
1232 Iterator<int[]> regions = iterator();
1233 ArrayList<int[]> newhidden = new ArrayList<>();
1235 int numGapsBefore = 0;
1236 int gapPosition = 0;
1237 while (regions.hasNext())
1239 // get region coordinates accounting for gaps
1240 // we can rely on gaps not being *in* hidden regions because we already
1242 int[] region = regions.next();
1243 while (gapPosition < region[0])
1246 if (gaps.get(gapPosition))
1252 int left = region[0] - numGapsBefore;
1253 int right = region[1] - numGapsBefore;
1254 newhidden.add(new int[] { left, right });
1256 // make a string with number of gaps = length of hidden region
1257 StringBuffer sb = new StringBuffer();
1258 for (int s = 0; s < right - left + 1; s++)
1262 padGaps(sb, left, profileseq, al);
1265 hiddenColumns = newhidden;
1269 * Pad gaps in all sequences in alignment except profileseq
1272 * gap string to insert
1274 * position to insert at
1276 * sequence not to pad
1278 * alignment to pad sequences in
1280 private void padGaps(StringBuffer sb, int pos, SequenceI profileseq,
1283 // loop over the sequences and pad with gaps where required
1284 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1286 SequenceI sqobj = al.getSequenceAt(s);
1287 if (sqobj != profileseq)
1289 String sq = al.getSequenceAt(s).getSequenceAsString();
1290 if (sq.length() <= pos)
1293 int diff = pos - sq.length() - 1;
1298 while ((diff = pos - sq.length() - 1) > 0)
1300 if (diff >= sb.length())
1302 sq += sb.toString();
1306 char[] buf = new char[diff];
1307 sb.getChars(0, diff, buf, 0);
1308 sq += buf.toString();
1312 sq += sb.toString();
1316 al.getSequenceAt(s).setSequence(
1317 sq.substring(0, pos) + sb.toString() + sq.substring(pos));
1324 * Returns a hashCode built from hidden column ranges
1327 public int hashCode()
1331 LOCK.readLock().lock();
1333 if (hiddenColumns != null)
1335 for (int[] hidden : hiddenColumns)
1337 hashCode = HASH_MULTIPLIER * hashCode + hidden[0];
1338 hashCode = HASH_MULTIPLIER * hashCode + hidden[1];
1344 LOCK.readLock().unlock();
1349 * Hide columns corresponding to the marked bits
1352 * - columns map to bits starting from zero
1354 public void hideMarkedBits(BitSet inserts)
1358 LOCK.writeLock().lock();
1359 for (int firstSet = inserts
1360 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1361 .nextSetBit(lastSet))
1363 lastSet = inserts.nextClearBit(firstSet);
1364 hideColumns(firstSet, lastSet - 1);
1368 LOCK.writeLock().unlock();
1375 * BitSet where hidden columns will be marked
1377 public void markHiddenRegions(BitSet inserts)
1381 LOCK.readLock().lock();
1382 if (hiddenColumns == null)
1386 for (int[] range : hiddenColumns)
1388 inserts.set(range[0], range[1] + 1);
1392 LOCK.readLock().unlock();
1397 * Calculate the visible start and end index of an alignment.
1400 * full alignment width
1401 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1403 public int[] getVisibleStartAndEndIndex(int width)
1407 LOCK.readLock().lock();
1408 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1409 int startPos = alignmentStartEnd[0];
1410 int endPos = alignmentStartEnd[1];
1412 int[] lowestRange = new int[] { -1, -1 };
1413 int[] higestRange = new int[] { -1, -1 };
1415 if (hiddenColumns == null)
1417 return new int[] { startPos, endPos };
1420 for (int[] hiddenCol : hiddenColumns)
1422 lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
1423 higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
1426 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1428 startPos = alignmentStartEnd[0];
1432 startPos = lowestRange[1] + 1;
1435 if (higestRange[0] == -1 && higestRange[1] == -1)
1437 endPos = alignmentStartEnd[1];
1441 endPos = higestRange[0] - 1;
1443 return new int[] { startPos, endPos };
1446 LOCK.readLock().unlock();
1452 * Finds the hidden region (if any) which starts or ends at res
1455 * visible residue position, unadjusted for hidden columns
1456 * @return region as [start,end] or null if no matching region is found
1458 public int[] getRegionWithEdgeAtRes(int res)
1462 LOCK.readLock().lock();
1463 int adjres = adjustForHiddenColumns(res);
1465 int[] reveal = null;
1466 if (hiddenColumns != null)
1468 for (int[] region : hiddenColumns)
1470 if (adjres + 1 == region[0] || adjres - 1 == region[1])
1480 LOCK.readLock().unlock();
1484 public Iterator<int[]> iterator()
1486 if (hiddenColumns != null)
1488 int last = hiddenColumns.get(hiddenColumns.size() - 1)[1];
1489 return new BoundedHiddenColsIterator(0, last, true);
1493 return new BoundedHiddenColsIterator(0, 0, true);
1497 public Iterator<int[]> getBoundedIterator(int start, int end)
1499 return new BoundedHiddenColsIterator(start, end, true);
1502 public Iterator<Integer> getBoundedStartIterator(int start, int end)
1504 return new BoundedStartRegionIterator(start, end, true);
1507 public Iterator<Integer> getVisibleColsIterator(int start, int end)
1509 return new VisibleColsIterator(start, end, true);
1513 * An iterator which iterates over hidden column regions in a range.
1518 class BoundedHiddenColsIterator implements Iterator<int[]>
1520 private int start; // start position to iterate from
1522 private int end; // end position to iterate to
1524 // current index in hiddenColumns
1525 private int currentPosition = 0;
1527 // current column in hiddenColumns
1528 private int[] currentRegion;
1530 // whether to make a local copy of hiddenColumns
1531 private final boolean useCopy;
1533 // local copy or reference to hiddenColumns
1534 private List<int[]> localHidden;
1537 * Construct an iterator over hiddenColums bounded at
1538 * [lowerBound,upperBound]
1541 * lower bound to iterate from
1543 * upper bound to iterate to
1544 * @param useCopyCols
1545 * whether to make a local copy of hiddenColumns for iteration (set
1546 * to true if calling from outwith the HiddenColumns class)
1548 BoundedHiddenColsIterator(int lowerBound, int upperBound,
1549 boolean useCopyCols)
1553 useCopy = useCopyCols;
1559 // assume that if useCopy is false the calling code has locked
1561 LOCK.readLock().lock();
1564 if (hiddenColumns != null)
1566 localHidden = new ArrayList<>();
1568 // iterate until a region overlaps with [start,end]
1570 while ((i < hiddenColumns.size())
1571 && (hiddenColumns.get(i)[1] < start))
1576 // iterate from start to end, adding each hidden region. Positions are
1577 // absolute, and all regions which *overlap* [start,end] are added.
1578 while (i < hiddenColumns.size()
1579 && (hiddenColumns.get(i)[0] <= end))
1583 rh = hiddenColumns.get(i);
1586 cp = new int[rh.length];
1587 System.arraycopy(rh, 0, cp, 0, rh.length);
1588 localHidden.add(cp);
1598 LOCK.readLock().unlock();
1604 public boolean hasNext()
1606 return (localHidden != null)
1607 && (currentPosition < localHidden.size());
1613 currentRegion = localHidden.get(currentPosition);
1615 return currentRegion;
1619 class BoundedStartRegionIterator implements Iterator<Integer>
1622 private int start; // start position to iterate from
1624 private int end; // end position to iterate to
1626 // current index in hiddenColumns
1627 private int currentPosition = 0;
1629 // local copy or reference to hiddenColumns
1630 private List<Integer> positions = null;
1633 * Construct an iterator over hiddenColums bounded at
1634 * [lowerBound,upperBound]
1637 * lower bound to iterate from
1639 * upper bound to iterate to
1640 * @param useCopyCols
1641 * whether to make a local copy of hiddenColumns for iteration (set
1642 * to true if calling from outwith the HiddenColumns class)
1644 BoundedStartRegionIterator(int lowerBound, int upperBound,
1654 // assume that if useCopy is false the calling code has locked
1656 LOCK.readLock().lock();
1659 if (hiddenColumns != null)
1661 positions = new ArrayList<>(hiddenColumns.size());
1663 // navigate to start, keeping count of hidden columns
1665 int hiddenSoFar = 0;
1666 while ((i < hiddenColumns.size())
1667 && (hiddenColumns.get(i)[0] < start + hiddenSoFar))
1669 int[] region = hiddenColumns.get(i);
1670 hiddenSoFar += region[1] - region[0] + 1;
1674 // iterate from start to end, adding start positions of each
1675 // hidden region. Positions are visible columns count, not absolute
1676 while (i < hiddenColumns.size()
1677 && (hiddenColumns.get(i)[0] <= end + hiddenSoFar))
1679 int[] region = hiddenColumns.get(i);
1680 positions.add(region[0] - hiddenSoFar);
1681 hiddenSoFar += region[1] - region[0] + 1;
1687 positions = new ArrayList<>();
1693 LOCK.readLock().unlock();
1699 public boolean hasNext()
1701 return (currentPosition < positions.size());
1705 public Integer next()
1707 int result = positions.get(currentPosition);
1713 public class VisibleColsIterator implements Iterator<Integer>
1717 private int current;
1721 private List<int[]> localHidden = new ArrayList<>();
1723 private int lasthiddenregion;
1725 public VisibleColsIterator(int firstcol, int lastcol, boolean useCopy)
1730 lasthiddenregion = -1;
1736 // assume that if useCopy is false the calling code has locked
1738 LOCK.readLock().lock();
1741 if (hiddenColumns != null)
1744 for (i = 0; i < hiddenColumns.size(); ++i)
1746 if (current >= hiddenColumns.get(i)[0]
1747 && current <= hiddenColumns.get(i)[1])
1749 // current is hidden, move to right
1750 current = hiddenColumns.get(i)[1] + 1;
1753 if (current < hiddenColumns.get(i)[0])
1759 lasthiddenregion = i - 1;
1761 for (i = hiddenColumns.size() - 1; i >= 0; --i)
1763 if (last >= hiddenColumns.get(i)[0]
1764 && last <= hiddenColumns.get(i)[1])
1766 // last is hidden, move to left
1767 last = hiddenColumns.get(i)[0] - 1;
1769 if (last > hiddenColumns.get(i)[1])
1775 // make a local copy of the bit we need
1776 i = lasthiddenregion + 1;
1777 while (i < hiddenColumns.size()
1778 && hiddenColumns.get(i)[0] <= last)
1780 int[] region = new int[] { hiddenColumns.get(i)[0],
1781 hiddenColumns.get(i)[1] };
1782 localHidden.add(region);
1785 lasthiddenregion = -1;
1791 LOCK.readLock().unlock();
1797 public boolean hasNext()
1799 return next <= last;
1803 public Integer next()
1807 throw new NoSuchElementException();
1810 if ((localHidden != null)
1811 && (lasthiddenregion + 1 < localHidden.size()))
1813 // still some more hidden regions
1814 if (next + 1 < localHidden.get(lasthiddenregion + 1)[0])
1816 // next+1 is still before the next hidden region
1819 else if ((next + 1 >= localHidden.get(lasthiddenregion + 1)[0])
1820 && (next + 1 <= localHidden.get(lasthiddenregion + 1)[1]))
1822 // next + 1 is in the next hidden region
1823 next = localHidden.get(lasthiddenregion + 1)[1] + 1;
1829 // finished with hidden regions, just increment normally
1836 public void remove()
1838 throw new UnsupportedOperationException();