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.FeatureRenderer;
24 import jalview.api.SequenceRenderer;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.SequenceI;
27 import jalview.structure.StructureMapping;
28 import jalview.structure.StructureMappingcommandSet;
29 import jalview.structure.StructureSelectionManager;
31 import java.awt.Color;
32 import java.util.ArrayList;
35 * Routines for generating Jmol commands for Jalview/Jmol binding another
41 public class JmolCommands
45 * Jmol utility which constructs the commands to colour chains by the given
48 * @returns Object[] { Object[] { <model being coloured>,
51 public static StructureMappingcommandSet[] getColourBySequenceCommand(
52 StructureSelectionManager ssm, String[] files,
53 SequenceI[][] sequence, SequenceRenderer sr, FeatureRenderer fr,
57 ArrayList<StructureMappingcommandSet> cset = new ArrayList<StructureMappingcommandSet>();
59 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
61 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
62 StringBuffer command = new StringBuffer();
63 StructureMappingcommandSet smc;
64 ArrayList<String> str = new ArrayList<String>();
66 if (mapping == null || mapping.length < 1)
72 for (int s = 0; s < sequence[pdbfnum].length; s++)
74 for (int sp, m = 0; m < mapping.length; m++)
76 if (mapping[m].getSequence() == sequence[pdbfnum][s]
77 && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1)
79 SequenceI asp = alignment.getSequenceAt(sp);
80 for (int r = 0; r < asp.getLength(); r++)
82 // no mapping to gaps in sequence
83 if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
87 int pos = mapping[m].getPDBResNum(asp.findPosition(r));
89 if (pos < 1 || pos == lastPos)
96 Color col = sr.getResidueBoxColour(sequence[pdbfnum][s], r);
100 col = fr.findFeatureColour(col, sequence[pdbfnum][s], r);
102 String newSelcom = (mapping[m].getChain() != " " ? ":"
103 + mapping[m].getChain() : "")
112 + col.getBlue() + "]";
113 if (command.length() > newSelcom.length()
114 && command.substring(
115 command.length() - newSelcom.length())
118 command = JmolCommands.condenseCommand(command, pos);
121 // TODO: deal with case when buffer is too large for Jmol to parse
122 // - execute command and flush
125 if (command.length() > 51200)
128 str.add(command.toString());
129 command.setLength(0);
131 command.append("select " + pos);
132 command.append(newSelcom);
140 str.add(command.toString());
141 command.setLength(0);
143 // Finally, add the command set ready to be returned.
144 cset.add(new StructureMappingcommandSet(JmolCommands.class,
145 files[pdbfnum], str.toArray(new String[str.size()])));
148 return cset.toArray(new StructureMappingcommandSet[cset.size()]);
151 public static StringBuffer condenseCommand(StringBuffer command, int pos)
154 // work back to last 'select'
155 int p = command.length(), q = p;
164 } while ((q = command.indexOf("select", p)) == -1 && p > 0);
166 StringBuffer sb = new StringBuffer(command.substring(0, q + 7));
168 command = command.delete(0, q + 7);
172 if (command.indexOf("-") > -1)
174 start = command.substring(0, command.indexOf("-"));
178 start = command.substring(0, command.indexOf(":"));
181 sb.append(start + "-" + pos + command.substring(command.indexOf(":")));