2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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.assertTrue;
25 import jalview.analysis.AlignmentUtils.MappingResult;
26 import jalview.datamodel.AlignedCodonFrame;
27 import jalview.datamodel.Alignment;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.Mapping;
30 import jalview.datamodel.Sequence;
31 import jalview.datamodel.SequenceI;
32 import jalview.io.AppletFormatAdapter;
33 import jalview.io.FormatAdapter;
34 import jalview.util.MapList;
36 import java.io.IOException;
37 import java.util.Arrays;
38 import java.util.List;
41 import org.junit.Test;
43 public class AlignmentUtilsTests
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 public static Sequence ts=new Sequence("short","ASDASDASDASDASDASDASDASDASDASDASDASDASD");
81 public void testExpandFlanks()
83 AlignmentI al = new Alignment(new Sequence[] {});
84 for (int i=4;i<14;i+=3)
86 SequenceI s1=ts.deriveSequence().getSubSequence(i, i+7);
89 System.out.println(new AppletFormatAdapter().formatSequences("Clustal", al, true));
90 for (int flnk=-1;flnk<25; flnk++)
93 System.out.println("\nFlank size: "+flnk);
94 System.out.println(new AppletFormatAdapter().formatSequences("Clustal", exp=AlignmentUtils.expandContext(al, flnk), true));
96 for (SequenceI sq:exp.getSequences())
98 String ung = sq.getSequenceAsString().replaceAll("-+", "");
99 assertTrue("Flanking sequence not the same as original dataset sequence.\n"+ung+"\n"+sq.getDatasetSequence().getSequenceAsString(),ung.equalsIgnoreCase(sq.getDatasetSequence().getSequenceAsString()));
106 * Test method that returns a map of lists of sequences by sequence name.
108 * @throws IOException
111 public void testGetSequencesByName() throws IOException
113 final String data = ">Seq1Name\nKQYL\n" + ">Seq2Name\nRFPW\n"
114 + ">Seq1Name\nABCD\n";
115 AlignmentI al = loadAlignment(data, "FASTA");
116 Map<String, List<SequenceI>> map = AlignmentUtils
117 .getSequencesByName(al);
118 assertEquals(2, map.keySet().size());
119 assertEquals(2, map.get("Seq1Name").size());
120 assertEquals("KQYL", map.get("Seq1Name").get(0).getSequenceAsString());
121 assertEquals("ABCD", map.get("Seq1Name").get(1).getSequenceAsString());
122 assertEquals(1, map.get("Seq2Name").size());
123 assertEquals("RFPW", map.get("Seq2Name").get(0).getSequenceAsString());
126 * Helper method to load an alignment and ensure dataset sequences are set up.
131 * @throws IOException
133 protected AlignmentI loadAlignment(final String data, String format) throws IOException
135 Alignment a = new FormatAdapter().readFile(data,
136 AppletFormatAdapter.PASTE, format);
141 * Test mapping of protein to cDNA.
143 * @throws IOException
146 public void testMapProteinToCdna() throws IOException
148 // protein: Human + Mouse, 3 residues
149 AlignmentI protein = loadAlignment(
150 ">Human\nKQY\n>Mouse\nAFP\n>Worm\nRST\n",
152 // cDNA: Mouse, Human, Mouse, 9 bases
155 ">Mouse\nGAAATCCAG\n" +
156 ">Human\nTTCGATTAC\n" +
157 ">Mouse\nGTCGTTTGC\n" +
158 ">Mouse\nGTCGTTTGCgac\n" + // not mapped - wrong length
159 ">Fly\nGTCGTTTGC\n"; // not mapped - no name match
161 AlignmentI cdna1 = loadAlignment(
164 MappingResult mapped = AlignmentUtils.mapProteinToCdna(protein, cdna1);
165 assertEquals(mapped, MappingResult.Mapped);
168 * Check two mappings (one for Mouse, one for Human)
170 assertEquals(2, protein.getCodonFrames().length);
171 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).length);
172 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).length);
175 * Inspect mapping for Human protein
177 AlignedCodonFrame humanMapping = protein.getCodonFrame(protein
178 .getSequenceAt(0))[0];
179 assertEquals(1, humanMapping.getdnaSeqs().length);
180 assertEquals(cdna1.getSequenceAt(1).getDatasetSequence(),
181 humanMapping.getdnaSeqs()[0]);
182 Mapping[] protMappings = humanMapping.getProtMappings();
183 assertEquals(1, protMappings.length);
184 MapList mapList = protMappings[0].getMap();
185 assertEquals(3, mapList.getFromRatio());
186 assertEquals(1, mapList.getToRatio());
187 assertTrue(Arrays.equals(new int[]
188 { 1, 9 }, mapList.getFromRanges()));
189 assertTrue(Arrays.equals(new int[]
190 { 1, 3 }, mapList.getToRanges()));
193 * Inspect mappings for Mouse protein
195 AlignedCodonFrame mouseMapping1 = protein.getCodonFrame(protein
196 .getSequenceAt(1))[0];
197 assertEquals(2, mouseMapping1.getdnaSeqs().length);
198 assertEquals(cdna1.getSequenceAt(0).getDatasetSequence(),
199 mouseMapping1.getdnaSeqs()[0]);
200 assertEquals(cdna1.getSequenceAt(2).getDatasetSequence(),
201 mouseMapping1.getdnaSeqs()[1]);
202 protMappings = mouseMapping1.getProtMappings();
203 assertEquals(2, protMappings.length);
204 for (int i = 0; i < 2; i++)
206 mapList = protMappings[i].getMap();
207 assertEquals(3, mapList.getFromRatio());
208 assertEquals(1, mapList.getToRatio());
209 assertTrue(Arrays.equals(new int[]
210 { 1, 9 }, mapList.getFromRanges()));
211 assertTrue(Arrays.equals(new int[]
212 { 1, 3 }, mapList.getToRanges()));
217 * Test mapping of protein to cDNA which may include start and/or stop codons.
219 * @throws IOException
222 public void testMapProteinToCdna_stopStartCodons() throws IOException
224 // protein: Human + Mouse, 3 residues
225 AlignmentI protein = loadAlignment(
226 ">Human\nKQY\n>Mouse\nAFP\n>Worm\nRST\n", "FASTA");
229 ">Mouse\natgGAAATCCAG\n" + // Mouse with start codon
230 ">Human\nTTCGATtactaa\n" + // Human with stop codon TAA
231 ">Mouse\nGTCGTTTGctaG\n" + // Mouse with stop codon TAG
232 ">Human\nGTCGTTTgctGa\n" + // Human with stop codon TGA
233 ">Mouse\nATGGTCGTTTGCtag\n"; // Mouse with start and stop codons
235 AlignmentI cdna1 = loadAlignment(
238 MappingResult mapped = AlignmentUtils.mapProteinToCdna(protein, cdna1);
239 assertEquals(mapped, MappingResult.Mapped);
242 * Check two mappings (one for Mouse, one for Human)
244 assertEquals(2, protein.getCodonFrames().length);
245 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).length);
246 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).length);
249 * Inspect mapping for Human protein - should map to 2nd and 4th cDNA seqs
251 AlignedCodonFrame humanMapping = protein.getCodonFrame(protein
252 .getSequenceAt(0))[0];
253 assertEquals(2, humanMapping.getdnaSeqs().length);
254 assertEquals(cdna1.getSequenceAt(1).getDatasetSequence(),
255 humanMapping.getdnaSeqs()[0]);
256 assertEquals(cdna1.getSequenceAt(3).getDatasetSequence(),
257 humanMapping.getdnaSeqs()[1]);
258 Mapping[] protMappings = humanMapping.getProtMappings();
259 // two mappings, both to cDNA with stop codon
260 assertEquals(2, protMappings.length);
261 MapList mapList = protMappings[0].getMap();
262 assertEquals(3, mapList.getFromRatio());
263 assertEquals(1, mapList.getToRatio());
264 assertTrue(Arrays.equals(new int[]
265 { 1, 9 }, mapList.getFromRanges()));
266 assertTrue(Arrays.equals(new int[]
267 { 1, 3 }, mapList.getToRanges()));
268 mapList = protMappings[1].getMap();
269 assertEquals(3, mapList.getFromRatio());
270 assertEquals(1, mapList.getToRatio());
271 assertTrue(Arrays.equals(new int[]
272 { 1, 9 }, mapList.getFromRanges()));
273 assertTrue(Arrays.equals(new int[]
274 { 1, 3 }, mapList.getToRanges()));
277 * Inspect mapping for Mouse protein - should map to 1st/3rd/5th cDNA seqs
279 AlignedCodonFrame mouseMapping = protein.getCodonFrame(protein
280 .getSequenceAt(1))[0];
281 assertEquals(3, mouseMapping.getdnaSeqs().length);
282 assertEquals(cdna1.getSequenceAt(0).getDatasetSequence(),
283 mouseMapping.getdnaSeqs()[0]);
284 assertEquals(cdna1.getSequenceAt(2).getDatasetSequence(),
285 mouseMapping.getdnaSeqs()[1]);
286 assertEquals(cdna1.getSequenceAt(4).getDatasetSequence(),
287 mouseMapping.getdnaSeqs()[2]);
290 protMappings = mouseMapping.getProtMappings();
291 assertEquals(3, protMappings.length);
293 // first mapping to cDNA with start codon
294 mapList = protMappings[0].getMap();
295 assertEquals(3, mapList.getFromRatio());
296 assertEquals(1, mapList.getToRatio());
297 assertTrue(Arrays.equals(new int[]
298 { 4, 12 }, mapList.getFromRanges()));
299 assertTrue(Arrays.equals(new int[]
300 { 1, 3 }, mapList.getToRanges()));
302 // second mapping to cDNA with stop codon
303 mapList = protMappings[1].getMap();
304 assertEquals(3, mapList.getFromRatio());
305 assertEquals(1, mapList.getToRatio());
306 assertTrue(Arrays.equals(new int[]
307 { 1, 9 }, mapList.getFromRanges()));
308 assertTrue(Arrays.equals(new int[]
309 { 1, 3 }, mapList.getToRanges()));
311 // third mapping to cDNA with start and stop codon
312 mapList = protMappings[2].getMap();
313 assertEquals(3, mapList.getFromRatio());
314 assertEquals(1, mapList.getToRatio());
315 assertTrue(Arrays.equals(new int[]
316 { 4, 12 }, mapList.getFromRanges()));
317 assertTrue(Arrays.equals(new int[]
318 { 1, 3 }, mapList.getToRanges()));
322 * Test for the alignSequenceAs method that takes two sequences and a mapping.
325 public void testAlignSequenceAs_withMapping_noIntrons()
328 * Simple case: no gaps in dna
330 SequenceI dna = new Sequence("Seq1", "GGGAAA");
331 dna.createDatasetSequence();
332 SequenceI protein = new Sequence("Seq1", "-A-L-");
333 protein.createDatasetSequence();
334 AlignedCodonFrame acf = new AlignedCodonFrame();
335 MapList map = new MapList(new int[]
338 acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
341 * No existing gaps in dna:
343 AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', false,
345 assertEquals("---GGG---AAA", dna.getSequenceAsString());
348 * Now introduce gaps in dna but ignore them when realigning.
350 dna.setSequence("-G-G-G-A-A-A-");
351 AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', false,
353 assertEquals("---GGG---AAA", dna.getSequenceAsString());
356 * Now include gaps in dna when realigning. First retaining 'mapped' gaps
357 * only, i.e. those within the exon region.
359 dna.setSequence("-G-G--G-A--A-A-");
360 AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', true,
362 assertEquals("---G-G--G---A--A-A", dna.getSequenceAsString());
365 * Include all gaps in dna when realigning (within and without the exon
366 * region). The leading gap, and the gaps between codons, are subsumed by
367 * the protein alignment gap.
369 dna.setSequence("-G-GG--AA-A-");
370 AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', true,
372 assertEquals("---G-GG---AA-A-", dna.getSequenceAsString());
375 * Include only unmapped gaps in dna when realigning (outside the exon
376 * region). The leading gap, and the gaps between codons, are subsumed by
377 * the protein alignment gap.
379 dna.setSequence("-G-GG--AA-A-");
380 AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', false,
382 assertEquals("---GGG---AAA-", dna.getSequenceAsString());
386 * Test for the alignSequenceAs method that takes two sequences and a mapping.
389 public void testAlignSequenceAs_withMapping_withIntrons()
392 * Simple case: no gaps in dna
394 SequenceI dna = new Sequence("Seq1", "GGGAAACCCTTTGGG");
395 dna.createDatasetSequence();
396 SequenceI protein = new Sequence("Seq1", "-A-L-");
397 protein.createDatasetSequence();
398 AlignedCodonFrame acf = new AlignedCodonFrame();
401 * Exons at codon 2 (AAA) and 4 (TTT)
403 MapList map = new MapList(new int[]
404 { 4, 6, 10, 12 }, new int[]
406 acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
409 * Align dna as "-A-L-". The protein 'gaps' follow the introns, i.e are
410 * placed immediately before the mapped codons.
412 AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', false,
414 assertEquals("GGG---AAACCC---TTTGGG", dna.getSequenceAsString());
417 * Add gaps to dna - but ignore when realigning.
419 dna.setSequence("-G-G-G--A--A---AC-CC-T-TT-GG-G-");
420 AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', false,
422 assertEquals("GGG---AAACCC---TTTGGG", dna.getSequenceAsString());
425 * Add gaps to dna - include within exons only when realigning.
427 dna.setSequence("-G-G-G--A--A---A-C-CC-T-TT-GG-G-");
428 AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', true,
430 assertEquals("GGG---A--A---ACCC---T-TTGGG", dna.getSequenceAsString());
433 * Include gaps outside exons only when realigning.
435 dna.setSequence("-G-G-G--A--A---A-C-CC-T-TT-GG-G-");
436 AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', false,
438 assertEquals("-G-G-G---AAA-C-CC---TTT-GG-G-", dna.getSequenceAsString());
441 * Include all gaps in dna when realigning.
443 dna.setSequence("-G-G-G--A--A---A-C-CC-T-TT-GG-G-");
444 AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', true,
446 assertEquals("-G-G-G---A--A---A-C-CC---T-TT-GG-G-",
447 dna.getSequenceAsString());
451 * Test for the case where not all of the protein sequence is mapped to cDNA.
454 public void testAlignSequenceAs_withMapping_withUnmappedProtein()
456 SequenceI dna = new Sequence("Seq1", "GGGAAACCCTTTGGG");
457 dna.createDatasetSequence();
458 SequenceI protein = new Sequence("Seq1", "-A-L-P-");
459 protein.createDatasetSequence();
460 AlignedCodonFrame acf = new AlignedCodonFrame();
463 * Exons at codon 2 (AAA) and 4 (TTT) mapped to A and P
465 MapList map = new MapList(new int[]
466 { 4, 6, 10, 12 }, new int[]
467 { 1, 1, 3, 3 }, 3, 1);
468 acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
471 * Align dna as "-A-L-P-". Currently, does nothing (aborts realignment).
472 * Change this test first if different behaviour wanted.
474 AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', false,
476 assertEquals("GGGAAACCCTTTGGG", dna.getSequenceAsString());