dee6e49cbf151ce878ac82d690741b7ebfd98663
[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.Arrays;
25 import java.util.List;
26
27 import jalview.structure.AtomSpecModel;
28 import jalview.structure.StructureCommand;
29 import jalview.structure.StructureCommandI;
30
31 /**
32  * Routines for generating ChimeraX commands for Jalview/ChimeraX binding
33  */
34 public class ChimeraXCommands extends ChimeraCommands
35 {
36   private static final StructureCommand SHOW_BACKBONE = new StructureCommand(
37           "~display all;~ribbon;show @CA|P atoms");
38
39   private static final StructureCommand FOCUS_VIEW = new StructureCommand(
40           "view");
41
42   private static final StructureCommandI COLOUR_BY_CHARGE = new StructureCommand(
43           "color white;color :ASP,GLU red;color :LYS,ARG blue;color :CYS yellow");
44
45   @Override
46   public List<StructureCommandI> colourByCharge()
47   {
48     return Arrays.asList(COLOUR_BY_CHARGE);
49   }
50
51   @Override
52   public String getResidueSpec(String residue)
53   {
54     return ":" + residue;
55   }
56
57   @Override
58   public StructureCommandI colourResidues(String atomSpec, Color colour)
59   {
60     // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/color.html
61     String colourCode = getColourString(colour);
62
63     return new StructureCommand("color " + atomSpec + " " + colourCode);
64   }
65
66   @Override
67   public StructureCommandI focusView()
68   {
69     // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/view.html
70     return FOCUS_VIEW;
71   }
72
73   /**
74    * Returns a viewer command to set the given residue attribute value on
75    * residues specified by the AtomSpecModel, for example
76    * 
77    * <pre>
78    * setattr #0/A:3-9,14-20,39-43 res jv_strand 'strand' create true
79    * </pre>
80    * 
81    * @param attributeName
82    * @param attributeValue
83    * @param atomSpecModel
84    * @return
85    */
86   @Override
87   protected StructureCommandI setAttribute(String attributeName,
88           String attributeValue, AtomSpecModel atomSpecModel)
89   {
90     StringBuilder sb = new StringBuilder(128);
91     sb.append("setattr ").append(getAtomSpec(atomSpecModel, false));
92     sb.append(" res ").append(attributeName).append(" '")
93             .append(attributeValue).append("'");
94     sb.append(" create true");
95     return new StructureCommand(sb.toString());
96   }
97
98   @Override
99   public StructureCommandI openCommandFile(String path)
100   {
101     // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/open.html
102     return new StructureCommand("open " + path);
103   }
104
105   @Override
106   public StructureCommandI saveSession(String filepath)
107   {
108     // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/save.html
109     // note ChimeraX will append ".cxs" to the filepath!
110     return new StructureCommand("save " + filepath + " format session");
111   }
112
113   /**
114    * Returns the range(s) formatted as a ChimeraX atomspec, for example
115    * <p>
116    * #1/A:2-20,30-40/B:10-20|#2/A:12-30
117    * 
118    * @return
119    */
120   @Override
121   public String getAtomSpec(AtomSpecModel atomSpec, boolean alphaOnly)
122   {
123     StringBuilder sb = new StringBuilder(128);
124     boolean firstModel = true;
125     for (String model : atomSpec.getModels())
126     {
127       if (!firstModel)
128       {
129         sb.append("|");
130       }
131       firstModel = false;
132       appendModel(sb, model, atomSpec);
133       if (alphaOnly)
134       {
135         // TODO @P if RNA - add nucleotide flag to AtomSpecModel?
136         sb.append("@CA");
137       }
138       // todo: is there ChimeraX syntax to exclude altlocs?
139     }
140     return sb.toString();
141   }
142
143   /**
144    * A helper method to append an atomSpec string for atoms in the given model
145    * 
146    * @param sb
147    * @param model
148    * @param atomSpec
149    */
150   protected void appendModel(StringBuilder sb, String model,
151           AtomSpecModel atomSpec)
152   {
153     sb.append("#").append(model);
154
155     for (String chain : atomSpec.getChains(model))
156     {
157       boolean firstPositionForChain = true;
158       sb.append("/").append(chain.trim()).append(":");
159       List<int[]> rangeList = atomSpec.getRanges(model, chain);
160       boolean first = true;
161       for (int[] range : rangeList)
162       {
163         if (!first)
164         {
165           sb.append(",");
166         }
167         first = false;
168         appendRange(sb, range[0], range[1], chain, firstPositionForChain,
169                 true);
170       }
171     }
172   }
173
174   @Override
175   public List<StructureCommandI> showBackbone()
176   {
177     return Arrays.asList(SHOW_BACKBONE);
178   }
179
180   @Override
181   public List<StructureCommandI> superposeStructures(AtomSpecModel ref,
182           AtomSpecModel spec)
183   {
184     /*
185      * Form ChimeraX match command to match spec to ref
186      * 
187      * match #1/A:2-94 toAtoms #2/A:1-93
188      * 
189      * @see https://www.cgl.ucsf.edu/chimerax/docs/user/commands/align.html
190      */
191     StringBuilder cmd = new StringBuilder();
192     String atomSpec = getAtomSpec(spec, true);
193     String refSpec = getAtomSpec(ref, true);
194     cmd.append("align ").append(atomSpec).append(" toAtoms ")
195             .append(refSpec);
196
197     /*
198      * show superposed residues as ribbon, others as chain
199      */
200     cmd.append("; ribbon ");
201     cmd.append(getAtomSpec(spec, false)).append("|");
202     cmd.append(getAtomSpec(ref, false)).append("; view");
203
204     return Arrays.asList(new StructureCommand(cmd.toString()));
205   }
206
207   @Override
208   public StructureCommandI openSession(String filepath)
209   {
210     // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/open.html#composite
211     // this version of the command has no dependency on file extension
212     return new StructureCommand("open " + filepath + " format session");
213   }
214
215   @Override
216   public StructureCommandI hideChain(String modelId, String chainId)
217   {
218     String cmd = "~ribbon #" + modelId + "/" + chainId;
219     return new StructureCommand(cmd);
220   }
221 }