2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.analysis;
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;
28 import org.junit.Test;
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;
38 public class AlignmentUtilsTests
40 public static Sequence ts = new Sequence("short",
41 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm");
44 public void testExpandContext()
46 AlignmentI al = new Alignment(new Sequence[] {});
47 for (int i = 4; i < 14; i += 2)
49 SequenceI s1=ts.deriveSequence().getSubSequence(i, i+7);
52 System.out.println(new AppletFormatAdapter().formatSequences("Clustal", al, true));
53 for (int flnk=-1;flnk<25; flnk++)
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));
62 * Full expansion to complete sequences
64 for (SequenceI sq : exp.getSequences())
66 String ung = sq.getSequenceAsString().replaceAll("-+", "");
67 final String errorMsg = "Flanking sequence not the same as original dataset sequence.\n"
70 + sq.getDatasetSequence().getSequenceAsString();
71 assertTrue(errorMsg, ung.equalsIgnoreCase(sq.getDatasetSequence()
72 .getSequenceAsString()));
78 * Last sequence is fully expanded, others have leading gaps to match
80 assertTrue(exp.getSequenceAt(4).getSequenceAsString()
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"));
95 * Test that annotations are correctly adjusted by expandContext
98 public void testExpandContext_annotation()
100 AlignmentI al = new Alignment(new Sequence[]
102 SequenceI ds = new Sequence("Seq1", "ABCDEFGHI");
104 SequenceI seq1 = ds.deriveSequence().getSubSequence(3, 6);
105 al.addSequence(seq1);
108 * Annotate DEF with 4/5/6 respectively
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);
117 * The annotations array should match aligned positions
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);
125 * Check annotation to sequence position mappings before expanding the
126 * sequence; these are set up in Sequence.addAlignmentAnnotation ->
127 * Annotation.setSequenceRef -> createSequenceMappings
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));
140 * Expand the subsequence to the full sequence abcDEFghi
142 AlignmentI expanded = AlignmentUtils.expandContext(al, -1);
143 assertEquals("abcDEFghi", expanded.getSequenceAt(0)
144 .getSequenceAsString());
147 * Confirm the alignment and sequence have the same SS annotation,
148 * referencing the expanded sequence
150 ann = expanded.getSequenceAt(0).getAnnotation()[0];
151 assertSame(ann, expanded.getAlignmentAnnotation()[0]);
152 assertSame(expanded.getSequenceAt(0), ann.sequenceRef);
155 * The annotations array should have null values except for annotated
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]);
169 * sequence position mappings should be unchanged
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));