+ * Adds the specified column range to the hidden columns collection
+ *
+ * @param start
+ * start of range to add (absolute position in alignment)
+ * @param end
+ * end of range to add (absolute position in alignment)
+ */
+ public void hideColumns(int start, int end)
+ {
+ boolean wasAlreadyLocked = false;
+ try
+ {
+ // check if the write lock was already locked by this thread,
+ // as this method can be called internally in loops within HiddenColumns
+ if (!LOCK.isWriteLockedByCurrentThread())
+ {
+ LOCK.writeLock().lock();
+ }
+ else
+ {
+ wasAlreadyLocked = true;
+ }
+
+ int previndex = 0;
+ int prevHiddenCount = 0;
+ int regionindex = 0;
+ if (hiddenColumns == null)
+ {
+ hiddenColumns = new ArrayList<>();
+ }
+ else
+ {
+ // set up cursor reset values
+ HiddenCursorPosition cursorPos = cursor.findRegionForColumn(start);
+ regionindex = cursorPos.getRegionIndex();
+
+ if (regionindex > 0)
+ {
+ // get previous index and hidden count for updating the cursor later
+ previndex = regionindex - 1;
+ int[] prevRegion = hiddenColumns.get(previndex);
+ prevHiddenCount = cursorPos.getHiddenSoFar()
+ - (prevRegion[1] - prevRegion[0] + 1);
+ }
+ }
+
+ /*
+ * new range follows everything else; check first to avoid looping over whole hiddenColumns collection
+ */
+ if (hiddenColumns.isEmpty()
+ || start > hiddenColumns.get(hiddenColumns.size() - 1)[1])
+ {
+ hiddenColumns.add(new int[] { start, end });
+ }
+ else
+ {
+ /*
+ * traverse existing hidden ranges and insert / amend / append as
+ * appropriate
+ */
+ boolean added = false;
+ if (regionindex > 0)
+ {
+ added = insertRangeAtRegion(regionindex - 1, start, end);
+ }
+ if (!added && regionindex < hiddenColumns.size())
+ {
+ insertRangeAtRegion(regionindex, start, end);
+ }
+ }
+
+ // reset the cursor to just before our insertion point: this saves
+ // a lot of reprocessing in large alignments
+ cursor.resetCursor(hiddenColumns, previndex, prevHiddenCount);
+
+ // reset the number of columns so they will be recounted
+ numColumns = 0;
+
+ } finally
+ {
+ if (!wasAlreadyLocked)
+ {
+ LOCK.writeLock().unlock();
+ }
+ }
+ }
+
+ /**
+ * Insert [start, range] at the region at index i in hiddenColumns, if
+ * feasible
+ *
+ * @param i
+ * index to insert at
+ * @param start
+ * start of range to insert
+ * @param end
+ * end of range to insert
+ * @return true if range was successfully inserted
+ */
+ private boolean insertRangeAtRegion(int i, int start, int end)
+ {
+ boolean added = false;
+
+ int[] region = hiddenColumns.get(i);
+ if (end < region[0] - 1)
+ {
+ /*
+ * insert discontiguous preceding range
+ */
+ hiddenColumns.add(i, new int[] { start, end });
+ added = true;
+ }
+ else if (end <= region[1])
+ {
+ /*
+ * new range overlaps existing, or is contiguous preceding it - adjust
+ * start column
+ */
+ region[0] = Math.min(region[0], start);
+ added = true;
+ }
+ else if (start <= region[1] + 1)
+ {
+ /*
+ * new range overlaps existing, or is contiguous following it - adjust
+ * start and end columns
+ */
+ region[0] = Math.min(region[0], start);
+ region[1] = Math.max(region[1], end);
+
+ /*
+ * also update or remove any subsequent ranges
+ * that are overlapped
+ */
+ while (i < hiddenColumns.size() - 1)
+ {
+ int[] nextRegion = hiddenColumns.get(i + 1);
+ if (nextRegion[0] > end + 1)
+ {
+ /*
+ * gap to next hidden range - no more to update
+ */
+ break;
+ }
+ region[1] = Math.max(nextRegion[1], end);
+
+ // in theory this is faster than hiddenColumns.remove(i+1)
+ // benchmarking results a bit ambivalent
+ hiddenColumns.subList(i + 1, i + 2).clear();
+ }
+ added = true;
+ }
+ return added;
+ }
+
+ /**
+ * hide a list of ranges
+ *
+ * @param ranges
+ */
+ public void hideList(List<int[]> ranges)
+ {
+ try
+ {
+ LOCK.writeLock().lock();
+ for (int[] r : ranges)
+ {
+ hideColumns(r[0], r[1]);
+ }
+ cursor.resetCursor(hiddenColumns);
+ numColumns = 0;
+ } finally
+ {
+ LOCK.writeLock().unlock();
+ }
+ }
+
+ /**
+ * Unhides, and adds to the selection list, all hidden columns
+ */
+ public void revealAllHiddenColumns(ColumnSelection sel)
+ {
+ try
+ {
+ LOCK.writeLock().lock();
+ if (hiddenColumns != null)
+ {
+ Iterator<int[]> it = hiddenColumns.iterator();
+ while (it.hasNext())
+ {
+ int[] region = it.next();
+ for (int j = region[0]; j < region[1] + 1; j++)
+ {
+ sel.addElement(j);
+ }
+ }
+ hiddenColumns = null;
+ cursor.resetCursor(hiddenColumns);
+ numColumns = 0;
+ }
+ } finally
+ {
+ LOCK.writeLock().unlock();
+ }
+ }
+
+ /**
+ * Reveals, and marks as selected, the hidden column range with the given
+ * start column
+ *
+ * @param start
+ * the start column to look for
+ * @param sel
+ * the column selection to add the hidden column range to
+ */
+ public void revealHiddenColumns(int start, ColumnSelection sel)
+ {
+ try
+ {
+ LOCK.writeLock().lock();
+
+ if (hiddenColumns != null)
+ {
+ int regionIndex = cursor.findRegionForColumn(start)
+ .getRegionIndex();
+
+ if (regionIndex != -1 && regionIndex != hiddenColumns.size())
+ {
+ // regionIndex is the region which either contains start
+ // or lies to the right of start
+ int[] region = hiddenColumns.get(regionIndex);
+ if (start == region[0])
+ {
+ for (int j = region[0]; j < region[1] + 1; j++)
+ {
+ sel.addElement(j);
+ }
+ int colsToRemove = region[1] - region[0] + 1;
+ hiddenColumns.remove(regionIndex);
+
+ if (hiddenColumns.isEmpty())
+ {
+ hiddenColumns = null;
+ numColumns = 0;
+ }
+ else
+ {
+ numColumns -= colsToRemove;
+ }
+ cursor.updateForDeletedRegion(hiddenColumns);
+ }
+ }
+ }
+ } finally
+ {
+ LOCK.writeLock().unlock();
+ }
+ }
+
+ /**
+ * Add gaps into the sequences aligned to profileseq under the given
+ * AlignmentView
+ *
+ * @param profileseq
+ * sequence in al which sequences are aligned to
+ * @param al
+ * alignment to have gaps inserted into it
+ * @param input
+ * alignment view where sequence corresponding to profileseq is first
+ * entry
+ * @return new HiddenColumns for new alignment view, with insertions into
+ * profileseq marked as hidden.
+ */
+ public static HiddenColumns propagateInsertions(SequenceI profileseq,
+ AlignmentI al, AlignmentView input)
+ {
+ int profsqpos = 0;
+
+ char gc = al.getGapCharacter();
+ Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
+ HiddenColumns nview = (HiddenColumns) alandhidden[1];
+ SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
+ nview.propagateInsertions(profileseq, al, origseq);
+ return nview;
+ }
+
+ /**
+ *
+ * @param profileseq
+ * sequence in al which corresponds to origseq
+ * @param al
+ * alignment which is to have gaps inserted into it
+ * @param origseq
+ * sequence corresponding to profileseq which defines gap map for
+ * modifying al
+ */
+ private void propagateInsertions(SequenceI profileseq, AlignmentI al,
+ SequenceI origseq)
+ {
+ try
+ {
+ LOCK.writeLock().lock();
+
+ char gc = al.getGapCharacter();
+
+ // take the set of hidden columns, and the set of gaps in origseq,
+ // and remove all the hidden gaps from hiddenColumns
+
+ // first get the gaps as a Bitset
+ BitSet gaps = origseq.gapBitset();
+
+ // now calculate hidden ^ not(gap)
+ BitSet hidden = new BitSet();
+ markHiddenRegions(hidden);
+ hidden.andNot(gaps);
+ hiddenColumns = null;
+ this.hideColumns(hidden);
+
+ // for each sequence in the alignment, except the profile sequence,
+ // insert gaps corresponding to each hidden region but where each hidden
+ // column region is shifted backwards by the number of preceding visible
+ // gaps update hidden columns at the same time
+ Iterator<int[]> regions = hiddenColumns.iterator();
+ List<int[]> newhidden = new ArrayList<>();
+
+ int numGapsBefore = 0;
+ int gapPosition = 0;
+ while (regions.hasNext())
+ {
+ // get region coordinates accounting for gaps
+ // we can rely on gaps not being *in* hidden regions because we already
+ // removed those
+ int[] region = regions.next();
+ while (gapPosition < region[0])
+ {
+ gapPosition++;
+ if (gaps.get(gapPosition))
+ {
+ numGapsBefore++;
+ }
+ }
+
+ int left = region[0] - numGapsBefore;
+ int right = region[1] - numGapsBefore;
+ newhidden.add(new int[] { left, right });
+
+ // make a string with number of gaps = length of hidden region
+ StringBuffer sb = new StringBuffer();
+ for (int s = 0; s < right - left + 1; s++)
+ {
+ sb.append(gc);
+ }
+ padGaps(sb, left, profileseq, al);
+
+ }
+ hiddenColumns = newhidden;
+ cursor.resetCursor(hiddenColumns);
+ numColumns = 0;
+ } finally
+ {
+ LOCK.writeLock().unlock();
+ }
+ }
+
+ /**
+ * Pad gaps in all sequences in alignment except profileseq
+ *
+ * @param sb
+ * gap string to insert
+ * @param left
+ * position to insert at
+ * @param profileseq
+ * sequence not to pad
+ * @param al
+ * alignment to pad sequences in
+ */
+ private void padGaps(StringBuffer sb, int pos, SequenceI profileseq,
+ AlignmentI al)
+ {
+ // loop over the sequences and pad with gaps where required
+ for (int s = 0, ns = al.getHeight(); s < ns; s++)
+ {
+ SequenceI sqobj = al.getSequenceAt(s);
+ if (sqobj != profileseq)
+ {
+ String sq = al.getSequenceAt(s).getSequenceAsString();
+ if (sq.length() <= pos)
+ {
+ // pad sequence
+ int diff = pos - sq.length() - 1;
+ if (diff > 0)
+ {
+ // pad gaps
+ sq = sq + sb;
+ while ((diff = pos - sq.length() - 1) > 0)
+ {
+ if (diff >= sb.length())
+ {
+ sq += sb.toString();
+ }
+ else
+ {
+ char[] buf = new char[diff];
+ sb.getChars(0, diff, buf, 0);
+ sq += buf.toString();
+ }
+ }
+ }
+ sq += sb.toString();
+ }
+ else
+ {
+ al.getSequenceAt(s).setSequence(
+ sq.substring(0, pos) + sb.toString() + sq.substring(pos));
+ }
+ }
+ }
+ }
+
+ /*
+ * Methods which only need read access to the hidden columns collection.
+ * These methods should use a readLock to prevent other threads changing
+ * the hidden columns collection while it is in use.
+ */
+
+ /**