1 package jalview.datamodel;
3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertFalse;
5 import static org.testng.AssertJUnit.assertTrue;
6 import org.testng.annotations.Test;
7 import org.testng.annotations.BeforeMethod;
8 import jalview.io.AppletFormatAdapter;
9 import jalview.io.FormatAdapter;
10 import jalview.util.MapList;
12 import java.io.IOException;
13 import java.util.Iterator;
16 * Unit tests for Alignment datamodel.
21 public class AlignmentTest
24 private static final String TEST_DATA =
26 "#=GS D.melanogaster.1 AC AY119185.1/838-902\n" +
27 "#=GS D.melanogaster.2 AC AC092237.1/57223-57161\n" +
28 "#=GS D.melanogaster.3 AC AY060611.1/560-627\n" +
29 "D.melanogaster.1 G.AGCC.CU...AUGAUCGA\n" +
30 "#=GR D.melanogaster.1 SS ................((((\n" +
31 "D.melanogaster.2 C.AUUCAACU.UAUGAGGAU\n" +
32 "#=GR D.melanogaster.2 SS ................((((\n" +
33 "D.melanogaster.3 G.UGGCGCU..UAUGACGCA\n" +
34 "#=GR D.melanogaster.3 SS (.(((...(....(((((((\n" +
37 private static final String AA_SEQS_1 =
43 private static final String CDNA_SEQS_1 =
45 "AC-GG--CUC-CAA-CT\n" +
47 "-CG-TTA--ACG---AAGT\n";
49 private static final String CDNA_SEQS_2 =
56 private AlignmentI al;
59 * Helper method to load an alignment and ensure dataset sequences are set up.
67 protected AlignmentI loadAlignment(final String data, String format)
70 Alignment a = new FormatAdapter().readFile(data,
71 AppletFormatAdapter.PASTE, format);
77 * Read in Stockholm format test data including secondary structure
81 public void setUp() throws IOException
83 al = loadAlignment(TEST_DATA, "STH");
85 for (AlignmentAnnotation ann : al.getAlignmentAnnotation())
87 ann.setCalcId("CalcIdFor" + al.getSequenceAt(i).getName());
93 * Test method that returns annotations that match on calcId.
96 public void testFindAnnotation_byCalcId()
98 Iterable<AlignmentAnnotation> anns = al
99 .findAnnotation("CalcIdForD.melanogaster.2");
100 Iterator<AlignmentAnnotation> iter = anns.iterator();
101 assertTrue(iter.hasNext());
102 AlignmentAnnotation ann = iter.next();
103 assertEquals("D.melanogaster.2", ann.sequenceRef.getName());
104 assertFalse(iter.hasNext());
108 public void testDeleteAllAnnotations_includingAutocalculated()
110 AlignmentAnnotation aa = new AlignmentAnnotation("Consensus",
112 aa.autoCalculated = true;
113 al.addAnnotation(aa);
114 AlignmentAnnotation[] anns = al.getAlignmentAnnotation();
115 assertEquals("Wrong number of annotations before deleting", 4,
117 al.deleteAllAnnotations(true);
118 assertEquals("Not all deleted", 0, al.getAlignmentAnnotation().length);
122 public void testDeleteAllAnnotations_excludingAutocalculated()
124 AlignmentAnnotation aa = new AlignmentAnnotation("Consensus",
126 aa.autoCalculated = true;
127 al.addAnnotation(aa);
128 AlignmentAnnotation[] anns = al.getAlignmentAnnotation();
129 assertEquals("Wrong number of annotations before deleting", 4,
131 al.deleteAllAnnotations(false);
132 assertEquals("Not just one annotation left", 1,
133 al.getAlignmentAnnotation().length);
137 * Tests for realigning as per a supplied alignment: Dna as Dna.
139 * Note: AlignedCodonFrame's state variables are named for protein-to-cDNA
140 * mapping, but can be exploited for a general 'sequence-to-sequence' mapping
143 * @throws IOException
146 public void testAlignAs_dnaAsDna() throws IOException
149 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
151 AlignmentI al2 = loadAlignment(CDNA_SEQS_2, "FASTA");
154 * Make mappings between sequences. The 'aligned cDNA' is playing the role
155 * of what would normally be protein here.
157 AlignedCodonFrame acf = new AlignedCodonFrame();
158 MapList ml = new MapList(new int[]
161 acf.addMap(al2.getSequenceAt(0), al1.getSequenceAt(0), ml);
162 acf.addMap(al2.getSequenceAt(1), al1.getSequenceAt(1), ml);
163 al1.addCodonFrame(acf);
165 ((Alignment) al2).alignAs(al1, false, true);
166 assertEquals("GC-TC--GUC-GTA-CT", al2.getSequenceAt(0)
167 .getSequenceAsString());
168 assertEquals("-GG-GTC--AGG---CAGT", al2.getSequenceAt(1)
169 .getSequenceAsString());
173 * Aligning protein from cDNA.
175 * @throws IOException
178 public void testAlignAs_proteinAsCdna() throws IOException
180 // see also AlignmentUtilsTests
181 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
182 AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA");
183 AlignedCodonFrame acf = new AlignedCodonFrame();
184 MapList ml = new MapList(new int[]
187 acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml);
188 acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml);
189 al2.addCodonFrame(acf);
191 ((Alignment) al2).alignAs(al1, false, true);
192 assertEquals("K-Q-Y-L-", al2.getSequenceAt(0).getSequenceAsString());
193 assertEquals("-R-F-P-W", al2.getSequenceAt(1).getSequenceAsString());
197 * Test aligning cdna as per protein alignment.
199 * @throws IOException
202 public void testAlignAs_cdnaAsProtein() throws IOException
205 * Load alignments and add mappings for cDNA to protein
207 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
208 AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA");
209 AlignedCodonFrame acf = new AlignedCodonFrame();
210 MapList ml = new MapList(new int[]
213 acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml);
214 acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml);
215 al2.addCodonFrame(acf);
218 * Realign DNA; currently keeping existing gaps in introns only
220 ((Alignment) al1).alignAs(al2, false, true);
221 assertEquals("ACG---GCUCCA------ACT", al1.getSequenceAt(0)
222 .getSequenceAsString());
223 assertEquals("---CGT---TAACGA---AGT", al1.getSequenceAt(1)
224 .getSequenceAsString());
228 * Test aligning dna as per protein alignment, for the case where there are
229 * introns (i.e. some dna sites have no mapping from a peptide).
231 * @throws IOException
234 public void testAlignAs_dnaAsProtein_withIntrons() throws IOException
237 * Load alignments and add mappings for cDNA to protein
239 String dna1 = "A-Aa-gG-GCC-cT-TT";
240 String dna2 = "c--CCGgg-TT--T-AA-A";
241 AlignmentI al1 = loadAlignment(">Seq1\n" + dna1 + "\n>Seq2\n" + dna2
243 AlignmentI al2 = loadAlignment(">Seq1\n-P--YK\n>Seq2\nG-T--F\n",
245 AlignedCodonFrame acf = new AlignedCodonFrame();
246 // Seq1 has intron at dna positions 3,4,9 so splice is AAG GCC TTT
247 // Seq2 has intron at dna positions 1,5,6 so splice is CCG TTT AAA
248 MapList ml1 = new MapList(new int[]
249 { 1, 2, 5, 8, 10, 12 }, new int[]
251 acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml1);
252 MapList ml2 = new MapList(new int[]
253 { 2, 4, 7, 12 }, new int[]
255 acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml2);
256 al2.addCodonFrame(acf);
259 * Align ignoring gaps in dna introns and exons
261 ((Alignment) al1).alignAs(al2, false, false);
262 assertEquals("---AAagG------GCCcTTT", al1.getSequenceAt(0)
263 .getSequenceAsString());
264 // note 1 gap in protein corresponds to 'gg-' in DNA (3 positions)
265 assertEquals("cCCGgg-TTT------AAA", al1.getSequenceAt(1)
266 .getSequenceAsString());
269 * Reset and realign, preserving gaps in dna introns and exons
271 al1.getSequenceAt(0).setSequence(dna1);
272 al1.getSequenceAt(1).setSequence(dna2);
273 ((Alignment) al1).alignAs(al2, true, true);
274 // String dna1 = "A-Aa-gG-GCC-cT-TT";
275 // String dna2 = "c--CCGgg-TT--T-AA-A";
276 // assumption: we include 'the greater of' protein/dna gap lengths, not both
277 assertEquals("---A-Aa-gG------GCC-cT-TT", al1.getSequenceAt(0)
278 .getSequenceAsString());
279 assertEquals("c--CCGgg-TT--T------AA-A", al1.getSequenceAt(1)
280 .getSequenceAsString());