JAL-1919 Fixed failing tests, enabled HETATM processing from structure file to be...
[jalview.git] / test / jalview / ext / jmol / JmolParserTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ext.jmol;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertTrue;
25
26 import jalview.bin.Cache;
27 import jalview.datamodel.Alignment;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.SequenceI;
30 import jalview.gui.AlignFrame;
31 import jalview.io.AppletFormatAdapter;
32 import jalview.io.FileLoader;
33 import jalview.structure.StructureImportSettings;
34
35 import java.util.Vector;
36
37 import org.jmol.c.STR;
38 import org.testng.annotations.BeforeMethod;
39 import org.testng.annotations.Test;
40
41 import MCview.PDBfile;
42
43 /**
44  * @author jimp
45  * 
46  */
47 public class JmolParserTest
48 {
49   /*
50    * 1GAQ has been reduced to alpha carbons only
51    * 1QCF is the full PDB file including headers, HETATM etc
52    */
53   String[] testFile = new String[] { "./examples/1GAQ.txt",
54       "./test/jalview/ext/jmol/1xyz.pdb",
55       "./test/jalview/ext/jmol/1qcf.pdb" };
56
57   //@formatter:off
58   // a modified and very cut-down extract of 4UJ4
59   String pastePDBDataWithChainBreak =
60      "HEADER    TRANSPORT PROTEIN                       08-APR-15   4UJ4\n" +
61      // chain B has missing residues; these should all go in the same sequence:
62      "ATOM   1909  CA  VAL B 358      21.329 -19.739 -67.740  1.00201.05           C\n" +
63      "ATOM   1916  CA  GLY B 359      21.694 -23.563 -67.661  1.00198.09           C\n" +
64      "ATOM   1920  CA  LYS B 367      32.471 -12.135 -77.100  1.00257.97           C\n" +
65      "ATOM   1925  CA  ALA B 368      31.032  -9.324 -74.946  1.00276.01           C\n" +
66      // switch to chain C; should be a separate sequence
67      "ATOM   1930  CA  SER C 369      32.589  -7.517 -71.978  1.00265.44           C\n" +
68      "ATOM   1936  CA  ALA C 370      31.650  -6.849 -68.346  1.00249.48           C\n";
69   //@formatter:on
70
71   //@formatter:off
72   // a very cut-down extract of 1ejg
73   String pdbWithAltLoc =
74      "HEADER    TRANSPORT PROTEIN                       08-APR-15   1EJG\n" +
75      "ATOM    448  CA  ALA A  24       6.619  16.195   1.970  1.00  1.65           C\n" +
76      "ATOM    458  CA ALEU A  25       3.048  14.822   1.781  0.57  1.48           C\n" +
77      // alternative residue 25 entries (with ILE instead of LEU) should be ignored:
78      "ATOM    478  CA BILE A  25       3.048  14.822   1.781  0.21  1.48           C\n" +
79      // including the next altloc causes the unit test to fail but it works with the full file
80      // not sure why!
81      //     "ATOM    479  CA CILE A  25       3.048  14.822   1.781  0.22  1.48           C\n" +
82      "ATOM    512  CA  CYS A  26       4.137  11.461   3.154  1.00  1.52           C\n";
83   //@formatter:on
84
85   @BeforeMethod(alwaysRun = true)
86   public void setUp()
87   {
88     Cache.applicationProperties.setProperty("STRUCT_FROM_PDB",
89             Boolean.TRUE.toString());
90     Cache.applicationProperties.setProperty("ADD_SS_ANN",
91             Boolean.TRUE.toString());
92   }
93
94   @Test(groups = { "Functional" })
95   public void testAlignmentLoader() throws Exception
96   {
97     for (String f : testFile)
98     {
99       FileLoader fl = new jalview.io.FileLoader(false);
100       AlignFrame af = fl
101               .LoadFileWaitTillLoaded(f, AppletFormatAdapter.FILE);
102       validateSecStrRows(af.getViewport().getAlignment());
103     }
104   }
105
106   @Test(groups = { "Functional" })
107   public void testFileParser() throws Exception
108   {
109     StructureImportSettings.setProcessHETATMs(false);
110     for (String pdbStr : testFile)
111     {
112       PDBfile mctest = new PDBfile(false, false, false, pdbStr,
113               AppletFormatAdapter.FILE);
114       JmolParser jtest = new JmolParser(false, false, false, pdbStr,
115               jalview.io.AppletFormatAdapter.FILE);
116       Vector<SequenceI> seqs = jtest.getSeqs(), mcseqs = mctest.getSeqs();
117
118       assertTrue(
119               "No sequences extracted from testfile\n"
120                       + (jtest.hasWarningMessage() ? jtest.getWarningMessage()
121                               : "(No warnings raised)"), seqs != null
122                       && seqs.size() > 0);
123       for (SequenceI sq : seqs)
124       {
125         assertEquals("JMol didn't process " + pdbStr
126                 + " to the same sequence as MCView",
127                 sq.getSequenceAsString(), mcseqs.remove(0)
128                         .getSequenceAsString());
129         AlignmentI al = new Alignment(new SequenceI[] { sq });
130         validateSecStrRows(al);
131       }
132     }
133     StructureImportSettings.setProcessHETATMs(true);
134     for (String pdbStr : testFile)
135     {
136       PDBfile mctest = new PDBfile(false, false, false, pdbStr,
137               AppletFormatAdapter.FILE);
138       JmolParser jtest = new JmolParser(false, false, false, pdbStr,
139               jalview.io.AppletFormatAdapter.FILE);
140       Vector<SequenceI> seqs = jtest.getSeqs(), mcseqs = mctest.getSeqs();
141
142       assertTrue(
143               "No sequences extracted from testfile\n"
144                       + (jtest.hasWarningMessage() ? jtest.getWarningMessage()
145                               : "(No warnings raised)"), seqs != null
146                       && seqs.size() > 0);
147       for (SequenceI sq : seqs)
148       {
149         assertEquals("JMol didn't process " + pdbStr
150                 + " to the same sequence as MCView",
151                 sq.getSequenceAsString(), mcseqs.remove(0)
152                         .getSequenceAsString());
153         AlignmentI al = new Alignment(new SequenceI[] { sq });
154         validateSecStrRows(al);
155       }
156     }
157   }
158
159   private void validateSecStrRows(AlignmentI al)
160   {
161     if (!al.isNucleotide())
162     {
163       for (SequenceI asq : al.getSequences())
164       {
165         SequenceI sq = asq;
166         boolean hasDs = false;
167         while (sq.getDatasetSequence() != null
168                 && sq.getAnnotation() == null)
169         {
170           sq = sq.getDatasetSequence();
171           hasDs = true;
172         }
173         checkFirstAAIsAssoc(sq);
174         if (hasDs)
175         {
176           // also verify if alignment sequence has annotation on it
177           // that is correctly mapped
178           checkFirstAAIsAssoc(asq);
179         }
180       }
181     }
182   }
183
184   private void checkFirstAAIsAssoc(SequenceI sq)
185   {
186     assertTrue("No secondary structure assigned for protein sequence.",
187             sq.getAnnotation() != null && sq.getAnnotation().length >= 1
188                     && sq.getAnnotation()[0].hasIcons);
189     assertTrue(
190             "Secondary structure not associated for sequence "
191                     + sq.getName(), sq.getAnnotation()[0].sequenceRef == sq);
192   }
193
194   /**
195    * Test parsing a chain with missing residues
196    * 
197    * @throws Exception
198    */
199   @Test(groups = { "Functional" })
200   public void testParse_missingResidues() throws Exception
201   {
202     PDBfile mctest = new PDBfile(false, false, false,
203             pastePDBDataWithChainBreak,
204             AppletFormatAdapter.PASTE);
205     boolean annotFromStructure = false;
206     boolean localSecondaryStruct = false;
207     boolean serviceSecondaryStruct = false;
208     JmolParser jtest = new JmolParser(annotFromStructure,
209             localSecondaryStruct, serviceSecondaryStruct,
210             pastePDBDataWithChainBreak,
211             jalview.io.AppletFormatAdapter.PASTE);
212     Vector<SequenceI> seqs = jtest.getSeqs();
213     Vector<SequenceI> mcseqs = mctest.getSeqs();
214
215     assertEquals("Failed to find 2 sequences\n", 2, seqs.size());
216     assertEquals("Failed to find 2 sequences\n", 2, mcseqs.size());
217     assertEquals("VGKA", seqs.get(0).getSequenceAsString());
218     assertEquals("VGKA", mcseqs.get(0).getSequenceAsString());
219     assertEquals("SA", seqs.get(1).getSequenceAsString());
220     assertEquals("SA", mcseqs.get(1).getSequenceAsString());
221   }
222
223   /**
224    * Test parsing a chain with 'altloc' residues
225    * 
226    * @throws Exception
227    */
228   @Test(groups = { "Functional" })
229   public void testParse_alternativeResidues() throws Exception
230   {
231     PDBfile mctest = new PDBfile(false, false, false, pdbWithAltLoc,
232             AppletFormatAdapter.PASTE);
233     boolean annotFromStructure = false;
234     boolean localSecondaryStruct = false;
235     boolean serviceSecondaryStruct = false;
236     JmolParser jtest = new JmolParser(annotFromStructure,
237             localSecondaryStruct, serviceSecondaryStruct, pdbWithAltLoc,
238             jalview.io.AppletFormatAdapter.PASTE);
239     Vector<SequenceI> seqs = jtest.getSeqs();
240     Vector<SequenceI> mcseqs = mctest.getSeqs();
241   
242     assertEquals("Failed to find 1 sequence\n", 1, seqs.size());
243     assertEquals("Failed to find 1 sequence\n", 1, mcseqs.size());
244     assertEquals("ALC", seqs.get(0).getSequenceAsString());
245     assertEquals("ALC", mcseqs.get(0).getSequenceAsString());
246   }
247
248   @Test(groups = "Functional")
249   public void testSetSecondaryStructure()
250   {
251     JmolParser testee = new JmolParser();
252     char[] struct = new char[10];
253     char[] structCode = new char[10];
254     struct[0] = '1';
255     structCode[0] = '1';
256
257     testee.setSecondaryStructure(STR.NONE, 0, struct, structCode);
258     testee.setSecondaryStructure(STR.HELIX, 1, struct, structCode);
259     testee.setSecondaryStructure(STR.HELIX310, 2, struct, structCode);
260     testee.setSecondaryStructure(STR.HELIXALPHA, 3, struct, structCode);
261     testee.setSecondaryStructure(STR.HELIXPI, 4, struct, structCode);
262     testee.setSecondaryStructure(STR.SHEET, 5, struct, structCode);
263
264     assertEquals(0, struct[0]);
265     assertEquals('H', struct[1]);
266     assertEquals('3', struct[2]);
267     assertEquals('H', struct[3]);
268     assertEquals('P', struct[4]);
269     assertEquals('E', struct[5]);
270
271     assertEquals(0, structCode[0]);
272     assertEquals('H', structCode[1]);
273     assertEquals('H', structCode[2]);
274     assertEquals('H', structCode[3]);
275     assertEquals('H', structCode[4]);
276     assertEquals('E', structCode[5]);
277   }
278 }