fde4bb5ada29bcfff790cc8cb95492d6c64379a0
[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 java.util.Arrays;
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Map;
10
11 import org.testng.annotations.BeforeTest;
12 import org.testng.annotations.Test;
13
14 import ext.edu.ucsf.rbvi.strucviz2.ChimeraModel;
15 import ext.edu.ucsf.rbvi.strucviz2.StructureManager.ModelType;
16 import jalview.datamodel.PDBEntry;
17 import jalview.datamodel.SequenceI;
18 import jalview.gui.AlignFrame;
19 import jalview.gui.AlignViewport;
20 import jalview.gui.ChimeraViewFrame;
21 import jalview.gui.JalviewChimeraBindingModel;
22 import jalview.io.DataSourceType;
23 import jalview.io.FileLoader;
24 import jalview.structure.StructureCommandI;
25 import jalview.structure.StructureMapping;
26 import jalview.structure.StructureSelectionManager;
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     StructureCommandI cmd = testee.buildShowStructuresCommand(av, true);
59     assertEquals(cmd.getCommand(), "~display; ribbon; focus");
60     cmd = testee.buildShowStructuresCommand(av, false);
61     assertEquals(cmd.getCommand(), "~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.getCommand(),
120             "~display; ribbon; ~ribbon #0:.B; ~ribbon #1:.C");
121
122     /*
123      * show alignment only, no chains hidden
124      */
125     chainsToHide.clear();
126     testee.setShowAlignmentOnly(true);
127     cmd = testee.buildShowStructuresCommand(av, false);
128     assertEquals(cmd
129             .getCommand(),
130             "~display; ~ribbon; ribbon #0:1-50.A,60-97.A|#1:2-97.B");
131
132     /*
133      * now with a chain hidden
134      */
135     chainsToHide.add("4ZHO:C");
136     cmd = testee.buildShowStructuresCommand(av, false);
137     String expected = "~display; ~ribbon; ribbon #0:1-50.A,60-97.A|#1:2-97.B; ~ribbon #1:.C";
138     assertEquals(cmd.getCommand(), expected);
139
140     /*
141      * hide columns in the mapped region - should not change the command (yet)
142      */
143     int fromCol = seq1.findIndex(60); // structure residue 10
144     int toCol = seq1.findIndex(70); // structure residue 20
145     av.hideColumns(fromCol - 1, toCol - 1);
146     cmd = testee.buildShowStructuresCommand(av, false);
147     assertEquals(cmd.getCommand(), expected);
148
149     /*
150      * select 'hide hidden columns'
151      * command should now exclude these in both mapped sequences
152      */
153     testee.setHideHiddenRegions(true);
154     cmd = testee.buildShowStructuresCommand(av, false);
155     expected = "~display; ~ribbon; ribbon #0:1-9.A,21-50.A,60-97.A|#1:2-10.B,22-97.B; ~ribbon #1:.C";
156     assertEquals(cmd.getCommand(), expected);
157
158     /*
159      * deselect 'show alignment only'
160      * hide hidden columns is now ignored
161      */
162     testee.setShowAlignmentOnly(false);
163     cmd = testee.buildShowStructuresCommand(av, false);
164     assertEquals(cmd.getCommand(), "~display; ribbon; ~ribbon #1:.C");
165   }
166 }