3 import java.util.ArrayList;
4 import java.util.HashMap;
8 import jalview.api.AlignmentViewPanel;
9 import jalview.bin.Cache;
10 import jalview.datamodel.PDBEntry;
11 import jalview.datamodel.SequenceI;
12 import jalview.ext.pymol.PymolCommands;
13 import jalview.ext.pymol.PymolManager;
14 import jalview.gui.StructureViewer.ViewerType;
15 import jalview.structure.AtomSpec;
16 import jalview.structure.AtomSpecModel;
17 import jalview.structure.StructureCommand;
18 import jalview.structure.StructureCommandI;
19 import jalview.structure.StructureSelectionManager;
20 import jalview.structures.models.AAStructureBindingModel;
22 public class PymolBindingModel extends AAStructureBindingModel
25 * format for labels shown on structures when mousing over sequence;
26 * see https://pymolwiki.org/index.php/Label#examples
27 * left not final so customisable e.g. with a Groovy script
29 private static String LABEL_FORMAT = "\"%s %s\" % (resn,resi)";
31 private PymolManager pymolManager;
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 avp)
108 return new SequenceRenderer(avp.getAlignViewport());
112 protected List<String> executeCommand(StructureCommandI command,
115 // System.out.println(command.toString()); // debug
116 return pymolManager.sendCommand(command, getReply);
120 protected String getModelIdForFile(String file)
122 return pymolObjects.containsKey(file) ? pymolObjects.get(file) : "";
126 protected ViewerType getViewerType()
128 return ViewerType.PYMOL;
132 public boolean isViewerRunning()
134 return pymolManager.isPymolLaunched();
138 public void closeViewer(boolean closePymol)
140 super.closeViewer(closePymol);
144 public boolean launchPymol()
146 if (pymolManager.isPymolLaunched())
151 Process pymol = pymolManager.launchPymol();
154 // start listening for PyMOL selections - how??
155 startExternalViewerMonitor(pymol);
159 Cache.log.error("Failed to launch PyMOL!");
161 return pymol != null;
164 public void openFile(PDBEntry pe)
166 // todo : check not already open, remap / rename, etc
167 String file = pe.getFile();
168 StructureCommandI cmd = getCommandGenerator().loadFile(file);
171 * a second parameter sets the pdbid as the loaded PyMOL object name
173 String pdbId = pe.getId();
175 String safePDBId = java.net.URLEncoder.encode(pdbId,"UTF-8");
176 pdbId = safePDBId.replace('%', '_');
177 pdbId = pdbId.replace("-", "__");
178 char fc = pdbId.charAt(0);
179 // put an 's' before any numerics
180 if (fc>='0' && fc<='9')
184 // pdbId.replace('-', 0)
185 } catch (Exception x)
187 Cache.log.error("Unxpected encoding exception for '"+pdbId+"'",x);
189 cmd.addParameter(pdbId);
191 executeCommand(cmd, false);
193 pymolObjects.put(file, pdbId);
194 if (!structureFiles.contains(file))
196 structureFiles.add(file);
198 if (getSsm() != null)
200 getSsm().addStructureViewerListener(this);
206 protected String getModelId(int pdbfnum, String file)
212 * Returns the file extension to use for a saved viewer session file (.pse)
215 * @see https://pymolwiki.org/index.php/Save
218 public String getSessionFileExtension()
224 public String getHelpURL()
226 return "https://pymolwiki.org/";
230 * Constructs and sends commands to set atom properties for visible Jalview
231 * features on residues mapped to structure
236 public int sendFeaturesToViewer(AlignmentViewPanel avp)
238 // todo pull up this and JalviewChimeraBinding variant
239 Map<String, Map<Object, AtomSpecModel>> featureValues = buildFeaturesMap(
241 List<StructureCommandI> commands = getCommandGenerator()
242 .setAttributes(featureValues);
243 executeCommands(commands, false, null);
244 return commands.size();