JAL-3518 more pull up / test coverage of structure command generation
[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 setAttribute(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       sb.append("/").append(chain.trim()).append(":");
169       List<int[]> rangeList = atomSpec.getRanges(model, chain);
170
171       /*
172        * sort ranges into ascending start position order
173        */
174       Collections.sort(rangeList, IntRangeComparator.ASCENDING);
175
176       int start = rangeList.isEmpty() ? 0 : rangeList.get(0)[0];
177       int end = rangeList.isEmpty() ? 0 : rangeList.get(0)[1];
178
179       Iterator<int[]> iterator = rangeList.iterator();
180       while (iterator.hasNext())
181       {
182         int[] range = iterator.next();
183         if (range[0] <= end + 1)
184         {
185           /*
186            * range overlaps or is contiguous with the last one
187            * - so just extend the end position, and carry on
188            * (unless this is the last in the list)
189            */
190           end = Math.max(end, range[1]);
191         }
192         else
193         {
194           /*
195            * we have a break so append the last range
196            */
197           appendRange(sb, start, end, chain, firstPositionForChain, true);
198           start = range[0];
199           end = range[1];
200           firstPositionForChain = false;
201         }
202       }
203
204       /*
205        * and append the last range
206        */
207       if (!rangeList.isEmpty())
208       {
209         appendRange(sb, start, end, chain, firstPositionForChain, true);
210       }
211       firstPositionForChain = false;
212     }
213   }
214
215   @Override
216   public String showBackbone()
217   {
218     return "~display all;show @CA|P pbonds";
219   }
220
221   @Override
222   public String superposeStructures(AtomSpecModel spec, AtomSpecModel ref)
223   {
224     /*
225      * Form ChimeraX match command to match spec to ref
226      * 
227      * match #1/A:2-94 toAtoms #2/A:1-93
228      * 
229      * @see
230      * https://www.cgl.ucsf.edu/chimera/docs/UsersGuide/midas/match.html
231      */
232     StringBuilder cmd = new StringBuilder();
233     String atomSpec = getAtomSpec(spec, true);
234     String refSpec = getAtomSpec(ref, true);
235     cmd.append("align ").append(atomSpec).append(" toAtoms ")
236             .append(refSpec);
237
238     /*
239      * show superposed residues as ribbon, others as chain
240      */
241     cmd.append("; ribbon ");
242     cmd.append(getAtomSpec(spec, false)).append("|");
243     cmd.append(getAtomSpec(ref, false)).append("; view");
244
245     return cmd.toString();
246   }
247
248 }