19f535cf214ce803f7eb8f6dead60046a222893e
[jalview.git] / src / jalview / ext / jmol / JmolCommands.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10  * of the License, or (at your option) any later version.
11  *  
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.
16  * 
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.
20  */
21 package jalview.ext.jmol;
22
23 import jalview.api.FeatureRenderer;
24 import jalview.api.SequenceRenderer;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.SequenceI;
27 import jalview.structure.StructureMapping;
28 import jalview.structure.StructureMappingcommandSet;
29 import jalview.structure.StructureSelectionManager;
30
31 import java.awt.Color;
32 import java.util.ArrayList;
33
34 /**
35  * Routines for generating Jmol commands for Jalview/Jmol binding another
36  * cruisecontrol test.
37  * 
38  * @author JimP
39  * 
40  */
41 public class JmolCommands
42 {
43
44   /**
45    * Jmol utility which constructs the commands to colour chains by the given
46    * alignment
47    * 
48    * @returns Object[] { Object[] { <model being coloured>,
49    * 
50    */
51   public static StructureMappingcommandSet[] getColourBySequenceCommand(
52           StructureSelectionManager ssm, String[] files,
53           SequenceI[][] sequence, SequenceRenderer sr, FeatureRenderer fr,
54           AlignmentI alignment)
55   {
56
57     ArrayList<StructureMappingcommandSet> cset = new ArrayList<StructureMappingcommandSet>();
58
59     for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
60     {
61       StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
62       StringBuffer command = new StringBuffer();
63       StructureMappingcommandSet smc;
64       ArrayList<String> str = new ArrayList<String>();
65
66       if (mapping == null || mapping.length < 1)
67         continue;
68
69       int lastPos = -1;
70       for (int s = 0; s < sequence[pdbfnum].length; s++)
71       {
72         for (int sp, m = 0; m < mapping.length; m++)
73         {
74           if (mapping[m].getSequence() == sequence[pdbfnum][s]
75                   && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1)
76           {
77             SequenceI asp = alignment.getSequenceAt(sp);
78             for (int r = 0; r < asp.getLength(); r++)
79             {
80               // no mapping to gaps in sequence
81               if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
82               {
83                 continue;
84               }
85               int pos = mapping[m].getPDBResNum(asp.findPosition(r));
86
87               if (pos < 1 || pos == lastPos)
88                 continue;
89
90               lastPos = pos;
91
92               Color col = sr.getResidueBoxColour(sequence[pdbfnum][s], r);
93
94               if (fr != null)
95                 col = fr.findFeatureColour(col, sequence[pdbfnum][s], r);
96               String newSelcom = (mapping[m].getChain() != " " ? ":"
97                       + mapping[m].getChain() : "")
98                       + "/"
99                       + (pdbfnum + 1)
100                       + ".1"
101                       + ";color["
102                       + col.getRed()
103                       + ","
104                       + col.getGreen()
105                       + ","
106                       + col.getBlue() + "]";
107               if (command.length() > newSelcom.length()
108                       && command.substring(
109                               command.length() - newSelcom.length())
110                               .equals(newSelcom))
111               {
112                 command = JmolCommands.condenseCommand(command, pos);
113                 continue;
114               }
115               // TODO: deal with case when buffer is too large for Jmol to parse
116               // - execute command and flush
117
118               command.append(";");
119               if (command.length() > 51200)
120               {
121                 // add another chunk
122                 str.add(command.toString());
123                 command.setLength(0);
124               }
125               command.append("select " + pos);
126               command.append(newSelcom);
127             }
128             break;
129           }
130         }
131       }
132       {
133         // add final chunk
134         str.add(command.toString());
135         command.setLength(0);
136       }
137       // Finally, add the command set ready to be returned.
138       cset.add(new StructureMappingcommandSet(JmolCommands.class,
139               files[pdbfnum], str.toArray(new String[str.size()])));
140
141     }
142     return cset.toArray(new StructureMappingcommandSet[cset.size()]);
143   }
144
145   public static StringBuffer condenseCommand(StringBuffer command, int pos)
146   {
147
148     // work back to last 'select'
149     int p = command.length(), q = p;
150     do
151     {
152       p -= 6;
153       if (p < 1)
154       {
155         p = 0;
156       }
157       ;
158     } while ((q = command.indexOf("select", p)) == -1 && p > 0);
159
160     StringBuffer sb = new StringBuffer(command.substring(0, q + 7));
161
162     command = command.delete(0, q + 7);
163
164     String start;
165
166     if (command.indexOf("-") > -1)
167     {
168       start = command.substring(0, command.indexOf("-"));
169     }
170     else
171     {
172       start = command.substring(0, command.indexOf(":"));
173     }
174
175     sb.append(start + "-" + pos + command.substring(command.indexOf(":")));
176
177     return sb;
178   }
179
180 }