Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.ext.rbvi.chimera

File ChimeraXCommands.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

6
55
18
1
278
168
21
0.38
3.06
18
1.17

Classes

Class Line # Actions
ChimeraXCommands 36 55 21
1.0100%
 

Contributing tests

This file is covered by 20 tests. .

Source view

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