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 if (colourMap == null)
138 List<StructureCommandI> commands = new ArrayList<>();
139 StringBuilder sb = new StringBuilder(colourMap.size() * 20);
140 boolean first = true;
141 for (Object key : colourMap.keySet())
143 Color colour = (Color) key;
144 final AtomSpecModel colourData = colourMap.get(colour);
145 StructureCommandI command = getColourCommand(colourData, colour);
148 sb.append(getCommandSeparator());
151 sb.append(command.getCommand());
154 commands.add(new StructureCommand(sb.toString()));
159 * Returns a command to colour the atoms represented by {@code atomSpecModel}
160 * with the colour specified by {@code colourCode}.
162 * @param atomSpecModel
166 protected StructureCommandI getColourCommand(AtomSpecModel atomSpecModel,
169 String atomSpec = getAtomSpec(atomSpecModel, AtomSpecType.RESIDUE_ONLY);
170 return colourResidues(atomSpec, colour);
174 * Returns a command to colour the atoms described (in viewer command syntax)
175 * by {@code atomSpec} with the colour specified by {@code colourCode}
181 protected abstract StructureCommandI colourResidues(String atomSpec,
185 public List<StructureCommandI> colourByResidues(
186 Map<String, Color> colours)
188 List<StructureCommandI> commands = new ArrayList<>();
189 for (Entry<String, Color> entry : colours.entrySet())
191 commands.add(colourResidue(entry.getKey(), entry.getValue()));
196 private StructureCommandI colourResidue(String resName, Color col)
198 String atomSpec = getResidueSpec(resName);
199 return colourResidues(atomSpec, col);
203 * Helper method to append one start-end range to an atomspec string
209 * @param firstPositionForModel
211 protected void appendRange(StringBuilder sb, int start, int end,
212 String chain, boolean firstPositionForModel, boolean isChimeraX)
214 if (!firstPositionForModel)
224 sb.append(start).append("-").append(end);
230 if (!" ".equals(chain))
238 * Returns the atom specifier meaning all occurrences of the given residue
243 protected abstract String getResidueSpec(String residue);
246 public List<StructureCommandI> setAttributes(
247 Map<String, Map<Object, AtomSpecModel>> featureValues)
249 // default does nothing, override where this is implemented
254 public List<StructureCommandI> startNotifications(String uri)
260 public List<StructureCommandI> stopNotifications()
266 public StructureCommandI getSelectedResidues()
272 public StructureCommandI listResidueAttributes()
278 public StructureCommandI getResidueAttributes(String attName)
284 public StructureCommandI restoreSession(String filePath)
286 return loadFile(filePath);