From: kiramt Date: Mon, 20 Nov 2017 11:31:34 +0000 (+0000) Subject: JAL-2759 rationalise visible contigs iteration X-Git-Tag: Release_2_10_4~55^2~1^2~65 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=fa20ec4effe2fb9205d844fd85befbe785bf1a2e;p=jalview.git JAL-2759 rationalise visible contigs iteration --- diff --git a/src/jalview/appletgui/SeqCanvas.java b/src/jalview/appletgui/SeqCanvas.java index b499a74..e83d04a 100755 --- a/src/jalview/appletgui/SeqCanvas.java +++ b/src/jalview/appletgui/SeqCanvas.java @@ -22,10 +22,10 @@ package jalview.appletgui; import jalview.datamodel.AlignmentI; import jalview.datamodel.HiddenColumns; -import jalview.datamodel.HiddenColumns.VisibleBlocksVisBoundsIterator; import jalview.datamodel.SearchResultsI; import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; +import jalview.datamodel.VisibleContigsIterator; import jalview.renderer.ScaleRenderer; import jalview.renderer.ScaleRenderer.ScaleMark; import jalview.viewmodel.AlignmentViewport; @@ -565,7 +565,7 @@ public class SeqCanvas extends Panel implements ViewportListenerI int blockEnd; HiddenColumns hidden = av.getAlignment().getHiddenColumns(); - VisibleBlocksVisBoundsIterator regions = (VisibleBlocksVisBoundsIterator) hidden + VisibleContigsIterator regions = (VisibleContigsIterator) hidden .getVisibleBlocksIterator(startRes, endRes, true); while (regions.hasNext()) diff --git a/src/jalview/datamodel/HiddenColumns.java b/src/jalview/datamodel/HiddenColumns.java index 0a4d04b..48cfb42 100644 --- a/src/jalview/datamodel/HiddenColumns.java +++ b/src/jalview/datamodel/HiddenColumns.java @@ -32,8 +32,15 @@ public class HiddenColumns private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock(); + /* + * Cursor which tracks the last used hidden columns region, and the number + * of hidden columns up to (but not including) that region. + */ private HiddenColumnsCursor cursor = new HiddenColumnsCursor(); + /* + * cache of the number of hidden columns + */ private int numColumns = 0; /* @@ -49,16 +56,25 @@ public class HiddenColumns { } + /* + * Methods which change the hiddenColumns collection. These methods should + * use a writeLock to prevent other threads accessing the hiddenColumns + * collection while changes are being made. They should also reset the hidden + * columns cursor, and either update the hidden columns count, or set it to 0. + */ + /** * Copy constructor * * @param copy + * the HiddenColumns object to copy from */ public HiddenColumns(HiddenColumns copy) { try { LOCK.writeLock().lock(); + numColumns = 0; if (copy != null) { if (copy.hiddenColumns != null) @@ -67,7 +83,9 @@ public class HiddenColumns Iterator it = copy.iterator(); while (it.hasNext()) { - hiddenColumns.add(it.next()); + int[] region = it.next(); + hiddenColumns.add(region); + numColumns += region[1] - region[0] + 1; } cursor.resetCursor(hiddenColumns); } @@ -125,501 +143,438 @@ public class HiddenColumns } /** - * Output regions data as a string. String is in the format: - * reg0[0]reg0[1]reg1[0]reg1[1] ... regn[1] + * Adds the specified column range to the hidden columns collection * - * @param delimiter - * string to delimit regions - * @param betweenstring - * to put between start and end region values - * @return regions formatted according to delimiter and between strings + * @param start + * start of range to add (absolute position in alignment) + * @param end + * end of range to add (absolute position in alignment) */ - public String regionsToString(String delimiter, String between) + public void hideColumns(int start, int end) { + boolean wasAlreadyLocked = false; try { - LOCK.readLock().lock(); - StringBuilder regionBuilder = new StringBuilder(); - if (hiddenColumns != null) + // 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()) { - Iterator it = hiddenColumns.iterator(); - while (it.hasNext()) + LOCK.writeLock().lock(); + } + else + { + wasAlreadyLocked = true; + } + + if (hiddenColumns == null) + { + hiddenColumns = new ArrayList<>(); + } + + /* + * 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; + for (int i = 0; !added && i < hiddenColumns.size(); i++) { - int[] range = it.next(); - regionBuilder.append(delimiter).append(range[0]).append(between) - .append(range[1]); - if (!it.hasNext()) - { - regionBuilder.deleteCharAt(0); - } - } + added = insertRangeAtRegion(i, start, end); + } // for + } + if (!wasAlreadyLocked) + { + cursor.resetCursor(hiddenColumns); + + // reset the number of columns so they will be recounted + numColumns = 0; } - return regionBuilder.toString(); } finally { - LOCK.readLock().unlock(); + if (!wasAlreadyLocked) + { + LOCK.writeLock().unlock(); + } } } /** - * Find the number of hidden columns + * Insert [start, range] at the region at index i in hiddenColumns, if + * feasible * - * @return number of hidden columns + * @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 */ - public int getSize() + private boolean insertRangeAtRegion(int i, int start, int end) { - try + boolean added = false; + + int[] region = hiddenColumns.get(i); + if (end < region[0] - 1) { - LOCK.readLock().lock(); + /* + * 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); - if (numColumns == 0 && hiddenColumns != null) + /* + * also update or remove any subsequent ranges + * that are overlapped + */ + while (i < hiddenColumns.size() - 1) { - // numColumns is out of date, so recalculate - int size = 0; - if (hiddenColumns != null) + int[] nextRegion = hiddenColumns.get(i + 1); + if (nextRegion[0] > end + 1) { - Iterator it = hiddenColumns.iterator(); - while (it.hasNext()) - { - int[] range = it.next(); - size += range[1] - range[0] + 1; - } + /* + * gap to next hidden range - no more to update + */ + break; } - numColumns = size; + region[1] = Math.max(nextRegion[1], end); + hiddenColumns.subList(i + 1, i + 2).clear(); } - - return numColumns; - } finally - { - LOCK.readLock().unlock(); + added = true; } + return added; } /** - * Get the number of distinct hidden regions + * mark the columns corresponding to gap characters as hidden in the column + * selection * - * @return number of regions + * @param sr */ - public int getNumberOfRegions() + public void hideInsertionsFor(SequenceI sr) { try { - LOCK.readLock().lock(); - int num = 0; - if (hasHiddenColumns()) + LOCK.writeLock().lock(); + List inserts = sr.getInsertions(); + for (int[] r : inserts) { - num = hiddenColumns.size(); + hideColumns(r[0], r[1]); } - return num; + cursor.resetCursor(hiddenColumns); + numColumns = 0; } finally { - LOCK.readLock().unlock(); + LOCK.writeLock().unlock(); } } - @Override - public boolean equals(Object obj) + /** + * Unhides, and adds to the selection list, all hidden columns + */ + public void revealAllHiddenColumns(ColumnSelection sel) { try { - LOCK.readLock().lock(); - - if (!(obj instanceof HiddenColumns)) - { - return false; - } - HiddenColumns that = (HiddenColumns) obj; - - /* - * check hidden columns are either both null, or match - */ - if (this.hiddenColumns == null) - { - return (that.hiddenColumns == null); - } - if (that.hiddenColumns == null - || that.hiddenColumns.size() != this.hiddenColumns.size()) - { - return false; - } - - Iterator it = hiddenColumns.iterator(); - Iterator thatit = that.iterator(); - while (it.hasNext()) + LOCK.writeLock().lock(); + if (hiddenColumns != null) { - int[] thisRange = it.next(); - int[] thatRange = thatit.next(); - if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1]) + Iterator it = hiddenColumns.iterator(); + while (it.hasNext()) { - return false; + int[] region = it.next(); + for (int j = region[0]; j < region[1] + 1; j++) + { + sel.addElement(j); + } } + hiddenColumns = null; + cursor.resetCursor(hiddenColumns); + numColumns = 0; } - return true; } finally { - LOCK.readLock().unlock(); + LOCK.writeLock().unlock(); } } /** - * Return absolute column index for a visible column index + * Reveals, and marks as selected, the hidden column range with the given + * start column * - * @param column - * int column index in alignment view (count from zero) - * @return alignment column index for column + * @param start + * the start column to look for + * @param sel + * the column selection to add the hidden column range to */ - public int adjustForHiddenColumns(int column) + public void revealHiddenColumns(int start, ColumnSelection sel) { try { - LOCK.readLock().lock(); - int result = column; + LOCK.writeLock().lock(); if (hiddenColumns != null) { - result += cursor.getHiddenOffset(column).getHiddenSoFar(); - } - - return result; - } finally - { - LOCK.readLock().unlock(); - } - } + int regionIndex = cursor.findRegionForColumn(start) + .getRegionIndex(); - /** - * Use this method to find out where a column will appear in the visible - * alignment when hidden columns exist. If the column is not visible, then the - * left-most visible column will always be returned. - * - * @param hiddenColumn - * the column index in the full alignment including hidden columns - * @return the position of the column in the visible alignment - */ - public int findColumnPosition(int hiddenColumn) - { - try - { - LOCK.readLock().lock(); - int result = hiddenColumn; - - if (hiddenColumns != null) - { - HiddenCursorPosition cursorPos = cursor - .findRegionForColumn(hiddenColumn); - int index = cursorPos.getRegionIndex(); - int hiddenBeforeCol = cursorPos.getHiddenSoFar(); - - // just subtract hidden cols count - this works fine if column is - // visible - result = hiddenColumn - hiddenBeforeCol; - - // now check in case column is hidden - it will be in the returned - // hidden region - if (index < hiddenColumns.size()) + if (regionIndex != -1 && regionIndex != hiddenColumns.size()) { - int[] region = hiddenColumns.get(index); - if (hiddenColumn >= region[0] && hiddenColumn <= region[1]) + // regionIndex is the region which either contains start + // or lies to the right of start + int[] region = hiddenColumns.get(regionIndex); + if (start == region[0]) { - // actually col is hidden, return region[0]-1 - // unless region[0]==0 in which case return 0 - if (region[0] == 0) + for (int j = region[0]; j < region[1] + 1; j++) { - result = 0; + sel.addElement(j); + } + int colsToRemove = region[1] - region[0] + 1; + hiddenColumns.remove(regionIndex); + + if (hiddenColumns.isEmpty()) + { + hiddenColumns = null; + numColumns = 0; } else { - result = region[0] - 1 - hiddenBeforeCol; + numColumns -= colsToRemove; } + cursor.updateForDeletedRegion(hiddenColumns); } } } - - return result; // return the shifted position after removing hidden - // columns. } finally { - LOCK.readLock().unlock(); + LOCK.writeLock().unlock(); } } /** - * Find the visible column which is a given visible number of columns to the - * left of another visible column. i.e. for a startColumn x, the column which - * is distance 1 away will be column x-1. + * Add gaps into the sequences aligned to profileseq under the given + * AlignmentView * - * @param visibleDistance - * the number of visible columns to offset by - * @param startColumn - * the column to start from - * @return the position of the column in the visible alignment + * @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 int subtractVisibleColumns(int visibleDistance, int startColumn) + public static HiddenColumns propagateInsertions(SequenceI profileseq, + AlignmentI al, AlignmentView input) { - try - { - LOCK.readLock().lock(); - int distance = visibleDistance; - - // in case startColumn is in a hidden region, move it to the left - int start = adjustForHiddenColumns(findColumnPosition(startColumn)); - - Iterator it = new ReverseRegionsIterator(0, start, - hiddenColumns); - - while (it.hasNext() && (distance > 0)) - { - int[] region = it.next(); - - if (start > region[1]) - { - // subtract the gap to right of region from distance - if (start - region[1] <= distance) - { - distance -= start - region[1]; - start = region[0] - 1; - } - else - { - start = start - distance; - distance = 0; - } - } - } - - return start - distance; + int profsqpos = 0; - } finally - { - LOCK.readLock().unlock(); - } + 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; } /** - * This method returns the rightmost limit of a region of an alignment with - * hidden columns. In otherwords, the next hidden column. * - * @param alPos - * the absolute (visible) alignmentPosition to find the next hidden - * column for + * @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 */ - public int getHiddenBoundaryRight(int alPos) + private void propagateInsertions(SequenceI profileseq, AlignmentI al, + SequenceI origseq) { try { - LOCK.readLock().lock(); - if (hiddenColumns != null) + 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.hideMarkedBits(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 regions = hiddenColumns.iterator(); + ArrayList newhidden = new ArrayList<>(); + + int numGapsBefore = 0; + int gapPosition = 0; + while (regions.hasNext()) { - int index = cursor.findRegionForColumn(alPos).getRegionIndex(); - if (index < hiddenColumns.size()) + // 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]) { - int[] region = hiddenColumns.get(index); - if (alPos < region[0]) - { - return region[0]; - } - else if ((alPos <= region[1]) - && (index + 1 < hiddenColumns.size())) + gapPosition++; + if (gaps.get(gapPosition)) { - // alPos is within a hidden region, return the next one - // if there is one - region = hiddenColumns.get(index + 1); - return region[0]; + numGapsBefore++; } } - } - return alPos; - } finally - { - LOCK.readLock().unlock(); - } - } - /** - * This method returns the leftmost limit of a region of an alignment with - * hidden columns. In otherwords, the previous hidden column. - * - * @param alPos - * the absolute (visible) alignmentPosition to find the previous - * hidden column for - */ - public int getHiddenBoundaryLeft(int alPos) - { - try - { - LOCK.readLock().lock(); - - if (hiddenColumns != null) - { - int index = cursor.findRegionForColumn(alPos).getRegionIndex(); + int left = region[0] - numGapsBefore; + int right = region[1] - numGapsBefore; + newhidden.add(new int[] { left, right }); - if (index > 0) + // make a string with number of gaps = length of hidden region + StringBuffer sb = new StringBuffer(); + for (int s = 0; s < right - left + 1; s++) { - int[] region = hiddenColumns.get(index - 1); - return region[1]; + sb.append(gc); } - } - return alPos; - } finally - { - LOCK.readLock().unlock(); - } - } - - /** - * 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; - } - - if (hiddenColumns == null) - { - hiddenColumns = new ArrayList<>(); - } - - /* - * 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; - for (int i = 0; !added && i < hiddenColumns.size(); i++) - { - added = insertRangeAtRegion(i, start, end); - } // for - } - if (!wasAlreadyLocked) - { - cursor.resetCursor(hiddenColumns); + padGaps(sb, left, profileseq, al); - // reset the number of columns so they will be recounted - numColumns = 0; } + hiddenColumns = newhidden; + cursor.resetCursor(hiddenColumns); + numColumns = 0; } finally { - if (!wasAlreadyLocked) - { - LOCK.writeLock().unlock(); - } + LOCK.writeLock().unlock(); } } /** - * Insert [start, range] at the region at index i in hiddenColumns, if - * feasible + * Pad gaps in all sequences in alignment except profileseq * - * @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 + * @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 boolean insertRangeAtRegion(int i, int start, int end) + private void padGaps(StringBuffer sb, int pos, SequenceI profileseq, + AlignmentI al) { - 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) + // loop over the sequences and pad with gaps where required + for (int s = 0, ns = al.getHeight(); s < ns; s++) { - /* - * 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) + SequenceI sqobj = al.getSequenceAt(s); + if (sqobj != profileseq) { - int[] nextRegion = hiddenColumns.get(i + 1); - if (nextRegion[0] > end + 1) + String sq = al.getSequenceAt(s).getSequenceAsString(); + if (sq.length() <= pos) { - /* - * gap to next hidden range - no more to update - */ - break; + // 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)); } - region[1] = Math.max(nextRegion[1], end); - hiddenColumns.subList(i + 1, i + 2).clear(); } - added = true; } - return added; } + /* + * 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. + */ + /** - * Answers if a column in the alignment is visible + * Output regions data as a string. String is in the format: + * reg0[0]reg0[1]reg1[0]reg1[1] ... regn[1] * - * @param column - * absolute position of column in the alignment - * @return true if column is visible + * @param delimiter + * string to delimit regions + * @param betweenstring + * to put between start and end region values + * @return regions formatted according to delimiter and between strings */ - public boolean isVisible(int column) + public String regionsToString(String delimiter, String between) { try { LOCK.readLock().lock(); - - int regionindex = cursor.findRegionForColumn(column).getRegionIndex(); - if (regionindex > -1 && regionindex < hiddenColumns.size()) + StringBuilder regionBuilder = new StringBuilder(); + if (hiddenColumns != null) { - int[] region = hiddenColumns.get(regionindex); - if (column >= region[0] && column <= region[1]) + Iterator it = hiddenColumns.iterator(); + while (it.hasNext()) { - return false; + int[] range = it.next(); + regionBuilder.append(delimiter).append(range[0]).append(between) + .append(range[1]); + if (!it.hasNext()) + { + regionBuilder.deleteCharAt(0); + } } } - return true; - + return regionBuilder.toString(); } finally { LOCK.readLock().unlock(); @@ -627,60 +582,33 @@ public class HiddenColumns } /** - * Get the visible sections of a set of sequences + * Find the number of hidden columns * - * @param start - * sequence position to start from - * @param end - * sequence position to end at - * @param seqs - * an array of sequences - * @return an array of strings encoding the visible parts of each sequence + * @return number of hidden columns */ - public String[] getVisibleSequenceStrings(int start, int end, - SequenceI[] seqs) + public int getSize() { try { LOCK.readLock().lock(); - int iSize = seqs.length; - String[] selections = new String[iSize]; - if (hiddenColumns != null && hiddenColumns.size() > 0) + + if (numColumns == 0 && hiddenColumns != null) { - for (int i = 0; i < iSize; i++) + // numColumns is out of date, so recalculate + int size = 0; + if (hiddenColumns != null) { - StringBuffer visibleSeq = new StringBuffer(); - - Iterator blocks = new VisibleContigsIterator(start, - end + 1, hiddenColumns); - - while (blocks.hasNext()) + Iterator it = hiddenColumns.iterator(); + while (it.hasNext()) { - int[] block = blocks.next(); - if (blocks.hasNext()) - { - visibleSeq - .append(seqs[i].getSequence(block[0], block[1] + 1)); - } - else - { - visibleSeq - .append(seqs[i].getSequence(block[0], block[1])); - } + int[] range = it.next(); + size += range[1] - range[0] + 1; } - - selections[i] = visibleSeq.toString(); - } - } - else - { - for (int i = 0; i < iSize; i++) - { - selections[i] = seqs[i].getSequenceAsString(start, end); } + numColumns = size; } - return selections; + return numColumns; } finally { LOCK.readLock().unlock(); @@ -688,67 +616,65 @@ public class HiddenColumns } /** - * Locate the first position visible for this sequence. If seq isn't visible - * then return the position of the left side of the hidden boundary region. + * Get the number of distinct hidden regions * - * @param seq - * sequence to find position for - * @return visible start position + * @return number of regions */ - public int locateVisibleStartOfSequence(SequenceI seq) + public int getNumberOfRegions() { try { LOCK.readLock().lock(); - int start = 0; - - if (hiddenColumns == null || hiddenColumns.size() == 0) + int num = 0; + if (hasHiddenColumns()) { - return seq.findIndex(seq.getStart()) - 1; + num = hiddenColumns.size(); } + return num; + } finally + { + LOCK.readLock().unlock(); + } + } - // Simply walk along the sequence whilst watching for hidden column - // boundaries - Iterator regions = hiddenColumns.iterator(); - int hideStart = seq.getLength(); - int hideEnd = -1; - int visPrev = 0; - int visNext = 0; - boolean foundStart = false; + @Override + public boolean equals(Object obj) + { + try + { + LOCK.readLock().lock(); - // step through the non-gapped positions of the sequence - for (int i = seq.getStart(); i <= seq.getEnd() && (!foundStart); i++) + if (!(obj instanceof HiddenColumns)) { - // get alignment position of this residue in the sequence - int p = seq.findIndex(i) - 1; + return false; + } + HiddenColumns that = (HiddenColumns) obj; - // update hidden region start/end - while (hideEnd < p && regions.hasNext()) - { - int[] region = regions.next(); - visPrev = visNext; - visNext += region[0] - visPrev; - hideStart = region[0]; - hideEnd = region[1]; - } - if (hideEnd < p) - { - hideStart = seq.getLength(); - } - // update visible boundary for sequence - if (p < hideStart) - { - start = p; - foundStart = true; - } + /* + * check hidden columns are either both null, or match + */ + if (this.hiddenColumns == null) + { + return (that.hiddenColumns == null); + } + if (that.hiddenColumns == null + || that.hiddenColumns.size() != this.hiddenColumns.size()) + { + return false; } - if (foundStart) + Iterator it = hiddenColumns.iterator(); + Iterator thatit = that.iterator(); + while (it.hasNext()) { - return findColumnPosition(start); + int[] thisRange = it.next(); + int[] thatRange = thatit.next(); + if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1]) + { + return false; + } } - // otherwise, sequence was completely hidden - return visPrev; + return true; } finally { LOCK.readLock().unlock(); @@ -756,124 +682,172 @@ public class HiddenColumns } /** - * delete any columns in alignmentAnnotation that are hidden (including - * sequence associated annotation). + * Return absolute column index for a visible column index * - * @param alignmentAnnotation + * @param column + * int column index in alignment view (count from zero) + * @return alignment column index for column */ - public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation) + public int adjustForHiddenColumns(int column) { - makeVisibleAnnotation(0, alignmentAnnotation.annotations.length, - alignmentAnnotation); + try + { + LOCK.readLock().lock(); + int result = column; + + if (hiddenColumns != null) + { + result += cursor.getHiddenOffset(column).getHiddenSoFar(); + } + + return result; + } finally + { + LOCK.readLock().unlock(); + } } /** - * delete any columns in alignmentAnnotation that are hidden (including - * sequence associated annotation). + * Use this method to find out where a column will appear in the visible + * alignment when hidden columns exist. If the column is not visible, then the + * left-most visible column will always be returned. * - * @param start - * remove any annotation to the right of this column - * @param end - * remove any annotation to the left of this column - * @param alignmentAnnotation - * the annotation to operate on + * @param hiddenColumn + * the column index in the full alignment including hidden columns + * @return the position of the column in the visible alignment */ - public void makeVisibleAnnotation(int start, int end, - AlignmentAnnotation alignmentAnnotation) + public int findColumnPosition(int hiddenColumn) { try { LOCK.readLock().lock(); + int result = hiddenColumn; - int startFrom = start; - int endAt = end; - - if (alignmentAnnotation.annotations != null) + if (hiddenColumns != null) { - if (hiddenColumns != null && hiddenColumns.size() > 0) - { - removeHiddenAnnotation(startFrom, endAt, alignmentAnnotation); - } - else + HiddenCursorPosition cursorPos = cursor + .findRegionForColumn(hiddenColumn); + int index = cursorPos.getRegionIndex(); + int hiddenBeforeCol = cursorPos.getHiddenSoFar(); + + // just subtract hidden cols count - this works fine if column is + // visible + result = hiddenColumn - hiddenBeforeCol; + + // now check in case column is hidden - it will be in the returned + // hidden region + if (index < hiddenColumns.size()) { - alignmentAnnotation.restrict(startFrom, endAt); + int[] region = hiddenColumns.get(index); + if (hiddenColumn >= region[0] && hiddenColumn <= region[1]) + { + // actually col is hidden, return region[0]-1 + // unless region[0]==0 in which case return 0 + if (region[0] == 0) + { + result = 0; + } + else + { + result = region[0] - 1 - hiddenBeforeCol; + } + } } } + + return result; // return the shifted position after removing hidden + // columns. } finally { LOCK.readLock().unlock(); } } - private void removeHiddenAnnotation(int start, int end, - AlignmentAnnotation alignmentAnnotation) + /** + * Find the visible column which is a given visible number of columns to the + * left of another visible column. i.e. for a startColumn x, the column which + * is distance 1 away will be column x-1. + * + * @param visibleDistance + * the number of visible columns to offset by + * @param startColumn + * the column to start from + * @return the position of the column in the visible alignment + */ + public int subtractVisibleColumns(int visibleDistance, int startColumn) { - // mangle the alignmentAnnotation annotation array - ArrayList annels = new ArrayList<>(); - Annotation[] els = null; + try + { + LOCK.readLock().lock(); + int distance = visibleDistance; + + // in case startColumn is in a hidden region, move it to the left + int start = adjustForHiddenColumns(findColumnPosition(startColumn)); - int w = 0; - - Iterator blocks = new VisibleContigsIterator(start, end + 1, - hiddenColumns); + Iterator it = new ReverseRegionsIterator(0, start, + hiddenColumns); - int copylength; - int annotationLength; - while (blocks.hasNext()) - { - int[] block = blocks.next(); - annotationLength = block[1] - block[0] + 1; - - if (blocks.hasNext()) - { - // copy just the visible segment of the annotation row - copylength = annotationLength; - } - else + while (it.hasNext() && (distance > 0)) { - if (annotationLength + block[0] <= alignmentAnnotation.annotations.length) - { - // copy just the visible segment of the annotation row - copylength = annotationLength; - } - else + int[] region = it.next(); + + if (start > region[1]) { - // copy to the end of the annotation row - copylength = alignmentAnnotation.annotations.length - block[0]; + // subtract the gap to right of region from distance + if (start - region[1] <= distance) + { + distance -= start - region[1]; + start = region[0] - 1; + } + else + { + start = start - distance; + distance = 0; + } } } - - els = new Annotation[annotationLength]; - annels.add(els); - System.arraycopy(alignmentAnnotation.annotations, block[0], els, 0, - copylength); - w += annotationLength; - } - - if (w != 0) - { - alignmentAnnotation.annotations = new Annotation[w]; + return start - distance; - w = 0; - for (Annotation[] chnk : annels) - { - System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w, - chnk.length); - w += chnk.length; - } + } finally + { + LOCK.readLock().unlock(); } } /** + * This method returns the rightmost limit of a region of an alignment with + * hidden columns. In otherwords, the next hidden column. * - * @return true if there are columns hidden + * @param alPos + * the absolute (visible) alignmentPosition to find the next hidden + * column for */ - public boolean hasHiddenColumns() + public int getHiddenBoundaryRight(int alPos) { try { LOCK.readLock().lock(); - return hiddenColumns != null && hiddenColumns.size() > 0; + if (hiddenColumns != null) + { + int index = cursor.findRegionForColumn(alPos).getRegionIndex(); + if (index < hiddenColumns.size()) + { + int[] region = hiddenColumns.get(index); + if (alPos < region[0]) + { + return region[0]; + } + else if ((alPos <= region[1]) + && (index + 1 < hiddenColumns.size())) + { + // alPos is within a hidden region, return the next one + // if there is one + region = hiddenColumns.get(index + 1); + return region[0]; + } + } + } + return alPos; } finally { LOCK.readLock().unlock(); @@ -881,286 +855,338 @@ public class HiddenColumns } /** + * This method returns the leftmost limit of a region of an alignment with + * hidden columns. In otherwords, the previous hidden column. * - * @return true if there are more than one set of columns hidden + * @param alPos + * the absolute (visible) alignmentPosition to find the previous + * hidden column for */ - public boolean hasManyHiddenColumns() + public int getHiddenBoundaryLeft(int alPos) { try { LOCK.readLock().lock(); - return hiddenColumns != null && hiddenColumns.size() > 1; + + if (hiddenColumns != null) + { + int index = cursor.findRegionForColumn(alPos).getRegionIndex(); + + if (index > 0) + { + int[] region = hiddenColumns.get(index - 1); + return region[1]; + } + } + return alPos; } finally { LOCK.readLock().unlock(); } } + /** - * mark the columns corresponding to gap characters as hidden in the column - * selection + * Answers if a column in the alignment is visible * - * @param sr + * @param column + * absolute position of column in the alignment + * @return true if column is visible */ - public void hideInsertionsFor(SequenceI sr) + public boolean isVisible(int column) { try { - LOCK.writeLock().lock(); - List inserts = sr.getInsertions(); - for (int[] r : inserts) + LOCK.readLock().lock(); + + int regionindex = cursor.findRegionForColumn(column).getRegionIndex(); + if (regionindex > -1 && regionindex < hiddenColumns.size()) { - hideColumns(r[0], r[1]); + int[] region = hiddenColumns.get(regionindex); + if (column >= region[0] && column <= region[1]) + { + return false; + } } - cursor.resetCursor(hiddenColumns); - numColumns = 0; + return true; + } finally { - LOCK.writeLock().unlock(); + LOCK.readLock().unlock(); } } /** - * Unhides, and adds to the selection list, all hidden columns + * Get the visible sections of a set of sequences + * + * @param start + * sequence position to start from + * @param end + * sequence position to end at + * @param seqs + * an array of sequences + * @return an array of strings encoding the visible parts of each sequence */ - public void revealAllHiddenColumns(ColumnSelection sel) + public String[] getVisibleSequenceStrings(int start, int end, + SequenceI[] seqs) { try { - LOCK.writeLock().lock(); - if (hiddenColumns != null) + LOCK.readLock().lock(); + int iSize = seqs.length; + String[] selections = new String[iSize]; + if (hiddenColumns != null && hiddenColumns.size() > 0) { - Iterator it = hiddenColumns.iterator(); - while (it.hasNext()) + for (int i = 0; i < iSize; i++) { - int[] region = it.next(); - for (int j = region[0]; j < region[1] + 1; j++) + StringBuffer visibleSeq = new StringBuffer(); + + Iterator blocks = new VisibleContigsIterator(start, + end + 1, hiddenColumns); + + while (blocks.hasNext()) { - sel.addElement(j); + int[] block = blocks.next(); + if (blocks.hasNext()) + { + visibleSeq + .append(seqs[i].getSequence(block[0], block[1] + 1)); + } + else + { + visibleSeq + .append(seqs[i].getSequence(block[0], block[1])); + } } + + selections[i] = visibleSeq.toString(); + } + } + else + { + for (int i = 0; i < iSize; i++) + { + selections[i] = seqs[i].getSequenceAsString(start, end); } - hiddenColumns = null; - cursor.resetCursor(hiddenColumns); - numColumns = 0; } + + return selections; } finally { - LOCK.writeLock().unlock(); + LOCK.readLock().unlock(); } } /** - * Reveals, and marks as selected, the hidden column range with the given - * start column + * Locate the first position visible for this sequence. If seq isn't visible + * then return the position of the left side of the hidden boundary region. * - * @param start + * @param seq + * sequence to find position for + * @return visible start position */ - public void revealHiddenColumns(int start, ColumnSelection sel) + public int locateVisibleStartOfSequence(SequenceI seq) { try { - LOCK.writeLock().lock(); + LOCK.readLock().lock(); + int start = 0; - if (hiddenColumns != null) + if (hiddenColumns == null || hiddenColumns.size() == 0) { - int regionIndex = cursor.findRegionForColumn(start) - .getRegionIndex(); + return seq.findIndex(seq.getStart()) - 1; + } - 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); + // Simply walk along the sequence whilst watching for hidden column + // boundaries + Iterator regions = hiddenColumns.iterator(); + int hideStart = seq.getLength(); + int hideEnd = -1; + int visPrev = 0; + int visNext = 0; + boolean foundStart = false; - if (hiddenColumns.isEmpty()) - { - hiddenColumns = null; - numColumns = 0; - } - else - { - numColumns -= colsToRemove; - } - cursor.updateForDeletedRegion(hiddenColumns); + // step through the non-gapped positions of the sequence + for (int i = seq.getStart(); i <= seq.getEnd() && (!foundStart); i++) + { + // get alignment position of this residue in the sequence + int p = seq.findIndex(i) - 1; - } + // update hidden region start/end + while (hideEnd < p && regions.hasNext()) + { + int[] region = regions.next(); + visPrev = visNext; + visNext += region[0] - visPrev; + hideStart = region[0]; + hideEnd = region[1]; + } + if (hideEnd < p) + { + hideStart = seq.getLength(); + } + // update visible boundary for sequence + if (p < hideStart) + { + start = p; + foundStart = true; } } + + if (foundStart) + { + return findColumnPosition(start); + } + // otherwise, sequence was completely hidden + return visPrev; } finally { - LOCK.writeLock().unlock(); + LOCK.readLock().unlock(); } } /** - * Add gaps into the sequences aligned to profileseq under the given - * AlignmentView + * delete any columns in alignmentAnnotation that are hidden (including + * sequence associated annotation). * - * @param profileseq - * @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. + * @param alignmentAnnotation */ - public static HiddenColumns propagateInsertions(SequenceI profileseq, - AlignmentI al, AlignmentView input) + public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation) { - 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; + makeVisibleAnnotation(0, alignmentAnnotation.annotations.length, + alignmentAnnotation); } /** + * delete any columns in alignmentAnnotation that are hidden (including + * sequence associated annotation). * - * @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 + * @param start + * remove any annotation to the right of this column + * @param end + * remove any annotation to the left of this column + * @param alignmentAnnotation + * the annotation to operate on */ - private void propagateInsertions(SequenceI profileseq, AlignmentI al, - SequenceI origseq) + public void makeVisibleAnnotation(int start, int end, + AlignmentAnnotation alignmentAnnotation) { 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.hideMarkedBits(hidden); + LOCK.readLock().lock(); - // 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 regions = hiddenColumns.iterator(); - ArrayList newhidden = new ArrayList<>(); + int startFrom = start; + int endAt = end; - int numGapsBefore = 0; - int gapPosition = 0; - while (regions.hasNext()) + if (alignmentAnnotation.annotations != null) { - // 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]) + if (hiddenColumns != null && hiddenColumns.size() > 0) { - gapPosition++; - if (gaps.get(gapPosition)) - { - numGapsBefore++; - } + removeHiddenAnnotation(startFrom, endAt, alignmentAnnotation); } - - 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++) + else { - sb.append(gc); + alignmentAnnotation.restrict(startFrom, endAt); } - padGaps(sb, left, profileseq, al); - } - hiddenColumns = newhidden; - cursor.resetCursor(hiddenColumns); - numColumns = 0; } finally { - LOCK.writeLock().unlock(); + LOCK.readLock().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) + private void removeHiddenAnnotation(int start, int end, + AlignmentAnnotation alignmentAnnotation) { - // loop over the sequences and pad with gaps where required - for (int s = 0, ns = al.getHeight(); s < ns; s++) + // mangle the alignmentAnnotation annotation array + ArrayList annels = new ArrayList<>(); + Annotation[] els = null; + + int w = 0; + + Iterator blocks = new VisibleContigsIterator(start, end + 1, + hiddenColumns); + + int copylength; + int annotationLength; + while (blocks.hasNext()) { - SequenceI sqobj = al.getSequenceAt(s); - if (sqobj != profileseq) + int[] block = blocks.next(); + annotationLength = block[1] - block[0] + 1; + + if (blocks.hasNext()) { - String sq = al.getSequenceAt(s).getSequenceAsString(); - if (sq.length() <= pos) + // copy just the visible segment of the annotation row + copylength = annotationLength; + } + else + { + if (annotationLength + block[0] <= alignmentAnnotation.annotations.length) { - // 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(); + // copy just the visible segment of the annotation row + copylength = annotationLength; } else { - al.getSequenceAt(s).setSequence( - sq.substring(0, pos) + sb.toString() + sq.substring(pos)); + // copy to the end of the annotation row + copylength = alignmentAnnotation.annotations.length - block[0]; } } + + els = new Annotation[annotationLength]; + annels.add(els); + System.arraycopy(alignmentAnnotation.annotations, block[0], els, 0, + copylength); + w += annotationLength; + } + + if (w != 0) + { + alignmentAnnotation.annotations = new Annotation[w]; + + w = 0; + for (Annotation[] chnk : annels) + { + System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w, + chnk.length); + w += chnk.length; + } + } + } + + /** + * + * @return true if there are columns hidden + */ + public boolean hasHiddenColumns() + { + try + { + LOCK.readLock().lock(); + return hiddenColumns != null && hiddenColumns.size() > 0; + } finally + { + LOCK.readLock().unlock(); + } + } + + /** + * + * @return true if there are more than one set of columns hidden + */ + public boolean hasManyHiddenColumns() + { + try + { + LOCK.readLock().lock(); + return hiddenColumns != null && hiddenColumns.size() > 1; + } finally + { + LOCK.readLock().unlock(); } } + /** * Returns a hashCode built from hidden column ranges */ @@ -1466,7 +1492,18 @@ public class HiddenColumns // and then create a VisibleContigsIterator // but without a cursor this will be horribly slow in some situations // ... so until then... - return new VisibleBlocksVisBoundsIterator(start, end, true); + // return new VisibleBlocksVisBoundsIterator(start, end, true); + try + { + LOCK.readLock().lock(); + int adjstart = adjustForHiddenColumns(start); + int adjend = adjustForHiddenColumns(end); + return new VisibleContigsIterator(adjstart, adjend + 1, + hiddenColumns); + } finally + { + LOCK.readLock().unlock(); + } } else { @@ -1475,7 +1512,7 @@ public class HiddenColumns LOCK.readLock().lock(); return new VisibleContigsIterator(start, end + 1, hiddenColumns); } finally - { + { LOCK.readLock().unlock(); } } diff --git a/src/jalview/datamodel/VisibleContigsIterator.java b/src/jalview/datamodel/VisibleContigsIterator.java index d7594f9..ff060ac 100644 --- a/src/jalview/datamodel/VisibleContigsIterator.java +++ b/src/jalview/datamodel/VisibleContigsIterator.java @@ -13,6 +13,8 @@ public class VisibleContigsIterator implements Iterator private int currentPosition = 0; + private boolean endsAtHidden = false; + VisibleContigsIterator(int start, int end, List hiddenColumns) { @@ -24,6 +26,7 @@ public class VisibleContigsIterator implements Iterator for (int[] region : hiddenColumns) { + endsAtHidden = false; hideStart = region[0]; hideEnd = region[1]; @@ -36,6 +39,7 @@ public class VisibleContigsIterator implements Iterator { int[] contig = new int[] { vstart, hideStart - 1 }; vcontigs.add(contig); + endsAtHidden = true; } vstart = hideEnd + 1; @@ -50,6 +54,7 @@ public class VisibleContigsIterator implements Iterator { int[] contig = new int[] { vstart, end - 1 }; vcontigs.add(contig); + endsAtHidden = false; } } else @@ -72,5 +77,10 @@ public class VisibleContigsIterator implements Iterator currentPosition++; return result; } + + public boolean endsAtHidden() + { + return endsAtHidden; + } } diff --git a/src/jalview/gui/SeqCanvas.java b/src/jalview/gui/SeqCanvas.java index 6c4391c..29e3c7b 100755 --- a/src/jalview/gui/SeqCanvas.java +++ b/src/jalview/gui/SeqCanvas.java @@ -22,10 +22,10 @@ package jalview.gui; import jalview.datamodel.AlignmentI; import jalview.datamodel.HiddenColumns; -import jalview.datamodel.HiddenColumns.VisibleBlocksVisBoundsIterator; import jalview.datamodel.SearchResultsI; import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; +import jalview.datamodel.VisibleContigsIterator; import jalview.renderer.ScaleRenderer; import jalview.renderer.ScaleRenderer.ScaleMark; import jalview.util.Comparison; @@ -1021,7 +1021,7 @@ public class SeqCanvas extends JComponent implements ViewportListenerI int blockEnd; HiddenColumns hidden = av.getAlignment().getHiddenColumns(); - VisibleBlocksVisBoundsIterator regions = (VisibleBlocksVisBoundsIterator) hidden + VisibleContigsIterator regions = (VisibleContigsIterator) hidden .getVisibleBlocksIterator(startRes, endRes, true); while (regions.hasNext()) @@ -1274,7 +1274,7 @@ public class SeqCanvas extends JComponent implements ViewportListenerI int blockEnd; HiddenColumns hidden = av.getAlignment().getHiddenColumns(); - VisibleBlocksVisBoundsIterator regions = (VisibleBlocksVisBoundsIterator) hidden + VisibleContigsIterator regions = (VisibleContigsIterator) hidden .getVisibleBlocksIterator(startRes, endRes, true); while (regions.hasNext()) {