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;
29 import jalview.bin.Console;
32 * A base class holding methods useful to all classes that implement commands
33 * for structure viewers
38 public abstract class StructureCommandsBase implements StructureCommandsI
40 public static final String NAMESPACE_PREFIX = "jv_";
42 private static final String CMD_SEPARATOR = ";";
45 * Returns something that separates concatenated commands
49 protected String getCommandSeparator()
55 * Returns the lowest model number used by the structure viewer
60 public int getModelStartNo()
66 * Helper method to add one contiguous range to the AtomSpec model for the
67 * given value (creating the model if necessary). As used by Jalview,
70 * <li>a colour, when building a 'colour structure by sequence' command</li>
71 * <li>a feature value, when building a 'set Chimera attributes from features'
82 public static final void addAtomSpecRange(Map<Object, AtomSpecModel> map,
83 Object value, String model, int startPos, int endPos,
87 * Get/initialize map of data for the colour
89 AtomSpecModel atomSpec = map.get(value);
92 atomSpec = new AtomSpecModel();
93 map.put(value, atomSpec);
96 atomSpec.addRange(model, startPos, endPos, chain);
100 * Makes a structure viewer attribute name for a Jalview feature type by
101 * prefixing it with "jv_", and replacing any non-alphanumeric characters with
107 protected String makeAttributeName(String featureType)
109 StringBuilder sb = new StringBuilder();
110 if (featureType != null)
112 for (char c : featureType.toCharArray())
114 sb.append(Character.isLetterOrDigit(c) ? c : '_');
117 String attName = NAMESPACE_PREFIX + sb.toString();
122 * Traverse the map of colours/models/chains/positions to construct a list of
123 * 'color' commands (one per distinct colour used). The format of each command
124 * is specific to the structure viewer.
126 * The default implementation returns a single command containing one command
127 * per colour, concatenated.
133 public List<StructureCommandI> colourBySequence(
134 Map<Object, AtomSpecModel> colourMap)
136 List<StructureCommandI> commands = new ArrayList<>();
137 StringBuilder sb = new StringBuilder(colourMap.size() * 20);
138 boolean first = true;
139 for (Object key : colourMap.keySet())
141 Color colour = (Color) key;
142 final AtomSpecModel colourData = colourMap.get(colour);
143 StructureCommandI command = getColourCommand(colourData, colour);
146 sb.append(getCommandSeparator());
149 sb.append(command.getCommand());
152 commands.add(new StructureCommand(sb.toString()));
157 * Returns a command to colour the atoms represented by {@code atomSpecModel}
158 * with the colour specified by {@code colourCode}.
160 * @param atomSpecModel
164 protected StructureCommandI getColourCommand(AtomSpecModel atomSpecModel,
167 String atomSpec = getAtomSpec(atomSpecModel, AtomSpecType.RESIDUE_ONLY);
168 return colourResidues(atomSpec, colour);
172 * Returns a command to colour the atoms described (in viewer command syntax)
173 * by {@code atomSpec} with the colour specified by {@code colourCode}
179 protected abstract StructureCommandI colourResidues(String atomSpec,
183 public List<StructureCommandI> colourByResidues(
184 Map<String, Color> colours)
186 List<StructureCommandI> commands = new ArrayList<>();
187 for (Entry<String, Color> entry : colours.entrySet())
189 commands.add(colourResidue(entry.getKey(), entry.getValue()));
194 private StructureCommandI colourResidue(String resName, Color col)
196 String atomSpec = getResidueSpec(resName);
197 return colourResidues(atomSpec, col);
201 * Helper method to append one start-end range to an atomspec string
207 * @param firstPositionForModel
209 protected void appendRange(StringBuilder sb, int start, int end,
210 String chain, boolean firstPositionForModel, boolean isChimeraX)
212 if (!firstPositionForModel)
222 sb.append(start).append("-").append(end);
228 if (!" ".equals(chain))
236 * Returns the atom specifier meaning all occurrences of the given residue
241 protected abstract String getResidueSpec(String residue);
244 public List<StructureCommandI> setAttributes(
245 Map<String, Map<Object, AtomSpecModel>> featureValues)
247 // default does nothing, override where this is implemented
252 public List<StructureCommandI> startNotifications(String uri)
258 public List<StructureCommandI> stopNotifications()
264 public StructureCommandI getSelectedResidues()
270 public StructureCommandI listResidueAttributes()
276 public StructureCommandI getResidueAttributes(String attName)
282 public StructureCommandI restoreSession(String filePath)
284 return loadFile(filePath);