1 package jalview.datamodel;
3 import static org.junit.Assert.*;
7 import javax.swing.JFrame;
8 import javax.swing.SwingWorker;
10 import jalview.analysis.AlignSeq;
11 import jalview.gui.AlignFrame;
12 import jalview.io.AppletFormatAdapter;
13 import jalview.io.FormatAdapter;
15 import org.junit.Test;
17 public class AlignmentAnnotationTests
20 * create some dummy annotation derived from the sequence
24 public static void createAnnotation(SequenceI sq)
26 Annotation[] al = new Annotation[sq.getLength()];
27 for (int i = 0; i < al.length; i++)
29 al[i] = new Annotation(new Annotation("" + sq.getCharAt(i), "",
30 (char) 0, (float) sq.findPosition(i)));
32 AlignmentAnnotation alan = new AlignmentAnnotation("For "
33 + sq.getName(), "Fake alignment annot", al);
34 // create a sequence mapping for the annotation vector in its current state
35 alan.createSequenceMapping(sq, sq.getStart(), false);
36 sq.addAlignmentAnnotation(alan);
40 * use this to test annotation derived from method above as it is transferred
41 * across different sequences derived from same dataset coordinate frame
45 public static void testAnnotTransfer(AlignmentAnnotation ala)
48 "Failed - need annotation created by createAnnotation method",
49 ala.description, "Fake alignment annot");
50 ala.adjustForAlignment();
51 for (int p = 0; p < ala.annotations.length; p++)
53 if (ala.annotations[p] != null)
55 assertEquals("Mismatch at position " + p
56 + " between annotation position value and sequence"
57 + ala.annotations[p], (int) ala.annotations[p].value,
58 ala.sequenceRef.findPosition(p));
64 * Tests the liftOver method and also exercises the functions for remapping
65 * annotation across different reference sequences. Here, the test is between
66 * different dataset frames (annotation transferred by mapping between
70 public void testLiftOver()
72 SequenceI sqFrom = new Sequence("fromLong", "QQQCDEWGH");
74 sqFrom.setEnd(sqFrom.findPosition(sqFrom.getLength() - 1));
75 SequenceI sqTo = new Sequence("toShort", "RCDEW");
77 sqTo.setEnd(sqTo.findPosition(sqTo.getLength() - 1));
78 createAnnotation(sqTo);
79 AlignmentAnnotation origTo = sqTo.getAnnotation()[0];
80 createAnnotation(sqFrom);
81 AlignmentAnnotation origFrom = sqFrom.getAnnotation()[0];
82 AlignSeq align = AlignSeq.doGlobalNWAlignment(sqFrom, sqTo,
84 SequenceI alSeq1 = new Sequence(sqFrom.getName(), align.getAStr1());
85 alSeq1.setStart(sqFrom.getStart() + align.getSeq1Start() - 1);
86 alSeq1.setEnd(sqFrom.getStart() + align.getSeq1End() - 1);
87 alSeq1.setDatasetSequence(sqFrom);
88 SequenceI alSeq2 = new Sequence(sqTo.getName(), align.getAStr2());
89 alSeq2.setStart(sqTo.getStart() + align.getSeq2Start() - 1);
90 alSeq2.setEnd(sqTo.getStart() + align.getSeq2End() - 1);
91 alSeq2.setDatasetSequence(sqTo);
92 System.out.println(new AppletFormatAdapter().formatSequences("STH",
93 new Alignment(new SequenceI[]
94 { sqFrom, alSeq1, sqTo, alSeq2 }), true));
96 Mapping mp = align.getMappingFromS1(false);
98 AlignmentAnnotation almap1 = new AlignmentAnnotation(
99 sqTo.getAnnotation()[0]);
100 almap1.liftOver(sqFrom, mp);
101 assertEquals(almap1.sequenceRef, sqFrom);
102 alSeq1.addAlignmentAnnotation(almap1);
103 almap1.setSequenceRef(alSeq1);
104 almap1.adjustForAlignment();
105 AlignmentAnnotation almap2 = new AlignmentAnnotation(
106 sqFrom.getAnnotation()[0]);
107 almap2.liftOver(sqTo, mp);
108 assertEquals(almap2.sequenceRef, sqTo);
110 alSeq2.addAlignmentAnnotation(almap2);
111 almap2.setSequenceRef(alSeq2);
112 almap2.adjustForAlignment();
114 AlignmentI all = new Alignment(new SequenceI[]
116 all.addAnnotation(almap1);
117 all.addAnnotation(almap2);
118 System.out.println(new AppletFormatAdapter().formatSequences("STH",
121 for (int p = 0; p < alSeq1.getLength(); p++)
123 Annotation orig1, trans1, orig2, trans2;
124 trans2 = almap2.annotations[p];
125 orig2 = origFrom.annotations[alSeq1.findPosition(p)
126 - sqFrom.getStart()];
127 orig1 = origTo.annotations[alSeq2.findPosition(p) - sqTo.getStart()];
128 trans1 = almap1.annotations[p];
129 if (trans1 == trans2)
131 System.out.println("Pos " + p + " mismatch");
135 "Mismatch on Original From and transferred annotation on 2",
136 (orig2 != null) ? orig2.toString() : null,
137 (trans2 != null) ? trans2.toString() : null);
139 "Mismatch on Original To and transferred annotation on 1",
140 (orig1 != null) ? orig1.toString() : null,
141 (trans1 != null) ? trans1.toString() : null);
143 + (almap1.annotations.length > p ? almap1.annotations[p].displayCharacter
146 + (almap2.annotations.length > p ? almap2.annotations[p].displayCharacter
148 assertEquals("Position " + p + " " + alm1 + " " + alm2, alm1, alm2);
150 // new jalview.io.FormatAdapter().formatSequences("STOCKHOLM", n)