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