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