package jalview.datamodel; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import java.util.List; import org.junit.Before; import org.junit.Test; public class SequenceTest { SequenceI seq; @Before public void setUp() { seq = new Sequence("FER1", "AKPNGVL"); } @Test public void testInsertGapsAndGapmaps() { SequenceI aseq = seq.deriveSequence(); aseq.insertCharAt(2, 3, '-'); aseq.insertCharAt(6, 3, '-'); assertEquals("Gap insertions not correct", "AK---P---NGVL", aseq.getSequenceAsString()); List gapInt = aseq.getInsertions(); assertEquals("Gap interval 1 start wrong", 2, gapInt.get(0)[0]); assertEquals("Gap interval 1 end wrong", 4, gapInt.get(0)[1]); assertEquals("Gap interval 2 start wrong", 6, gapInt.get(1)[0]); assertEquals("Gap interval 2 end wrong", 8, gapInt.get(1)[1]); } @Test public void testGetAnnotation() { // initial state returns null not an empty array assertNull(seq.getAnnotation()); AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1", 1f); AlignmentAnnotation[] anns = seq.getAnnotation(); assertEquals(1, anns.length); assertSame(ann, anns[0]); // removing all annotations reverts array to null seq.removeAlignmentAnnotation(ann); assertNull(seq.getAnnotation()); } @Test public void testGetAnnotation_forLabel() { AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1", 1f); AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2", 1f); AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3", 1f); AlignmentAnnotation[] anns = seq.getAnnotation("label1"); assertEquals(2, anns.length); assertSame(ann1, anns[0]); assertSame(ann3, anns[1]); } private AlignmentAnnotation addAnnotation(String label, String description, String calcId, float value) { final AlignmentAnnotation annotation = new AlignmentAnnotation(label, description, value); annotation.setCalcId(calcId); seq.addAlignmentAnnotation(annotation); return annotation; } @Test public void testGetAlignmentAnnotations_forCalcIdAndLabel() { AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1", 1f); AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2", 1f); AlignmentAnnotation ann3 = addAnnotation("label2", "desc3", "calcId3", 1f); AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2", 1f); AlignmentAnnotation ann5 = addAnnotation("label5", "desc3", null, 1f); AlignmentAnnotation ann6 = addAnnotation(null, "desc3", "calcId3", 1f); List anns = seq.getAlignmentAnnotations("calcId2", "label2"); assertEquals(2, anns.size()); assertSame(ann2, anns.get(0)); assertSame(ann4, anns.get(1)); assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty()); assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty()); assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty()); assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty()); assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty()); } /** * Tests for addAlignmentAnnotation. Note this method has the side-effect of * setting the sequenceRef on the annotation. Adding the same annotation twice * should be ignored. */ @Test public void testAddAlignmentAnnotation() { assertNull(seq.getAnnotation()); final AlignmentAnnotation annotation = new AlignmentAnnotation("a", "b", 2d); assertNull(annotation.sequenceRef); seq.addAlignmentAnnotation(annotation); assertSame(seq, annotation.sequenceRef); AlignmentAnnotation[] anns = seq.getAnnotation(); assertEquals(1, anns.length); assertSame(annotation, anns[0]); // re-adding does nothing seq.addAlignmentAnnotation(annotation); anns = seq.getAnnotation(); assertEquals(1, anns.length); assertSame(annotation, anns[0]); // an identical but different annotation can be added final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a", "b", 2d); seq.addAlignmentAnnotation(annotation2); anns = seq.getAnnotation(); assertEquals(2, anns.length); assertSame(annotation, anns[0]); assertSame(annotation2, anns[1]); } }