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