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