2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.ext.rbvi.chimera;
23 import jalview.api.ColorI;
24 import jalview.api.FeatureRenderer;
25 import jalview.api.SequenceRenderer;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.SequenceI;
28 import jalview.structure.StructureMapping;
29 import jalview.structure.StructureMappingcommandSet;
30 import jalview.structure.StructureSelectionManager;
31 import jalview.util.ColorUtils;
32 import jalview.util.Comparison;
34 import java.util.ArrayList;
35 import java.util.LinkedHashMap;
36 import java.util.List;
38 import java.util.TreeMap;
41 * Routines for generating Chimera commands for Jalview/Chimera binding
46 public class ChimeraCommands
50 * utility to construct the commands to colour chains by the given alignment
51 * for passing to Chimera
53 * @returns Object[] { Object[] { <model being coloured>,
56 public static StructureMappingcommandSet[] getColourBySequenceCommand(
57 StructureSelectionManager ssm, String[] files,
58 SequenceI[][] sequence, SequenceRenderer sr, FeatureRenderer fr,
61 Map<ColorI, Map<Integer, Map<String, List<int[]>>>> colourMap = buildColoursMap(
62 ssm, files, sequence, sr, fr, alignment);
64 List<String> colourCommands = buildColourCommands(colourMap);
66 StructureMappingcommandSet cs = new StructureMappingcommandSet(
67 ChimeraCommands.class, null,
68 colourCommands.toArray(new String[0]));
70 return new StructureMappingcommandSet[] { cs };
74 * Traverse the map of colours/models/chains/positions to construct a list of
75 * 'color' commands (one per distinct colour used). The format of each command
78 * <blockquote> color colorname #modelnumber:range.chain e.g. color #00ff00
79 * #0:2.B,4.B,9-12.B|#1:1.A,2-6.A,...
82 * ://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/frameatom_spec
88 protected static List<String> buildColourCommands(
89 Map<ColorI, Map<Integer, Map<String, List<int[]>>>> colourMap)
92 * This version concatenates all commands into a single String (semi-colon
93 * delimited). If length limit issues arise, refactor to return one color
96 List<String> commands = new ArrayList<String>();
97 StringBuilder sb = new StringBuilder(256);
98 boolean firstColour = true;
99 for (ColorI colour : colourMap.keySet())
101 String colourCode = ColorUtils.toTkCode(colour);
106 sb.append("color ").append(colourCode).append(" ");
108 boolean firstModelForColour = true;
109 final Map<Integer, Map<String, List<int[]>>> colourData = colourMap
111 for (Integer model : colourData.keySet())
113 boolean firstPositionForModel = true;
114 if (!firstModelForColour)
118 firstModelForColour = false;
119 sb.append("#").append(model).append(":");
121 final Map<String, List<int[]>> modelData = colourData.get(model);
122 for (String chain : modelData.keySet())
124 boolean hasChain = !"".equals(chain.trim());
125 for (int[] range : modelData.get(chain))
127 if (!firstPositionForModel)
131 if (range[0] == range[1])
137 sb.append(range[0]).append("-").append(range[1]);
141 sb.append(".").append(chain);
143 firstPositionForModel = false;
148 commands.add(sb.toString());
154 * Build a data structure which maps contiguous subsequences for each colour.
155 * This generates a data structure from which we can easily generate the
156 * Chimera command for colour by sequence.
160 * list of start/end ranges
161 * Ordering is by order of addition (for colours and positions), natural ordering (for models and chains)
164 protected static Map<ColorI, Map<Integer, Map<String, List<int[]>>>> buildColoursMap(
165 StructureSelectionManager ssm, String[] files,
166 SequenceI[][] sequence, SequenceRenderer sr, FeatureRenderer fr,
167 AlignmentI alignment)
169 Map<ColorI, Map<Integer, Map<String, List<int[]>>>> colourMap = new LinkedHashMap<ColorI, Map<Integer, Map<String, List<int[]>>>>();
170 ColorI lastColour = null;
171 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
173 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
175 if (mapping == null || mapping.length < 1)
180 int startPos = -1, lastPos = -1;
181 String lastChain = "";
182 for (int s = 0; s < sequence[pdbfnum].length; s++)
184 for (int sp, m = 0; m < mapping.length; m++)
186 final SequenceI seq = sequence[pdbfnum][s];
187 if (mapping[m].getSequence() == seq
188 && (sp = alignment.findIndex(seq)) > -1)
190 SequenceI asp = alignment.getSequenceAt(sp);
191 for (int r = 0; r < asp.getLength(); r++)
193 // no mapping to gaps in sequence
194 if (Comparison.isGap(asp.getCharAt(r)))
198 int pos = mapping[m].getPDBResNum(asp.findPosition(r));
200 if (pos < 1 || pos == lastPos)
205 ColorI colour = sr.getResidueColour(seq, r, fr);
206 final String chain = mapping[m].getChain();
209 * Just keep incrementing the end position for this colour range
210 * _unless_ colour, PDB model or chain has changed, or there is a
211 * gap in the mapped residue sequence
213 final boolean newColour = !colour.equals(lastColour);
214 final boolean nonContig = lastPos + 1 != pos;
215 final boolean newChain = !chain.equals(lastChain);
216 if (newColour || nonContig || newChain)
220 addColourRange(colourMap, lastColour, pdbfnum, startPos,
229 // final colour range
230 if (lastColour != null)
232 addColourRange(colourMap, lastColour, pdbfnum, startPos,
244 * Helper method to add one contiguous colour range to the colour map.
253 protected static void addColourRange(
254 Map<ColorI, Map<Integer, Map<String, List<int[]>>>> colourMap,
255 ColorI colour, int model, int startPos, int endPos, String chain)
258 * Get/initialize map of data for the colour
260 Map<Integer, Map<String, List<int[]>>> colourData = colourMap
262 if (colourData == null)
266 colourData = new TreeMap<Integer, Map<String, List<int[]>>>());
270 * Get/initialize map of data for the colour and model
272 Map<String, List<int[]>> modelData = colourData.get(model);
273 if (modelData == null)
275 colourData.put(model, modelData = new TreeMap<String, List<int[]>>());
279 * Get/initialize map of data for colour, model and chain
281 List<int[]> chainData = modelData.get(chain);
282 if (chainData == null)
284 modelData.put(chain, chainData = new ArrayList<int[]>());
288 * Add the start/end positions
290 chainData.add(new int[] { startPos, endPos });