4 package jalview.ext.jmol;
\r
6 import jalview.api.FeatureRenderer;
\r
7 import jalview.api.SequenceRenderer;
\r
8 import jalview.datamodel.AlignmentI;
\r
9 import jalview.datamodel.SequenceI;
\r
10 import jalview.structure.StructureMapping;
\r
11 import jalview.structure.StructureSelectionManager;
\r
12 import jalview.util.Comparison;
\r
14 import java.awt.Color;
\r
15 import java.util.ArrayList;
\r
18 * Routines for generating Jmol commands for Jalview/Jmol binding
\r
22 public class JmolCommands
\r
26 * Jmol utility which constructs the commands to colour chains by the given alignment
\r
29 public static String[] getColourBySequenceCommand(StructureSelectionManager ssm, String[] files, SequenceI[][] sequence, SequenceRenderer sr, FeatureRenderer fr, AlignmentI alignment)
\r
31 ArrayList<String> str = new ArrayList<String>();
\r
32 StringBuffer command = new StringBuffer();
\r
34 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
\r
36 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
\r
38 if (mapping == null || mapping.length < 1)
\r
42 for (int s = 0; s < sequence[pdbfnum].length; s++)
\r
44 for (int sp, m = 0; m < mapping.length; m++)
\r
46 if (mapping[m].getSequence() == sequence[pdbfnum][s]
\r
47 && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1)
\r
49 SequenceI asp = alignment.getSequenceAt(sp);
\r
50 for (int r = 0; r < asp.getLength(); r++)
\r
52 // no mapping to gaps in sequence
\r
53 if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
\r
57 int pos = mapping[m].getPDBResNum(asp.findPosition(r));
\r
59 if (pos < 1 || pos == lastPos)
\r
64 Color col = sr.getResidueBoxColour(sequence[pdbfnum][s], r);
\r
67 col = fr.findFeatureColour(col, sequence[pdbfnum][s], r);
\r
68 String newSelcom = (mapping[m].getChain() != " " ? ":"
\r
69 + mapping[m].getChain() : "")
\r
78 + col.getBlue() + "]";
\r
79 if (command.length()>newSelcom.length() && command.substring(command.length()-newSelcom.length()).equals(newSelcom))
\r
81 command = JmolCommands.condenseCommand(command, pos);
\r
84 // TODO: deal with case when buffer is too large for Jmol to parse
\r
85 // - execute command and flush
\r
87 command.append(";");
\r
88 if (command.length()>51200)
\r
90 // add another chunk
\r
91 str.add(command.toString());
\r
92 command.setLength(0);
\r
94 command.append("select " + pos);
\r
95 command.append(newSelcom);
\r
104 str.add(command.toString());
\r
105 command.setLength(0);
\r
107 return str.toArray(new String[str.size()]);
\r
110 public static StringBuffer condenseCommand(StringBuffer command, int pos)
\r
113 // work back to last 'select'
\r
114 int p=command.length(),q=p;
\r
118 } while ((q=command.indexOf("select",p))==-1 && p>0);
\r
120 StringBuffer sb = new StringBuffer(command.substring(0,q+7));
\r
122 command = command.delete(0,q+7);
\r
126 if (command.indexOf("-") > -1)
\r
128 start = command.substring(0, command.indexOf("-"));
\r
132 start = command.substring(0, command.indexOf(":"));
\r
135 sb.append(start + "-" + pos + command.substring(command.indexOf(":")));
\r