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
45 * given value (creating the model if necessary). As used by Jalview,
48 * <li>a colour, when building a 'colour structure by sequence' command</li>
49 * <li>a feature value, when building a 'set Chimera attributes from features'
60 public static final void addAtomSpecRange(Map<Object, AtomSpecModel> map,
61 Object value, String model, int startPos, int endPos,
65 * Get/initialize map of data for the colour
67 AtomSpecModel atomSpec = map.get(value);
70 atomSpec = new AtomSpecModel();
71 map.put(value, atomSpec);
74 atomSpec.addRange(model, startPos, endPos, chain);
78 * Makes a structure viewer attribute name for a Jalview feature type by
79 * prefixing it with "jv_", and replacing any non-alphanumeric characters with
85 protected String makeAttributeName(String featureType)
87 StringBuilder sb = new StringBuilder();
88 if (featureType != null)
90 for (char c : featureType.toCharArray())
92 sb.append(Character.isLetterOrDigit(c) ? c : '_');
95 String attName = NAMESPACE_PREFIX + sb.toString();
100 * Traverse the map of colours/models/chains/positions to construct a list of
101 * 'color' commands (one per distinct colour used). The format of each command
102 * is specific to the structure viewer.
104 * The default implementation returns a single command containing one command
105 * per colour, concatenated.
111 public List<StructureCommandI> colourBySequence(
112 Map<Object, AtomSpecModel> colourMap)
114 List<StructureCommandI> commands = new ArrayList<>();
115 StringBuilder sb = new StringBuilder(colourMap.size() * 20);
116 boolean first = true;
117 for (Object key : colourMap.keySet())
119 Color colour = (Color) key;
120 final AtomSpecModel colourData = colourMap.get(colour);
121 StructureCommandI command = getColourCommand(colourData, colour);
124 sb.append(getCommandSeparator());
127 sb.append(command.getCommand());
130 commands.add(new StructureCommand(sb.toString()));
135 * Returns a command to colour the atoms represented by {@code atomSpecModel}
136 * with the colour specified by {@code colourCode}.
138 * @param atomSpecModel
142 protected StructureCommandI getColourCommand(AtomSpecModel atomSpecModel,
145 String atomSpec = getAtomSpec(atomSpecModel, AtomSpecType.RESIDUE_ONLY);
146 return colourResidues(atomSpec, colour);
150 * Returns a command to colour the atoms described (in viewer command syntax)
151 * by {@code atomSpec} with the colour specified by {@code colourCode}
157 protected abstract StructureCommandI colourResidues(String atomSpec,
161 public List<StructureCommandI> colourByResidues(
162 Map<String, Color> colours)
164 List<StructureCommandI> commands = new ArrayList<>();
165 for (Entry<String, Color> entry : colours.entrySet())
167 commands.add(colourResidue(entry.getKey(), entry.getValue()));
172 private StructureCommandI colourResidue(String resName, Color col)
174 String atomSpec = getResidueSpec(resName);
175 return colourResidues(atomSpec, col);
179 * Helper method to append one start-end range to an atomspec string
185 * @param firstPositionForModel
187 protected void appendRange(StringBuilder sb, int start, int end,
188 String chain, boolean firstPositionForModel, boolean isChimeraX)
190 if (!firstPositionForModel)
200 sb.append(start).append("-").append(end);
206 if (!" ".equals(chain))
214 * Returns the atom specifier meaning all occurrences of the given residue
219 protected abstract String getResidueSpec(String residue);
222 public List<StructureCommandI> setAttributes(
223 Map<String, Map<Object, AtomSpecModel>> featureValues)
225 // default does nothing, override where this is implemented
230 public List<StructureCommandI> startNotifications(String uri)
236 public List<StructureCommandI> stopNotifications()
242 public StructureCommandI getSelectedResidues()
248 public StructureCommandI listResidueAttributes()
254 public StructureCommandI getResidueAttributes(String attName)