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.ext.jmol;
23 import jalview.api.AlignViewportI;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.api.FeatureRenderer;
26 import jalview.api.SequenceRenderer;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.HiddenColumns;
29 import jalview.datamodel.SequenceI;
30 import jalview.renderer.seqfeatures.FeatureColourFinder;
31 import jalview.structure.StructureCommandsBase;
32 import jalview.structure.StructureMapping;
33 import jalview.structure.StructureSelectionManager;
34 import jalview.util.Comparison;
36 import java.awt.Color;
37 import java.util.ArrayList;
38 import java.util.List;
40 import java.util.Map.Entry;
43 * Routines for generating Jmol commands for Jalview/Jmol binding
48 public class JmolCommands extends StructureCommandsBase
50 private static final String CMD_COLOUR_BY_CHARGE = "select *;color white;select ASP,GLU;color red;"
51 + "select LYS,ARG;color blue;select CYS;color yellow";
53 private static final String CMD_COLOUR_BY_CHAIN = "select *;color chain";
55 private static String formatRGB(Color c) {
56 return c == null ? null
57 : String.format("[%d,%d,%d]", c.getRed(), c.getGreen(),
62 public String[] colourBySequence(
63 StructureSelectionManager ssm, String[] files,
64 SequenceI[][] sequence, SequenceRenderer sr,
65 AlignmentViewPanel viewPanel)
67 // TODO refactor to call buildColoursMap() first...
69 FeatureRenderer fr = viewPanel.getFeatureRenderer();
70 FeatureColourFinder finder = new FeatureColourFinder(fr);
71 AlignViewportI viewport = viewPanel.getAlignViewport();
72 HiddenColumns cs = viewport.getAlignment().getHiddenColumns();
73 AlignmentI al = viewport.getAlignment();
74 List<String> cset = new ArrayList<>();
76 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
78 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
79 StringBuilder command = new StringBuilder(128);
80 List<String> str = new ArrayList<>();
82 if (mapping == null || mapping.length < 1)
87 for (int s = 0; s < sequence[pdbfnum].length; s++)
89 for (int sp, m = 0; m < mapping.length; m++)
91 if (mapping[m].getSequence() == sequence[pdbfnum][s]
92 && (sp = al.findIndex(sequence[pdbfnum][s])) > -1)
94 int lastPos = StructureMapping.UNASSIGNED_VALUE;
95 SequenceI asp = al.getSequenceAt(sp);
96 for (int r = 0; r < asp.getLength(); r++)
98 // no mapping to gaps in sequence
99 if (Comparison.isGap(asp.getCharAt(r)))
103 int pos = mapping[m].getPDBResNum(asp.findPosition(r));
109 if (pos == StructureMapping.UNASSIGNED_VALUE)
111 // terminate current colour op
112 if (command.length() > 0
113 && command.charAt(command.length() - 1) != ';')
118 lastPos = StructureMapping.UNASSIGNED_VALUE;
124 Color col = sr.getResidueColour(sequence[pdbfnum][s], r,
128 * shade hidden regions darker
130 if (!cs.isVisible(r))
135 String newSelcom = (mapping[m].getChain() != " "
136 ? ":" + mapping[m].getChain()
137 : "") + "/" + (pdbfnum + 1) + ".1" + ";color["
138 + col.getRed() + "," + col.getGreen() + ","
139 + col.getBlue() + "]";
140 if (command.length() > newSelcom.length() && command
141 .substring(command.length() - newSelcom.length())
144 command = JmolCommands.condenseCommand(command, pos);
147 // TODO: deal with case when buffer is too large for Jmol to parse
148 // - execute command and flush
150 if (command.length() > 0
151 && command.charAt(command.length() - 1) != ';')
156 if (command.length() > 51200)
159 str.add(command.toString());
160 command.setLength(0);
162 command.append("select " + pos);
163 command.append(newSelcom);
171 str.add(command.toString());
172 command.setLength(0);
177 return cset.toArray(new String[cset.size()]);
180 public static StringBuilder condenseCommand(StringBuilder command,
184 // work back to last 'select'
185 int p = command.length(), q = p;
194 } while ((q = command.indexOf("select", p)) == -1 && p > 0);
196 StringBuilder sb = new StringBuilder(command.substring(0, q + 7));
198 command = command.delete(0, q + 7);
202 if (command.indexOf("-") > -1)
204 start = command.substring(0, command.indexOf("-"));
208 start = command.substring(0, command.indexOf(":"));
211 sb.append(start + "-" + pos + command.substring(command.indexOf(":")));
217 public String colourByChain()
219 return CMD_COLOUR_BY_CHAIN;
223 public String colourByCharge()
225 return CMD_COLOUR_BY_CHARGE;
229 public String colourByResidues(Map<String, Color> colours)
231 StringBuilder cmd = new StringBuilder(128);
232 cmd.append("select *;color white;");
235 * concatenate commands like
236 * select VAL;color[100,215,55]
238 for (Entry<String, Color> entry : colours.entrySet())
240 Color col = entry.getValue();
241 String resCode = entry.getKey();
242 cmd.append("select ").append(resCode).append(";");
243 cmd.append("color[");
244 cmd.append(formatRGB(col));
248 return cmd.toString();
252 public String setBackgroundColour(Color col)
254 return "background " + formatRGB(col);
258 public String focusView()
264 public String showChains(List<String> toShow)
266 StringBuilder atomSpec = new StringBuilder(128);
267 boolean first = true;
268 for (String chain : toShow)
270 String[] tokens = chain.split(":");
271 if (tokens.length == 2)
275 atomSpec.append(" or ");
278 atomSpec.append(":").append(tokens[1]).append(" /").append(tokens[0]);
282 String spec = atomSpec.toString();
283 String command = "select *;restrict " + spec + ";cartoon;center "