2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.datamodel;
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertTrue;
27 import jalview.io.AppletFormatAdapter;
28 import jalview.io.FormatAdapter;
29 import jalview.util.MapList;
31 import java.io.IOException;
32 import java.util.Iterator;
34 import org.testng.annotations.BeforeMethod;
35 import org.testng.annotations.Test;
38 * Unit tests for Alignment datamodel.
43 public class AlignmentTest
46 private static final String TEST_DATA =
48 "#=GS D.melanogaster.1 AC AY119185.1/838-902\n" +
49 "#=GS D.melanogaster.2 AC AC092237.1/57223-57161\n" +
50 "#=GS D.melanogaster.3 AC AY060611.1/560-627\n" +
51 "D.melanogaster.1 G.AGCC.CU...AUGAUCGA\n" +
52 "#=GR D.melanogaster.1 SS ................((((\n" +
53 "D.melanogaster.2 C.AUUCAACU.UAUGAGGAU\n" +
54 "#=GR D.melanogaster.2 SS ................((((\n" +
55 "D.melanogaster.3 G.UGGCGCU..UAUGACGCA\n" +
56 "#=GR D.melanogaster.3 SS (.(((...(....(((((((\n" +
59 private static final String AA_SEQS_1 =
65 private static final String CDNA_SEQS_1 =
67 "AC-GG--CUC-CAA-CT\n" +
69 "-CG-TTA--ACG---AAGT\n";
71 private static final String CDNA_SEQS_2 =
78 private AlignmentI al;
81 * Helper method to load an alignment and ensure dataset sequences are set up.
89 protected AlignmentI loadAlignment(final String data, String format)
92 AlignmentI a = new FormatAdapter().readFile(data,
93 AppletFormatAdapter.PASTE, format);
99 * Read in Stockholm format test data including secondary structure
102 @BeforeMethod(alwaysRun = true)
103 public void setUp() throws IOException
105 al = loadAlignment(TEST_DATA, "STH");
107 for (AlignmentAnnotation ann : al.getAlignmentAnnotation())
109 ann.setCalcId("CalcIdFor" + al.getSequenceAt(i).getName());
115 * Test method that returns annotations that match on calcId.
117 @Test(groups = { "Functional" })
118 public void testFindAnnotation_byCalcId()
120 Iterable<AlignmentAnnotation> anns = al
121 .findAnnotation("CalcIdForD.melanogaster.2");
122 Iterator<AlignmentAnnotation> iter = anns.iterator();
123 assertTrue(iter.hasNext());
124 AlignmentAnnotation ann = iter.next();
125 assertEquals("D.melanogaster.2", ann.sequenceRef.getName());
126 assertFalse(iter.hasNext());
129 @Test(groups = { "Functional" })
130 public void testDeleteAllAnnotations_includingAutocalculated()
132 AlignmentAnnotation aa = new AlignmentAnnotation("Consensus",
134 aa.autoCalculated = true;
135 al.addAnnotation(aa);
136 AlignmentAnnotation[] anns = al.getAlignmentAnnotation();
137 assertEquals("Wrong number of annotations before deleting", 4,
139 al.deleteAllAnnotations(true);
140 assertEquals("Not all deleted", 0, al.getAlignmentAnnotation().length);
143 @Test(groups = { "Functional" })
144 public void testDeleteAllAnnotations_excludingAutocalculated()
146 AlignmentAnnotation aa = new AlignmentAnnotation("Consensus",
148 aa.autoCalculated = true;
149 al.addAnnotation(aa);
150 AlignmentAnnotation[] anns = al.getAlignmentAnnotation();
151 assertEquals("Wrong number of annotations before deleting", 4,
153 al.deleteAllAnnotations(false);
154 assertEquals("Not just one annotation left", 1,
155 al.getAlignmentAnnotation().length);
159 * Tests for realigning as per a supplied alignment: Dna as Dna.
161 * Note: AlignedCodonFrame's state variables are named for protein-to-cDNA
162 * mapping, but can be exploited for a general 'sequence-to-sequence' mapping
165 * @throws IOException
167 @Test(groups = { "Functional" })
168 public void testAlignAs_dnaAsDna() throws IOException
171 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
173 AlignmentI al2 = loadAlignment(CDNA_SEQS_2, "FASTA");
176 * Make mappings between sequences. The 'aligned cDNA' is playing the role
177 * of what would normally be protein here.
179 AlignedCodonFrame acf = new AlignedCodonFrame();
180 MapList ml = new MapList(new int[] { 1, 12 }, new int[] { 1, 12 }, 1, 1);
181 acf.addMap(al2.getSequenceAt(0), al1.getSequenceAt(0), ml);
182 acf.addMap(al2.getSequenceAt(1), al1.getSequenceAt(1), ml);
183 al1.addCodonFrame(acf);
185 ((Alignment) al2).alignAs(al1, false, true);
186 assertEquals("GC-TC--GUC-GTA-CT", al2.getSequenceAt(0)
187 .getSequenceAsString());
188 assertEquals("-GG-GTC--AGG---CAGT", al2.getSequenceAt(1)
189 .getSequenceAsString());
193 * Aligning protein from cDNA.
195 * @throws IOException
197 @Test(groups = { "Functional" })
198 public void testAlignAs_proteinAsCdna() throws IOException
200 // see also AlignmentUtilsTests
201 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
202 AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA");
203 AlignedCodonFrame acf = new AlignedCodonFrame();
204 MapList ml = new MapList(new int[] { 1, 12 }, new int[] { 1, 4 }, 3, 1);
205 acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml);
206 acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml);
207 al2.addCodonFrame(acf);
209 ((Alignment) al2).alignAs(al1, false, true);
210 assertEquals("K-Q-Y-L-", al2.getSequenceAt(0).getSequenceAsString());
211 assertEquals("-R-F-P-W", al2.getSequenceAt(1).getSequenceAsString());
215 * Test aligning cdna as per protein alignment.
217 * @throws IOException
219 @Test(groups = { "Functional" })
220 public void testAlignAs_cdnaAsProtein() throws IOException
223 * Load alignments and add mappings for cDNA to protein
225 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
226 AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA");
227 AlignedCodonFrame acf = new AlignedCodonFrame();
228 MapList ml = new MapList(new int[] { 1, 12 }, new int[] { 1, 4 }, 3, 1);
229 acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml);
230 acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml);
231 al2.addCodonFrame(acf);
234 * Realign DNA; currently keeping existing gaps in introns only
236 ((Alignment) al1).alignAs(al2, false, true);
237 assertEquals("ACG---GCUCCA------ACT", al1.getSequenceAt(0)
238 .getSequenceAsString());
239 assertEquals("---CGT---TAACGA---AGT", al1.getSequenceAt(1)
240 .getSequenceAsString());
244 * Test aligning dna as per protein alignment, for the case where there are
245 * introns (i.e. some dna sites have no mapping from a peptide).
247 * @throws IOException
249 @Test(groups = { "Functional" })
250 public void testAlignAs_dnaAsProtein_withIntrons() throws IOException
253 * Load alignments and add mappings for cDNA to protein
255 String dna1 = "A-Aa-gG-GCC-cT-TT";
256 String dna2 = "c--CCGgg-TT--T-AA-A";
257 AlignmentI al1 = loadAlignment(">Seq1\n" + dna1 + "\n>Seq2\n" + dna2
259 AlignmentI al2 = loadAlignment(">Seq1\n-P--YK\n>Seq2\nG-T--F\n",
261 AlignedCodonFrame acf = new AlignedCodonFrame();
262 // Seq1 has intron at dna positions 3,4,9 so splice is AAG GCC TTT
263 // Seq2 has intron at dna positions 1,5,6 so splice is CCG TTT AAA
264 MapList ml1 = new MapList(new int[] { 1, 2, 5, 8, 10, 12 }, new int[] {
266 acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml1);
267 MapList ml2 = new MapList(new int[] { 2, 4, 7, 12 },
268 new int[] { 1, 3 }, 3, 1);
269 acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml2);
270 al2.addCodonFrame(acf);
273 * Align ignoring gaps in dna introns and exons
275 ((Alignment) al1).alignAs(al2, false, false);
276 assertEquals("---AAagG------GCCcTTT", al1.getSequenceAt(0)
277 .getSequenceAsString());
278 // note 1 gap in protein corresponds to 'gg-' in DNA (3 positions)
279 assertEquals("cCCGgg-TTT------AAA", al1.getSequenceAt(1)
280 .getSequenceAsString());
283 * Reset and realign, preserving gaps in dna introns and exons
285 al1.getSequenceAt(0).setSequence(dna1);
286 al1.getSequenceAt(1).setSequence(dna2);
287 ((Alignment) al1).alignAs(al2, true, true);
288 // String dna1 = "A-Aa-gG-GCC-cT-TT";
289 // String dna2 = "c--CCGgg-TT--T-AA-A";
290 // assumption: we include 'the greater of' protein/dna gap lengths, not both
291 assertEquals("---A-Aa-gG------GCC-cT-TT", al1.getSequenceAt(0)
292 .getSequenceAsString());
293 assertEquals("c--CCGgg-TT--T------AA-A", al1.getSequenceAt(1)
294 .getSequenceAsString());