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