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.AlignmentViewPanel;
24 import jalview.api.SequenceRenderer;
25 import jalview.api.structures.JalviewStructureDisplayI;
26 import jalview.bin.Cache;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.HiddenColumns;
29 import jalview.datamodel.PDBEntry;
30 import jalview.datamodel.SearchResultMatchI;
31 import jalview.datamodel.SearchResultsI;
32 import jalview.datamodel.SequenceFeature;
33 import jalview.datamodel.SequenceI;
34 import jalview.httpserver.AbstractRequestHandler;
35 import jalview.io.DataSourceType;
36 import jalview.schemes.ColourSchemeI;
37 import jalview.schemes.ResidueProperties;
38 import jalview.structure.AtomSpec;
39 import jalview.structure.StructureMappingcommandSet;
40 import jalview.structure.StructureSelectionManager;
41 import jalview.structures.models.AAStructureBindingModel;
42 import jalview.util.MessageManager;
44 import java.awt.Color;
46 import java.io.FileOutputStream;
47 import java.io.IOException;
48 import java.io.PrintWriter;
49 import java.net.BindException;
50 import java.util.ArrayList;
51 import java.util.BitSet;
52 import java.util.Collections;
53 import java.util.Hashtable;
54 import java.util.LinkedHashMap;
55 import java.util.List;
58 import ext.edu.ucsf.rbvi.strucviz2.ChimeraManager;
59 import ext.edu.ucsf.rbvi.strucviz2.ChimeraModel;
60 import ext.edu.ucsf.rbvi.strucviz2.StructureManager;
61 import ext.edu.ucsf.rbvi.strucviz2.StructureManager.ModelType;
63 public abstract class JalviewChimeraBinding extends AAStructureBindingModel
65 public static final String CHIMERA_FEATURE_GROUP = "Chimera";
67 // Chimera clause to exclude alternate locations in atom selection
68 private static final String NO_ALTLOCS = "&~@.B-Z&~@.2-9";
70 private static final String COLOURING_CHIMERA = MessageManager
71 .getString("status.colouring_chimera");
73 private static final boolean debug = false;
75 private static final String PHOSPHORUS = "P";
77 private static final String ALPHACARBON = "CA";
79 private List<String> chainNames = new ArrayList<>();
81 private Hashtable<String, String> chainFile = new Hashtable<>();
84 * Object through which we talk to Chimera
86 private ChimeraManager viewer;
89 * Object which listens to Chimera notifications
91 private AbstractRequestHandler chimeraListener;
94 * set if chimera state is being restored from some source - instructs binding
95 * not to apply default display style when structure set is updated for first
98 private boolean loadingFromArchive = false;
101 * flag to indicate if the Chimera viewer should ignore sequence colouring
102 * events from the structure manager because the GUI is still setting up
104 private boolean loadingFinished = true;
107 * Map of ChimeraModel objects keyed by PDB full local file name
109 private Map<String, List<ChimeraModel>> chimeraMaps = new LinkedHashMap<>();
111 String lastHighlightCommand;
114 * incremented every time a load notification is successfully handled -
115 * lightweight mechanism for other threads to detect when they can start
116 * referring to new structures.
118 private long loadNotifiesHandled = 0;
120 private Thread chimeraMonitor;
123 * Open a PDB structure file in Chimera and set up mappings from Jalview.
125 * We check if the PDB model id is already loaded in Chimera, if so don't reopen
126 * it. This is the case if Chimera has opened a saved session file.
131 public boolean openFile(PDBEntry pe)
133 String file = pe.getFile();
136 List<ChimeraModel> modelsToMap = new ArrayList<>();
137 List<ChimeraModel> oldList = viewer.isChimeraX() ? new ArrayList<>()
138 : viewer.getModelList();
139 boolean alreadyOpen = false;
142 * If Chimera already has this model, don't reopen it, but do remap it.
144 for (ChimeraModel open : oldList)
146 if (open.getModelName().equals(pe.getId()))
149 modelsToMap.add(open);
154 * If Chimera doesn't yet have this model, ask it to open it, and retrieve
155 * the model name(s) added by Chimera.
159 viewer.openModel(file, pe.getId(), ModelType.PDB_MODEL);
160 if (viewer.isChimeraX())
163 * ChimeraX hack: force chimera model name to pdbId
165 int modelNumber = chimeraMaps.size() + 1;
166 String command = "setattr #" + modelNumber + " models name "
168 sendChimeraCommand(command, false);
169 modelsToMap.add(new ChimeraModel(pe.getId(), ModelType.PDB_MODEL,
175 * Chimera: query for actual models and find the one with
176 * matching model name - set in viewer.openModel()
178 List<ChimeraModel> newList = viewer.getModelList();
179 // JAL-1728 newList.removeAll(oldList) does not work
180 for (ChimeraModel cm : newList)
182 if (cm.getModelName().equals(pe.getId()))
190 chimeraMaps.put(file, modelsToMap);
192 if (getSsm() != null)
194 getSsm().addStructureViewerListener(this);
197 } catch (Exception q)
199 log("Exception when trying to open model " + file + "\n"
214 public JalviewChimeraBinding(StructureSelectionManager ssm,
215 PDBEntry[] pdbentry, SequenceI[][] sequenceIs,
216 DataSourceType protocol)
218 super(ssm, pdbentry, sequenceIs, protocol);
219 viewer = new ChimeraManager(new StructureManager(true));
223 * Starts a thread that waits for the Chimera process to finish, so that we can
224 * then close the associated resources. This avoids leaving orphaned Chimera
225 * viewer panels in Jalview if the user closes Chimera.
227 protected void startChimeraProcessMonitor()
229 final Process p = viewer.getChimeraProcess();
230 chimeraMonitor = new Thread(new Runnable()
239 JalviewStructureDisplayI display = getViewer();
242 display.closeViewer(false);
244 } catch (InterruptedException e)
246 // exit thread if Chimera Viewer is closed in Jalview
250 chimeraMonitor.start();
254 * Start a dedicated HttpServer to listen for Chimera notifications, and tell it
257 public void startChimeraListener()
261 chimeraListener = new ChimeraListener(this);
262 viewer.startListening(chimeraListener.getUri());
263 } catch (BindException e)
266 "Failed to start Chimera listener: " + e.getMessage());
271 * Tells Chimera to display only the specified chains
275 public void showChains(List<String> toshow)
278 * Construct a chimera command like
280 * ~display #*;~ribbon #*;ribbon :.A,:.B
282 StringBuilder cmd = new StringBuilder(64);
283 boolean first = true;
284 for (String chain : toshow)
286 int modelNumber = getModelNoForChain(chain);
287 String showChainCmd = modelNumber == -1 ? ""
288 : modelNumber + ":." + chain.split(":")[1];
293 cmd.append(showChainCmd);
298 * could append ";focus" to this command to resize the display to fill the
299 * window, but it looks more helpful not to (easier to relate chains to the
302 final String command = "~display #*; ~ribbon #*; ribbon :"
304 sendChimeraCommand(command, false);
308 * Close down the Jalview viewer and listener, and (optionally) the associated
311 public void closeViewer(boolean closeChimera)
313 getSsm().removeStructureViewerListener(this, this.getStructureFiles());
316 viewer.exitChimera();
318 if (this.chimeraListener != null)
320 chimeraListener.shutdown();
321 chimeraListener = null;
325 if (chimeraMonitor != null)
327 chimeraMonitor.interrupt();
329 releaseUIResources();
333 public void colourByChain()
335 colourBySequence = false;
336 sendAsynchronousCommand("rainbow chain", COLOURING_CHIMERA);
340 * Constructs and sends a Chimera command to colour by charge
342 * <li>Aspartic acid and Glutamic acid (negative charge) red</li>
343 * <li>Lysine and Arginine (positive charge) blue</li>
344 * <li>Cysteine - yellow</li>
345 * <li>all others - white</li>
349 public void colourByCharge()
351 colourBySequence = false;
352 String command = "color white;color red ::ASP;color red ::GLU;color blue ::LYS;color blue ::ARG;color yellow ::CYS";
353 sendAsynchronousCommand(command, COLOURING_CHIMERA);
360 public String superposeStructures(AlignmentI[] _alignment,
361 int[] _refStructure, HiddenColumns[] _hiddenCols)
363 StringBuilder allComs = new StringBuilder(128);
364 String[] files = getStructureFiles();
366 if (!waitForFileLoad(files))
372 StringBuilder selectioncom = new StringBuilder(256);
373 for (int a = 0; a < _alignment.length; a++)
375 int refStructure = _refStructure[a];
376 AlignmentI alignment = _alignment[a];
377 HiddenColumns hiddenCols = _hiddenCols[a];
379 if (refStructure >= files.length)
381 System.err.println("Ignoring invalid reference structure value "
387 * 'matched' bit i will be set for visible alignment columns i where
388 * all sequences have a residue with a mapping to the PDB structure
390 BitSet matched = new BitSet();
391 for (int m = 0; m < alignment.getWidth(); m++)
393 if (hiddenCols == null || hiddenCols.isVisible(m))
399 SuperposeData[] structures = new SuperposeData[files.length];
400 for (int f = 0; f < files.length; f++)
402 structures[f] = new SuperposeData(alignment.getWidth());
406 * Calculate the superposable alignment columns ('matched'), and the
407 * corresponding structure residue positions (structures.pdbResNo)
409 int candidateRefStructure = findSuperposableResidues(alignment,
410 matched, structures);
411 if (refStructure < 0)
414 * If no reference structure was specified, pick the first one that has
415 * a mapping in the alignment
417 refStructure = candidateRefStructure;
420 int nmatched = matched.cardinality();
423 return MessageManager.formatMessage("label.insufficient_residues",
428 * Generate select statements to select regions to superimpose structures
430 String[] selcom = new String[files.length];
431 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
433 String chainCd = "." + structures[pdbfnum].chain;
436 StringBuilder molsel = new StringBuilder();
438 int nextColumnMatch = matched.nextSetBit(0);
439 while (nextColumnMatch != -1)
441 int pdbResNum = structures[pdbfnum].pdbResNo[nextColumnMatch];
442 if (lpos != pdbResNum - 1)
445 * discontiguous - append last residue now
449 molsel.append(String.valueOf(lpos));
450 molsel.append(chainCd);
458 * extending a contiguous run
463 * start the range selection
465 molsel.append(String.valueOf(lpos));
471 nextColumnMatch = matched.nextSetBit(nextColumnMatch + 1);
475 * and terminate final selection
479 molsel.append(String.valueOf(lpos));
480 molsel.append(chainCd);
482 if (molsel.length() > 1)
484 selcom[pdbfnum] = molsel.toString();
485 selectioncom.append("#").append(String.valueOf(pdbfnum))
487 selectioncom.append(selcom[pdbfnum]);
488 selectioncom.append(" ");
489 if (pdbfnum < files.length - 1)
491 selectioncom.append("| ");
496 selcom[pdbfnum] = null;
500 StringBuilder command = new StringBuilder(256);
501 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
503 if (pdbfnum == refStructure || selcom[pdbfnum] == null
504 || selcom[refStructure] == null)
508 if (command.length() > 0)
514 * Form Chimera match command, from the 'new' structure to the
515 * 'reference' structure e.g. (50 residues, chain B/A, alphacarbons):
517 * match #1:1-30.B,81-100.B@CA #0:21-40.A,61-90.A@CA
520 * https://www.cgl.ucsf.edu/chimera/docs/UsersGuide/midas/match.html
522 command.append("match ").append(getModelSpec(pdbfnum)).append(":");
523 command.append(selcom[pdbfnum]);
524 command.append("@").append(
525 structures[pdbfnum].isRna ? PHOSPHORUS : ALPHACARBON);
526 // JAL-1757 exclude alternate CA locations
527 command.append(NO_ALTLOCS);
528 command.append(" ").append(getModelSpec(refStructure)).append(":");
529 command.append(selcom[refStructure]);
530 command.append("@").append(
531 structures[refStructure].isRna ? PHOSPHORUS : ALPHACARBON);
532 command.append(NO_ALTLOCS);
534 if (selectioncom.length() > 0)
538 System.out.println("Select regions:\n" + selectioncom.toString());
540 "Superimpose command(s):\n" + command.toString());
542 allComs.append("~display all; chain @CA|P; ribbon ")
543 .append(selectioncom.toString())
544 .append(";" + command.toString());
549 if (selectioncom.length() > 0)
551 // TODO: visually distinguish regions that were superposed
552 if (selectioncom.substring(selectioncom.length() - 1).equals("|"))
554 selectioncom.setLength(selectioncom.length() - 1);
558 System.out.println("Select regions:\n" + selectioncom.toString());
560 allComs.append("; ~display all; chain @CA|P; ribbon ")
561 .append(selectioncom.toString()).append("; focus");
562 List<String> chimeraReplies = sendChimeraCommand(allComs.toString(),
564 for (String reply : chimeraReplies)
566 if (reply.toLowerCase().contains("unequal numbers of atoms"))
576 * Helper method to construct model spec in Chimera format:
578 * <li>#0 (#1 etc) for a PDB file with no sub-models</li>
579 * <li>#0.1 (#1.1 etc) for a PDB file with sub-models</li>
581 * Note for now we only ever choose the first of multiple models. This
582 * corresponds to the hard-coded Jmol equivalent (compare {1.1}). Refactor in
583 * future if there is a need to select specific sub-models.
588 protected String getModelSpec(int pdbfnum)
590 if (pdbfnum < 0 || pdbfnum >= getPdbCount())
596 * For now, the test for having sub-models is whether multiple Chimera
597 * models are mapped for the PDB file; the models are returned as a response
598 * to the Chimera command 'list models type molecule', see
599 * ChimeraManager.getModelList().
601 List<ChimeraModel> maps = chimeraMaps.get(getStructureFiles()[pdbfnum]);
602 boolean hasSubModels = maps != null && maps.size() > 1;
603 return "#" + String.valueOf(pdbfnum) + (hasSubModels ? ".1" : "");
607 * Launch Chimera, unless an instance linked to this object is already
608 * running. Returns true if Chimera is successfully launched, or already
609 * running, else false.
613 public boolean launchChimera()
615 if (viewer.isChimeraLaunched())
620 boolean launched = viewer
621 .launchChimera(StructureManager.getChimeraPaths());
624 startChimeraProcessMonitor();
628 log("Failed to launch Chimera!");
634 * Answers true if the Chimera process is still running, false if ended or not
639 public boolean isChimeraRunning()
641 return viewer.isChimeraLaunched();
645 * Send a command to Chimera, and optionally log and return any responses.
647 * Does nothing, and returns null, if the command is the same as the last one
653 public List<String> sendChimeraCommand(final String command,
658 // ? thread running after viewer shut down
661 List<String> reply = null;
662 viewerCommandHistory(false);
663 if (true /*lastCommand == null || !lastCommand.equals(command)*/)
665 // trim command or it may never find a match in the replyLog!!
666 List<String> lastReply = viewer.sendChimeraCommand(command.trim(),
673 log("Response from command ('" + command + "') was:\n"
678 viewerCommandHistory(true);
684 * Send a Chimera command asynchronously in a new thread. If the progress
685 * message is not null, display this message while the command is executing.
690 protected abstract void sendAsynchronousCommand(String command,
694 * Sends a set of colour commands to the structure viewer
696 * @param colourBySequenceCommands
699 protected void colourBySequence(
700 StructureMappingcommandSet[] colourBySequenceCommands)
702 for (StructureMappingcommandSet cpdbbyseq : colourBySequenceCommands)
704 for (String command : cpdbbyseq.commands)
706 sendAsynchronousCommand(command, COLOURING_CHIMERA);
718 protected StructureMappingcommandSet[] getColourBySequenceCommands(
719 String[] files, SequenceRenderer sr, AlignmentViewPanel viewPanel)
721 return ChimeraCommands.getColourBySequenceCommand(getSsm(), files,
722 getSequence(), sr, viewPanel, viewer.isChimeraX());
728 protected void executeWhenReady(String command)
731 sendChimeraCommand(command, false);
735 private void waitForChimera()
737 while (viewer != null && viewer.isBusy())
742 } catch (InterruptedException q)
748 // End StructureListener
749 // //////////////////////////
752 * instruct the Jalview binding to update the pdbentries vector if necessary
753 * prior to matching the viewer's contents to the list of structure files
754 * Jalview knows about.
756 public abstract void refreshPdbEntries();
759 * map between index of model filename returned from getPdbFile and the first
760 * index of models from this file in the viewer. Note - this is not trimmed -
761 * use getPdbFile to get number of unique models.
763 private int _modelFileNameMap[];
765 // ////////////////////////////////
766 // /StructureListener
768 public synchronized String[] getStructureFiles()
772 return new String[0];
775 return chimeraMaps.keySet()
776 .toArray(modelFileNames = new String[chimeraMaps.size()]);
780 * Construct and send a command to highlight zero, one or more atoms. We do this
781 * by sending an "rlabel" command to show the residue label at that position.
784 public void highlightAtoms(List<AtomSpec> atoms)
786 if (atoms == null || atoms.size() == 0)
791 boolean forChimeraX = viewer.isChimeraX();
792 StringBuilder cmd = new StringBuilder(128);
793 boolean first = true;
794 boolean found = false;
796 for (AtomSpec atom : atoms)
798 int pdbResNum = atom.getPdbResNum();
799 String chain = atom.getChain();
800 String pdbfile = atom.getPdbFile();
801 List<ChimeraModel> cms = chimeraMaps.get(pdbfile);
802 if (cms != null && !cms.isEmpty())
806 cmd.append(forChimeraX ? "label #" : "rlabel #");
815 cmd.append(cms.get(0).getModelNumber())
816 .append("/").append(chain).append(":").append(pdbResNum);
820 cmd.append(cms.get(0).getModelNumber())
821 .append(":").append(pdbResNum);
822 if (!chain.equals(" ") && !forChimeraX)
824 cmd.append(".").append(chain);
830 String command = cmd.toString();
833 * avoid repeated commands for the same residue
835 if (command.equals(lastHighlightCommand))
841 * unshow the label for the previous residue
843 if (lastHighlightCommand != null)
845 viewer.sendChimeraCommand("~" + lastHighlightCommand, false);
849 viewer.sendChimeraCommand(command, false);
851 this.lastHighlightCommand = command;
855 * Query Chimera for its current selection, and highlight it on the alignment
857 public void highlightChimeraSelection()
860 * Ask Chimera for its current selection
862 List<String> selection = viewer.getSelectedResidueSpecs();
865 * Parse model number, residue and chain for each selected position,
866 * formatted as #0:123.A or #1.2:87.B (#model.submodel:residue.chain)
868 List<AtomSpec> atomSpecs = convertStructureResiduesToAlignment(
872 * Broadcast the selection (which may be empty, if the user just cleared all
875 getSsm().mouseOverStructure(atomSpecs);
879 * Converts a list of Chimera atomspecs to a list of AtomSpec representing the
880 * corresponding residues (if any) in Jalview
882 * @param structureSelection
885 protected List<AtomSpec> convertStructureResiduesToAlignment(
886 List<String> structureSelection)
888 List<AtomSpec> atomSpecs = new ArrayList<>();
889 for (String atomSpec : structureSelection)
893 AtomSpec spec = AtomSpec.fromChimeraAtomspec(atomSpec);
894 String pdbfilename = getPdbFileForModel(spec.getModelNumber());
895 spec.setPdbFile(pdbfilename);
897 } catch (IllegalArgumentException e)
899 System.err.println("Failed to parse atomspec: " + atomSpec);
909 protected String getPdbFileForModel(int modelId)
912 * Work out the pdbfilename from the model number
914 String pdbfilename = modelFileNames[0];
915 findfileloop: for (String pdbfile : this.chimeraMaps.keySet())
917 for (ChimeraModel cm : chimeraMaps.get(pdbfile))
919 if (cm.getModelNumber() == modelId)
921 pdbfilename = pdbfile;
929 private void log(String message)
931 System.err.println("## Chimera log: " + message);
934 private void viewerCommandHistory(boolean enable)
936 // log("(Not yet implemented) History "
937 // + ((debug || enable) ? "on" : "off"));
940 public long getLoadNotifiesHandled()
942 return loadNotifiesHandled;
946 public void setJalviewColourScheme(ColourSchemeI cs)
948 colourBySequence = false;
955 // Chimera expects RBG values in the range 0-1
956 final double normalise = 255D;
957 viewerCommandHistory(false);
958 StringBuilder command = new StringBuilder(128);
960 List<String> residueSet = ResidueProperties.getResidues(isNucleotide(),
962 for (String resName : residueSet)
964 char res = resName.length() == 3
965 ? ResidueProperties.getSingleCharacterCode(resName)
967 Color col = cs.findColour(res, 0, null, null, 0f);
968 command.append("color " + col.getRed() / normalise + ","
969 + col.getGreen() / normalise + "," + col.getBlue() / normalise
970 + " ::" + resName + ";");
973 sendAsynchronousCommand(command.toString(), COLOURING_CHIMERA);
974 viewerCommandHistory(true);
978 * called when the binding thinks the UI needs to be refreshed after a Chimera
979 * state change. this could be because structures were loaded, or because an
980 * error has occurred.
982 public abstract void refreshGUI();
985 public void setLoadingFromArchive(boolean loadingFromArchive)
987 this.loadingFromArchive = loadingFromArchive;
992 * @return true if Chimeral is still restoring state or loading is still going
993 * on (see setFinsihedLoadingFromArchive)
996 public boolean isLoadingFromArchive()
998 return loadingFromArchive && !loadingFinished;
1002 * modify flag which controls if sequence colouring events are honoured by the
1003 * binding. Should be true for normal operation
1005 * @param finishedLoading
1008 public void setFinishedLoadingFromArchive(boolean finishedLoading)
1010 loadingFinished = finishedLoading;
1014 * Send the Chimera 'background solid <color>" command.
1017 * ://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/background
1022 public void setBackgroundColour(Color col)
1024 viewerCommandHistory(false);
1025 double normalise = 255D;
1026 final String command = "background solid " + col.getRed() / normalise
1027 + "," + col.getGreen() / normalise + ","
1028 + col.getBlue() / normalise + ";";
1029 viewer.sendChimeraCommand(command, false);
1030 viewerCommandHistory(true);
1034 * Ask Chimera to save its session to the given file. Returns true if
1035 * successful, else false.
1040 public boolean saveSession(String filepath)
1042 if (isChimeraRunning())
1044 List<String> reply = viewer.sendChimeraCommand("save " + filepath,
1046 if (reply.contains("Session written"))
1053 .error("Error saving Chimera session: " + reply.toString());
1060 * Ask Chimera to open a session file. Returns true if successful, else false.
1061 * The filename must have a .py extension for this command to work.
1066 public boolean openSession(String filepath)
1068 sendChimeraCommand("open " + filepath, true);
1069 // todo: test for failure - how?
1074 * Returns a list of chains mapped in this viewer. Note this list is not
1075 * currently scoped per structure.
1080 public List<String> getChainNames()
1086 * Send a 'focus' command to Chimera to recentre the visible display
1088 public void focusView()
1090 sendChimeraCommand("focus", false);
1094 * Send a 'show' command for all atoms in the currently selected columns
1096 * TODO: pull up to abstract structure viewer interface
1100 public void highlightSelection(AlignmentViewPanel vp)
1102 List<Integer> cols = vp.getAlignViewport().getColumnSelection()
1104 AlignmentI alignment = vp.getAlignment();
1105 StructureSelectionManager sm = getSsm();
1106 for (SequenceI seq : alignment.getSequences())
1109 * convert selected columns into sequence positions
1111 int[] positions = new int[cols.size()];
1113 for (Integer col : cols)
1115 positions[i++] = seq.findPosition(col);
1117 sm.highlightStructure(this, seq, positions);
1122 * Constructs and send commands to Chimera to set attributes on residues for
1123 * features visible in Jalview
1128 public int sendFeaturesToViewer(AlignmentViewPanel avp)
1130 // TODO refactor as required to pull up to an interface
1131 AlignmentI alignment = avp.getAlignment();
1133 String[] files = getStructureFiles();
1139 StructureMappingcommandSet commandSet = ChimeraCommands
1140 .getSetAttributeCommandsForFeatures(getSsm(), files,
1141 getSequence(), avp, viewer.isChimeraX());
1142 String[] commands = commandSet.commands;
1143 if (commands.length > 10)
1145 sendCommandsByFile(commands);
1149 for (String command : commands)
1151 sendAsynchronousCommand(command, null);
1154 return commands.length;
1158 * Write commands to a temporary file, and send a command to Chimera to open the
1159 * file as a commands script. For use when sending a large number of separate
1160 * commands would overload the REST interface mechanism.
1164 protected void sendCommandsByFile(String[] commands)
1166 boolean toChimeraX = viewer.isChimeraX();
1169 File tmp = File.createTempFile("chim", toChimeraX ? ".cxc" : ".com");
1171 PrintWriter out = new PrintWriter(new FileOutputStream(tmp));
1172 for (String command : commands)
1174 out.println(command);
1178 String path = tmp.getAbsolutePath();
1179 String command = "open " + (toChimeraX ? "" : "cmd:") + path;
1180 sendAsynchronousCommand(command, null);
1181 } catch (IOException e)
1183 System.err.println("Sending commands to Chimera via file failed with "
1189 * Get Chimera residues which have the named attribute, find the mapped
1190 * positions in the Jalview sequence(s), and set as sequence features
1193 * @param alignmentPanel
1195 public void copyStructureAttributesToFeatures(String attName,
1196 AlignmentViewPanel alignmentPanel)
1198 // todo pull up to AAStructureBindingModel (and interface?)
1201 * ask Chimera to list residues with the attribute, reporting its value
1203 // this alternative command
1204 // list residues spec ':*/attName' attr attName
1205 // doesn't report 'None' values (which is good), but
1206 // fails for 'average.bfactor' (which is bad):
1208 String cmd = "list residues attr '" + attName + "'";
1209 List<String> residues = sendChimeraCommand(cmd, true);
1211 boolean featureAdded = createFeaturesForAttributes(attName, residues);
1214 alignmentPanel.getFeatureRenderer().featuresAdded();
1219 * Create features in Jalview for the given attribute name and structure
1223 * The residue list should be 0, 1 or more reply lines of the format:
1224 * residue id #0:5.A isHelix -155.000836316 index 5
1226 * residue id #0:6.A isHelix None
1233 protected boolean createFeaturesForAttributes(String attName,
1234 List<String> residues)
1236 boolean featureAdded = false;
1237 String featureGroup = getViewerFeatureGroup();
1239 for (String residue : residues)
1241 AtomSpec spec = null;
1242 String[] tokens = residue.split(" ");
1243 if (tokens.length < 5)
1247 String atomSpec = tokens[2];
1248 String attValue = tokens[4];
1251 * ignore 'None' (e.g. for phi) or 'False' (e.g. for isHelix)
1253 if ("None".equalsIgnoreCase(attValue)
1254 || "False".equalsIgnoreCase(attValue))
1261 spec = AtomSpec.fromChimeraAtomspec(atomSpec);
1262 } catch (IllegalArgumentException e)
1264 System.err.println("Problem parsing atomspec " + atomSpec);
1268 String chainId = spec.getChain();
1269 String description = attValue;
1270 float score = Float.NaN;
1273 score = Float.valueOf(attValue);
1274 description = chainId;
1275 } catch (NumberFormatException e)
1277 // was not a float value
1280 String pdbFile = getPdbFileForModel(spec.getModelNumber());
1281 spec.setPdbFile(pdbFile);
1283 List<AtomSpec> atoms = Collections.singletonList(spec);
1286 * locate the mapped position in the alignment (if any)
1288 SearchResultsI sr = getSsm()
1289 .findAlignmentPositionsForStructurePositions(atoms);
1292 * expect one matched alignment position, or none
1293 * (if the structure position is not mapped)
1295 for (SearchResultMatchI m : sr.getResults())
1297 SequenceI seq = m.getSequence();
1298 int start = m.getStart();
1299 int end = m.getEnd();
1300 SequenceFeature sf = new SequenceFeature(attName, description,
1301 start, end, score, featureGroup);
1302 // todo: should SequenceFeature have an explicit property for chain?
1303 // note: repeating the action shouldn't duplicate features
1304 featureAdded |= seq.addSequenceFeature(sf);
1307 return featureAdded;
1311 * Answers the feature group name to apply to features created in Jalview from
1312 * Chimera attributes
1316 protected String getViewerFeatureGroup()
1318 // todo pull up to interface
1319 return CHIMERA_FEATURE_GROUP;
1322 public Hashtable<String, String> getChainFile()
1327 public List<ChimeraModel> getChimeraModelByChain(String chain)
1329 return chimeraMaps.get(chainFile.get(chain));
1332 public int getModelNoForChain(String chain)
1334 List<ChimeraModel> foundModels = getChimeraModelByChain(chain);
1335 if (foundModels != null && !foundModels.isEmpty())
1337 return foundModels.get(0).getModelNumber();