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