From 0e5e2bec7c3518eab6df87070b23164b135af828 Mon Sep 17 00:00:00 2001 From: kiramt Date: Fri, 19 Jan 2018 16:25:16 +0000 Subject: [PATCH] JAL-2759 Changes to getVisibleSequenceStrings after review --- src/jalview/appletgui/SeqPanel.java | 7 +- src/jalview/datamodel/HiddenColumns.java | 103 ++----------------------- src/jalview/datamodel/Sequence.java | 21 +++++ src/jalview/datamodel/SequenceI.java | 12 +++ src/jalview/gui/AnnotationLabels.java | 6 +- src/jalview/gui/SeqPanel.java | 7 +- src/jalview/viewmodel/AlignmentViewport.java | 13 +++- test/jalview/datamodel/HiddenColumnsTest.java | 45 ++++++----- 8 files changed, 86 insertions(+), 128 deletions(-) diff --git a/src/jalview/appletgui/SeqPanel.java b/src/jalview/appletgui/SeqPanel.java index d6ed3f3..e07dae6 100644 --- a/src/jalview/appletgui/SeqPanel.java +++ b/src/jalview/appletgui/SeqPanel.java @@ -1123,9 +1123,9 @@ public class SeqPanel extends Panel implements MouseMotionListener, { fixedColumns = true; int y1 = av.getAlignment().getHiddenColumns() - .getHiddenBoundaryLeft(startres); + .getNextHiddenBoundary(true, startres); int y2 = av.getAlignment().getHiddenColumns() - .getHiddenBoundaryRight(startres); + .getNextHiddenBoundary(false, startres); if ((insertGap && startres > y1 && lastres < y1) || (!insertGap && startres < y2 && lastres > y2)) @@ -1197,7 +1197,8 @@ public class SeqPanel extends Panel implements MouseMotionListener, if (sg.getSize() == av.getAlignment().getHeight()) { if ((av.hasHiddenColumns() && startres < av.getAlignment() - .getHiddenColumns().getHiddenBoundaryRight(startres))) + .getHiddenColumns() + .getNextHiddenBoundary(false, startres))) { endEditing(); return; diff --git a/src/jalview/datamodel/HiddenColumns.java b/src/jalview/datamodel/HiddenColumns.java index 6e594bd..ab71cbe 100644 --- a/src/jalview/datamodel/HiddenColumns.java +++ b/src/jalview/datamodel/HiddenColumns.java @@ -639,7 +639,7 @@ public class HiddenColumns * @return the index of the next hidden column, or alPos if there is no next * hidden column */ - public int getHiddenBoundaryRight(int alPos) + public int getNextHiddenBoundary(boolean left, int alPos) { try { @@ -647,7 +647,13 @@ public class HiddenColumns if (!hiddenColumns.isEmpty()) { int index = cursor.findRegionForColumn(alPos).getRegionIndex(); - if (index < hiddenColumns.size()) + + if (left && index > 0) + { + int[] region = hiddenColumns.get(index - 1); + return region[1]; + } + else if (!left && index < hiddenColumns.size()) { int[] region = hiddenColumns.get(index); if (alPos < region[0]) @@ -672,38 +678,6 @@ public class HiddenColumns } /** - * 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.isEmpty()) - { - int index = cursor.findRegionForColumn(alPos).getRegionIndex(); - - if (index > 0) - { - int[] region = hiddenColumns.get(index - 1); - return region[1]; - } - } - return alPos; - } finally - { - LOCK.readLock().unlock(); - } - } - - - /** * Answers if a column in the alignment is visible * * @param column @@ -736,67 +710,6 @@ public class HiddenColumns } /** - * 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 String[] getVisibleSequenceStrings(int start, int end, - SequenceI[] seqs) - { - try - { - LOCK.readLock().lock(); - int iSize = seqs.length; - String[] selections = new String[iSize]; - if (!hiddenColumns.isEmpty()) - { - for (int i = 0; i < iSize; i++) - { - StringBuilder visibleSeq = new StringBuilder(); - - Iterator blocks = new VisibleContigsIterator(start, - end + 1, hiddenColumns); - - while (blocks.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])); - } - } - - selections[i] = visibleSeq.toString(); - } - } - else - { - for (int i = 0; i < iSize; i++) - { - selections[i] = seqs[i].getSequenceAsString(start, end); - } - } - - return selections; - } finally - { - LOCK.readLock().unlock(); - } - } - - /** * 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. * diff --git a/src/jalview/datamodel/Sequence.java b/src/jalview/datamodel/Sequence.java index 9c2fa25..895b230 100755 --- a/src/jalview/datamodel/Sequence.java +++ b/src/jalview/datamodel/Sequence.java @@ -34,6 +34,7 @@ import java.util.Arrays; import java.util.BitSet; import java.util.Collections; import java.util.Enumeration; +import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.Vector; @@ -1925,4 +1926,24 @@ public class Sequence extends ASequence implements SequenceI return count; } + + @Override + public String getSequenceStringFromIterator(Iterator it) + { + StringBuilder newSequence = new StringBuilder(); + while (it.hasNext()) + { + int[] block = it.next(); + if (it.hasNext()) + { + newSequence.append(getSequence(block[0], block[1] + 1)); + } + else + { + newSequence.append(getSequence(block[0], block[1])); + } + } + + return newSequence.toString(); + } } diff --git a/src/jalview/datamodel/SequenceI.java b/src/jalview/datamodel/SequenceI.java index 6669d43..52072bb 100755 --- a/src/jalview/datamodel/SequenceI.java +++ b/src/jalview/datamodel/SequenceI.java @@ -23,6 +23,7 @@ package jalview.datamodel; import jalview.datamodel.features.SequenceFeaturesI; import java.util.BitSet; +import java.util.Iterator; import java.util.List; import java.util.Vector; @@ -531,4 +532,15 @@ public interface SequenceI extends ASequenceI * @param c2 */ public int replace(char c1, char c2); + + /** + * Returns the sequence string constructed from the substrings of a sequence + * defined by the int[] ranges provided by an iterator. E.g. the iterator + * could iterate over all visible regions of the alignment + * + * @param it + * the iterator to use + * @return a String corresponding to the sequence + */ + public String getSequenceStringFromIterator(Iterator it); } diff --git a/src/jalview/gui/AnnotationLabels.java b/src/jalview/gui/AnnotationLabels.java index 45350fd..c883071 100755 --- a/src/jalview/gui/AnnotationLabels.java +++ b/src/jalview/gui/AnnotationLabels.java @@ -52,6 +52,7 @@ import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.util.Arrays; import java.util.Collections; +import java.util.Iterator; import java.util.regex.Pattern; import javax.swing.JCheckBoxMenuItem; @@ -945,8 +946,9 @@ public class AnnotationLabels extends JPanel Alignment ds = new Alignment(dseqs); if (av.hasHiddenColumns()) { - omitHidden = av.getAlignment().getHiddenColumns() - .getVisibleSequenceStrings(0, sq.getLength(), seqs); + Iterator it = av.getAlignment().getHiddenColumns() + .getVisContigsIterator(0, sq.getLength(), false); + omitHidden = new String[] { sq.getSequenceStringFromIterator(it) }; } int[] alignmentStartEnd = new int[] { 0, ds.getWidth() - 1 }; diff --git a/src/jalview/gui/SeqPanel.java b/src/jalview/gui/SeqPanel.java index 57d0e35..9359569 100644 --- a/src/jalview/gui/SeqPanel.java +++ b/src/jalview/gui/SeqPanel.java @@ -1268,9 +1268,9 @@ public class SeqPanel extends JPanel { fixedColumns = true; int y1 = av.getAlignment().getHiddenColumns() - .getHiddenBoundaryLeft(startres); + .getNextHiddenBoundary(true, startres); int y2 = av.getAlignment().getHiddenColumns() - .getHiddenBoundaryRight(startres); + .getNextHiddenBoundary(false, startres); if ((insertGap && startres > y1 && lastres < y1) || (!insertGap && startres < y2 && lastres > y2)) @@ -1346,7 +1346,8 @@ public class SeqPanel extends JPanel if (sg.getSize() == av.getAlignment().getHeight()) { if ((av.hasHiddenColumns() && startres < av.getAlignment() - .getHiddenColumns().getHiddenBoundaryRight(startres))) + .getHiddenColumns() + .getNextHiddenBoundary(false, startres))) { endEditing(); return; diff --git a/src/jalview/viewmodel/AlignmentViewport.java b/src/jalview/viewmodel/AlignmentViewport.java index 44dfe8d..01ec8b1 100644 --- a/src/jalview/viewmodel/AlignmentViewport.java +++ b/src/jalview/viewmodel/AlignmentViewport.java @@ -67,6 +67,7 @@ import java.util.BitSet; import java.util.Deque; import java.util.HashMap; import java.util.Hashtable; +import java.util.Iterator; import java.util.List; import java.util.Map; @@ -1740,8 +1741,12 @@ public abstract class AlignmentViewport if (alignment.getHiddenColumns() != null && alignment.getHiddenColumns().hasHiddenColumns()) { - selection = alignment.getHiddenColumns() - .getVisibleSequenceStrings(start, end, seqs); + for (i = 0; i < iSize; i++) + { + Iterator blocks = alignment.getHiddenColumns() + .getVisContigsIterator(start, end + 1, false); + selection[i] = seqs[i].getSequenceStringFromIterator(blocks); + } } else { @@ -1771,7 +1776,7 @@ public abstract class AlignmentViewport start = hidden.visibleToAbsoluteColumn(start); } - end = hidden.getHiddenBoundaryRight(start); + end = hidden.getNextHiddenBoundary(false, start); if (start == end) { end = max; @@ -1787,7 +1792,7 @@ public abstract class AlignmentViewport if (hidden != null && hidden.hasHiddenColumns()) { start = hidden.visibleToAbsoluteColumn(end); - start = hidden.getHiddenBoundaryLeft(start) + 1; + start = hidden.getNextHiddenBoundary(true, start) + 1; } } while (end < max); diff --git a/test/jalview/datamodel/HiddenColumnsTest.java b/test/jalview/datamodel/HiddenColumnsTest.java index 07a4810..316b003 100644 --- a/test/jalview/datamodel/HiddenColumnsTest.java +++ b/test/jalview/datamodel/HiddenColumnsTest.java @@ -292,9 +292,12 @@ public class HiddenColumnsTest // hide columns around most of sequence - leave one residue remaining cs.hideColumns(1, 3); cs.hideColumns(6, 11); - assertEquals("-D", - cs.getVisibleSequenceStrings(0, 5, new SequenceI[] - { seq })[0]); + + Iterator it = cs.getVisContigsIterator(0, 6, false); + + assertEquals("-D", seq.getSequenceStringFromIterator(it)); + // cs.getVisibleSequenceStrings(0, 5, new SequenceI[] + // { seq })[0]); assertEquals(1, cs.locateVisibleStartOfSequence(seq)); cs.revealAllHiddenColumns(colsel); @@ -925,37 +928,37 @@ public class HiddenColumnsTest HiddenColumns h = new HiddenColumns(); // returns same value if no hidden cols - assertEquals(3, h.getHiddenBoundaryLeft(3)); + assertEquals(3, h.getNextHiddenBoundary(true, 3)); h.hideColumns(5, 10); - assertEquals(10, h.getHiddenBoundaryLeft(15)); - assertEquals(3, h.getHiddenBoundaryLeft(3)); - assertEquals(7, h.getHiddenBoundaryLeft(7)); + assertEquals(10, h.getNextHiddenBoundary(true, 15)); + assertEquals(3, h.getNextHiddenBoundary(true, 3)); + assertEquals(7, h.getNextHiddenBoundary(true, 7)); h.hideColumns(15, 20); - assertEquals(10, h.getHiddenBoundaryLeft(15)); - assertEquals(20, h.getHiddenBoundaryLeft(21)); + assertEquals(10, h.getNextHiddenBoundary(true, 15)); + assertEquals(20, h.getNextHiddenBoundary(true, 21)); } @Test(groups = "Functional") - public void testGetHiddenBoundaryRight() + public void testGetNextHiddenBoundary() { HiddenColumns h = new HiddenColumns(); // returns same value if no hidden cols - assertEquals(3, h.getHiddenBoundaryRight(3)); + assertEquals(3, h.getNextHiddenBoundary(false, 3)); h.hideColumns(5, 10); - assertEquals(5, h.getHiddenBoundaryRight(3)); - assertEquals(15, h.getHiddenBoundaryRight(15)); - assertEquals(7, h.getHiddenBoundaryRight(7)); + assertEquals(5, h.getNextHiddenBoundary(false, 3)); + assertEquals(15, h.getNextHiddenBoundary(false, 15)); + assertEquals(7, h.getNextHiddenBoundary(false, 7)); h.hideColumns(15, 20); - assertEquals(15, h.getHiddenBoundaryRight(7)); - assertEquals(15, h.getHiddenBoundaryRight(14)); + assertEquals(15, h.getNextHiddenBoundary(false, 7)); + assertEquals(15, h.getNextHiddenBoundary(false, 14)); // returns same value if there is no next hidden column - assertEquals(22, h.getHiddenBoundaryRight(22)); + assertEquals(22, h.getNextHiddenBoundary(false, 22)); } @Test(groups = "Functional") @@ -993,7 +996,7 @@ public class HiddenColumnsTest assertFalse(result.hasNext()); } - @Test(groups = "Functional") + /* @Test(groups = "Functional") public void testGetVisibleSequenceStrings() { HiddenColumns h = new HiddenColumns(); @@ -1006,13 +1009,13 @@ public class HiddenColumnsTest assertEquals(2, result.length); assertEquals("WKQES", result[0]); assertEquals("RNDTG", result[1]); - + h.hideColumns(6, 8); result = h.getVisibleSequenceStrings(5, 10, seqs); assertEquals(2, result.length); assertEquals("WS", result[0]); assertEquals("RG", result[1]); - + SequenceI seq = new Sequence("RefSeq", "-A-SD-ASD--E---"); ColumnSelection sel = new ColumnSelection(); h.revealAllHiddenColumns(sel); @@ -1021,7 +1024,7 @@ public class HiddenColumnsTest assertEquals("-D", h.getVisibleSequenceStrings(0, 5, new SequenceI[] { seq })[0]); - } + }*/ @Test(groups = "Functional") public void testHideInsertionsFor() -- 1.7.10.2