+
+ @Test(groups = { "Functional" })
+ public void testDeleteAllAnnotations_includingAutocalculated()
+ {
+ AlignmentAnnotation aa = new AlignmentAnnotation("Consensus",
+ "Consensus", 0.5);
+ aa.autoCalculated = true;
+ al.addAnnotation(aa);
+ AlignmentAnnotation[] anns = al.getAlignmentAnnotation();
+ assertEquals("Wrong number of annotations before deleting", 4,
+ anns.length);
+ al.deleteAllAnnotations(true);
+ assertEquals("Not all deleted", 0, al.getAlignmentAnnotation().length);
+ }
+
+ @Test(groups = { "Functional" })
+ public void testDeleteAllAnnotations_excludingAutocalculated()
+ {
+ AlignmentAnnotation aa = new AlignmentAnnotation("Consensus",
+ "Consensus", 0.5);
+ aa.autoCalculated = true;
+ al.addAnnotation(aa);
+ AlignmentAnnotation[] anns = al.getAlignmentAnnotation();
+ assertEquals("Wrong number of annotations before deleting", 4,
+ anns.length);
+ al.deleteAllAnnotations(false);
+ assertEquals("Not just one annotation left", 1,
+ al.getAlignmentAnnotation().length);
+ }
+
+ /**
+ * Tests for realigning as per a supplied alignment: Dna as Dna.
+ *
+ * Note: AlignedCodonFrame's state variables are named for protein-to-cDNA
+ * mapping, but can be exploited for a general 'sequence-to-sequence' mapping
+ * as here.
+ *
+ * @throws IOException
+ */
+ @Test(groups = { "Functional" })
+ public void testAlignAs_dnaAsDna() throws IOException
+ {
+ // aligned cDNA:
+ AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
+ // unaligned cDNA:
+ AlignmentI al2 = loadAlignment(CDNA_SEQS_2, "FASTA");
+
+ /*
+ * Make mappings between sequences. The 'aligned cDNA' is playing the role
+ * of what would normally be protein here.
+ */
+ makeMappings(al1, al2);
+
+ ((Alignment) al2).alignAs(al1, false, true);
+ assertEquals("GC-TC--GUC-GTACT", al2.getSequenceAt(0)
+ .getSequenceAsString());
+ assertEquals("-GG-GTC--AGG--CAGT", al2.getSequenceAt(1)
+ .getSequenceAsString());
+ }
+
+ /**
+ * Aligning protein from cDNA.
+ *
+ * @throws IOException
+ */
+ @Test(groups = { "Functional" })
+ public void testAlignAs_proteinAsCdna() throws IOException
+ {
+ // see also AlignmentUtilsTests
+ AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
+ AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA");
+ makeMappings(al1, al2);
+
+ // Fudge - alignProteinAsCdna expects mappings to be on protein
+ al2.getCodonFrames().addAll(al1.getCodonFrames());
+
+ ((Alignment) al2).alignAs(al1, false, true);
+ assertEquals("K-Q-Y-L-", al2.getSequenceAt(0).getSequenceAsString());
+ assertEquals("-R-F-P-W", al2.getSequenceAt(1).getSequenceAsString());
+ }
+
+ /**
+ * Test aligning cdna as per protein alignment.
+ *
+ * @throws IOException
+ */
+ @Test(groups = { "Functional" }, enabled = true)
+ // TODO review / update this test after redesign of alignAs method
+ public void testAlignAs_cdnaAsProtein() throws IOException
+ {
+ /*
+ * Load alignments and add mappings for cDNA to protein
+ */
+ AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
+ AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA");
+ makeMappings(al1, al2);
+
+ /*
+ * Realign DNA; currently keeping existing gaps in introns only
+ */
+ ((Alignment) al1).alignAs(al2, false, true);
+ assertEquals("ACG---GCUCCA------ACT---", al1.getSequenceAt(0)
+ .getSequenceAsString());
+ assertEquals("---CGT---TAACGA---AGT---", al1.getSequenceAt(1)
+ .getSequenceAsString());
+ }
+
+ /**
+ * Test aligning cdna as per protein - single sequences
+ *
+ * @throws IOException
+ */
+ @Test(groups = { "Functional" }, enabled = true)
+ // TODO review / update this test after redesign of alignAs method
+ public void testAlignAs_cdnaAsProtein_singleSequence() throws IOException
+ {
+ /*
+ * simple case insert one gap
+ */
+ verifyAlignAs(">dna\nCAAaaa\n", ">protein\nQ-K\n", "CAA---aaa");
+
+ /*
+ * simple case but with sequence offsets
+ */
+ verifyAlignAs(">dna/5-10\nCAAaaa\n", ">protein/20-21\nQ-K\n",
+ "CAA---aaa");
+
+ /*
+ * insert gaps as per protein, drop gaps within codons
+ */
+ verifyAlignAs(">dna/10-18\nCA-Aa-aa--AGA\n", ">aa/6-8\n-Q-K--R\n",
+ "---CAA---aaa------AGA");
+ }
+
+ /**
+ * Helper method that makes mappings and then aligns the first alignment as
+ * the second
+ *
+ * @param fromSeqs
+ * @param toSeqs
+ * @param expected
+ * @throws IOException
+ */
+ public void verifyAlignAs(String fromSeqs, String toSeqs, String expected)
+ throws IOException
+ {
+ /*
+ * Load alignments and add mappings from nucleotide to protein (or from
+ * first to second if both the same type)
+ */
+ AlignmentI al1 = loadAlignment(fromSeqs, "FASTA");
+ AlignmentI al2 = loadAlignment(toSeqs, "FASTA");
+ makeMappings(al1, al2);
+
+ /*
+ * Realign DNA; currently keeping existing gaps in introns only
+ */
+ ((Alignment) al1).alignAs(al2, false, true);
+ assertEquals(expected, al1.getSequenceAt(0).getSequenceAsString());
+ }
+
+ /**
+ * Helper method to make mappings between sequences, and add the mappings to
+ * the 'mapped from' alignment
+ *
+ * @param alFrom
+ * @param alTo
+ */
+ public void makeMappings(AlignmentI alFrom, AlignmentI alTo)
+ {
+ int ratio = (alFrom.isNucleotide() == alTo.isNucleotide() ? 1 : 3);
+
+ AlignedCodonFrame acf = new AlignedCodonFrame();
+
+ for (int i = 0; i < alFrom.getHeight(); i++)
+ {
+ SequenceI seqFrom = alFrom.getSequenceAt(i);
+ SequenceI seqTo = alTo.getSequenceAt(i);
+ MapList ml = new MapList(new int[] { seqFrom.getStart(),
+ seqFrom.getEnd() },
+ new int[] { seqTo.getStart(), seqTo.getEnd() }, ratio, 1);
+ acf.addMap(seqFrom, seqTo, ml);
+ }
+
+ /*
+ * not sure whether mappings 'belong' or protein or nucleotide
+ * alignment, so adding to both ;~)
+ */
+ alFrom.addCodonFrame(acf);
+ alTo.addCodonFrame(acf);
+ }
+
+ /**
+ * Test aligning dna as per protein alignment, for the case where there are
+ * introns (i.e. some dna sites have no mapping from a peptide).
+ *
+ * @throws IOException
+ */
+ @Test(groups = { "Functional" }, enabled = false)
+ // TODO review / update this test after redesign of alignAs method
+ public void testAlignAs_dnaAsProtein_withIntrons() throws IOException
+ {
+ /*
+ * Load alignments and add mappings for cDNA to protein
+ */
+ String dna1 = "A-Aa-gG-GCC-cT-TT";
+ String dna2 = "c--CCGgg-TT--T-AA-A";
+ AlignmentI al1 = loadAlignment(">Dna1/6-17\n" + dna1
+ + "\n>Dna2/20-31\n" + dna2 + "\n", "FASTA");
+ AlignmentI al2 = loadAlignment(
+ ">Pep1/7-9\n-P--YK\n>Pep2/11-13\nG-T--F\n", "FASTA");
+ AlignedCodonFrame acf = new AlignedCodonFrame();
+ // Seq1 has intron at dna positions 3,4,9 so splice is AAG GCC TTT
+ // Seq2 has intron at dna positions 1,5,6 so splice is CCG TTT AAA
+ MapList ml1 = new MapList(new int[] { 6, 7, 10, 13, 15, 17 }, new int[]
+ { 7, 9 }, 3, 1);
+ acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml1);
+ MapList ml2 = new MapList(new int[] { 21, 23, 26, 31 }, new int[] { 11,
+ 13 }, 3, 1);
+ acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml2);
+ al2.addCodonFrame(acf);
+
+ /*
+ * Align ignoring gaps in dna introns and exons
+ */
+ ((Alignment) al1).alignAs(al2, false, false);
+ assertEquals("---AAagG------GCCcTTT", al1.getSequenceAt(0)
+ .getSequenceAsString());
+ // note 1 gap in protein corresponds to 'gg-' in DNA (3 positions)
+ assertEquals("cCCGgg-TTT------AAA", al1.getSequenceAt(1)
+ .getSequenceAsString());
+
+ /*
+ * Reset and realign, preserving gaps in dna introns and exons
+ */
+ al1.getSequenceAt(0).setSequence(dna1);
+ al1.getSequenceAt(1).setSequence(dna2);
+ ((Alignment) al1).alignAs(al2, true, true);
+ // String dna1 = "A-Aa-gG-GCC-cT-TT";
+ // String dna2 = "c--CCGgg-TT--T-AA-A";
+ // assumption: we include 'the greater of' protein/dna gap lengths, not both
+ assertEquals("---A-Aa-gG------GCC-cT-TT", al1.getSequenceAt(0)
+ .getSequenceAsString());
+ assertEquals("c--CCGgg-TT--T------AA-A", al1.getSequenceAt(1)
+ .getSequenceAsString());
+ }
+
+ @Test(groups = "Functional")
+ public void testCopyConstructor() throws IOException
+ {
+ AlignmentI protein = loadAlignment(AA_SEQS_1, FormatAdapter.PASTE);
+ // create sequence and alignment datasets
+ protein.setDataset(null);
+ AlignedCodonFrame acf = new AlignedCodonFrame();
+ List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
+ { acf });
+ protein.getDataset().setCodonFrames(acfList);
+ AlignmentI copy = new Alignment(protein);
+
+ /*
+ * copy has different aligned sequences but the same dataset sequences
+ */
+ assertFalse(copy.getSequenceAt(0) == protein.getSequenceAt(0));
+ assertFalse(copy.getSequenceAt(1) == protein.getSequenceAt(1));
+ assertSame(copy.getSequenceAt(0).getDatasetSequence(), protein
+ .getSequenceAt(0).getDatasetSequence());
+ assertSame(copy.getSequenceAt(1).getDatasetSequence(), protein
+ .getSequenceAt(1).getDatasetSequence());
+
+ // TODO should the copy constructor copy the dataset?
+ // or make a new one referring to the same dataset sequences??
+ assertNull(copy.getDataset());
+ // TODO test metadata is copied when AlignmentI is a dataset
+
+ // assertArrayEquals(copy.getDataset().getSequencesArray(), protein
+ // .getDataset().getSequencesArray());
+ }
+
+ /**
+ * Test behaviour of createDataset
+ *
+ * @throws IOException
+ */
+ @Test(groups = "Functional")
+ public void testCreateDatasetAlignment() throws IOException
+ {
+ AlignmentI protein = new FormatAdapter().readFile(AA_SEQS_1,
+ AppletFormatAdapter.PASTE, "FASTA");
+ /*
+ * create a dataset sequence on first sequence
+ * leave the second without one
+ */
+ protein.getSequenceAt(0).createDatasetSequence();
+ assertNotNull(protein.getSequenceAt(0).getDatasetSequence());
+ assertNull(protein.getSequenceAt(1).getDatasetSequence());
+
+ /*
+ * add a mapping to the alignment
+ */
+ AlignedCodonFrame acf = new AlignedCodonFrame();
+ protein.addCodonFrame(acf);
+ assertNull(protein.getDataset());
+ assertTrue(protein.getCodonFrames().contains(acf));
+
+ /*
+ * create the alignment dataset
+ * note this creates sequence datasets where missing
+ * as a side-effect (in this case, on seq2
+ */
+ // TODO promote this method to AlignmentI
+ ((Alignment) protein).createDatasetAlignment();
+
+ AlignmentI ds = protein.getDataset();
+
+ // side-effect: dataset created on second sequence
+ assertNotNull(protein.getSequenceAt(1).getDatasetSequence());
+ // dataset alignment has references to dataset sequences
+ assertEquals(ds.getSequenceAt(0), protein.getSequenceAt(0)
+ .getDatasetSequence());
+ assertEquals(ds.getSequenceAt(1), protein.getSequenceAt(1)
+ .getDatasetSequence());
+
+ // codon frames should have been moved to the dataset
+ // getCodonFrames() should delegate to the dataset:
+ assertTrue(protein.getCodonFrames().contains(acf));
+ // prove the codon frames are indeed on the dataset:
+ assertTrue(ds.getCodonFrames().contains(acf));
+ }
+
+ /**
+ * tests the addition of *all* sequences referred to by a sequence being added
+ * to the dataset
+ */
+ @Test(groups = "Functional")
+ public void testCreateDatasetAlignmentWithMappedToSeqs()
+ {
+ // Alignment with two sequences, gapped.
+ SequenceI sq1 = new Sequence("sq1", "A--SDF");
+ SequenceI sq2 = new Sequence("sq2", "G--TRQ");
+
+ // cross-references to two more sequences.
+ DBRefEntry dbr = new DBRefEntry("SQ1", "", "sq3");
+ SequenceI sq3 = new Sequence("sq3", "VWANG");
+ dbr.setMap(new Mapping(sq3, new MapList(new int[] { 1, 4 }, new int[] {
+ 2, 5 }, 1, 1)));
+ sq1.addDBRef(dbr);
+
+ SequenceI sq4 = new Sequence("sq4", "ERKWI");
+ DBRefEntry dbr2 = new DBRefEntry("SQ2", "", "sq4");
+ dbr2.setMap(new Mapping(sq4, new MapList(new int[] { 1, 4 }, new int[] {
+ 2, 5 }, 1, 1)));
+ sq2.addDBRef(dbr2);
+ // and a 1:1 codonframe mapping between them.
+ AlignedCodonFrame alc = new AlignedCodonFrame();
+ alc.addMap(sq1, sq2, new MapList(new int[] { 1, 4 },
+ new int[] { 1, 4 }, 1, 1));
+
+ AlignmentI protein = new Alignment(new SequenceI[] { sq1, sq2 });
+
+ /*
+ * create the alignment dataset
+ * note this creates sequence datasets where missing
+ * as a side-effect (in this case, on seq2
+ */
+
+ // TODO promote this method to AlignmentI
+ ((Alignment) protein).createDatasetAlignment();
+
+ AlignmentI ds = protein.getDataset();
+
+ // should be 4 sequences in dataset - two materialised, and two propagated
+ // from dbref
+ assertEquals(4, ds.getHeight());
+ assertTrue(ds.getSequences().contains(sq1.getDatasetSequence()));
+ assertTrue(ds.getSequences().contains(sq2.getDatasetSequence()));
+ assertTrue(ds.getSequences().contains(sq3));
+ assertTrue(ds.getSequences().contains(sq4));
+ // Should have one codon frame mapping between sq1 and sq2 via dataset
+ // sequences
+ assertEquals(ds.getCodonFrame(sq1.getDatasetSequence()),
+ ds.getCodonFrame(sq2.getDatasetSequence()));
+ }
+
+ @Test(groups = "Functional")
+ public void testAddCodonFrame()
+ {
+ AlignmentI align = new Alignment(new SequenceI[] {});
+ AlignedCodonFrame acf = new AlignedCodonFrame();
+ align.addCodonFrame(acf);
+ assertEquals(1, align.getCodonFrames().size());
+ assertTrue(align.getCodonFrames().contains(acf));
+ // can't add the same object twice:
+ align.addCodonFrame(acf);
+ assertEquals(1, align.getCodonFrames().size());
+
+ // create dataset alignment - mappings move to dataset
+ ((Alignment) align).createDatasetAlignment();
+ assertSame(align.getCodonFrames(), align.getDataset().getCodonFrames());
+ assertEquals(1, align.getCodonFrames().size());
+
+ AlignedCodonFrame acf2 = new AlignedCodonFrame();
+ align.addCodonFrame(acf2);
+ assertTrue(align.getDataset().getCodonFrames().contains(acf));
+ }
+
+ @Test(groups = "Functional")
+ public void getVisibleStartAndEndIndexTest()
+ {
+ Sequence seq = new Sequence("testSeq", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
+ AlignmentI align = new Alignment(new SequenceI[] { seq });
+ ArrayList<int[]> hiddenCols = new ArrayList<int[]>();
+
+ int[] startEnd = align.getVisibleStartAndEndIndex(hiddenCols);
+ assertEquals(0, startEnd[0]);
+ assertEquals(25, startEnd[1]);
+
+ hiddenCols.add(new int[] { 0, 0 });
+ startEnd = align.getVisibleStartAndEndIndex(hiddenCols);
+ assertEquals(1, startEnd[0]);
+ assertEquals(25, startEnd[1]);
+
+ hiddenCols.add(new int[] { 6, 9 });
+ hiddenCols.add(new int[] { 11, 12 });
+ startEnd = align.getVisibleStartAndEndIndex(hiddenCols);
+ assertEquals(1, startEnd[0]);
+ assertEquals(25, startEnd[1]);
+
+ hiddenCols.add(new int[] { 24, 25 });
+ startEnd = align.getVisibleStartAndEndIndex(hiddenCols);
+ System.out.println(startEnd[0] + " : " + startEnd[1]);
+ assertEquals(1, startEnd[0]);
+ assertEquals(23, startEnd[1]);
+ }