1 package jalview.structure;
4 import java.util.ArrayList;
7 import java.util.Map.Entry;
9 import jalview.api.AlignmentViewPanel;
10 import jalview.datamodel.SequenceI;
13 * A base class holding methods useful to all classes that implement commands
14 * for structure viewers
19 public abstract class StructureCommandsBase implements StructureCommandsI
21 private static final String CMD_SEPARATOR = ";";
24 * Returns something that separates concatenated commands
28 protected static String getCommandSeparator()
34 public List<StructureCommandI> setAttributesForFeatures(
35 StructureSelectionManager ssm,
36 String[] files, SequenceI[][] sequence, AlignmentViewPanel avp)
38 // default does nothing, override where this is implemented
43 * Returns the lowest model number used by the structure viewer
48 public int getModelStartNo()
54 * Helper method to add one contiguous range to the AtomSpec model for the given
55 * value (creating the model if necessary). As used by Jalview, {@code value} is
57 * <li>a colour, when building a 'colour structure by sequence' command</li>
58 * <li>a feature value, when building a 'set Chimera attributes from features'
69 public static final void addAtomSpecRange(Map<Object, AtomSpecModel> map,
70 Object value, String model, int startPos, int endPos,
74 * Get/initialize map of data for the colour
76 AtomSpecModel atomSpec = map.get(value);
79 atomSpec = new AtomSpecModel();
80 map.put(value, atomSpec);
83 atomSpec.addRange(model, startPos, endPos, chain);
87 * Traverse the map of colours/models/chains/positions to construct a list of
88 * 'color' commands (one per distinct colour used). The format of each command
89 * is specific to the structure viewer.
91 * The default implementation returns a single command containing one command
92 * per colour, concatenated.
98 public List<StructureCommandI> colourBySequence(
99 Map<Object, AtomSpecModel> colourMap)
101 List<StructureCommandI> commands = new ArrayList<>();
102 StringBuilder sb = new StringBuilder(colourMap.size() * 20);
103 boolean first = true;
104 for (Object key : colourMap.keySet())
106 Color colour = (Color) key;
107 final AtomSpecModel colourData = colourMap.get(colour);
108 StructureCommandI command = getColourCommand(colourData, colour);
111 sb.append(getCommandSeparator());
114 sb.append(command.getCommand());
117 commands.add(new StructureCommand(sb.toString()));
122 * Returns a command to colour the atoms represented by {@code atomSpecModel}
123 * with the colour specified by {@code colourCode}.
125 * @param atomSpecModel
129 protected StructureCommandI getColourCommand(AtomSpecModel atomSpecModel,
132 String atomSpec = getAtomSpec(atomSpecModel, false);
133 return getColourCommand(atomSpec, colour);
137 * Returns a command to colour the atoms described (in viewer command syntax)
138 * by {@code atomSpec} with the colour specified by {@code colourCode}
144 protected abstract StructureCommandI getColourCommand(String atomSpec,
148 public List<StructureCommandI> colourByResidues(
149 Map<String, Color> colours)
151 List<StructureCommandI> commands = new ArrayList<>();
152 for (Entry<String, Color> entry : colours.entrySet())
154 commands.add(colourResidue(entry.getKey(), entry.getValue()));
159 private StructureCommandI colourResidue(String resName, Color col)
161 String atomSpec = getResidueSpec(resName);
162 return getColourCommand(atomSpec, col);
166 * Helper method to append one start-end range to an atomspec string
172 * @param firstPositionForModel
174 protected void appendRange(StringBuilder sb, int start, int end,
175 String chain, boolean firstPositionForModel, boolean isChimeraX)
177 if (!firstPositionForModel)
187 sb.append(start).append("-").append(end);
193 if (!" ".equals(chain))
201 * Returns the atom specifier meaning all occurrences of the given residue
206 protected abstract String getResidueSpec(String residue);