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.assertNotNull;
26 import static org.testng.AssertJUnit.assertNull;
27 import static org.testng.AssertJUnit.assertSame;
28 import static org.testng.AssertJUnit.assertTrue;
30 import jalview.datamodel.AlignedCodonFrame.SequenceToSequenceMapping;
31 import jalview.io.AppletFormatAdapter;
32 import jalview.io.FormatAdapter;
33 import jalview.util.MapList;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.Iterator;
39 import java.util.List;
41 import org.testng.Assert;
42 import org.testng.annotations.BeforeMethod;
43 import org.testng.annotations.Test;
46 * Unit tests for Alignment datamodel.
51 public class AlignmentTest
54 private static final String TEST_DATA =
56 "#=GS D.melanogaster.1 AC AY119185.1/838-902\n" +
57 "#=GS D.melanogaster.2 AC AC092237.1/57223-57161\n" +
58 "#=GS D.melanogaster.3 AC AY060611.1/560-627\n" +
59 "D.melanogaster.1 G.AGCC.CU...AUGAUCGA\n" +
60 "#=GR D.melanogaster.1 SS ................((((\n" +
61 "D.melanogaster.2 C.AUUCAACU.UAUGAGGAU\n" +
62 "#=GR D.melanogaster.2 SS ................((((\n" +
63 "D.melanogaster.3 G.UGGCGCU..UAUGACGCA\n" +
64 "#=GR D.melanogaster.3 SS (.(((...(....(((((((\n" +
67 private static final String AA_SEQS_1 =
73 private static final String CDNA_SEQS_1 =
74 ">Seq1Name/100-111\n" +
75 "AC-GG--CUC-CAA-CT\n" +
76 ">Seq2Name/200-211\n" +
77 "-CG-TTA--ACG---AAGT\n";
79 private static final String CDNA_SEQS_2 =
86 private AlignmentI al;
89 * Helper method to load an alignment and ensure dataset sequences are set up.
97 protected AlignmentI loadAlignment(final String data, String format)
100 AlignmentI a = new FormatAdapter().readFile(data,
101 AppletFormatAdapter.PASTE, format);
107 * assert wrapper: tests all references in the given alignment are consistent
111 public static void assertAlignmentDatasetRefs(AlignmentI alignment)
113 verifyAlignmentDatasetRefs(alignment, true, null);
117 * assert wrapper: tests all references in the given alignment are consistent
121 * - prefixed to any assert failed messages
123 public static void assertAlignmentDatasetRefs(AlignmentI alignment,
126 verifyAlignmentDatasetRefs(alignment, true, message);
130 * verify sequence and dataset references are properly contained within
134 * - the alignmentI object to verify (either alignment or dataset)
136 * - when set, testng assertions are raised.
138 * - null or a string message to prepend to the assert failed messages.
139 * @return true if alignment references were in order, otherwise false.
141 public static boolean verifyAlignmentDatasetRefs(AlignmentI alignment,
142 boolean raiseAssert, String message)
144 if (message==null) { message = ""; }
145 if (alignment == null)
149 Assert.fail(message+"Alignment for verification was null.");
153 if (alignment.getDataset() != null)
155 AlignmentI dataset = alignment.getDataset();
156 // check all alignment sequences have their dataset within the dataset
157 for (SequenceI seq : alignment.getSequences())
159 SequenceI seqds = seq.getDatasetSequence();
160 if (seqds.getDatasetSequence() != null)
164 Assert.fail(message+" Alignment contained a sequence who's dataset sequence has a second dataset reference.");
168 if (dataset.findIndex(seqds) == -1)
172 Assert.fail(message+" Alignment contained a sequence who's dataset sequence was not in the dataset.");
177 return verifyAlignmentDatasetRefs(alignment.getDataset(), raiseAssert, message);
181 // verify all dataset sequences
182 for (SequenceI seqds : alignment.getSequences())
184 if (seqds.getDatasetSequence() != null)
188 Assert.fail(message+" Dataset contained a sequence with non-null dataset reference (ie not a dataset sequence!)");
192 if (seqds.getDBRefs() != null)
194 for (DBRefEntry dbr : seqds.getDBRefs())
196 if (dbr.getMap() != null)
198 SequenceI seqdbrmapto = dbr.getMap().getTo();
199 if (seqdbrmapto != null)
201 if (seqdbrmapto.getDatasetSequence() != null)
205 Assert.fail(message+" DBRefEntry for sequence in alignment had map to sequence which was not a dataset sequence");
210 if (alignment.findIndex(dbr.getMap().getTo()) == -1)
214 Assert.fail(message+" DBRefEntry for sequence in alignment had map to sequence not in dataset");
223 // finally, verify codonmappings involve only dataset sequences.
224 if (alignment.getCodonFrames() != null)
226 for (AlignedCodonFrame alc : alignment.getCodonFrames())
228 for (SequenceToSequenceMapping ssm : alc.getMappings())
230 if (ssm.getFromSeq().getDatasetSequence() != null)
234 Assert.fail(message+" CodonFrame-SSM-FromSeq is not a dataset sequence");
238 if (alignment.findIndex(ssm.getFromSeq()) == -1)
243 Assert.fail(message+" CodonFrame-SSM-FromSeq is not contained in dataset");
247 if (ssm.getMapping().getTo().getDatasetSequence() != null)
251 Assert.fail(message+" CodonFrame-SSM-Mapping-ToSeq is not a dataset sequence");
255 if (alignment.findIndex(ssm.getMapping().getTo()) == -1)
260 Assert.fail(message+" CodonFrame-SSM-Mapping-ToSeq is not contained in dataset");
268 return true; // all relationships verified!
272 * call verifyAlignmentDatasetRefs with and without assertion raising enabled,
273 * to check expected pass/fail actually occurs in both conditions
279 private void assertVerifyAlignment(AlignmentI al, boolean expected,
287 Assert.assertTrue(verifyAlignmentDatasetRefs(al, true, null),
288 "Valid test alignment failed when raiseAsserts enabled:"
290 } catch (AssertionError ae)
292 ae.printStackTrace();
294 "Valid test alignment raised assertion errors when raiseAsserts enabled: "
297 // also check validation passes with asserts disabled
298 Assert.assertTrue(verifyAlignmentDatasetRefs(al, false, null),
299 "Valid test alignment failed when raiseAsserts disabled:"
306 Assert.assertFalse(verifyAlignmentDatasetRefs(al, true, null));
307 Assert.fail("Invalid test alignment passed but no assertion raised when raiseAsserts enabled:"
309 } catch (AssertionError ae)
311 // expected behaviour
313 // also check validation passes with asserts disabled
314 Assert.assertFalse(verifyAlignmentDatasetRefs(al, false, null),
315 "Invalid test alignment passed when raiseAsserts disabled:"
319 @Test(groups = { "Functional" })
320 public void testVerifyAlignmentDatasetRefs()
322 SequenceI sq1 = new Sequence("sq1", "ASFDD"), sq2 = new Sequence("sq2",
325 // construct simple valid alignment dataset
326 Alignment al = new Alignment(new SequenceI[] {
328 // expect this to pass
329 assertVerifyAlignment(al, true, "Simple valid alignment didn't verify");
331 // check test for sequence->datasetSequence validity
332 sq1.setDatasetSequence(sq2);
333 assertVerifyAlignment(
336 "didn't detect dataset sequence with a dataset sequence reference.");
338 sq1.setDatasetSequence(null);
339 assertVerifyAlignment(
342 "didn't reinstate validity after nulling dataset sequence dataset reference");
344 // now create dataset and check again
345 al.createDatasetAlignment();
346 assertNotNull(al.getDataset());
348 assertVerifyAlignment(al, true,
349 "verify failed after createDatasetAlignment");
351 // create a dbref on sq1 with a sequence ref to sq2
352 DBRefEntry dbrs1tos2 = new DBRefEntry("UNIPROT", "1", "Q111111");
353 dbrs1tos2.setMap(new Mapping(sq2.getDatasetSequence(),
354 new int[] { 1, 5 }, new int[] { 2, 6 }, 1, 1));
355 sq1.getDatasetSequence().addDBRef(dbrs1tos2);
356 assertVerifyAlignment(al, true,
357 "verify failed after addition of valid DBRefEntry/map");
358 // now create a dbref on a new sequence which maps to another sequence
359 // outside of the dataset
360 SequenceI sqout = new Sequence("sqout", "ututututucagcagcag"), sqnew = new Sequence(
362 DBRefEntry sqnewsqout = new DBRefEntry("ENAFOO", "1", "R000001");
363 sqnewsqout.setMap(new Mapping(sqout, new int[] { 1, 6 }, new int[] { 1,
365 al.getDataset().addSequence(sqnew);
367 assertVerifyAlignment(al, true,
368 "verify failed after addition of new sequence to dataset");
369 // now start checking exception conditions
370 sqnew.addDBRef(sqnewsqout);
371 assertVerifyAlignment(
374 "verify passed when a dbref with map to sequence outside of dataset was added");
375 // make the verify pass by adding the outsider back in
376 al.getDataset().addSequence(sqout);
377 assertVerifyAlignment(al, true,
378 "verify should have passed after adding dbref->to sequence in to dataset");
379 // and now the same for a codon mapping...
380 SequenceI sqanotherout = new Sequence("sqanotherout",
381 "aggtutaggcagcagcag");
383 AlignedCodonFrame alc = new AlignedCodonFrame();
384 alc.addMap(sqanotherout, sqnew, new MapList(new int[] { 1, 6 },
385 new int[] { 1, 18 }, 3, 1));
387 al.addCodonFrame(alc);
388 Assert.assertEquals(al.getDataset().getCodonFrames().size(), 1);
390 assertVerifyAlignment(
393 "verify passed when alCodonFrame mapping to sequence outside of dataset was added");
394 // make the verify pass by adding the outsider back in
395 al.getDataset().addSequence(sqanotherout);
396 assertVerifyAlignment(
399 "verify should have passed once all sequences involved in alCodonFrame were added to dataset");
403 * Read in Stockholm format test data including secondary structure
406 @BeforeMethod(alwaysRun = true)
407 public void setUp() throws IOException
409 al = loadAlignment(TEST_DATA, "STH");
411 for (AlignmentAnnotation ann : al.getAlignmentAnnotation())
413 ann.setCalcId("CalcIdFor" + al.getSequenceAt(i).getName());
419 * Test method that returns annotations that match on calcId.
421 @Test(groups = { "Functional" })
422 public void testFindAnnotation_byCalcId()
424 Iterable<AlignmentAnnotation> anns = al
425 .findAnnotation("CalcIdForD.melanogaster.2");
426 Iterator<AlignmentAnnotation> iter = anns.iterator();
427 assertTrue(iter.hasNext());
428 AlignmentAnnotation ann = iter.next();
429 assertEquals("D.melanogaster.2", ann.sequenceRef.getName());
430 assertFalse(iter.hasNext());
433 @Test(groups = { "Functional" })
434 public void testDeleteAllAnnotations_includingAutocalculated()
436 AlignmentAnnotation aa = new AlignmentAnnotation("Consensus",
438 aa.autoCalculated = true;
439 al.addAnnotation(aa);
440 AlignmentAnnotation[] anns = al.getAlignmentAnnotation();
441 assertEquals("Wrong number of annotations before deleting", 4,
443 al.deleteAllAnnotations(true);
444 assertEquals("Not all deleted", 0, al.getAlignmentAnnotation().length);
447 @Test(groups = { "Functional" })
448 public void testDeleteAllAnnotations_excludingAutocalculated()
450 AlignmentAnnotation aa = new AlignmentAnnotation("Consensus",
452 aa.autoCalculated = true;
453 al.addAnnotation(aa);
454 AlignmentAnnotation[] anns = al.getAlignmentAnnotation();
455 assertEquals("Wrong number of annotations before deleting", 4,
457 al.deleteAllAnnotations(false);
458 assertEquals("Not just one annotation left", 1,
459 al.getAlignmentAnnotation().length);
463 * Tests for realigning as per a supplied alignment: Dna as Dna.
465 * Note: AlignedCodonFrame's state variables are named for protein-to-cDNA
466 * mapping, but can be exploited for a general 'sequence-to-sequence' mapping
469 * @throws IOException
471 @Test(groups = { "Functional" })
472 public void testAlignAs_dnaAsDna() throws IOException
475 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
477 AlignmentI al2 = loadAlignment(CDNA_SEQS_2, "FASTA");
480 * Make mappings between sequences. The 'aligned cDNA' is playing the role
481 * of what would normally be protein here.
483 makeMappings(al1, al2);
485 ((Alignment) al2).alignAs(al1, false, true);
486 assertEquals("GC-TC--GUC-GTACT", al2.getSequenceAt(0)
487 .getSequenceAsString());
488 assertEquals("-GG-GTC--AGG--CAGT", al2.getSequenceAt(1)
489 .getSequenceAsString());
493 * Aligning protein from cDNA.
495 * @throws IOException
497 @Test(groups = { "Functional" })
498 public void testAlignAs_proteinAsCdna() throws IOException
500 // see also AlignmentUtilsTests
501 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
502 AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA");
503 makeMappings(al1, al2);
505 // Fudge - alignProteinAsCdna expects mappings to be on protein
506 al2.getCodonFrames().addAll(al1.getCodonFrames());
508 ((Alignment) al2).alignAs(al1, false, true);
509 assertEquals("K-Q-Y-L-", al2.getSequenceAt(0).getSequenceAsString());
510 assertEquals("-R-F-P-W", al2.getSequenceAt(1).getSequenceAsString());
514 * Test aligning cdna as per protein alignment.
516 * @throws IOException
518 @Test(groups = { "Functional" }, enabled = true)
519 // TODO review / update this test after redesign of alignAs method
520 public void testAlignAs_cdnaAsProtein() throws IOException
523 * Load alignments and add mappings for cDNA to protein
525 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
526 AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA");
527 makeMappings(al1, al2);
530 * Realign DNA; currently keeping existing gaps in introns only
532 ((Alignment) al1).alignAs(al2, false, true);
533 assertEquals("ACG---GCUCCA------ACT---", al1.getSequenceAt(0)
534 .getSequenceAsString());
535 assertEquals("---CGT---TAACGA---AGT---", al1.getSequenceAt(1)
536 .getSequenceAsString());
540 * Test aligning cdna as per protein - single sequences
542 * @throws IOException
544 @Test(groups = { "Functional" }, enabled = true)
545 // TODO review / update this test after redesign of alignAs method
546 public void testAlignAs_cdnaAsProtein_singleSequence() throws IOException
549 * simple case insert one gap
551 verifyAlignAs(">dna\nCAAaaa\n", ">protein\nQ-K\n", "CAA---aaa");
554 * simple case but with sequence offsets
556 verifyAlignAs(">dna/5-10\nCAAaaa\n", ">protein/20-21\nQ-K\n",
560 * insert gaps as per protein, drop gaps within codons
562 verifyAlignAs(">dna/10-18\nCA-Aa-aa--AGA\n", ">aa/6-8\n-Q-K--R\n",
563 "---CAA---aaa------AGA");
567 * Helper method that makes mappings and then aligns the first alignment as
573 * @throws IOException
575 public void verifyAlignAs(String fromSeqs, String toSeqs, String expected)
579 * Load alignments and add mappings from nucleotide to protein (or from
580 * first to second if both the same type)
582 AlignmentI al1 = loadAlignment(fromSeqs, "FASTA");
583 AlignmentI al2 = loadAlignment(toSeqs, "FASTA");
584 makeMappings(al1, al2);
587 * Realign DNA; currently keeping existing gaps in introns only
589 ((Alignment) al1).alignAs(al2, false, true);
590 assertEquals(expected, al1.getSequenceAt(0).getSequenceAsString());
594 * Helper method to make mappings between sequences, and add the mappings to
595 * the 'mapped from' alignment
600 public void makeMappings(AlignmentI alFrom, AlignmentI alTo)
602 int ratio = (alFrom.isNucleotide() == alTo.isNucleotide() ? 1 : 3);
604 AlignedCodonFrame acf = new AlignedCodonFrame();
606 for (int i = 0; i < alFrom.getHeight(); i++)
608 SequenceI seqFrom = alFrom.getSequenceAt(i);
609 SequenceI seqTo = alTo.getSequenceAt(i);
610 MapList ml = new MapList(new int[] { seqFrom.getStart(),
612 new int[] { seqTo.getStart(), seqTo.getEnd() }, ratio, 1);
613 acf.addMap(seqFrom, seqTo, ml);
617 * not sure whether mappings 'belong' or protein or nucleotide
618 * alignment, so adding to both ;~)
620 alFrom.addCodonFrame(acf);
621 alTo.addCodonFrame(acf);
625 * Test aligning dna as per protein alignment, for the case where there are
626 * introns (i.e. some dna sites have no mapping from a peptide).
628 * @throws IOException
630 @Test(groups = { "Functional" }, enabled = false)
631 // TODO review / update this test after redesign of alignAs method
632 public void testAlignAs_dnaAsProtein_withIntrons() throws IOException
635 * Load alignments and add mappings for cDNA to protein
637 String dna1 = "A-Aa-gG-GCC-cT-TT";
638 String dna2 = "c--CCGgg-TT--T-AA-A";
639 AlignmentI al1 = loadAlignment(">Dna1/6-17\n" + dna1
640 + "\n>Dna2/20-31\n" + dna2 + "\n", "FASTA");
641 AlignmentI al2 = loadAlignment(
642 ">Pep1/7-9\n-P--YK\n>Pep2/11-13\nG-T--F\n", "FASTA");
643 AlignedCodonFrame acf = new AlignedCodonFrame();
644 // Seq1 has intron at dna positions 3,4,9 so splice is AAG GCC TTT
645 // Seq2 has intron at dna positions 1,5,6 so splice is CCG TTT AAA
646 MapList ml1 = new MapList(new int[] { 6, 7, 10, 13, 15, 17 }, new int[]
648 acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml1);
649 MapList ml2 = new MapList(new int[] { 21, 23, 26, 31 }, new int[] { 11,
651 acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml2);
652 al2.addCodonFrame(acf);
655 * Align ignoring gaps in dna introns and exons
657 ((Alignment) al1).alignAs(al2, false, false);
658 assertEquals("---AAagG------GCCcTTT", al1.getSequenceAt(0)
659 .getSequenceAsString());
660 // note 1 gap in protein corresponds to 'gg-' in DNA (3 positions)
661 assertEquals("cCCGgg-TTT------AAA", al1.getSequenceAt(1)
662 .getSequenceAsString());
665 * Reset and realign, preserving gaps in dna introns and exons
667 al1.getSequenceAt(0).setSequence(dna1);
668 al1.getSequenceAt(1).setSequence(dna2);
669 ((Alignment) al1).alignAs(al2, true, true);
670 // String dna1 = "A-Aa-gG-GCC-cT-TT";
671 // String dna2 = "c--CCGgg-TT--T-AA-A";
672 // assumption: we include 'the greater of' protein/dna gap lengths, not both
673 assertEquals("---A-Aa-gG------GCC-cT-TT", al1.getSequenceAt(0)
674 .getSequenceAsString());
675 assertEquals("c--CCGgg-TT--T------AA-A", al1.getSequenceAt(1)
676 .getSequenceAsString());
679 @Test(groups = "Functional")
680 public void testCopyConstructor() throws IOException
682 AlignmentI protein = loadAlignment(AA_SEQS_1, FormatAdapter.PASTE);
683 // create sequence and alignment datasets
684 protein.setDataset(null);
685 AlignedCodonFrame acf = new AlignedCodonFrame();
686 List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
688 protein.getDataset().setCodonFrames(acfList);
689 AlignmentI copy = new Alignment(protein);
692 * copy has different aligned sequences but the same dataset sequences
694 assertFalse(copy.getSequenceAt(0) == protein.getSequenceAt(0));
695 assertFalse(copy.getSequenceAt(1) == protein.getSequenceAt(1));
696 assertSame(copy.getSequenceAt(0).getDatasetSequence(), protein
697 .getSequenceAt(0).getDatasetSequence());
698 assertSame(copy.getSequenceAt(1).getDatasetSequence(), protein
699 .getSequenceAt(1).getDatasetSequence());
701 // TODO should the copy constructor copy the dataset?
702 // or make a new one referring to the same dataset sequences??
703 assertNull(copy.getDataset());
704 // TODO test metadata is copied when AlignmentI is a dataset
706 // assertArrayEquals(copy.getDataset().getSequencesArray(), protein
707 // .getDataset().getSequencesArray());
711 * Test behaviour of createDataset
713 * @throws IOException
715 @Test(groups = "Functional")
716 public void testCreateDatasetAlignment() throws IOException
718 AlignmentI protein = new FormatAdapter().readFile(AA_SEQS_1,
719 AppletFormatAdapter.PASTE, "FASTA");
721 * create a dataset sequence on first sequence
722 * leave the second without one
724 protein.getSequenceAt(0).createDatasetSequence();
725 assertNotNull(protein.getSequenceAt(0).getDatasetSequence());
726 assertNull(protein.getSequenceAt(1).getDatasetSequence());
729 * add a mapping to the alignment
731 AlignedCodonFrame acf = new AlignedCodonFrame();
732 protein.addCodonFrame(acf);
733 assertNull(protein.getDataset());
734 assertTrue(protein.getCodonFrames().contains(acf));
737 * create the alignment dataset
738 * note this creates sequence datasets where missing
739 * as a side-effect (in this case, on seq2
741 // TODO promote this method to AlignmentI
742 ((Alignment) protein).createDatasetAlignment();
744 AlignmentI ds = protein.getDataset();
746 // side-effect: dataset created on second sequence
747 assertNotNull(protein.getSequenceAt(1).getDatasetSequence());
748 // dataset alignment has references to dataset sequences
749 assertEquals(ds.getSequenceAt(0), protein.getSequenceAt(0)
750 .getDatasetSequence());
751 assertEquals(ds.getSequenceAt(1), protein.getSequenceAt(1)
752 .getDatasetSequence());
754 // codon frames should have been moved to the dataset
755 // getCodonFrames() should delegate to the dataset:
756 assertTrue(protein.getCodonFrames().contains(acf));
757 // prove the codon frames are indeed on the dataset:
758 assertTrue(ds.getCodonFrames().contains(acf));
762 * tests the addition of *all* sequences referred to by a sequence being added
765 @Test(groups = "Functional")
766 public void testCreateDatasetAlignmentWithMappedToSeqs()
768 // Alignment with two sequences, gapped.
769 SequenceI sq1 = new Sequence("sq1", "A--SDF");
770 SequenceI sq2 = new Sequence("sq2", "G--TRQ");
772 // cross-references to two more sequences.
773 DBRefEntry dbr = new DBRefEntry("SQ1", "", "sq3");
774 SequenceI sq3 = new Sequence("sq3", "VWANG");
775 dbr.setMap(new Mapping(sq3, new MapList(new int[] { 1, 4 }, new int[] {
779 SequenceI sq4 = new Sequence("sq4", "ERKWI");
780 DBRefEntry dbr2 = new DBRefEntry("SQ2", "", "sq4");
781 dbr2.setMap(new Mapping(sq4, new MapList(new int[] { 1, 4 }, new int[] {
784 // and a 1:1 codonframe mapping between them.
785 AlignedCodonFrame alc = new AlignedCodonFrame();
786 alc.addMap(sq1, sq2, new MapList(new int[] { 1, 4 },
787 new int[] { 1, 4 }, 1, 1));
789 AlignmentI protein = new Alignment(new SequenceI[] { sq1, sq2 });
792 * create the alignment dataset
793 * note this creates sequence datasets where missing
794 * as a side-effect (in this case, on seq2
797 // TODO promote this method to AlignmentI
798 ((Alignment) protein).createDatasetAlignment();
800 AlignmentI ds = protein.getDataset();
802 // should be 4 sequences in dataset - two materialised, and two propagated
804 assertEquals(4, ds.getHeight());
805 assertTrue(ds.getSequences().contains(sq1.getDatasetSequence()));
806 assertTrue(ds.getSequences().contains(sq2.getDatasetSequence()));
807 assertTrue(ds.getSequences().contains(sq3));
808 assertTrue(ds.getSequences().contains(sq4));
809 // Should have one codon frame mapping between sq1 and sq2 via dataset
811 assertEquals(ds.getCodonFrame(sq1.getDatasetSequence()),
812 ds.getCodonFrame(sq2.getDatasetSequence()));
815 @Test(groups = "Functional")
816 public void testAddCodonFrame()
818 AlignmentI align = new Alignment(new SequenceI[] {});
819 AlignedCodonFrame acf = new AlignedCodonFrame();
820 align.addCodonFrame(acf);
821 assertEquals(1, align.getCodonFrames().size());
822 assertTrue(align.getCodonFrames().contains(acf));
823 // can't add the same object twice:
824 align.addCodonFrame(acf);
825 assertEquals(1, align.getCodonFrames().size());
827 // create dataset alignment - mappings move to dataset
828 ((Alignment) align).createDatasetAlignment();
829 assertSame(align.getCodonFrames(), align.getDataset().getCodonFrames());
830 assertEquals(1, align.getCodonFrames().size());
832 AlignedCodonFrame acf2 = new AlignedCodonFrame();
833 align.addCodonFrame(acf2);
834 assertTrue(align.getDataset().getCodonFrames().contains(acf));
837 @Test(groups = "Functional")
838 public void getVisibleStartAndEndIndexTest()
840 Sequence seq = new Sequence("testSeq", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
841 AlignmentI align = new Alignment(new SequenceI[] { seq });
842 ArrayList<int[]> hiddenCols = new ArrayList<int[]>();
844 int[] startEnd = align.getVisibleStartAndEndIndex(hiddenCols);
845 assertEquals(0, startEnd[0]);
846 assertEquals(25, startEnd[1]);
848 hiddenCols.add(new int[] { 0, 0 });
849 startEnd = align.getVisibleStartAndEndIndex(hiddenCols);
850 assertEquals(1, startEnd[0]);
851 assertEquals(25, startEnd[1]);
853 hiddenCols.add(new int[] { 6, 9 });
854 hiddenCols.add(new int[] { 11, 12 });
855 startEnd = align.getVisibleStartAndEndIndex(hiddenCols);
856 assertEquals(1, startEnd[0]);
857 assertEquals(25, startEnd[1]);
859 hiddenCols.add(new int[] { 24, 25 });
860 startEnd = align.getVisibleStartAndEndIndex(hiddenCols);
861 System.out.println(startEnd[0] + " : " + startEnd[1]);
862 assertEquals(1, startEnd[0]);
863 assertEquals(23, startEnd[1]);