JAL-845 first simple unit tests added
[jalview.git] / test / jalview / analysis / DnaTest.java
1 package jalview.analysis;
2
3 import static org.junit.Assert.assertEquals;
4 import jalview.datamodel.AlignmentI;
5 import jalview.datamodel.ColumnSelection;
6 import jalview.io.FormatAdapter;
7
8 import java.io.IOException;
9
10 import org.junit.Test;
11
12 public class DnaTest
13 {
14   // AA encoding codons as ordered on the Jalview help page Amino Acid Table
15   private static String fasta = ">B\n" + "GCT" + "GCC" + "GCA" + "GCG"
16           + "TGT" + "TGC" + "GAT" + "GAC" + "GAA" + "GAG" + "TTT" + "TTC"
17           + "GGT" + "GGC" + "GGA" + "GGG" + "CAT" + "CAC" + "ATT" + "ATC"
18           + "ATA" + "AAA" + "AAG" + "TTG" + "TTA" + "CTT" + "CTC" + "CTA"
19           + "CTG" + "ATG" + "AAT" + "AAC" + "CCT" + "CCC" + "CCA" + "CCG"
20           + "CAA" + "CAG" + "CGT" + "CGC" + "CGA" + "CGG" + "AGA" + "AGG"
21           + "TCT" + "TCC" + "TCA" + "TCG" + "AGT" + "AGC" + "ACT" + "ACC"
22           + "ACA" + "ACG" + "GTT" + "GTC" + "GTA" + "GTG" + "TGG" + "TAT"
23           + "TAC" + "TAA" + "TAG" + "TGA";
24
25   /**
26    * Test simple translation to Amino Acids (with STOP codons translated to X).
27    * 
28    * @throws IOException
29    */
30   @Test
31   public void testCdnaTranslate_simple() throws IOException
32   {
33     AlignmentI alf = new FormatAdapter().readFile(fasta,
34             FormatAdapter.PASTE, "FASTA");
35     final String sequenceAsString = alf
36                 .getSequenceAt(0).getSequenceAsString();
37     AlignmentI translated = Dna.CdnaTranslate(alf.getSequencesArray(),
38             new String[]
39             { sequenceAsString }, new int[]
40             { 0, alf.getWidth() - 1 }, alf.getGapCharacter(), null,
41             alf.getWidth(), null);
42     String aa = translated.getSequenceAt(0).getSequenceAsString();
43     assertEquals(
44             "AAAACCDDEEFFGGGGHHIIIKKLLLLLLMNNPPPPQQRRRRRRSSSSSSTTTTVVVVWYYXXX",
45             aa);
46   }
47
48   /**
49    * Test translation excluding hidden columns.
50    * 
51    * @throws IOException
52    */
53   @Test
54   public void testCdnaTranslate_hiddenColumns() throws IOException
55   {
56     AlignmentI alf = new FormatAdapter().readFile(fasta,
57             FormatAdapter.PASTE, "FASTA");
58     ColumnSelection cs = new jalview.datamodel.ColumnSelection();
59     cs.hideColumns(6, 14); // hide codons 3/4/5
60     cs.hideColumns(24, 35); // hide codons 9-12
61     cs.hideColumns(177, 191); // hide codons 60-64
62     AlignmentI translated = Dna.CdnaTranslate(alf.getSequencesArray(),
63             cs.getVisibleSequenceStrings(0, alf.getWidth(),
64                     alf.getSequencesArray()), new int[]
65             { 0, alf.getWidth() - 1 }, alf.getGapCharacter(), null,
66             alf.getWidth(), null);
67     String aa = translated.getSequenceAt(0).getSequenceAsString();
68     assertEquals("AACDDGGGGHHIIIKKLLLLLLMNNPPPPQQRRRRRRSSSSSSTTTTVVVVW", aa);
69   }
70 }