update author list in license for (JAL-826)
[jalview.git] / src / jalview / ext / jmol / JmolCommands.java
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
4  *
5  * This file is part of Jalview.
6  *
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.
10  *
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.
15  *
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  *******************************************************************************/
18 /**\r
19  * \r
20  */\r
21 package jalview.ext.jmol;\r
22 \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
31 \r
32 import java.awt.Color;\r
33 import java.util.ArrayList;\r
34 /**\r
35  * Routines for generating Jmol commands for Jalview/Jmol binding\r
36  * another cruisecontrol test.\r
37  * \r
38  * @author JimP\r
39  *\r
40  */\r
41 public class JmolCommands\r
42 {\r
43 \r
44   /**\r
45    * Jmol utility which constructs the commands to colour chains by the given alignment\r
46    * @returns Object[] { Object[] { <model being coloured>, \r
47    * \r
48    */\r
49   public static StructureMappingcommandSet[] getColourBySequenceCommand(StructureSelectionManager ssm, String[] files, SequenceI[][] sequence, SequenceRenderer sr, FeatureRenderer fr, AlignmentI alignment)\r
50   {\r
51     \r
52     ArrayList<StructureMappingcommandSet> cset = new ArrayList<StructureMappingcommandSet>();\r
53     \r
54     for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)\r
55     {\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
60   \r
61       if (mapping == null || mapping.length < 1)\r
62         continue;\r
63       \r
64       int lastPos = -1;\r
65       for (int s = 0; s < sequence[pdbfnum].length; s++)\r
66       {\r
67         for (int sp, m = 0; m < mapping.length; m++)\r
68         {\r
69           if (mapping[m].getSequence() == sequence[pdbfnum][s]\r
70                   && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1)\r
71           {\r
72             SequenceI asp = alignment.getSequenceAt(sp);\r
73             for (int r = 0; r < asp.getLength(); r++)\r
74             {\r
75               // no mapping to gaps in sequence\r
76               if (jalview.util.Comparison.isGap(asp.getCharAt(r)))\r
77               {\r
78                 continue;\r
79               }\r
80               int pos = mapping[m].getPDBResNum(asp.findPosition(r));\r
81   \r
82               if (pos < 1 || pos == lastPos)\r
83                 continue;\r
84   \r
85               lastPos = pos;\r
86   \r
87               Color col = sr.getResidueBoxColour(sequence[pdbfnum][s], r);\r
88   \r
89               if (fr != null)\r
90                 col = fr.findFeatureColour(col, sequence[pdbfnum][s], r);\r
91               String newSelcom = (mapping[m].getChain() != " " ? ":"\r
92                       + mapping[m].getChain() : "")\r
93                       + "/"\r
94                       + (pdbfnum + 1)\r
95                       + ".1"\r
96                       + ";color["\r
97                       + col.getRed()\r
98                       + ","\r
99                       + col.getGreen()\r
100                       + ","\r
101                       + col.getBlue() + "]";\r
102               if (command.length()>newSelcom.length() && command.substring(command.length()-newSelcom.length()).equals(newSelcom))\r
103               {\r
104                 command = JmolCommands.condenseCommand(command, pos);\r
105                 continue;\r
106               }\r
107               // TODO: deal with case when buffer is too large for Jmol to parse\r
108               // - execute command and flush\r
109   \r
110               command.append(";");\r
111               if (command.length()>51200)\r
112               {\r
113                 // add another chunk\r
114                 str.add(command.toString());\r
115                 command.setLength(0);\r
116               }\r
117               command.append("select " + pos);\r
118               command.append(newSelcom);\r
119             }\r
120             break;\r
121           }\r
122         }\r
123       }\r
124       {\r
125           // add final chunk\r
126           str.add(command.toString());\r
127           command.setLength(0);\r
128         }\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
131 \r
132     }\r
133     return cset.toArray(new StructureMappingcommandSet[cset.size()]);\r
134   }\r
135 \r
136   public static StringBuffer condenseCommand(StringBuffer command, int pos)\r
137   {\r
138   \r
139     // work back to last 'select'\r
140     int p=command.length(),q=p;\r
141     do {\r
142       p-=6;\r
143       if (p<1) { p=0; };\r
144     } while ((q=command.indexOf("select",p))==-1 && p>0);\r
145     \r
146     StringBuffer sb = new StringBuffer(command.substring(0,q+7));\r
147   \r
148     command =  command.delete(0,q+7);\r
149   \r
150     String start;\r
151   \r
152     if (command.indexOf("-") > -1)\r
153     {\r
154       start = command.substring(0, command.indexOf("-"));\r
155     }\r
156     else\r
157     {\r
158       start = command.substring(0, command.indexOf(":"));\r
159     }\r
160   \r
161     sb.append(start + "-" + pos + command.substring(command.indexOf(":")));\r
162   \r
163     return sb;\r
164   }\r
165 \r
166 }\r