1 package jalview.structure;
3 import jalview.api.AlignmentViewPanel;
4 import jalview.datamodel.SequenceI;
7 import java.util.ArrayList;
10 import java.util.Map.Entry;
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.
95 public List<StructureCommandI> colourBySequence(
96 Map<Object, AtomSpecModel> colourMap)
99 * default implementation creates one command per colour;
100 * override to concatenate colour commands if wanted
102 List<StructureCommandI> commands = new ArrayList<>();
103 for (Object key : colourMap.keySet())
105 Color colour = (Color) key;
106 final AtomSpecModel colourData = colourMap.get(colour);
107 commands.add(getColourCommand(colourData, colour));
114 * Returns a command to colour the atoms represented by {@code atomSpecModel}
115 * with the colour specified by {@code colourCode}.
117 * @param atomSpecModel
121 protected StructureCommandI getColourCommand(AtomSpecModel atomSpecModel,
124 String atomSpec = getAtomSpec(atomSpecModel, false);
125 return getColourCommand(atomSpec, colour);
129 * Returns a command to colour the atoms described (in viewer command syntax)
130 * by {@code atomSpec} with the colour specified by {@code colourCode}
136 protected abstract StructureCommandI getColourCommand(String atomSpec,
140 public List<StructureCommandI> colourByResidues(
141 Map<String, Color> colours)
143 List<StructureCommandI> commands = new ArrayList<>();
144 for (Entry<String, Color> entry : colours.entrySet())
146 commands.add(colourResidue(entry.getKey(), entry.getValue()));
151 private StructureCommandI colourResidue(String resName, Color col)
153 String atomSpec = getResidueSpec(resName);
154 return getColourCommand(atomSpec, col);
158 * Helper method to append one start-end range to an atomspec string
164 * @param firstPositionForModel
166 protected void appendRange(StringBuilder sb, int start, int end,
167 String chain, boolean firstPositionForModel, boolean isChimeraX)
169 if (!firstPositionForModel)
179 sb.append(start).append("-").append(end);
185 if (!" ".equals(chain))
193 * Returns the atom specifier meaning all occurrences of the given residue
198 protected abstract String getResidueSpec(String residue);