0a0b634cceb7717c68904da594706950c935673d
[jalview.git] / test / jalview / ext / jmol / JalviewJmolBindingTest.java
1 package jalview.ext.jmol;
2
3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.assertNotNull;
5
6 import jalview.datamodel.PDBEntry;
7 import jalview.datamodel.SequenceI;
8 import jalview.gui.AlignFrame;
9 import jalview.gui.AlignViewport;
10 import jalview.gui.AppJmolBinding;
11 import jalview.io.DataSourceType;
12 import jalview.io.FileLoader;
13 import jalview.structure.StructureMapping;
14 import jalview.structure.StructureSelectionManager;
15
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.testng.annotations.BeforeTest;
21 import org.testng.annotations.Test;
22
23 import junit.extensions.PA;
24
25 public class JalviewJmolBindingTest
26 {
27   private AlignFrame af;
28
29   @BeforeTest(alwaysRun = true)
30   public void setup()
31   {
32     af = new FileLoader().LoadFileWaitTillLoaded("examples/uniref50.fa",
33             DataSourceType.FILE);
34   }
35
36   @Test(groups = "Functional")
37   public void testBuildShowStructuresCommand()
38   {
39     AlignViewport av = af.getViewport();
40     PDBEntry[] pdbs = new PDBEntry[] {};
41     SequenceI seq1 = av.getAlignment().findSequenceMatch("FER1_SPIOL")[0];
42     assertNotNull(seq1);
43     SequenceI seq2 = av.getAlignment().findSequenceMatch("FER2_ARATH")[0];
44     assertNotNull(seq2);
45     StructureSelectionManager ssm = new StructureSelectionManager();
46     SequenceI[][] seqs = new SequenceI[][] { { seq1 }, { seq2 } };
47     AppJmolBinding testee = new AppJmolBinding(null, ssm, pdbs, seqs,
48             null);
49
50     /*
51      * map FER1_SPIOL residues 51-100 to residues 1-50 (atoms 1-250) in 1A70
52      * and residues 110-147 to structure residues 60-97
53      * (in fact there is no gap, added here for test purposes)
54      */
55     HashMap<Integer, int[]> map = new HashMap<>();
56     for (int pos = 51; pos <= 100; pos++)
57     {
58       map.put(pos, new int[] { pos - 50, 5 * (pos - 50) });
59     }
60     for (int pos = 110; pos <= 147; pos++)
61     {
62       map.put(pos, new int[] { pos - 50, 5 * (pos - 50) });
63     }
64     StructureMapping sm1 = new StructureMapping(seq1, "1a70.pdb", "1A70",
65             "A", map, null);
66     ssm.addStructureMapping(sm1);
67
68     /*
69      * map FER2_ARATH residues 53-148 to residues 2-97 in 4ZHO
70      */
71     map = new HashMap<>();
72     for (int pos = 53; pos <= 148; pos++)
73     {
74       map.put(pos, new int[] { pos - 51, 5 * (pos - 51) });
75     }
76     StructureMapping sm2 = new StructureMapping(seq2, "4zho.pdb", "4ZHO",
77             "B", map, null);
78     ssm.addStructureMapping(sm2);
79
80     /*
81      * show everything
82      */
83     String cmd = testee.buildShowStructuresCommand(av, true);
84     assertEquals(cmd, "display *; cartoon only; zoom 0");
85     cmd = testee.buildShowStructuresCommand(av, false);
86     assertEquals(cmd, "display *; cartoon only");
87
88     /*
89      * stub out modelFileNames - array index is Jmol
90      * model number - 1
91      */
92     PA.setValue(testee, "modelFileNames",
93             new String[]
94             { "1a70.pdb", "4zho.pdb" });
95
96     /*
97      * stub out lookup map from pdb:chain to filename
98      */
99     Map<String, String> chainFiles = new HashMap<>();
100     PA.setValue(testee, "chainFile", chainFiles);
101     chainFiles.put("1A70:A", "1a70.pdb");
102     chainFiles.put("1A70:B", "1a70.pdb");
103     chainFiles.put("4ZHO:B", "4zho.pdb");
104     chainFiles.put("4ZHO:C", "4zho.pdb");
105
106     /*
107      * show all except for selected chains to hide
108      */
109     List<String> chainsToHide = (List<String>) PA.getValue(testee,
110             "chainsToHide");
111     chainsToHide.add("1A70:B");
112     chainsToHide.add("4ZHO:C");
113     cmd = testee.buildShowStructuresCommand(av, true);
114     assertEquals(cmd,
115             "display *; hide add :B/1.1,:C/2.1; cartoon only; zoom 0");
116
117     /*
118      * show alignment only, no chains hidden
119      */
120     chainsToHide.clear();
121     testee.setShowAlignmentOnly(true);
122     cmd = testee.buildShowStructuresCommand(av, false);
123     assertEquals(cmd,
124             "hide *;display (1-50,60-97)&:A/1.1,2-97:B/2.1; select displayed; cartoon only");
125
126     /*
127      * now with a chain hidden
128      */
129     chainsToHide.add("4ZHO:C");
130     cmd = testee.buildShowStructuresCommand(av, false);
131     String expected = "hide *;display (1-50,60-97)&:A/1.1,2-97:B/2.1; select displayed; hide add :C/2.1; cartoon only";
132     assertEquals(cmd, expected);
133
134     /*
135      * hide columns in the mapped region - should not change the command (yet)
136      */
137     int fromCol = seq1.findIndex(60); // structure residue 10
138     int toCol = seq1.findIndex(70); // structure residue 20
139     av.hideColumns(fromCol - 1, toCol - 1);
140     cmd = testee.buildShowStructuresCommand(av, false);
141     assertEquals(cmd, expected);
142
143     /*
144      * select 'hide hidden columns'
145      * command should now exclude these in both mapped sequences
146      */
147     testee.setHideHiddenRegions(true);
148     cmd = testee.buildShowStructuresCommand(av, false);
149     expected = "hide *;display (1-9,21-50,60-97)&:A/1.1,(2-10,22-97)&:B/2.1; select displayed; hide add :C/2.1; cartoon only";
150     assertEquals(cmd, expected);
151
152     /*
153      * deselect 'show alignment only'
154      * hide hidden columns is now ignored
155      */
156     testee.setShowAlignmentOnly(false);
157     cmd = testee.buildShowStructuresCommand(av, false);
158     assertEquals(cmd, "display *; hide add :C/2.1; cartoon only");
159   }
160 }