JAL-674 JAL-1305 JAL-348 new liftOver method and test to allow annotation to be propa...
[jalview.git] / test / jalview / datamodel / AlignmentAnnotationTests.java
1 package jalview.datamodel;
2
3 import static org.junit.Assert.*;
4
5 import java.awt.Frame;
6
7 import javax.swing.JFrame;
8 import javax.swing.SwingWorker;
9
10 import jalview.analysis.AlignSeq;
11 import jalview.gui.AlignFrame;
12 import jalview.io.AppletFormatAdapter;
13 import jalview.io.FormatAdapter;
14
15 import org.junit.Test;
16
17 public class AlignmentAnnotationTests
18 {
19   /**
20    * create some dummy annotation derived from the sequence
21    * 
22    * @param sq
23    */
24   public static void createAnnotation(SequenceI sq)
25   {
26     Annotation[] al = new Annotation[sq.getLength()];
27     for (int i = 0; i < al.length; i++)
28     {
29       al[i] = new Annotation(new Annotation("" + sq.getCharAt(i), "",
30               (char) 0, (float) sq.findPosition(i)));
31     }
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);
37   }
38
39   /**
40    * use this to test annotation derived from method above as it is transferred
41    * across different sequences derived from same dataset coordinate frame
42    * 
43    * @param ala
44    */
45   public static void testAnnotTransfer(AlignmentAnnotation ala)
46   {
47     assertEquals(
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++)
52     {
53       if (ala.annotations[p] != null)
54       {
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));
59       }
60     }
61   }
62
63   /**
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
67    * sequences)
68    */
69   @Test
70   public void testLiftOver()
71   {
72     SequenceI sqFrom = new Sequence("fromLong", "QQQCDEWGH");
73     sqFrom.setStart(10);
74     sqFrom.setEnd(sqFrom.findPosition(sqFrom.getLength() - 1));
75     SequenceI sqTo = new Sequence("toShort", "RCDEW");
76     sqTo.setStart(20);
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,
83             AlignSeq.PEP);
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));
95
96     Mapping mp = align.getMappingFromS1(false);
97
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);
109
110     alSeq2.addAlignmentAnnotation(almap2);
111     almap2.setSequenceRef(alSeq2);
112     almap2.adjustForAlignment();
113
114     AlignmentI all = new Alignment(new SequenceI[]
115     { alSeq1, alSeq2 });
116     all.addAnnotation(almap1);
117     all.addAnnotation(almap2);
118     System.out.println(new AppletFormatAdapter().formatSequences("STH",
119             all, true));
120
121     for (int p = 0; p < alSeq1.getLength(); p++)
122     {
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)
130       {
131         System.out.println("Pos " + p + " mismatch");
132         continue;
133       }
134       assertEquals(
135               "Mismatch on Original From and transferred annotation on 2",
136               (orig2 != null) ? orig2.toString() : null,
137               (trans2 != null) ? trans2.toString() : null);
138       assertEquals(
139               "Mismatch on Original To and transferred annotation on 1",
140               (orig1 != null) ? orig1.toString() : null,
141               (trans1 != null) ? trans1.toString() : null);
142       String alm1 = ""
143               + (almap1.annotations.length > p ? almap1.annotations[p].displayCharacter
144                       : "Out of range");
145       String alm2 = ""
146               + (almap2.annotations.length > p ? almap2.annotations[p].displayCharacter
147                       : "Out of range");
148       assertEquals("Position " + p + " " + alm1 + " " + alm2, alm1, alm2);
149     }
150     // new jalview.io.FormatAdapter().formatSequences("STOCKHOLM", n)
151   }
152
153 }