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