+ * Test aligning cdna as per protein - single sequences
+ *
+ * @throws IOException
+ */
+ @Test(groups = { "Functional" })
+ 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 from protein to dna sequences, and add the
+ * mappings to the protein alignment
+ *
+ * @param alFrom
+ * @param alTo
+ */
+ public void makeMappings(AlignmentI alFrom, AlignmentI alTo)
+ {
+ AlignmentI prot = !alFrom.isNucleotide() ? alFrom : alTo;
+ AlignmentI nuc = alFrom == prot ? alTo : alFrom;
+
+ int ratio = (alFrom.isNucleotide() == alTo.isNucleotide() ? 1 : 3);
+
+ AlignedCodonFrame acf = new AlignedCodonFrame();
+
+ for (int i = 0; i < nuc.getHeight(); i++)
+ {
+ SequenceI seqFrom = nuc.getSequenceAt(i);
+ SequenceI seqTo = prot.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);
+ }
+
+ prot.addCodonFrame(acf);
+ }
+
+ /**