JAL-1619 first draft of 'linked protein and cDNA'
[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 static org.junit.Assert.fail;
7 import jalview.io.AppletFormatAdapter;
8 import jalview.io.FormatAdapter;
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 TEST_DATA2 = 
39           ">TEST21 test21\n" +
40           "AC-GG--CUC-CAA-CT\n" +
41           ">TEST22 test22\n" +
42           "-CG-TTA--ACG---AAGT\n";
43
44   private static final String TEST_DATA3 = 
45           ">TEST31 test31\n" +
46           "K-QY--L\n" +
47           ">TEST32 test32\n" +
48           "-R-FP-W-\n";
49
50   private static final String TEST_DATA4 = 
51           ">TEST41 test41\n" +
52           "GCTCGUCGTACT\n" +
53           ">TEST42 test42\n" +
54           "GGGTCAGGCAGT\n";
55   // @formatter:on
56
57   private Alignment al;
58
59   /*
60    * Read in Stockholm format test data including secondary structure
61    * annotations.
62    */
63   @Before
64   public void setUp() throws IOException
65   {
66     al = new FormatAdapter().readFile(TEST_DATA,
67             AppletFormatAdapter.PASTE, "STH");
68     int i = 0;
69     for (AlignmentAnnotation ann : al.getAlignmentAnnotation())
70     {
71       ann.setCalcId("CalcIdFor"
72               + al.getSequenceAt(i).getName());
73       i++;
74     }
75   }
76
77   /**
78    * Test method that returns annotations that match on calcId.
79    */
80   @Test
81   public void testFindAnnotation_byCalcId()
82   {
83     Iterable<AlignmentAnnotation> anns = al
84             .findAnnotation("CalcIdForD.melanogaster.2");
85     Iterator<AlignmentAnnotation> iter = anns.iterator();
86     assertTrue(iter.hasNext());
87     AlignmentAnnotation ann = iter.next();
88     assertEquals("D.melanogaster.2", ann.sequenceRef.getName());
89     assertFalse(iter.hasNext());
90   }
91
92   /**
93    * Tests for method that checks for alignment 'mappability'.
94    * 
95    * @throws IOException
96    */
97   @Test
98   public void testIsMappableTo() throws IOException
99   {
100     al = new FormatAdapter().readFile(TEST_DATA2,
101             AppletFormatAdapter.PASTE, "FASTA");
102     al.setDataset(null);
103
104     // not mappable to self
105     assertFalse(al.isMappableTo(al));
106
107     // dna mappable to protein and vice versa
108     AlignmentI alp = new FormatAdapter().readFile(TEST_DATA3,
109             AppletFormatAdapter.PASTE, "FASTA");
110     alp.setDataset(null);
111     assertTrue(al.isMappableTo(alp));
112     assertTrue(alp.isMappableTo(al));
113     assertFalse(alp.isMappableTo(alp));
114
115     // not mappable if any sequence length mismatch
116     alp.getSequenceAt(1).setSequence("-R--FP-");
117     alp.getSequenceAt(1).setDatasetSequence(new Sequence("", "RFP"));
118     assertFalse(alp.isMappableTo(al));
119     assertFalse(al.isMappableTo(alp));
120
121     // not mappable if number of sequences differs
122     alp.deleteSequence(1);
123     assertFalse(alp.isMappableTo(al));
124     assertFalse(al.isMappableTo(alp));
125   }
126
127   /**
128    * Tests for realigning as per a supplied alignment.
129    * 
130    * @throws IOException
131    */
132   @Test
133   public void testAlignAs_dnaAsDna() throws IOException
134   {
135     // aligned cDNA:
136     Alignment al1 = new FormatAdapter().readFile(TEST_DATA2,
137             AppletFormatAdapter.PASTE, "FASTA");
138     al1.setDataset(null);
139     // unaligned cDNA:
140     Alignment al2 = new FormatAdapter().readFile(TEST_DATA4,
141             AppletFormatAdapter.PASTE, "FASTA");
142     al2.setDataset(null);
143
144     al2.alignAs(al1);
145     assertEquals("GC-TC--GUC-GTA-CT", al2.getSequenceAt(0)
146             .getSequenceAsString());
147     assertEquals("-GG-GTC--AGG---CAGT", al2.getSequenceAt(1)
148             .getSequenceAsString());
149   }
150
151   /**
152    * Aligning protein from cDNA yet to be implemented.
153    * 
154    * @throws IOException
155    */
156   @Test
157   public void testAlignAs_proteinAsCdna() throws IOException
158   {
159     // aligned cDNA:
160     Alignment al1 = new FormatAdapter().readFile(TEST_DATA2,
161             AppletFormatAdapter.PASTE, "FASTA");
162     al1.setDataset(null);
163     // unaligned cDNA:
164     Alignment al2 = new FormatAdapter().readFile(TEST_DATA3,
165             AppletFormatAdapter.PASTE, "FASTA");
166     al2.setDataset(null);
167
168     try
169     {
170       al2.alignAs(al1);
171       fail("No exception thrown");
172     } catch (UnsupportedOperationException e)
173     {
174       // expected;
175     }
176   }
177
178   /**
179    * Test aligning cdna as per protein alignment.
180    * 
181    * @throws IOException
182    */
183   @Test
184   public void testAlignAs_cdnaAsProtein() throws IOException
185   {
186     // aligned cDNA:
187     Alignment al1 = new FormatAdapter().readFile(TEST_DATA2,
188             AppletFormatAdapter.PASTE, "FASTA");
189     al1.setDataset(null);
190     // unaligned cDNA:
191     Alignment al2 = new FormatAdapter().readFile(TEST_DATA3,
192             AppletFormatAdapter.PASTE, "FASTA");
193     al2.setDataset(null);
194
195     al1.alignAs(al2);
196     assertEquals("ACG---GCUCCA------ACT", al1.getSequenceAt(0)
197             .getSequenceAsString());
198     assertEquals("---CGT---TAACGA---AGT---", al1.getSequenceAt(1)
199             .getSequenceAsString());
200   }
201 }