JAL-1761 backbone type parameter for configuring which atoms are used for superpositi...
[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 java.awt.Color;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27
28 import jalview.structure.AtomSpecModel;
29 import jalview.structure.StructureCommand;
30 import jalview.structure.StructureCommandI;
31 import jalview.structure.StructureCommandsI.AtomSpecType;
32
33 /**
34  * Routines for generating ChimeraX commands for Jalview/ChimeraX binding
35  */
36 public class ChimeraXCommands extends ChimeraCommands
37 {
38   // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/info.html#resattr
39   private static final StructureCommand LIST_RESIDUE_ATTRIBUTES = new StructureCommand(
40           "info resattr");
41
42   // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/exit.html
43   private static final StructureCommand CLOSE_CHIMERAX = new StructureCommand(
44           "exit");
45
46   // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/info.html#notify
47   private static final StructureCommand STOP_NOTIFY_SELECTION = new StructureCommand(
48           "info notify stop selection jalview");
49
50   private static final StructureCommand STOP_NOTIFY_MODELS = new StructureCommand(
51           "info notify stop models jalview");
52
53   // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/info.html#selection
54   private static final StructureCommand GET_SELECTION = new StructureCommand(
55           "info selection level residue");
56
57   private static final StructureCommand SHOW_BACKBONE = new StructureCommand(
58           "~display all;~ribbon;show @CA|P atoms");
59
60   // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/view.html
61   private static final StructureCommand FOCUS_VIEW = new StructureCommand(
62           "view");
63
64   private static final StructureCommandI COLOUR_BY_CHARGE = new StructureCommand(
65           "color white;color :ASP,GLU red;color :LYS,ARG blue;color :CYS yellow");
66
67   @Override
68   public List<StructureCommandI> colourByCharge()
69   {
70     return Arrays.asList(COLOUR_BY_CHARGE);
71   }
72
73   @Override
74   public String getResidueSpec(String residue)
75   {
76     return ":" + residue;
77   }
78
79   @Override
80   public StructureCommandI colourResidues(String atomSpec, Color colour)
81   {
82     // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/color.html
83     String colourCode = getColourString(colour);
84
85     return new StructureCommand("color " + atomSpec + " " + colourCode);
86   }
87
88   @Override
89   public StructureCommandI focusView()
90   {
91     return FOCUS_VIEW;
92   }
93
94   /**
95    * {@inheritDoc}
96    * 
97    * @return
98    */
99   @Override
100   public int getModelStartNo()
101   {
102     return 1;
103   }
104
105   /**
106    * Returns a viewer command to set the given residue attribute value on residues
107    * specified by the AtomSpecModel, for example
108    * 
109    * <pre>
110    * setattr #0/A:3-9,14-20,39-43 res jv_strand 'strand' create true
111    * </pre>
112    * 
113    * @param attributeName
114    * @param attributeValue
115    * @param atomSpecModel
116    * @return
117    */
118   @Override
119   protected StructureCommandI setAttribute(String attributeName,
120           String attributeValue, AtomSpecModel atomSpecModel)
121   {
122     StringBuilder sb = new StringBuilder(128);
123     sb.append("setattr ").append(getAtomSpec(atomSpecModel, AtomSpecType.RESIDUE_ONLY));
124     sb.append(" res ").append(attributeName).append(" '")
125             .append(attributeValue).append("'");
126     sb.append(" create true");
127     return new StructureCommand(sb.toString());
128   }
129
130   @Override
131   public StructureCommandI openCommandFile(String path)
132   {
133     // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/open.html
134     return new StructureCommand("open " + path);
135   }
136
137   @Override
138   public StructureCommandI saveSession(String filepath)
139   {
140     // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/save.html
141     // note ChimeraX will append ".cxs" to the filepath!
142     return new StructureCommand("save " + filepath + " format session");
143   }
144
145   /**
146    * Returns the range(s) formatted as a ChimeraX atomspec, for example
147    * <p>
148    * #1/A:2-20,30-40/B:10-20|#2/A:12-30
149    * <p>
150    * Note there is no need to explicitly exclude ALTLOC atoms when
151    * {@code alphaOnly == true}, as this is the default behaviour of ChimeraX (a
152    * change from Chimera)
153    * 
154    * @return
155    */
156   @Override
157   public String getAtomSpec(AtomSpecModel atomSpec, AtomSpecType specType)
158   {
159     StringBuilder sb = new StringBuilder(128);
160     boolean firstModel = true;
161     for (String model : atomSpec.getModels())
162     {
163       if (!firstModel)
164       {
165         sb.append("|");
166       }
167       firstModel = false;
168       appendModel(sb, model, atomSpec);
169       if (specType == AtomSpecType.ALPHA)
170       {
171         sb.append("@CA");
172       }
173       if (specType == AtomSpecType.PHOSPHATE)
174       {
175         sb.append("@P");
176       }
177     }
178     return sb.toString();
179   }
180
181   /**
182    * A helper method to append an atomSpec string for atoms in the given model
183    * 
184    * @param sb
185    * @param model
186    * @param atomSpec
187    */
188   protected void appendModel(StringBuilder sb, String model,
189           AtomSpecModel atomSpec)
190   {
191     sb.append("#").append(model);
192
193     for (String chain : atomSpec.getChains(model))
194     {
195       boolean firstPositionForChain = true;
196       sb.append("/").append(chain.trim()).append(":");
197       List<int[]> rangeList = atomSpec.getRanges(model, chain);
198       boolean first = true;
199       for (int[] range : rangeList)
200       {
201         if (!first)
202         {
203           sb.append(",");
204         }
205         first = false;
206         appendRange(sb, range[0], range[1], chain, firstPositionForChain,
207                 true);
208       }
209     }
210   }
211
212   @Override
213   public List<StructureCommandI> showBackbone()
214   {
215     return Arrays.asList(SHOW_BACKBONE);
216   }
217
218   @Override
219   public List<StructureCommandI> superposeStructures(AtomSpecModel ref,
220           AtomSpecModel spec, AtomSpecType backbone)
221   {
222     /*
223      * Form ChimeraX match command to match spec to ref
224      * 
225      * match #1/A:2-94 toAtoms #2/A:1-93
226      * 
227      * @see https://www.cgl.ucsf.edu/chimerax/docs/user/commands/align.html
228      */
229     StringBuilder cmd = new StringBuilder();
230     String atomSpec = getAtomSpec(spec, backbone);
231     String refSpec = getAtomSpec(ref, backbone);
232     cmd.append("align ").append(atomSpec).append(" toAtoms ")
233             .append(refSpec);
234
235     /*
236      * show superposed residues as ribbon, others as chain
237      */
238     cmd.append("; ribbon ");
239     cmd.append(getAtomSpec(spec, AtomSpecType.RESIDUE_ONLY)).append("|");
240     cmd.append(getAtomSpec(ref, AtomSpecType.RESIDUE_ONLY)).append("; view");
241
242     return Arrays.asList(new StructureCommand(cmd.toString()));
243   }
244
245   @Override
246   public StructureCommandI openSession(String filepath)
247   {
248     // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/open.html#composite
249     // this version of the command has no dependency on file extension
250     return new StructureCommand("open " + filepath + " format session");
251   }
252
253   @Override
254   public StructureCommandI closeViewer()
255   {
256     return CLOSE_CHIMERAX;
257   }
258
259   @Override
260   public List<StructureCommandI> startNotifications(String uri)
261   {
262     List<StructureCommandI> cmds = new ArrayList<>();
263     cmds.add(new StructureCommand(
264             "info notify start models jalview prefix ModelChanged url "
265                     + uri));
266     cmds.add(new StructureCommand(
267             "info notify start selection jalview prefix SelectionChanged url "
268                     + uri));
269     return cmds;
270   }
271
272   @Override
273   public List<StructureCommandI> stopNotifications()
274   {
275     List<StructureCommandI> cmds = new ArrayList<>();
276     cmds.add(STOP_NOTIFY_MODELS);
277     cmds.add(STOP_NOTIFY_SELECTION);
278     return cmds;
279   }
280
281   @Override
282   public StructureCommandI getSelectedResidues()
283   {
284     return GET_SELECTION;
285   }
286
287   @Override
288   public StructureCommandI listResidueAttributes()
289   {
290     return LIST_RESIDUE_ATTRIBUTES;
291   }
292 }