JAL-3518 correct override for ChimeraX colour by residues
[jalview.git] / src / jalview / ext / rbvi / chimera / ChimeraXCommands.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.rbvi.chimera;
22
23 import jalview.structure.AtomSpecModel;
24 import jalview.util.ColorUtils;
25 import jalview.util.IntRangeComparator;
26
27 import java.awt.Color;
28 import java.util.Collections;
29 import java.util.Iterator;
30 import java.util.List;
31
32 /**
33  * Routines for generating ChimeraX commands for Jalview/ChimeraX binding
34  */
35 public class ChimeraXCommands extends ChimeraCommands
36 {
37   private static final String CMD_COLOUR_BY_CHARGE = "color white;color :ASP,GLU red;color :LYS,ARG blue;color :CYS yellow";
38
39   @Override
40   public String colourByCharge()
41   {
42     return CMD_COLOUR_BY_CHARGE;
43   }
44
45   @Override
46   public String getResidueSpec(String residue)
47   {
48     return ":" + residue;
49   }
50
51   @Override
52   public String setBackgroundColour(Color col)
53   {
54     // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/set.html
55     return "set bgColor " + ColorUtils.toTkCode(col);
56   }
57
58   @Override
59   public String getColourCommand(String atomSpec, Color colour)
60   {
61     // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/color.html
62     String colourCode = getColourString(colour);
63
64     return "color " + atomSpec + " " + colourCode;
65   }
66
67   @Override
68   public String focusView()
69   {
70     // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/view.html
71     return "view";
72   }
73
74   /**
75    * {@inheritDoc}
76    * 
77    * @return
78    */
79   @Override
80   public int getModelStartNo()
81   {
82     return 1;
83   }
84
85   /**
86    * Returns a viewer command to set the given residue attribute value on
87    * residues specified by the AtomSpecModel, for example
88    * 
89    * <pre>
90    * setattr #0/A:3-9,14-20,39-43 res jv_strand 'strand' create true
91    * </pre>
92    * 
93    * @param attributeName
94    * @param attributeValue
95    * @param atomSpecModel
96    * @return
97    */
98   @Override
99   protected String getSetAttributeCommand(String attributeName,
100           String attributeValue, AtomSpecModel atomSpecModel)
101   {
102     StringBuilder sb = new StringBuilder(128);
103     sb.append("setattr ").append(getAtomSpec(atomSpecModel, false));
104     sb.append(" res ").append(attributeName).append(" '")
105             .append(attributeValue).append("'");
106     sb.append(" create true");
107     return sb.toString();
108   }
109
110   @Override
111   public String openCommandFile(String path)
112   {
113     // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/open.html
114     return "open " + path;
115   }
116
117   @Override
118   public String saveSession(String filepath)
119   {
120     // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/save.html
121     return "save session " + filepath;
122   }
123
124   /**
125    * Returns the range(s) formatted as a ChimeraX atomspec, for example
126    * <p>
127    * #1/A:2-20,30-40/B:10-20|#2/A:12-30
128    * 
129    * @return
130    */
131   @Override
132   public String getAtomSpec(AtomSpecModel atomSpec, boolean alphaOnly)
133   {
134     StringBuilder sb = new StringBuilder(128);
135     boolean firstModel = true;
136     for (Integer model : atomSpec.getModels())
137     {
138       if (!firstModel)
139       {
140         sb.append("|");
141       }
142       firstModel = false;
143       appendModel(sb, model, atomSpec);
144       if (alphaOnly)
145       {
146         sb.append("@CA|P");
147       }
148       // todo: is there ChimeraX syntax to exclude altlocs?
149     }
150     return sb.toString();
151   }
152
153   /**
154    * A helper method to append an atomSpec string for atoms in the given model
155    * 
156    * @param sb
157    * @param model
158    * @param atomSpec
159    */
160   protected void appendModel(StringBuilder sb, Integer model,
161           AtomSpecModel atomSpec)
162   {
163     sb.append("#").append(model);
164
165     for (String chain : atomSpec.getChains(model))
166     {
167       boolean firstPositionForChain = true;
168       chain = " ".equals(chain) ? chain : chain.trim();
169       sb.append("/").append(chain).append(":");
170       List<int[]> rangeList = atomSpec.getRanges(model, chain);
171
172       /*
173        * sort ranges into ascending start position order
174        */
175       Collections.sort(rangeList, IntRangeComparator.ASCENDING);
176
177       int start = rangeList.isEmpty() ? 0 : rangeList.get(0)[0];
178       int end = rangeList.isEmpty() ? 0 : rangeList.get(0)[1];
179
180       Iterator<int[]> iterator = rangeList.iterator();
181       while (iterator.hasNext())
182       {
183         int[] range = iterator.next();
184         if (range[0] <= end + 1)
185         {
186           /*
187            * range overlaps or is contiguous with the last one
188            * - so just extend the end position, and carry on
189            * (unless this is the last in the list)
190            */
191           end = Math.max(end, range[1]);
192         }
193         else
194         {
195           /*
196            * we have a break so append the last range
197            */
198           appendRange(sb, start, end, chain, firstPositionForChain, true);
199           start = range[0];
200           end = range[1];
201           firstPositionForChain = false;
202         }
203       }
204
205       /*
206        * and append the last range
207        */
208       if (!rangeList.isEmpty())
209       {
210         appendRange(sb, start, end, chain, firstPositionForChain, true);
211       }
212       firstPositionForChain = false;
213     }
214   }
215
216   @Override
217   public String showBackbone()
218   {
219     return "~display all;show @CA|P pbonds";
220   }
221
222   @Override
223   public String superposeStructures(AtomSpecModel spec, AtomSpecModel ref)
224   {
225     /*
226      * Form ChimeraX match command to match spec to ref
227      * 
228      * match #1/A:2-94 toAtoms #2/A:1-93
229      * 
230      * @see
231      * https://www.cgl.ucsf.edu/chimera/docs/UsersGuide/midas/match.html
232      */
233     StringBuilder cmd = new StringBuilder();
234     String atomSpec = getAtomSpec(spec, true);
235     String refSpec = getAtomSpec(ref, true);
236     cmd.append("align ").append(atomSpec).append(" toAtoms ")
237             .append(refSpec);
238
239     /*
240      * show superposed residues as ribbon, others as chain
241      */
242     cmd.append("; ribbon ");
243     cmd.append(getAtomSpec(spec, false)).append("|");
244     cmd.append(getAtomSpec(ref, false)).append("; view");
245
246     return cmd.toString();
247   }
248
249 }