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);
182 // verify all dataset sequences
183 for (SequenceI seqds : alignment.getSequences())
186 if (seqds.getDatasetSequence() != null)
190 Assert.fail(message+" Dataset contained a sequence with non-null dataset reference (ie not a dataset sequence!)");
194 int foundp = alignment.findIndex(seqds);
200 + " Dataset sequence array contains a reference at "
201 + dsp + " to a sequence first seen at " + foundp + " ("
202 + seqds.toString() + ")");
206 if (seqds.getDBRefs() != null)
208 for (DBRefEntry dbr : seqds.getDBRefs())
210 if (dbr.getMap() != null)
212 SequenceI seqdbrmapto = dbr.getMap().getTo();
213 if (seqdbrmapto != null)
215 if (seqdbrmapto.getDatasetSequence() != null)
219 Assert.fail(message+" DBRefEntry for sequence in alignment had map to sequence which was not a dataset sequence");
224 if (alignment.findIndex(dbr.getMap().getTo()) == -1)
228 Assert.fail(message+" DBRefEntry for sequence in alignment had map to sequence not in dataset");
237 // finally, verify codonmappings involve only dataset sequences.
238 if (alignment.getCodonFrames() != null)
240 for (AlignedCodonFrame alc : alignment.getCodonFrames())
242 for (SequenceToSequenceMapping ssm : alc.getMappings())
244 if (ssm.getFromSeq().getDatasetSequence() != null)
248 Assert.fail(message+" CodonFrame-SSM-FromSeq is not a dataset sequence");
252 if (alignment.findIndex(ssm.getFromSeq()) == -1)
257 Assert.fail(message+" CodonFrame-SSM-FromSeq is not contained in dataset");
261 if (ssm.getMapping().getTo().getDatasetSequence() != null)
265 Assert.fail(message+" CodonFrame-SSM-Mapping-ToSeq is not a dataset sequence");
269 if (alignment.findIndex(ssm.getMapping().getTo()) == -1)
274 Assert.fail(message+" CodonFrame-SSM-Mapping-ToSeq is not contained in dataset");
282 return true; // all relationships verified!
286 * call verifyAlignmentDatasetRefs with and without assertion raising enabled,
287 * to check expected pass/fail actually occurs in both conditions
293 private void assertVerifyAlignment(AlignmentI al, boolean expected,
301 Assert.assertTrue(verifyAlignmentDatasetRefs(al, true, null),
302 "Valid test alignment failed when raiseAsserts enabled:"
304 } catch (AssertionError ae)
306 ae.printStackTrace();
308 "Valid test alignment raised assertion errors when raiseAsserts enabled: "
311 // also check validation passes with asserts disabled
312 Assert.assertTrue(verifyAlignmentDatasetRefs(al, false, null),
313 "Valid test alignment tested false when raiseAsserts disabled:"
318 boolean assertRaised = false;
321 verifyAlignmentDatasetRefs(al, true, null);
322 } catch (AssertionError ae)
324 // expected behaviour
329 Assert.fail("Invalid test alignment passed when raiseAsserts enabled:"
332 // also check validation passes with asserts disabled
333 Assert.assertFalse(verifyAlignmentDatasetRefs(al, false, null),
334 "Invalid test alignment tested true when raiseAsserts disabled:"
338 @Test(groups = { "Functional" })
339 public void testVerifyAlignmentDatasetRefs()
341 SequenceI sq1 = new Sequence("sq1", "ASFDD"), sq2 = new Sequence("sq2",
344 // construct simple valid alignment dataset
345 Alignment al = new Alignment(new SequenceI[] {
347 // expect this to pass
348 assertVerifyAlignment(al, true, "Simple valid alignment didn't verify");
350 // check test for sequence->datasetSequence validity
351 sq1.setDatasetSequence(sq2);
352 assertVerifyAlignment(
355 "didn't detect dataset sequence with a dataset sequence reference.");
357 sq1.setDatasetSequence(null);
358 assertVerifyAlignment(
361 "didn't reinstate validity after nulling dataset sequence dataset reference");
363 // now create dataset and check again
364 al.createDatasetAlignment();
365 assertNotNull(al.getDataset());
367 assertVerifyAlignment(al, true,
368 "verify failed after createDatasetAlignment");
370 // create a dbref on sq1 with a sequence ref to sq2
371 DBRefEntry dbrs1tos2 = new DBRefEntry("UNIPROT", "1", "Q111111");
372 dbrs1tos2.setMap(new Mapping(sq2.getDatasetSequence(),
373 new int[] { 1, 5 }, new int[] { 2, 6 }, 1, 1));
374 sq1.getDatasetSequence().addDBRef(dbrs1tos2);
375 assertVerifyAlignment(al, true,
376 "verify failed after addition of valid DBRefEntry/map");
377 // now create a dbref on a new sequence which maps to another sequence
378 // outside of the dataset
379 SequenceI sqout = new Sequence("sqout", "ututututucagcagcag"), sqnew = new Sequence(
381 DBRefEntry sqnewsqout = new DBRefEntry("ENAFOO", "1", "R000001");
382 sqnewsqout.setMap(new Mapping(sqout, new int[] { 1, 6 }, new int[] { 1,
384 al.getDataset().addSequence(sqnew);
386 assertVerifyAlignment(al, true,
387 "verify failed after addition of new sequence to dataset");
388 // now start checking exception conditions
389 sqnew.addDBRef(sqnewsqout);
390 assertVerifyAlignment(
393 "verify passed when a dbref with map to sequence outside of dataset was added");
394 // make the verify pass by adding the outsider back in
395 al.getDataset().addSequence(sqout);
396 assertVerifyAlignment(al, true,
397 "verify should have passed after adding dbref->to sequence in to dataset");
398 // and now the same for a codon mapping...
399 SequenceI sqanotherout = new Sequence("sqanotherout",
400 "aggtutaggcagcagcag");
402 AlignedCodonFrame alc = new AlignedCodonFrame();
403 alc.addMap(sqanotherout, sqnew, new MapList(new int[] { 1, 6 },
404 new int[] { 1, 18 }, 3, 1));
406 al.addCodonFrame(alc);
407 Assert.assertEquals(al.getDataset().getCodonFrames().size(), 1);
409 assertVerifyAlignment(
412 "verify passed when alCodonFrame mapping to sequence outside of dataset was added");
413 // make the verify pass by adding the outsider back in
414 al.getDataset().addSequence(sqanotherout);
415 assertVerifyAlignment(
418 "verify should have passed once all sequences involved in alCodonFrame were added to dataset");
419 al.getDataset().addSequence(sqanotherout);
420 assertVerifyAlignment(al, false,
421 "verify should have failed when a sequence was added twice to the dataset");
422 al.getDataset().deleteSequence(sqanotherout);
423 assertVerifyAlignment(al, true,
424 "verify should have passed after duplicate entry for sequence was removed");
428 * checks that the sequence data for an alignment's dataset is non-redundant.
429 * Fails if there are sequences with same id, sequence, start, and.
432 public static void assertDatasetIsNormalised(AlignmentI al)
434 assertAlignmentDatasetRefs(al, null);
436 public static void assertDatasetIsNormalised(AlignmentI al, String message)
438 if (al.getDataset()!=null)
440 assertDatasetIsNormalised(al.getDataset(), message);
444 * look for pairs of sequences with same ID, start, end, and sequence
446 List<SequenceI> seqSet = al.getSequences();
447 for (int p=0;p<seqSet.size(); p++)
449 SequenceI pSeq = seqSet.get(p);
450 for (int q=p+1; q<seqSet.size(); q++)
452 SequenceI qSeq = seqSet.get(q);
453 if (pSeq.getStart()!=qSeq.getStart())
457 if (pSeq.getEnd()!=qSeq.getEnd())
461 if (!pSeq.getName().equals(qSeq.getName()))
465 if (!Arrays.equals(pSeq.getSequence(), qSeq.getSequence()))
469 Assert.fail((message == null ? "" : message + " :")
470 + "Found similar sequences at position " + p + " and " + q
471 + "\n" + pSeq.toString());
476 @Test(groups = { "Functional", "Asserts" })
477 public void testAssertDatasetIsNormalised()
479 Sequence sq1 = new Sequence("s1/1-4", "asdf");
480 Sequence sq1shift = new Sequence("s1/2-5", "asdf");
481 Sequence sq1seqd = new Sequence("s1/1-4", "asdt");
482 Sequence sq2 = new Sequence("s2/1-4", "asdf");
483 Sequence sq1dup = new Sequence("s1/1-4", "asdf");
485 Alignment al = new Alignment(new SequenceI[] { sq1 });
490 assertDatasetIsNormalised(al);
491 } catch (AssertionError ae)
493 Assert.fail("Single sequence should be valid normalised dataset.");
498 assertDatasetIsNormalised(al);
499 } catch (AssertionError ae)
501 Assert.fail("Two different sequences should be valid normalised dataset.");
504 * now change sq2's name in the alignment. should still be valid
506 al.findName(sq2.getName()).setName("sq1");
509 assertDatasetIsNormalised(al);
510 } catch (AssertionError ae)
512 Assert.fail("Two different sequences in dataset, but same name in alignment, should be valid normalised dataset.");
515 al.addSequence(sq1seqd);
518 assertDatasetIsNormalised(al);
519 } catch (AssertionError ae)
521 Assert.fail("sq1 and sq1 with different sequence should be distinct.");
524 al.addSequence(sq1shift);
527 assertDatasetIsNormalised(al);
528 } catch (AssertionError ae)
530 Assert.fail("sq1 and sq1 with different start/end should be distinct.");
533 * finally, the failure case
535 al.addSequence(sq1dup);
536 boolean ssertRaised = false;
539 assertDatasetIsNormalised(al);
541 } catch (AssertionError ae)
547 Assert.fail("Expected identical sequence to raise exception.");
551 * Read in Stockholm format test data including secondary structure
554 @BeforeMethod(alwaysRun = true)
555 public void setUp() throws IOException
557 al = loadAlignment(TEST_DATA, "STH");
559 for (AlignmentAnnotation ann : al.getAlignmentAnnotation())
561 ann.setCalcId("CalcIdFor" + al.getSequenceAt(i).getName());
567 * Test method that returns annotations that match on calcId.
569 @Test(groups = { "Functional" })
570 public void testFindAnnotation_byCalcId()
572 Iterable<AlignmentAnnotation> anns = al
573 .findAnnotation("CalcIdForD.melanogaster.2");
574 Iterator<AlignmentAnnotation> iter = anns.iterator();
575 assertTrue(iter.hasNext());
576 AlignmentAnnotation ann = iter.next();
577 assertEquals("D.melanogaster.2", ann.sequenceRef.getName());
578 assertFalse(iter.hasNext());
581 @Test(groups = { "Functional" })
582 public void testDeleteAllAnnotations_includingAutocalculated()
584 AlignmentAnnotation aa = new AlignmentAnnotation("Consensus",
586 aa.autoCalculated = true;
587 al.addAnnotation(aa);
588 AlignmentAnnotation[] anns = al.getAlignmentAnnotation();
589 assertEquals("Wrong number of annotations before deleting", 4,
591 al.deleteAllAnnotations(true);
592 assertEquals("Not all deleted", 0, al.getAlignmentAnnotation().length);
595 @Test(groups = { "Functional" })
596 public void testDeleteAllAnnotations_excludingAutocalculated()
598 AlignmentAnnotation aa = new AlignmentAnnotation("Consensus",
600 aa.autoCalculated = true;
601 al.addAnnotation(aa);
602 AlignmentAnnotation[] anns = al.getAlignmentAnnotation();
603 assertEquals("Wrong number of annotations before deleting", 4,
605 al.deleteAllAnnotations(false);
606 assertEquals("Not just one annotation left", 1,
607 al.getAlignmentAnnotation().length);
611 * Tests for realigning as per a supplied alignment: Dna as Dna.
613 * Note: AlignedCodonFrame's state variables are named for protein-to-cDNA
614 * mapping, but can be exploited for a general 'sequence-to-sequence' mapping
617 * @throws IOException
619 @Test(groups = { "Functional" })
620 public void testAlignAs_dnaAsDna() throws IOException
623 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
625 AlignmentI al2 = loadAlignment(CDNA_SEQS_2, "FASTA");
628 * Make mappings between sequences. The 'aligned cDNA' is playing the role
629 * of what would normally be protein here.
631 makeMappings(al1, al2);
633 ((Alignment) al2).alignAs(al1, false, true);
634 assertEquals("GC-TC--GUC-GTACT", al2.getSequenceAt(0)
635 .getSequenceAsString());
636 assertEquals("-GG-GTC--AGG--CAGT", al2.getSequenceAt(1)
637 .getSequenceAsString());
641 * Aligning protein from cDNA.
643 * @throws IOException
645 @Test(groups = { "Functional" })
646 public void testAlignAs_proteinAsCdna() throws IOException
648 // see also AlignmentUtilsTests
649 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
650 AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA");
651 makeMappings(al1, al2);
653 // Fudge - alignProteinAsCdna expects mappings to be on protein
654 al2.getCodonFrames().addAll(al1.getCodonFrames());
656 ((Alignment) al2).alignAs(al1, false, true);
657 assertEquals("K-Q-Y-L-", al2.getSequenceAt(0).getSequenceAsString());
658 assertEquals("-R-F-P-W", al2.getSequenceAt(1).getSequenceAsString());
662 * Test aligning cdna as per protein alignment.
664 * @throws IOException
666 @Test(groups = { "Functional" }, enabled = true)
667 // TODO review / update this test after redesign of alignAs method
668 public void testAlignAs_cdnaAsProtein() throws IOException
671 * Load alignments and add mappings for cDNA to protein
673 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
674 AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA");
675 makeMappings(al1, al2);
678 * Realign DNA; currently keeping existing gaps in introns only
680 ((Alignment) al1).alignAs(al2, false, true);
681 assertEquals("ACG---GCUCCA------ACT---", al1.getSequenceAt(0)
682 .getSequenceAsString());
683 assertEquals("---CGT---TAACGA---AGT---", al1.getSequenceAt(1)
684 .getSequenceAsString());
688 * Test aligning cdna as per protein - single sequences
690 * @throws IOException
692 @Test(groups = { "Functional" }, enabled = true)
693 // TODO review / update this test after redesign of alignAs method
694 public void testAlignAs_cdnaAsProtein_singleSequence() throws IOException
697 * simple case insert one gap
699 verifyAlignAs(">dna\nCAAaaa\n", ">protein\nQ-K\n", "CAA---aaa");
702 * simple case but with sequence offsets
704 verifyAlignAs(">dna/5-10\nCAAaaa\n", ">protein/20-21\nQ-K\n",
708 * insert gaps as per protein, drop gaps within codons
710 verifyAlignAs(">dna/10-18\nCA-Aa-aa--AGA\n", ">aa/6-8\n-Q-K--R\n",
711 "---CAA---aaa------AGA");
715 * Helper method that makes mappings and then aligns the first alignment as
721 * @throws IOException
723 public void verifyAlignAs(String fromSeqs, String toSeqs, String expected)
727 * Load alignments and add mappings from nucleotide to protein (or from
728 * first to second if both the same type)
730 AlignmentI al1 = loadAlignment(fromSeqs, "FASTA");
731 AlignmentI al2 = loadAlignment(toSeqs, "FASTA");
732 makeMappings(al1, al2);
735 * Realign DNA; currently keeping existing gaps in introns only
737 ((Alignment) al1).alignAs(al2, false, true);
738 assertEquals(expected, al1.getSequenceAt(0).getSequenceAsString());
742 * Helper method to make mappings between sequences, and add the mappings to
743 * the 'mapped from' alignment
748 public void makeMappings(AlignmentI alFrom, AlignmentI alTo)
750 int ratio = (alFrom.isNucleotide() == alTo.isNucleotide() ? 1 : 3);
752 AlignedCodonFrame acf = new AlignedCodonFrame();
754 for (int i = 0; i < alFrom.getHeight(); i++)
756 SequenceI seqFrom = alFrom.getSequenceAt(i);
757 SequenceI seqTo = alTo.getSequenceAt(i);
758 MapList ml = new MapList(new int[] { seqFrom.getStart(),
760 new int[] { seqTo.getStart(), seqTo.getEnd() }, ratio, 1);
761 acf.addMap(seqFrom, seqTo, ml);
765 * not sure whether mappings 'belong' or protein or nucleotide
766 * alignment, so adding to both ;~)
768 alFrom.addCodonFrame(acf);
769 alTo.addCodonFrame(acf);
773 * Test aligning dna as per protein alignment, for the case where there are
774 * introns (i.e. some dna sites have no mapping from a peptide).
776 * @throws IOException
778 @Test(groups = { "Functional" }, enabled = false)
779 // TODO review / update this test after redesign of alignAs method
780 public void testAlignAs_dnaAsProtein_withIntrons() throws IOException
783 * Load alignments and add mappings for cDNA to protein
785 String dna1 = "A-Aa-gG-GCC-cT-TT";
786 String dna2 = "c--CCGgg-TT--T-AA-A";
787 AlignmentI al1 = loadAlignment(">Dna1/6-17\n" + dna1
788 + "\n>Dna2/20-31\n" + dna2 + "\n", "FASTA");
789 AlignmentI al2 = loadAlignment(
790 ">Pep1/7-9\n-P--YK\n>Pep2/11-13\nG-T--F\n", "FASTA");
791 AlignedCodonFrame acf = new AlignedCodonFrame();
792 // Seq1 has intron at dna positions 3,4,9 so splice is AAG GCC TTT
793 // Seq2 has intron at dna positions 1,5,6 so splice is CCG TTT AAA
794 MapList ml1 = new MapList(new int[] { 6, 7, 10, 13, 15, 17 }, new int[]
796 acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml1);
797 MapList ml2 = new MapList(new int[] { 21, 23, 26, 31 }, new int[] { 11,
799 acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml2);
800 al2.addCodonFrame(acf);
803 * Align ignoring gaps in dna introns and exons
805 ((Alignment) al1).alignAs(al2, false, false);
806 assertEquals("---AAagG------GCCcTTT", al1.getSequenceAt(0)
807 .getSequenceAsString());
808 // note 1 gap in protein corresponds to 'gg-' in DNA (3 positions)
809 assertEquals("cCCGgg-TTT------AAA", al1.getSequenceAt(1)
810 .getSequenceAsString());
813 * Reset and realign, preserving gaps in dna introns and exons
815 al1.getSequenceAt(0).setSequence(dna1);
816 al1.getSequenceAt(1).setSequence(dna2);
817 ((Alignment) al1).alignAs(al2, true, true);
818 // String dna1 = "A-Aa-gG-GCC-cT-TT";
819 // String dna2 = "c--CCGgg-TT--T-AA-A";
820 // assumption: we include 'the greater of' protein/dna gap lengths, not both
821 assertEquals("---A-Aa-gG------GCC-cT-TT", al1.getSequenceAt(0)
822 .getSequenceAsString());
823 assertEquals("c--CCGgg-TT--T------AA-A", al1.getSequenceAt(1)
824 .getSequenceAsString());
827 @Test(groups = "Functional")
828 public void testCopyConstructor() throws IOException
830 AlignmentI protein = loadAlignment(AA_SEQS_1, FormatAdapter.PASTE);
831 // create sequence and alignment datasets
832 protein.setDataset(null);
833 AlignedCodonFrame acf = new AlignedCodonFrame();
834 List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
836 protein.getDataset().setCodonFrames(acfList);
837 AlignmentI copy = new Alignment(protein);
840 * copy has different aligned sequences but the same dataset sequences
842 assertFalse(copy.getSequenceAt(0) == protein.getSequenceAt(0));
843 assertFalse(copy.getSequenceAt(1) == protein.getSequenceAt(1));
844 assertSame(copy.getSequenceAt(0).getDatasetSequence(), protein
845 .getSequenceAt(0).getDatasetSequence());
846 assertSame(copy.getSequenceAt(1).getDatasetSequence(), protein
847 .getSequenceAt(1).getDatasetSequence());
849 // TODO should the copy constructor copy the dataset?
850 // or make a new one referring to the same dataset sequences??
851 assertNull(copy.getDataset());
852 // TODO test metadata is copied when AlignmentI is a dataset
854 // assertArrayEquals(copy.getDataset().getSequencesArray(), protein
855 // .getDataset().getSequencesArray());
859 * Test behaviour of createDataset
861 * @throws IOException
863 @Test(groups = "Functional")
864 public void testCreateDatasetAlignment() throws IOException
866 AlignmentI protein = new FormatAdapter().readFile(AA_SEQS_1,
867 AppletFormatAdapter.PASTE, "FASTA");
869 * create a dataset sequence on first sequence
870 * leave the second without one
872 protein.getSequenceAt(0).createDatasetSequence();
873 assertNotNull(protein.getSequenceAt(0).getDatasetSequence());
874 assertNull(protein.getSequenceAt(1).getDatasetSequence());
877 * add a mapping to the alignment
879 AlignedCodonFrame acf = new AlignedCodonFrame();
880 protein.addCodonFrame(acf);
881 assertNull(protein.getDataset());
882 assertTrue(protein.getCodonFrames().contains(acf));
885 * create the alignment dataset
886 * note this creates sequence datasets where missing
887 * as a side-effect (in this case, on seq2
889 // TODO promote this method to AlignmentI
890 ((Alignment) protein).createDatasetAlignment();
892 AlignmentI ds = protein.getDataset();
894 // side-effect: dataset created on second sequence
895 assertNotNull(protein.getSequenceAt(1).getDatasetSequence());
896 // dataset alignment has references to dataset sequences
897 assertEquals(ds.getSequenceAt(0), protein.getSequenceAt(0)
898 .getDatasetSequence());
899 assertEquals(ds.getSequenceAt(1), protein.getSequenceAt(1)
900 .getDatasetSequence());
902 // codon frames should have been moved to the dataset
903 // getCodonFrames() should delegate to the dataset:
904 assertTrue(protein.getCodonFrames().contains(acf));
905 // prove the codon frames are indeed on the dataset:
906 assertTrue(ds.getCodonFrames().contains(acf));
910 * tests the addition of *all* sequences referred to by a sequence being added
913 @Test(groups = "Functional")
914 public void testCreateDatasetAlignmentWithMappedToSeqs()
916 // Alignment with two sequences, gapped.
917 SequenceI sq1 = new Sequence("sq1", "A--SDF");
918 SequenceI sq2 = new Sequence("sq2", "G--TRQ");
920 // cross-references to two more sequences.
921 DBRefEntry dbr = new DBRefEntry("SQ1", "", "sq3");
922 SequenceI sq3 = new Sequence("sq3", "VWANG");
923 dbr.setMap(new Mapping(sq3, new MapList(new int[] { 1, 4 }, new int[] {
927 SequenceI sq4 = new Sequence("sq4", "ERKWI");
928 DBRefEntry dbr2 = new DBRefEntry("SQ2", "", "sq4");
929 dbr2.setMap(new Mapping(sq4, new MapList(new int[] { 1, 4 }, new int[] {
932 // and a 1:1 codonframe mapping between them.
933 AlignedCodonFrame alc = new AlignedCodonFrame();
934 alc.addMap(sq1, sq2, new MapList(new int[] { 1, 4 },
935 new int[] { 1, 4 }, 1, 1));
937 AlignmentI protein = new Alignment(new SequenceI[] { sq1, sq2 });
940 * create the alignment dataset
941 * note this creates sequence datasets where missing
942 * as a side-effect (in this case, on seq2
945 // TODO promote this method to AlignmentI
946 ((Alignment) protein).createDatasetAlignment();
948 AlignmentI ds = protein.getDataset();
950 // should be 4 sequences in dataset - two materialised, and two propagated
952 assertEquals(4, ds.getHeight());
953 assertTrue(ds.getSequences().contains(sq1.getDatasetSequence()));
954 assertTrue(ds.getSequences().contains(sq2.getDatasetSequence()));
955 assertTrue(ds.getSequences().contains(sq3));
956 assertTrue(ds.getSequences().contains(sq4));
957 // Should have one codon frame mapping between sq1 and sq2 via dataset
959 assertEquals(ds.getCodonFrame(sq1.getDatasetSequence()),
960 ds.getCodonFrame(sq2.getDatasetSequence()));
963 @Test(groups = "Functional")
964 public void testAddCodonFrame()
966 AlignmentI align = new Alignment(new SequenceI[] {});
967 AlignedCodonFrame acf = new AlignedCodonFrame();
968 align.addCodonFrame(acf);
969 assertEquals(1, align.getCodonFrames().size());
970 assertTrue(align.getCodonFrames().contains(acf));
971 // can't add the same object twice:
972 align.addCodonFrame(acf);
973 assertEquals(1, align.getCodonFrames().size());
975 // create dataset alignment - mappings move to dataset
976 ((Alignment) align).createDatasetAlignment();
977 assertSame(align.getCodonFrames(), align.getDataset().getCodonFrames());
978 assertEquals(1, align.getCodonFrames().size());
980 AlignedCodonFrame acf2 = new AlignedCodonFrame();
981 align.addCodonFrame(acf2);
982 assertTrue(align.getDataset().getCodonFrames().contains(acf));
985 @Test(groups = "Functional")
986 public void testAddSequencePreserveDatasetIntegrity()
988 Sequence seq = new Sequence("testSeq", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
989 Alignment align = new Alignment(new SequenceI[] { seq });
990 align.createDatasetAlignment();
991 AlignmentI ds = align.getDataset();
992 SequenceI copy = new Sequence(seq);
993 copy.insertCharAt(3, 5, '-');
994 align.addSequence(copy);
995 Assert.assertEquals(align.getDataset().getHeight(), 1,
996 "Dataset shouldn't have more than one sequence.");
998 Sequence seq2 = new Sequence("newtestSeq", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
999 align.addSequence(seq2);
1000 Assert.assertEquals(align.getDataset().getHeight(), 2,
1001 "Dataset should now have two sequences.");
1003 assertAlignmentDatasetRefs(align,
1004 "addSequence broke dataset reference integrity");
1006 @Test(groups = "Functional")
1007 public void getVisibleStartAndEndIndexTest()
1009 Sequence seq = new Sequence("testSeq", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
1010 AlignmentI align = new Alignment(new SequenceI[] { seq });
1011 ArrayList<int[]> hiddenCols = new ArrayList<int[]>();
1013 int[] startEnd = align.getVisibleStartAndEndIndex(hiddenCols);
1014 assertEquals(0, startEnd[0]);
1015 assertEquals(25, startEnd[1]);
1017 hiddenCols.add(new int[] { 0, 0 });
1018 startEnd = align.getVisibleStartAndEndIndex(hiddenCols);
1019 assertEquals(1, startEnd[0]);
1020 assertEquals(25, startEnd[1]);
1022 hiddenCols.add(new int[] { 6, 9 });
1023 hiddenCols.add(new int[] { 11, 12 });
1024 startEnd = align.getVisibleStartAndEndIndex(hiddenCols);
1025 assertEquals(1, startEnd[0]);
1026 assertEquals(25, startEnd[1]);
1028 hiddenCols.add(new int[] { 24, 25 });
1029 startEnd = align.getVisibleStartAndEndIndex(hiddenCols);
1030 System.out.println(startEnd[0] + " : " + startEnd[1]);
1031 assertEquals(1, startEnd[0]);
1032 assertEquals(23, startEnd[1]);