b296586c9429321696cfe3191e9befbeb3d9d56b
[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.assertFalse;
24 import static org.testng.AssertJUnit.assertEquals;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import jalview.api.AlignmentViewPanel;
28 import jalview.api.FeatureRenderer;
29 import jalview.api.SequenceRendererI;
30 import jalview.datamodel.Alignment;
31 import jalview.datamodel.AlignmentI;
32 import jalview.datamodel.HiddenColumns;
33 import jalview.datamodel.PDBEntry;
34 import jalview.datamodel.PDBEntry.Type;
35 import jalview.datamodel.Sequence;
36 import jalview.datamodel.SequenceI;
37 import jalview.gui.JvOptionPane;
38 import jalview.io.DataSourceType;
39 import jalview.io.FileFormats;
40 import jalview.schemes.ColourSchemeI;
41 import jalview.structure.AtomSpec;
42 import jalview.structure.StructureMappingcommandSet;
43 import jalview.structure.StructureSelectionManager;
44 import jalview.structures.models.AAStructureBindingModel.SuperposeData;
45
46 import java.awt.Color;
47 import java.io.IOException;
48 import java.util.Arrays;
49 import java.util.BitSet;
50 import java.util.List;
51
52 import org.testng.annotations.BeforeClass;
53 import org.testng.annotations.BeforeMethod;
54 import org.testng.annotations.Test;
55
56 /**
57  * Unit tests for non-abstract methods of abstract base class
58  * 
59  * @author gmcarstairs
60  *
61  */
62 public class AAStructureBindingModelTest
63 {
64
65   @BeforeClass(alwaysRun = true)
66   public void setUpJvOptionPane()
67   {
68     JvOptionPane.setInteractiveMode(false);
69     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
70   }
71
72   /*
73    * Scenario: Jalview has 4 sequences, corresponding to 1YCS (chains A and B), 3A6S|B, 1OOT|A
74    */
75   private static final String PDB_1 = "HEADER    COMPLEX (ANTI-ONCOGENE/ANKYRIN REPEATS) 30-SEP-96   1YCS              \n"
76           + "ATOM      2  CA  VAL A  97      24.134   4.926  45.821  1.00 47.43           C  \n"
77           + "ATOM      9  CA  PRO A  98      25.135   8.584  46.217  1.00 41.60           C  \n"
78           + "ATOM     16  CA  SER A  99      28.243   9.596  44.271  1.00 39.63           C  \n"
79           + "ATOM     22  CA  GLN A 100      31.488  10.133  46.156  1.00 35.60           C  \n"
80           // artificial jump in residue numbering to prove it is correctly
81           // mapped:
82           + "ATOM     31  CA  LYS A 102      33.323  11.587  43.115  1.00 41.69           C  \n"
83           + "ATOM   1857  CA  GLU B 374       9.193 -16.005  95.870  1.00 54.22           C  \n"
84           + "ATOM   1866  CA  ILE B 375       7.101 -14.921  92.847  1.00 46.82           C  \n"
85           + "ATOM   1874  CA  VAL B 376      10.251 -13.625  91.155  1.00 47.80           C  \n"
86           + "ATOM   1881  CA  LYS B 377      11.767 -17.068  91.763  1.00 50.21           C  \n"
87           + "ATOM   1890  CA  PHE B 378       8.665 -18.948  90.632  1.00 44.85           C  \n";
88
89   private static final String PDB_2 = "HEADER    HYDROLASE                               09-SEP-09   3A6S              \n"
90           + "ATOM      2  CA  MET B   1      15.366 -11.648  24.854  1.00 32.05           C  \n"
91           + "ATOM     10  CA  LYS B   2      16.846  -9.215  22.340  1.00 25.68           C  \n"
92           + "ATOM     19  CA  LYS B   3      15.412  -6.335  20.343  1.00 19.42           C  \n"
93           + "ATOM     28  CA  LEU B   4      15.629  -5.719  16.616  1.00 15.49           C  \n"
94           + "ATOM     36  CA  GLN B   5      14.412  -2.295  15.567  1.00 12.19           C  \n";
95
96   private static final String PDB_3 = "HEADER    STRUCTURAL GENOMICS                     04-MAR-03   1OOT              \n"
97           + "ATOM      2  CA  SER A   7      29.427   3.330  -6.578  1.00 32.50           C  \n"
98           + "ATOM      8  CA  PRO A   8      29.975   3.340  -2.797  1.00 17.62           C  \n"
99           + "ATOM     16  CA ALYS A   9      26.958   3.024  -0.410  0.50  8.78           C  \n"
100           + "ATOM     33  CA  ALA A  10      26.790   4.320   3.172  1.00 11.98           C  \n"
101           + "ATOM     39  CA AVAL A  12      24.424   3.853   6.106  0.50 13.83           C  \n";
102
103   /**
104    * Multichain PDB with identical sequences imported - Binding should correctly
105    * recover chain mappings for each derived sequence
106    */
107   private static final String PDB_4_MC = "HEADER    HYDROLASE                               09-SEP-09   3A6S              \n"
108           + "ATOM      2  CA  MET A   1      15.366 -11.648  24.854  1.00 32.05           C  \n"
109           + "ATOM     10  CA  LYS A   2      16.846  -9.215  22.340  1.00 25.68           C  \n"
110           + "ATOM     19  CA  LYS A   3      15.412  -6.335  20.343  1.00 19.42           C  \n"
111           + "ATOM     28  CA  LEU A   4      15.629  -5.719  16.616  1.00 15.49           C  \n"
112           + "ATOM     36  CA  GLN A   5      14.412  -2.295  15.567  1.00 12.19           C  \n"
113           + "ATOM   1030  CA  MET B   1      18.869  -7.572   3.432  1.00 31.52           C  \n"
114           + "ATOM   1038  CA  LYS B   2      19.182 -10.025   6.313  1.00 26.41           C  \n"
115           + "ATOM   1047  CA  LYS B   3      17.107 -12.963   7.534  1.00 19.71           C  \n"
116           + "ATOM   1056  CA  LEU B   4      16.142 -13.579  11.164  1.00 14.81           C  \n"
117           + "ATOM   1064  CA  GLN B   5      14.648 -17.005  11.785  1.00 13.38           C  \n";
118
119   // TODO: JAL-2227 - import mmCIF PISA assembly & identify master/copy chains
120
121   @Test(groups= {"Functional"})
122   public void testImportPDBPreservesChainMappings() throws IOException
123   {
124     AlignmentI importedAl = new jalview.io.FormatAdapter().readFile(
125             PDB_4_MC, DataSourceType.PASTE, FileFormats.getInstance()
126                     .forName(jalview.io.FileFormat.PDB.toString()));
127     // ideally, we would match on the actual data for the 'File' handle for
128     // pasted files,
129     // see JAL-623 - pasting is still not correctly handled...
130     PDBEntry importedPDB = new PDBEntry("3A6S", "", Type.PDB,
131             "Paste");
132     AAStructureBindingModel binder = new AAStructureBindingModel(
133             new StructureSelectionManager(), new PDBEntry[]
134             { importedPDB },
135             new SequenceI[][]
136             { importedAl.getSequencesArray() }, null)
137     {
138       
139       @Override
140       public void updateColours(Object source)
141       {
142       }
143       
144       @Override
145       public void releaseReferences(Object svl)
146       {
147       }
148       
149       @Override
150       public String[] getStructureFiles()
151       {
152         return null;
153       }
154       
155       @Override
156       public String superposeStructures(AlignmentI[] alignments,
157               int[] structureIndices, HiddenColumns[] hiddenCols)
158       {
159         return null;
160       }
161       
162       @Override
163       public void setSimpleColourScheme(ColourSchemeI cs)
164       {
165       }
166       
167       @Override
168       public void setBackgroundColour(Color col)
169       {
170         
171       }
172       
173       @Override
174       public void highlightAtoms(List<AtomSpec> atoms)
175       {
176       }
177       
178       @Override
179       public FeatureRenderer getFeatureRenderer(AlignmentViewPanel alignment)
180       {
181         return null;
182       }
183       
184       @Override
185       protected StructureMappingcommandSet[] getColourBySequenceCommands(
186               String[] files, SequenceRendererI sr, AlignmentViewPanel avp,
187               boolean showFeatures)
188       {
189         return null;
190       }
191       
192       @Override
193       public List<String> getChainNames()
194       {
195         return null;
196       }
197       
198       @Override
199       protected void colourBySequence(
200               StructureMappingcommandSet[] colourBySequenceCommands)
201       {
202       }
203       
204       @Override
205       public void colourByCharge()
206       {
207       }
208       
209       @Override
210       public void colourByChain()
211       {
212       }
213     };
214     String[][] chains = binder.getChains();
215     assertFalse(chains == null || chains[0] == null,
216             "No chains discovered by binding");
217     assertEquals(2, chains[0].length);
218     assertEquals("A", chains[0][0]);
219     assertEquals("B", chains[0][1]);
220   }
221   AAStructureBindingModel testee;
222
223   AlignmentI al = null;
224
225   /**
226    * Set up test conditions with three aligned sequences,
227    */
228   @BeforeMethod(alwaysRun = true)
229   public void setUp()
230   {
231     SequenceI seq1a = new Sequence("1YCS|A", "-VPSQK");
232     SequenceI seq1b = new Sequence("1YCS|B", "EIVKF-");
233     SequenceI seq2 = new Sequence("3A6S", "MK-KLQ");
234     SequenceI seq3 = new Sequence("1OOT", "SPK-AV");
235     al = new Alignment(new SequenceI[] { seq1a, seq1b, seq2, seq3 });
236     al.setDataset(null);
237
238     /*
239      * give pdb files the name generated by Jalview for PASTE source
240      */
241     PDBEntry[] pdbFiles = new PDBEntry[3];
242     pdbFiles[0] = new PDBEntry("1YCS", "A", Type.PDB, "INLINE1YCS");
243     pdbFiles[1] = new PDBEntry("3A6S", "B", Type.PDB, "INLINE3A6S");
244     pdbFiles[2] = new PDBEntry("1OOT", "A", Type.PDB, "INLINE1OOT");
245     SequenceI[][] seqs = new SequenceI[3][];
246     seqs[0] = new SequenceI[] { seq1a, seq1b };
247     seqs[1] = new SequenceI[] { seq2 };
248     seqs[2] = new SequenceI[] { seq3 };
249     StructureSelectionManager ssm = new StructureSelectionManager();
250
251     ssm.setMapping(new SequenceI[] { seq1a, seq1b }, null, PDB_1,
252             DataSourceType.PASTE, null);
253     ssm.setMapping(new SequenceI[] { seq2 }, null, PDB_2,
254             DataSourceType.PASTE, null);
255     ssm.setMapping(new SequenceI[] { seq3 }, null, PDB_3,
256             DataSourceType.PASTE, null);
257
258     testee = new AAStructureBindingModel(ssm, pdbFiles, seqs, null)
259     {
260       @Override
261       public String[] getStructureFiles()
262       {
263         return new String[] { "INLINE1YCS", "INLINE3A6S", "INLINE1OOT" };
264       }
265
266       @Override
267       public void updateColours(Object source)
268       {
269       }
270
271       @Override
272       public void releaseReferences(Object svl)
273       {
274       }
275
276       @Override
277       public void highlightAtoms(List<AtomSpec> atoms)
278       {
279       }
280
281       @Override
282       public List<String> getChainNames()
283       {
284         return null;
285       }
286
287       @Override
288       public void setSimpleColourScheme(ColourSchemeI cs)
289       {
290       }
291
292       @Override
293       public String superposeStructures(AlignmentI[] als, int[] alm,
294               HiddenColumns[] alc)
295       {
296         return null;
297       }
298
299       @Override
300       public void setBackgroundColour(Color col)
301       {
302       }
303
304       @Override
305       protected StructureMappingcommandSet[] getColourBySequenceCommands(
306               String[] files, SequenceRendererI sr, AlignmentViewPanel avp,
307               boolean showFeatures)
308       {
309         return null;
310       }
311
312       @Override
313       protected void colourBySequence(
314               StructureMappingcommandSet[] colourBySequenceCommands)
315       {
316       }
317
318       @Override
319       public void colourByChain()
320       {
321       }
322
323       @Override
324       public void colourByCharge()
325       {
326       }
327
328       @Override
329       public FeatureRenderer getFeatureRenderer(
330               AlignmentViewPanel alignment)
331       {
332         return null;
333       }
334     };
335   }
336
337   /**
338    * Verify that the method determines that columns 2, 5 and 6 of the alignment
339    * are alignable in structure
340    */
341   @Test(groups = { "Functional" })
342   public void testFindSuperposableResidues()
343   {
344     /*
345      * create a data bean to hold data per structure file
346      */
347     SuperposeData[] structs = new SuperposeData[testee.getStructureFiles().length];
348     for (int i = 0; i < structs.length; i++)
349     {
350       structs[i] = testee.new SuperposeData(al.getWidth());
351     }
352     /*
353      * initialise BitSet of 'superposable columns' to true (would be false for
354      * hidden columns)
355      */
356     BitSet matched = new BitSet();
357     for (int i = 0; i < al.getWidth(); i++)
358     {
359       matched.set(i);
360     }
361
362     int refStructure = testee
363             .findSuperposableResidues(al, matched, structs);
364
365     assertEquals(0, refStructure);
366
367     /*
368      * only ungapped, structure-mapped columns are superposable
369      */
370     assertFalse(matched.get(0)); // gap in first sequence
371     assertTrue(matched.get(1));
372     assertFalse(matched.get(2)); // gap in third sequence
373     assertFalse(matched.get(3)); // gap in fourth sequence
374     assertTrue(matched.get(4));
375     assertTrue(matched.get(5)); // gap in second sequence
376
377     assertEquals("1YCS", structs[0].pdbId);
378     assertEquals("3A6S", structs[1].pdbId);
379     assertEquals("1OOT", structs[2].pdbId);
380     assertEquals("A", structs[0].chain); // ? struct has chains A _and_ B
381     assertEquals("B", structs[1].chain);
382     assertEquals("A", structs[2].chain);
383     // the 0's for unsuperposable positions propagate down the columns:
384     assertEquals("[0, 97, 98, 99, 100, 102]",
385             Arrays.toString(structs[0].pdbResNo));
386     assertEquals("[0, 2, 0, 3, 4, 5]", Arrays.toString(structs[1].pdbResNo));
387     assertEquals("[0, 8, 0, 0, 10, 12]",
388             Arrays.toString(structs[2].pdbResNo));
389   }
390
391   @Test(groups = { "Functional" })
392   public void testFindSuperposableResidues_hiddenColumn()
393   {
394     SuperposeData[] structs = new SuperposeData[al.getHeight()];
395     for (int i = 0; i < structs.length; i++)
396     {
397       structs[i] = testee.new SuperposeData(al.getWidth());
398     }
399     /*
400      * initialise BitSet of 'superposable columns' to true (would be false for
401      * hidden columns)
402      */
403     BitSet matched = new BitSet();
404     for (int i = 0; i < al.getWidth(); i++)
405     {
406       matched.set(i);
407     }
408
409     // treat column 5 of the alignment as hidden
410     matched.clear(4);
411
412     int refStructure = testee
413             .findSuperposableResidues(al, matched, structs);
414
415     assertEquals(0, refStructure);
416
417     // only ungapped, structure-mapped columns are not superposable
418     assertFalse(matched.get(0));
419     assertTrue(matched.get(1));
420     assertFalse(matched.get(2));
421     assertFalse(matched.get(3));
422     assertFalse(matched.get(4)); // superposable, but hidden, column
423     assertTrue(matched.get(5));
424   }
425 }