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.FeatureRenderer;
25 import jalview.api.SequenceRenderer;
26 import jalview.api.structures.JalviewStructureDisplayI;
27 import jalview.bin.Cache;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.ColumnSelection;
30 import jalview.datamodel.PDBEntry;
31 import jalview.datamodel.SequenceI;
32 import jalview.httpserver.AbstractRequestHandler;
33 import jalview.io.DataSourceType;
34 import jalview.schemes.ColourSchemeI;
35 import jalview.schemes.ResidueProperties;
36 import jalview.structure.AtomSpec;
37 import jalview.structure.StructureMappingcommandSet;
38 import jalview.structure.StructureSelectionManager;
39 import jalview.structures.models.AAStructureBindingModel;
40 import jalview.util.MessageManager;
42 import java.awt.Color;
43 import java.net.BindException;
44 import java.util.ArrayList;
45 import java.util.Hashtable;
46 import java.util.LinkedHashMap;
47 import java.util.List;
50 import ext.edu.ucsf.rbvi.strucviz2.ChimeraManager;
51 import ext.edu.ucsf.rbvi.strucviz2.ChimeraModel;
52 import ext.edu.ucsf.rbvi.strucviz2.StructureManager;
53 import ext.edu.ucsf.rbvi.strucviz2.StructureManager.ModelType;
55 public abstract class JalviewChimeraBinding extends AAStructureBindingModel
57 // Chimera clause to exclude alternate locations in atom selection
58 private static final String NO_ALTLOCS = "&~@.B-Z&~@.2-9";
60 private static final String COLOURING_CHIMERA = MessageManager
61 .getString("status.colouring_chimera");
63 private static final boolean debug = false;
65 private static final String PHOSPHORUS = "P";
67 private static final String ALPHACARBON = "CA";
69 private List<String> chainNames = new ArrayList<String>();
71 private Hashtable<String, String> chainFile = new Hashtable<String, String>();
74 * Object through which we talk to Chimera
76 private ChimeraManager viewer;
79 * Object which listens to Chimera notifications
81 private AbstractRequestHandler chimeraListener;
84 * set if chimera state is being restored from some source - instructs binding
85 * not to apply default display style when structure set is updated for first
88 private boolean loadingFromArchive = false;
91 * flag to indicate if the Chimera viewer should ignore sequence colouring
92 * events from the structure manager because the GUI is still setting up
94 private boolean loadingFinished = true;
97 * Map of ChimeraModel objects keyed by PDB full local file name
99 private Map<String, List<ChimeraModel>> chimeraMaps = new LinkedHashMap<String, List<ChimeraModel>>();
102 * the default or current model displayed if the model cannot be identified
103 * from the selection message
105 private int frameNo = 0;
107 private String lastCommand;
109 String lastHighlightCommand;
112 * incremented every time a load notification is successfully handled -
113 * lightweight mechanism for other threads to detect when they can start
114 * referring to new structures.
116 private long loadNotifiesHandled = 0;
118 private Thread chimeraMonitor;
121 * Open a PDB structure file in Chimera and set up mappings from Jalview.
123 * We check if the PDB model id is already loaded in Chimera, if so don't
124 * reopen it. This is the case if Chimera has opened a saved session file.
129 public boolean openFile(PDBEntry pe)
131 String file = pe.getFile();
134 List<ChimeraModel> modelsToMap = new ArrayList<ChimeraModel>();
135 List<ChimeraModel> oldList = viewer.getModelList();
136 boolean alreadyOpen = false;
139 * If Chimera already has this model, don't reopen it, but do remap it.
141 for (ChimeraModel open : oldList)
143 if (open.getModelName().equals(pe.getId()))
146 modelsToMap.add(open);
151 * If Chimera doesn't yet have this model, ask it to open it, and retrieve
152 * the model name(s) added by Chimera.
156 viewer.openModel(file, pe.getId(), ModelType.PDB_MODEL);
157 List<ChimeraModel> newList = viewer.getModelList();
158 // JAL-1728 newList.removeAll(oldList) does not work
159 for (ChimeraModel cm : newList)
161 if (cm.getModelName().equals(pe.getId()))
168 chimeraMaps.put(file, modelsToMap);
170 if (getSsm() != null)
172 getSsm().addStructureViewerListener(this);
173 // ssm.addSelectionListener(this);
174 FeatureRenderer fr = getFeatureRenderer(null);
182 } catch (Exception q)
184 log("Exception when trying to open model " + file + "\n"
199 public JalviewChimeraBinding(StructureSelectionManager ssm,
200 PDBEntry[] pdbentry, SequenceI[][] sequenceIs, DataSourceType protocol)
202 super(ssm, pdbentry, sequenceIs, protocol);
203 viewer = new ChimeraManager(new StructureManager(true));
207 * Starts a thread that waits for the Chimera process to finish, so that we
208 * can then close the associated resources. This avoids leaving orphaned
209 * Chimera viewer panels in Jalview if the user closes Chimera.
211 protected void startChimeraProcessMonitor()
213 final Process p = viewer.getChimeraProcess();
214 chimeraMonitor = new Thread(new Runnable()
223 JalviewStructureDisplayI display = getViewer();
226 display.closeViewer(false);
228 } catch (InterruptedException e)
230 // exit thread if Chimera Viewer is closed in Jalview
234 chimeraMonitor.start();
238 * Start a dedicated HttpServer to listen for Chimera notifications, and tell
239 * it to start listening
241 public void startChimeraListener()
245 chimeraListener = new ChimeraListener(this);
246 viewer.startListening(chimeraListener.getUri());
247 } catch (BindException e)
249 System.err.println("Failed to start Chimera listener: "
255 * Tells Chimera to display only the specified chains
259 public void showChains(List<String> toshow)
262 * Construct a chimera command like
264 * ~display #*;~ribbon #*;ribbon :.A,:.B
266 StringBuilder cmd = new StringBuilder(64);
267 boolean first = true;
268 for (String chain : toshow)
270 int modelNumber = getModelNoForChain(chain);
271 String showChainCmd = modelNumber == -1 ? "" : modelNumber + ":."
272 + chain.split(":")[1];
277 cmd.append(showChainCmd);
282 * could append ";focus" to this command to resize the display to fill the
283 * window, but it looks more helpful not to (easier to relate chains to the
286 final String command = "~display #*; ~ribbon #*; ribbon :"
288 sendChimeraCommand(command, false);
292 * Close down the Jalview viewer and listener, and (optionally) the associated
295 public void closeViewer(boolean closeChimera)
297 getSsm().removeStructureViewerListener(this, this.getPdbFile());
300 viewer.exitChimera();
302 if (this.chimeraListener != null)
304 chimeraListener.shutdown();
305 chimeraListener = null;
310 if (chimeraMonitor != null)
312 chimeraMonitor.interrupt();
314 releaseUIResources();
318 public void colourByChain()
320 colourBySequence = false;
321 sendAsynchronousCommand("rainbow chain", COLOURING_CHIMERA);
325 * Constructs and sends a Chimera command to colour by charge
327 * <li>Aspartic acid and Glutamic acid (negative charge) red</li>
328 * <li>Lysine and Arginine (positive charge) blue</li>
329 * <li>Cysteine - yellow</li>
330 * <li>all others - white</li>
334 public void colourByCharge()
336 colourBySequence = false;
337 String command = "color white;color red ::ASP;color red ::GLU;color blue ::LYS;color blue ::ARG;color yellow ::CYS";
338 sendAsynchronousCommand(command, COLOURING_CHIMERA);
342 * Construct and send a command to align structures against a reference
343 * structure, based on one or more sequence alignments
346 * an array of alignments to process
347 * @param _refStructure
348 * an array of corresponding reference structures (index into pdb
349 * file array); if a negative value is passed, the first PDB file
350 * mapped to an alignment sequence is used as the reference for
353 * an array of corresponding hidden columns for each alignment
356 public void superposeStructures(AlignmentI[] _alignment,
357 int[] _refStructure, ColumnSelection[] _hiddenCols)
359 StringBuilder allComs = new StringBuilder(128);
360 String[] files = getPdbFile();
362 if (!waitForFileLoad(files))
368 StringBuilder selectioncom = new StringBuilder(256);
369 for (int a = 0; a < _alignment.length; a++)
371 int refStructure = _refStructure[a];
372 AlignmentI alignment = _alignment[a];
373 ColumnSelection hiddenCols = _hiddenCols[a];
375 if (refStructure >= files.length)
377 System.err.println("Ignoring invalid reference structure value "
383 * 'matched' array will hold 'true' for visible alignment columns where
384 * all sequences have a residue with a mapping to the PDB structure
386 boolean matched[] = new boolean[alignment.getWidth()];
387 for (int m = 0; m < matched.length; m++)
389 matched[m] = (hiddenCols != null) ? hiddenCols.isVisible(m) : true;
392 SuperposeData[] structures = new SuperposeData[files.length];
393 for (int f = 0; f < files.length; f++)
395 structures[f] = new SuperposeData(alignment.getWidth());
399 * Calculate the superposable alignment columns ('matched'), and the
400 * corresponding structure residue positions (structures.pdbResNo)
402 int candidateRefStructure = findSuperposableResidues(alignment,
403 matched, structures);
404 if (refStructure < 0)
407 * If no reference structure was specified, pick the first one that has
408 * a mapping in the alignment
410 refStructure = candidateRefStructure;
414 for (boolean b : matched)
423 // TODO: bail out here because superposition illdefined?
427 * Generate select statements to select regions to superimpose structures
429 String[] selcom = new String[files.length];
430 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
432 String chainCd = "." + structures[pdbfnum].chain;
435 StringBuilder molsel = new StringBuilder();
436 for (int r = 0; r < matched.length; r++)
440 int pdbResNum = structures[pdbfnum].pdbResNo[r];
441 if (lpos != pdbResNum - 1)
444 * discontiguous - append last residue now
448 molsel.append(String.valueOf(lpos));
449 molsel.append(chainCd);
457 * extending a contiguous run
462 * start the range selection
464 molsel.append(String.valueOf(lpos));
474 * and terminate final selection
478 molsel.append(String.valueOf(lpos));
479 molsel.append(chainCd);
481 if (molsel.length() > 1)
483 selcom[pdbfnum] = molsel.toString();
484 selectioncom.append("#").append(String.valueOf(pdbfnum))
486 selectioncom.append(selcom[pdbfnum]);
487 selectioncom.append(" ");
488 if (pdbfnum < files.length - 1)
490 selectioncom.append("| ");
495 selcom[pdbfnum] = null;
499 StringBuilder command = new StringBuilder(256);
500 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
502 if (pdbfnum == refStructure || selcom[pdbfnum] == null
503 || selcom[refStructure] == null)
507 if (command.length() > 0)
513 * Form Chimera match command, from the 'new' structure to the
514 * 'reference' structure e.g. (50 residues, chain B/A, alphacarbons):
516 * match #1:1-30.B,81-100.B@CA #0:21-40.A,61-90.A@CA
519 * https://www.cgl.ucsf.edu/chimera/docs/UsersGuide/midas/match.html
521 command.append("match ").append(getModelSpec(pdbfnum)).append(":");
522 command.append(selcom[pdbfnum]);
523 command.append("@").append(
524 structures[pdbfnum].isRna ? PHOSPHORUS : ALPHACARBON);
525 // JAL-1757 exclude alternate CA locations
526 command.append(NO_ALTLOCS);
527 command.append(" ").append(getModelSpec(refStructure)).append(":");
528 command.append(selcom[refStructure]);
529 command.append("@").append(
530 structures[refStructure].isRna ? PHOSPHORUS : ALPHACARBON);
531 command.append(NO_ALTLOCS);
533 if (selectioncom.length() > 0)
537 System.out.println("Select regions:\n" + selectioncom.toString());
538 System.out.println("Superimpose command(s):\n"
539 + command.toString());
541 allComs.append("~display all; chain @CA|P; ribbon ")
542 .append(selectioncom.toString())
543 .append(";" + command.toString());
546 if (selectioncom.length() > 0)
548 // TODO: visually distinguish regions that were superposed
549 if (selectioncom.substring(selectioncom.length() - 1).equals("|"))
551 selectioncom.setLength(selectioncom.length() - 1);
555 System.out.println("Select regions:\n" + selectioncom.toString());
557 allComs.append("; ~display all; chain @CA|P; ribbon ")
558 .append(selectioncom.toString()).append("; focus");
559 sendChimeraCommand(allComs.toString(), false);
565 * Helper method to construct model spec in Chimera format:
567 * <li>#0 (#1 etc) for a PDB file with no sub-models</li>
568 * <li>#0.1 (#1.1 etc) for a PDB file with sub-models</li>
570 * Note for now we only ever choose the first of multiple models. This
571 * corresponds to the hard-coded Jmol equivalent (compare {1.1}). Refactor in
572 * future if there is a need to select specific sub-models.
577 protected String getModelSpec(int pdbfnum)
579 if (pdbfnum < 0 || pdbfnum >= getPdbCount())
585 * For now, the test for having sub-models is whether multiple Chimera
586 * models are mapped for the PDB file; the models are returned as a response
587 * to the Chimera command 'list models type molecule', see
588 * ChimeraManager.getModelList().
590 List<ChimeraModel> maps = chimeraMaps.get(getPdbFile()[pdbfnum]);
591 boolean hasSubModels = maps != null && maps.size() > 1;
592 return "#" + String.valueOf(pdbfnum) + (hasSubModels ? ".1" : "");
596 * Launch Chimera, unless an instance linked to this object is already
597 * running. Returns true if Chimera is successfully launched, or already
598 * running, else false.
602 public boolean launchChimera()
604 if (viewer.isChimeraLaunched())
609 boolean launched = viewer.launchChimera(StructureManager
613 startChimeraProcessMonitor();
617 log("Failed to launch Chimera!");
623 * Answers true if the Chimera process is still running, false if ended or not
628 public boolean isChimeraRunning()
630 return viewer.isChimeraLaunched();
634 * Send a command to Chimera, and optionally log any responses.
639 public void sendChimeraCommand(final String command, boolean logResponse)
643 // ? thread running after viewer shut down
646 viewerCommandHistory(false);
647 if (lastCommand == null || !lastCommand.equals(command))
649 // trim command or it may never find a match in the replyLog!!
650 List<String> lastReply = viewer.sendChimeraCommand(command.trim(),
652 if (logResponse && debug)
654 log("Response from command ('" + command + "') was:\n" + lastReply);
657 viewerCommandHistory(true);
658 lastCommand = command;
662 * Send a Chimera command asynchronously in a new thread. If the progress
663 * message is not null, display this message while the command is executing.
668 protected abstract void sendAsynchronousCommand(String command,
672 * Sends a set of colour commands to the structure viewer
674 * @param colourBySequenceCommands
677 protected void colourBySequence(
678 StructureMappingcommandSet[] colourBySequenceCommands)
680 for (StructureMappingcommandSet cpdbbyseq : colourBySequenceCommands)
682 for (String command : cpdbbyseq.commands)
684 sendAsynchronousCommand(command, COLOURING_CHIMERA);
697 protected StructureMappingcommandSet[] getColourBySequenceCommands(
698 String[] files, SequenceRenderer sr, FeatureRenderer fr,
699 AlignmentI alignment)
701 return ChimeraCommands.getColourBySequenceCommand(getSsm(), files,
702 getSequence(), sr, fr, alignment);
708 protected void executeWhenReady(String command)
711 sendChimeraCommand(command, false);
715 private void waitForChimera()
717 while (viewer != null && viewer.isBusy())
722 } catch (InterruptedException q)
728 // End StructureListener
729 // //////////////////////////
732 * instruct the Jalview binding to update the pdbentries vector if necessary
733 * prior to matching the viewer's contents to the list of structure files
734 * Jalview knows about.
736 public abstract void refreshPdbEntries();
739 * map between index of model filename returned from getPdbFile and the first
740 * index of models from this file in the viewer. Note - this is not trimmed -
741 * use getPdbFile to get number of unique models.
743 private int _modelFileNameMap[];
745 // ////////////////////////////////
746 // /StructureListener
748 public synchronized String[] getPdbFile()
752 return new String[0];
755 return chimeraMaps.keySet().toArray(
756 modelFileNames = new String[chimeraMaps.size()]);
760 * Construct and send a command to highlight zero, one or more atoms. We do
761 * this by sending an "rlabel" command to show the residue label at that
765 public void highlightAtoms(List<AtomSpec> atoms)
767 if (atoms == null || atoms.size() == 0)
772 StringBuilder cmd = new StringBuilder(128);
773 boolean first = true;
774 boolean found = false;
776 for (AtomSpec atom : atoms)
778 int pdbResNum = atom.getPdbResNum();
779 String chain = atom.getChain();
780 String pdbfile = atom.getPdbFile();
781 List<ChimeraModel> cms = chimeraMaps.get(pdbfile);
782 if (cms != null && !cms.isEmpty())
786 cmd.append("rlabel #").append(cms.get(0).getModelNumber())
794 cmd.append(pdbResNum);
795 if (!chain.equals(" "))
797 cmd.append(".").append(chain);
802 String command = cmd.toString();
805 * avoid repeated commands for the same residue
807 if (command.equals(lastHighlightCommand))
813 * unshow the label for the previous residue
815 if (lastHighlightCommand != null)
817 viewer.sendChimeraCommand("~" + lastHighlightCommand, false);
821 viewer.sendChimeraCommand(command, false);
823 this.lastHighlightCommand = command;
827 * Query Chimera for its current selection, and highlight it on the alignment
829 public void highlightChimeraSelection()
832 * Ask Chimera for its current selection
834 List<String> selection = viewer.getSelectedResidueSpecs();
837 * Parse model number, residue and chain for each selected position,
838 * formatted as #0:123.A or #1.2:87.B (#model.submodel:residue.chain)
840 List<AtomSpec> atomSpecs = new ArrayList<AtomSpec>();
841 for (String atomSpec : selection)
843 int colonPos = atomSpec.indexOf(":");
846 continue; // malformed
849 int hashPos = atomSpec.indexOf("#");
850 String modelSubmodel = atomSpec.substring(hashPos + 1, colonPos);
851 int dotPos = modelSubmodel.indexOf(".");
855 modelId = Integer.valueOf(dotPos == -1 ? modelSubmodel
856 : modelSubmodel.substring(0, dotPos));
857 } catch (NumberFormatException e)
859 // ignore, default to model 0
862 String residueChain = atomSpec.substring(colonPos + 1);
863 dotPos = residueChain.indexOf(".");
864 int pdbResNum = Integer.parseInt(dotPos == -1 ? residueChain
865 : residueChain.substring(0, dotPos));
867 String chainId = dotPos == -1 ? "" : residueChain
868 .substring(dotPos + 1);
871 * Work out the pdbfilename from the model number
873 String pdbfilename = modelFileNames[frameNo];
874 findfileloop: for (String pdbfile : this.chimeraMaps.keySet())
876 for (ChimeraModel cm : chimeraMaps.get(pdbfile))
878 if (cm.getModelNumber() == modelId)
880 pdbfilename = pdbfile;
885 atomSpecs.add(new AtomSpec(pdbfilename, chainId, pdbResNum, 0));
889 * Broadcast the selection (which may be empty, if the user just cleared all
892 getSsm().mouseOverStructure(atomSpecs);
895 private void log(String message)
897 System.err.println("## Chimera log: " + message);
900 private void viewerCommandHistory(boolean enable)
902 // log("(Not yet implemented) History "
903 // + ((debug || enable) ? "on" : "off"));
906 public long getLoadNotifiesHandled()
908 return loadNotifiesHandled;
912 public void setJalviewColourScheme(ColourSchemeI cs)
914 colourBySequence = false;
921 // Chimera expects RBG values in the range 0-1
922 final double normalise = 255D;
923 viewerCommandHistory(false);
924 StringBuilder command = new StringBuilder(128);
926 List<String> residueSet = ResidueProperties.getResidues(isNucleotide(),
928 for (String resName : residueSet)
930 char res = resName.length() == 3 ? ResidueProperties
931 .getSingleCharacterCode(resName) : resName.charAt(0);
932 Color col = cs.findColour(res, 0, null, null, 0f);
933 command.append("color " + col.getRed() / normalise + ","
934 + col.getGreen() / normalise + "," + col.getBlue()
935 / normalise + " ::" + resName + ";");
938 sendAsynchronousCommand(command.toString(), COLOURING_CHIMERA);
939 viewerCommandHistory(true);
943 * called when the binding thinks the UI needs to be refreshed after a Chimera
944 * state change. this could be because structures were loaded, or because an
945 * error has occurred.
947 public abstract void refreshGUI();
950 public void setLoadingFromArchive(boolean loadingFromArchive)
952 this.loadingFromArchive = loadingFromArchive;
957 * @return true if Chimeral is still restoring state or loading is still going
958 * on (see setFinsihedLoadingFromArchive)
961 public boolean isLoadingFromArchive()
963 return loadingFromArchive && !loadingFinished;
967 * modify flag which controls if sequence colouring events are honoured by the
968 * binding. Should be true for normal operation
970 * @param finishedLoading
973 public void setFinishedLoadingFromArchive(boolean finishedLoading)
975 loadingFinished = finishedLoading;
979 * Send the Chimera 'background solid <color>" command.
982 * ://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/background
987 public void setBackgroundColour(Color col)
989 viewerCommandHistory(false);
990 double normalise = 255D;
991 final String command = "background solid " + col.getRed() / normalise
992 + "," + col.getGreen() / normalise + "," + col.getBlue()
994 viewer.sendChimeraCommand(command, false);
995 viewerCommandHistory(true);
999 * Ask Chimera to save its session to the given file. Returns true if
1000 * successful, else false.
1005 public boolean saveSession(String filepath)
1007 if (isChimeraRunning())
1009 List<String> reply = viewer.sendChimeraCommand("save " + filepath,
1011 if (reply.contains("Session written"))
1018 .error("Error saving Chimera session: " + reply.toString());
1025 * Ask Chimera to open a session file. Returns true if successful, else false.
1026 * The filename must have a .py extension for this command to work.
1031 public boolean openSession(String filepath)
1033 sendChimeraCommand("open " + filepath, true);
1034 // todo: test for failure - how?
1039 * Send a 'focus' command to Chimera to recentre the visible display
1041 public void focusView()
1043 sendChimeraCommand("focus", false);
1047 * Send a 'show' command for all atoms in the currently selected columns
1049 * TODO: pull up to abstract structure viewer interface
1053 public void highlightSelection(AlignmentViewPanel vp)
1055 List<Integer> cols = vp.getAlignViewport().getColumnSelection()
1057 AlignmentI alignment = vp.getAlignment();
1058 StructureSelectionManager sm = getSsm();
1059 for (SequenceI seq : alignment.getSequences())
1062 * convert selected columns into sequence positions
1064 int[] positions = new int[cols.size()];
1066 for (Integer col : cols)
1068 positions[i++] = seq.findPosition(col);
1070 sm.highlightStructure(this, seq, positions);
1076 public List<String> getChainNames()
1081 public Hashtable<String, String> getChainFile()
1086 public List<ChimeraModel> getChimeraModelByChain(String chain)
1088 return chimeraMaps.get(chainFile.get(chain));
1091 public int getModelNoForChain(String chain)
1093 List<ChimeraModel> foundModels = getChimeraModelByChain(chain);
1094 if (foundModels != null && !foundModels.isEmpty())
1096 return foundModels.get(0).getModelNumber();