From 62086b8b7a0b9436c445a94bfab5eff253adb6d5 Mon Sep 17 00:00:00 2001 From: kiramt Date: Tue, 16 Jan 2018 09:41:34 +0000 Subject: [PATCH] JAL-2759 Moved alignment annotation code out of HiddenColumns --- src/jalview/datamodel/AlignmentAnnotation.java | 129 ++++++++++++++++++-- src/jalview/datamodel/HiddenColumns.java | 113 ----------------- src/jalview/io/FormatAdapter.java | 6 +- src/jalview/viewmodel/AlignmentViewport.java | 6 +- .../datamodel/AlignmentAnnotationTests.java | 90 +++++++++++++- test/jalview/datamodel/HiddenColumnsTest.java | 95 -------------- 6 files changed, 215 insertions(+), 224 deletions(-) diff --git a/src/jalview/datamodel/AlignmentAnnotation.java b/src/jalview/datamodel/AlignmentAnnotation.java index 09facbf..6c33c1b 100755 --- a/src/jalview/datamodel/AlignmentAnnotation.java +++ b/src/jalview/datamodel/AlignmentAnnotation.java @@ -24,6 +24,7 @@ import jalview.analysis.Rna; import jalview.analysis.SecStrConsensus.SimpleBP; import jalview.analysis.WUSSParseException; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -665,7 +666,7 @@ public class AlignmentAnnotation this.calcId = annotation.calcId; if (annotation.properties != null) { - properties = new HashMap(); + properties = new HashMap<>(); for (Map.Entry val : annotation.properties.entrySet()) { properties.put(val.getKey(), val.getValue()); @@ -701,7 +702,7 @@ public class AlignmentAnnotation if (annotation.sequenceMapping != null) { Integer p = null; - sequenceMapping = new HashMap(); + sequenceMapping = new HashMap<>(); Iterator pos = annotation.sequenceMapping.keySet() .iterator(); while (pos.hasNext()) @@ -781,7 +782,7 @@ public class AlignmentAnnotation int epos = sequenceRef.findPosition(endRes); if (sequenceMapping != null) { - Map newmapping = new HashMap(); + Map newmapping = new HashMap<>(); Iterator e = sequenceMapping.keySet().iterator(); while (e.hasNext()) { @@ -908,7 +909,7 @@ public class AlignmentAnnotation { return; } - sequenceMapping = new HashMap(); + sequenceMapping = new HashMap<>(); int seqPos; @@ -1123,7 +1124,7 @@ public class AlignmentAnnotation { return; } - hidden.makeVisibleAnnotation(this); + makeVisibleAnnotation(hidden); } public void setPadGaps(boolean padgaps, char gapchar) @@ -1189,7 +1190,7 @@ public class AlignmentAnnotation /** * properties associated with the calcId */ - protected Map properties = new HashMap(); + protected Map properties = new HashMap<>(); /** * base colour for line graphs. If null, will be set automatically by @@ -1235,7 +1236,7 @@ public class AlignmentAnnotation : false; // TODO build a better annotation element map and get rid of annotations[] - Map mapForsq = new HashMap(); + Map mapForsq = new HashMap<>(); if (sequenceMapping != null) { if (sp2sq != null) @@ -1288,7 +1289,7 @@ public class AlignmentAnnotation if (mapping != null) { Map old = sequenceMapping; - Map remap = new HashMap(); + Map remap = new HashMap<>(); int index = -1; for (int mp[] : mapping.values()) { @@ -1346,7 +1347,7 @@ public class AlignmentAnnotation { if (properties == null) { - properties = new HashMap(); + properties = new HashMap<>(); } properties.put(property, value); } @@ -1471,4 +1472,114 @@ public class AlignmentAnnotation { return graphMin < graphMax; } + + /** + * delete any columns in alignmentAnnotation that are hidden (including + * sequence associated annotation). + * + * @param hiddenColumns + * the set of hidden columns + */ + public void makeVisibleAnnotation(HiddenColumns hiddenColumns) + { + if (annotations != null) + { + makeVisibleAnnotation(0, annotations.length, hiddenColumns); + } + } + + /** + * delete any columns in alignmentAnnotation that are hidden (including + * sequence associated annotation). + * + * @param start + * remove any annotation to the right of this column + * @param end + * remove any annotation to the left of this column + * @param hiddenColumns + * the set of hidden columns + */ + public void makeVisibleAnnotation(int start, int end, + HiddenColumns hiddenColumns) + { + if (annotations != null) + { + if (hiddenColumns.hasHiddenColumns()) + { + removeHiddenAnnotation(start, end, hiddenColumns); + } + else + { + restrict(start, end); + } + } + } + + /** + * The actual implementation of deleting hidden annotation columns + * + * @param start + * remove any annotation to the right of this column + * @param end + * remove any annotation to the left of this column + * @param hiddenColumns + * the set of hidden columns + */ + private void removeHiddenAnnotation(int start, int end, + HiddenColumns hiddenColumns) + { + // mangle the alignmentAnnotation annotation array + ArrayList annels = new ArrayList<>(); + Annotation[] els = null; + + int w = 0; + + Iterator blocks = hiddenColumns.getVisContigsIterator(start, + end + 1, false); + + 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 + { + if (annotationLength + block[0] <= annotations.length) + { + // copy just the visible segment of the annotation row + copylength = annotationLength; + } + else + { + // copy to the end of the annotation row + copylength = annotations.length - block[0]; + } + } + + els = new Annotation[annotationLength]; + annels.add(els); + System.arraycopy(annotations, block[0], els, 0, copylength); + w += annotationLength; + } + + if (w != 0) + { + annotations = new Annotation[w]; + + w = 0; + for (Annotation[] chnk : annels) + { + System.arraycopy(chnk, 0, annotations, w, chnk.length); + w += chnk.length; + } + } + } + } diff --git a/src/jalview/datamodel/HiddenColumns.java b/src/jalview/datamodel/HiddenColumns.java index c666a41..49f82f5 100644 --- a/src/jalview/datamodel/HiddenColumns.java +++ b/src/jalview/datamodel/HiddenColumns.java @@ -1058,119 +1058,6 @@ public class HiddenColumns } } - /** - * delete any columns in alignmentAnnotation that are hidden (including - * sequence associated annotation). - * - * @param alignmentAnnotation - */ - public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation) - { - if (alignmentAnnotation != null - && alignmentAnnotation.annotations != null) - { - makeVisibleAnnotation(0, alignmentAnnotation.annotations.length, - alignmentAnnotation); - } - } - - /** - * delete any columns in alignmentAnnotation that are hidden (including - * sequence associated annotation). - * - * @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 - */ - public void makeVisibleAnnotation(int start, int end, - AlignmentAnnotation alignmentAnnotation) - { - try - { - LOCK.readLock().lock(); - - int startFrom = start; - int endAt = end; - - if (alignmentAnnotation != null - && alignmentAnnotation.annotations != null) - { - if (!hiddenColumns.isEmpty()) - { - removeHiddenAnnotation(startFrom, endAt, alignmentAnnotation); - } - else - { - alignmentAnnotation.restrict(startFrom, endAt); - } - } - } finally - { - LOCK.readLock().unlock(); - } - } - - private void removeHiddenAnnotation(int start, int end, - AlignmentAnnotation alignmentAnnotation) - { - // 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()) - { - 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 - { - if (annotationLength + block[0] <= alignmentAnnotation.annotations.length) - { - // copy just the visible segment of the annotation row - copylength = annotationLength; - } - else - { - // 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; - } - } - } /** * diff --git a/src/jalview/io/FormatAdapter.java b/src/jalview/io/FormatAdapter.java index 3027ab1..6a28b1d 100755 --- a/src/jalview/io/FormatAdapter.java +++ b/src/jalview/io/FormatAdapter.java @@ -211,12 +211,12 @@ public class FormatAdapter extends AppletFormatAdapter AlignmentAnnotation na = new AlignmentAnnotation(ala[i]); if (selgp != null) { - hidden.makeVisibleAnnotation(selgp.getStartRes(), - selgp.getEndRes(), na); + na.makeVisibleAnnotation(selgp.getStartRes(), selgp.getEndRes(), + hidden); } else { - hidden.makeVisibleAnnotation(na); + na.makeVisibleAnnotation(hidden); } alv.addAnnotation(na); } diff --git a/src/jalview/viewmodel/AlignmentViewport.java b/src/jalview/viewmodel/AlignmentViewport.java index 946c8e6..44dfe8d 100644 --- a/src/jalview/viewmodel/AlignmentViewport.java +++ b/src/jalview/viewmodel/AlignmentViewport.java @@ -1809,13 +1809,13 @@ public abstract class AlignmentViewport AlignmentAnnotation clone = new AlignmentAnnotation(annot); if (selectedOnly && selectionGroup != null) { - alignment.getHiddenColumns().makeVisibleAnnotation( + clone.makeVisibleAnnotation( selectionGroup.getStartRes(), selectionGroup.getEndRes(), - clone); + alignment.getHiddenColumns()); } else { - alignment.getHiddenColumns().makeVisibleAnnotation(clone); + clone.makeVisibleAnnotation(alignment.getHiddenColumns()); } ala.add(clone); } diff --git a/test/jalview/datamodel/AlignmentAnnotationTests.java b/test/jalview/datamodel/AlignmentAnnotationTests.java index e47e9d6..19a725e 100644 --- a/test/jalview/datamodel/AlignmentAnnotationTests.java +++ b/test/jalview/datamodel/AlignmentAnnotationTests.java @@ -20,8 +20,8 @@ */ package jalview.datamodel; +import static org.testng.Assert.assertNull; import static org.testng.AssertJUnit.assertEquals; -import static org.testng.AssertJUnit.assertNull; import jalview.analysis.AlignSeq; import jalview.gui.JvOptionPane; @@ -335,4 +335,92 @@ public class AlignmentAnnotationTests Assert.assertTrue(ann.isQuantitative(), "Mixed 'E' annotation set should be quantitative."); } + + @Test(groups = "Functional") + public void testMakeVisibleAnnotation() + { + HiddenColumns h = new HiddenColumns(); + Annotation[] anns = new Annotation[] { null, null, new Annotation(1), + new Annotation(2), new Annotation(3), null, null, new Annotation(4), + new Annotation(5), new Annotation(6), new Annotation(7), + new Annotation(8) }; + AlignmentAnnotation ann = new AlignmentAnnotation("an", "some an", + anns); + + // null annotations + AlignmentAnnotation emptyann = new AlignmentAnnotation("an", "some ann", + null); + emptyann.makeVisibleAnnotation(h); + assertNull(emptyann.annotations); + + emptyann.makeVisibleAnnotation(3, 4, h); + assertNull(emptyann.annotations); + + // without bounds, does everything + ann.makeVisibleAnnotation(h); + assertEquals(12, ann.annotations.length); + assertNull(ann.annotations[0]); + assertNull(ann.annotations[1]); + assertEquals(1.0f, ann.annotations[2].value); + assertEquals(2.0f, ann.annotations[3].value); + assertEquals(3.0f, ann.annotations[4].value); + assertNull(ann.annotations[5]); + assertNull(ann.annotations[6]); + assertEquals(4.0f, ann.annotations[7].value); + assertEquals(5.0f, ann.annotations[8].value); + assertEquals(6.0f, ann.annotations[9].value); + assertEquals(7.0f, ann.annotations[10].value); + assertEquals(8.0f, ann.annotations[11].value); + + // without hidden cols, just truncates + ann.makeVisibleAnnotation(3, 5, h); + assertEquals(3, ann.annotations.length); + assertEquals(2.0f, ann.annotations[0].value); + assertEquals(3.0f, ann.annotations[1].value); + assertNull(ann.annotations[2]); + + anns = new Annotation[] { null, null, new Annotation(1), + new Annotation(2), new Annotation(3), null, null, new Annotation(4), + new Annotation(5), new Annotation(6), new Annotation(7), + new Annotation(8) }; + ann = new AlignmentAnnotation("an", "some an", anns); + h.hideColumns(4, 7); + ann.makeVisibleAnnotation(1, 9, h); + assertEquals(5, ann.annotations.length); + assertNull(ann.annotations[0]); + assertEquals(1.0f, ann.annotations[1].value); + assertEquals(2.0f, ann.annotations[2].value); + assertEquals(5.0f, ann.annotations[3].value); + assertEquals(6.0f, ann.annotations[4].value); + + anns = new Annotation[] { null, null, new Annotation(1), + new Annotation(2), new Annotation(3), null, null, new Annotation(4), + new Annotation(5), new Annotation(6), new Annotation(7), + new Annotation(8) }; + ann = new AlignmentAnnotation("an", "some an", anns); + h.hideColumns(1, 2); + ann.makeVisibleAnnotation(1, 9, h); + assertEquals(3, ann.annotations.length); + assertEquals(2.0f, ann.annotations[0].value); + assertEquals(5.0f, ann.annotations[1].value); + assertEquals(6.0f, ann.annotations[2].value); + + anns = new Annotation[] { null, null, new Annotation(1), + new Annotation(2), new Annotation(3), null, null, new Annotation(4), + new Annotation(5), new Annotation(6), new Annotation(7), + new Annotation(8), new Annotation(9), new Annotation(10), + new Annotation(11), new Annotation(12), new Annotation(13), + new Annotation(14), new Annotation(15) }; + ann = new AlignmentAnnotation("an", "some an", anns); + h = new HiddenColumns(); + h.hideColumns(5, 18); + h.hideColumns(20, 21); + ann.makeVisibleAnnotation(1, 21, h); + assertEquals(5, ann.annotations.length); + assertEquals(1.0f, ann.annotations[1].value); + assertEquals(2.0f, ann.annotations[2].value); + assertEquals(3.0f, ann.annotations[3].value); + assertNull(ann.annotations[0]); + assertNull(ann.annotations[4]); + } } diff --git a/test/jalview/datamodel/HiddenColumnsTest.java b/test/jalview/datamodel/HiddenColumnsTest.java index e6c9b32..dbde943 100644 --- a/test/jalview/datamodel/HiddenColumnsTest.java +++ b/test/jalview/datamodel/HiddenColumnsTest.java @@ -1172,101 +1172,6 @@ public class HiddenColumnsTest } @Test(groups = "Functional") - public void testMakeVisibleAnnotation() - { - HiddenColumns h = new HiddenColumns(); - Annotation[] anns = new Annotation[] { null, null, new Annotation(1), - new Annotation(2), new Annotation(3), null, null, new Annotation(4), - new Annotation(5), new Annotation(6), new Annotation(7), - new Annotation(8) }; - AlignmentAnnotation ann = new AlignmentAnnotation("an", "some an", - anns); - - // null annotation - AlignmentAnnotation nullann = null; - h.makeVisibleAnnotation(nullann); - assertNull(nullann); - - h.makeVisibleAnnotation(1, 2, nullann); - assertNull(nullann); - - // null annotations - AlignmentAnnotation emptyann = new AlignmentAnnotation("an", "some ann", null); - h.makeVisibleAnnotation(emptyann); - assertNull(emptyann.annotations); - - h.makeVisibleAnnotation(3, 4, emptyann); - assertNull(emptyann.annotations); - - // without bounds, does everything - h.makeVisibleAnnotation(ann); - assertEquals(12, ann.annotations.length); - assertNull(ann.annotations[0]); - assertNull(ann.annotations[1]); - assertEquals(1.0f, ann.annotations[2].value); - assertEquals(2.0f, ann.annotations[3].value); - assertEquals(3.0f, ann.annotations[4].value); - assertNull(ann.annotations[5]); - assertNull(ann.annotations[6]); - assertEquals(4.0f, ann.annotations[7].value); - assertEquals(5.0f, ann.annotations[8].value); - assertEquals(6.0f, ann.annotations[9].value); - assertEquals(7.0f, ann.annotations[10].value); - assertEquals(8.0f, ann.annotations[11].value); - - // without hidden cols, just truncates - h.makeVisibleAnnotation(3, 5, ann); - assertEquals(3, ann.annotations.length); - assertEquals(2.0f, ann.annotations[0].value); - assertEquals(3.0f, ann.annotations[1].value); - assertNull(ann.annotations[2]); - - anns = new Annotation[] { null, null, new Annotation(1), - new Annotation(2), new Annotation(3), null, null, new Annotation(4), - new Annotation(5), new Annotation(6), new Annotation(7), - new Annotation(8) }; - ann = new AlignmentAnnotation("an", "some an", anns); - h.hideColumns(4, 7); - h.makeVisibleAnnotation(1, 9, ann); - assertEquals(5, ann.annotations.length); - assertNull(ann.annotations[0]); - assertEquals(1.0f, ann.annotations[1].value); - assertEquals(2.0f, ann.annotations[2].value); - assertEquals(5.0f, ann.annotations[3].value); - assertEquals(6.0f, ann.annotations[4].value); - - anns = new Annotation[] { null, null, new Annotation(1), - new Annotation(2), new Annotation(3), null, null, new Annotation(4), - new Annotation(5), new Annotation(6), new Annotation(7), - new Annotation(8) }; - ann = new AlignmentAnnotation("an", "some an", anns); - h.hideColumns(1, 2); - h.makeVisibleAnnotation(1, 9, ann); - assertEquals(3, ann.annotations.length); - assertEquals(2.0f, ann.annotations[0].value); - assertEquals(5.0f, ann.annotations[1].value); - assertEquals(6.0f, ann.annotations[2].value); - - anns = new Annotation[] { null, null, new Annotation(1), - new Annotation(2), new Annotation(3), null, null, new Annotation(4), - new Annotation(5), new Annotation(6), new Annotation(7), - new Annotation(8), new Annotation(9), new Annotation(10), - new Annotation(11), new Annotation(12), new Annotation(13), - new Annotation(14), new Annotation(15) }; - ann = new AlignmentAnnotation("an", "some an", anns); - h = new HiddenColumns(); - h.hideColumns(5, 18); - h.hideColumns(20, 21); - h.makeVisibleAnnotation(1, 21, ann); - assertEquals(5, ann.annotations.length); - assertEquals(1.0f, ann.annotations[1].value); - assertEquals(2.0f, ann.annotations[2].value); - assertEquals(3.0f, ann.annotations[3].value); - assertNull(ann.annotations[0]); - assertNull(ann.annotations[4]); - } - - @Test(groups = "Functional") public void testSubtractVisibleColumns() { HiddenColumns h = new HiddenColumns(); -- 1.7.10.2