JAL-2422 JAL-3551 unit tests updated for code changes
[jalview.git] / test / jalview / structures / models / AAStructureBindingModelTest.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.structures.models;
22
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.assertFalse;
25 import static org.testng.Assert.assertNotNull;
26 import static org.testng.Assert.assertTrue;
27
28 import java.awt.Color;
29 import java.io.IOException;
30 import java.util.Arrays;
31 import java.util.BitSet;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35
36 import org.testng.annotations.BeforeClass;
37 import org.testng.annotations.BeforeMethod;
38 import org.testng.annotations.Test;
39
40 import jalview.api.AlignmentViewPanel;
41 import jalview.api.SequenceRenderer;
42 import jalview.datamodel.Alignment;
43 import jalview.datamodel.AlignmentI;
44 import jalview.datamodel.ColumnSelection;
45 import jalview.datamodel.PDBEntry;
46 import jalview.datamodel.PDBEntry.Type;
47 import jalview.datamodel.Sequence;
48 import jalview.datamodel.SequenceI;
49 import jalview.ext.rbvi.chimera.ChimeraCommands;
50 import jalview.gui.AlignFrame;
51 import jalview.gui.JvOptionPane;
52 import jalview.gui.StructureViewer.ViewerType;
53 import jalview.io.DataSourceType;
54 import jalview.io.FileFormats;
55 import jalview.schemes.JalviewColourScheme;
56 import jalview.structure.AtomSpec;
57 import jalview.structure.AtomSpecModel;
58 import jalview.structure.StructureCommandI;
59 import jalview.structure.StructureMapping;
60 import jalview.structure.StructureSelectionManager;
61 import junit.extensions.PA;
62
63 /**
64  * Unit tests for non-abstract methods of abstract base class
65  * 
66  * @author gmcarstairs
67  *
68  */
69 public class AAStructureBindingModelTest
70 {
71
72   @BeforeClass(alwaysRun = true)
73   public void setUpJvOptionPane()
74   {
75     JvOptionPane.setInteractiveMode(false);
76     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
77   }
78
79   /*
80    * Scenario: Jalview has 4 sequences, corresponding to 1YCS (chains A and B), 3A6S|B, 1OOT|A
81    */
82   private static final String PDB_1 = "HEADER    COMPLEX (ANTI-ONCOGENE/ANKYRIN REPEATS) 30-SEP-96   1YCS              \n"
83           + "ATOM      2  CA  VAL A  97      24.134   4.926  45.821  1.00 47.43           C  \n"
84           + "ATOM      9  CA  PRO A  98      25.135   8.584  46.217  1.00 41.60           C  \n"
85           + "ATOM     16  CA  SER A  99      28.243   9.596  44.271  1.00 39.63           C  \n"
86           + "ATOM     22  CA  GLN A 100      31.488  10.133  46.156  1.00 35.60           C  \n"
87           // artificial jump in residue numbering to prove it is correctly
88           // mapped:
89           + "ATOM     31  CA  LYS A 102      33.323  11.587  43.115  1.00 41.69           C  \n"
90           + "ATOM   1857  CA  GLU B 374       9.193 -16.005  95.870  1.00 54.22           C  \n"
91           + "ATOM   1866  CA  ILE B 375       7.101 -14.921  92.847  1.00 46.82           C  \n"
92           + "ATOM   1874  CA  VAL B 376      10.251 -13.625  91.155  1.00 47.80           C  \n"
93           + "ATOM   1881  CA  LYS B 377      11.767 -17.068  91.763  1.00 50.21           C  \n"
94           + "ATOM   1890  CA  PHE B 378       8.665 -18.948  90.632  1.00 44.85           C  \n";
95
96   private static final String PDB_2 = "HEADER    HYDROLASE                               09-SEP-09   3A6S              \n"
97           + "ATOM      2  CA  MET B   1      15.366 -11.648  24.854  1.00 32.05           C  \n"
98           + "ATOM     10  CA  LYS B   2      16.846  -9.215  22.340  1.00 25.68           C  \n"
99           + "ATOM     19  CA  LYS B   3      15.412  -6.335  20.343  1.00 19.42           C  \n"
100           + "ATOM     28  CA  LEU B   4      15.629  -5.719  16.616  1.00 15.49           C  \n"
101           + "ATOM     36  CA  GLN B   5      14.412  -2.295  15.567  1.00 12.19           C  \n";
102
103   private static final String PDB_3 = "HEADER    STRUCTURAL GENOMICS                     04-MAR-03   1OOT              \n"
104           + "ATOM      2  CA  SER A   7      29.427   3.330  -6.578  1.00 32.50           C  \n"
105           + "ATOM      8  CA  PRO A   8      29.975   3.340  -2.797  1.00 17.62           C  \n"
106           + "ATOM     16  CA ALYS A   9      26.958   3.024  -0.410  0.50  8.78           C  \n"
107           + "ATOM     33  CA  ALA A  10      26.790   4.320   3.172  1.00 11.98           C  \n"
108           + "ATOM     39  CA AVAL A  12      24.424   3.853   6.106  0.50 13.83           C  \n";
109
110   /**
111    * Multichain PDB with identical sequences imported - Binding should correctly
112    * recover chain mappings for each derived sequence
113    */
114   private static final String PDB_4_MC = "HEADER    HYDROLASE                               09-SEP-09   3A6S              \n"
115           + "ATOM      2  CA  MET A   1      15.366 -11.648  24.854  1.00 32.05           C  \n"
116           + "ATOM     10  CA  LYS A   2      16.846  -9.215  22.340  1.00 25.68           C  \n"
117           + "ATOM     19  CA  LYS A   3      15.412  -6.335  20.343  1.00 19.42           C  \n"
118           + "ATOM     28  CA  LEU A   4      15.629  -5.719  16.616  1.00 15.49           C  \n"
119           + "ATOM     36  CA  GLN A   5      14.412  -2.295  15.567  1.00 12.19           C  \n"
120           + "ATOM   1030  CA  MET B   1      18.869  -7.572   3.432  1.00 31.52           C  \n"
121           + "ATOM   1038  CA  LYS B   2      19.182 -10.025   6.313  1.00 26.41           C  \n"
122           + "ATOM   1047  CA  LYS B   3      17.107 -12.963   7.534  1.00 19.71           C  \n"
123           + "ATOM   1056  CA  LEU B   4      16.142 -13.579  11.164  1.00 14.81           C  \n"
124           + "ATOM   1064  CA  GLN B   5      14.648 -17.005  11.785  1.00 13.38           C  \n";
125
126   // TODO: JAL-2227 - import mmCIF PISA assembly & identify master/copy chains
127
128   @Test(groups= {"Functional"})
129   public void testImportPDBPreservesChainMappings() throws IOException
130   {
131     AlignmentI importedAl = new jalview.io.FormatAdapter().readFile(
132             PDB_4_MC, DataSourceType.PASTE, FileFormats.getInstance()
133                     .forName(jalview.io.FileFormat.PDB.toString()));
134     // ideally, we would match on the actual data for the 'File' handle for
135     // pasted files,
136     // see JAL-623 - pasting is still not correctly handled...
137     PDBEntry importedPDB = new PDBEntry("3A6S", "", Type.PDB,
138             "Paste");
139     AAStructureBindingModel binder = new AAStructureBindingModel(
140             new StructureSelectionManager(), new PDBEntry[]
141             { importedPDB },
142             new SequenceI[][]
143             { importedAl.getSequencesArray() }, null)
144     {
145       
146       @Override
147       public void updateColours(Object source)
148       {
149       }
150       
151       @Override
152       public void releaseReferences(Object svl)
153       {
154       }
155       
156       @Override
157       public String[] getStructureFiles()
158       {
159         return null;
160       }
161       
162       @Override
163       public void highlightAtoms(List<AtomSpec> atoms)
164       {
165       }
166       
167       @Override
168       public SequenceRenderer getSequenceRenderer(AlignmentViewPanel alignment)
169       {
170         return null;
171       }
172
173       @Override
174       protected List<String> executeCommand(StructureCommandI command,
175               boolean getReply)
176       {
177         return null;
178       }
179
180       @Override
181       protected String getModelIdForFile(String chainId)
182       {
183         return "";
184       }
185
186       @Override
187       protected ViewerType getViewerType()
188       {
189         return null;
190       }
191     };
192     String[][] chains = binder.getChains();
193     assertFalse(chains == null || chains[0] == null,
194             "No chains discovered by binding");
195     assertEquals(chains[0].length, 2);
196     assertEquals(chains[0][0], "A");
197     assertEquals(chains[0][1], "B");
198   }
199   AAStructureBindingModel testee;
200
201   AlignmentI al = null;
202
203   /**
204    * Set up test conditions with three aligned sequences,
205    */
206   @BeforeMethod(alwaysRun = true)
207   public void setUp()
208   {
209     SequenceI seq1a = new Sequence("1YCS|A", "-VPSQK");
210     SequenceI seq1b = new Sequence("1YCS|B", "EIVKF-");
211     SequenceI seq2 = new Sequence("3A6S", "MK-KLQ");
212     SequenceI seq3 = new Sequence("1OOT", "SPK-AV");
213     al = new Alignment(new SequenceI[] { seq1a, seq1b, seq2, seq3 });
214     al.setDataset(null);
215
216     /*
217      * give pdb files the name generated by Jalview for PASTE source
218      */
219     PDBEntry[] pdbFiles = new PDBEntry[3];
220     pdbFiles[0] = new PDBEntry("1YCS", "A", Type.PDB, "INLINE1YCS");
221     pdbFiles[1] = new PDBEntry("3A6S", "B", Type.PDB, "INLINE3A6S");
222     pdbFiles[2] = new PDBEntry("1OOT", "A", Type.PDB, "INLINE1OOT");
223     SequenceI[][] seqs = new SequenceI[3][];
224     seqs[0] = new SequenceI[] { seq1a, seq1b };
225     seqs[1] = new SequenceI[] { seq2 };
226     seqs[2] = new SequenceI[] { seq3 };
227     StructureSelectionManager ssm = new StructureSelectionManager();
228
229     ssm.setMapping(new SequenceI[] { seq1a, seq1b }, null, PDB_1,
230             DataSourceType.PASTE, null);
231     ssm.setMapping(new SequenceI[] { seq2 }, null, PDB_2,
232             DataSourceType.PASTE, null);
233     ssm.setMapping(new SequenceI[] { seq3 }, null, PDB_3,
234             DataSourceType.PASTE, null);
235
236     testee = newBindingModel(pdbFiles, seqs, ssm);
237   }
238
239   /**
240    * A helper method to construct the test target object
241    * 
242    * @param pdbFiles
243    * @param seqs
244    * @param ssm
245    */
246   protected AAStructureBindingModel newBindingModel(PDBEntry[] pdbFiles,
247           SequenceI[][] seqs,
248           StructureSelectionManager ssm)
249   {
250     AAStructureBindingModel model = new AAStructureBindingModel(ssm,
251             pdbFiles, seqs, null)
252     {
253       @Override
254       public String[] getStructureFiles()
255       {
256         return new String[] { "INLINE1YCS", "INLINE3A6S", "INLINE1OOT" };
257       }
258
259       @Override
260       public void updateColours(Object source)
261       {
262       }
263
264       @Override
265       public void releaseReferences(Object svl)
266       {
267       }
268
269       @Override
270       public void highlightAtoms(List<AtomSpec> atoms)
271       {
272       }
273
274       @Override
275       public SequenceRenderer getSequenceRenderer(
276               AlignmentViewPanel alignment)
277       {
278         return null;
279       }
280
281       @Override
282       protected List<String> executeCommand(StructureCommandI command,
283               boolean getReply)
284       {
285         return null;
286       }
287
288       /*
289        * for this test, let structure model ids be 0, 1, ...
290        * corresponding to first, second etc pdbfile
291        */
292       @Override
293       protected String getModelIdForFile(String pdbfile)
294       {
295         for (int i = 0; i < this.getPdbCount(); i++)
296         {
297           if (pdbfile.equals(this.getPdbEntry(i).getFile()))
298           {
299             return String.valueOf(i);
300           }
301         }
302         return "";
303       }
304
305       @Override
306       protected ViewerType getViewerType()
307       {
308         return null;
309       }
310     };
311     PA.setValue(model, "commandGenerator", new ChimeraCommands());
312     return model;
313   }
314
315   /**
316    * Verify that the method determines that columns 2, 5 and 6 of the alignment
317    * are alignable in structure
318    */
319   @Test(groups = { "Functional" })
320   public void testFindSuperposableResidues()
321   {
322     /*
323      * create a data bean to hold data per structure file
324      */
325     AAStructureBindingModel.SuperposeData[] structs = new AAStructureBindingModel.SuperposeData[testee.getStructureFiles().length];
326     for (int i = 0; i < structs.length; i++)
327     {
328       structs[i] = new AAStructureBindingModel.SuperposeData(al.getWidth(), "0");
329     }
330     /*
331      * initialise BitSet of 'superposable columns' to true (would be false for
332      * hidden columns)
333      */
334     BitSet matched = new BitSet();
335     for (int i = 0; i < al.getWidth(); i++)
336     {
337       matched.set(i);
338     }
339
340     int refStructure = testee
341             .findSuperposableResidues(al, matched, structs);
342
343     assertEquals(refStructure, 0);
344
345     /*
346      * only ungapped, structure-mapped columns are superposable
347      */
348     assertFalse(matched.get(0)); // gap in first sequence
349     assertTrue(matched.get(1));
350     assertFalse(matched.get(2)); // gap in third sequence
351     assertFalse(matched.get(3)); // gap in fourth sequence
352     assertTrue(matched.get(4));
353     assertTrue(matched.get(5)); // gap in second sequence
354
355     assertEquals(structs[0].pdbId, "1YCS");
356     assertEquals(structs[1].pdbId, "3A6S");
357     assertEquals(structs[2].pdbId, "1OOT");
358     assertEquals(structs[0].chain, "A"); // ? struct has chains A _and_ B
359     assertEquals(structs[1].chain, "B");
360     assertEquals(structs[2].chain, "A");
361     // the 0's for unsuperposable positions propagate down the columns:
362     assertEquals(Arrays.toString(structs[0].pdbResNo),
363             "[0, 97, 98, 99, 100, 102]");
364     assertEquals(Arrays.toString(structs[1].pdbResNo),
365             "[0, 2, 0, 3, 4, 5]");
366     assertEquals(Arrays.toString(structs[2].pdbResNo),
367             "[0, 8, 0, 0, 10, 12]");
368   }
369
370   @Test(groups = { "Functional" })
371   public void testFindSuperposableResidues_hiddenColumn()
372   {
373     AAStructureBindingModel.SuperposeData[] structs = new AAStructureBindingModel.SuperposeData[al.getHeight()];
374     for (int i = 0; i < structs.length; i++)
375     {
376       structs[i] = new AAStructureBindingModel.SuperposeData(al.getWidth(), "0");
377     }
378     /*
379      * initialise BitSet of 'superposable columns' to true (would be false for
380      * hidden columns)
381      */
382     BitSet matched = new BitSet();
383     for (int i = 0; i < al.getWidth(); i++)
384     {
385       matched.set(i);
386     }
387
388     // treat column 5 of the alignment as hidden
389     matched.clear(4);
390
391     int refStructure = testee
392             .findSuperposableResidues(al, matched, structs);
393
394     assertEquals(refStructure, 0);
395
396     // only ungapped, structure-mapped columns are not superposable
397     assertFalse(matched.get(0));
398     assertTrue(matched.get(1));
399     assertFalse(matched.get(2));
400     assertFalse(matched.get(3));
401     assertFalse(matched.get(4)); // superposable, but hidden, column
402     assertTrue(matched.get(5));
403   }
404
405   @Test(groups = { "Functional" })
406   public void testBuildColoursMap()
407   {
408     /*
409      * load these sequences, coloured by Strand propensity,
410      * with columns 2-4 hidden
411      */
412     SequenceI seq1 = new Sequence("seq1", "MHRSQSSSGG");
413     SequenceI seq2 = new Sequence("seq2", "MVRSNGGSSS");
414     AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
415     AlignFrame af = new AlignFrame(al, 800, 500);
416     af.changeColour_actionPerformed(JalviewColourScheme.Strand.toString());
417     ColumnSelection cs = new ColumnSelection();
418     cs.addElement(2);
419     cs.addElement(3);
420     cs.addElement(4);
421     af.getViewport().setColumnSelection(cs);
422     af.hideSelColumns_actionPerformed(null);
423     SequenceRenderer sr = new jalview.gui.SequenceRenderer(
424             af.getViewport());
425     SequenceI[][] seqs = new SequenceI[][] { { seq1 }, { seq2 } };
426     String[] files = new String[] { "seq1.pdb", "seq2.pdb" };
427     PDBEntry[] pdbFiles = new PDBEntry[2];
428     pdbFiles[0] = new PDBEntry("PDB1", "A", Type.PDB, "seq1.pdb");
429     pdbFiles[1] = new PDBEntry("PDB2", "B", Type.PDB, "seq2.pdb");
430     StructureSelectionManager ssm = new StructureSelectionManager();
431   
432     /*
433      * map residues 1-10 to residues 21-30 (atoms 105-150) in structures
434      */
435     HashMap<Integer, int[]> map = new HashMap<>();
436     for (int pos = 1; pos <= seq1.getLength(); pos++)
437     {
438       map.put(pos, new int[] { 20 + pos, 5 * (20 + pos) });
439     }
440     StructureMapping sm1 = new StructureMapping(seq1, "seq1.pdb", "pdb1",
441             "A", map, null);
442     ssm.addStructureMapping(sm1);
443     StructureMapping sm2 = new StructureMapping(seq2, "seq2.pdb", "pdb2",
444             "B", map, null);
445     ssm.addStructureMapping(sm2);
446
447     AAStructureBindingModel binding = newBindingModel(pdbFiles, seqs, ssm);
448
449     /*
450      * method under test builds a map of structures residues by colour
451      * verify the map holds what it should
452      */
453     Map<Object, AtomSpecModel> colours = binding.buildColoursMap(ssm, files,
454             seqs, sr, af.alignPanel);
455     ChimeraCommands helper = new ChimeraCommands();
456     
457     /*
458      * M colour is #82827d (see strand.html help page)
459      * sequence residue 1 mapped to structure residue 21
460      */
461     Color mColor = new Color(0x82827d);
462     AtomSpecModel atomSpec = colours.get(mColor);
463     assertNotNull(atomSpec);
464     assertEquals(helper.getAtomSpec(atomSpec, false), "#0:21.A|#1:21.B");
465
466     /*
467      * H colour is #60609f, seq1.2 mapped to structure 0 residue 22
468      */
469     Color hColor = new Color(0x60609f);
470     atomSpec = colours.get(hColor);
471     assertNotNull(atomSpec);
472     assertEquals(helper.getAtomSpec(atomSpec, false), "#0:22.A");
473
474     /*
475      * V colour is #ffff00, seq2.2 mapped to structure 1 residue 22
476      */
477     Color vColor = new Color(0xffff00);
478     atomSpec = colours.get(vColor);
479     assertNotNull(atomSpec);
480     assertEquals(helper.getAtomSpec(atomSpec, false), "#1:22.B");
481
482     /*
483      * hidden columns are Gray (128, 128, 128)
484      * sequence positions 3-5 mapped to structure residues 23-25
485      */
486     Color gray = new Color(128, 128, 128);
487     atomSpec = colours.get(gray);
488     assertNotNull(atomSpec);
489     assertEquals(helper.getAtomSpec(atomSpec, false), "#0:23-25.A|#1:23-25.B");
490
491     /*
492      * S and G are both coloured #4949b6, structure residues 26-30
493      */
494     Color sgColour = new Color(0x4949b6);
495     atomSpec = colours.get(sgColour);
496     assertNotNull(atomSpec);
497     assertEquals(helper.getAtomSpec(atomSpec, false),
498             "#0:26-30.A|#1:26-30.B");
499   }
500 }