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.varna;
23 import jalview.api.SequenceRenderer;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.SequenceI;
26 import jalview.renderer.seqfeatures.FeatureColourFinder;
27 import jalview.structure.StructureMapping;
28 import jalview.structure.StructureSelectionManager;
30 import java.awt.Color;
31 import java.util.ArrayList;
34 * Routines for generating Jmol commands for Jalview/Jmol binding another
40 public class VarnaCommands
44 * Jmol utility which constructs the commands to colour chains by the given
48 public static String[] getColourBySequenceCommand(
49 StructureSelectionManager ssm, String[] files,
50 SequenceI[][] sequence, SequenceRenderer sr,
51 FeatureColourFinder finder, AlignmentI alignment)
53 ArrayList<String> str = new ArrayList<String>();
54 StringBuffer command = new StringBuffer();
56 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
58 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
60 if (mapping == null || mapping.length < 1)
66 for (int s = 0; s < sequence[pdbfnum].length; s++)
68 for (int sp, m = 0; m < mapping.length; m++)
70 if (mapping[m].getSequence() == sequence[pdbfnum][s]
71 && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1)
73 SequenceI asp = alignment.getSequenceAt(sp);
74 for (int r = 0; r < asp.getLength(); r++)
76 // no mapping to gaps in sequence
77 if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
81 int pos = mapping[m].getPDBResNum(asp.findPosition(r));
83 if (pos < 1 || pos == lastPos)
90 Color col = sr.getResidueColour(sequence[pdbfnum][s], r,
93 String newSelcom = (mapping[m].getChain() != " "
94 ? ":" + mapping[m].getChain()
95 : "") + "/" + (pdbfnum + 1) + ".1" + ";color["
96 + col.getRed() + "," + col.getGreen() + ","
97 + col.getBlue() + "]";
98 if (command.length() > newSelcom.length() && command
99 .substring(command.length() - newSelcom.length())
102 command = VarnaCommands.condenseCommand(command, pos);
105 // TODO: deal with case when buffer is too large for Jmol to parse
106 // - execute command and flush
109 if (command.length() > 51200)
112 str.add(command.toString());
113 command.setLength(0);
115 command.append("select " + pos);
116 command.append(newSelcom);
125 str.add(command.toString());
126 command.setLength(0);
128 return str.toArray(new String[str.size()]);
131 public static StringBuffer condenseCommand(StringBuffer command, int pos)
134 // work back to last 'select'
135 int p = command.length(), q = p;
144 } while ((q = command.indexOf("select", p)) == -1 && p > 0);
146 StringBuffer sb = new StringBuffer(command.substring(0, q + 7));
148 command = command.delete(0, q + 7);
152 if (command.indexOf("-") > -1)
154 start = command.substring(0, command.indexOf("-"));
158 start = command.substring(0, command.indexOf(":"));
161 sb.append(start + "-" + pos + command.substring(command.indexOf(":")));