+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ *
+ * This file is part of Jalview.
+ *
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * Jalview is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
package jalview.ext.rbvi.chimera;
-import static org.testng.AssertJUnit.assertEquals;
-import static org.testng.AssertJUnit.assertTrue;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+import jalview.structure.AtomSpecModel;
+import jalview.structure.StructureCommandsI;
import java.awt.Color;
-import java.util.Arrays;
+import java.util.HashMap;
import java.util.LinkedHashMap;
-import java.util.List;
import java.util.Map;
import org.testng.annotations.Test;
public class ChimeraCommandsTest
{
+
@Test(groups = { "Functional" })
- public void testAddColourRange()
+ public void testColourBySequence()
{
- Map<Color, Map<Integer, Map<String, List<int[]>>>> map = new LinkedHashMap<Color, Map<Integer, Map<String, List<int[]>>>>();
- ChimeraCommands.addColourRange(map, Color.pink, 1, 2, 4, "A");
- ChimeraCommands.addColourRange(map, Color.pink, 1, 8, 8, "A");
- ChimeraCommands.addColourRange(map, Color.pink, 1, 5, 7, "B");
- ChimeraCommands.addColourRange(map, Color.red, 1, 3, 5, "A");
- ChimeraCommands.addColourRange(map, Color.red, 0, 1, 4, "B");
- ChimeraCommands.addColourRange(map, Color.orange, 0, 5, 9, "C");
-
- // three colours mapped
- assertEquals(3, map.keySet().size());
-
- // Red has two models, Pink and Orange one each
- assertEquals(2, map.get(Color.red).keySet().size());
- assertEquals(1, map.get(Color.orange).keySet().size());
- assertEquals(1, map.get(Color.pink).keySet().size());
-
- // pink model 1 has two chains, red.0 / red.1 / orange.0 one each
- assertEquals(2, map.get(Color.pink).get(1).keySet().size());
- assertEquals(1, map.get(Color.red).get(0).keySet().size());
- assertEquals(1, map.get(Color.red).get(1).keySet().size());
- assertEquals(1, map.get(Color.orange).get(0).keySet().size());
-
- // inspect positions
- List<int[]> posList = map.get(Color.pink).get(1).get("A");
- assertEquals(2, posList.size());
- assertTrue(Arrays.equals(new int[] { 2, 4 }, posList.get(0)));
- assertTrue(Arrays.equals(new int[] { 8, 8 }, posList.get(1)));
-
- posList = map.get(Color.pink).get(1).get("B");
- assertEquals(1, posList.size());
- assertTrue(Arrays.equals(new int[] { 5, 7 }, posList.get(0)));
-
- posList = map.get(Color.red).get(0).get("B");
- assertEquals(1, posList.size());
- assertTrue(Arrays.equals(new int[] { 1, 4 }, posList.get(0)));
-
- posList = map.get(Color.red).get(1).get("A");
- assertEquals(1, posList.size());
- assertTrue(Arrays.equals(new int[] { 3, 5 }, posList.get(0)));
-
- posList = map.get(Color.orange).get(0).get("C");
- assertEquals(1, posList.size());
- assertTrue(Arrays.equals(new int[] { 5, 9 }, posList.get(0)));
+
+ Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
+ ChimeraCommands.addAtomSpecRange(map, Color.blue, 0, 2, 5, "A");
+ ChimeraCommands.addAtomSpecRange(map, Color.blue, 0, 7, 7, "B");
+ ChimeraCommands.addAtomSpecRange(map, Color.blue, 0, 9, 23, "A");
+ ChimeraCommands.addAtomSpecRange(map, Color.blue, 1, 1, 1, "A");
+ ChimeraCommands.addAtomSpecRange(map, Color.blue, 1, 4, 7, "B");
+ ChimeraCommands.addAtomSpecRange(map, Color.yellow, 1, 8, 8, "A");
+ ChimeraCommands.addAtomSpecRange(map, Color.yellow, 1, 3, 5, "A");
+ ChimeraCommands.addAtomSpecRange(map, Color.red, 0, 3, 5, "A");
+ ChimeraCommands.addAtomSpecRange(map, Color.red, 0, 6, 9, "A");
+
+ // Colours should appear in the Chimera command in the order in which
+ // they were added; within colour, by model, by chain, ranges in start order
+ String[] commands = new ChimeraCommands().colourBySequence(map);
+ assertEquals(commands.length, 1);
+ assertEquals(
+ commands[0],
+ "color #0000ff #0:2-5.A,9-23.A,7.B|#1:1.A,4-7.B; color #ffff00 #1:3-5.A,8.A; color #ff0000 #0:3-9.A");
}
@Test(groups = { "Functional" })
- public void testBuildColourCommands()
+ public void testSetAttributes()
{
+ /*
+ * make a map of { featureType, {featureValue, {residue range specification } } }
+ */
+ Map<String, Map<Object, AtomSpecModel>> featuresMap = new LinkedHashMap<>();
+ Map<Object, AtomSpecModel> featureValues = new HashMap<>();
+
+ /*
+ * start with just one feature/value...
+ */
+ featuresMap.put("chain", featureValues);
+ ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 8, 20, "A");
+
+ ChimeraCommands commandGenerator = new ChimeraCommands();
+ String[] commands = commandGenerator.setAttributes(featuresMap);
+ assertEquals(1, commands.length);
- Map<Color, Map<Integer, Map<String, List<int[]>>>> map = new LinkedHashMap<Color, Map<Integer, Map<String, List<int[]>>>>();
- ChimeraCommands.addColourRange(map, Color.blue, 0, 2, 5, "A");
- ChimeraCommands.addColourRange(map, Color.blue, 0, 7, 7, "B");
- ChimeraCommands.addColourRange(map, Color.blue, 0, 9, 23, "A");
- ChimeraCommands.addColourRange(map, Color.blue, 1, 1, 1, "A");
- ChimeraCommands.addColourRange(map, Color.blue, 1, 4, 7, "B");
- ChimeraCommands.addColourRange(map, Color.yellow, 1, 8, 8, "A");
- ChimeraCommands.addColourRange(map, Color.yellow, 1, 3, 5, "A");
- ChimeraCommands.addColourRange(map, Color.red, 0, 3, 5, "A");
+ /*
+ * feature name gets a jv_ namespace prefix
+ * feature value is quoted in case it contains spaces
+ */
+ assertEquals(commands[0], "setattr res jv_chain 'X' #0:8-20.A");
- // Colours should appear in the Chimera command in the order in which
- // they were added; within colour, by model, by chain, and positions as
- // added
- String command = ChimeraCommands.buildColourCommands(map).get(0);
+ // add same feature value, overlapping range
+ ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 3, 9, "A");
+ // same feature value, contiguous range
+ ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 21, 25, "A");
+ commands = commandGenerator.setAttributes(featuresMap);
+ assertEquals(1, commands.length);
+ assertEquals(commands[0], "setattr res jv_chain 'X' #0:3-25.A");
+
+ // same feature value and model, different chain
+ ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 21, 25, "B");
+ // same feature value and chain, different model
+ ChimeraCommands.addAtomSpecRange(featureValues, "X", 1, 26, 30, "A");
+ commands = commandGenerator.setAttributes(featuresMap);
+ assertEquals(1, commands.length);
+ String expected1 = "setattr res jv_chain 'X' #0:3-25.A,21-25.B|#1:26-30.A";
+ assertEquals(commands[0],
+ expected1);
+
+ // same feature, different value
+ ChimeraCommands.addAtomSpecRange(featureValues, "Y", 0, 40, 50, "A");
+ commands = commandGenerator.setAttributes(featuresMap);
+ assertEquals(2, commands.length);
+ // commands are ordered by feature type but not by value
+ // so test for the expected command in either order
+ assertTrue(
+ commands[0].equals(expected1) || commands[1].equals(expected1));
+ String expected2 = "setattr res jv_chain 'Y' #0:40-50.A";
+ assertTrue(
+ commands[0].equals(expected2) || commands[1].equals(expected2));
+
+ featuresMap.clear();
+ featureValues.clear();
+ featuresMap.put("side-chain binding!", featureValues);
+ ChimeraCommands.addAtomSpecRange(featureValues,
+ "<html>metal <a href=\"http:a.b.c/x\"> 'ion!", 0, 7, 15,
+ "A");
+ // feature names are sanitised to change non-alphanumeric to underscore
+ // feature values are sanitised to encode single quote characters
+ commands = commandGenerator.setAttributes(featuresMap);
+ String expected3 = "setattr res jv_side_chain_binding_ '<html>metal <a href=\"http:a.b.c/x\"> 'ion!' #0:7-15.A";
+ assertTrue(
+ commands[0].equals(expected3) || commands[1].equals(expected3));
+ }
+
+ /**
+ * Tests for the method that prefixes and sanitises a feature name so it can
+ * be used as a valid, namespaced attribute name in Chimera
+ */
+ @Test(groups = { "Functional" })
+ public void testMakeAttributeName()
+ {
+ assertEquals(ChimeraCommands.makeAttributeName(null), "jv_");
+ assertEquals(ChimeraCommands.makeAttributeName(""), "jv_");
+ assertEquals(ChimeraCommands.makeAttributeName("helix"), "jv_helix");
+ assertEquals(ChimeraCommands.makeAttributeName("Hello World 24"),
+ "jv_Hello_World_24");
assertEquals(
- "color #0000ff #0:2-5.A,9-23.A,7.B|#1:1.A,4-7.B; color #ffff00 #1:8.A,3-5.A; color #ff0000 #0:3-5.A",
- command);
+ ChimeraCommands.makeAttributeName("!this is-a_very*{odd(name"),
+ "jv__this_is_a_very__odd_name");
+ // name ending in color gets underscore appended
+ assertEquals(ChimeraCommands.makeAttributeName("helixColor"),
+ "jv_helixColor_");
+ }
+
+ @Test(groups = "Functional")
+ public void testGetAtomSpec()
+ {
+ StructureCommandsI testee = new ChimeraCommands();
+ AtomSpecModel model = new AtomSpecModel();
+ assertEquals(testee.getAtomSpec(model, false), "");
+ model.addRange(1, 2, 4, "A");
+ assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A");
+ model.addRange(1, 8, 8, "A");
+ assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A,8.A");
+ model.addRange(1, 5, 7, "B");
+ assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A,8.A,5-7.B");
+ model.addRange(1, 3, 5, "A");
+ assertEquals(testee.getAtomSpec(model, false), "#1:2-5.A,8.A,5-7.B");
+ model.addRange(0, 1, 4, "B");
+ assertEquals(testee.getAtomSpec(model, false),
+ "#0:1-4.B|#1:2-5.A,8.A,5-7.B");
+ model.addRange(0, 5, 9, "C");
+ assertEquals(testee.getAtomSpec(model, false),
+ "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-7.B");
+ model.addRange(1, 8, 10, "B");
+ assertEquals(testee.getAtomSpec(model, false),
+ "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
+ model.addRange(1, 8, 9, "B");
+ assertEquals(testee.getAtomSpec(model, false),
+ "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
+ model.addRange(0, 3, 10, "C"); // subsumes 5-9
+ assertEquals(testee.getAtomSpec(model, false),
+ "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B");
+ model.addRange(5, 25, 35, " "); // empty chain code - e.g. from homology
+ // modelling
+ assertEquals(testee.getAtomSpec(model, false),
+ "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B|#5:25-35.");
+
+ }
+
+ @Test(groups = { "Functional" })
+ public void testSuperposeStructures()
+ {
+ StructureCommandsI testee = new ChimeraCommands();
+ AtomSpecModel ref = new AtomSpecModel();
+ ref.addRange(1, 12, 14, "A");
+ ref.addRange(1, 18, 18, "B");
+ ref.addRange(1, 22, 23, "B");
+ AtomSpecModel toAlign = new AtomSpecModel();
+ toAlign.addRange(2, 15, 17, "B");
+ toAlign.addRange(2, 20, 21, "B");
+ toAlign.addRange(2, 22, 22, "C");
+ String command = testee.superposeStructures(ref, toAlign);
+ String refSpec = "#1:12-14.A,18.B,22-23.B@CA|P&~@.B-Z&~@.2-9";
+ String toAlignSpec = "#2:15-17.B,20-21.B,22.C@CA|P&~@.B-Z&~@.2-9";
+ String expected = String.format(
+ "match %s %s;~display all; chain @CA|P; ribbon %s|%s; focus",
+ refSpec, toAlignSpec, refSpec, toAlignSpec);
+ assertEquals(command, expected);
+ }
+
+ @Test(groups = "Functional")
+ public void testGetAtomSpec_alphaOnly()
+ {
+ StructureCommandsI testee = new ChimeraCommands();
+ AtomSpecModel model = new AtomSpecModel();
+ assertEquals(testee.getAtomSpec(model, true), "");
+ model.addRange(1, 2, 4, "A");
+ assertEquals(testee.getAtomSpec(model, true),
+ "#1:2-4.A@CA|P&~@.B-Z&~@.2-9");
+ model.addRange(1, 8, 8, "A");
+ assertEquals(testee.getAtomSpec(model, true),
+ "#1:2-4.A,8.A@CA|P&~@.B-Z&~@.2-9");
+ model.addRange(1, 5, 7, "B");
+ assertEquals(testee.getAtomSpec(model, true),
+ "#1:2-4.A,8.A,5-7.B@CA|P&~@.B-Z&~@.2-9");
+ model.addRange(1, 3, 5, "A");
+ assertEquals(testee.getAtomSpec(model, true),
+ "#1:2-5.A,8.A,5-7.B@CA|P&~@.B-Z&~@.2-9");
+ model.addRange(0, 1, 4, "B");
+ assertEquals(testee.getAtomSpec(model, true),
+ "#0:1-4.B@CA|P&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-7.B@CA|P&~@.B-Z&~@.2-9");
+ model.addRange(0, 5, 9, "C");
+ assertEquals(testee.getAtomSpec(model, true),
+ "#0:1-4.B,5-9.C@CA|P&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-7.B@CA|P&~@.B-Z&~@.2-9");
+ model.addRange(1, 8, 10, "B");
+ assertEquals(testee.getAtomSpec(model, true),
+ "#0:1-4.B,5-9.C@CA|P&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-10.B@CA|P&~@.B-Z&~@.2-9");
+ model.addRange(1, 8, 9, "B");
+ assertEquals(testee.getAtomSpec(model, true),
+ "#0:1-4.B,5-9.C@CA|P&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-10.B@CA|P&~@.B-Z&~@.2-9");
+ model.addRange(0, 3, 10, "C"); // subsumes 5-9
+ assertEquals(testee.getAtomSpec(model, true),
+ "#0:1-4.B,3-10.C@CA|P&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-10.B@CA|P&~@.B-Z&~@.2-9");
+ model.addRange(5, 25, 35, " "); // empty chain code
+ assertEquals(testee.getAtomSpec(model, true),
+ "#0:1-4.B,3-10.C@CA|P&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-10.B@CA|P&~@.B-Z&~@.2-9|#5:25-35.@CA|P&~@.B-Z&~@.2-9");
+
+ }
+
+ @Test(groups = "Functional")
+ public void testGetModelStartNo()
+ {
+ StructureCommandsI testee = new ChimeraCommands();
+ assertEquals(testee.getModelStartNo(), 0);
+ }
+
+ @Test(groups = "Functional")
+ public void testGetResidueSpec()
+ {
+ ChimeraCommands testee = new ChimeraCommands();
+ assertEquals(testee.getResidueSpec("ALA"), "::ALA");
+ }
+
+ @Test(groups = "Functional")
+ public void testShowBackbone()
+ {
+ ChimeraCommands testee = new ChimeraCommands();
+ assertEquals(testee.showBackbone(), "~display all;chain @CA|P");
+ }
+
+ @Test(groups = "Functional")
+ public void testOpenCommandFile()
+ {
+ ChimeraCommands testee = new ChimeraCommands();
+ assertEquals(testee.openCommandFile("nowhere"), "open cmd:nowhere");
+ }
+
+ @Test(groups = "Functional")
+ public void testSaveSession()
+ {
+ ChimeraCommands testee = new ChimeraCommands();
+ assertEquals(testee.saveSession("somewhere"), "save somewhere");
+ }
+
+ @Test(groups = "Functional")
+ public void testGetColourCommand()
+ {
+ ChimeraCommands testee = new ChimeraCommands();
+ assertEquals(testee.getColourCommand("something", Color.MAGENTA),
+ "color #ff00ff something");
+ }
+
+ @Test(groups = "Functional")
+ public void testSetAttribute()
+ {
+ ChimeraCommands testee = new ChimeraCommands();
+ AtomSpecModel model = new AtomSpecModel();
+ model.addRange(1, 89, 92, "A");
+ model.addRange(2, 12, 20, "B");
+ model.addRange(2, 8, 9, "B");
+ assertEquals(testee.setAttribute("phi", "27.3", model),
+ "setattr res phi '27.3' #1:89-92.A|#2:8-9.B,12-20.B");
}
}