From: kiramt Date: Mon, 22 Jan 2018 14:44:09 +0000 (+0000) Subject: JAL-2579 Moved locateVisibleStartOfSequence to Sequence (refactored) X-Git-Tag: Release_2_10_4~55^2~1^2~24 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=acf6d0deb9caf0148791f2993be0a2cd7987f569;p=jalview.git JAL-2579 Moved locateVisibleStartOfSequence to Sequence (refactored) --- diff --git a/src/jalview/datamodel/HiddenColumns.java b/src/jalview/datamodel/HiddenColumns.java index b903826..8cb7971 100644 --- a/src/jalview/datamodel/HiddenColumns.java +++ b/src/jalview/datamodel/HiddenColumns.java @@ -706,75 +706,6 @@ 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. - * - * @param seq - * sequence to find position for - * @return visible start position - */ - public int locateVisibleStartOfSequence(SequenceI seq) - { - try - { - LOCK.readLock().lock(); - int start = 0; - - if (hiddenColumns.isEmpty()) - { - return seq.findIndex(seq.getStart()) - 1; - } - - // 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; - - // 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 absoluteToVisibleColumn(start); - } - // otherwise, sequence was completely hidden - return visPrev; - } finally - { - LOCK.readLock().unlock(); - } - } - - - /** * * @return true if there are columns hidden */ diff --git a/src/jalview/datamodel/Sequence.java b/src/jalview/datamodel/Sequence.java index 895b230..e851a29 100755 --- a/src/jalview/datamodel/Sequence.java +++ b/src/jalview/datamodel/Sequence.java @@ -445,7 +445,7 @@ public class Sequence extends ASequence implements SequenceI @Override public Vector getAllPDBEntries() { - return pdbIds == null ? new Vector() : pdbIds; + return pdbIds == null ? new Vector<>() : pdbIds; } /** @@ -1946,4 +1946,53 @@ public class Sequence extends ASequence implements SequenceI return newSequence.toString(); } + + @Override + public int firstResidueOutsideIterator(Iterator regions) + { + int start = 0; + + if (!regions.hasNext()) + { + return findIndex(getStart()) - 1; + } + + // Simply walk along the sequence whilst watching for region + // boundaries + int hideStart = getLength(); + int hideEnd = -1; + boolean foundStart = false; + + // step through the non-gapped positions of the sequence + for (int i = getStart(); i <= getEnd() && (!foundStart); i++) + { + // get alignment position of this residue in the sequence + int p = findIndex(i) - 1; + + // update region start/end + while (hideEnd < p && regions.hasNext()) + { + int[] region = regions.next(); + hideStart = region[0]; + hideEnd = region[1]; + } + if (hideEnd < p) + { + hideStart = getLength(); + } + // update boundary for sequence + if (p < hideStart) + { + start = p; + foundStart = true; + } + } + + if (foundStart) + { + return start; + } + // otherwise, sequence was completely hidden + return 0; + } } diff --git a/src/jalview/datamodel/SequenceI.java b/src/jalview/datamodel/SequenceI.java index 52072bb..b22e48f 100755 --- a/src/jalview/datamodel/SequenceI.java +++ b/src/jalview/datamodel/SequenceI.java @@ -543,4 +543,14 @@ public interface SequenceI extends ASequenceI * @return a String corresponding to the sequence */ public String getSequenceStringFromIterator(Iterator it); + + /** + * Locate the first position in this sequence which is not contained in an + * iterator region. If no such position exists, return 0 + * + * @param it + * iterator over regions + * @return first residue not contained in regions + */ + public int firstResidueOutsideIterator(Iterator it); } diff --git a/src/jalview/renderer/ScaleRenderer.java b/src/jalview/renderer/ScaleRenderer.java index c7406a5..dc3272f 100644 --- a/src/jalview/renderer/ScaleRenderer.java +++ b/src/jalview/renderer/ScaleRenderer.java @@ -21,9 +21,11 @@ package jalview.renderer; import jalview.api.AlignViewportI; +import jalview.datamodel.HiddenColumns; import jalview.datamodel.SequenceI; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; /** @@ -83,12 +85,16 @@ public class ScaleRenderer int refSp = 0; int refStartI = 0; int refEndI = -1; + + HiddenColumns hc = av.getAlignment().getHiddenColumns(); + if (refSeq != null) { // find bounds and set origin appropriately - // locate first visible position for this sequence - refSp = av.getAlignment().getHiddenColumns() - .locateVisibleStartOfSequence(refSeq); + // locate first residue in sequence which is not hidden + Iterator it = hc.iterator(); + int index = refSeq.firstResidueOutsideIterator(it); + refSp = hc.absoluteToVisibleColumn(index); refStartI = refSeq.findIndex(refSeq.getStart()) - 1; @@ -114,14 +120,12 @@ public class ScaleRenderer { if (refSeq == null) { - iadj = av.getAlignment().getHiddenColumns() - .visibleToAbsoluteColumn(i - 1) + 1; + iadj = hc.visibleToAbsoluteColumn(i - 1) + 1; string = String.valueOf(iadj); } else { - iadj = av.getAlignment().getHiddenColumns() - .visibleToAbsoluteColumn(i - 1); + iadj = hc.visibleToAbsoluteColumn(i - 1); refN = refSeq.findPosition(iadj); // TODO show bounds if position is a gap // - ie L--R -> "1L|2R" for diff --git a/test/jalview/datamodel/HiddenColumnsTest.java b/test/jalview/datamodel/HiddenColumnsTest.java index 316b003..6730626 100644 --- a/test/jalview/datamodel/HiddenColumnsTest.java +++ b/test/jalview/datamodel/HiddenColumnsTest.java @@ -259,175 +259,6 @@ public class HiddenColumnsTest assertFalse(cs2.hasHiddenColumns()); } - /** - * Test the code used to locate the reference sequence ruler origin - */ - @Test(groups = { "Functional" }) - public void testLocateVisibleStartofSequence() - { - // create random alignment - AlignmentGenerator gen = new AlignmentGenerator(false); - AlignmentI al = gen.generate(50, 20, 123, 5, 5); - - HiddenColumns cs = al.getHiddenColumns(); - ColumnSelection colsel = new ColumnSelection(); - - SequenceI seq = new Sequence("RefSeq", "-A-SD-ASD--E---"); - assertEquals(2, seq.findIndex(seq.getStart())); - - // no hidden columns - assertEquals(seq.findIndex(seq.getStart()) - 1, cs.locateVisibleStartOfSequence(seq)); - - // hidden column on gap after end of sequence - should not affect bounds - colsel.hideSelectedColumns(13, al.getHiddenColumns()); - assertEquals(seq.findIndex(seq.getStart()) - 1,cs.locateVisibleStartOfSequence(seq)); - - cs.revealAllHiddenColumns(colsel); - // hidden column on gap before beginning of sequence - should vis bounds by - // one - colsel.hideSelectedColumns(0, al.getHiddenColumns()); - assertEquals(seq.findIndex(seq.getStart()) - 2,cs.locateVisibleStartOfSequence(seq)); - - cs.revealAllHiddenColumns(colsel); - // hide columns around most of sequence - leave one residue remaining - cs.hideColumns(1, 3); - cs.hideColumns(6, 11); - - 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); - - // hide whole sequence - should just get location of hidden region - // containing sequence - cs.hideColumns(1, 11); - assertEquals(0,cs.locateVisibleStartOfSequence(seq)); - - cs.revealAllHiddenColumns(colsel); - cs.hideColumns(0, 15); - assertEquals(0,cs.locateVisibleStartOfSequence(seq)); - - SequenceI seq2 = new Sequence("RefSeq2", "-------A-SD-ASD--E---"); - - cs.revealAllHiddenColumns(colsel); - cs.hideColumns(7, 17); - assertEquals(0,cs.locateVisibleStartOfSequence(seq2)); - - cs.revealAllHiddenColumns(colsel); - cs.hideColumns(3, 17); - assertEquals(0,cs.locateVisibleStartOfSequence(seq2)); - - cs.revealAllHiddenColumns(colsel); - cs.hideColumns(3, 19); - assertEquals(0,cs.locateVisibleStartOfSequence(seq2)); - - cs.revealAllHiddenColumns(colsel); - cs.hideColumns(0, 0); - assertEquals(0,cs.locateVisibleStartOfSequence(seq)); - - cs.revealAllHiddenColumns(colsel); - cs.hideColumns(0, 1); - assertEquals(1,cs.locateVisibleStartOfSequence(seq)); - - cs.revealAllHiddenColumns(colsel); - cs.hideColumns(0, 2); - assertEquals(0,cs.locateVisibleStartOfSequence(seq)); - - cs.revealAllHiddenColumns(colsel); - cs.hideColumns(1, 1); - assertEquals(2,cs.locateVisibleStartOfSequence(seq)); - - cs.revealAllHiddenColumns(colsel); - cs.hideColumns(1, 2); - assertEquals(1,cs.locateVisibleStartOfSequence(seq)); - - cs.revealAllHiddenColumns(colsel); - cs.hideColumns(1, 3); - assertEquals(1,cs.locateVisibleStartOfSequence(seq)); - - cs.revealAllHiddenColumns(colsel); - cs.hideColumns(0, 2); - cs.hideColumns(5, 6); - assertEquals(0,cs.locateVisibleStartOfSequence(seq)); - - cs.revealAllHiddenColumns(colsel); - cs.hideColumns(0, 2); - cs.hideColumns(5, 6); - cs.hideColumns(9, 10); - assertEquals(0,cs.locateVisibleStartOfSequence(seq)); - - cs.revealAllHiddenColumns(colsel); - cs.hideColumns(0, 2); - cs.hideColumns(7, 11); - assertEquals(0,cs.locateVisibleStartOfSequence(seq)); - - cs.revealAllHiddenColumns(colsel); - cs.hideColumns(2, 4); - cs.hideColumns(7, 11); - assertEquals(1,cs.locateVisibleStartOfSequence(seq)); - - cs.revealAllHiddenColumns(colsel); - cs.hideColumns(2, 4); - cs.hideColumns(7, 12); - assertEquals(1,cs.locateVisibleStartOfSequence(seq)); - - cs.revealAllHiddenColumns(colsel); - cs.hideColumns(1, 11); - assertEquals(0,cs.locateVisibleStartOfSequence(seq)); - - cs.revealAllHiddenColumns(colsel); - cs.hideColumns(0, 12); - assertEquals(0,cs.locateVisibleStartOfSequence(seq)); - - cs.revealAllHiddenColumns(colsel); - cs.hideColumns(0, 4); - cs.hideColumns(6, 12); - assertEquals(0,cs.locateVisibleStartOfSequence(seq)); - - cs.revealAllHiddenColumns(colsel); - cs.hideColumns(0, 1); - cs.hideColumns(3, 12); - assertEquals(0,cs.locateVisibleStartOfSequence(seq)); - - cs.revealAllHiddenColumns(colsel); - cs.hideColumns(3, 14); - cs.hideColumns(17, 19); - assertEquals(3,cs.locateVisibleStartOfSequence(seq2)); - - cs.revealAllHiddenColumns(colsel); - cs.hideColumns(3, 7); - cs.hideColumns(9, 14); - cs.hideColumns(17, 19); - assertEquals(9,cs.locateVisibleStartOfSequence(seq2)); - - cs.revealAllHiddenColumns(colsel); - cs.hideColumns(0, 1); - cs.hideColumns(3, 4); - cs.hideColumns(6, 8); - cs.hideColumns(10, 12); - assertEquals(6, cs.locateVisibleStartOfSequence(seq)); - - } - - @Test(groups = { "Functional" }) - public void testLocateVisibleStartPathologicals() - { - // test some pathological cases we missed - AlignmentI al = new Alignment( - new SequenceI[] - { new Sequence("refseqGaptest", "KTDVTI----------NFI-----G----L") }); - HiddenColumns cs = new HiddenColumns(); - cs.hideList(al.getSequenceAt(0).getInsertions()); - assertEquals("G", "" - + al.getSequenceAt(0).getCharAt(cs.visibleToAbsoluteColumn(9))); - - // KM: no idea what this is meant to be testing... seems to be an unfinished - // test - } @Test(groups = { "Functional" }) public void testHideColumns() diff --git a/test/jalview/datamodel/SequenceTest.java b/test/jalview/datamodel/SequenceTest.java index 72bfd86..5a14514 100644 --- a/test/jalview/datamodel/SequenceTest.java +++ b/test/jalview/datamodel/SequenceTest.java @@ -28,6 +28,7 @@ import static org.testng.AssertJUnit.assertNull; import static org.testng.AssertJUnit.assertSame; import static org.testng.AssertJUnit.assertTrue; +import jalview.analysis.AlignmentGenerator; import jalview.commands.EditCommand; import jalview.commands.EditCommand.Action; import jalview.datamodel.PDBEntry.Type; @@ -38,6 +39,7 @@ import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; +import java.util.Iterator; import java.util.List; import java.util.Vector; @@ -1895,4 +1897,162 @@ public class SequenceTest assertEquals(8, sq.getDatasetSequence().getStart()); assertEquals(9, sq.getDatasetSequence().getEnd()); } + + /** + * Test the code used to locate the reference sequence ruler origin + */ + @Test(groups = { "Functional" }) + public void testLocateVisibleStartofSequence() + { + // create random alignment + AlignmentGenerator gen = new AlignmentGenerator(false); + AlignmentI al = gen.generate(50, 20, 123, 5, 5); + + HiddenColumns cs = al.getHiddenColumns(); + ColumnSelection colsel = new ColumnSelection(); + + SequenceI seq = new Sequence("RefSeq", "-A-SD-ASD--E---"); + assertEquals(2, seq.findIndex(seq.getStart())); + + // no hidden columns + assertEquals(seq.findIndex(seq.getStart()) - 1, + seq.firstResidueOutsideIterator(cs.iterator())); + + // hidden column on gap after end of sequence - should not affect bounds + colsel.hideSelectedColumns(13, al.getHiddenColumns()); + assertEquals(seq.findIndex(seq.getStart()) - 1, + seq.firstResidueOutsideIterator(cs.iterator())); + + cs.revealAllHiddenColumns(colsel); + // hidden column on gap before beginning of sequence - should vis bounds by + // one + colsel.hideSelectedColumns(0, al.getHiddenColumns()); + assertEquals(seq.findIndex(seq.getStart()) - 2, + cs.absoluteToVisibleColumn( + seq.firstResidueOutsideIterator(cs.iterator()))); + + cs.revealAllHiddenColumns(colsel); + // hide columns around most of sequence - leave one residue remaining + cs.hideColumns(1, 3); + cs.hideColumns(6, 11); + + Iterator it = cs.getVisContigsIterator(0, 6, false); + + assertEquals("-D", seq.getSequenceStringFromIterator(it)); + // cs.getVisibleSequenceStrings(0, 5, new SequenceI[] + // { seq })[0]); + + assertEquals(4, seq.firstResidueOutsideIterator(cs.iterator())); + cs.revealAllHiddenColumns(colsel); + + // hide whole sequence - should just get location of hidden region + // containing sequence + cs.hideColumns(1, 11); + assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator())); + + cs.revealAllHiddenColumns(colsel); + cs.hideColumns(0, 15); + assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator())); + + SequenceI seq2 = new Sequence("RefSeq2", "-------A-SD-ASD--E---"); + + cs.revealAllHiddenColumns(colsel); + cs.hideColumns(7, 17); + assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator())); + + cs.revealAllHiddenColumns(colsel); + cs.hideColumns(3, 17); + assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator())); + + cs.revealAllHiddenColumns(colsel); + cs.hideColumns(3, 19); + assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator())); + + cs.revealAllHiddenColumns(colsel); + cs.hideColumns(0, 0); + assertEquals(1, seq.firstResidueOutsideIterator(cs.iterator())); + + cs.revealAllHiddenColumns(colsel); + cs.hideColumns(0, 1); + assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator())); + + cs.revealAllHiddenColumns(colsel); + cs.hideColumns(0, 2); + assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator())); + + cs.revealAllHiddenColumns(colsel); + cs.hideColumns(1, 1); + assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator())); + + cs.revealAllHiddenColumns(colsel); + cs.hideColumns(1, 2); + assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator())); + + cs.revealAllHiddenColumns(colsel); + cs.hideColumns(1, 3); + assertEquals(4, seq.firstResidueOutsideIterator(cs.iterator())); + + cs.revealAllHiddenColumns(colsel); + cs.hideColumns(0, 2); + cs.hideColumns(5, 6); + assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator())); + + cs.revealAllHiddenColumns(colsel); + cs.hideColumns(0, 2); + cs.hideColumns(5, 6); + cs.hideColumns(9, 10); + assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator())); + + cs.revealAllHiddenColumns(colsel); + cs.hideColumns(0, 2); + cs.hideColumns(7, 11); + assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator())); + + cs.revealAllHiddenColumns(colsel); + cs.hideColumns(2, 4); + cs.hideColumns(7, 11); + assertEquals(1, seq.firstResidueOutsideIterator(cs.iterator())); + + cs.revealAllHiddenColumns(colsel); + cs.hideColumns(2, 4); + cs.hideColumns(7, 12); + assertEquals(1, seq.firstResidueOutsideIterator(cs.iterator())); + + cs.revealAllHiddenColumns(colsel); + cs.hideColumns(1, 11); + assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator())); + + cs.revealAllHiddenColumns(colsel); + cs.hideColumns(0, 12); + assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator())); + + cs.revealAllHiddenColumns(colsel); + cs.hideColumns(0, 4); + cs.hideColumns(6, 12); + assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator())); + + cs.revealAllHiddenColumns(colsel); + cs.hideColumns(0, 1); + cs.hideColumns(3, 12); + assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator())); + + cs.revealAllHiddenColumns(colsel); + cs.hideColumns(3, 14); + cs.hideColumns(17, 19); + assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator())); + + cs.revealAllHiddenColumns(colsel); + cs.hideColumns(3, 7); + cs.hideColumns(9, 14); + cs.hideColumns(17, 19); + assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator())); + + cs.revealAllHiddenColumns(colsel); + cs.hideColumns(0, 1); + cs.hideColumns(3, 4); + cs.hideColumns(6, 8); + cs.hideColumns(10, 12); + assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator())); + + } }