Clover icon

Coverage Report

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

File StructureCommandsBase.java

 

Coverage histogram

../../img/srcFileCovDistChart8.png
20% of files have more coverage

Code metrics

16
49
15
1
257
147
23
0.47
3.27
15
1.53

Classes

Class Line # Actions
StructureCommandsBase 16 49 23
0.77577.5%
 

Contributing tests

This file is covered by 26 tests. .

Source view

1    package jalview.structure;
2   
3    import java.awt.Color;
4    import java.util.ArrayList;
5    import java.util.List;
6    import java.util.Map;
7    import java.util.Map.Entry;
8   
9    /**
10    * A base class holding methods useful to all classes that implement commands
11    * for structure viewers
12    *
13    * @author gmcarstairs
14    *
15    */
 
16    public abstract class StructureCommandsBase implements StructureCommandsI
17    {
18    public static final String NAMESPACE_PREFIX = "jv_";
19   
20    private static final String CMD_SEPARATOR = ";";
21   
22    /**
23    * Returns something that separates concatenated commands
24    *
25    * @return
26    */
 
27  19 toggle protected static String getCommandSeparator()
28    {
29  19 return CMD_SEPARATOR;
30    }
31   
32    /**
33    * Returns the lowest model number used by the structure viewer
34    *
35    * @return
36    */
 
37  2 toggle @Override
38    public int getModelStartNo()
39    {
40  2 return 0;
41    }
42   
43    /**
44    * Helper method to add one contiguous range to the AtomSpec model for the given
45    * value (creating the model if necessary). As used by Jalview, {@code value} is
46    * <ul>
47    * <li>a colour, when building a 'colour structure by sequence' command</li>
48    * <li>a feature value, when building a 'set Chimera attributes from features'
49    * command</li>
50    * </ul>
51    *
52    * @param map
53    * @param value
54    * @param model
55    * @param startPos
56    * @param endPos
57    * @param chain
58    */
 
59  57 toggle public static final void addAtomSpecRange(Map<Object, AtomSpecModel> map,
60    Object value, String model, int startPos, int endPos,
61    String chain)
62    {
63    /*
64    * Get/initialize map of data for the colour
65    */
66  57 AtomSpecModel atomSpec = map.get(value);
67  57 if (atomSpec == null)
68    {
69  21 atomSpec = new AtomSpecModel();
70  21 map.put(value, atomSpec);
71    }
72   
73  57 atomSpec.addRange(model, startPos, endPos, chain);
74    }
75   
76    /**
77    * Makes a structure viewer attribute name for a Jalview feature type by
78    * prefixing it with "jv_", and replacing any non-alphanumeric characters with
79    * an underscore
80    *
81    * @param featureType
82    * @return
83    */
 
84  21 toggle protected String makeAttributeName(String featureType)
85    {
86  21 StringBuilder sb = new StringBuilder();
87  21 if (featureType != null)
88    {
89  20 for (char c : featureType.toCharArray())
90    {
91  171 sb.append(Character.isLetterOrDigit(c) ? c : '_');
92    }
93    }
94  21 String attName = NAMESPACE_PREFIX + sb.toString();
95  21 return attName;
96    }
97   
98    /**
99    * Traverse the map of colours/models/chains/positions to construct a list of
100    * 'color' commands (one per distinct colour used). The format of each command
101    * is specific to the structure viewer.
102    * <p>
103    * The default implementation returns a single command containing one command
104    * per colour, concatenated.
105    *
106    * @param colourMap
107    * @return
108    */
 
109  11 toggle @Override
110    public List<StructureCommandI> colourBySequence(
111    Map<Object, AtomSpecModel> colourMap)
112    {
113  11 List<StructureCommandI> commands = new ArrayList<>();
114  11 StringBuilder sb = new StringBuilder(colourMap.size() * 20);
115  11 boolean first = true;
116  11 for (Object key : colourMap.keySet())
117    {
118  11 Color colour = (Color) key;
119  11 final AtomSpecModel colourData = colourMap.get(colour);
120  11 StructureCommandI command = getColourCommand(colourData, colour);
121  11 if (!first)
122    {
123  2 sb.append(getCommandSeparator());
124    }
125  11 first = false;
126  11 sb.append(command.getCommand());
127    }
128   
129  11 commands.add(new StructureCommand(sb.toString()));
130  11 return commands;
131    }
132   
133    /**
134    * Returns a command to colour the atoms represented by {@code atomSpecModel}
135    * with the colour specified by {@code colourCode}.
136    *
137    * @param atomSpecModel
138    * @param colour
139    * @return
140    */
 
141  20 toggle protected StructureCommandI getColourCommand(AtomSpecModel atomSpecModel,
142    Color colour)
143    {
144  20 String atomSpec = getAtomSpec(atomSpecModel, false);
145  20 return colourResidues(atomSpec, colour);
146    }
147   
148    /**
149    * Returns a command to colour the atoms described (in viewer command syntax)
150    * by {@code atomSpec} with the colour specified by {@code colourCode}
151    *
152    * @param atomSpec
153    * @param colour
154    * @return
155    */
156    protected abstract StructureCommandI colourResidues(String atomSpec,
157    Color colour);
158   
 
159  0 toggle @Override
160    public List<StructureCommandI> colourByResidues(
161    Map<String, Color> colours)
162    {
163  0 List<StructureCommandI> commands = new ArrayList<>();
164  0 for (Entry<String, Color> entry : colours.entrySet())
165    {
166  0 commands.add(colourResidue(entry.getKey(), entry.getValue()));
167    }
168  0 return commands;
169    }
170   
 
171  0 toggle private StructureCommandI colourResidue(String resName, Color col)
172    {
173  0 String atomSpec = getResidueSpec(resName);
174  0 return colourResidues(atomSpec, col);
175    }
176   
177    /**
178    * Helper method to append one start-end range to an atomspec string
179    *
180    * @param sb
181    * @param start
182    * @param end
183    * @param chain
184    * @param firstPositionForModel
185    */
 
186  230 toggle protected void appendRange(StringBuilder sb, int start, int end,
187    String chain, boolean firstPositionForModel, boolean isChimeraX)
188    {
189  230 if (!firstPositionForModel)
190    {
191  59 sb.append(",");
192    }
193  230 if (end == start)
194    {
195  54 sb.append(start);
196    }
197    else
198    {
199  176 sb.append(start).append("-").append(end);
200    }
201   
202  230 if (!isChimeraX)
203    {
204  119 sb.append(".");
205  119 if (!" ".equals(chain))
206    {
207  117 sb.append(chain);
208    }
209    }
210    }
211   
212    /**
213    * Returns the atom specifier meaning all occurrences of the given residue
214    *
215    * @param residue
216    * @return
217    */
218    protected abstract String getResidueSpec(String residue);
219   
 
220  0 toggle @Override
221    public List<StructureCommandI> setAttributes(
222    Map<String, Map<Object, AtomSpecModel>> featureValues)
223    {
224    // default does nothing, override where this is implemented
225  0 return null;
226    }
227   
 
228  0 toggle @Override
229    public List<StructureCommandI> startNotifications(String uri)
230    {
231  0 return null;
232    }
233   
 
234  7 toggle @Override
235    public List<StructureCommandI> stopNotifications()
236    {
237  7 return null;
238    }
239   
 
240  0 toggle @Override
241    public StructureCommandI getSelectedResidues()
242    {
243  0 return null;
244    }
245   
 
246  0 toggle @Override
247    public StructureCommandI listResidueAttributes()
248    {
249  0 return null;
250    }
251   
 
252  0 toggle @Override
253    public StructureCommandI getResidueAttributes(String attName)
254    {
255  0 return null;
256    }
257    }