1 package jalview.structure;
3 import jalview.api.AlignViewportI;
4 import jalview.api.AlignmentViewPanel;
5 import jalview.api.FeatureRenderer;
6 import jalview.api.SequenceRenderer;
7 import jalview.datamodel.AlignmentI;
8 import jalview.datamodel.HiddenColumns;
9 import jalview.datamodel.SequenceI;
10 import jalview.renderer.seqfeatures.FeatureColourFinder;
11 import jalview.util.Comparison;
13 import java.awt.Color;
14 import java.util.ArrayList;
15 import java.util.LinkedHashMap;
16 import java.util.List;
18 import java.util.Map.Entry;
21 * A base class holding methods useful to all classes that implement commands
22 * for structure viewers
27 public abstract class StructureCommandsBase implements StructureCommandsI
29 private static final String CMD_SEPARATOR = ";";
32 * Returns something that separates concatenated commands
36 protected static String getCommandSeparator()
42 public String[] setAttributesForFeatures(StructureSelectionManager ssm,
43 String[] files, SequenceI[][] sequence, AlignmentViewPanel avp)
45 // default does nothing, override where this is implemented
50 * Returns the lowest model number used by the structure viewer
55 public int getModelStartNo()
61 * Helper method to add one contiguous range to the AtomSpec model for the given
62 * value (creating the model if necessary). As used by Jalview, {@code value} is
64 * <li>a colour, when building a 'colour structure by sequence' command</li>
65 * <li>a feature value, when building a 'set Chimera attributes from features'
76 public static final void addAtomSpecRange(Map<Object, AtomSpecModel> map,
78 int model, int startPos, int endPos, String chain)
81 * Get/initialize map of data for the colour
83 AtomSpecModel atomSpec = map.get(value);
86 atomSpec = new AtomSpecModel();
87 map.put(value, atomSpec);
90 atomSpec.addRange(model, startPos, endPos, chain);
94 * Traverse the map of colours/models/chains/positions to construct a list of
95 * 'color' commands (one per distinct colour used). The format of each command
96 * is specific to the structure viewer.
102 public String[] colourBySequence(Map<Object, AtomSpecModel> colourMap)
105 * This version concatenates all commands into a single String (semi-colon
106 * delimited). If length limit issues arise, refactor to return one color
107 * command per colour.
109 List<String> commands = new ArrayList<>();
110 StringBuilder sb = new StringBuilder(256);
111 boolean firstColour = true;
112 for (Object key : colourMap.keySet())
114 Color colour = (Color) key;
117 sb.append(getCommandSeparator()).append(" ");
120 final AtomSpecModel colourData = colourMap.get(colour);
121 sb.append(getColourCommand(colourData, colour));
123 commands.add(sb.toString());
125 return commands.toArray(new String[commands.size()]);
129 * Returns a command to colour the atoms represented by {@code atomSpecModel}
130 * with the colour specified by {@code colourCode}.
132 * @param atomSpecModel
136 protected String getColourCommand(AtomSpecModel atomSpecModel, Color colour)
138 String atomSpec = getAtomSpec(atomSpecModel, false);
139 return getColourCommand(atomSpec, colour);
143 * Returns a command to colour the atoms described (in viewer command syntax)
144 * by {@code atomSpec} with the colour specified by {@code colourCode}
150 protected abstract String getColourCommand(String atomSpec, Color colour);
153 public String colourByResidues(Map<String, Color> colours)
155 StringBuilder cmd = new StringBuilder(12 * colours.size());
157 for (Entry<String, Color> entry : colours.entrySet())
159 String residue = entry.getKey();
160 String atomSpec = getResidueSpec(residue);
161 cmd.append(getColourCommand(atomSpec, entry.getValue()));
162 cmd.append(getCommandSeparator());
164 return cmd.toString();
168 * Helper method to append one start-end range to an atomspec string
174 * @param firstPositionForModel
176 protected void appendRange(StringBuilder sb, int start, int end,
177 String chain, boolean firstPositionForModel, boolean isChimeraX)
179 if (!firstPositionForModel)
189 sb.append(start).append("-").append(end);
195 if (!" ".equals(chain))
203 * Returns the atom specifier meaning all occurrences of the given residue
208 protected abstract String getResidueSpec(String residue);