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 * Helper method to add one contiguous range to the AtomSpec model for the given
34 * value (creating the model if necessary). As used by Jalview, {@code value} is
36 * <li>a colour, when building a 'colour structure by sequence' command</li>
37 * <li>a feature value, when building a 'set Chimera attributes from features'
48 public static final void addAtomSpecRange(Map<Object, AtomSpecModel> map,
49 Object value, String model, int startPos, int endPos,
53 * Get/initialize map of data for the colour
55 AtomSpecModel atomSpec = map.get(value);
58 atomSpec = new AtomSpecModel();
59 map.put(value, atomSpec);
62 atomSpec.addRange(model, startPos, endPos, chain);
66 * Makes a structure viewer attribute name for a Jalview feature type by
67 * prefixing it with "jv_", and replacing any non-alphanumeric characters with
73 protected String makeAttributeName(String featureType)
75 StringBuilder sb = new StringBuilder();
76 if (featureType != null)
78 for (char c : featureType.toCharArray())
80 sb.append(Character.isLetterOrDigit(c) ? c : '_');
83 String attName = NAMESPACE_PREFIX + sb.toString();
88 * Traverse the map of colours/models/chains/positions to construct a list of
89 * 'color' commands (one per distinct colour used). The format of each command
90 * is specific to the structure viewer.
92 * The default implementation returns a single command containing one command
93 * per colour, concatenated.
99 public List<StructureCommandI> colourBySequence(
100 Map<Object, AtomSpecModel> colourMap)
102 List<StructureCommandI> commands = new ArrayList<>();
103 StringBuilder sb = new StringBuilder(colourMap.size() * 20);
104 boolean first = true;
105 for (Object key : colourMap.keySet())
107 Color colour = (Color) key;
108 final AtomSpecModel colourData = colourMap.get(colour);
109 StructureCommandI command = getColourCommand(colourData, colour);
112 sb.append(getCommandSeparator());
115 sb.append(command.getCommand());
118 commands.add(new StructureCommand(sb.toString()));
123 * Returns a command to colour the atoms represented by {@code atomSpecModel}
124 * with the colour specified by {@code colourCode}.
126 * @param atomSpecModel
130 protected StructureCommandI getColourCommand(AtomSpecModel atomSpecModel,
133 String atomSpec = getAtomSpec(atomSpecModel, false);
134 return colourResidues(atomSpec, colour);
138 * Returns a command to colour the atoms described (in viewer command syntax)
139 * by {@code atomSpec} with the colour specified by {@code colourCode}
145 protected abstract StructureCommandI colourResidues(String atomSpec,
149 public List<StructureCommandI> colourByResidues(
150 Map<String, Color> colours)
152 List<StructureCommandI> commands = new ArrayList<>();
153 for (Entry<String, Color> entry : colours.entrySet())
155 commands.add(colourResidue(entry.getKey(), entry.getValue()));
160 private StructureCommandI colourResidue(String resName, Color col)
162 String atomSpec = getResidueSpec(resName);
163 return colourResidues(atomSpec, col);
167 * Helper method to append one start-end range to an atomspec string
173 * @param firstPositionForModel
175 protected void appendRange(StringBuilder sb, int start, int end,
176 String chain, boolean firstPositionForModel, boolean isChimeraX)
178 if (!firstPositionForModel)
188 sb.append(start).append("-").append(end);
194 if (!" ".equals(chain))
202 * Returns the atom specifier meaning all occurrences of the given residue
207 protected abstract String getResidueSpec(String residue);
210 public List<StructureCommandI> setAttributes(
211 Map<String, Map<Object, AtomSpecModel>> featureValues)
213 // default does nothing, override where this is implemented
218 public List<StructureCommandI> startNotifications(String uri)
224 public List<StructureCommandI> stopNotifications()
230 public StructureCommandI getSelectedResidues()
236 public StructureCommandI listResidueAttributes()
242 public StructureCommandI getResidueAttributes(String attName)