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