c3ece9d267c1e35cd16d5ec6603198021d227609
[jalview.git] / test / jalview / ext / jmol / JmolCommandsTest.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.Assert.assertEquals;
24 import static org.testng.Assert.assertTrue;
25
26 import jalview.datamodel.Alignment;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.ColumnSelection;
29 import jalview.datamodel.Sequence;
30 import jalview.datamodel.SequenceI;
31 import jalview.gui.AlignFrame;
32 import jalview.gui.JvOptionPane;
33 import jalview.gui.SequenceRenderer;
34 import jalview.schemes.JalviewColourScheme;
35 import jalview.structure.AtomSpecModel;
36 import jalview.structure.StructureCommandsI;
37 import jalview.structure.StructureMapping;
38 import jalview.structure.StructureSelectionManager;
39
40 import java.util.HashMap;
41
42 import org.testng.annotations.BeforeClass;
43 import org.testng.annotations.Test;
44
45 public class JmolCommandsTest
46 {
47
48   @BeforeClass(alwaysRun = true)
49   public void setUpJvOptionPane()
50   {
51     JvOptionPane.setInteractiveMode(false);
52     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
53   }
54
55   @Test(groups = { "Functional" })
56   public void testGetColourBySequenceCommand_noFeatures()
57   {
58     SequenceI seq1 = new Sequence("seq1", "MHRSQTRALK");
59     SequenceI seq2 = new Sequence("seq2", "MRLEITQSGD");
60     AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
61     AlignFrame af = new AlignFrame(al, 800, 500);
62     SequenceRenderer sr = new SequenceRenderer(af.getViewport());
63     SequenceI[][] seqs = new SequenceI[][] { { seq1 }, { seq2 } };
64     String[] files = new String[] { "seq1.pdb", "seq2.pdb" };
65     StructureSelectionManager ssm = new StructureSelectionManager();
66
67     // need some mappings!
68
69     String[] commands = new JmolCommands().colourBySequence(ssm, files,
70             seqs, sr, af.alignPanel);
71     assertEquals(commands.length, 0);
72   }
73
74   @Test(groups = { "Functional" })
75   public void testGetColourBySequenceCommands_hiddenColumns()
76   {
77     /*
78      * load these sequences, coloured by Strand propensity,
79      * with columns 2-4 hidden
80      */
81     SequenceI seq1 = new Sequence("seq1", "MHRSQSSSGG");
82     SequenceI seq2 = new Sequence("seq2", "MVRSNGGSSS");
83     AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
84     AlignFrame af = new AlignFrame(al, 800, 500);
85     af.changeColour_actionPerformed(JalviewColourScheme.Strand.toString());
86     ColumnSelection cs = new ColumnSelection();
87     cs.addElement(2);
88     cs.addElement(3);
89     cs.addElement(4);
90     af.getViewport().setColumnSelection(cs);
91     af.hideSelColumns_actionPerformed(null);
92     SequenceRenderer sr = new SequenceRenderer(af.getViewport());
93     SequenceI[][] seqs = new SequenceI[][] { { seq1 }, { seq2 } };
94     String[] files = new String[] { "seq1.pdb", "seq2.pdb" };
95     StructureSelectionManager ssm = new StructureSelectionManager();
96
97     /*
98      * map residues 1-10 to residues 21-30 (atoms 105-150) in structures
99      */
100     HashMap<Integer, int[]> map = new HashMap<>();
101     for (int pos = 1; pos <= seq1.getLength(); pos++)
102     {
103       map.put(pos, new int[] { 20 + pos, 5 * (20 + pos) });
104     }
105     StructureMapping sm1 = new StructureMapping(seq1, "seq1.pdb", "pdb1",
106             "A", map, null);
107     ssm.addStructureMapping(sm1);
108     StructureMapping sm2 = new StructureMapping(seq2, "seq2.pdb", "pdb2",
109             "B", map, null);
110     ssm.addStructureMapping(sm2);
111
112     String[] commands = new JmolCommands().colourBySequence(ssm, files,
113             seqs, sr, af.alignPanel);
114     assertEquals(commands.length, 2);
115
116     String chainACommand = commands[0];
117     // M colour is #82827d == (130, 130, 125) (see strand.html help page)
118     assertTrue(
119             chainACommand.contains("select 21:A/1.1;color[130,130,125]")); // first
120                                                                            // one
121     // H colour is #60609f == (96, 96, 159)
122     assertTrue(chainACommand.contains(";select 22:A/1.1;color[96,96,159]"));
123     // hidden columns are Gray (128, 128, 128)
124     assertTrue(chainACommand
125             .contains(";select 23-25:A/1.1;color[128,128,128]"));
126     // S and G are both coloured #4949b6 == (73, 73, 182)
127     assertTrue(
128             chainACommand.contains(";select 26-30:A/1.1;color[73,73,182]"));
129
130     String chainBCommand = commands[1];
131     // M colour is #82827d == (130, 130, 125)
132     assertTrue(
133             chainBCommand.contains("select 21:B/2.1;color[130,130,125]"));
134     // V colour is #ffff00 == (255, 255, 0)
135     assertTrue(chainBCommand.contains(";select 22:B/2.1;color[255,255,0]"));
136     // hidden columns are Gray (128, 128, 128)
137     assertTrue(chainBCommand
138             .contains(";select 23-25:B/2.1;color[128,128,128]"));
139     // S and G are both coloured #4949b6 == (73, 73, 182)
140     assertTrue(
141             chainBCommand.contains(";select 26-30:B/2.1;color[73,73,182]"));
142   }
143
144   @Test(groups = "Functional")
145   public void testGetAtomSpec()
146   {
147     StructureCommandsI testee = new JmolCommands();
148     AtomSpecModel model = new AtomSpecModel();
149     assertEquals(testee.getAtomSpec(model, false), "");
150     model.addRange(1, 2, 4, "A");
151     assertEquals(testee.getAtomSpec(model, false), "2-4:A/1.1");
152     model.addRange(1, 8, 8, "A");
153     assertEquals(testee.getAtomSpec(model, false), "2-4:A/1.1|8:A/1.1");
154     model.addRange(1, 5, 7, "B");
155     assertEquals(testee.getAtomSpec(model, false),
156             "2-4:A/1.1|8:A/1.1|5-7:B/1.1");
157     model.addRange(1, 3, 5, "A");
158     assertEquals(testee.getAtomSpec(model, false),
159             "2-5:A/1.1|8:A/1.1|5-7:B/1.1");
160     model.addRange(2, 1, 4, "B");
161     assertEquals(testee.getAtomSpec(model, false),
162             "2-5:A/1.1|8:A/1.1|5-7:B/1.1|1-4:B/2.1");
163     model.addRange(2, 5, 9, "C");
164     assertEquals(testee.getAtomSpec(model, false),
165             "2-5:A/1.1|8:A/1.1|5-7:B/1.1|1-4:B/2.1|5-9:C/2.1");
166     model.addRange(1, 8, 10, "B");
167     assertEquals(testee.getAtomSpec(model, false),
168             "2-5:A/1.1|8:A/1.1|5-10:B/1.1|1-4:B/2.1|5-9:C/2.1");
169     model.addRange(1, 8, 9, "B");
170     assertEquals(testee.getAtomSpec(model, false),
171             "2-5:A/1.1|8:A/1.1|5-10:B/1.1|1-4:B/2.1|5-9:C/2.1");
172     model.addRange(2, 3, 10, "C"); // subsumes 5-9
173     assertEquals(testee.getAtomSpec(model, false),
174             "2-5:A/1.1|8:A/1.1|5-10:B/1.1|1-4:B/2.1|3-10:C/2.1");
175     model.addRange(5, 25, 35, " ");
176     assertEquals(testee.getAtomSpec(model, false),
177             "2-5:A/1.1|8:A/1.1|5-10:B/1.1|1-4:B/2.1|3-10:C/2.1|25-35:/5.1");
178   
179   }
180
181   @Test(groups = { "Functional" })
182   public void testSuperposeStructures()
183   {
184     StructureCommandsI testee = new JmolCommands();
185     AtomSpecModel ref = new AtomSpecModel();
186     ref.addRange(1, 12, 14, "A");
187     ref.addRange(1, 18, 18, "B");
188     ref.addRange(1, 22, 23, "B");
189     AtomSpecModel toAlign = new AtomSpecModel();
190     toAlign.addRange(2, 15, 17, "B");
191     toAlign.addRange(2, 20, 21, "B");
192     toAlign.addRange(2, 22, 22, "C");
193     String command = testee.superposeStructures(ref, toAlign);
194     String refSpec = "12-14:A/1.1|18:B/1.1|22-23:B/1.1";
195     String toAlignSpec = "15-17:B/2.1|20-21:B/2.1|22:C/2.1";
196     String expected = String.format(
197             "compare {2.1} {1.1} SUBSET {(*.CA | *.P) and conformation=1} ATOMS {%s}{%s} ROTATE TRANSLATE ;select %s|%s;cartoons",
198             toAlignSpec, refSpec, toAlignSpec, refSpec);
199     assertEquals(command, expected);
200   }
201 }