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