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