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 public static final String NAMESPACE_PREFIX = "jv_";
20 private static final String CMD_SEPARATOR = ";";
23 * Returns something that separates concatenated commands
27 protected String getCommandSeparator()
33 * Returns the lowest model number used by the structure viewer
38 public int getModelStartNo()
44 * Helper method to add one contiguous range to the AtomSpec model for the given
45 * value (creating the model if necessary). As used by Jalview, {@code value} is
47 * <li>a colour, when building a 'colour structure by sequence' command</li>
48 * <li>a feature value, when building a 'set Chimera attributes from features'
59 public static final void addAtomSpecRange(Map<Object, AtomSpecModel> map,
60 Object value, String model, int startPos, int endPos,
64 * Get/initialize map of data for the colour
66 AtomSpecModel atomSpec = map.get(value);
69 atomSpec = new AtomSpecModel();
70 map.put(value, atomSpec);
73 atomSpec.addRange(model, startPos, endPos, chain);
77 * Makes a structure viewer attribute name for a Jalview feature type by
78 * prefixing it with "jv_", and replacing any non-alphanumeric characters with
84 protected String makeAttributeName(String featureType)
86 StringBuilder sb = new StringBuilder();
87 if (featureType != null)
89 for (char c : featureType.toCharArray())
91 sb.append(Character.isLetterOrDigit(c) ? c : '_');
94 String attName = NAMESPACE_PREFIX + sb.toString();
99 * Traverse the map of colours/models/chains/positions to construct a list of
100 * 'color' commands (one per distinct colour used). The format of each command
101 * is specific to the structure viewer.
103 * The default implementation returns a single command containing one command
104 * per colour, concatenated.
110 public List<StructureCommandI> colourBySequence(
111 Map<Object, AtomSpecModel> colourMap)
113 List<StructureCommandI> commands = new ArrayList<>();
114 StringBuilder sb = new StringBuilder(colourMap.size() * 20);
115 boolean first = true;
116 for (Object key : colourMap.keySet())
118 Color colour = (Color) key;
119 final AtomSpecModel colourData = colourMap.get(colour);
120 StructureCommandI command = getColourCommand(colourData, colour);
123 sb.append(getCommandSeparator());
126 sb.append(command.getCommand());
129 commands.add(new StructureCommand(sb.toString()));
134 * Returns a command to colour the atoms represented by {@code atomSpecModel}
135 * with the colour specified by {@code colourCode}.
137 * @param atomSpecModel
141 protected StructureCommandI getColourCommand(AtomSpecModel atomSpecModel,
144 String atomSpec = getAtomSpec(atomSpecModel, false);
145 return colourResidues(atomSpec, colour);
149 * Returns a command to colour the atoms described (in viewer command syntax)
150 * by {@code atomSpec} with the colour specified by {@code colourCode}
156 protected abstract StructureCommandI colourResidues(String atomSpec,
160 public List<StructureCommandI> colourByResidues(
161 Map<String, Color> colours)
163 List<StructureCommandI> commands = new ArrayList<>();
164 for (Entry<String, Color> entry : colours.entrySet())
166 commands.add(colourResidue(entry.getKey(), entry.getValue()));
171 private StructureCommandI colourResidue(String resName, Color col)
173 String atomSpec = getResidueSpec(resName);
174 return colourResidues(atomSpec, col);
178 * Helper method to append one start-end range to an atomspec string
184 * @param firstPositionForModel
186 protected void appendRange(StringBuilder sb, int start, int end,
187 String chain, boolean firstPositionForModel, boolean isChimeraX)
189 if (!firstPositionForModel)
199 sb.append(start).append("-").append(end);
205 if (!" ".equals(chain))
213 * Returns the atom specifier meaning all occurrences of the given residue
218 protected abstract String getResidueSpec(String residue);
221 public List<StructureCommandI> setAttributes(
222 Map<String, Map<Object, AtomSpecModel>> featureValues)
224 // default does nothing, override where this is implemented
229 public List<StructureCommandI> startNotifications(String uri)
235 public List<StructureCommandI> stopNotifications()
241 public StructureCommandI getSelectedResidues()
247 public StructureCommandI listResidueAttributes()
253 public StructureCommandI getResidueAttributes(String attName)