1 package jalview.structure;
4 import java.util.ArrayList;
7 import java.util.Map.Entry;
10 * A base class holding methods useful to all classes that implement commands
11 * for structure viewers
16 public abstract class StructureCommandsBase implements StructureCommandsI
18 private static final String CMD_SEPARATOR = ";";
19 public static final String NAMESPACE_PREFIX = "jv_";
22 * Returns something that separates concatenated commands
26 protected static String getCommandSeparator()
32 * Returns the lowest model number used by the structure viewer
37 public int getModelStartNo()
43 * Helper method to add one contiguous range to the AtomSpec model for the given
44 * value (creating the model if necessary). As used by Jalview, {@code value} is
46 * <li>a colour, when building a 'colour structure by sequence' command</li>
47 * <li>a feature value, when building a 'set Chimera attributes from features'
58 public static final void addAtomSpecRange(Map<Object, AtomSpecModel> map,
59 Object value, String model, int startPos, int endPos,
63 * Get/initialize map of data for the colour
65 AtomSpecModel atomSpec = map.get(value);
68 atomSpec = new AtomSpecModel();
69 map.put(value, atomSpec);
72 atomSpec.addRange(model, startPos, endPos, chain);
76 * Makes a structure viewer attribute name for a Jalview feature type by
77 * prefixing it with "jv_", and replacing any non-alphanumeric characters with
83 protected String makeAttributeName(String featureType)
85 StringBuilder sb = new StringBuilder();
86 if (featureType != null)
88 for (char c : featureType.toCharArray())
90 sb.append(Character.isLetterOrDigit(c) ? c : '_');
93 String attName = NAMESPACE_PREFIX + sb.toString();
98 * Traverse the map of colours/models/chains/positions to construct a list of
99 * 'color' commands (one per distinct colour used). The format of each command
100 * is specific to the structure viewer.
102 * The default implementation returns a single command containing one command
103 * per colour, concatenated.
109 public List<StructureCommandI> colourBySequence(
110 Map<Object, AtomSpecModel> colourMap)
112 List<StructureCommandI> commands = new ArrayList<>();
113 StringBuilder sb = new StringBuilder(colourMap.size() * 20);
114 boolean first = true;
115 for (Object key : colourMap.keySet())
117 Color colour = (Color) key;
118 final AtomSpecModel colourData = colourMap.get(colour);
119 StructureCommandI command = getColourCommand(colourData, colour);
122 sb.append(getCommandSeparator());
125 sb.append(command.getCommand());
128 commands.add(new StructureCommand(sb.toString()));
133 * Returns a command to colour the atoms represented by {@code atomSpecModel}
134 * with the colour specified by {@code colourCode}.
136 * @param atomSpecModel
140 protected StructureCommandI getColourCommand(AtomSpecModel atomSpecModel,
143 String atomSpec = getAtomSpec(atomSpecModel, false);
144 return colourResidues(atomSpec, colour);
148 * Returns a command to colour the atoms described (in viewer command syntax)
149 * by {@code atomSpec} with the colour specified by {@code colourCode}
155 protected abstract StructureCommandI colourResidues(String atomSpec,
159 public List<StructureCommandI> colourByResidues(
160 Map<String, Color> colours)
162 List<StructureCommandI> commands = new ArrayList<>();
163 for (Entry<String, Color> entry : colours.entrySet())
165 commands.add(colourResidue(entry.getKey(), entry.getValue()));
170 private StructureCommandI colourResidue(String resName, Color col)
172 String atomSpec = getResidueSpec(resName);
173 return colourResidues(atomSpec, col);
177 * Helper method to append one start-end range to an atomspec string
183 * @param firstPositionForModel
185 protected void appendRange(StringBuilder sb, int start, int end,
186 String chain, boolean firstPositionForModel, boolean isChimeraX)
188 if (!firstPositionForModel)
198 sb.append(start).append("-").append(end);
204 if (!" ".equals(chain))
212 * Returns the atom specifier meaning all occurrences of the given residue
217 protected abstract String getResidueSpec(String residue);
220 public List<StructureCommandI> setAttributes(
221 Map<String, Map<Object, AtomSpecModel>> featureValues)
223 // default does nothing, override where this is implemented
228 public List<StructureCommandI> startNotifications(String uri)
234 public List<StructureCommandI> stopNotifications()
240 public StructureCommandI getSelectedResidues()