3 import java.util.ArrayList;
4 import java.util.HashMap;
8 import jalview.api.AlignmentViewPanel;
9 import jalview.datamodel.PDBEntry;
10 import jalview.datamodel.SequenceI;
11 import jalview.ext.pymol.PymolCommands;
12 import jalview.ext.pymol.PymolManager;
13 import jalview.gui.StructureViewer.ViewerType;
14 import jalview.structure.AtomSpec;
15 import jalview.structure.StructureCommand;
16 import jalview.structure.StructureCommandI;
17 import jalview.structure.StructureSelectionManager;
18 import jalview.structures.models.AAStructureBindingModel;
20 public class PymolBindingModel extends AAStructureBindingModel
23 * format for labels shown on structures when mousing over sequence;
24 * see https://pymolwiki.org/index.php/Label#examples
25 * left not final so customisable e.g. with a Groovy script
27 private static String LABEL_FORMAT = "\"%s %s\" % (resn,resi)";
29 private PymolManager pymolManager;
31 private Thread pymolMonitor;
34 * full paths to structure files opened in PyMOL
36 List<String> structureFiles = new ArrayList<>();
39 * lookup from file path to PyMOL object name
41 Map<String, String> pymolObjects = new HashMap<>();
43 private String lastLabelSpec;
53 public PymolBindingModel(StructureViewerBase viewer,
54 StructureSelectionManager ssm, PDBEntry[] pdbentry,
55 SequenceI[][] sequenceIs)
57 super(ssm, pdbentry, sequenceIs, null);
58 pymolManager = new PymolManager();
59 setStructureCommands(new PymolCommands());
64 public String[] getStructureFiles()
66 return structureFiles.toArray(new String[structureFiles.size()]);
70 public void highlightAtoms(List<AtomSpec> atoms)
73 * https://pymolwiki.org/index.php/Label#examples
75 StringBuilder sb = new StringBuilder();
76 for (AtomSpec atom : atoms)
78 // todo promote to StructureCommandsI.showLabel()
79 // todo handle CA|P correctly
80 String modelId = getModelIdForFile(atom.getPdbFile());
81 sb.append(String.format(" %s//%s/%d/CA", modelId,
83 atom.getPdbResNum()));
85 String labelSpec = sb.toString();
86 if (labelSpec.equals(lastLabelSpec))
90 StructureCommandI command = new StructureCommand("label", labelSpec, LABEL_FORMAT);
91 executeCommand(command, false);
94 * and remove the label(s) previously shown
96 if (lastLabelSpec != null)
98 command = new StructureCommand("label", lastLabelSpec, "");
99 executeCommand(command, false);
102 lastLabelSpec = labelSpec;
106 public SequenceRenderer getSequenceRenderer(AlignmentViewPanel alignment)
109 return new SequenceRenderer(alignment.getAlignViewport());
113 protected List<String> executeCommand(StructureCommandI command,
116 // System.out.println(command.toString()); // debug
117 return pymolManager.sendCommand(command, getReply);
121 protected String getModelIdForFile(String file)
123 return pymolObjects.containsKey(file) ? pymolObjects.get(file) : "";
127 protected ViewerType getViewerType()
129 return ViewerType.PYMOL;
132 public boolean isPymolRunning()
134 return pymolManager.isPymolLaunched();
137 public void closeViewer(boolean closePymol)
139 getSsm().removeStructureViewerListener(this, this.getStructureFiles());
142 pymolManager.exitPymol();
146 if (pymolMonitor != null)
148 pymolMonitor.interrupt();
150 releaseUIResources();
153 public boolean openSession(String pymolSessionFile)
155 StructureCommandI cmd = getCommandGenerator()
156 .loadFile(pymolSessionFile);
157 executeCommand(cmd, false);
161 public boolean launchPymol()
163 if (pymolManager.isPymolLaunched())
168 boolean launched = pymolManager.launchPymol();
171 // start listening for PyMOL selections - how??
175 System.err.println("Failed to launch PyMOL!");
180 public void openFile(PDBEntry pe)
182 // todo : check not already open, remap / rename, etc
183 String file = pe.getFile();
184 StructureCommandI cmd = getCommandGenerator().loadFile(file);
187 * a second parameter sets the pdbid as the loaded PyMOL object name
189 String pdbId = pe.getId();
190 cmd.addParameter(pdbId);
192 executeCommand(cmd, false);
194 pymolObjects.put(file, pdbId);
195 if (!structureFiles.contains(file))
197 structureFiles.add(file);
199 if (getSsm() != null)
201 getSsm().addStructureViewerListener(this);
207 protected String getModelId(int pdbfnum, String file)
213 * Returns the file extension to use for a saved viewer session file (.pse)
216 * @see https://pymolwiki.org/index.php/Save
219 public String getSessionFileExtension()