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.Vector;
31 import java.util.concurrent.locks.ReentrantReadWriteLock;
33 public class HiddenColumns
35 private static final int HASH_MULTIPLIER = 31;
37 private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock();
40 * list of hidden column [start, end] ranges; the list is maintained in
41 * ascending start column order
43 private ArrayList<int[]> hiddenColumns;
48 public HiddenColumns()
57 public HiddenColumns(HiddenColumns copy)
61 LOCK.writeLock().lock();
64 if (copy.hiddenColumns != null)
66 hiddenColumns = copy.copyHiddenRegionsToArrayList(0);
71 LOCK.writeLock().unlock();
76 * This method is used to return all the HiddenColumn regions and is intended
77 * to remain private. External callers which need a copy of the regions can
78 * call getHiddenColumnsCopyAsList.
80 * @return empty list or List of hidden column intervals
82 private List<int[]> getHiddenRegions()
84 return hiddenColumns == null ? Collections.<int[]> emptyList()
89 * Output regions data as a string. String is in the format:
90 * reg0[0]<between>reg0[1]<delimiter>reg1[0]<between>reg1[1] ... regn[1]
93 * string to delimit regions
94 * @param betweenstring
95 * to put between start and end region values
96 * @return regions formatted according to delimiter and between strings
98 public String regionsToString(String delimiter, String between)
102 LOCK.readLock().lock();
103 StringBuilder regionBuilder = new StringBuilder();
104 if (hiddenColumns != null)
106 for (int[] range : hiddenColumns)
108 regionBuilder.append(delimiter).append(range[0]).append(between)
112 regionBuilder.deleteCharAt(0);
114 return regionBuilder.toString();
117 LOCK.readLock().unlock();
122 * Find the number of hidden columns
124 * @return number of hidden columns
130 LOCK.readLock().lock();
132 if (hasHiddenColumns())
134 for (int[] range : hiddenColumns)
136 size += range[1] - range[0] + 1;
142 LOCK.readLock().unlock();
147 public boolean equals(Object obj)
151 LOCK.readLock().lock();
153 if (!(obj instanceof HiddenColumns))
157 HiddenColumns that = (HiddenColumns) obj;
160 * check hidden columns are either both null, or match
162 if (this.hiddenColumns == null)
164 return (that.hiddenColumns == null);
166 if (that.hiddenColumns == null
167 || that.hiddenColumns.size() != this.hiddenColumns.size())
172 for (int[] thisRange : hiddenColumns)
174 int[] thatRange = that.hiddenColumns.get(i++);
175 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
183 LOCK.readLock().unlock();
188 * Return absolute column index for a visible column index
191 * int column index in alignment view (count from zero)
192 * @return alignment column index for column
194 public int adjustForHiddenColumns(int column)
198 LOCK.readLock().lock();
200 if (hiddenColumns != null)
202 for (int i = 0; i < hiddenColumns.size(); i++)
204 int[] region = hiddenColumns.get(i);
205 if (result >= region[0])
207 result += region[1] - region[0] + 1;
214 LOCK.readLock().unlock();
219 * Use this method to find out where a column will appear in the visible
220 * alignment when hidden columns exist. If the column is not visible, then the
221 * left-most visible column will always be returned.
223 * @param hiddenColumn
224 * the column index in the full alignment including hidden columns
225 * @return the position of the column in the visible alignment
227 public int findColumnPosition(int hiddenColumn)
231 LOCK.readLock().lock();
232 int result = hiddenColumn;
233 if (hiddenColumns != null)
239 region = hiddenColumns.get(index++);
240 if (hiddenColumn > region[1])
242 result -= region[1] + 1 - region[0];
244 } while ((hiddenColumn > region[1])
245 && (index < hiddenColumns.size()));
247 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
249 // Here the hidden column is within a region, so
250 // we want to return the position of region[0]-1, adjusted for any
251 // earlier hidden columns.
252 // Calculate the difference between the actual hidden col position
253 // and region[0]-1, and then subtract from result to convert result
255 // the adjusted hiddenColumn value to the adjusted region[0]-1 value
257 // However, if the region begins at 0 we cannot return region[0]-1
265 return result - (hiddenColumn - region[0] + 1);
269 return result; // return the shifted position after removing hidden
273 LOCK.readLock().unlock();
278 * Find the visible column which is a given visible number of columns to the
279 * left of another visible column. i.e. for a startColumn x, the column which
280 * is distance 1 away will be column x-1.
282 * @param visibleDistance
283 * the number of visible columns to offset by
285 * the column to start from
286 * @return the position of the column in the visible alignment
288 public int subtractVisibleColumns(int visibleDistance, int startColumn)
293 LOCK.readLock().lock();
294 int distance = visibleDistance;
296 // in case startColumn is in a hidden region, move it to the left
297 int start = adjustForHiddenColumns(findColumnPosition(startColumn));
299 // get index of hidden region to left of start
300 int index = getHiddenIndexLeft(start);
303 // no hidden regions to left of startColumn
304 return start - distance;
307 // walk backwards through the alignment subtracting the counts of visible
308 // columns from distance
311 int nextstart = start;
313 while ((index > -1) && (distance - gap > 0))
315 // subtract the gap to right of region from distance
319 // calculate the next gap
320 region = hiddenColumns.get(index);
321 gap = start - region[1];
323 // set start to just to left of current region
324 nextstart = region[0] - 1;
328 if (distance - gap > 0)
330 // fell out of loop because there are no more hidden regions
332 return nextstart - distance;
334 return start - distance;
337 LOCK.readLock().unlock();
343 * Use this method to determine the set of hiddenRegion start positions
344 * between absolute position <start> and absolute position <end>
347 * absolute residue to start from
349 * absolute residue to end at
351 * @return list of column numbers in *visible* view where hidden regions start
353 public List<Integer> findHiddenRegionPositions(int start, int end)
357 LOCK.readLock().lock();
358 List<Integer> positions = null;
360 if (hiddenColumns != null)
362 positions = new ArrayList<>(hiddenColumns.size());
364 // navigate to start, keeping count of hidden columns
367 while ((i < hiddenColumns.size())
368 && (hiddenColumns.get(i)[0] < start))
370 int[] region = hiddenColumns.get(i);
371 hiddenSoFar += region[1] - region[0] + 1;
375 // iterate from start to end, adding start positions of each
376 // hidden region. Positions are visible columns count, not absolute
377 while (i < hiddenColumns.size()
378 && (hiddenColumns.get(i)[0] < end))
380 int[] region = hiddenColumns.get(i);
381 positions.add(region[0] - hiddenSoFar);
382 hiddenSoFar += region[1] - region[0] + 1;
388 positions = new ArrayList<>();
394 LOCK.readLock().unlock();
399 * This method returns the rightmost limit of a region of an alignment with
400 * hidden columns. In otherwords, the next hidden column.
405 public int getHiddenBoundaryRight(int alPos)
409 LOCK.readLock().lock();
410 if (hiddenColumns != null)
415 int[] region = hiddenColumns.get(index);
416 if (alPos < region[0])
422 } while (index < hiddenColumns.size());
428 LOCK.readLock().unlock();
434 * This method returns the leftmost limit of a region of an alignment with
435 * hidden columns. In otherwords, the previous hidden column.
440 public int getHiddenBoundaryLeft(int alPos)
444 LOCK.readLock().lock();
446 if (hiddenColumns != null)
448 int index = hiddenColumns.size() - 1;
451 int[] region = hiddenColumns.get(index);
452 if (alPos > region[1])
458 } while (index > -1);
464 LOCK.readLock().unlock();
469 * This method returns the index of the hidden region to the left of a column
470 * position. If the column is in a hidden region it returns the index of the
471 * region to the left. If there is no hidden region to the left it returns -1.
476 private int getHiddenIndexLeft(int pos)
481 LOCK.readLock().lock();
482 if (hiddenColumns != null)
484 int index = hiddenColumns.size() - 1;
487 int[] region = hiddenColumns.get(index);
494 } while (index > -1);
500 LOCK.readLock().unlock();
506 * Adds the specified column range to the hidden columns
511 public void hideColumns(int start, int end)
513 boolean wasAlreadyLocked = false;
516 // check if the write lock was already locked by this thread,
517 // as this method can be called internally in loops within HiddenColumns
518 if (!LOCK.isWriteLockedByCurrentThread())
520 LOCK.writeLock().lock();
524 wasAlreadyLocked = true;
527 if (hiddenColumns == null)
529 hiddenColumns = new ArrayList<>();
533 * traverse existing hidden ranges and insert / amend / append as
536 for (int i = 0; i < hiddenColumns.size(); i++)
538 int[] region = hiddenColumns.get(i);
540 if (end < region[0] - 1)
543 * insert discontiguous preceding range
545 hiddenColumns.add(i, new int[] { start, end });
549 if (end <= region[1])
552 * new range overlaps existing, or is contiguous preceding it - adjust
555 region[0] = Math.min(region[0], start);
559 if (start <= region[1] + 1)
562 * new range overlaps existing, or is contiguous following it - adjust
563 * start and end columns
565 region[0] = Math.min(region[0], start);
566 region[1] = Math.max(region[1], end);
569 * also update or remove any subsequent ranges
570 * that are overlapped
572 while (i < hiddenColumns.size() - 1)
574 int[] nextRegion = hiddenColumns.get(i + 1);
575 if (nextRegion[0] > end + 1)
578 * gap to next hidden range - no more to update
582 region[1] = Math.max(nextRegion[1], end);
583 hiddenColumns.remove(i + 1);
590 * remaining case is that the new range follows everything else
592 hiddenColumns.add(new int[] { start, end });
595 if (!wasAlreadyLocked)
597 LOCK.writeLock().unlock();
602 public boolean isVisible(int column)
606 LOCK.readLock().lock();
608 if (hiddenColumns != null)
610 for (int[] region : hiddenColumns)
612 if (column >= region[0] && column <= region[1])
622 LOCK.readLock().unlock();
626 private ArrayList<int[]> copyHiddenRegionsToArrayList(int startIndex)
629 if (hiddenColumns != null)
631 size = hiddenColumns.size();
633 ArrayList<int[]> copy = new ArrayList<>(size);
635 for (int i = startIndex, j = size; i < j; i++)
639 rh = hiddenColumns.get(i);
642 cp = new int[rh.length];
643 System.arraycopy(rh, 0, cp, 0, rh.length);
652 * Returns a copy of the vector of hidden regions, as an ArrayList. Before
653 * using this method please consider if you really need access to the hidden
654 * regions - a new (or existing!) method on HiddenColumns might be more
657 * @return hidden regions as an ArrayList of [start,end] pairs
659 public ArrayList<int[]> getHiddenColumnsCopy()
663 LOCK.readLock().lock();
664 return copyHiddenRegionsToArrayList(0);
667 LOCK.readLock().unlock();
672 * return all visible segments between the given start and end boundaries
675 * (first column inclusive from 0)
677 * (last column - not inclusive)
678 * @return int[] {i_start, i_end, ..} where intervals lie in
679 * start<=i_start<=i_end<end
681 public int[] getVisibleContigs(int start, int end)
685 LOCK.readLock().lock();
686 if (hiddenColumns != null && hiddenColumns.size() > 0)
688 // max limit on number of visible contigs
689 // so we can dimension array
690 int maxcontigs = end - start + 1;
691 if (maxcontigs > (hiddenColumns.size() + 1) * 2)
693 maxcontigs = (hiddenColumns.size() + 1) * 2;
695 int[] vcontigs = new int[maxcontigs];
701 for (int[] region : hiddenColumns)
703 hideStart = region[0];
707 if (hideEnd < vstart)
711 if (hideStart > vstart)
713 vcontigs[i * 2] = vstart;
714 vcontigs[i * 2 + 1] = hideStart - 1;
717 vstart = hideEnd + 1;
719 // exit if we're past the end
728 vcontigs[i * 2] = vstart;
729 vcontigs[i * 2 + 1] = end - 1;
733 // copy final array into array of correct size
734 int[] trimmmedContigs = new int[i * 2];
735 System.arraycopy(vcontigs, 0, trimmmedContigs, 0, i * 2);
737 return trimmmedContigs;
741 return new int[] { start, end - 1 };
745 LOCK.readLock().unlock();
749 public String[] getVisibleSequenceStrings(int start, int end,
754 LOCK.readLock().lock();
755 int iSize = seqs.length;
756 String[] selections = new String[iSize];
757 if (hiddenColumns != null && hiddenColumns.size() > 0)
759 for (int i = 0; i < iSize; i++)
761 StringBuffer visibleSeq = new StringBuffer();
763 int blockStart = start;
768 for (int[] region : hiddenColumns)
770 hideStart = region[0];
773 if (hideStart < start)
778 blockStart = Math.min(blockStart, hideEnd + 1);
779 blockEnd = Math.min(blockEnd, hideStart);
781 if (blockStart > blockEnd)
786 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
788 blockStart = hideEnd + 1;
792 if (end > blockStart)
794 visibleSeq.append(seqs[i].getSequence(blockStart, end));
797 selections[i] = visibleSeq.toString();
802 for (int i = 0; i < iSize; i++)
804 selections[i] = seqs[i].getSequenceAsString(start, end);
811 LOCK.readLock().unlock();
816 * Locate the first and last position visible for this sequence. if seq isn't
817 * visible then return the position of the left and right of the hidden
818 * boundary region, and the corresponding alignment column indices for the
819 * extent of the sequence
822 * @return int[] { visible start, visible end, first seqpos, last seqpos,
823 * alignment index for seq start, alignment index for seq end }
825 public int[] locateVisibleBoundsOfSequence(SequenceI seq)
829 LOCK.readLock().lock();
830 int fpos = seq.getStart();
831 int lpos = seq.getEnd();
834 if (hiddenColumns == null || hiddenColumns.size() == 0)
836 int ifpos = seq.findIndex(fpos) - 1;
837 int ilpos = seq.findIndex(lpos) - 1;
838 return new int[] { ifpos, ifpos, ilpos };
841 // Simply walk along the sequence whilst watching for hidden column
843 List<int[]> regions = getHiddenRegions();
846 int hideStart = seq.getLength();
852 boolean foundStart = false;
853 for (int p = 0, pLen = seq.getLength(); spos <= seq.getEnd()
856 if (!Comparison.isGap(seq.getCharAt(p)))
858 // keep track of first/last column
859 // containing sequence data regardless of visibility
865 // update hidden region start/end
866 while (hideEnd < p && rcount < regions.size())
868 int[] region = regions.get(rcount++);
870 visNext += region[0] - visPrev;
871 hideStart = region[0];
876 hideStart = seq.getLength();
878 // update visible boundary for sequence
889 // look for next sequence position
895 return new int[] { findColumnPosition(start), firstP, lastP };
897 // otherwise, sequence was completely hidden
898 return new int[] { visPrev, firstP, lastP };
901 LOCK.readLock().unlock();
906 * delete any columns in alignmentAnnotation that are hidden (including
907 * sequence associated annotation).
909 * @param alignmentAnnotation
911 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
913 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
917 * delete any columns in alignmentAnnotation that are hidden (including
918 * sequence associated annotation).
921 * remove any annotation to the right of this column
923 * remove any annotation to the left of this column
924 * @param alignmentAnnotation
925 * the annotation to operate on
927 public void makeVisibleAnnotation(int start, int end,
928 AlignmentAnnotation alignmentAnnotation)
932 LOCK.readLock().lock();
933 if (alignmentAnnotation.annotations == null)
937 if (start == end && end == -1)
940 end = alignmentAnnotation.annotations.length;
942 if (hiddenColumns != null && hiddenColumns.size() > 0)
944 // then mangle the alignmentAnnotation annotation array
945 Vector<Annotation[]> annels = new Vector<>();
946 Annotation[] els = null;
947 int blockStart = start;
953 for (int[] region : hiddenColumns)
955 hideStart = region[0];
958 if (hideStart < start)
963 blockStart = Math.min(blockStart, hideEnd + 1);
964 blockEnd = Math.min(blockEnd, hideStart);
966 if (blockStart > blockEnd)
971 els = new Annotation[blockEnd - blockStart];
972 annels.addElement(els);
973 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
976 blockStart = hideEnd + 1;
980 if (end > blockStart)
982 els = new Annotation[end - blockStart + 1];
983 annels.addElement(els);
985 + blockStart) <= alignmentAnnotation.annotations.length)
987 // copy just the visible segment of the annotation row
988 System.arraycopy(alignmentAnnotation.annotations, blockStart,
993 // copy to the end of the annotation row
994 System.arraycopy(alignmentAnnotation.annotations, blockStart,
996 (alignmentAnnotation.annotations.length - blockStart));
1005 alignmentAnnotation.annotations = new Annotation[w];
1008 for (Annotation[] chnk : annels)
1010 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1017 alignmentAnnotation.restrict(start, end);
1021 LOCK.readLock().unlock();
1027 * @return true if there are columns hidden
1029 public boolean hasHiddenColumns()
1033 LOCK.readLock().lock();
1034 return hiddenColumns != null && hiddenColumns.size() > 0;
1037 LOCK.readLock().unlock();
1043 * @return true if there are more than one set of columns hidden
1045 public boolean hasManyHiddenColumns()
1049 LOCK.readLock().lock();
1050 return hiddenColumns != null && hiddenColumns.size() > 1;
1053 LOCK.readLock().unlock();
1058 * mark the columns corresponding to gap characters as hidden in the column
1063 public void hideInsertionsFor(SequenceI sr)
1067 LOCK.writeLock().lock();
1068 List<int[]> inserts = sr.getInsertions();
1069 for (int[] r : inserts)
1071 hideColumns(r[0], r[1]);
1075 LOCK.writeLock().unlock();
1080 * Unhides, and adds to the selection list, all hidden columns
1082 public void revealAllHiddenColumns(ColumnSelection sel)
1086 LOCK.writeLock().lock();
1087 if (hiddenColumns != null)
1089 for (int i = 0; i < hiddenColumns.size(); i++)
1091 int[] region = hiddenColumns.get(i);
1092 for (int j = region[0]; j < region[1] + 1; j++)
1099 hiddenColumns = null;
1102 LOCK.writeLock().unlock();
1107 * Reveals, and marks as selected, the hidden column range with the given
1112 public void revealHiddenColumns(int start, ColumnSelection sel)
1116 LOCK.writeLock().lock();
1117 for (int i = 0; i < hiddenColumns.size(); i++)
1119 int[] region = hiddenColumns.get(i);
1120 if (start == region[0])
1122 for (int j = region[0]; j < region[1] + 1; j++)
1127 hiddenColumns.remove(region);
1131 if (hiddenColumns.size() == 0)
1133 hiddenColumns = null;
1137 LOCK.writeLock().unlock();
1142 * Add gaps into the sequences aligned to profileseq under the given
1147 * - alignment to have gaps inserted into it
1149 * - alignment view where sequence corresponding to profileseq is
1151 * @return new HiddenColumns for new alignment view, with insertions into
1152 * profileseq marked as hidden.
1154 public static HiddenColumns propagateInsertions(SequenceI profileseq,
1155 AlignmentI al, AlignmentView input)
1159 char gc = al.getGapCharacter();
1160 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
1161 HiddenColumns nview = (HiddenColumns) alandhidden[1];
1162 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
1163 nview.propagateInsertions(profileseq, al, origseq);
1170 * - sequence in al which corresponds to origseq
1172 * - alignment which is to have gaps inserted into it
1174 * - sequence corresponding to profileseq which defines gap map for
1177 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
1180 char gc = al.getGapCharacter();
1182 // take the set of hidden columns, and the set of gaps in origseq,
1183 // and remove all the hidden gaps from hiddenColumns
1185 // first get the gaps as a Bitset
1186 BitSet gaps = origseq.gapBitset();
1188 // now calculate hidden ^ not(gap)
1189 BitSet hidden = new BitSet();
1190 markHiddenRegions(hidden);
1191 hidden.andNot(gaps);
1192 hiddenColumns = null;
1193 this.hideMarkedBits(hidden);
1195 // for each sequence in the alignment, except the profile sequence,
1196 // insert gaps corresponding to each hidden region
1197 // but where each hidden column region is shifted backwards by the number of
1198 // preceding visible gaps
1199 // update hidden columns at the same time
1200 ArrayList<int[]> regions = getHiddenColumnsCopy();
1201 ArrayList<int[]> newhidden = new ArrayList<>();
1203 int numGapsBefore = 0;
1204 int gapPosition = 0;
1205 for (int[] region : regions)
1207 // get region coordinates accounting for gaps
1208 // we can rely on gaps not being *in* hidden regions because we already
1210 while (gapPosition < region[0])
1213 if (gaps.get(gapPosition))
1219 int left = region[0] - numGapsBefore;
1220 int right = region[1] - numGapsBefore;
1221 newhidden.add(new int[] { left, right });
1223 // make a string with number of gaps = length of hidden region
1224 StringBuffer sb = new StringBuffer();
1225 for (int s = 0; s < right - left + 1; s++)
1229 padGaps(sb, left, profileseq, al);
1232 hiddenColumns = newhidden;
1236 * Pad gaps in all sequences in alignment except profileseq
1239 * gap string to insert
1241 * position to insert at
1243 * sequence not to pad
1245 * alignment to pad sequences in
1247 private void padGaps(StringBuffer sb, int pos, SequenceI profileseq,
1250 // loop over the sequences and pad with gaps where required
1251 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1253 SequenceI sqobj = al.getSequenceAt(s);
1254 if (sqobj != profileseq)
1256 String sq = al.getSequenceAt(s).getSequenceAsString();
1257 if (sq.length() <= pos)
1260 int diff = pos - sq.length() - 1;
1265 while ((diff = pos - sq.length() - 1) > 0)
1267 if (diff >= sb.length())
1269 sq += sb.toString();
1273 char[] buf = new char[diff];
1274 sb.getChars(0, diff, buf, 0);
1275 sq += buf.toString();
1279 sq += sb.toString();
1283 al.getSequenceAt(s).setSequence(
1284 sq.substring(0, pos) + sb.toString() + sq.substring(pos));
1291 * Returns a hashCode built from hidden column ranges
1294 public int hashCode()
1298 LOCK.readLock().lock();
1300 if (hiddenColumns != null)
1302 for (int[] hidden : hiddenColumns)
1304 hashCode = HASH_MULTIPLIER * hashCode + hidden[0];
1305 hashCode = HASH_MULTIPLIER * hashCode + hidden[1];
1311 LOCK.readLock().unlock();
1316 * Hide columns corresponding to the marked bits
1319 * - columns map to bits starting from zero
1321 public void hideMarkedBits(BitSet inserts)
1325 LOCK.writeLock().lock();
1326 for (int firstSet = inserts
1327 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1328 .nextSetBit(lastSet))
1330 lastSet = inserts.nextClearBit(firstSet);
1331 hideColumns(firstSet, lastSet - 1);
1335 LOCK.writeLock().unlock();
1342 * BitSet where hidden columns will be marked
1344 public void markHiddenRegions(BitSet inserts)
1348 LOCK.readLock().lock();
1349 if (hiddenColumns == null)
1353 for (int[] range : hiddenColumns)
1355 inserts.set(range[0], range[1] + 1);
1359 LOCK.readLock().unlock();
1364 * Calculate the visible start and end index of an alignment.
1367 * full alignment width
1368 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1370 public int[] getVisibleStartAndEndIndex(int width)
1374 LOCK.readLock().lock();
1375 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1376 int startPos = alignmentStartEnd[0];
1377 int endPos = alignmentStartEnd[1];
1379 int[] lowestRange = new int[] { -1, -1 };
1380 int[] higestRange = new int[] { -1, -1 };
1382 if (hiddenColumns == null)
1384 return new int[] { startPos, endPos };
1387 for (int[] hiddenCol : hiddenColumns)
1389 lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
1390 higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
1393 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1395 startPos = alignmentStartEnd[0];
1399 startPos = lowestRange[1] + 1;
1402 if (higestRange[0] == -1 && higestRange[1] == -1)
1404 endPos = alignmentStartEnd[1];
1408 endPos = higestRange[0] - 1;
1410 return new int[] { startPos, endPos };
1413 LOCK.readLock().unlock();
1419 * Finds the hidden region (if any) which starts or ends at res
1422 * visible residue position, unadjusted for hidden columns
1423 * @return region as [start,end] or null if no matching region is found
1425 public int[] getRegionWithEdgeAtRes(int res)
1429 LOCK.readLock().lock();
1430 int adjres = adjustForHiddenColumns(res);
1432 int[] reveal = null;
1433 if (hiddenColumns != null)
1435 for (int[] region : hiddenColumns)
1437 if (adjres + 1 == region[0] || adjres - 1 == region[1])
1447 LOCK.readLock().unlock();
1451 public Iterator<int[]> iterator()
1453 if (hiddenColumns != null)
1455 return new BoundedHiddenColsIterator(0, hiddenColumns.size(), true);
1459 return new BoundedHiddenColsIterator(0, 0, true);
1463 public Iterator<int[]> getBoundedIterator(int start, int end,
1466 return new BoundedHiddenColsIterator(start, end, useCopy);
1469 public Iterator<Integer> getBoundedStartIterator(int start, int end,
1472 return new BoundedStartRegionIterator(start, end, useCopy);
1476 * An iterator which iterates over hidden column regions in a range.
1483 class BoundedHiddenColsIterator implements Iterator<int[]>
1486 private int start; // start position to iterate from
1488 private int end; // end position to iterate to
1490 // current index in hiddenColumns
1491 private int currentPosition = 0;
1493 // current column in hiddenColumns
1494 private int[] currentRegion;
1496 // whether to make a local copy of hiddenColumns
1497 private final boolean useCopy;
1499 // local copy or reference to hiddenColumns
1500 private List<int[]> localHidden;
1503 * Construct an iterator over hiddenColums bounded at
1504 * [lowerBound,upperBound]
1507 * lower bound to iterate from
1509 * upper bound to iterate to
1511 * Option.OVERLAP: regions which overlap [lowerBound,upperBound]
1512 * are included Option.START: regions which start in
1513 * [lowerBound,upperBound] are included
1514 * @param useAbsolutePos
1515 * have bounds and return values with reference to absolute indices
1516 * (if false, use indices for visible columns)
1517 * @param useCopyCols
1518 * whether to make a local copy of hiddenColumns for iteration (set
1519 * to true if calling from outwith the HiddenColumns class)
1521 BoundedHiddenColsIterator(int lowerBound, int upperBound,
1522 boolean useCopyCols)
1526 useCopy = useCopyCols;
1532 // assume that if useCopy is false the calling code has locked
1534 LOCK.readLock().lock();
1537 if (hiddenColumns != null)
1539 localHidden = new ArrayList<>();
1541 // iterate until a region overlaps with [start,end]
1543 while ((i < hiddenColumns.size())
1544 && (hiddenColumns.get(i)[1] < start))
1549 // iterate from start to end, adding each hidden region. Positions are
1550 // absolute, and all regions which *overlap* [start,end] are added.
1551 while (i < hiddenColumns.size()
1552 && (hiddenColumns.get(i)[0] <= end))
1556 rh = hiddenColumns.get(i);
1559 cp = new int[rh.length];
1560 System.arraycopy(rh, 0, cp, 0, rh.length);
1561 localHidden.add(cp);
1571 LOCK.readLock().unlock();
1577 public boolean hasNext()
1579 return (localHidden != null)
1580 && (currentPosition < localHidden.size());
1586 currentRegion = localHidden.get(currentPosition);
1588 return currentRegion;
1592 class BoundedStartRegionIterator implements Iterator<Integer>
1595 private int start; // start position to iterate from
1597 private int end; // end position to iterate to
1599 // current index in hiddenColumns
1600 private int currentPosition = 0;
1602 // local copy or reference to hiddenColumns
1603 private List<Integer> positions = null;
1606 * Construct an iterator over hiddenColums bounded at
1607 * [lowerBound,upperBound]
1610 * lower bound to iterate from
1612 * upper bound to iterate to
1613 * @param useCopyCols
1614 * whether to make a local copy of hiddenColumns for iteration (set
1615 * to true if calling from outwith the HiddenColumns class)
1617 BoundedStartRegionIterator(int lowerBound, int upperBound,
1627 // assume that if useCopy is false the calling code has locked
1629 LOCK.readLock().lock();
1632 if (hiddenColumns != null)
1634 positions = new ArrayList<>(hiddenColumns.size());
1636 // navigate to start, keeping count of hidden columns
1638 int hiddenSoFar = 0;
1639 while ((i < hiddenColumns.size())
1640 && (hiddenColumns.get(i)[0] < start + hiddenSoFar))
1642 int[] region = hiddenColumns.get(i);
1643 hiddenSoFar += region[1] - region[0] + 1;
1647 // iterate from start to end, adding start positions of each
1648 // hidden region. Positions are visible columns count, not absolute
1649 while (i < hiddenColumns.size()
1650 && (hiddenColumns.get(i)[0] <= end + hiddenSoFar))
1652 int[] region = hiddenColumns.get(i);
1653 positions.add(region[0] - hiddenSoFar);
1654 hiddenSoFar += region[1] - region[0] + 1;
1660 positions = new ArrayList<>();
1666 LOCK.readLock().unlock();
1672 public boolean hasNext()
1674 return (currentPosition < positions.size());
1678 public Integer next()
1680 int result = positions.get(currentPosition);