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.assertSame;
25 import static org.junit.Assert.assertTrue;
26 import jalview.analysis.AlignmentUtils.MappingResult;
27 import jalview.datamodel.AlignedCodonFrame;
28 import jalview.datamodel.Alignment;
29 import jalview.datamodel.AlignmentI;
30 import jalview.datamodel.Mapping;
31 import jalview.datamodel.Sequence;
32 import jalview.datamodel.SequenceI;
33 import jalview.io.AppletFormatAdapter;
34 import jalview.io.FormatAdapter;
35 import jalview.util.MapList;
37 import java.io.IOException;
38 import java.util.Arrays;
39 import java.util.Collections;
40 import java.util.List;
43 import org.junit.Test;
45 public class AlignmentUtilsTests
48 private static final String TEST_DATA =
50 "#=GS D.melanogaster.1 AC AY119185.1/838-902\n" +
51 "#=GS D.melanogaster.2 AC AC092237.1/57223-57161\n" +
52 "#=GS D.melanogaster.3 AC AY060611.1/560-627\n" +
53 "D.melanogaster.1 G.AGCC.CU...AUGAUCGA\n" +
54 "#=GR D.melanogaster.1 SS ................((((\n" +
55 "D.melanogaster.2 C.AUUCAACU.UAUGAGGAU\n" +
56 "#=GR D.melanogaster.2 SS ................((((\n" +
57 "D.melanogaster.3 G.UGGCGCU..UAUGACGCA\n" +
58 "#=GR D.melanogaster.3 SS (.(((...(....(((((((\n" +
61 private static final String AA_SEQS_1 =
67 private static final String CDNA_SEQS_1 =
69 "AC-GG--CUC-CAA-CT\n" +
71 "-CG-TTA--ACG---AAGT\n";
73 private static final String CDNA_SEQS_2 =
80 public static Sequence ts=new Sequence("short","ASDASDASDASDASDASDASDASDASDASDASDASDASD");
83 public void testExpandFlanks()
85 AlignmentI al = new Alignment(new Sequence[] {});
86 for (int i=4;i<14;i+=3)
88 SequenceI s1=ts.deriveSequence().getSubSequence(i, i+7);
91 System.out.println(new AppletFormatAdapter().formatSequences("Clustal", al, true));
92 for (int flnk=-1;flnk<25; flnk++)
95 System.out.println("\nFlank size: "+flnk);
96 System.out.println(new AppletFormatAdapter().formatSequences("Clustal", exp=AlignmentUtils.expandContext(al, flnk), true));
98 for (SequenceI sq:exp.getSequences())
100 String ung = sq.getSequenceAsString().replaceAll("-+", "");
101 assertTrue("Flanking sequence not the same as original dataset sequence.\n"+ung+"\n"+sq.getDatasetSequence().getSequenceAsString(),ung.equalsIgnoreCase(sq.getDatasetSequence().getSequenceAsString()));
108 * Test method that returns a map of lists of sequences by sequence name.
110 * @throws IOException
113 public void testGetSequencesByName() throws IOException
115 final String data = ">Seq1Name\nKQYL\n" + ">Seq2Name\nRFPW\n"
116 + ">Seq1Name\nABCD\n";
117 AlignmentI al = loadAlignment(data, "FASTA");
118 Map<String, List<SequenceI>> map = AlignmentUtils
119 .getSequencesByName(al);
120 assertEquals(2, map.keySet().size());
121 assertEquals(2, map.get("Seq1Name").size());
122 assertEquals("KQYL", map.get("Seq1Name").get(0).getSequenceAsString());
123 assertEquals("ABCD", map.get("Seq1Name").get(1).getSequenceAsString());
124 assertEquals(1, map.get("Seq2Name").size());
125 assertEquals("RFPW", map.get("Seq2Name").get(0).getSequenceAsString());
128 * Helper method to load an alignment and ensure dataset sequences are set up.
133 * @throws IOException
135 protected AlignmentI loadAlignment(final String data, String format) throws IOException
137 Alignment a = new FormatAdapter().readFile(data,
138 AppletFormatAdapter.PASTE, format);
143 * Test mapping of protein to cDNA.
145 * @throws IOException
148 public void testMapProteinToCdna() throws IOException
150 // protein: Human + Mouse, 3 residues
151 AlignmentI protein = loadAlignment(
152 ">Human\nKQY\n>Mouse\nAFP\n>Worm\nRST\n",
154 // cDNA: Mouse, Human, Mouse, 9 bases
157 ">Mouse\nGAAATCCAG\n" +
158 ">Human\nTTCGATTAC\n" +
159 ">Mouse\nGTCGTTTGC\n" +
160 ">Mouse\nGTCGTTTGCgac\n" + // not mapped - wrong length
161 ">Fly\nGTCGTTTGC\n"; // not mapped - no name match
163 AlignmentI cdna1 = loadAlignment(
166 MappingResult mapped = AlignmentUtils.mapProteinToCdna(protein, cdna1);
167 assertEquals(mapped, MappingResult.Mapped);
170 * Check two mappings (one for Mouse, one for Human)
172 assertEquals(2, protein.getCodonFrames().size());
173 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
174 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
177 * Inspect mapping for Human protein
179 AlignedCodonFrame humanMapping = protein.getCodonFrame(
180 protein.getSequenceAt(0)).get(0);
181 assertEquals(1, humanMapping.getdnaSeqs().length);
182 assertEquals(cdna1.getSequenceAt(1).getDatasetSequence(),
183 humanMapping.getdnaSeqs()[0]);
184 Mapping[] protMappings = humanMapping.getProtMappings();
185 assertEquals(1, protMappings.length);
186 MapList mapList = protMappings[0].getMap();
187 assertEquals(3, mapList.getFromRatio());
188 assertEquals(1, mapList.getToRatio());
189 assertTrue(Arrays.equals(new int[]
190 { 1, 9 }, mapList.getFromRanges().get(0)));
191 assertEquals(1, mapList.getFromRanges().size());
192 assertTrue(Arrays.equals(new int[]
193 { 1, 3 }, mapList.getToRanges().get(0)));
194 assertEquals(1, mapList.getToRanges().size());
197 * Inspect mappings for Mouse protein
199 AlignedCodonFrame mouseMapping1 = protein.getCodonFrame(
200 protein.getSequenceAt(1)).get(0);
201 assertEquals(2, mouseMapping1.getdnaSeqs().length);
202 assertEquals(cdna1.getSequenceAt(0).getDatasetSequence(),
203 mouseMapping1.getdnaSeqs()[0]);
204 assertEquals(cdna1.getSequenceAt(2).getDatasetSequence(),
205 mouseMapping1.getdnaSeqs()[1]);
206 protMappings = mouseMapping1.getProtMappings();
207 assertEquals(2, protMappings.length);
208 for (int i = 0; i < 2; i++)
210 mapList = protMappings[i].getMap();
211 assertEquals(3, mapList.getFromRatio());
212 assertEquals(1, mapList.getToRatio());
213 assertTrue(Arrays.equals(new int[]
214 { 1, 9 }, mapList.getFromRanges().get(0)));
215 assertEquals(1, mapList.getFromRanges().size());
216 assertTrue(Arrays.equals(new int[]
217 { 1, 3 }, mapList.getToRanges().get(0)));
218 assertEquals(1, mapList.getToRanges().size());
223 * Test mapping of protein to cDNA which may include start and/or stop codons.
225 * @throws IOException
228 public void testMapProteinToCdna_stopStartCodons() throws IOException
230 // protein: Human + Mouse, 3 residues
231 AlignmentI protein = loadAlignment(
232 ">Human\nKQY\n>Mouse\nAFP\n>Worm\nRST\n", "FASTA");
235 ">Mouse\natgGAAATCCAG\n" + // Mouse with start codon
236 ">Human\nTTCGATtactaa\n" + // Human with stop codon TAA
237 ">Mouse\nGTCGTTTGctaG\n" + // Mouse with stop codon TAG
238 ">Human\nGTCGTTTgctGa\n" + // Human with stop codon TGA
239 ">Mouse\nATGGTCGTTTGCtag\n"; // Mouse with start and stop codons
241 AlignmentI cdna1 = loadAlignment(
244 MappingResult mapped = AlignmentUtils.mapProteinToCdna(protein, cdna1);
245 assertEquals(mapped, MappingResult.Mapped);
248 * Check two mappings (one for Mouse, one for Human)
250 assertEquals(2, protein.getCodonFrames().size());
251 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
252 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
255 * Inspect mapping for Human protein - should map to 2nd and 4th cDNA seqs
257 AlignedCodonFrame humanMapping = protein.getCodonFrame(
258 protein.getSequenceAt(0)).get(0);
259 assertEquals(2, humanMapping.getdnaSeqs().length);
260 assertEquals(cdna1.getSequenceAt(1).getDatasetSequence(),
261 humanMapping.getdnaSeqs()[0]);
262 assertEquals(cdna1.getSequenceAt(3).getDatasetSequence(),
263 humanMapping.getdnaSeqs()[1]);
264 Mapping[] protMappings = humanMapping.getProtMappings();
265 // two mappings, both to cDNA with stop codon
266 assertEquals(2, protMappings.length);
268 MapList mapList = protMappings[0].getMap();
269 assertEquals(3, mapList.getFromRatio());
270 assertEquals(1, mapList.getToRatio());
271 assertTrue(Arrays.equals(new int[]
272 { 1, 9 }, mapList.getFromRanges().get(0)));
273 assertEquals(1, mapList.getFromRanges().size());
274 assertTrue(Arrays.equals(new int[]
275 { 1, 3 }, mapList.getToRanges().get(0)));
276 assertEquals(1, mapList.getToRanges().size());
278 mapList = protMappings[1].getMap();
279 assertEquals(3, mapList.getFromRatio());
280 assertEquals(1, mapList.getToRatio());
281 assertTrue(Arrays.equals(new int[]
282 { 1, 9 }, mapList.getFromRanges().get(0)));
283 assertEquals(1, mapList.getFromRanges().size());
284 assertTrue(Arrays.equals(new int[]
285 { 1, 3 }, mapList.getToRanges().get(0)));
286 assertEquals(1, mapList.getToRanges().size());
289 * Inspect mapping for Mouse protein - should map to 1st/3rd/5th cDNA seqs
291 AlignedCodonFrame mouseMapping = protein.getCodonFrame(
292 protein.getSequenceAt(1)).get(0);
293 assertEquals(3, mouseMapping.getdnaSeqs().length);
294 assertEquals(cdna1.getSequenceAt(0).getDatasetSequence(),
295 mouseMapping.getdnaSeqs()[0]);
296 assertEquals(cdna1.getSequenceAt(2).getDatasetSequence(),
297 mouseMapping.getdnaSeqs()[1]);
298 assertEquals(cdna1.getSequenceAt(4).getDatasetSequence(),
299 mouseMapping.getdnaSeqs()[2]);
302 protMappings = mouseMapping.getProtMappings();
303 assertEquals(3, protMappings.length);
305 // first mapping to cDNA with start codon
306 mapList = protMappings[0].getMap();
307 assertEquals(3, mapList.getFromRatio());
308 assertEquals(1, mapList.getToRatio());
309 assertTrue(Arrays.equals(new int[]
310 { 4, 12 }, mapList.getFromRanges().get(0)));
311 assertEquals(1, mapList.getFromRanges().size());
312 assertTrue(Arrays.equals(new int[]
313 { 1, 3 }, mapList.getToRanges().get(0)));
314 assertEquals(1, mapList.getToRanges().size());
316 // second mapping to cDNA with stop codon
317 mapList = protMappings[1].getMap();
318 assertEquals(3, mapList.getFromRatio());
319 assertEquals(1, mapList.getToRatio());
320 assertTrue(Arrays.equals(new int[]
321 { 1, 9 }, mapList.getFromRanges().get(0)));
322 assertEquals(1, mapList.getFromRanges().size());
323 assertTrue(Arrays.equals(new int[]
324 { 1, 3 }, mapList.getToRanges().get(0)));
325 assertEquals(1, mapList.getToRanges().size());
327 // third mapping to cDNA with start and stop codon
328 mapList = protMappings[2].getMap();
329 assertEquals(3, mapList.getFromRatio());
330 assertEquals(1, mapList.getToRatio());
331 assertTrue(Arrays.equals(new int[]
332 { 4, 12 }, mapList.getFromRanges().get(0)));
333 assertEquals(1, mapList.getFromRanges().size());
334 assertTrue(Arrays.equals(new int[]
335 { 1, 3 }, mapList.getToRanges().get(0)));
336 assertEquals(1, mapList.getToRanges().size());
340 * Test for the alignSequenceAs method that takes two sequences and a mapping.
343 public void testAlignSequenceAs_withMapping_noIntrons()
345 MapList map = new MapList(new int[]
350 * No existing gaps in dna:
352 checkAlignSequenceAs("GGGAAA", "-A-L-", false, false, map,
356 * Now introduce gaps in dna but ignore them when realigning.
358 checkAlignSequenceAs("-G-G-G-A-A-A-", "-A-L-", false, false, map,
362 * Now include gaps in dna when realigning. First retaining 'mapped' gaps
363 * only, i.e. those within the exon region.
365 checkAlignSequenceAs("-G-G--G-A--A-A-", "-A-L-", true, false, map,
366 "---G-G--G---A--A-A");
369 * Include all gaps in dna when realigning (within and without the exon
370 * region). The leading gap, and the gaps between codons, are subsumed by
371 * the protein alignment gap.
373 checkAlignSequenceAs("-G-GG--AA-A-", "-A-L-", true, true, map,
377 * Include only unmapped gaps in dna when realigning (outside the exon
378 * region). The leading gap, and the gaps between codons, are subsumed by
379 * the protein alignment gap.
381 checkAlignSequenceAs("-G-GG--AA-A-", "-A-L-", false, true, map,
386 * Test for the alignSequenceAs method that takes two sequences and a mapping.
389 public void testAlignSequenceAs_withMapping_withIntrons()
392 * Exons at codon 2 (AAA) and 4 (TTT)
394 MapList map = new MapList(new int[]
395 { 4, 6, 10, 12 }, new int[]
399 * Simple case: no gaps in dna
401 checkAlignSequenceAs("GGGAAACCCTTTGGG", "--A-L-", false, false, map,
402 "GGG---AAACCCTTTGGG");
405 * Add gaps to dna - but ignore when realigning.
407 checkAlignSequenceAs("-G-G-G--A--A---AC-CC-T-TT-GG-G-", "--A-L-",
408 false, false, map, "GGG---AAACCCTTTGGG");
411 * Add gaps to dna - include within exons only when realigning.
413 checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
414 true, false, map, "GGG---A--A---ACCCT-TTGGG");
417 * Include gaps outside exons only when realigning.
419 checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
420 false, true, map, "-G-G-GAAAC-CCTTT-GG-G-");
423 * Include gaps following first intron if we are 'preserving mapped gaps'
425 checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
426 true, true, map, "-G-G-G--A--A---A-C-CC-T-TT-GG-G-");
429 * Include all gaps in dna when realigning.
431 checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
432 true, true, map, "-G-G-G--A--A---A-C-CC-T-TT-GG-G-");
436 * Test for the case where not all of the protein sequence is mapped to cDNA.
439 public void testAlignSequenceAs_withMapping_withUnmappedProtein()
443 * Exons at codon 2 (AAA) and 4 (TTT) mapped to A and P
445 final MapList map = new MapList(new int[]
446 { 4, 6, 10, 12 }, new int[]
447 { 1, 1, 3, 3 }, 3, 1);
451 * Expect alignment does nothing (aborts realignment). Change this test
452 * first if different behaviour wanted.
454 checkAlignSequenceAs("GGGAAACCCTTTGGG", "-A-L-P-", false,
455 false, map, "GGGAAACCCTTTGGG");
459 * Helper method that performs and verifies the method under test.
463 * @param preserveMappedGaps
464 * @param preserveUnmappedGaps
468 protected void checkAlignSequenceAs(final String dnaSeq,
469 final String proteinSeq, final boolean preserveMappedGaps,
470 final boolean preserveUnmappedGaps, MapList map,
471 final String expected)
473 SequenceI dna = new Sequence("Seq1", dnaSeq);
474 dna.createDatasetSequence();
475 SequenceI protein = new Sequence("Seq1", proteinSeq);
476 protein.createDatasetSequence();
477 AlignedCodonFrame acf = new AlignedCodonFrame();
478 acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
480 AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-',
481 preserveMappedGaps, preserveUnmappedGaps);
482 assertEquals(expected, dna.getSequenceAsString());
486 * Test for the alignSequenceAs method where we preserve gaps in introns only.
489 public void testAlignSequenceAs_keepIntronGapsOnly()
493 * Intron GGGAAA followed by exon CCCTTT
495 MapList map = new MapList(new int[]
499 checkAlignSequenceAs("GG-G-AA-A-C-CC-T-TT", "AL",
500 false, true, map, "GG-G-AA-ACCCTTT");
504 * Test for the method that generates an aligned translated sequence from one
508 public void testGetAlignedTranslation_dnaLikeProtein()
510 // dna alignment will be replaced
511 SequenceI dna = new Sequence("Seq1", "T-G-CC-A--T-TAC-CAG-");
512 dna.createDatasetSequence();
513 // protein alignment will be 'applied' to dna
514 SequenceI protein = new Sequence("Seq1", "-CH-Y--Q-");
515 protein.createDatasetSequence();
516 MapList map = new MapList(new int[]
519 AlignedCodonFrame acf = new AlignedCodonFrame();
520 acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
522 final SequenceI aligned = AlignmentUtils
523 .getAlignedTranslation(protein, '-', acf);
524 assertEquals("---TGCCAT---TAC------CAG---", aligned.getSequenceAsString());
525 assertSame(aligned.getDatasetSequence(), dna.getDatasetSequence());
529 * Test the method that realigns protein to match mapped codon alignment.
532 public void testAlignProteinAsDna()
534 // seq1 codons are [1,2,3] [4,5,6] [7,8,9] [10,11,12]
535 SequenceI dna1 = new Sequence("Seq1", "TGCCATTACCAG-");
536 // seq2 codons are [1,3,4] [5,6,7] [8,9,10] [11,12,13]
537 SequenceI dna2 = new Sequence("Seq2", "T-GCCATTACCAG");
538 // seq3 codons are [1,2,3] [4,5,7] [8,9,10] [11,12,13]
539 SequenceI dna3 = new Sequence("Seq3", "TGCCA-TTACCAG");
540 AlignmentI dna = new Alignment(new SequenceI[]
541 { dna1, dna2, dna3 });
542 dna.setDataset(null);
544 // protein alignment will be realigned like dna
545 SequenceI prot1 = new Sequence("Seq1", "CHYQ");
546 SequenceI prot2 = new Sequence("Seq2", "CHYQ");
547 SequenceI prot3 = new Sequence("Seq3", "CHYQ");
548 AlignmentI protein = new Alignment(new SequenceI[]
549 { prot1, prot2, prot3 });
550 protein.setDataset(null);
552 MapList map = new MapList(new int[]
555 AlignedCodonFrame acf = new AlignedCodonFrame();
556 acf.addMap(dna1.getDatasetSequence(), prot1.getDatasetSequence(), map);
557 acf.addMap(dna2.getDatasetSequence(), prot2.getDatasetSequence(), map);
558 acf.addMap(dna3.getDatasetSequence(), prot3.getDatasetSequence(), map);
559 protein.setCodonFrames(Collections.singleton(acf));
562 * Translated codon order is [1,2,3] [1,3,4] [4,5,6] [4,5,7] [5,6,7] [7,8,9]
563 * [8,9,10] [10,11,12] [11,12,13]
565 AlignmentUtils.alignProteinAsDna(protein, dna);
566 assertEquals("C-H--Y-Q-", prot1.getSequenceAsString());
567 assertEquals("-C--H-Y-Q", prot2.getSequenceAsString());
568 assertEquals("C--H--Y-Q", prot3.getSequenceAsString());