2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
\r
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
\r
5 * This file is part of Jalview.
\r
7 * Jalview is free software: you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
\r
11 * Jalview is distributed in the hope that it will be useful, but
\r
12 * WITHOUT ANY WARRANTY; without even the implied warranty
\r
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
14 * PURPOSE. See the GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
\r
18 package jalview.ext.varna;
\r
20 import jalview.api.FeatureRenderer;
\r
21 import jalview.api.SequenceRenderer;
\r
22 import jalview.datamodel.AlignmentI;
\r
23 import jalview.datamodel.SequenceI;
\r
24 import jalview.structure.StructureMapping;
\r
25 import jalview.structure.StructureSelectionManager;
\r
26 import jalview.util.Comparison;
\r
28 import java.awt.Color;
\r
29 import java.util.ArrayList;
\r
32 * Routines for generating Jmol commands for Jalview/Jmol binding
\r
33 * another cruisecontrol test.
\r
38 public class VarnaCommands
\r
42 * Jmol utility which constructs the commands to colour chains by the given alignment
\r
45 public static String[] getColourBySequenceCommand(StructureSelectionManager ssm, String[] files, SequenceI[][] sequence, SequenceRenderer sr, FeatureRenderer fr, AlignmentI alignment)
\r
48 ArrayList<String> str = new ArrayList<String>();
\r
49 StringBuffer command = new StringBuffer();
\r
51 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
\r
53 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
\r
55 if (mapping == null || mapping.length < 1)
\r
59 for (int s = 0; s < sequence[pdbfnum].length; s++)
\r
61 for (int sp, m = 0; m < mapping.length; m++)
\r
63 if (mapping[m].getSequence() == sequence[pdbfnum][s]
\r
64 && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1)
\r
66 SequenceI asp = alignment.getSequenceAt(sp);
\r
67 for (int r = 0; r < asp.getLength(); r++)
\r
69 // no mapping to gaps in sequence
\r
70 if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
\r
74 int pos = mapping[m].getPDBResNum(asp.findPosition(r));
\r
76 if (pos < 1 || pos == lastPos)
\r
81 Color col = sr.getResidueBoxColour(sequence[pdbfnum][s], r);
\r
84 col = fr.findFeatureColour(col, sequence[pdbfnum][s], r);
\r
85 String newSelcom = (mapping[m].getChain() != " " ? ":"
\r
86 + mapping[m].getChain() : "")
\r
95 + col.getBlue() + "]";
\r
96 if (command.length()>newSelcom.length() && command.substring(command.length()-newSelcom.length()).equals(newSelcom))
\r
98 command = VarnaCommands.condenseCommand(command, pos);
\r
101 // TODO: deal with case when buffer is too large for Jmol to parse
\r
102 // - execute command and flush
\r
104 command.append(";");
\r
105 if (command.length()>51200)
\r
107 // add another chunk
\r
108 str.add(command.toString());
\r
109 command.setLength(0);
\r
111 command.append("select " + pos);
\r
112 command.append(newSelcom);
\r
121 str.add(command.toString());
\r
122 command.setLength(0);
\r
124 return str.toArray(new String[str.size()]);
\r
127 public static StringBuffer condenseCommand(StringBuffer command, int pos)
\r
130 // work back to last 'select'
\r
131 int p=command.length(),q=p;
\r
135 } while ((q=command.indexOf("select",p))==-1 && p>0);
\r
137 StringBuffer sb = new StringBuffer(command.substring(0,q+7));
\r
139 command = command.delete(0,q+7);
\r
143 if (command.indexOf("-") > -1)
\r
145 start = command.substring(0, command.indexOf("-"));
\r
149 start = command.substring(0, command.indexOf(":"));
\r
152 sb.append(start + "-" + pos + command.substring(command.indexOf(":")));
\r