1 package jalview.datamodel;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6 import jalview.io.AppletFormatAdapter;
7 import jalview.io.FormatAdapter;
8 import jalview.util.MapList;
10 import java.io.IOException;
11 import java.util.Iterator;
13 import org.junit.Before;
14 import org.junit.Test;
17 * Unit tests for Alignment datamodel.
22 public class AlignmentTest
25 private static final String TEST_DATA =
27 "#=GS D.melanogaster.1 AC AY119185.1/838-902\n" +
28 "#=GS D.melanogaster.2 AC AC092237.1/57223-57161\n" +
29 "#=GS D.melanogaster.3 AC AY060611.1/560-627\n" +
30 "D.melanogaster.1 G.AGCC.CU...AUGAUCGA\n" +
31 "#=GR D.melanogaster.1 SS ................((((\n" +
32 "D.melanogaster.2 C.AUUCAACU.UAUGAGGAU\n" +
33 "#=GR D.melanogaster.2 SS ................((((\n" +
34 "D.melanogaster.3 G.UGGCGCU..UAUGACGCA\n" +
35 "#=GR D.melanogaster.3 SS (.(((...(....(((((((\n" +
38 private static final String AA_SEQS_1 =
44 private static final String CDNA_SEQS_1 =
46 "AC-GG--CUC-CAA-CT\n" +
48 "-CG-TTA--ACG---AAGT\n";
50 private static final String CDNA_SEQS_2 =
57 private AlignmentI al;
60 * Helper method to load an alignment and ensure dataset sequences are set up.
68 protected AlignmentI loadAlignment(final String data, String format)
71 Alignment a = new FormatAdapter().readFile(data,
72 AppletFormatAdapter.PASTE, format);
78 * Read in Stockholm format test data including secondary structure
82 public void setUp() throws IOException
84 al = loadAlignment(TEST_DATA, "STH");
86 for (AlignmentAnnotation ann : al.getAlignmentAnnotation())
88 ann.setCalcId("CalcIdFor" + al.getSequenceAt(i).getName());
94 * Test method that returns annotations that match on calcId.
97 public void testFindAnnotation_byCalcId()
99 Iterable<AlignmentAnnotation> anns = al
100 .findAnnotation("CalcIdForD.melanogaster.2");
101 Iterator<AlignmentAnnotation> iter = anns.iterator();
102 assertTrue(iter.hasNext());
103 AlignmentAnnotation ann = iter.next();
104 assertEquals("D.melanogaster.2", ann.sequenceRef.getName());
105 assertFalse(iter.hasNext());
109 public void testDeleteAllAnnotations_includingAutocalculated()
111 AlignmentAnnotation aa = new AlignmentAnnotation("Consensus",
113 aa.autoCalculated = true;
114 al.addAnnotation(aa);
115 AlignmentAnnotation[] anns = al.getAlignmentAnnotation();
116 assertEquals("Wrong number of annotations before deleting", 4,
118 al.deleteAllAnnotations(true);
119 assertEquals("Not all deleted", 0, al.getAlignmentAnnotation().length);
123 public void testDeleteAllAnnotations_excludingAutocalculated()
125 AlignmentAnnotation aa = new AlignmentAnnotation("Consensus",
127 aa.autoCalculated = true;
128 al.addAnnotation(aa);
129 AlignmentAnnotation[] anns = al.getAlignmentAnnotation();
130 assertEquals("Wrong number of annotations before deleting", 4,
132 al.deleteAllAnnotations(false);
133 assertEquals("Not just one annotation left", 1,
134 al.getAlignmentAnnotation().length);
138 * Tests for realigning as per a supplied alignment: Dna as Dna.
140 * Note: AlignedCodonFrame's state variables are named for protein-to-cDNA
141 * mapping, but can be exploited for a general 'sequence-to-sequence' mapping
144 * @throws IOException
147 public void testAlignAs_dnaAsDna() throws IOException
150 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
152 AlignmentI al2 = loadAlignment(CDNA_SEQS_2, "FASTA");
155 * Make mappings between sequences. The 'aligned cDNA' is playing the role
156 * of what would normally be protein here.
158 AlignedCodonFrame acf = new AlignedCodonFrame();
159 MapList ml = new MapList(new int[]
162 acf.addMap(al2.getSequenceAt(0), al1.getSequenceAt(0), ml);
163 acf.addMap(al2.getSequenceAt(1), al1.getSequenceAt(1), ml);
164 al1.addCodonFrame(acf);
166 ((Alignment) al2).alignAs(al1, false, true);
167 assertEquals("GC-TC--GUC-GTA-CT", al2.getSequenceAt(0)
168 .getSequenceAsString());
169 assertEquals("-GG-GTC--AGG---CAGT", al2.getSequenceAt(1)
170 .getSequenceAsString());
174 * Aligning protein from cDNA.
176 * @throws IOException
179 public void testAlignAs_proteinAsCdna() throws IOException
181 // see also AlignmentUtilsTests
182 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
183 AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA");
184 AlignedCodonFrame acf = new AlignedCodonFrame();
185 MapList ml = new MapList(new int[]
188 acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml);
189 acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml);
190 al2.addCodonFrame(acf);
192 ((Alignment) al2).alignAs(al1, false, true);
193 assertEquals("K-Q-Y-L-", al2.getSequenceAt(0).getSequenceAsString());
194 assertEquals("-R-F-P-W", al2.getSequenceAt(1).getSequenceAsString());
198 * Test aligning cdna as per protein alignment.
200 * @throws IOException
203 public void testAlignAs_cdnaAsProtein() throws IOException
206 * Load alignments and add mappings for cDNA to protein
208 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
209 AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA");
210 AlignedCodonFrame acf = new AlignedCodonFrame();
211 MapList ml = new MapList(new int[]
214 acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml);
215 acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml);
216 al2.addCodonFrame(acf);
219 * Realign DNA; currently keeping existing gaps in introns only
221 ((Alignment) al1).alignAs(al2, false, true);
222 assertEquals("ACG---GCUCCA------ACT", al1.getSequenceAt(0)
223 .getSequenceAsString());
224 assertEquals("---CGT---TAACGA---AGT", al1.getSequenceAt(1)
225 .getSequenceAsString());
229 * Test aligning dna as per protein alignment, for the case where there are
230 * introns (i.e. some dna sites have no mapping from a peptide).
232 * @throws IOException
235 public void testAlignAs_dnaAsProtein_withIntrons() throws IOException
238 * Load alignments and add mappings for cDNA to protein
240 String dna1 = "A-Aa-gG-GCC-cT-TT";
241 String dna2 = "c--CCGgg-TT--T-AA-A";
242 AlignmentI al1 = loadAlignment(">Seq1\n" + dna1 + "\n>Seq2\n" + dna2
244 AlignmentI al2 = loadAlignment(">Seq1\n-P--YK\n>Seq2\nG-T--F\n",
246 AlignedCodonFrame acf = new AlignedCodonFrame();
247 // Seq1 has intron at dna positions 3,4,9 so splice is AAG GCC TTT
248 // Seq2 has intron at dna positions 1,5,6 so splice is CCG TTT AAA
249 MapList ml1 = new MapList(new int[]
250 { 1, 2, 5, 8, 10, 12 }, new int[]
252 acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml1);
253 MapList ml2 = new MapList(new int[]
254 { 2, 4, 7, 12 }, new int[]
256 acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml2);
257 al2.addCodonFrame(acf);
260 * Align ignoring gaps in dna introns and exons
262 ((Alignment) al1).alignAs(al2, false, false);
263 assertEquals("---AAagG------GCCcTTT", al1.getSequenceAt(0)
264 .getSequenceAsString());
265 // note 1 gap in protein corresponds to 'gg-' in DNA (3 positions)
266 assertEquals("cCCGgg-TTT------AAA", al1.getSequenceAt(1)
267 .getSequenceAsString());
270 * Reset and realign, preserving gaps in dna introns and exons
272 al1.getSequenceAt(0).setSequence(dna1);
273 al1.getSequenceAt(1).setSequence(dna2);
274 ((Alignment) al1).alignAs(al2, true, true);
275 // String dna1 = "A-Aa-gG-GCC-cT-TT";
276 // String dna2 = "c--CCGgg-TT--T-AA-A";
277 // assumption: we include 'the greater of' protein/dna gap lengths, not both
278 assertEquals("---A-Aa-gG------GCC-cT-TT", al1.getSequenceAt(0)
279 .getSequenceAsString());
280 assertEquals("c--CCGgg-TT--T------AA-A", al1.getSequenceAt(1)
281 .getSequenceAsString());