From: gmungoc Date: Fri, 2 Dec 2016 14:02:32 +0000 (+0000) Subject: Unit tests added, minor refactoring X-Git-Tag: Release_2_10_3b1~336^2~2^2~1^2~2^2~11 X-Git-Url: http://source.jalview.org/gitweb/?p=jalview.git;a=commitdiff_plain;h=2963965a346b42b6b9a2d0878ee076220be7077b Unit tests added, minor refactoring --- diff --git a/src/jalview/gui/AnnotationColourChooser.java b/src/jalview/gui/AnnotationColourChooser.java index 4daf6b2..b2c4255 100644 --- a/src/jalview/gui/AnnotationColourChooser.java +++ b/src/jalview/gui/AnnotationColourChooser.java @@ -32,6 +32,7 @@ import java.util.Hashtable; import java.util.Vector; import jalview.bin.Cache; +import jalview.datamodel.GraphLine; import jalview.datamodel.SequenceGroup; import jalview.schemes.AnnotationColourGradient; import jalview.schemes.ColourSchemeI; @@ -458,7 +459,7 @@ public class AnnotationColourChooser extends AnnotationRowFilter { getCurrentAnnotation() .setThreshold( - new jalview.datamodel.GraphLine( + new GraphLine( (getCurrentAnnotation().graphMax - getCurrentAnnotation().graphMin) / 2f, "Threshold", Color.black)); } diff --git a/test/jalview/gui/AnnotationRowFilterTest.java b/test/jalview/gui/AnnotationRowFilterTest.java new file mode 100644 index 0000000..2785639 --- /dev/null +++ b/test/jalview/gui/AnnotationRowFilterTest.java @@ -0,0 +1,119 @@ +package jalview.gui; + +import static org.testng.Assert.assertEquals; + +import java.util.Vector; + +import jalview.bin.Cache; +import jalview.datamodel.AlignmentAnnotation; +import jalview.datamodel.AlignmentI; +import jalview.datamodel.SequenceI; +import jalview.io.DataSourceType; +import jalview.io.FileLoader; + +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +/** + * Tests for methods of base class of annotation column or colour chooser + */ +public class AnnotationRowFilterTest +{ + AlignFrame af; + + private AnnotationRowFilter testee; + + @BeforeClass(alwaysRun = true) + public void setUp() + { + Cache.loadProperties("test/jalview/io/testProps.jvprops"); + Cache.applicationProperties.setProperty("SHOW_ANNOTATIONS", + Boolean.TRUE.toString()); + af = new FileLoader().LoadFileWaitTillLoaded("examples/uniref50.fa", + DataSourceType.FILE); + testee = new AnnotationRowFilter(af.viewport, af.alignPanel) + { + @Override + public void valueChanged(boolean updateAllAnnotation) + { + } + + @Override + public void updateView() + { + } + + @Override + public void reset() + { + } + }; + } + + /** + * Test the method that builds the drop-down list of annotations to choose + * from for colour by annotation or select columns by annotation + */ + @Test(groups = "Functional") + public void testGetAnnotationItems() + { + AlignmentI al = af.getViewport().getAlignment(); + SequenceI seq1 = al.findSequenceMatch("FER_CAPAA")[0]; + SequenceI seq2 = al.findSequenceMatch("FER_BRANA")[0]; + + AlignmentAnnotation ann1 = new AlignmentAnnotation("ann1Label", "ann1", + null); + al.addAnnotation(ann1); + AlignmentAnnotation ann2 = new AlignmentAnnotation("Significance", + "ann2", null); + al.addAnnotation(ann2); + /* + * a second Significance alignment annotation + */ + AlignmentAnnotation ann2a = new AlignmentAnnotation("Significance", + "ann2", null); + al.addAnnotation(ann2a); + + AlignmentAnnotation ann3 = new AlignmentAnnotation("Jronn", "Jronn", + null); + ann3.setSequenceRef(seq1); + al.addAnnotation(ann3); + AlignmentAnnotation ann4 = new AlignmentAnnotation("Jronn", "Jronn", + null); + ann4.setSequenceRef(seq2); + al.addAnnotation(ann4); + AlignmentAnnotation ann5 = new AlignmentAnnotation("Jnet", "Jnet", null); + ann5.setSequenceRef(seq2); + al.addAnnotation(ann5); + /* + * a second Jnet annotation for FER_BRANA + */ + AlignmentAnnotation ann6 = new AlignmentAnnotation("Jnet", "Jnet", null); + ann6.setSequenceRef(seq2); + al.addAnnotation(ann6); + + /* + * drop-down items with 'Per-sequence only' not checked + */ + Vector items = testee.getAnnotationItems(false); + assertEquals( + items.toString(), + "[Conservation, Quality, Consensus, ann1Label, Significance, Significance_1, Jronn_FER_CAPAA, Jronn_FER_BRANA, Jnet_FER_BRANA, Jnet_FER_BRANA_2]"); + assertEquals(testee.getAnnotationMenuLabel(ann1), "ann1Label"); + assertEquals(testee.getAnnotationMenuLabel(ann2), "Significance"); + assertEquals(testee.getAnnotationMenuLabel(ann2a), "Significance_1"); + assertEquals(testee.getAnnotationMenuLabel(ann3), "Jronn_FER_CAPAA"); + assertEquals(testee.getAnnotationMenuLabel(ann4), "Jronn_FER_BRANA"); + assertEquals(testee.getAnnotationMenuLabel(ann5), "Jnet_FER_BRANA"); + assertEquals(testee.getAnnotationMenuLabel(ann6), "Jnet_FER_BRANA_2"); + + /* + * drop-down items with 'Per-sequence only' checked + */ + items = testee.getAnnotationItems(true); + assertEquals(items.toString(), "[Jronn, Jnet]"); + // the first annotation of the type is associated with the menu item + assertEquals(testee.getAnnotationMenuLabel(ann3), "Jronn"); + assertEquals(testee.getAnnotationMenuLabel(ann5), "Jnet"); + } +} diff --git a/test/jalview/schemes/AnnotationColourGradientTest.java b/test/jalview/schemes/AnnotationColourGradientTest.java index c9ceebb..42e3eb4 100644 --- a/test/jalview/schemes/AnnotationColourGradientTest.java +++ b/test/jalview/schemes/AnnotationColourGradientTest.java @@ -1,12 +1,168 @@ package jalview.schemes; +import static org.testng.Assert.assertEquals; + +import java.awt.Color; + +import jalview.datamodel.AlignmentAnnotation; +import jalview.datamodel.Annotation; +import jalview.datamodel.GraphLine; + +import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class AnnotationColourGradientTest { + final static int WIDTH = 11; + + final static int THRESHOLD = 5; + + private AlignmentAnnotation ann; + + Color minColour = new Color(50, 200, 150); + + Color maxColour = new Color(150, 100, 250); + + @BeforeClass + public void setUp() + { + Annotation[] anns = new Annotation[WIDTH]; + /* + * set annotations with values 0-10 + */ + for (int col = 0; col < WIDTH; col++) + { + anns[col] = new Annotation("a", "a", 'a', col); + } + + /* + * AlignmentAnnotation constructor works out min-max range + */ + ann = new AlignmentAnnotation("", "", anns); + ann.setThreshold(new GraphLine(THRESHOLD, "", Color.RED)); + } + @Test(groups = "Functional") - public void testShadeCalculation() + public void testShadeCalculation_noThreshold() { + AnnotationColourGradient testee = new AnnotationColourGradient(ann, + minColour, maxColour, AnnotationColourGradient.NO_THRESHOLD); + for (int col = 0; col < WIDTH; col++) + { + Color result = testee.shadeCalculation(ann, col); + /* + * column is n/10 of the way from minCol to maxCol + */ + Color expected = new Color(50 + 10 * col, 200 - 10 * col, + 150 + 10 * col); + assertEquals(result, expected, "for column " + col); + } + } + + /** + * Test the 'colour above threshold' case + */ + @Test(groups = "Functional") + public void testShadeCalculation_aboveThreshold() + { + AnnotationColourGradient testee = new AnnotationColourGradient(ann, + minColour, maxColour, AnnotationColourGradient.ABOVE_THRESHOLD); + for (int col = 0; col < WIDTH; col++) + { + Color result = testee.shadeCalculation(ann, col); + /* + * colour is derived regardless of the threshold value + * (the renderer that will suppress colouring if + * above/below threshold) + */ + Color expected = new Color(50 + 10 * col, 200 - 10 * col, + 150 + 10 * col); + assertEquals(result, expected, "for column " + col); + } + + /* + * now make 6-10 the span of the colour range + * (annotation value == column number in this test) + */ + testee.setThresholdIsMinMax(true); + for (int col = 0; col < THRESHOLD; col++) + { + /* + * colours below the threshold are computed as before + */ + Color expected = new Color(50 + 10 * col, 200 - 10 * col, + 150 + 10 * col); + Color result = testee.shadeCalculation(ann, col); + assertEquals(result, expected, "for column " + col); + } + for (int col = THRESHOLD; col < WIDTH; col++) + { + /* + * colours for values >= threshold are graduated + * range is 6-10 so steps of 100/5 = 20 + */ + int factor = col - THRESHOLD; + Color expected = new Color(50 + 20 * factor, 200 - 20 * factor, + 150 + 20 * factor); + Color result = testee.shadeCalculation(ann, col); + assertEquals(result, expected, "for column " + col); + } + } + + /** + * Test the 'colour below threshold' case + */ + @Test(groups = "Functional") + public void testShadeCalculation_belowThreshold() + { + /* + * change threshold to 5 so we have an easy calculation of + * the min-max range 0-5 + */ + int threshold = 5; + ann.setThreshold(new GraphLine(threshold, "", Color.RED)); + AnnotationColourGradient testee = new AnnotationColourGradient(ann, + minColour, maxColour, AnnotationColourGradient.BELOW_THRESHOLD); + + for (int col = 0; col < WIDTH; col++) + { + Color result = testee.shadeCalculation(ann, col); + /* + * colour is derived regardless of the threshold value + * (the renderer that will suppress colouring if + * above/below threshold) + */ + Color expected = new Color(50 + 10 * col, 200 - 10 * col, + 150 + 10 * col); + assertEquals(result, expected, "for column " + col); + } + + /* + * now make 0-5 the span of the colour range + * (annotation value == column number in this test) + */ + testee.setThresholdIsMinMax(true); + for (int col = threshold + 1; col < WIDTH; col++) + { + /* + * colours above the threshold are computed as before + */ + Color expected = new Color(50 + 10 * col, 200 - 10 * col, + 150 + 10 * col); + Color result = testee.shadeCalculation(ann, col); + assertEquals(result, expected, "for column " + col); + } + for (int col = 0; col <= threshold; col++) + { + /* + * colours for values <= threshold are graduated + * range is 0-5 so steps of 100/5 = 20 + */ + Color expected = new Color(50 + 20 * col, 200 - 20 * col, + 150 + 20 * col); + Color result = testee.shadeCalculation(ann, col); + assertEquals(result, expected, "for column " + col); + } } }