JAL-1270 JUnit to TestNG refactoring
[jalview.git] / test / MCview / PDBfileTest.java
1 package MCview;
2
3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertFalse;
5 import static org.testng.AssertJUnit.assertNull;
6 import static org.testng.AssertJUnit.assertSame;
7 import static org.testng.AssertJUnit.assertTrue;
8
9 import java.io.IOException;
10
11 import org.testng.annotations.Test;
12
13 import jalview.datamodel.Alignment;
14 import jalview.datamodel.AlignmentAnnotation;
15 import jalview.datamodel.AlignmentI;
16 import jalview.datamodel.PDBEntry;
17 import jalview.datamodel.Sequence;
18 import jalview.datamodel.SequenceI;
19 import jalview.io.AppletFormatAdapter;
20
21 public class PDBfileTest
22 {
23   @Test
24   public void testIsRna()
25   {
26     SequenceI seq = new Sequence("Seq1", "CGAU");
27     assertTrue(PDBfile.isRNA(seq));
28
29     seq.setSequence("CGAu");
30     assertFalse(PDBfile.isRNA(seq));
31
32     seq.setSequence("CGAT");
33     assertFalse(PDBfile.isRNA(seq));
34
35     seq.setSequence("GRSWYFLAVM");
36     assertFalse(PDBfile.isRNA(seq));
37   }
38
39   /**
40    * Test the 'high level' outputs of parsing. More detailed tests in
41    * PDBChainTest.
42    * 
43    * @throws IOException
44    */
45   @Test
46   public void testParse() throws IOException
47   {
48     /*
49      * Constructor with file path performs parse()
50      */
51     PDBfile pf = new PDBfile(false, false, false, "examples/3W5V.pdb",
52             AppletFormatAdapter.FILE);
53
54     assertEquals("3W5V", pf.id);
55     // verify no alignment annotations created
56     assertNull(getAlignmentAnnotations(pf));
57
58     assertEquals(4, pf.chains.size());
59     assertEquals("A", pf.chains.get(0).id);
60     assertEquals("B", pf.chains.get(1).id);
61     assertEquals("C", pf.chains.get(2).id);
62     assertEquals("D", pf.chains.get(3).id);
63
64     PDBChain chainA = pf.chains.get(0);
65     assertEquals(0, chainA.seqstart); // not set
66     assertEquals(0, chainA.seqend); // not set
67     assertEquals(18, chainA.sequence.getStart());
68     assertEquals(314, chainA.sequence.getEnd());
69     assertTrue(chainA.sequence.getSequenceAsString().startsWith("KCSKKQEE"));
70     assertTrue(chainA.sequence.getSequenceAsString().endsWith("WNVEVY"));
71     assertEquals("3W5V|A", chainA.sequence.getName());
72     assertNull(chainA.sequence.getAnnotation());
73     assertEquals(1, chainA.sequence.getPDBId().size());
74     PDBEntry pdb = chainA.sequence.getPDBId().get(0);
75     assertEquals("A", pdb.getChainCode());
76     assertEquals("PDB", pdb.getType());
77     assertEquals("3W5V", pdb.getId());
78
79     PDBChain chainB = pf.chains.get(1);
80     assertEquals(1, chainB.sequence.getStart());
81     assertEquals(96, chainB.sequence.getEnd());
82     assertTrue(chainB.sequence.getSequenceAsString().startsWith("ATYNVK"));
83     assertTrue(chainB.sequence.getSequenceAsString().endsWith("KEEELT"));
84     assertEquals("3W5V|B", chainB.sequence.getName());
85
86     PDBChain chainC = pf.chains.get(2);
87     assertEquals(18, chainC.sequence.getStart());
88     assertEquals(314, chainC.sequence.getEnd());
89     assertTrue(chainC.sequence.getSequenceAsString().startsWith("KCSKKQEE"));
90     assertTrue(chainC.sequence.getSequenceAsString().endsWith("WNVEVY"));
91     assertEquals("3W5V|C", chainC.sequence.getName());
92
93     PDBChain chainD = pf.chains.get(3);
94     assertEquals(1, chainD.sequence.getStart());
95     assertEquals(96, chainD.sequence.getEnd());
96     assertTrue(chainD.sequence.getSequenceAsString().startsWith("ATYNVK"));
97     assertTrue(chainD.sequence.getSequenceAsString().endsWith("KEEELT"));
98     assertEquals("3W5V|D", chainD.sequence.getName());
99   }
100
101   /**
102    * Test parsing, with annotations added to the alignment but no secondary
103    * structure prediction
104    * 
105    * @throws IOException
106    */
107   @Test
108   public void testParse_withAnnotations_noSS() throws IOException
109   {
110     PDBfile pf = new PDBfile(true, false, false, "examples/3W5V.pdb",
111             AppletFormatAdapter.FILE);
112
113     AlignmentAnnotation[] anns = getAlignmentAnnotations(pf);
114     assertEquals(4, anns.length);
115
116     /*
117      * Inspect temp factor annotation for chain A
118      */
119     AlignmentAnnotation chainAnnotation = anns[0];
120     assertEquals("Temperature Factor", chainAnnotation.label);
121     // PDBChain constructor changes PDB id to lower case (why?)
122     assertEquals("Temperature Factor for 3w5vA",
123             chainAnnotation.description);
124     assertSame(pf.getSeqs().get(0), chainAnnotation.sequenceRef);
125     assertEquals(AlignmentAnnotation.LINE_GRAPH, chainAnnotation.graph);
126     assertEquals(0f, chainAnnotation.graphMin, 0.001f);
127     assertEquals(40f, chainAnnotation.graphMax, 0.001f);
128     assertEquals(297, chainAnnotation.annotations.length);
129     assertEquals(40f, chainAnnotation.annotations[0].value, 0.001f);
130
131     /*
132      * Chain B temp factor
133      */
134     chainAnnotation = anns[1];
135     assertEquals("Temperature Factor for 3w5vB",
136             chainAnnotation.description);
137     assertSame(pf.getSeqs().get(1), chainAnnotation.sequenceRef);
138     assertEquals(96, chainAnnotation.annotations.length);
139
140     /*
141      * Chain C temp factor
142      */
143     chainAnnotation = anns[2];
144     assertEquals("Temperature Factor for 3w5vC",
145             chainAnnotation.description);
146     assertSame(pf.getSeqs().get(2), chainAnnotation.sequenceRef);
147     assertEquals(297, chainAnnotation.annotations.length);
148
149     /*
150      * Chain D temp factor
151      */
152     chainAnnotation = anns[3];
153     assertEquals("Temperature Factor for 3w5vD",
154             chainAnnotation.description);
155     assertSame(pf.getSeqs().get(3), chainAnnotation.sequenceRef);
156     assertEquals(96, chainAnnotation.annotations.length);
157   }
158
159   /**
160    * Test parsing including secondary structure annotation using JMol; this test
161    * for the case where flag to add annotations to alignment is set false
162    * 
163    * @throws IOException
164    */
165   @Test
166   public void testParse_withJmol_noAnnotations() throws IOException
167   {
168     PDBfile pf = new PDBfile(false, true, false, "examples/3W5V.pdb",
169             AppletFormatAdapter.FILE);
170
171     /*
172      * alignment annotations _are_ created anyway (in
173      * AlignSeq.replaceMatchingSeqsWith())
174      */
175     final AlignmentAnnotation[] anns = getAlignmentAnnotations(pf);
176     assertEquals(4, anns.length);
177
178     /*
179      * no sequence annotations created - tempFactor annotation is not added
180      * unless the flag to 'addAlignmentAnnotations' is set true
181      */
182     for (PDBChain c : pf.chains)
183     {
184       assertNull(c.sequence.getAnnotation());
185     }
186   }
187
188   /**
189    * Test parsing including secondary structure prediction and annotation using
190    * JMol
191    * 
192    * @throws IOException
193    */
194   @Test
195   public void testParse_withJmolAddAlignmentAnnotations()
196           throws IOException
197   {
198     PDBfile pf = new PDBfile(true, true, false, "examples/3W5V.pdb",
199             AppletFormatAdapter.FILE);
200
201     /*
202      * Alignment annotations for TempFactor, SecStruct, per sequence (chain)
203      */
204     AlignmentAnnotation[] anns = getAlignmentAnnotations(pf);
205     assertEquals(8, anns.length);
206
207     /*
208      * other tests have detailed assertions for Temp Factor annotations
209      */
210     assertEquals("Temperature Factor for 3w5vA", anns[1].description);
211     assertEquals("Temperature Factor for 3w5vB", anns[3].description);
212     assertEquals("Temperature Factor for 3w5vC", anns[5].description);
213     assertEquals("Temperature Factor for 3w5vD", anns[7].description);
214
215     /*
216      * PDBFileWithJmol (unlike PDBChain!) leaves PDB id upper case
217      */
218     assertEquals("Secondary Structure for 3W5VA", anns[0].description);
219     assertEquals("Secondary Structure for 3W5VB", anns[2].description);
220     assertEquals("Secondary Structure for 3W5VC", anns[4].description);
221     assertEquals("Secondary Structure for 3W5VD", anns[6].description);
222
223     /*
224      * Verify SS annotations are linked to respective sequences (chains)
225      */
226     assertSame(pf.getSeqs().get(0), anns[0].sequenceRef);
227     assertSame(pf.getSeqs().get(1), anns[2].sequenceRef);
228     assertSame(pf.getSeqs().get(2), anns[4].sequenceRef);
229     assertSame(pf.getSeqs().get(3), anns[6].sequenceRef);
230
231     /*
232      * Verify a sample of SS predictions
233      */
234     for (int i = 0; i < 20; i++)
235     {
236       assertNull(anns[0].annotations[i]);
237       assertEquals("E", anns[0].annotations[20].displayCharacter);
238       assertEquals('E', anns[0].annotations[20].secondaryStructure);
239       assertEquals("E", anns[2].annotations[18].displayCharacter);
240       assertEquals("H", anns[2].annotations[23].displayCharacter);
241     }
242   }
243
244   /**
245    * Placeholder for a test of parsing RNA structure with secondary structure
246    * prediction using the Annotate3D service
247    * 
248    * @throws IOException
249    */
250
251   @Test(enabled = false)
252   public void testParse_withAnnotate3D() throws IOException
253   {
254     // TODO requires a mock for Annotate3D processing
255     // and/or run as an integration test
256     PDBfile pf = new PDBfile(true, true, true, "examples/2GIS.pdb",
257             AppletFormatAdapter.FILE);
258   }
259   /**
260    * Helper method to extract parsed annotations from the PDBfile
261    * 
262    * @param pf
263    * @return
264    */
265   private AlignmentAnnotation[] getAlignmentAnnotations(PDBfile pf)
266   {
267     AlignmentI al = new Alignment(pf.getSeqsAsArray());
268     pf.addAnnotations((Alignment) al);
269     return al.getAlignmentAnnotation();
270   }
271   }