JAL-3253 ApplicationSingletonProvider replaces Instance
[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.StructureMapping;
36 import jalview.structure.StructureMappingcommandSet;
37 import jalview.structure.StructureSelectionManager;
38
39 import java.util.HashMap;
40
41 import org.testng.annotations.BeforeClass;
42 import org.testng.annotations.Test;
43
44 public class JmolCommandsTest
45 {
46
47   @BeforeClass(alwaysRun = true)
48   public void setUpJvOptionPane()
49   {
50     JvOptionPane.setInteractiveMode(false);
51     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
52   }
53
54   @Test(groups = { "Functional" })
55   public void testGetColourBySequenceCommand_noFeatures()
56   {
57     SequenceI seq1 = new Sequence("seq1", "MHRSQTRALK");
58     SequenceI seq2 = new Sequence("seq2", "MRLEITQSGD");
59     AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
60     AlignFrame af = new AlignFrame(al, 800, 500);
61     SequenceRenderer sr = new SequenceRenderer(af.getViewport());
62     SequenceI[][] seqs = new SequenceI[][] { { seq1 }, { seq2 } };
63     String[] files = new String[] { "seq1.pdb", "seq2.pdb" };
64     StructureSelectionManager ssm = StructureSelectionManager
65             .getStructureSelectionManager(null);
66
67     // need some mappings!
68
69     StructureMappingcommandSet[] commands = JmolCommands
70             .getColourBySequenceCommand(ssm, files, seqs, sr, af.alignPanel);
71   }
72
73   @Test(groups = { "Functional" })
74   public void testGetColourBySequenceCommands_hiddenColumns()
75   {
76     /*
77      * load these sequences, coloured by Strand propensity,
78      * with columns 2-4 hidden
79      */
80     SequenceI seq1 = new Sequence("seq1", "MHRSQSSSGG");
81     SequenceI seq2 = new Sequence("seq2", "MVRSNGGSSS");
82     AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
83     AlignFrame af = new AlignFrame(al, 800, 500);
84     af.changeColour_actionPerformed(JalviewColourScheme.Strand.toString());
85     ColumnSelection cs = new ColumnSelection();
86     cs.addElement(2);
87     cs.addElement(3);
88     cs.addElement(4);
89     af.getViewport().setColumnSelection(cs);
90     af.hideSelColumns_actionPerformed(null);
91     SequenceRenderer sr = new SequenceRenderer(af.getViewport());
92     SequenceI[][] seqs = new SequenceI[][] { { seq1 }, { seq2 } };
93     String[] files = new String[] { "seq1.pdb", "seq2.pdb" };
94     StructureSelectionManager ssm = StructureSelectionManager
95             .getStructureSelectionManager(null);
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     StructureMappingcommandSet[] commands = JmolCommands
113             .getColourBySequenceCommand(ssm, files, seqs, sr, af.alignPanel);
114     assertEquals(commands.length, 2);
115     assertEquals(commands[0].commands.length, 1);
116
117     String chainACommand = commands[0].commands[0];
118     // M colour is #82827d == (130, 130, 125) (see strand.html help page)
119     assertTrue(chainACommand
120             .contains("select 21:A/1.1;color[130,130,125]")); // first 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(chainACommand
128             .contains(";select 26-30:A/1.1;color[73,73,182]"));
129
130     String chainBCommand = commands[1].commands[0];
131     // M colour is #82827d == (130, 130, 125)
132     assertTrue(chainBCommand
133             .contains("select 21:B/2.1;color[130,130,125]"));
134     // V colour is #ffff00 == (255, 255, 0)
135     assertTrue(chainBCommand
136 .contains(";select 22:B/2.1;color[255,255,0]"));
137     // hidden columns are Gray (128, 128, 128)
138     assertTrue(chainBCommand
139             .contains(";select 23-25:B/2.1;color[128,128,128]"));
140     // S and G are both coloured #4949b6 == (73, 73, 182)
141     assertTrue(chainBCommand
142             .contains(";select 26-30:B/2.1;color[73,73,182]"));
143   }
144 }