JAL-1712 fixes/tests for Castor binding and 'show flanking regions'
[jalview.git] / test / jalview / analysis / AlignmentUtilsTests.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.analysis;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertSame;
26 import static org.junit.Assert.assertTrue;
27
28 import org.junit.Test;
29
30 import jalview.datamodel.Alignment;
31 import jalview.datamodel.AlignmentAnnotation;
32 import jalview.datamodel.AlignmentI;
33 import jalview.datamodel.Annotation;
34 import jalview.datamodel.Sequence;
35 import jalview.datamodel.SequenceI;
36 import jalview.io.AppletFormatAdapter;
37
38 public class AlignmentUtilsTests 
39 {
40   public static Sequence ts = new Sequence("short",
41           "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm");
42
43   @Test
44   public void testExpandContext()
45   {
46     AlignmentI al = new Alignment(new Sequence[] {});
47     for (int i = 4; i < 14; i += 2)
48     {
49       SequenceI s1=ts.deriveSequence().getSubSequence(i, i+7);
50       al.addSequence(s1);
51     }
52     System.out.println(new AppletFormatAdapter().formatSequences("Clustal", al, true));
53     for (int flnk=-1;flnk<25; flnk++)
54     {
55       AlignmentI exp = AlignmentUtils.expandContext(al, flnk);
56       System.out.println("\nFlank size: " + flnk);
57       System.out.println(new AppletFormatAdapter().formatSequences(
58               "Clustal", exp, true));
59       if (flnk == -1)
60       {
61         /*
62          * Full expansion to complete sequences
63          */
64         for (SequenceI sq : exp.getSequences())
65         {
66           String ung = sq.getSequenceAsString().replaceAll("-+", "");
67           final String errorMsg = "Flanking sequence not the same as original dataset sequence.\n"
68                   + ung
69                   + "\n"
70                   + sq.getDatasetSequence().getSequenceAsString();
71           assertTrue(errorMsg, ung.equalsIgnoreCase(sq.getDatasetSequence()
72                   .getSequenceAsString()));
73         }
74       }
75       else if (flnk == 24)
76       {
77         /*
78          * Last sequence is fully expanded, others have leading gaps to match
79          */
80         assertTrue(exp.getSequenceAt(4).getSequenceAsString()
81                 .startsWith("abc"));
82         assertTrue(exp.getSequenceAt(3).getSequenceAsString()
83                 .startsWith("--abc"));
84         assertTrue(exp.getSequenceAt(2).getSequenceAsString()
85                 .startsWith("----abc"));
86         assertTrue(exp.getSequenceAt(1).getSequenceAsString()
87                 .startsWith("------abc"));
88         assertTrue(exp.getSequenceAt(0).getSequenceAsString()
89                 .startsWith("--------abc"));
90       }
91     }
92   }
93
94   /**
95    * Test that annotations are correctly adjusted by expandContext
96    */
97   @Test
98   public void testExpandContext_annotation()
99   {
100     AlignmentI al = new Alignment(new Sequence[]
101     {});
102     SequenceI ds = new Sequence("Seq1", "ABCDEFGHI");
103     // subsequence DEF:
104     SequenceI seq1 = ds.deriveSequence().getSubSequence(3, 6);
105     al.addSequence(seq1);
106
107     /*
108      * Annotate DEF with 4/5/6 respectively
109      */
110     Annotation[] anns = new Annotation[]
111     { new Annotation(4), new Annotation(5), new Annotation(6) };
112     AlignmentAnnotation ann = new AlignmentAnnotation("SS",
113             "secondary structure", anns);
114     seq1.addAlignmentAnnotation(ann);
115
116     /*
117      * The annotations array should match aligned positions
118      */
119     assertEquals(3, ann.annotations.length);
120     assertEquals(4, ann.annotations[0].value, 0.001);
121     assertEquals(5, ann.annotations[1].value, 0.001);
122     assertEquals(6, ann.annotations[2].value, 0.001);
123
124     /*
125      * Check annotation to sequence position mappings before expanding the
126      * sequence; these are set up in Sequence.addAlignmentAnnotation ->
127      * Annotation.setSequenceRef -> createSequenceMappings
128      */
129     assertNull(ann.getAnnotationForPosition(1));
130     assertNull(ann.getAnnotationForPosition(2));
131     assertNull(ann.getAnnotationForPosition(3));
132     assertEquals(4, ann.getAnnotationForPosition(4).value, 0.001);
133     assertEquals(5, ann.getAnnotationForPosition(5).value, 0.001);
134     assertEquals(6, ann.getAnnotationForPosition(6).value, 0.001);
135     assertNull(ann.getAnnotationForPosition(7));
136     assertNull(ann.getAnnotationForPosition(8));
137     assertNull(ann.getAnnotationForPosition(9));
138
139     /*
140      * Expand the subsequence to the full sequence abcDEFghi
141      */
142     AlignmentI expanded = AlignmentUtils.expandContext(al, -1);
143     assertEquals("abcDEFghi", expanded.getSequenceAt(0)
144             .getSequenceAsString());
145
146     /*
147      * Confirm the alignment and sequence have the same SS annotation,
148      * referencing the expanded sequence
149      */
150     ann = expanded.getSequenceAt(0).getAnnotation()[0];
151     assertSame(ann, expanded.getAlignmentAnnotation()[0]);
152     assertSame(expanded.getSequenceAt(0), ann.sequenceRef);
153
154     /*
155      * The annotations array should have null values except for annotated
156      * positions
157      */
158     assertNull(ann.annotations[0]);
159     assertNull(ann.annotations[1]);
160     assertNull(ann.annotations[2]);
161     assertEquals(4, ann.annotations[3].value, 0.001);
162     assertEquals(5, ann.annotations[4].value, 0.001);
163     assertEquals(6, ann.annotations[5].value, 0.001);
164     assertNull(ann.annotations[6]);
165     assertNull(ann.annotations[7]);
166     assertNull(ann.annotations[8]);
167
168     /*
169      * sequence position mappings should be unchanged
170      */
171     assertNull(ann.getAnnotationForPosition(1));
172     assertNull(ann.getAnnotationForPosition(2));
173     assertNull(ann.getAnnotationForPosition(3));
174     assertEquals(4, ann.getAnnotationForPosition(4).value, 0.001);
175     assertEquals(5, ann.getAnnotationForPosition(5).value, 0.001);
176     assertEquals(6, ann.getAnnotationForPosition(6).value, 0.001);
177     assertNull(ann.getAnnotationForPosition(7));
178     assertNull(ann.getAnnotationForPosition(8));
179     assertNull(ann.getAnnotationForPosition(9));
180   }
181
182 }