7ba22b49ddb1eba4dcf4b553602d319558fa618a
[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.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import jalview.api.AlignmentViewPanel;
28 import jalview.api.FeatureRenderer;
29 import jalview.api.SequenceRenderer;
30 import jalview.datamodel.Alignment;
31 import jalview.datamodel.AlignmentI;
32 import jalview.datamodel.ColumnSelection;
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.schemes.ColourSchemeI;
40 import jalview.structure.AtomSpec;
41 import jalview.structure.StructureMappingcommandSet;
42 import jalview.structure.StructureSelectionManager;
43 import jalview.structures.models.AAStructureBindingModel.SuperposeData;
44
45 import java.awt.Color;
46 import java.util.Arrays;
47 import java.util.BitSet;
48 import java.util.List;
49
50 import org.testng.annotations.BeforeClass;
51 import org.testng.annotations.BeforeMethod;
52 import org.testng.annotations.Test;
53
54 /**
55  * Unit tests for non-abstract methods of abstract base class
56  * 
57  * @author gmcarstairs
58  *
59  */
60 public class AAStructureBindingModelTest
61 {
62
63   @BeforeClass(alwaysRun = true)
64   public void setUpJvOptionPane()
65   {
66     JvOptionPane.setInteractiveMode(false);
67     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
68   }
69
70   /*
71    * Scenario: Jalview has 4 sequences, corresponding to 1YCS (chains A and B), 3A6S|B, 1OOT|A
72    */
73   private static final String PDB_1 = "HEADER    COMPLEX (ANTI-ONCOGENE/ANKYRIN REPEATS) 30-SEP-96   1YCS              \n"
74           + "ATOM      2  CA  VAL A  97      24.134   4.926  45.821  1.00 47.43           C  \n"
75           + "ATOM      9  CA  PRO A  98      25.135   8.584  46.217  1.00 41.60           C  \n"
76           + "ATOM     16  CA  SER A  99      28.243   9.596  44.271  1.00 39.63           C  \n"
77           + "ATOM     22  CA  GLN A 100      31.488  10.133  46.156  1.00 35.60           C  \n"
78           // artificial jump in residue numbering to prove it is correctly
79           // mapped:
80           + "ATOM     31  CA  LYS A 102      33.323  11.587  43.115  1.00 41.69           C  \n"
81           + "ATOM   1857  CA  GLU B 374       9.193 -16.005  95.870  1.00 54.22           C  \n"
82           + "ATOM   1866  CA  ILE B 375       7.101 -14.921  92.847  1.00 46.82           C  \n"
83           + "ATOM   1874  CA  VAL B 376      10.251 -13.625  91.155  1.00 47.80           C  \n"
84           + "ATOM   1881  CA  LYS B 377      11.767 -17.068  91.763  1.00 50.21           C  \n"
85           + "ATOM   1890  CA  PHE B 378       8.665 -18.948  90.632  1.00 44.85           C  \n";
86
87   private static final String PDB_2 = "HEADER    HYDROLASE                               09-SEP-09   3A6S              \n"
88           + "ATOM      2  CA  MET B   1      15.366 -11.648  24.854  1.00 32.05           C  \n"
89           + "ATOM     10  CA  LYS B   2      16.846  -9.215  22.340  1.00 25.68           C  \n"
90           + "ATOM     19  CA  LYS B   3      15.412  -6.335  20.343  1.00 19.42           C  \n"
91           + "ATOM     28  CA  LEU B   4      15.629  -5.719  16.616  1.00 15.49           C  \n"
92           + "ATOM     36  CA  GLN B   5      14.412  -2.295  15.567  1.00 12.19           C  \n";
93
94   private static final String PDB_3 = "HEADER    STRUCTURAL GENOMICS                     04-MAR-03   1OOT              \n"
95           + "ATOM      2  CA  SER A   7      29.427   3.330  -6.578  1.00 32.50           C  \n"
96           + "ATOM      8  CA  PRO A   8      29.975   3.340  -2.797  1.00 17.62           C  \n"
97           + "ATOM     16  CA ALYS A   9      26.958   3.024  -0.410  0.50  8.78           C  \n"
98           + "ATOM     33  CA  ALA A  10      26.790   4.320   3.172  1.00 11.98           C  \n"
99           + "ATOM     39  CA AVAL A  12      24.424   3.853   6.106  0.50 13.83           C  \n";
100
101   AAStructureBindingModel testee;
102
103   AlignmentI al = null;
104
105   /**
106    * Set up test conditions with three aligned sequences,
107    */
108   @BeforeMethod(alwaysRun = true)
109   public void setUp()
110   {
111     SequenceI seq1a = new Sequence("1YCS|A", "-VPSQK");
112     SequenceI seq1b = new Sequence("1YCS|B", "EIVKF-");
113     SequenceI seq2 = new Sequence("3A6S", "MK-KLQ");
114     SequenceI seq3 = new Sequence("1OOT", "SPK-AV");
115     al = new Alignment(new SequenceI[] { seq1a, seq1b, seq2, seq3 });
116     al.setDataset(null);
117
118     /*
119      * give pdb files the name generated by Jalview for PASTE source
120      */
121     PDBEntry[] pdbFiles = new PDBEntry[3];
122     pdbFiles[0] = new PDBEntry("1YCS", "A", Type.PDB, "INLINE1YCS");
123     pdbFiles[1] = new PDBEntry("3A6S", "B", Type.PDB, "INLINE3A6S");
124     pdbFiles[2] = new PDBEntry("1OOT", "A", Type.PDB, "INLINE1OOT");
125     SequenceI[][] seqs = new SequenceI[3][];
126     seqs[0] = new SequenceI[] { seq1a, seq1b };
127     seqs[1] = new SequenceI[] { seq2 };
128     seqs[2] = new SequenceI[] { seq3 };
129     StructureSelectionManager ssm = new StructureSelectionManager();
130
131     ssm.setMapping(new SequenceI[] { seq1a, seq1b }, null, PDB_1,
132             DataSourceType.PASTE);
133     ssm.setMapping(new SequenceI[] { seq2 }, null, PDB_2,
134             DataSourceType.PASTE);
135     ssm.setMapping(new SequenceI[] { seq3 }, null, PDB_3,
136             DataSourceType.PASTE);
137
138     testee = new AAStructureBindingModel(ssm, pdbFiles, seqs, null)
139     {
140       @Override
141       public String[] getPdbFile()
142       {
143         return new String[] { "INLINE1YCS", "INLINE3A6S", "INLINE1OOT" };
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 void highlightAtoms(List<AtomSpec> atoms)
158       {
159       }
160
161       @Override
162       public List<String> getChainNames()
163       {
164         return null;
165       }
166
167       @Override
168       public void setJalviewColourScheme(ColourSchemeI cs)
169       {
170       }
171
172       @Override
173       public String superposeStructures(AlignmentI[] als, int[] alm,
174               ColumnSelection[] alc)
175       {
176         return null;
177       }
178
179       @Override
180       public void setBackgroundColour(Color col)
181       {
182       }
183
184       @Override
185       protected StructureMappingcommandSet[] getColourBySequenceCommands(
186               String[] files, SequenceRenderer sr, AlignmentViewPanel avp)
187       {
188         return null;
189       }
190
191       @Override
192       public SequenceRenderer getSequenceRenderer(
193               AlignmentViewPanel alignment)
194       {
195         return null;
196       }
197
198       @Override
199       protected void colourBySequence(
200               StructureMappingcommandSet[] colourBySequenceCommands)
201       {
202       }
203
204       @Override
205       public void colourByChain()
206       {
207       }
208
209       @Override
210       public void colourByCharge()
211       {
212       }
213
214       @Override
215       public FeatureRenderer getFeatureRenderer(
216               AlignmentViewPanel alignment)
217       {
218         return null;
219       }
220     };
221   }
222
223   /**
224    * Verify that the method determines that columns 2, 5 and 6 of the alignment
225    * are alignable in structure
226    */
227   @Test(groups = { "Functional" })
228   public void testFindSuperposableResidues()
229   {
230     /*
231      * create a data bean to hold data per structure file
232      */
233     SuperposeData[] structs = new SuperposeData[testee.getPdbFile().length];
234     for (int i = 0; i < structs.length; i++)
235     {
236       structs[i] = testee.new SuperposeData(al.getWidth());
237     }
238     /*
239      * initialise BitSet of 'superposable columns' to true (would be false for
240      * hidden columns)
241      */
242     BitSet matched = new BitSet();
243     for (int i = 0; i < al.getWidth(); i++)
244     {
245       matched.set(i);
246     }
247
248     int refStructure = testee
249             .findSuperposableResidues(al, matched, structs);
250
251     assertEquals(0, refStructure);
252
253     /*
254      * only ungapped, structure-mapped columns are superposable
255      */
256     assertFalse(matched.get(0)); // gap in first sequence
257     assertTrue(matched.get(1));
258     assertFalse(matched.get(2)); // gap in third sequence
259     assertFalse(matched.get(3)); // gap in fourth sequence
260     assertTrue(matched.get(4));
261     assertTrue(matched.get(5)); // gap in second sequence
262
263     assertEquals("1YCS", structs[0].pdbId);
264     assertEquals("3A6S", structs[1].pdbId);
265     assertEquals("1OOT", structs[2].pdbId);
266     assertEquals("A", structs[0].chain); // ? struct has chains A _and_ B
267     assertEquals("B", structs[1].chain);
268     assertEquals("A", structs[2].chain);
269     // the 0's for unsuperposable positions propagate down the columns:
270     assertEquals("[0, 97, 98, 99, 100, 102]",
271             Arrays.toString(structs[0].pdbResNo));
272     assertEquals("[0, 2, 0, 3, 4, 5]", Arrays.toString(structs[1].pdbResNo));
273     assertEquals("[0, 8, 0, 0, 10, 12]",
274             Arrays.toString(structs[2].pdbResNo));
275   }
276
277   @Test(groups = { "Functional" })
278   public void testFindSuperposableResidues_hiddenColumn()
279   {
280     SuperposeData[] structs = new SuperposeData[al.getHeight()];
281     for (int i = 0; i < structs.length; i++)
282     {
283       structs[i] = testee.new SuperposeData(al.getWidth());
284     }
285     /*
286      * initialise BitSet of 'superposable columns' to true (would be false for
287      * hidden columns)
288      */
289     BitSet matched = new BitSet();
290     for (int i = 0; i < al.getWidth(); i++)
291     {
292       matched.set(i);
293     }
294
295     // treat column 5 of the alignment as hidden
296     matched.clear(4);
297
298     int refStructure = testee
299             .findSuperposableResidues(al, matched, structs);
300
301     assertEquals(0, refStructure);
302
303     // only ungapped, structure-mapped columns are not superposable
304     assertFalse(matched.get(0));
305     assertTrue(matched.get(1));
306     assertFalse(matched.get(2));
307     assertFalse(matched.get(3));
308     assertFalse(matched.get(4)); // superposable, but hidden, column
309     assertTrue(matched.get(5));
310   }
311 }