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;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.PrintWriter;
27 import java.net.BindException;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.Iterator;
31 import java.util.LinkedHashMap;
32 import java.util.List;
35 import ext.edu.ucsf.rbvi.strucviz2.ChimeraManager;
36 import ext.edu.ucsf.rbvi.strucviz2.ChimeraModel;
37 import ext.edu.ucsf.rbvi.strucviz2.StructureManager;
38 import ext.edu.ucsf.rbvi.strucviz2.StructureManager.ModelType;
39 import jalview.api.AlignViewportI;
40 import jalview.api.AlignmentViewPanel;
41 import jalview.api.structures.JalviewStructureDisplayI;
42 import jalview.bin.Cache;
43 import jalview.datamodel.AlignmentI;
44 import jalview.datamodel.PDBEntry;
45 import jalview.datamodel.SearchResultMatchI;
46 import jalview.datamodel.SearchResultsI;
47 import jalview.datamodel.SequenceFeature;
48 import jalview.datamodel.SequenceI;
49 import jalview.gui.StructureViewer.ViewerType;
50 import jalview.httpserver.AbstractRequestHandler;
51 import jalview.io.DataSourceType;
52 import jalview.structure.AtomSpec;
53 import jalview.structure.AtomSpecModel;
54 import jalview.structure.StructureCommand;
55 import jalview.structure.StructureCommandI;
56 import jalview.structure.StructureSelectionManager;
57 import jalview.structures.models.AAStructureBindingModel;
59 public abstract class JalviewChimeraBinding extends AAStructureBindingModel
61 public static final String CHIMERA_SESSION_EXTENSION = ".py";
63 public static final String CHIMERA_FEATURE_GROUP = "Chimera";
66 * Object through which we talk to Chimera
68 private ChimeraManager chimeraManager;
71 * Object which listens to Chimera notifications
73 private AbstractRequestHandler chimeraListener;
76 * Map of ChimeraModel objects keyed by PDB full local file name
78 protected Map<String, List<ChimeraModel>> chimeraMaps = new LinkedHashMap<>();
80 String lastHighlightCommand;
82 private Thread chimeraMonitor;
85 * Open a PDB structure file in Chimera and set up mappings from Jalview.
87 * We check if the PDB model id is already loaded in Chimera, if so don't reopen
88 * it. This is the case if Chimera has opened a saved session file.
93 public boolean openFile(PDBEntry pe)
95 String file = pe.getFile();
98 List<ChimeraModel> modelsToMap = new ArrayList<>();
99 List<ChimeraModel> oldList = chimeraManager.getModelList();
100 boolean alreadyOpen = false;
103 * If Chimera already has this model, don't reopen it, but do remap it.
105 for (ChimeraModel open : oldList)
107 if (open.getModelName().equals(pe.getId()))
110 modelsToMap.add(open);
115 * If Chimera doesn't yet have this model, ask it to open it, and retrieve
116 * the model name(s) added by Chimera.
120 chimeraManager.openModel(file, pe.getId(), ModelType.PDB_MODEL);
121 addChimeraModel(pe, modelsToMap);
124 chimeraMaps.put(file, modelsToMap);
126 if (getSsm() != null)
128 getSsm().addStructureViewerListener(this);
131 } catch (Exception q)
133 log("Exception when trying to open model " + file + "\n"
141 * Adds the ChimeraModel corresponding to the given PDBEntry, based on model
142 * name matching PDB id
147 protected void addChimeraModel(PDBEntry pe,
148 List<ChimeraModel> modelsToMap)
151 * Chimera: query for actual models and find the one with
152 * matching model name - already set in viewer.openModel()
154 List<ChimeraModel> newList = chimeraManager.getModelList();
155 // JAL-1728 newList.removeAll(oldList) does not work
156 for (ChimeraModel cm : newList)
158 if (cm.getModelName().equals(pe.getId()))
173 public JalviewChimeraBinding(StructureSelectionManager ssm,
174 PDBEntry[] pdbentry, SequenceI[][] sequenceIs,
175 DataSourceType protocol)
177 super(ssm, pdbentry, sequenceIs, protocol);
178 chimeraManager = new ChimeraManager(new StructureManager(true));
179 chimeraManager.setChimeraX(ViewerType.CHIMERAX.equals(getViewerType()));
180 setStructureCommands(new ChimeraCommands());
184 protected ViewerType getViewerType()
186 return ViewerType.CHIMERA;
190 * Starts a thread that waits for the Chimera process to finish, so that we can
191 * then close the associated resources. This avoids leaving orphaned Chimera
192 * viewer panels in Jalview if the user closes Chimera.
194 protected void startChimeraProcessMonitor()
196 final Process p = chimeraManager.getChimeraProcess();
197 chimeraMonitor = new Thread(new Runnable()
206 JalviewStructureDisplayI display = getViewer();
209 display.closeViewer(false);
211 } catch (InterruptedException e)
213 // exit thread if Chimera Viewer is closed in Jalview
217 chimeraMonitor.start();
221 * Start a dedicated HttpServer to listen for Chimera notifications, and tell it
224 public void startChimeraListener()
228 chimeraListener = new ChimeraListener(this);
229 chimeraManager.startListening(chimeraListener.getUri());
230 } catch (BindException e)
233 "Failed to start Chimera listener: " + e.getMessage());
238 * Close down the Jalview viewer and listener, and (optionally) the associated
242 public void closeViewer(boolean closeChimera)
244 super.closeViewer(closeChimera);
247 chimeraManager.exitChimera();
249 if (this.chimeraListener != null)
251 chimeraListener.shutdown();
252 chimeraListener = null;
254 chimeraManager = null;
256 if (chimeraMonitor != null)
258 chimeraMonitor.interrupt();
263 * Helper method to construct model spec in Chimera format:
265 * <li>#0 (#1 etc) for a PDB file with no sub-models</li>
266 * <li>#0.1 (#1.1 etc) for a PDB file with sub-models</li>
268 * Note for now we only ever choose the first of multiple models. This
269 * corresponds to the hard-coded Jmol equivalent (compare {1.1}). Refactor in
270 * future if there is a need to select specific sub-models.
276 public String getModelSpec(int pdbfnum)
278 if (pdbfnum < 0 || pdbfnum >= getPdbCount())
280 return "#" + pdbfnum; // temp hack for ChimeraX
284 * For now, the test for having sub-models is whether multiple Chimera
285 * models are mapped for the PDB file; the models are returned as a response
286 * to the Chimera command 'list models type molecule', see
287 * ChimeraManager.getModelList().
289 String[] structureFiles = getStructureFiles();
290 if (pdbfnum < 0 || pdbfnum >= structureFiles.length)
295 List<ChimeraModel> maps = chimeraMaps.get(structureFiles[pdbfnum]);
296 boolean hasSubModels = maps != null && maps.size() > 1;
297 String spec = "#" + String.valueOf(pdbfnum);
298 return hasSubModels ? spec + ".1" : spec;
302 * Launch Chimera, unless an instance linked to this object is already
303 * running. Returns true if Chimera is successfully launched, or already
304 * running, else false.
308 public boolean launchChimera()
310 if (chimeraManager.isChimeraLaunched())
315 boolean launched = chimeraManager.launchChimera(getChimeraPaths());
318 startChimeraProcessMonitor();
322 log("Failed to launch Chimera!");
328 * Returns a list of candidate paths to the Chimera program executable
332 protected List<String> getChimeraPaths()
334 return StructureManager.getChimeraPaths(false);
338 * Answers true if the Chimera process is still running, false if ended or not
344 public boolean isViewerRunning()
346 return chimeraManager.isChimeraLaunched();
350 * Send a command to Chimera, and optionally log and return any responses.
356 public List<String> executeCommand(final StructureCommandI command,
359 if (chimeraManager == null || command == null)
361 // ? thread running after viewer shut down
364 List<String> reply = null;
365 // trim command or it may never find a match in the replyLog!!
366 String cmd = command.getCommand().trim();
367 List<String> lastReply = chimeraManager
368 .sendChimeraCommand(cmd, getResponse);
373 "Response from command ('" + cmd + "') was:\n" + lastReply);
380 public synchronized String[] getStructureFiles()
382 if (chimeraManager == null)
384 return new String[0];
387 return chimeraMaps.keySet()
388 .toArray(modelFileNames = new String[chimeraMaps.size()]);
392 * Construct and send a command to highlight zero, one or more atoms. We do this
393 * by sending an "rlabel" command to show the residue label at that position.
396 public void highlightAtoms(List<AtomSpec> atoms)
398 if (atoms == null || atoms.size() == 0)
403 boolean forChimeraX = chimeraManager.isChimeraX();
404 StringBuilder cmd = new StringBuilder(128);
405 boolean first = true;
406 boolean found = false;
408 for (AtomSpec atom : atoms)
410 int pdbResNum = atom.getPdbResNum();
411 String chain = atom.getChain();
412 String pdbfile = atom.getPdbFile();
413 List<ChimeraModel> cms = chimeraMaps.get(pdbfile);
414 if (cms != null && !cms.isEmpty())
418 cmd.append(forChimeraX ? "label #" : "rlabel #");
427 cmd.append(cms.get(0).getModelNumber())
428 .append("/").append(chain).append(":").append(pdbResNum);
432 cmd.append(cms.get(0).getModelNumber())
433 .append(":").append(pdbResNum);
434 if (!chain.equals(" ") && !forChimeraX)
436 cmd.append(".").append(chain);
442 String command = cmd.toString();
445 * avoid repeated commands for the same residue
447 if (command.equals(lastHighlightCommand))
453 * unshow the label for the previous residue
455 if (lastHighlightCommand != null)
457 chimeraManager.sendChimeraCommand("~" + lastHighlightCommand, false);
461 chimeraManager.sendChimeraCommand(command, false);
463 this.lastHighlightCommand = command;
467 * Query Chimera for its current selection, and highlight it on the alignment
469 public void highlightChimeraSelection()
472 * Ask Chimera for its current selection
474 List<String> selection = chimeraManager.getSelectedResidueSpecs();
477 * Parse model number, residue and chain for each selected position,
478 * formatted as #0:123.A or #1.2:87.B (#model.submodel:residue.chain)
480 List<AtomSpec> atomSpecs = convertStructureResiduesToAlignment(
484 * Broadcast the selection (which may be empty, if the user just cleared all
487 getSsm().mouseOverStructure(atomSpecs);
491 * Converts a list of Chimera atomspecs to a list of AtomSpec representing the
492 * corresponding residues (if any) in Jalview
494 * @param structureSelection
497 protected List<AtomSpec> convertStructureResiduesToAlignment(
498 List<String> structureSelection)
500 boolean chimeraX = chimeraManager.isChimeraX();
501 List<AtomSpec> atomSpecs = new ArrayList<>();
502 for (String atomSpec : structureSelection)
506 AtomSpec spec = AtomSpec.fromChimeraAtomspec(atomSpec, chimeraX);
507 String pdbfilename = getPdbFileForModel(spec.getModelNumber());
508 spec.setPdbFile(pdbfilename);
510 } catch (IllegalArgumentException e)
512 System.err.println("Failed to parse atomspec: " + atomSpec);
522 protected String getPdbFileForModel(int modelId)
525 * Work out the pdbfilename from the model number
527 String pdbfilename = modelFileNames[0];
528 findfileloop: for (String pdbfile : this.chimeraMaps.keySet())
530 for (ChimeraModel cm : chimeraMaps.get(pdbfile))
532 if (cm.getModelNumber() == modelId)
534 pdbfilename = pdbfile;
542 private void log(String message)
544 System.err.println("## Chimera log: " + message);
548 * Send a 'show' command for all atoms in the currently selected columns
550 * TODO: pull up to abstract structure viewer interface
554 public void highlightSelection(AlignmentViewPanel vp)
556 List<Integer> cols = vp.getAlignViewport().getColumnSelection()
558 AlignmentI alignment = vp.getAlignment();
559 StructureSelectionManager sm = getSsm();
560 for (SequenceI seq : alignment.getSequences())
563 * convert selected columns into sequence positions
565 int[] positions = new int[cols.size()];
567 for (Integer col : cols)
569 positions[i++] = seq.findPosition(col);
571 sm.highlightStructure(this, seq, positions);
576 * Constructs and send commands to Chimera to set attributes on residues for
577 * features visible in Jalview.
579 * The syntax is: setattr r <attName> <attValue> <atomSpec>
581 * For example: setattr r jv_chain "Ferredoxin-1, Chloroplastic" #0:94.A
586 public int sendFeaturesToViewer(AlignmentViewPanel avp)
588 // TODO refactor as required to pull up to an interface
589 Map<String, Map<Object, AtomSpecModel>> featureValues = buildFeaturesMap(
591 List<StructureCommandI> commands = getCommandGenerator()
592 .setAttributes(featureValues);
593 if (commands.size() > 10)
595 sendCommandsByFile(commands);
599 for (StructureCommandI command : commands)
601 sendAsynchronousCommand(command, null);
604 return commands.size();
608 * Write commands to a temporary file, and send a command to Chimera to open the
609 * file as a commands script. For use when sending a large number of separate
610 * commands would overload the REST interface mechanism.
614 protected void sendCommandsByFile(List<StructureCommandI> commands)
618 File tmp = File.createTempFile("chim", getCommandFileExtension());
620 PrintWriter out = new PrintWriter(new FileOutputStream(tmp));
621 for (StructureCommandI command : commands)
623 out.println(command.getCommand());
627 String path = tmp.getAbsolutePath();
628 StructureCommandI command = getCommandGenerator()
629 .openCommandFile(path);
630 sendAsynchronousCommand(command, null);
631 } catch (IOException e)
633 System.err.println("Sending commands to Chimera via file failed with "
639 * Returns the file extension required for a file of commands to be read by
640 * the structure viewer
643 protected String getCommandFileExtension()
649 * Get Chimera residues which have the named attribute, find the mapped
650 * positions in the Jalview sequence(s), and set as sequence features
653 * @param alignmentPanel
655 public void copyStructureAttributesToFeatures(String attName,
656 AlignmentViewPanel alignmentPanel)
658 // todo pull up to AAStructureBindingModel (and interface?)
661 * ask Chimera to list residues with the attribute, reporting its value
663 // this alternative command
664 // list residues spec ':*/attName' attr attName
665 // doesn't report 'None' values (which is good), but
666 // fails for 'average.bfactor' (which is bad):
668 String cmd = "list residues attr '" + attName + "'";
669 List<String> residues = executeCommand(new StructureCommand(cmd), true);
671 boolean featureAdded = createFeaturesForAttributes(attName, residues);
674 alignmentPanel.getFeatureRenderer().featuresAdded();
679 * Create features in Jalview for the given attribute name and structure
683 * The residue list should be 0, 1 or more reply lines of the format:
684 * residue id #0:5.A isHelix -155.000836316 index 5
686 * residue id #0:6.A isHelix None
693 protected boolean createFeaturesForAttributes(String attName,
694 List<String> residues)
696 boolean featureAdded = false;
697 String featureGroup = getViewerFeatureGroup();
698 boolean chimeraX = chimeraManager.isChimeraX();
700 for (String residue : residues)
702 AtomSpec spec = null;
703 String[] tokens = residue.split(" ");
704 if (tokens.length < 5)
708 String atomSpec = tokens[2];
709 String attValue = tokens[4];
712 * ignore 'None' (e.g. for phi) or 'False' (e.g. for isHelix)
714 if ("None".equalsIgnoreCase(attValue)
715 || "False".equalsIgnoreCase(attValue))
722 spec = AtomSpec.fromChimeraAtomspec(atomSpec, chimeraX);
723 } catch (IllegalArgumentException e)
725 System.err.println("Problem parsing atomspec " + atomSpec);
729 String chainId = spec.getChain();
730 String description = attValue;
731 float score = Float.NaN;
734 score = Float.valueOf(attValue);
735 description = chainId;
736 } catch (NumberFormatException e)
738 // was not a float value
741 String pdbFile = getPdbFileForModel(spec.getModelNumber());
742 spec.setPdbFile(pdbFile);
744 List<AtomSpec> atoms = Collections.singletonList(spec);
747 * locate the mapped position in the alignment (if any)
749 SearchResultsI sr = getSsm()
750 .findAlignmentPositionsForStructurePositions(atoms);
753 * expect one matched alignment position, or none
754 * (if the structure position is not mapped)
756 for (SearchResultMatchI m : sr.getResults())
758 SequenceI seq = m.getSequence();
759 int start = m.getStart();
760 int end = m.getEnd();
761 SequenceFeature sf = new SequenceFeature(attName, description,
762 start, end, score, featureGroup);
763 // todo: should SequenceFeature have an explicit property for chain?
764 // note: repeating the action shouldn't duplicate features
765 featureAdded |= seq.addSequenceFeature(sf);
772 * Answers the feature group name to apply to features created in Jalview from
777 protected String getViewerFeatureGroup()
779 // todo pull up to interface
780 return CHIMERA_FEATURE_GROUP;
784 public String getModelIdForFile(String pdbFile)
786 List<ChimeraModel> foundModels = chimeraMaps.get(pdbFile);
787 if (foundModels != null && !foundModels.isEmpty())
789 return String.valueOf(foundModels.get(0).getModelNumber());
795 * Answers a (possibly empty) list of attribute names in Chimera[X], excluding
796 * any which were added from Jalview
800 public List<String> getChimeraAttributes()
802 List<String> atts = chimeraManager.getAttrList();
803 Iterator<String> it = atts.iterator();
806 if (it.next().startsWith(ChimeraCommands.NAMESPACE_PREFIX))
809 * attribute added from Jalview - exclude it
818 * Returns the file extension to use for a saved viewer session file (.py)
823 public String getSessionFileExtension()
825 return CHIMERA_SESSION_EXTENSION;
829 public String getHelpURL()
831 return "https://www.cgl.ucsf.edu/chimera/docs/UsersGuide";
835 public void showStructures(AlignViewportI av, boolean refocus)
837 StructureCommandI cmd = buildShowStructuresCommand(av, refocus);
838 executeCommand(cmd, false);
842 * Builds a command to show parts of the structure, depending on whether
844 * <li>all structures or regions mapped to alignment only are shown</li>
845 * <li>all chains or only selected chains are shown</li>
852 protected StructureCommandI buildShowStructuresCommand(
856 // TODO refactor using command generator
857 // pull up this method and Jmol variant to base class
858 StringBuilder cmd = new StringBuilder(128);
859 cmd.append("~display");
861 if (isShowAlignmentOnly())
863 AtomSpecModel model = getShownResidues(av);
864 String atomSpec = getCommandGenerator().getAtomSpec(model, false);
865 if (!atomSpec.isEmpty())
867 cmd.append("; ~ribbon; ribbon ").append(atomSpec);
872 cmd.append("; ribbon");
876 * hide any chains selected not to be shown (whether mapped to
877 * sequence in the alignment or not)
879 for (String pdbChain : chainsToHide)
881 String chainId = pdbChain.split(":")[1];
882 String modelNo = getModelIdForFile(getFileForChain(pdbChain));
883 if (!"".equals(modelNo))
885 cmd.append("; ~ribbon #").append(modelNo).append(":.")
891 cmd.append("; focus");
893 return new StructureCommand(cmd.toString());
897 public int getModelForPdbFile(String fileName, int fileIndex)
899 if (chimeraMaps.containsKey(fileName))
901 List<ChimeraModel> models = chimeraMaps.get(fileName);
902 if (!models.isEmpty())
904 return models.get(0).getModelNumber();