X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=test%2Fjalview%2Fanalysis%2FAlignmentUtilsTests.java;h=02955f179d20a484af033b16a8f47a0d7cf9c552;hb=0f26441e26325c728a3e9a108ac425d4d60dde1c;hp=c19884eb471f179ee85f4177b49db9694f660de1;hpb=e29e164306d88242fe615973eeebc8c4a49d1bb0;p=jalview.git diff --git a/test/jalview/analysis/AlignmentUtilsTests.java b/test/jalview/analysis/AlignmentUtilsTests.java index c19884e..02955f1 100644 --- a/test/jalview/analysis/AlignmentUtilsTests.java +++ b/test/jalview/analysis/AlignmentUtilsTests.java @@ -22,6 +22,7 @@ package jalview.analysis; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; @@ -30,6 +31,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; +import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -88,13 +90,16 @@ public class AlignmentUtilsTests "GGGTCAGGCAGT\n"; // @formatter:on - public static Sequence ts=new Sequence("short","ASDASDASDASDASDASDASDASDASDASDASDASDASD"); + // public static Sequence ts=new + // Sequence("short","ASDASDASDASDASDASDASDASDASDASDASDASDASD"); + public static Sequence ts = new Sequence("short", + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm"); @Test - public void testExpandFlanks() + public void testExpandContext() { AlignmentI al = new Alignment(new Sequence[] {}); - for (int i=4;i<14;i+=3) + for (int i = 4; i < 14; i += 2) { SequenceI s1=ts.deriveSequence().getSubSequence(i, i+7); al.addSequence(s1); @@ -102,18 +107,132 @@ public class AlignmentUtilsTests System.out.println(new AppletFormatAdapter().formatSequences("Clustal", al, true)); for (int flnk=-1;flnk<25; flnk++) { - AlignmentI exp; - System.out.println("\nFlank size: "+flnk); - System.out.println(new AppletFormatAdapter().formatSequences("Clustal", exp=AlignmentUtils.expandContext(al, flnk), true)); - if (flnk==-1) { - for (SequenceI sq:exp.getSequences()) + AlignmentI exp = AlignmentUtils.expandContext(al, flnk); + System.out.println("\nFlank size: " + flnk); + System.out.println(new AppletFormatAdapter().formatSequences( + "Clustal", exp, true)); + if (flnk == -1) { + /* + * Full expansion to complete sequences + */ + for (SequenceI sq : exp.getSequences()) + { String ung = sq.getSequenceAsString().replaceAll("-+", ""); - assertTrue("Flanking sequence not the same as original dataset sequence.\n"+ung+"\n"+sq.getDatasetSequence().getSequenceAsString(),ung.equalsIgnoreCase(sq.getDatasetSequence().getSequenceAsString())); + final String errorMsg = "Flanking sequence not the same as original dataset sequence.\n" + + ung + + "\n" + + sq.getDatasetSequence().getSequenceAsString(); + assertTrue(errorMsg, ung.equalsIgnoreCase(sq.getDatasetSequence() + .getSequenceAsString())); + } } + else if (flnk == 24) + { + /* + * Last sequence is fully expanded, others have leading gaps to match + */ + assertTrue(exp.getSequenceAt(4).getSequenceAsString() + .startsWith("abc")); + assertTrue(exp.getSequenceAt(3).getSequenceAsString() + .startsWith("--abc")); + assertTrue(exp.getSequenceAt(2).getSequenceAsString() + .startsWith("----abc")); + assertTrue(exp.getSequenceAt(1).getSequenceAsString() + .startsWith("------abc")); + assertTrue(exp.getSequenceAt(0).getSequenceAsString() + .startsWith("--------abc")); } } - } + } + + /** + * Test that annotations are correctly adjusted by expandContext + */ + @Test + public void testExpandContext_annotation() + { + AlignmentI al = new Alignment(new Sequence[] + {}); + SequenceI ds = new Sequence("Seq1", "ABCDEFGHI"); + // subsequence DEF: + SequenceI seq1 = ds.deriveSequence().getSubSequence(3, 6); + al.addSequence(seq1); + + /* + * Annotate DEF with 4/5/6 respectively + */ + Annotation[] anns = new Annotation[] + { new Annotation(4), new Annotation(5), new Annotation(6) }; + AlignmentAnnotation ann = new AlignmentAnnotation("SS", + "secondary structure", anns); + seq1.addAlignmentAnnotation(ann); + + /* + * The annotations array should match aligned positions + */ + assertEquals(3, ann.annotations.length); + assertEquals(4, ann.annotations[0].value, 0.001); + assertEquals(5, ann.annotations[1].value, 0.001); + assertEquals(6, ann.annotations[2].value, 0.001); + + /* + * Check annotation to sequence position mappings before expanding the + * sequence; these are set up in Sequence.addAlignmentAnnotation -> + * Annotation.setSequenceRef -> createSequenceMappings + */ + assertNull(ann.getAnnotationForPosition(1)); + assertNull(ann.getAnnotationForPosition(2)); + assertNull(ann.getAnnotationForPosition(3)); + assertEquals(4, ann.getAnnotationForPosition(4).value, 0.001); + assertEquals(5, ann.getAnnotationForPosition(5).value, 0.001); + assertEquals(6, ann.getAnnotationForPosition(6).value, 0.001); + assertNull(ann.getAnnotationForPosition(7)); + assertNull(ann.getAnnotationForPosition(8)); + assertNull(ann.getAnnotationForPosition(9)); + + /* + * Expand the subsequence to the full sequence abcDEFghi + */ + AlignmentI expanded = AlignmentUtils.expandContext(al, -1); + assertEquals("abcDEFghi", expanded.getSequenceAt(0) + .getSequenceAsString()); + + /* + * Confirm the alignment and sequence have the same SS annotation, + * referencing the expanded sequence + */ + ann = expanded.getSequenceAt(0).getAnnotation()[0]; + assertSame(ann, expanded.getAlignmentAnnotation()[0]); + assertSame(expanded.getSequenceAt(0), ann.sequenceRef); + + /* + * The annotations array should have null values except for annotated + * positions + */ + assertNull(ann.annotations[0]); + assertNull(ann.annotations[1]); + assertNull(ann.annotations[2]); + assertEquals(4, ann.annotations[3].value, 0.001); + assertEquals(5, ann.annotations[4].value, 0.001); + assertEquals(6, ann.annotations[5].value, 0.001); + assertNull(ann.annotations[6]); + assertNull(ann.annotations[7]); + assertNull(ann.annotations[8]); + + /* + * sequence position mappings should be unchanged + */ + assertNull(ann.getAnnotationForPosition(1)); + assertNull(ann.getAnnotationForPosition(2)); + assertNull(ann.getAnnotationForPosition(3)); + assertEquals(4, ann.getAnnotationForPosition(4).value, 0.001); + assertEquals(5, ann.getAnnotationForPosition(5).value, 0.001); + assertEquals(6, ann.getAnnotationForPosition(6).value, 0.001); + assertNull(ann.getAnnotationForPosition(7)); + assertNull(ann.getAnnotationForPosition(8)); + assertNull(ann.getAnnotationForPosition(9)); + } /** * Test method that returns a map of lists of sequences by sequence name. @@ -1064,6 +1183,10 @@ public class AlignmentUtilsTests acf.addMap(dna1.getDatasetSequence(), pep3.getDatasetSequence(), map); mappings.add(acf); + /* + * Create the Exon alignment; also replaces the dna-to-protein mappings with + * exon-to-protein and exon-to-dna mappings + */ AlignmentI exal = AlignmentUtils.makeExonAlignment(new SequenceI[] { dna1 }, mappings); @@ -1099,5 +1222,48 @@ public class AlignmentUtilsTests assertEquals("EMBLCDS", cdsRef.getSource()); assertEquals("4", cdsRef.getVersion()); assertEquals("A12347", cdsRef.getAccessionId()); + + /* + * Verify there are mappings from each exon sequence to its protein product + * and also to its dna source + */ + Iterator newMappingsIterator = mappings.iterator(); + + // mappings for dna1 - exon1 - pep1 + AlignedCodonFrame exonMapping = newMappingsIterator.next(); + List dnaMappings = exonMapping.getMappingsForSequence(dna1); + assertEquals(1, dnaMappings.size()); + assertSame(exons.get(0).getDatasetSequence(), dnaMappings.get(0) + .getTo()); + assertEquals("G(1) in CDS should map to G(4) in DNA", 4, dnaMappings + .get(0).getMap().getToPosition(1)); + List peptideMappings = exonMapping + .getMappingsForSequence(pep1); + assertEquals(1, peptideMappings.size()); + assertSame(pep1.getDatasetSequence(), peptideMappings.get(0).getTo()); + + // mappings for dna1 - exon2 - pep2 + exonMapping = newMappingsIterator.next(); + dnaMappings = exonMapping.getMappingsForSequence(dna1); + assertEquals(1, dnaMappings.size()); + assertSame(exons.get(1).getDatasetSequence(), dnaMappings.get(0) + .getTo()); + assertEquals("c(4) in CDS should map to c(7) in DNA", 7, dnaMappings + .get(0).getMap().getToPosition(4)); + peptideMappings = exonMapping.getMappingsForSequence(pep2); + assertEquals(1, peptideMappings.size()); + assertSame(pep2.getDatasetSequence(), peptideMappings.get(0).getTo()); + + // mappings for dna1 - exon3 - pep3 + exonMapping = newMappingsIterator.next(); + dnaMappings = exonMapping.getMappingsForSequence(dna1); + assertEquals(1, dnaMappings.size()); + assertSame(exons.get(2).getDatasetSequence(), dnaMappings.get(0) + .getTo()); + assertEquals("T(4) in CDS should map to T(10) in DNA", 10, dnaMappings + .get(0).getMap().getToPosition(4)); + peptideMappings = exonMapping.getMappingsForSequence(pep3); + assertEquals(1, peptideMappings.size()); + assertSame(pep3.getDatasetSequence(), peptideMappings.get(0).getTo()); } }