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 java.io.IOException;
31 import java.util.Arrays;
32 import java.util.Iterator;
33 import java.util.List;
35 import org.testng.Assert;
36 import org.testng.annotations.BeforeClass;
37 import org.testng.annotations.BeforeMethod;
38 import org.testng.annotations.Test;
40 import jalview.analysis.AlignmentGenerator;
41 import jalview.analysis.AlignmentUtils;
42 import jalview.analysis.CrossRef;
43 import jalview.datamodel.AlignedCodonFrame.SequenceToSequenceMapping;
44 import jalview.gui.JvOptionPane;
45 import jalview.io.DataSourceType;
46 import jalview.io.FastaFile;
47 import jalview.io.FileFormat;
48 import jalview.io.FileFormatI;
49 import jalview.io.FormatAdapter;
50 import jalview.util.Comparison;
51 import jalview.util.MapList;
54 * Unit tests for Alignment datamodel.
59 public class AlignmentTest
62 @BeforeClass(alwaysRun = true)
63 public void setUpJvOptionPane()
65 JvOptionPane.setInteractiveMode(false);
66 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
70 private static final String TEST_DATA =
72 "#=GS D.melanogaster.1 AC AY119185.1/838-902\n" +
73 "#=GS D.melanogaster.2 AC AC092237.1/57223-57161\n" +
74 "#=GS D.melanogaster.3 AC AY060611.1/560-627\n" +
75 "D.melanogaster.1 G.AGCC.CU...AUGAUCGA\n" +
76 "#=GR D.melanogaster.1 SS ................((((\n" +
77 "D.melanogaster.2 C.AUUCAACU.UAUGAGGAU\n" +
78 "#=GR D.melanogaster.2 SS ................((((\n" +
79 "D.melanogaster.3 G.UGGCGCU..UAUGACGCA\n" +
80 "#=GR D.melanogaster.3 SS (.(((...(....(((((((\n" +
83 private static final String AA_SEQS_1 =
89 private static final String CDNA_SEQS_1 =
90 ">Seq1Name/100-111\n" +
91 "AC-GG--CUC-CAA-CT\n" +
92 ">Seq2Name/200-211\n" +
93 "-CG-TTA--ACG---AAGT\n";
95 private static final String CDNA_SEQS_2 =
101 private static final String AA_SEQS_2 =
104 ">Seq2Name/12-15\n" +
106 private static final String AA_SEQS_2_DS =
109 ">Seq2Name/12-15\n" +
111 private static final String TD_SEQS_2_DS =
114 ">Seq2Name/12-15\n" +
116 private static final String TD_SEQS_2 =
119 ">Seq2Name/12-15\n" +
124 private AlignmentI al;
127 * Helper method to load an alignment and ensure dataset sequences are set up.
133 * @throws IOException
135 protected AlignmentI loadAlignment(final String data, FileFormatI format)
138 AlignmentI a = new FormatAdapter().readFile(data, DataSourceType.PASTE,
145 * assert wrapper: tests all references in the given alignment are consistent
149 public static void assertAlignmentDatasetRefs(AlignmentI alignment)
151 verifyAlignmentDatasetRefs(alignment, true, null);
155 * assert wrapper: tests all references in the given alignment are consistent
159 * - prefixed to any assert failed messages
161 public static void assertAlignmentDatasetRefs(AlignmentI alignment,
164 verifyAlignmentDatasetRefs(alignment, true, message);
168 * verify sequence and dataset references are properly contained within
172 * - the alignmentI object to verify (either alignment or dataset)
174 * - when set, testng assertions are raised.
176 * - null or a string message to prepend to the assert failed
178 * @return true if alignment references were in order, otherwise false.
180 public static boolean verifyAlignmentDatasetRefs(AlignmentI alignment,
181 boolean raiseAssert, String message)
187 if (alignment == null)
191 Assert.fail(message + "Alignment for verification was null.");
195 if (alignment.getDataset() != null)
197 AlignmentI dataset = alignment.getDataset();
198 // check all alignment sequences have their dataset within the dataset
199 for (SequenceI seq : alignment.getSequences())
201 SequenceI seqds = seq.getDatasetSequence();
202 if (seqds.getDatasetSequence() != null)
207 + " Alignment contained a sequence who's dataset sequence has a second dataset reference.");
211 if (dataset.findIndex(seqds) == -1)
216 + " Alignment contained a sequence who's dataset sequence was not in the dataset.");
221 return verifyAlignmentDatasetRefs(alignment.getDataset(), raiseAssert,
227 // verify all dataset sequences
228 for (SequenceI seqds : alignment.getSequences())
231 if (seqds.getDatasetSequence() != null)
236 + " Dataset contained a sequence with non-null dataset reference (ie not a dataset sequence!)");
240 int foundp = alignment.findIndex(seqds);
246 + " Dataset sequence array contains a reference at "
247 + dsp + " to a sequence first seen at " + foundp + " ("
248 + seqds.toString() + ")");
252 if (seqds.getDBRefs() != null)
254 for (DBRefEntry dbr : seqds.getDBRefs())
256 if (dbr.getMap() != null)
258 SequenceI seqdbrmapto = dbr.getMap().getTo();
259 if (seqdbrmapto != null)
261 if (seqdbrmapto.getDatasetSequence() != null)
266 + " DBRefEntry for sequence in alignment had map to sequence which was not a dataset sequence");
271 if (alignment.findIndex(dbr.getMap().getTo()) == -1)
275 Assert.fail(message + " DBRefEntry " + dbr
276 + " for sequence " + seqds
277 + " in alignment has map to sequence not in dataset");
286 // finally, verify codonmappings involve only dataset sequences.
287 if (alignment.getCodonFrames() != null)
289 for (AlignedCodonFrame alc : alignment.getCodonFrames())
291 for (SequenceToSequenceMapping ssm : alc.getMappings())
293 if (ssm.getFromSeq().getDatasetSequence() != null)
298 + " CodonFrame-SSM-FromSeq is not a dataset sequence");
302 if (alignment.findIndex(ssm.getFromSeq()) == -1)
308 + " CodonFrame-SSM-FromSeq is not contained in dataset");
312 if (ssm.getMapping().getTo().getDatasetSequence() != null)
317 + " CodonFrame-SSM-Mapping-ToSeq is not a dataset sequence");
321 if (alignment.findIndex(ssm.getMapping().getTo()) == -1)
327 + " CodonFrame-SSM-Mapping-ToSeq is not contained in dataset");
335 return true; // all relationships verified!
339 * call verifyAlignmentDatasetRefs with and without assertion raising enabled,
340 * to check expected pass/fail actually occurs in both conditions
346 private void assertVerifyAlignment(AlignmentI al, boolean expected,
354 Assert.assertTrue(verifyAlignmentDatasetRefs(al, true, null),
355 "Valid test alignment failed when raiseAsserts enabled:"
357 } catch (AssertionError ae)
359 ae.printStackTrace();
361 "Valid test alignment raised assertion errors when raiseAsserts enabled: "
365 // also check validation passes with asserts disabled
366 Assert.assertTrue(verifyAlignmentDatasetRefs(al, false, null),
367 "Valid test alignment tested false when raiseAsserts disabled:"
372 boolean assertRaised = false;
375 verifyAlignmentDatasetRefs(al, true, null);
376 } catch (AssertionError ae)
378 // expected behaviour
384 "Invalid test alignment passed when raiseAsserts enabled:"
387 // also check validation passes with asserts disabled
388 Assert.assertFalse(verifyAlignmentDatasetRefs(al, false, null),
389 "Invalid test alignment tested true when raiseAsserts disabled:"
394 @Test(groups = { "Functional" })
395 public void testVerifyAlignmentDatasetRefs()
397 SequenceI sq1 = new Sequence("sq1", "ASFDD"),
398 sq2 = new Sequence("sq2", "TTTTTT");
400 // construct simple valid alignment dataset
401 Alignment al = new Alignment(new SequenceI[] { sq1, sq2 });
402 // expect this to pass
403 assertVerifyAlignment(al, true, "Simple valid alignment didn't verify");
405 // check test for sequence->datasetSequence validity
406 sq1.setDatasetSequence(sq2);
407 assertVerifyAlignment(al, false,
408 "didn't detect dataset sequence with a dataset sequence reference.");
410 sq1.setDatasetSequence(null);
411 assertVerifyAlignment(al, true,
412 "didn't reinstate validity after nulling dataset sequence dataset reference");
414 // now create dataset and check again
415 al.createDatasetAlignment();
416 assertNotNull(al.getDataset());
418 assertVerifyAlignment(al, true,
419 "verify failed after createDatasetAlignment");
421 // create a dbref on sq1 with a sequence ref to sq2
422 DBRefEntry dbrs1tos2 = new DBRefEntry("UNIPROT", "1", "Q111111");
424 .setMap(new Mapping(sq2.getDatasetSequence(), new int[]
425 { 1, 5 }, new int[] { 2, 6 }, 1, 1));
426 sq1.getDatasetSequence().addDBRef(dbrs1tos2);
427 assertVerifyAlignment(al, true,
428 "verify failed after addition of valid DBRefEntry/map");
429 // now create a dbref on a new sequence which maps to another sequence
430 // outside of the dataset
431 SequenceI sqout = new Sequence("sqout", "ututututucagcagcag"),
432 sqnew = new Sequence("sqnew", "EEERRR");
433 DBRefEntry sqnewsqout = new DBRefEntry("ENAFOO", "1", "R000001");
435 .setMap(new Mapping(sqout, new int[]
436 { 1, 6 }, new int[] { 1, 18 }, 1, 3));
437 al.getDataset().addSequence(sqnew);
439 assertVerifyAlignment(al, true,
440 "verify failed after addition of new sequence to dataset");
441 // now start checking exception conditions
442 sqnew.addDBRef(sqnewsqout);
443 assertVerifyAlignment(al, false,
444 "verify passed when a dbref with map to sequence outside of dataset was added");
445 // make the verify pass by adding the outsider back in
446 al.getDataset().addSequence(sqout);
447 assertVerifyAlignment(al, true,
448 "verify should have passed after adding dbref->to sequence in to dataset");
449 // and now the same for a codon mapping...
450 SequenceI sqanotherout = new Sequence("sqanotherout",
451 "aggtutaggcagcagcag");
453 AlignedCodonFrame alc = new AlignedCodonFrame();
454 alc.addMap(sqanotherout, sqnew,
455 new MapList(new int[]
456 { 1, 6 }, new int[] { 1, 18 }, 3, 1));
458 al.addCodonFrame(alc);
459 Assert.assertEquals(al.getDataset().getCodonFrames().size(), 1);
461 assertVerifyAlignment(al, false,
462 "verify passed when alCodonFrame mapping to sequence outside of dataset was added");
463 // make the verify pass by adding the outsider back in
464 al.getDataset().addSequence(sqanotherout);
465 assertVerifyAlignment(al, true,
466 "verify should have passed once all sequences involved in alCodonFrame were added to dataset");
467 al.getDataset().addSequence(sqanotherout);
468 assertVerifyAlignment(al, false,
469 "verify should have failed when a sequence was added twice to the dataset");
470 al.getDataset().deleteSequence(sqanotherout);
471 assertVerifyAlignment(al, true,
472 "verify should have passed after duplicate entry for sequence was removed");
476 * checks that the sequence data for an alignment's dataset is non-redundant.
477 * Fails if there are sequences with same id, sequence, start, and.
480 public static void assertDatasetIsNormalised(AlignmentI al)
482 assertDatasetIsNormalised(al, null);
486 * checks that the sequence data for an alignment's dataset is non-redundant.
487 * Fails if there are sequences with same id, sequence, start, and.
490 * - alignment to verify
492 * - null or message prepended to exception message.
494 public static void assertDatasetIsNormalised(AlignmentI al,
497 if (al.getDataset() != null)
499 assertDatasetIsNormalised(al.getDataset(), message);
503 * look for pairs of sequences with same ID, start, end, and sequence
505 List<SequenceI> seqSet = al.getSequences();
506 for (int p = 0; p < seqSet.size(); p++)
508 SequenceI pSeq = seqSet.get(p);
509 for (int q = p + 1; q < seqSet.size(); q++)
511 SequenceI qSeq = seqSet.get(q);
512 if (pSeq.getStart() != qSeq.getStart())
516 if (pSeq.getEnd() != qSeq.getEnd())
520 if (!pSeq.getName().equals(qSeq.getName()))
524 if (!Arrays.equals(pSeq.getSequence(), qSeq.getSequence()))
528 Assert.fail((message == null ? "" : message + " :")
529 + "Found similar sequences at position " + p + " and " + q
530 + "\n" + pSeq.toString());
535 @Test(groups = { "Functional", "Asserts" })
536 public void testAssertDatasetIsNormalised()
538 Sequence sq1 = new Sequence("s1/1-4", "asdf");
539 Sequence sq1shift = new Sequence("s1/2-5", "asdf");
540 Sequence sq1seqd = new Sequence("s1/1-4", "asdt");
541 Sequence sq2 = new Sequence("s2/1-4", "asdf");
542 Sequence sq1dup = new Sequence("s1/1-4", "asdf");
544 Alignment al = new Alignment(new SequenceI[] { sq1 });
549 assertDatasetIsNormalised(al);
550 } catch (AssertionError ae)
552 Assert.fail("Single sequence should be valid normalised dataset.");
557 assertDatasetIsNormalised(al);
558 } catch (AssertionError ae)
561 "Two different sequences should be valid normalised dataset.");
564 * now change sq2's name in the alignment. should still be valid
566 al.findName(sq2.getName()).setName("sq1");
569 assertDatasetIsNormalised(al);
570 } catch (AssertionError ae)
573 "Two different sequences in dataset, but same name in alignment, should be valid normalised dataset.");
576 al.addSequence(sq1seqd);
579 assertDatasetIsNormalised(al);
580 } catch (AssertionError ae)
583 "sq1 and sq1 with different sequence should be distinct.");
586 al.addSequence(sq1shift);
589 assertDatasetIsNormalised(al);
590 } catch (AssertionError ae)
593 "sq1 and sq1 with different start/end should be distinct.");
596 * finally, the failure case
598 al.addSequence(sq1dup);
599 boolean ssertRaised = false;
602 assertDatasetIsNormalised(al);
604 } catch (AssertionError ae)
610 Assert.fail("Expected identical sequence to raise exception.");
615 * Read in Stockholm format test data including secondary structure
618 @BeforeMethod(alwaysRun = true)
619 public void setUp() throws IOException
621 al = loadAlignment(TEST_DATA, FileFormat.Stockholm);
623 for (AlignmentAnnotation ann : al.getAlignmentAnnotation())
625 ann.setCalcId("CalcIdFor" + al.getSequenceAt(i).getName());
631 * Test method that returns annotations that match on calcId.
633 @Test(groups = { "Functional" })
634 public void testFindAnnotation_byCalcId()
636 Iterable<AlignmentAnnotation> anns = al
637 .findAnnotation("CalcIdForD.melanogaster.2");
638 Iterator<AlignmentAnnotation> iter = anns.iterator();
639 assertTrue(iter.hasNext());
640 AlignmentAnnotation ann = iter.next();
641 assertEquals("D.melanogaster.2", ann.sequenceRef.getName());
642 assertFalse(iter.hasNext());
645 anns = al.findAnnotation("CalcIdForD.melanogaster.?");
646 assertFalse(iter.hasNext());
647 anns = al.findAnnotation(null);
648 assertFalse(iter.hasNext());
652 * Test method that returns annotations that match on reference sequence,
655 @Test(groups = { "Functional" })
656 public void testFindAnnotations_bySeqLabelandorCalcId()
658 // TODO: finish testFindAnnotations_bySeqLabelandorCalcId test
659 /* Note - this is an incomplete test - need to check null or
660 * non-null [ matches, not matches ] behaviour for each of the three
663 // search for a single, unique calcId with wildcards on other params
664 Iterable<AlignmentAnnotation> anns = al.findAnnotations(null,
665 "CalcIdForD.melanogaster.2", null);
666 Iterator<AlignmentAnnotation> iter = anns.iterator();
667 assertTrue(iter.hasNext());
668 AlignmentAnnotation ann = iter.next();
669 assertEquals("D.melanogaster.2", ann.sequenceRef.getName());
670 assertFalse(iter.hasNext());
672 // save reference to test sequence reference parameter
673 SequenceI rseq = ann.sequenceRef;
675 // search for annotation associated with a single sequence
676 anns = al.findAnnotations(rseq, null, null);
677 iter = anns.iterator();
678 assertTrue(iter.hasNext());
680 assertEquals("D.melanogaster.2", ann.sequenceRef.getName());
681 assertFalse(iter.hasNext());
683 // search for annotation with a non-existant calcId
684 anns = al.findAnnotations(null, "CalcIdForD.melanogaster.?", null);
685 iter = anns.iterator();
686 assertFalse(iter.hasNext());
688 // search for annotation with a particular label - expect three
689 anns = al.findAnnotations(null, null, "Secondary Structure");
690 iter = anns.iterator();
691 assertTrue(iter.hasNext());
693 assertTrue(iter.hasNext());
695 assertTrue(iter.hasNext());
698 assertFalse(iter.hasNext());
700 // search for annotation on one sequence with a particular label - expect
703 anns = al.findAnnotations(sqfound = al.getSequenceAt(1), null,
704 "Secondary Structure");
705 iter = anns.iterator();
706 assertTrue(iter.hasNext());
707 // expect reference to sequence 1 in the alignment
708 assertTrue(sqfound == iter.next().sequenceRef);
709 assertFalse(iter.hasNext());
711 // null on all parameters == find all annotations
712 anns = al.findAnnotations(null, null, null);
713 iter = anns.iterator();
714 int n = al.getAlignmentAnnotation().length;
715 while (iter.hasNext())
720 assertTrue("Found " + n + " fewer annotations from search.", n == 0);
723 @Test(groups = { "Functional" })
724 public void testDeleteAllAnnotations_includingAutocalculated()
726 AlignmentAnnotation aa = new AlignmentAnnotation("Consensus",
728 aa.autoCalculated = true;
729 al.addAnnotation(aa);
730 AlignmentAnnotation[] anns = al.getAlignmentAnnotation();
731 assertEquals("Wrong number of annotations before deleting", 4,
733 al.deleteAllAnnotations(true);
734 assertEquals("Not all deleted", 0, al.getAlignmentAnnotation().length);
737 @Test(groups = { "Functional" })
738 public void testDeleteAllAnnotations_excludingAutocalculated()
740 AlignmentAnnotation aa = new AlignmentAnnotation("Consensus",
742 aa.autoCalculated = true;
743 al.addAnnotation(aa);
744 AlignmentAnnotation[] anns = al.getAlignmentAnnotation();
745 assertEquals("Wrong number of annotations before deleting", 4,
747 al.deleteAllAnnotations(false);
748 assertEquals("Not just one annotation left", 1,
749 al.getAlignmentAnnotation().length);
753 * Tests for realigning as per a supplied alignment: Dna as Dna.
755 * Note: AlignedCodonFrame's state variables are named for protein-to-cDNA
756 * mapping, but can be exploited for a general 'sequence-to-sequence' mapping
759 * @throws IOException
761 @Test(groups = { "Functional" })
762 public void testAlignAs_dnaAsDna() throws IOException
765 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, FileFormat.Fasta);
767 AlignmentI al2 = loadAlignment(CDNA_SEQS_2, FileFormat.Fasta);
770 * Make mappings between sequences. The 'aligned cDNA' is playing the role
771 * of what would normally be protein here.
773 makeMappings(al1, al2);
775 ((Alignment) al2).alignAs(al1, false, true);
776 assertEquals("GC-TC--GUC-GTACT",
777 al2.getSequenceAt(0).getSequenceAsString());
778 assertEquals("-GG-GTC--AGG--CAGT",
779 al2.getSequenceAt(1).getSequenceAsString());
783 * Aligning protein from cDNA.
785 * @throws IOException
787 @Test(groups = { "Functional" })
788 public void testAlignAs_proteinAsCdna() throws IOException
790 // see also AlignmentUtilsTests
791 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, FileFormat.Fasta);
792 AlignmentI al2 = loadAlignment(AA_SEQS_1, FileFormat.Fasta);
793 makeMappings(al1, al2);
795 // Fudge - alignProteinAsCdna expects mappings to be on protein
796 al2.getCodonFrames().addAll(al1.getCodonFrames());
798 ((Alignment) al2).alignAs(al1, false, true);
799 assertEquals("K-Q-Y-L-", al2.getSequenceAt(0).getSequenceAsString());
800 assertEquals("-R-F-P-W", al2.getSequenceAt(1).getSequenceAsString());
805 * Recover protein MSA from tdi msa
807 * @throws IOException
809 @Test(groups = { "Functional" })
810 public void testAlignAs_prot_tdi() throws Exception
812 // see also AlignmentUtilsTests
813 AlignmentI al1 = loadAlignment(TD_SEQS_2, FileFormat.Fasta);
814 AlignmentI al2 = loadAlignment(AA_SEQS_2_DS, FileFormat.Fasta);
815 al1.setDataset(null);
816 al2.setDataset(al1.getDataset());
817 AlignmentI al1copy = new Alignment(al1);
818 AlignmentI al2copy = new Alignment(al2);
819 AlignmentUtils.map3diPeptideToProteinAligment(al2, al1);
820 if (al2.getCodonFrames().isEmpty()) {al2.getCodonFrames().addAll(al1.getCodonFrames()); }
821 else {al1.getCodonFrames().addAll(al2.getCodonFrames()); };
823 ((Alignment) al2).alignAs(al1);
824 assertEquals("-NMP-R", al1.getSequenceAt(0).getSequenceAsString());
825 assertEquals("VX--YA", al1.getSequenceAt(1).getSequenceAsString());
826 assertEquals("-KQY-L", al2.getSequenceAt(0).getSequenceAsString());
827 assertEquals("RF--PW", al2.getSequenceAt(1).getSequenceAsString());
831 * Recover TdI MSA from protein msa
833 * @throws IOException
835 @Test(groups = { "Functional" })
836 public void testAlignAs_tdi_prot() throws Exception
838 // see also AlignmentUtilsTests
839 AlignmentI al1 = loadAlignment(AA_SEQS_2, FileFormat.Fasta);
840 AlignmentI al2 = loadAlignment(TD_SEQS_2_DS, FileFormat.Fasta);
841 al1.setDataset(null);
842 al2.setDataset(al1.getDataset());
843 AlignmentI al1copy = new Alignment(al1);
844 AlignmentI al2copy = new Alignment(al2);
845 AlignmentUtils.map3diPeptideToProteinAligment(al1, al2);
846 if (al2.getCodonFrames().isEmpty()) {al2.getCodonFrames().addAll(al1.getCodonFrames()); }
847 else {al1.getCodonFrames().addAll(al2.getCodonFrames()); };
849 ((Alignment) al2).alignAs(al1);
850 assertEquals("K-QY-L", al1.getSequenceAt(0).getSequenceAsString());
851 assertEquals("-R-FPW", al1.getSequenceAt(1).getSequenceAsString());
852 assertEquals("N-MP-R", al2.getSequenceAt(0).getSequenceAsString());
853 assertEquals("-V-XYA", al2.getSequenceAt(1).getSequenceAsString());
857 * Test aligning cdna as per protein alignment.
859 * @throws IOException
861 @Test(groups = { "Functional" }, enabled = true)
862 // TODO review / update this test after redesign of alignAs method
863 public void testAlignAs_cdnaAsProtein() throws IOException
866 * Load alignments and add mappings for cDNA to protein
868 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, FileFormat.Fasta);
869 AlignmentI al2 = loadAlignment(AA_SEQS_1, FileFormat.Fasta);
870 makeMappings(al1, al2);
873 * Realign DNA; currently keeping existing gaps in introns only
875 ((Alignment) al1).alignAs(al2, false, true);
876 assertEquals("ACG---GCUCCA------ACT---",
877 al1.getSequenceAt(0).getSequenceAsString());
878 assertEquals("---CGT---TAACGA---AGT---",
879 al1.getSequenceAt(1).getSequenceAsString());
883 * Test aligning cdna as per protein - single sequences
885 * @throws IOException
887 @Test(groups = { "Functional" }, enabled = true)
888 // TODO review / update this test after redesign of alignAs method
889 public void testAlignAs_cdnaAsProtein_singleSequence() throws IOException
892 * simple case insert one gap
894 verifyAlignAs(">dna\nCAAaaa\n", ">protein\nQ-K\n", "CAA---aaa");
897 * simple case but with sequence offsets
899 verifyAlignAs(">dna/5-10\nCAAaaa\n", ">protein/20-21\nQ-K\n",
903 * insert gaps as per protein, drop gaps within codons
905 verifyAlignAs(">dna/10-18\nCA-Aa-aa--AGA\n", ">aa/6-8\n-Q-K--R\n",
906 "---CAA---aaa------AGA");
910 * test mapping between a protein and 3di sequence alignment. Assumes 1:1
911 * @throws IOException
913 @Test(groups={"Functional"},enabled=true)
914 public void testAlignAs_3di() throws IOException
916 String protAl = ">1ji5_A\n"
917 + "-----------------------------DQPVLLLLLLQLLLLLVLLLQQLVVCLVQAD\n"
918 + "DPCNVVSNVVSVVSSVVSVVSNVVSQVVCVVVVHHHDDDVSSVVRYPQDHHDPP--DYPL\n"
919 + "RSLVSLLVSLVVVLVSLVVSLVSCVVVVNVVSNVSSVVVSVVSVVSNVVSCVVVVD----\n"
920 + "---------------------------------------------------\n"
922 + "---------------------------DALLVVLLLLLLQLLLALVLLLQQLVLCLVLAD\n"
923 + "DPCNVVSNVVSVVVSVVSVVSNVVSQVVCVVSVHHHDDDVSSVVRYPQDHDDSP--DYPL\n"
924 + "RSLVSLLVSLVVLLVSLVVSLVSCVVNVNPVSNVSSVVSSVVSVVSNVVSVVVND-----\n"
925 + "---------------------------------------------------\n"
927 String tdiAl = ">1ji5_A\n"
928 + "-----------------------------MNKQVIEVLNKQVADWSVLFTKLHNFHWYVK\n"
929 + "GPQFFTLHEKFEELYTESATHIDEIAERILAIGGKPVATKEYLEISSIQEAAYG--ETAE\n"
930 + "GMVEAIMKDYEMMLVELKKGMEIAQNSDDEMTSDLLLGIYTELEKHAWMLRAFLNQ----\n"
931 + "---------------------------------------------------\n"
933 + "---------------------------MSTKTNVVEVLNKQVANWNVLYVKLHNYHWYVT\n"
934 + "GPHFFTLHEKFEEFYNEAGTYIDELAERILALEGKPLATKEYLATSSVNEGTSK--ESAE\n"
935 + "EMVQTLVNDYSALIQELKEGMEVAGEAGDATSADMLLAIHTTLEQHVWMLSAFLK-----\n"
936 + "---------------------------------------------------\n" + "";
937 AlignmentI prot = loadAlignment(protAl, FileFormat.Fasta);
938 ((Alignment) prot).createDatasetAlignment();
940 AlignmentI tdi = loadAlignment(tdiAl, FileFormat.Fasta);
941 assertTrue(AlignmentUtils.map3diPeptideToProteinAligment(prot, tdi));
943 AlignmentI newProt = new Alignment(
945 { prot.getSequenceAt(0).getSubSequence(25, 35),
946 prot.getSequenceAt(1).getSubSequence(35, 45) });
947 newProt.setDataset(prot.getDataset());
949 // TODO Find matching tdi sequence and construct alignment mirroring
950 // the protein alignment
951 // Alignment newTdi = new CrossRef(newProt.getSequencesArray(),
952 // newProt.getDataset()).findXrefSequences("", false);
954 // newTdi.alignAs(newProt);
956 // System.out.println("newProt - aa\n"+new
957 // FastaFile().print(newProt.getSequencesArray(), true));
958 // System.out.println("newProt - 3di\n"+new
959 // FastaFile().print(newTdi.getSequencesArray(), true));
963 * Helper method that makes mappings and then aligns the first alignment as
969 * @throws IOException
971 public void verifyAlignAs(String fromSeqs, String toSeqs, String expected)
975 * Load alignments and add mappings from nucleotide to protein (or from
976 * first to second if both the same type)
978 AlignmentI al1 = loadAlignment(fromSeqs, FileFormat.Fasta);
979 AlignmentI al2 = loadAlignment(toSeqs, FileFormat.Fasta);
980 makeMappings(al1, al2);
983 * Realign DNA; currently keeping existing gaps in introns only
985 ((Alignment) al1).alignAs(al2, false, true);
986 assertEquals(expected, al1.getSequenceAt(0).getSequenceAsString());
990 * Helper method to make mappings between sequences, and add the mappings to
991 * the 'mapped from' alignment. If alFrom.isNucleotide() == alTo.isNucleotide() then ratio is always 1:1
996 public void makeMappings(AlignmentI alFrom, AlignmentI alTo)
998 int ratio = (alFrom.isNucleotide() == alTo.isNucleotide() ? 1 : 3);
1000 AlignedCodonFrame acf = new AlignedCodonFrame();
1002 for (int i = 0; i < alFrom.getHeight(); i++)
1004 SequenceI seqFrom = alFrom.getSequenceAt(i);
1005 SequenceI seqTo = alTo.getSequenceAt(i);
1006 MapList ml = new MapList(
1008 { seqFrom.getStart(), seqFrom.getEnd() },
1010 { seqTo.getStart(), seqTo.getEnd() }, ratio, 1);
1011 acf.addMap(seqFrom, seqTo, ml);
1015 * not sure whether mappings 'belong' or protein or nucleotide
1016 * alignment, so adding to both ;~)
1018 alFrom.addCodonFrame(acf);
1019 alTo.addCodonFrame(acf);
1023 * Test aligning dna as per protein alignment, for the case where there are
1024 * introns (i.e. some dna sites have no mapping from a peptide).
1026 * @throws IOException
1028 @Test(groups = { "Functional" }, enabled = false)
1029 // TODO review / update this test after redesign of alignAs method
1030 public void testAlignAs_dnaAsProtein_withIntrons() throws IOException
1033 * Load alignments and add mappings for cDNA to protein
1035 String dna1 = "A-Aa-gG-GCC-cT-TT";
1036 String dna2 = "c--CCGgg-TT--T-AA-A";
1037 AlignmentI al1 = loadAlignment(
1038 ">Dna1/6-17\n" + dna1 + "\n>Dna2/20-31\n" + dna2 + "\n",
1040 AlignmentI al2 = loadAlignment(
1041 ">Pep1/7-9\n-P--YK\n>Pep2/11-13\nG-T--F\n", FileFormat.Fasta);
1042 AlignedCodonFrame acf = new AlignedCodonFrame();
1043 // Seq1 has intron at dna positions 3,4,9 so splice is AAG GCC TTT
1044 // Seq2 has intron at dna positions 1,5,6 so splice is CCG TTT AAA
1045 MapList ml1 = new MapList(new int[] { 6, 7, 10, 13, 15, 17 },
1048 acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml1);
1049 MapList ml2 = new MapList(new int[] { 21, 23, 26, 31 },
1052 acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml2);
1053 al2.addCodonFrame(acf);
1056 * Align ignoring gaps in dna introns and exons
1058 ((Alignment) al1).alignAs(al2, false, false);
1059 assertEquals("---AAagG------GCCcTTT",
1060 al1.getSequenceAt(0).getSequenceAsString());
1061 // note 1 gap in protein corresponds to 'gg-' in DNA (3 positions)
1062 assertEquals("cCCGgg-TTT------AAA",
1063 al1.getSequenceAt(1).getSequenceAsString());
1066 * Reset and realign, preserving gaps in dna introns and exons
1068 al1.getSequenceAt(0).setSequence(dna1);
1069 al1.getSequenceAt(1).setSequence(dna2);
1070 ((Alignment) al1).alignAs(al2, true, true);
1071 // String dna1 = "A-Aa-gG-GCC-cT-TT";
1072 // String dna2 = "c--CCGgg-TT--T-AA-A";
1073 // assumption: we include 'the greater of' protein/dna gap lengths, not both
1074 assertEquals("---A-Aa-gG------GCC-cT-TT",
1075 al1.getSequenceAt(0).getSequenceAsString());
1076 assertEquals("c--CCGgg-TT--T------AA-A",
1077 al1.getSequenceAt(1).getSequenceAsString());
1080 @Test(groups = "Functional")
1081 public void testCopyConstructor() throws IOException
1083 AlignmentI protein = loadAlignment(AA_SEQS_1, FileFormat.Fasta);
1084 // create sequence and alignment datasets
1085 protein.setDataset(null);
1086 AlignedCodonFrame acf = new AlignedCodonFrame();
1087 List<AlignedCodonFrame> acfList = Arrays
1088 .asList(new AlignedCodonFrame[]
1090 protein.getDataset().setCodonFrames(acfList);
1091 AlignmentI copy = new Alignment(protein);
1094 * copy has different aligned sequences but the same dataset sequences
1096 assertFalse(copy.getSequenceAt(0) == protein.getSequenceAt(0));
1097 assertFalse(copy.getSequenceAt(1) == protein.getSequenceAt(1));
1098 assertSame(copy.getSequenceAt(0).getDatasetSequence(),
1099 protein.getSequenceAt(0).getDatasetSequence());
1100 assertSame(copy.getSequenceAt(1).getDatasetSequence(),
1101 protein.getSequenceAt(1).getDatasetSequence());
1103 // TODO should the copy constructor copy the dataset?
1104 // or make a new one referring to the same dataset sequences??
1105 assertNull(copy.getDataset());
1106 // TODO test metadata is copied when AlignmentI is a dataset
1108 // assertArrayEquals(copy.getDataset().getSequencesArray(), protein
1109 // .getDataset().getSequencesArray());
1113 * Test behaviour of createDataset
1115 * @throws IOException
1117 @Test(groups = "Functional")
1118 public void testCreateDatasetAlignment() throws IOException
1120 AlignmentI protein = new FormatAdapter().readFile(AA_SEQS_1,
1121 DataSourceType.PASTE, FileFormat.Fasta);
1123 * create a dataset sequence on first sequence
1124 * leave the second without one
1126 protein.getSequenceAt(0).createDatasetSequence();
1127 assertNotNull(protein.getSequenceAt(0).getDatasetSequence());
1128 assertNull(protein.getSequenceAt(1).getDatasetSequence());
1131 * add a mapping to the alignment
1133 AlignedCodonFrame acf = new AlignedCodonFrame();
1134 protein.addCodonFrame(acf);
1135 assertNull(protein.getDataset());
1136 assertTrue(protein.getCodonFrames().contains(acf));
1139 * create the alignment dataset
1140 * note this creates sequence datasets where missing
1141 * as a side-effect (in this case, on seq2
1143 // TODO promote this method to AlignmentI
1144 ((Alignment) protein).createDatasetAlignment();
1146 AlignmentI ds = protein.getDataset();
1148 // side-effect: dataset created on second sequence
1149 assertNotNull(protein.getSequenceAt(1).getDatasetSequence());
1150 // dataset alignment has references to dataset sequences
1151 assertEquals(ds.getSequenceAt(0),
1152 protein.getSequenceAt(0).getDatasetSequence());
1153 assertEquals(ds.getSequenceAt(1),
1154 protein.getSequenceAt(1).getDatasetSequence());
1156 // codon frames should have been moved to the dataset
1157 // getCodonFrames() should delegate to the dataset:
1158 assertTrue(protein.getCodonFrames().contains(acf));
1159 // prove the codon frames are indeed on the dataset:
1160 assertTrue(ds.getCodonFrames().contains(acf));
1164 * tests the addition of *all* sequences referred to by a sequence being added
1167 @Test(groups = "Functional")
1168 public void testCreateDatasetAlignmentWithMappedToSeqs()
1170 // Alignment with two sequences, gapped.
1171 SequenceI sq1 = new Sequence("sq1", "A--SDF");
1172 SequenceI sq2 = new Sequence("sq2", "G--TRQ");
1174 // cross-references to two more sequences.
1175 DBRefEntry dbr = new DBRefEntry("SQ1", "", "sq3");
1176 SequenceI sq3 = new Sequence("sq3", "VWANG");
1178 new Mapping(sq3, new MapList(new int[]
1179 { 1, 4 }, new int[] { 2, 5 }, 1, 1)));
1182 SequenceI sq4 = new Sequence("sq4", "ERKWI");
1183 DBRefEntry dbr2 = new DBRefEntry("SQ2", "", "sq4");
1185 new Mapping(sq4, new MapList(new int[]
1186 { 1, 4 }, new int[] { 2, 5 }, 1, 1)));
1188 // and a 1:1 codonframe mapping between them.
1189 AlignedCodonFrame alc = new AlignedCodonFrame();
1190 alc.addMap(sq1, sq2,
1191 new MapList(new int[]
1192 { 1, 4 }, new int[] { 1, 4 }, 1, 1));
1194 AlignmentI protein = new Alignment(new SequenceI[] { sq1, sq2 });
1197 * create the alignment dataset
1198 * note this creates sequence datasets where missing
1199 * as a side-effect (in this case, on seq2
1202 // TODO promote this method to AlignmentI
1203 ((Alignment) protein).createDatasetAlignment();
1205 AlignmentI ds = protein.getDataset();
1207 // should be 4 sequences in dataset - two materialised, and two propagated
1209 assertEquals(4, ds.getHeight());
1210 assertTrue(ds.getSequences().contains(sq1.getDatasetSequence()));
1211 assertTrue(ds.getSequences().contains(sq2.getDatasetSequence()));
1212 assertTrue(ds.getSequences().contains(sq3));
1213 assertTrue(ds.getSequences().contains(sq4));
1214 // Should have one codon frame mapping between sq1 and sq2 via dataset
1216 assertEquals(ds.getCodonFrame(sq1.getDatasetSequence()),
1217 ds.getCodonFrame(sq2.getDatasetSequence()));
1220 @Test(groups = "Functional")
1221 public void testAddCodonFrame()
1223 AlignmentI align = new Alignment(new SequenceI[] {});
1224 AlignedCodonFrame acf = new AlignedCodonFrame();
1225 align.addCodonFrame(acf);
1226 assertEquals(1, align.getCodonFrames().size());
1227 assertTrue(align.getCodonFrames().contains(acf));
1228 // can't add the same object twice:
1229 align.addCodonFrame(acf);
1230 assertEquals(1, align.getCodonFrames().size());
1232 // create dataset alignment - mappings move to dataset
1233 ((Alignment) align).createDatasetAlignment();
1234 assertSame(align.getCodonFrames(), align.getDataset().getCodonFrames());
1235 assertEquals(1, align.getCodonFrames().size());
1237 AlignedCodonFrame acf2 = new AlignedCodonFrame();
1238 align.addCodonFrame(acf2);
1239 assertTrue(align.getDataset().getCodonFrames().contains(acf));
1242 @Test(groups = "Functional")
1243 public void testAddSequencePreserveDatasetIntegrity()
1245 Sequence seq = new Sequence("testSeq", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
1246 Alignment align = new Alignment(new SequenceI[] { seq });
1247 align.createDatasetAlignment();
1248 AlignmentI ds = align.getDataset();
1249 SequenceI copy = new Sequence(seq);
1250 copy.insertCharAt(3, 5, '-');
1251 align.addSequence(copy);
1252 Assert.assertEquals(align.getDataset().getHeight(), 1,
1253 "Dataset shouldn't have more than one sequence.");
1255 Sequence seq2 = new Sequence("newtestSeq",
1256 "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
1257 align.addSequence(seq2);
1258 Assert.assertEquals(align.getDataset().getHeight(), 2,
1259 "Dataset should now have two sequences.");
1261 assertAlignmentDatasetRefs(align,
1262 "addSequence broke dataset reference integrity");
1266 * Tests that dbrefs with mappings to sequence get updated if the sequence
1267 * acquires a dataset sequence
1269 @Test(groups = "Functional")
1270 public void testCreateDataset_updateDbrefMappings()
1272 SequenceI pep = new Sequence("pep", "ASD");
1273 SequenceI dna = new Sequence("dna", "aaaGCCTCGGATggg");
1274 SequenceI cds = new Sequence("cds", "GCCTCGGAT");
1276 // add dbref from dna to peptide
1277 DBRefEntry dbr = new DBRefEntry("UNIPROT", "", "pep");
1279 new Mapping(pep, new MapList(new int[]
1280 { 4, 15 }, new int[] { 1, 4 }, 3, 1)));
1283 // add dbref from dna to peptide
1284 DBRefEntry dbr2 = new DBRefEntry("UNIPROT", "", "pep");
1286 new Mapping(pep, new MapList(new int[]
1287 { 1, 12 }, new int[] { 1, 4 }, 3, 1)));
1290 // add dbref from peptide to dna
1291 DBRefEntry dbr3 = new DBRefEntry("EMBL", "", "dna");
1293 new Mapping(dna, new MapList(new int[]
1294 { 1, 4 }, new int[] { 4, 15 }, 1, 3)));
1297 // add dbref from peptide to cds
1298 DBRefEntry dbr4 = new DBRefEntry("EMBLCDS", "", "cds");
1300 new Mapping(cds, new MapList(new int[]
1301 { 1, 4 }, new int[] { 1, 12 }, 1, 3)));
1304 AlignmentI protein = new Alignment(new SequenceI[] { pep });
1307 * create the alignment dataset
1309 ((Alignment) protein).createDatasetAlignment();
1311 AlignmentI ds = protein.getDataset();
1313 // should be 3 sequences in dataset
1314 assertEquals(3, ds.getHeight());
1315 assertTrue(ds.getSequences().contains(pep.getDatasetSequence()));
1316 assertTrue(ds.getSequences().contains(dna));
1317 assertTrue(ds.getSequences().contains(cds));
1320 * verify peptide.cdsdbref.peptidedbref is now mapped to peptide dataset
1322 List<DBRefEntry> dbRefs = pep.getDBRefs();
1323 assertEquals(2, dbRefs.size());
1324 assertSame(dna, dbRefs.get(0).map.to);
1325 assertSame(cds, dbRefs.get(1).map.to);
1326 assertEquals(1, dna.getDBRefs().size());
1327 assertSame(pep.getDatasetSequence(), dna.getDBRefs().get(0).map.to);
1328 assertEquals(1, cds.getDBRefs().size());
1329 assertSame(pep.getDatasetSequence(), cds.getDBRefs().get(0).map.to);
1332 @Test(groups = { "Functional" })
1333 public void testFindGroup()
1335 SequenceI seq1 = new Sequence("seq1", "ABCDEF---GHI");
1336 SequenceI seq2 = new Sequence("seq2", "---JKLMNO---");
1337 AlignmentI a = new Alignment(new SequenceI[] { seq1, seq2 });
1339 assertNull(a.findGroup(null, 0));
1340 assertNull(a.findGroup(seq1, 1));
1341 assertNull(a.findGroup(seq1, -1));
1344 * add a group consisting of just "DEF"
1346 SequenceGroup sg1 = new SequenceGroup();
1347 sg1.addSequence(seq1, false);
1352 assertNull(a.findGroup(seq1, 2)); // position not in group
1353 assertNull(a.findGroup(seq1, 6)); // position not in group
1354 assertNull(a.findGroup(seq2, 5)); // sequence not in group
1355 assertSame(a.findGroup(seq1, 3), sg1); // yes
1356 assertSame(a.findGroup(seq1, 4), sg1);
1357 assertSame(a.findGroup(seq1, 5), sg1);
1360 * add a group consisting of
1364 SequenceGroup sg2 = new SequenceGroup();
1365 sg2.addSequence(seq1, false);
1366 sg2.addSequence(seq2, false);
1371 assertNull(a.findGroup(seq1, 2)); // unchanged
1372 assertSame(a.findGroup(seq1, 3), sg1); // unchanged
1374 * if a residue is in more than one group, method returns
1375 * the first found (in order groups were added)
1377 assertSame(a.findGroup(seq1, 4), sg1);
1378 assertSame(a.findGroup(seq1, 5), sg1);
1381 * seq2 only belongs to the second group
1383 assertSame(a.findGroup(seq2, 4), sg2);
1384 assertSame(a.findGroup(seq2, 5), sg2);
1385 assertSame(a.findGroup(seq2, 6), sg2);
1386 assertSame(a.findGroup(seq2, 7), sg2);
1387 assertNull(a.findGroup(seq2, 3));
1388 assertNull(a.findGroup(seq2, 8));
1391 @Test(groups = { "Functional" })
1392 public void testDeleteSequenceByIndex()
1394 // create random alignment
1395 AlignmentGenerator gen = new AlignmentGenerator(false);
1396 AlignmentI a = gen.generate(20, 15, 123, 5, 5);
1398 // delete sequence 10, alignment reduced by 1
1399 int height = a.getAbsoluteHeight();
1400 a.deleteSequence(10);
1401 assertEquals(a.getAbsoluteHeight(), height - 1);
1403 // try to delete -ve index, nothing happens
1404 a.deleteSequence(-1);
1405 assertEquals(a.getAbsoluteHeight(), height - 1);
1407 // try to delete beyond end of alignment, nothing happens
1408 a.deleteSequence(14);
1409 assertEquals(a.getAbsoluteHeight(), height - 1);
1412 @Test(groups = { "Functional" })
1413 public void testDeleteSequenceBySeq()
1415 // create random alignment
1416 AlignmentGenerator gen = new AlignmentGenerator(false);
1417 AlignmentI a = gen.generate(20, 15, 123, 5, 5);
1419 // delete sequence 10, alignment reduced by 1
1420 int height = a.getAbsoluteHeight();
1421 SequenceI seq = a.getSequenceAt(10);
1422 a.deleteSequence(seq);
1423 assertEquals(a.getAbsoluteHeight(), height - 1);
1425 // try to delete non-existent sequence, nothing happens
1426 seq = new Sequence("cds", "GCCTCGGAT");
1427 assertEquals(a.getAbsoluteHeight(), height - 1);
1430 @Test(groups = { "Functional" })
1431 public void testDeleteHiddenSequence()
1433 // create random alignment
1434 AlignmentGenerator gen = new AlignmentGenerator(false);
1435 AlignmentI a = gen.generate(20, 15, 123, 5, 5);
1437 // delete a sequence which is hidden, check it is NOT removed from hidden
1439 int height = a.getAbsoluteHeight();
1440 SequenceI seq = a.getSequenceAt(2);
1441 a.getHiddenSequences().hideSequence(seq);
1442 assertEquals(a.getHiddenSequences().getSize(), 1);
1443 a.deleteSequence(2);
1444 assertEquals(a.getAbsoluteHeight(), height - 1);
1445 assertEquals(a.getHiddenSequences().getSize(), 1);
1447 // delete a sequence which is not hidden, check hiddenSequences are not
1449 a.deleteSequence(10);
1450 assertEquals(a.getAbsoluteHeight(), height - 2);
1451 assertEquals(a.getHiddenSequences().getSize(), 1);
1455 groups = "Functional",
1456 expectedExceptions =
1457 { IllegalArgumentException.class })
1458 public void testSetDataset_selfReference()
1460 SequenceI seq = new Sequence("a", "a");
1461 AlignmentI alignment = new Alignment(new SequenceI[] { seq });
1462 alignment.setDataset(alignment);
1465 @Test(groups = "Functional")
1466 public void testAppend()
1468 SequenceI seq = new Sequence("seq1", "FRMLPSRT-A--L-");
1469 AlignmentI alignment = new Alignment(new SequenceI[] { seq });
1470 alignment.setGapCharacter('-');
1471 SequenceI seq2 = new Sequence("seq1", "KP..L.FQII.");
1472 AlignmentI alignment2 = new Alignment(new SequenceI[] { seq2 });
1473 alignment2.setGapCharacter('.');
1475 alignment.append(alignment2);
1477 assertEquals('-', alignment.getGapCharacter());
1478 assertSame(seq, alignment.getSequenceAt(0));
1479 assertEquals("KP--L-FQII-",
1480 alignment.getSequenceAt(1).getSequenceAsString());
1482 // todo test coverage for annotations, mappings, groups,
1483 // hidden sequences, properties
1487 * test that calcId == null on findOrCreate doesn't raise an NPE, and yields
1488 * an annotation with a null calcId
1491 @Test(groups = "Functional")
1492 public void testFindOrCreateForNullCalcId()
1494 SequenceI seq = new Sequence("seq1", "FRMLPSRT-A--L-");
1495 AlignmentI alignment = new Alignment(new SequenceI[] { seq });
1497 AlignmentAnnotation ala = alignment.findOrCreateAnnotation(
1498 "Temperature Factor", null, false, seq, null);
1500 assertEquals(seq, ala.sequenceRef);
1501 assertEquals("", ala.calcId);
1504 @Test(groups = "Functional")
1505 public void testPropagateInsertions()
1507 // create an alignment with no gaps - this will be the profile seq and other
1509 AlignmentGenerator gen = new AlignmentGenerator(false);
1510 AlignmentI al = gen.generate(25, 10, 1234, 0, 0);
1512 // get the profileseq
1513 SequenceI profileseq = al.getSequenceAt(0);
1514 SequenceI gappedseq = new Sequence(profileseq);
1515 gappedseq.insertCharAt(5, al.getGapCharacter());
1516 gappedseq.insertCharAt(6, al.getGapCharacter());
1517 gappedseq.insertCharAt(7, al.getGapCharacter());
1518 gappedseq.insertCharAt(8, al.getGapCharacter());
1520 // force different kinds of padding
1521 al.getSequenceAt(3).deleteChars(2, 23);
1522 al.getSequenceAt(4).deleteChars(2, 27);
1523 al.getSequenceAt(5).deleteChars(10, 27);
1525 // create an alignment view with the gapped sequence
1526 SequenceI[] seqs = new SequenceI[1];
1527 seqs[0] = gappedseq;
1528 AlignmentI newal = new Alignment(seqs);
1529 HiddenColumns hidden = new HiddenColumns();
1530 hidden.hideColumns(15, 17);
1532 AlignmentView view = new AlignmentView(newal, hidden, null, true, false,
1535 // confirm that original contigs are as expected
1536 Iterator<int[]> visible = hidden.getVisContigsIterator(0, 25, false);
1537 int[] region = visible.next();
1538 assertEquals("[0, 14]", Arrays.toString(region));
1539 region = visible.next();
1540 assertEquals("[18, 24]", Arrays.toString(region));
1542 // propagate insertions
1543 HiddenColumns result = al.propagateInsertions(profileseq, view);
1545 // confirm that the contigs have changed to account for the gaps
1546 visible = result.getVisContigsIterator(0, 25, false);
1547 region = visible.next();
1548 assertEquals("[0, 10]", Arrays.toString(region));
1549 region = visible.next();
1550 assertEquals("[14, 24]", Arrays.toString(region));
1552 // confirm the alignment has been changed so that the other sequences have
1553 // gaps inserted where the columns are hidden
1554 assertFalse(Comparison.isGap(al.getSequenceAt(1).getSequence()[10]));
1555 assertTrue(Comparison.isGap(al.getSequenceAt(1).getSequence()[11]));
1556 assertTrue(Comparison.isGap(al.getSequenceAt(1).getSequence()[12]));
1557 assertTrue(Comparison.isGap(al.getSequenceAt(1).getSequence()[13]));
1558 assertFalse(Comparison.isGap(al.getSequenceAt(1).getSequence()[14]));
1562 @Test(groups = "Functional")
1563 public void testPropagateInsertionsOverlap()
1565 // test propagateInsertions where gaps and hiddenColumns overlap
1567 // create an alignment with no gaps - this will be the profile seq and other
1569 AlignmentGenerator gen = new AlignmentGenerator(false);
1570 AlignmentI al = gen.generate(20, 10, 1234, 0, 0);
1572 // get the profileseq
1573 SequenceI profileseq = al.getSequenceAt(0);
1574 SequenceI gappedseq = new Sequence(profileseq);
1575 gappedseq.insertCharAt(5, al.getGapCharacter());
1576 gappedseq.insertCharAt(6, al.getGapCharacter());
1577 gappedseq.insertCharAt(7, al.getGapCharacter());
1578 gappedseq.insertCharAt(8, al.getGapCharacter());
1580 // create an alignment view with the gapped sequence
1581 SequenceI[] seqs = new SequenceI[1];
1582 seqs[0] = gappedseq;
1583 AlignmentI newal = new Alignment(seqs);
1585 // hide columns so that some overlap with the gaps
1586 HiddenColumns hidden = new HiddenColumns();
1587 hidden.hideColumns(7, 10);
1589 AlignmentView view = new AlignmentView(newal, hidden, null, true, false,
1592 // confirm that original contigs are as expected
1593 Iterator<int[]> visible = hidden.getVisContigsIterator(0, 20, false);
1594 int[] region = visible.next();
1595 assertEquals("[0, 6]", Arrays.toString(region));
1596 region = visible.next();
1597 assertEquals("[11, 19]", Arrays.toString(region));
1598 assertFalse(visible.hasNext());
1600 // propagate insertions
1601 HiddenColumns result = al.propagateInsertions(profileseq, view);
1603 // confirm that the contigs have changed to account for the gaps
1604 visible = result.getVisContigsIterator(0, 20, false);
1605 region = visible.next();
1606 assertEquals("[0, 4]", Arrays.toString(region));
1607 region = visible.next();
1608 assertEquals("[7, 19]", Arrays.toString(region));
1609 assertFalse(visible.hasNext());
1611 // confirm the alignment has been changed so that the other sequences have
1612 // gaps inserted where the columns are hidden
1613 assertFalse(Comparison.isGap(al.getSequenceAt(1).getSequence()[4]));
1614 assertTrue(Comparison.isGap(al.getSequenceAt(1).getSequence()[5]));
1615 assertTrue(Comparison.isGap(al.getSequenceAt(1).getSequence()[6]));
1616 assertFalse(Comparison.isGap(al.getSequenceAt(1).getSequence()[7]));
1619 @Test(groups = { "Functional" })
1620 public void testPadGaps()
1622 SequenceI seq1 = new Sequence("seq1", "ABCDEF--");
1623 SequenceI seq2 = new Sequence("seq2", "-JKLMNO--");
1624 SequenceI seq3 = new Sequence("seq2", "-PQR");
1625 AlignmentI a = new Alignment(new SequenceI[] { seq1, seq2, seq3 });
1626 a.setGapCharacter('.'); // this replaces existing gaps
1627 assertEquals("ABCDEF..", seq1.getSequenceAsString());
1629 // trailing gaps are pruned, short sequences padded with gap character
1630 assertEquals("ABCDEF.", seq1.getSequenceAsString());
1631 assertEquals(".JKLMNO", seq2.getSequenceAsString());
1632 assertEquals(".PQR...", seq3.getSequenceAsString());
1636 * Test for setHiddenColumns, to check it returns true if the hidden columns
1637 * have changed, else false
1639 @Test(groups = { "Functional" })
1640 public void testSetHiddenColumns()
1642 AlignmentI al = new Alignment(new SequenceI[] {});
1643 assertFalse(al.getHiddenColumns().hasHiddenColumns());
1645 HiddenColumns hc = new HiddenColumns();
1646 assertFalse(al.setHiddenColumns(hc)); // no change
1647 assertSame(hc, al.getHiddenColumns());
1649 hc.hideColumns(2, 4);
1650 assertTrue(al.getHiddenColumns().hasHiddenColumns());
1653 * set a different object but with the same columns hidden
1655 HiddenColumns hc2 = new HiddenColumns();
1656 hc2.hideColumns(2, 4);
1657 assertFalse(al.setHiddenColumns(hc2)); // no change
1658 assertSame(hc2, al.getHiddenColumns());
1660 assertTrue(al.setHiddenColumns(null));
1661 assertNull(al.getHiddenColumns());
1662 assertTrue(al.setHiddenColumns(hc));
1663 assertSame(hc, al.getHiddenColumns());
1665 al.getHiddenColumns().hideColumns(10, 12);
1666 hc2.hideColumns(10, 12);
1667 assertFalse(al.setHiddenColumns(hc2)); // no change
1670 * hide columns 15-16 then 17-18 in hc
1671 * hide columns 15-18 in hc2
1672 * these are not now 'equal' objects even though they
1673 * represent the same set of columns
1675 assertSame(hc2, al.getHiddenColumns());
1676 hc.hideColumns(15, 16);
1677 hc.hideColumns(17, 18);
1678 hc2.hideColumns(15, 18);
1679 assertFalse(hc.equals(hc2));
1680 assertTrue(al.setHiddenColumns(hc)); // 'changed'
1683 @Test(groups = { "Functional" })
1684 public void testGetWidth()
1686 SequenceI seq1 = new Sequence("seq1", "ABCDEF--");
1687 SequenceI seq2 = new Sequence("seq2", "-JKLMNO--");
1688 SequenceI seq3 = new Sequence("seq2", "-PQR");
1689 AlignmentI a = new Alignment(new SequenceI[] { seq1, seq2, seq3 });
1691 assertEquals(9, a.getWidth());
1693 // width includes hidden columns
1694 a.getHiddenColumns().hideColumns(2, 5);
1695 assertEquals(9, a.getWidth());
1698 @Test(groups = { "Functional" })
1699 public void testGetVisibleWidth()
1701 SequenceI seq1 = new Sequence("seq1", "ABCDEF--");
1702 SequenceI seq2 = new Sequence("seq2", "-JKLMNO--");
1703 SequenceI seq3 = new Sequence("seq2", "-PQR");
1704 AlignmentI a = new Alignment(new SequenceI[] { seq1, seq2, seq3 });
1706 assertEquals(9, a.getVisibleWidth());
1708 // width excludes hidden columns
1709 a.getHiddenColumns().hideColumns(2, 5);
1710 assertEquals(5, a.getVisibleWidth());
1713 @Test(groups = { "Functional" })
1714 public void testGetContactMap()
1717 // 1. test adding/removing/manipulating contact maps with/without associated
1718 // sequence(s) or groups
1719 // 2. For sequence associated - ensure that inserting a gap in sequence
1720 // results in the contact map being relocated accordingly
1721 // 3. RENDERER QUESTION - should contact maps reflect gaps in the alignment
1726 @Test(groups = { "Functional" })
1727 public void testEquals()
1729 SequenceI seq1 = new Sequence("seq1", "ABCDEF--");
1730 SequenceI seq2 = new Sequence("seq2", "-JKLMNO--");
1731 SequenceI seq3 = new Sequence("seq2", "-PQR");
1732 AlignmentI a = new Alignment(new SequenceI[] { seq1, seq2, seq3 });
1734 assertEquals(a.getDataset(), a.getDataset());