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