1 /*******************************************************************************
2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 *******************************************************************************/
21 package jalview.ext.jmol;
\r
23 import jalview.api.FeatureRenderer;
\r
24 import jalview.api.SequenceRenderer;
\r
25 import jalview.datamodel.AlignmentI;
\r
26 import jalview.datamodel.SequenceI;
\r
27 import jalview.structure.StructureMapping;
\r
28 import jalview.structure.StructureMappingcommandSet;
\r
29 import jalview.structure.StructureSelectionManager;
\r
30 import jalview.util.Comparison;
\r
32 import java.awt.Color;
\r
33 import java.util.ArrayList;
\r
35 * Routines for generating Jmol commands for Jalview/Jmol binding
\r
36 * another cruisecontrol test.
\r
41 public class JmolCommands
\r
45 * Jmol utility which constructs the commands to colour chains by the given alignment
\r
46 * @returns Object[] { Object[] { <model being coloured>,
\r
49 public static StructureMappingcommandSet[] getColourBySequenceCommand(StructureSelectionManager ssm, String[] files, SequenceI[][] sequence, SequenceRenderer sr, FeatureRenderer fr, AlignmentI alignment)
\r
52 ArrayList<StructureMappingcommandSet> cset = new ArrayList<StructureMappingcommandSet>();
\r
54 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
\r
56 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
\r
57 StringBuffer command = new StringBuffer();
\r
58 StructureMappingcommandSet smc;
\r
59 ArrayList<String> str = new ArrayList<String>();
\r
61 if (mapping == null || mapping.length < 1)
\r
65 for (int s = 0; s < sequence[pdbfnum].length; s++)
\r
67 for (int sp, m = 0; m < mapping.length; m++)
\r
69 if (mapping[m].getSequence() == sequence[pdbfnum][s]
\r
70 && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1)
\r
72 SequenceI asp = alignment.getSequenceAt(sp);
\r
73 for (int r = 0; r < asp.getLength(); r++)
\r
75 // no mapping to gaps in sequence
\r
76 if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
\r
80 int pos = mapping[m].getPDBResNum(asp.findPosition(r));
\r
82 if (pos < 1 || pos == lastPos)
\r
87 Color col = sr.getResidueBoxColour(sequence[pdbfnum][s], r);
\r
90 col = fr.findFeatureColour(col, sequence[pdbfnum][s], r);
\r
91 String newSelcom = (mapping[m].getChain() != " " ? ":"
\r
92 + mapping[m].getChain() : "")
\r
101 + col.getBlue() + "]";
\r
102 if (command.length()>newSelcom.length() && command.substring(command.length()-newSelcom.length()).equals(newSelcom))
\r
104 command = JmolCommands.condenseCommand(command, pos);
\r
107 // TODO: deal with case when buffer is too large for Jmol to parse
\r
108 // - execute command and flush
\r
110 command.append(";");
\r
111 if (command.length()>51200)
\r
113 // add another chunk
\r
114 str.add(command.toString());
\r
115 command.setLength(0);
\r
117 command.append("select " + pos);
\r
118 command.append(newSelcom);
\r
126 str.add(command.toString());
\r
127 command.setLength(0);
\r
129 // Finally, add the command set ready to be returned.
\r
130 cset.add(new StructureMappingcommandSet(JmolCommands.class,files[pdbfnum], str.toArray(new String[str.size()])));
\r
133 return cset.toArray(new StructureMappingcommandSet[cset.size()]);
\r
136 public static StringBuffer condenseCommand(StringBuffer command, int pos)
\r
139 // work back to last 'select'
\r
140 int p=command.length(),q=p;
\r
144 } while ((q=command.indexOf("select",p))==-1 && p>0);
\r
146 StringBuffer sb = new StringBuffer(command.substring(0,q+7));
\r
148 command = command.delete(0,q+7);
\r
152 if (command.indexOf("-") > -1)
\r
154 start = command.substring(0, command.indexOf("-"));
\r
158 start = command.substring(0, command.indexOf(":"));
\r
161 sb.append(start + "-" + pos + command.substring(command.indexOf(":")));
\r