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.FeatureRenderer;
25 import jalview.api.SequenceRenderer;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.ColumnSelection;
28 import jalview.datamodel.SequenceI;
29 import jalview.structure.StructureMapping;
30 import jalview.structure.StructureMappingcommandSet;
31 import jalview.structure.StructureSelectionManager;
33 import java.awt.Color;
34 import java.util.ArrayList;
35 import java.util.List;
38 * Routines for generating Jmol commands for Jalview/Jmol binding another
44 public class JmolCommands
48 * Jmol utility which constructs the commands to colour chains by the given
51 * @returns Object[] { Object[] { <model being coloured>,
54 public static StructureMappingcommandSet[] getColourBySequenceCommand(
55 StructureSelectionManager ssm, String[] files,
56 SequenceI[][] sequence, SequenceRenderer sr, FeatureRenderer fr,
57 AlignViewportI viewport)
59 ColumnSelection cs = viewport.getColumnSelection();
60 AlignmentI al = viewport.getAlignment();
61 List<StructureMappingcommandSet> cset = new ArrayList<StructureMappingcommandSet>();
63 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
65 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
66 StringBuffer command = new StringBuffer();
67 StructureMappingcommandSet smc;
68 ArrayList<String> str = new ArrayList<String>();
70 if (mapping == null || mapping.length < 1)
76 for (int s = 0; s < sequence[pdbfnum].length; s++)
78 for (int sp, m = 0; m < mapping.length; m++)
80 if (mapping[m].getSequence() == sequence[pdbfnum][s]
81 && (sp = al.findIndex(sequence[pdbfnum][s])) > -1)
83 SequenceI asp = al.getSequenceAt(sp);
84 for (int r = 0; r < asp.getLength(); r++)
86 // no mapping to gaps in sequence
87 if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
91 int pos = mapping[m].getPDBResNum(asp.findPosition(r));
93 if (pos < 1 || pos == lastPos)
100 Color col = sr.getResidueBoxColour(sequence[pdbfnum][s], r);
104 col = fr.findFeatureColour(col, sequence[pdbfnum][s], r);
108 * shade hidden regions darker
110 if (!cs.isVisible(r))
112 // col = ColorUtils.darkerThan(col);
116 String newSelcom = ":" + mapping[m].getChain()
125 + col.getBlue() + "]";
126 if (command.length() > newSelcom.length()
127 && command.substring(
128 command.length() - newSelcom.length())
131 command = JmolCommands.condenseCommand(command, pos);
134 // TODO: deal with case when buffer is too large for Jmol to parse
135 // - execute command and flush
138 if (command.length() > 51200)
141 str.add(command.toString());
142 command.setLength(0);
144 command.append("select " + pos);
145 command.append(newSelcom);
153 str.add(command.toString());
154 command.setLength(0);
156 // Finally, add the command set ready to be returned.
157 cset.add(new StructureMappingcommandSet(JmolCommands.class,
158 files[pdbfnum], str.toArray(new String[str.size()])));
161 return cset.toArray(new StructureMappingcommandSet[cset.size()]);
164 public static StringBuffer condenseCommand(StringBuffer command, int pos)
167 // work back to last 'select'
168 int p = command.length(), q = p;
177 } while ((q = command.indexOf("select", p)) == -1 && p > 0);
179 StringBuffer sb = new StringBuffer(command.substring(0, q + 7));
181 command = command.delete(0, q + 7);
185 if (command.indexOf("-") > -1)
187 start = command.substring(0, command.indexOf("-"));
191 start = command.substring(0, command.indexOf(":"));
194 sb.append(start + "-" + pos + command.substring(command.indexOf(":")));