2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.ext.jmol;
21 import jalview.api.FeatureRenderer;
22 import jalview.api.SequenceRenderer;
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.SequenceI;
25 import jalview.structure.StructureMapping;
26 import jalview.structure.StructureMappingcommandSet;
27 import jalview.structure.StructureSelectionManager;
29 import java.awt.Color;
30 import java.util.ArrayList;
33 * Routines for generating Jmol commands for Jalview/Jmol binding another
39 public class JmolCommands
43 * Jmol utility which constructs the commands to colour chains by the given
46 * @returns Object[] { Object[] { <model being coloured>,
49 public static StructureMappingcommandSet[] getColourBySequenceCommand(
50 StructureSelectionManager ssm, String[] files,
51 SequenceI[][] sequence, SequenceRenderer sr, FeatureRenderer fr,
55 ArrayList<StructureMappingcommandSet> cset = new ArrayList<StructureMappingcommandSet>();
57 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
59 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
60 StringBuffer command = new StringBuffer();
61 StructureMappingcommandSet smc;
62 ArrayList<String> str = new ArrayList<String>();
64 if (mapping == null || mapping.length < 1)
68 for (int s = 0; s < sequence[pdbfnum].length; s++)
70 for (int sp, m = 0; m < mapping.length; m++)
72 if (mapping[m].getSequence() == sequence[pdbfnum][s]
73 && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1)
75 SequenceI asp = alignment.getSequenceAt(sp);
76 for (int r = 0; r < asp.getLength(); r++)
78 // no mapping to gaps in sequence
79 if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
83 int pos = mapping[m].getPDBResNum(asp.findPosition(r));
85 if (pos < 1 || pos == lastPos)
90 Color col = sr.getResidueBoxColour(sequence[pdbfnum][s], r);
93 col = fr.findFeatureColour(col, sequence[pdbfnum][s], r);
94 String newSelcom = (mapping[m].getChain() != " " ? ":"
95 + mapping[m].getChain() : "")
104 + col.getBlue() + "]";
105 if (command.length() > newSelcom.length()
106 && command.substring(
107 command.length() - newSelcom.length())
110 command = JmolCommands.condenseCommand(command, pos);
113 // TODO: deal with case when buffer is too large for Jmol to parse
114 // - execute command and flush
117 if (command.length() > 51200)
120 str.add(command.toString());
121 command.setLength(0);
123 command.append("select " + pos);
124 command.append(newSelcom);
132 str.add(command.toString());
133 command.setLength(0);
135 // Finally, add the command set ready to be returned.
136 cset.add(new StructureMappingcommandSet(JmolCommands.class,
137 files[pdbfnum], str.toArray(new String[str.size()])));
140 return cset.toArray(new StructureMappingcommandSet[cset.size()]);
143 public static StringBuffer condenseCommand(StringBuffer command, int pos)
146 // work back to last 'select'
147 int p = command.length(), q = p;
156 } while ((q = command.indexOf("select", p)) == -1 && p > 0);
158 StringBuffer sb = new StringBuffer(command.substring(0, q + 7));
160 command = command.delete(0, q + 7);
164 if (command.indexOf("-") > -1)
166 start = command.substring(0, command.indexOf("-"));
170 start = command.substring(0, command.indexOf(":"));
173 sb.append(start + "-" + pos + command.substring(command.indexOf(":")));