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