2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.structure;
23 import java.awt.Color;
24 import java.util.ArrayList;
25 import java.util.List;
27 import java.util.Map.Entry;
30 * A base class holding methods useful to all classes that implement commands
31 * for structure viewers
36 public abstract class StructureCommandsBase implements StructureCommandsI
38 public static final String NAMESPACE_PREFIX = "jv_";
40 private static final String CMD_SEPARATOR = ";";
43 * Returns something that separates concatenated commands
47 protected String getCommandSeparator()
53 * Returns the lowest model number used by the structure viewer
58 public int getModelStartNo()
64 * Helper method to add one contiguous range to the AtomSpec model for the
65 * given value (creating the model if necessary). As used by Jalview,
68 * <li>a colour, when building a 'colour structure by sequence' command</li>
69 * <li>a feature value, when building a 'set Chimera attributes from features'
80 public static final void addAtomSpecRange(Map<Object, AtomSpecModel> map,
81 Object value, String model, int startPos, int endPos,
85 * Get/initialize map of data for the colour
87 AtomSpecModel atomSpec = map.get(value);
90 atomSpec = new AtomSpecModel();
91 map.put(value, atomSpec);
94 atomSpec.addRange(model, startPos, endPos, chain);
98 * Makes a structure viewer attribute name for a Jalview feature type by
99 * prefixing it with "jv_", and replacing any non-alphanumeric characters with
105 protected String makeAttributeName(String featureType)
107 StringBuilder sb = new StringBuilder();
108 if (featureType != null)
110 for (char c : featureType.toCharArray())
112 sb.append(Character.isLetterOrDigit(c) ? c : '_');
115 String attName = NAMESPACE_PREFIX + sb.toString();
120 * Traverse the map of colours/models/chains/positions to construct a list of
121 * 'color' commands (one per distinct colour used). The format of each command
122 * is specific to the structure viewer.
124 * The default implementation returns a single command containing one command
125 * per colour, concatenated.
131 public List<StructureCommandI> colourBySequence(
132 Map<Object, AtomSpecModel> colourMap)
134 List<StructureCommandI> commands = new ArrayList<>();
135 StringBuilder sb = new StringBuilder(colourMap.size() * 20);
136 boolean first = true;
137 for (Object key : colourMap.keySet())
139 Color colour = (Color) key;
140 final AtomSpecModel colourData = colourMap.get(colour);
141 StructureCommandI command = getColourCommand(colourData, colour);
144 sb.append(getCommandSeparator());
147 sb.append(command.getCommand());
150 commands.add(new StructureCommand(sb.toString()));
155 * Returns a command to colour the atoms represented by {@code atomSpecModel}
156 * with the colour specified by {@code colourCode}.
158 * @param atomSpecModel
162 protected StructureCommandI getColourCommand(AtomSpecModel atomSpecModel,
165 String atomSpec = getAtomSpec(atomSpecModel, AtomSpecType.RESIDUE_ONLY);
166 return colourResidues(atomSpec, colour);
170 * Returns a command to colour the atoms described (in viewer command syntax)
171 * by {@code atomSpec} with the colour specified by {@code colourCode}
177 protected abstract StructureCommandI colourResidues(String atomSpec,
181 public List<StructureCommandI> colourByResidues(
182 Map<String, Color> colours)
184 List<StructureCommandI> commands = new ArrayList<>();
185 for (Entry<String, Color> entry : colours.entrySet())
187 commands.add(colourResidue(entry.getKey(), entry.getValue()));
192 private StructureCommandI colourResidue(String resName, Color col)
194 String atomSpec = getResidueSpec(resName);
195 return colourResidues(atomSpec, col);
199 * Helper method to append one start-end range to an atomspec string
205 * @param firstPositionForModel
207 protected void appendRange(StringBuilder sb, int start, int end,
208 String chain, boolean firstPositionForModel, boolean isChimeraX)
210 if (!firstPositionForModel)
220 sb.append(start).append("-").append(end);
226 if (!" ".equals(chain))
234 * Returns the atom specifier meaning all occurrences of the given residue
239 protected abstract String getResidueSpec(String residue);
242 public List<StructureCommandI> setAttributes(
243 Map<String, Map<Object, AtomSpecModel>> featureValues)
245 // default does nothing, override where this is implemented
250 public List<StructureCommandI> startNotifications(String uri)
256 public List<StructureCommandI> stopNotifications()
262 public StructureCommandI getSelectedResidues()
268 public StructureCommandI listResidueAttributes()
274 public StructureCommandI getResidueAttributes(String attName)