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.AtomSpecModel;
16 import jalview.structure.StructureCommand;
17 import jalview.structure.StructureCommandI;
18 import jalview.structure.StructureSelectionManager;
19 import jalview.structures.models.AAStructureBindingModel;
21 public class PymolBindingModel extends AAStructureBindingModel
24 * format for labels shown on structures when mousing over sequence;
25 * see https://pymolwiki.org/index.php/Label#examples
26 * left not final so customisable e.g. with a Groovy script
28 private static String LABEL_FORMAT = "\"%s %s\" % (resn,resi)";
30 private PymolManager pymolManager;
32 private Thread pymolMonitor;
35 * full paths to structure files opened in PyMOL
37 List<String> structureFiles = new ArrayList<>();
40 * lookup from file path to PyMOL object name
42 Map<String, String> pymolObjects = new HashMap<>();
44 private String lastLabelSpec;
54 public PymolBindingModel(StructureViewerBase viewer,
55 StructureSelectionManager ssm, PDBEntry[] pdbentry,
56 SequenceI[][] sequenceIs)
58 super(ssm, pdbentry, sequenceIs, null);
59 pymolManager = new PymolManager();
60 setStructureCommands(new PymolCommands());
65 public String[] getStructureFiles()
67 return structureFiles.toArray(new String[structureFiles.size()]);
71 public void highlightAtoms(List<AtomSpec> atoms)
74 * https://pymolwiki.org/index.php/Label#examples
76 StringBuilder sb = new StringBuilder();
77 for (AtomSpec atom : atoms)
79 // todo promote to StructureCommandsI.showLabel()
80 // todo handle CA|P correctly
81 String modelId = getModelIdForFile(atom.getPdbFile());
82 sb.append(String.format(" %s//%s/%d/CA", modelId,
84 atom.getPdbResNum()));
86 String labelSpec = sb.toString();
87 if (labelSpec.equals(lastLabelSpec))
91 StructureCommandI command = new StructureCommand("label", labelSpec, LABEL_FORMAT);
92 executeCommand(command, false);
95 * and remove the label(s) previously shown
97 if (lastLabelSpec != null)
99 command = new StructureCommand("label", lastLabelSpec, "");
100 executeCommand(command, false);
103 lastLabelSpec = labelSpec;
107 public SequenceRenderer getSequenceRenderer(AlignmentViewPanel avp)
109 return new SequenceRenderer(avp.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;
133 public boolean isViewerRunning()
135 return pymolManager.isPymolLaunched();
139 public void closeViewer(boolean closePymol)
141 super.closeViewer(closePymol);
144 pymolManager.exitPymol();
148 if (pymolMonitor != null)
150 pymolMonitor.interrupt();
154 public boolean openSession(String pymolSessionFile)
156 StructureCommandI cmd = getCommandGenerator()
157 .loadFile(pymolSessionFile);
158 executeCommand(cmd, false);
162 public boolean launchPymol()
164 if (pymolManager.isPymolLaunched())
169 boolean launched = pymolManager.launchPymol();
172 // start listening for PyMOL selections - how??
176 System.err.println("Failed to launch PyMOL!");
181 public void openFile(PDBEntry pe)
183 // todo : check not already open, remap / rename, etc
184 String file = pe.getFile();
185 StructureCommandI cmd = getCommandGenerator().loadFile(file);
188 * a second parameter sets the pdbid as the loaded PyMOL object name
190 String pdbId = pe.getId();
191 cmd.addParameter(pdbId);
193 executeCommand(cmd, false);
195 pymolObjects.put(file, pdbId);
196 if (!structureFiles.contains(file))
198 structureFiles.add(file);
200 if (getSsm() != null)
202 getSsm().addStructureViewerListener(this);
208 protected String getModelId(int pdbfnum, String file)
214 * Returns the file extension to use for a saved viewer session file (.pse)
217 * @see https://pymolwiki.org/index.php/Save
220 public String getSessionFileExtension()
226 public String getHelpURL()
228 return "https://pymolwiki.org/";
232 * Constructs and sends commands to set atom properties for visible Jalview
233 * features on residues mapped to structure
238 public int sendFeaturesToViewer(AlignmentViewPanel avp)
240 // todo pull up this and JalviewChimeraBinding variant
241 Map<String, Map<Object, AtomSpecModel>> featureValues = buildFeaturesMap(
243 List<StructureCommandI> commands = getCommandGenerator()
244 .setAttributes(featureValues);
245 executeCommands(commands, false, null);
246 return commands.size();