89121556f1427e90f4b8ea2bd2f1ba424693c348
[jalview.git] / test / jalview / datamodel / AlignmentTest.java
1 package jalview.datamodel;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6 import jalview.io.AppletFormatAdapter;
7 import jalview.io.FormatAdapter;
8 import jalview.util.MapList;
9
10 import java.io.IOException;
11 import java.util.Iterator;
12
13 import org.junit.Before;
14 import org.junit.Test;
15
16 /**
17  * Unit tests for Alignment datamodel.
18  * 
19  * @author gmcarstairs
20  *
21  */
22 public class AlignmentTest
23 {
24   // @formatter:off
25   private static final String TEST_DATA = 
26           "# STOCKHOLM 1.0\n" +
27           "#=GS D.melanogaster.1 AC AY119185.1/838-902\n" +
28           "#=GS D.melanogaster.2 AC AC092237.1/57223-57161\n" +
29           "#=GS D.melanogaster.3 AC AY060611.1/560-627\n" +
30           "D.melanogaster.1          G.AGCC.CU...AUGAUCGA\n" +
31           "#=GR D.melanogaster.1 SS  ................((((\n" +
32           "D.melanogaster.2          C.AUUCAACU.UAUGAGGAU\n" +
33           "#=GR D.melanogaster.2 SS  ................((((\n" +
34           "D.melanogaster.3          G.UGGCGCU..UAUGACGCA\n" +
35           "#=GR D.melanogaster.3 SS  (.(((...(....(((((((\n" +
36           "//";
37
38   private static final String AA_SEQS_1 = 
39           ">Seq1Name\n" +
40           "K-QY--L\n" +
41           ">Seq2Name\n" +
42           "-R-FP-W-\n";
43
44   private static final String CDNA_SEQS_1 = 
45           ">Seq1Name\n" +
46           "AC-GG--CUC-CAA-CT\n" +
47           ">Seq2Name\n" +
48           "-CG-TTA--ACG---AAGT\n";
49
50   private static final String CDNA_SEQS_2 = 
51           ">Seq1Name\n" +
52           "GCTCGUCGTACT\n" +
53           ">Seq2Name\n" +
54           "GGGTCAGGCAGT\n";
55   // @formatter:on
56
57   private AlignmentI al;
58
59   /**
60    * Helper method to load an alignment and ensure dataset sequences are set up.
61    * 
62    * @param data
63    * @param format TODO
64    * @return
65    * @throws IOException
66    */
67   protected AlignmentI loadAlignment(final String data, String format) throws IOException
68   {
69     Alignment a = new FormatAdapter().readFile(data,
70             AppletFormatAdapter.PASTE, format);
71     a.setDataset(null);
72     return a;
73   }
74
75   /*
76    * Read in Stockholm format test data including secondary structure
77    * annotations.
78    */
79   @Before
80   public void setUp() throws IOException
81   {
82     al = loadAlignment(TEST_DATA, "STH");
83     int i = 0;
84     for (AlignmentAnnotation ann : al.getAlignmentAnnotation())
85     {
86       ann.setCalcId("CalcIdFor"
87               + al.getSequenceAt(i).getName());
88       i++;
89     }
90   }
91
92   /**
93    * Test method that returns annotations that match on calcId.
94    */
95   @Test
96   public void testFindAnnotation_byCalcId()
97   {
98     Iterable<AlignmentAnnotation> anns = al
99             .findAnnotation("CalcIdForD.melanogaster.2");
100     Iterator<AlignmentAnnotation> iter = anns.iterator();
101     assertTrue(iter.hasNext());
102     AlignmentAnnotation ann = iter.next();
103     assertEquals("D.melanogaster.2", ann.sequenceRef.getName());
104     assertFalse(iter.hasNext());
105   }
106
107   /**
108    * Tests for realigning as per a supplied alignment: Dna as Dna.
109    * 
110    * Note: AlignedCodonFrame's state variables are named for protein-to-cDNA
111    * mapping, but can be exploited for a general 'sequence-to-sequence' mapping
112    * as here.
113    * 
114    * @throws IOException
115    */
116   @Test
117   public void testAlignAs_dnaAsDna() throws IOException
118   {
119     // aligned cDNA:
120     AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
121     // unaligned cDNA:
122     AlignmentI al2 = loadAlignment(CDNA_SEQS_2, "FASTA");
123
124     /*
125      * Make mappings between sequences. The 'aligned cDNA' is playing the role
126      * of what would normally be protein here.
127      */
128     AlignedCodonFrame acf = new AlignedCodonFrame();
129     MapList ml = new MapList(new int[]
130     { 1, 12 }, new int[]
131     { 1, 12 }, 1, 1);
132     acf.addMap(al2.getSequenceAt(0), al1.getSequenceAt(0), ml);
133     acf.addMap(al2.getSequenceAt(1), al1.getSequenceAt(1), ml);
134     al1.addCodonFrame(acf);
135
136     al2.alignAs(al1);
137     assertEquals("GC-TC--GUC-GTA-CT", al2.getSequenceAt(0)
138             .getSequenceAsString());
139     assertEquals("-GG-GTC--AGG---CAGT", al2.getSequenceAt(1)
140             .getSequenceAsString());
141   }
142
143   /**
144    * Aligning protein from cDNA yet to be implemented, does nothing.
145    * 
146    * @throws IOException
147    */
148   @Test
149   public void testAlignAs_proteinAsCdna() throws IOException
150   {
151     AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
152     AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA");
153     String before0 = al2.getSequenceAt(0).getSequenceAsString();
154     String before1 = al2.getSequenceAt(1).getSequenceAsString();
155
156     al2.alignAs(al1);
157     assertEquals(before0, al2.getSequenceAt(0).getSequenceAsString());
158     assertEquals(before1, al2.getSequenceAt(1).getSequenceAsString());
159   }
160
161   /**
162    * Test aligning cdna as per protein alignment.
163    * 
164    * @throws IOException
165    */
166   @Test
167   public void testAlignAs_cdnaAsProtein() throws IOException
168   {
169     /*
170      * Load alignments and add mappings for cDNA to protein
171      */
172     AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
173     AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA");
174     AlignedCodonFrame acf = new AlignedCodonFrame();
175     MapList ml = new MapList(new int[]
176     { 1, 12 }, new int[]
177     { 1, 4 }, 3, 1);
178     acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml);
179     acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml);
180     al2.addCodonFrame(acf);
181
182     al1.alignAs(al2);
183     assertEquals("ACG---GCUCCA------ACT", al1.getSequenceAt(0)
184             .getSequenceAsString());
185     assertEquals("---CGT---TAACGA---AGT---", al1.getSequenceAt(1)
186             .getSequenceAsString());
187   }
188 }