1 package jalview.datamodel;
3 import static org.junit.Assert.assertEquals;
4 import jalview.analysis.AlignSeq;
5 import jalview.io.AppletFormatAdapter;
9 public class AlignmentAnnotationTests
12 public void testCopyConstructor()
14 SequenceI sq = new Sequence("Foo", "ARAARARARAWEAWEAWRAWEAWE");
16 AlignmentAnnotation alc, alo = sq.getAnnotation()[0];
17 alc = new AlignmentAnnotation(alo);
18 for (String key : alo.getProperties())
20 assertEquals("Property mismatch", alo.getProperty(key),
21 alc.getProperty(key));
25 * create some dummy annotation derived from the sequence
29 public static void createAnnotation(SequenceI sq)
31 Annotation[] al = new Annotation[sq.getLength()];
32 for (int i = 0; i < al.length; i++)
34 al[i] = new Annotation(new Annotation("" + sq.getCharAt(i), "",
35 (char) 0, sq.findPosition(i)));
37 AlignmentAnnotation alan = new AlignmentAnnotation("For "
38 + sq.getName(), "Fake alignment annot", al);
39 // create a sequence mapping for the annotation vector in its current state
40 alan.createSequenceMapping(sq, sq.getStart(), false);
41 alan.setProperty("CreatedBy", "createAnnotation");
42 sq.addAlignmentAnnotation(alan);
46 * use this to test annotation derived from method above as it is transferred
47 * across different sequences derived from same dataset coordinate frame
51 public static void testAnnotTransfer(AlignmentAnnotation ala)
54 "Failed - need annotation created by createAnnotation method",
55 ala.description, "Fake alignment annot");
56 ala.adjustForAlignment();
57 for (int p = 0; p < ala.annotations.length; p++)
59 if (ala.annotations[p] != null)
61 assertEquals("Mismatch at position " + p
62 + " between annotation position value and sequence"
63 + ala.annotations[p], (int) ala.annotations[p].value,
64 ala.sequenceRef.findPosition(p));
70 * Tests the liftOver method and also exercises the functions for remapping
71 * annotation across different reference sequences. Here, the test is between
72 * different dataset frames (annotation transferred by mapping between
76 public void testLiftOver()
78 SequenceI sqFrom = new Sequence("fromLong", "QQQCDEWGH");
80 sqFrom.setEnd(sqFrom.findPosition(sqFrom.getLength() - 1));
81 SequenceI sqTo = new Sequence("toShort", "RCDEW");
83 sqTo.setEnd(sqTo.findPosition(sqTo.getLength() - 1));
84 createAnnotation(sqTo);
85 AlignmentAnnotation origTo = sqTo.getAnnotation()[0];
86 createAnnotation(sqFrom);
87 AlignmentAnnotation origFrom = sqFrom.getAnnotation()[0];
88 AlignSeq align = AlignSeq.doGlobalNWAlignment(sqFrom, sqTo,
90 SequenceI alSeq1 = new Sequence(sqFrom.getName(), align.getAStr1());
91 alSeq1.setStart(sqFrom.getStart() + align.getSeq1Start() - 1);
92 alSeq1.setEnd(sqFrom.getStart() + align.getSeq1End() - 1);
93 alSeq1.setDatasetSequence(sqFrom);
94 SequenceI alSeq2 = new Sequence(sqTo.getName(), align.getAStr2());
95 alSeq2.setStart(sqTo.getStart() + align.getSeq2Start() - 1);
96 alSeq2.setEnd(sqTo.getStart() + align.getSeq2End() - 1);
97 alSeq2.setDatasetSequence(sqTo);
98 System.out.println(new AppletFormatAdapter().formatSequences("STH",
99 new Alignment(new SequenceI[]
100 { sqFrom, alSeq1, sqTo, alSeq2 }), true));
102 Mapping mp = align.getMappingFromS1(false);
104 AlignmentAnnotation almap1 = new AlignmentAnnotation(
105 sqTo.getAnnotation()[0]);
106 almap1.liftOver(sqFrom, mp);
107 assertEquals(almap1.sequenceRef, sqFrom);
108 alSeq1.addAlignmentAnnotation(almap1);
109 almap1.setSequenceRef(alSeq1);
110 almap1.adjustForAlignment();
111 AlignmentAnnotation almap2 = new AlignmentAnnotation(
112 sqFrom.getAnnotation()[0]);
113 almap2.liftOver(sqTo, mp);
114 assertEquals(almap2.sequenceRef, sqTo);
116 alSeq2.addAlignmentAnnotation(almap2);
117 almap2.setSequenceRef(alSeq2);
118 almap2.adjustForAlignment();
120 AlignmentI all = new Alignment(new SequenceI[]
122 all.addAnnotation(almap1);
123 all.addAnnotation(almap2);
124 System.out.println(new AppletFormatAdapter().formatSequences("STH",
127 for (int p = 0; p < alSeq1.getLength(); p++)
129 Annotation orig1, trans1, orig2, trans2;
130 trans2 = almap2.annotations[p];
131 orig2 = origFrom.annotations[alSeq1.findPosition(p)
132 - sqFrom.getStart()];
133 orig1 = origTo.annotations[alSeq2.findPosition(p) - sqTo.getStart()];
134 trans1 = almap1.annotations[p];
135 if (trans1 == trans2)
137 System.out.println("Pos " + p + " mismatch");
141 "Mismatch on Original From and transferred annotation on 2",
142 (orig2 != null) ? orig2.toString() : null,
143 (trans2 != null) ? trans2.toString() : null);
145 "Mismatch on Original To and transferred annotation on 1",
146 (orig1 != null) ? orig1.toString() : null,
147 (trans1 != null) ? trans1.toString() : null);
149 + (almap1.annotations.length > p ? almap1.annotations[p].displayCharacter
152 + (almap2.annotations.length > p ? almap2.annotations[p].displayCharacter
154 assertEquals("Position " + p + " " + alm1 + " " + alm2, alm1, alm2);
156 // new jalview.io.FormatAdapter().formatSequences("STOCKHOLM", n)