6787c8ab2ccee1a4381c4d9a1ee1459b49fda8b2
[jalview.git] / src / jalview / gui / PymolBindingModel.java
1 package jalview.gui;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
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.StructureCommandI;
16 import jalview.structure.StructureSelectionManager;
17 import jalview.structures.models.AAStructureBindingModel;
18
19 public class PymolBindingModel extends AAStructureBindingModel
20 {
21   private PymolManager pymolManager;
22
23   private Thread pymolMonitor;
24
25   /*
26    * full paths to structure files opened in PyMOL
27    */
28   List<String> structureFiles = new ArrayList<>();
29
30   /*
31    * lookup from file path to PyMOL object name
32    */
33   Map<String, String> pymolObjects = new HashMap<>();
34
35   /**
36    * Constructor
37    * 
38    * @param viewer
39    * @param ssm
40    * @param pdbentry
41    * @param sequenceIs
42    */
43   public PymolBindingModel(StructureViewerBase viewer,
44           StructureSelectionManager ssm, PDBEntry[] pdbentry,
45           SequenceI[][] sequenceIs)
46   {
47     super(ssm, pdbentry, sequenceIs, null);
48     pymolManager = new PymolManager();
49     setStructureCommands(new PymolCommands());
50     setViewer(viewer);
51   }
52
53   @Override
54   public String[] getStructureFiles()
55   {
56     return structureFiles.toArray(new String[structureFiles.size()]);
57   }
58
59   @Override
60   public void highlightAtoms(List<AtomSpec> atoms)
61   {
62   }
63
64   @Override
65   public SequenceRenderer getSequenceRenderer(AlignmentViewPanel alignment)
66   {
67     // pull up?
68     return new SequenceRenderer(alignment.getAlignViewport());
69   }
70
71   @Override
72   protected List<String> executeCommand(StructureCommandI command,
73           boolean getReply)
74   {
75     // System.out.println(command.toString()); // debug
76     return pymolManager.sendCommand(command, getReply);
77   }
78
79   @Override
80   protected String getModelIdForFile(String file)
81   {
82     return pymolObjects.containsKey(file) ? pymolObjects.get(file) : "";
83   }
84
85   @Override
86   protected ViewerType getViewerType()
87   {
88     return ViewerType.PYMOL;
89   }
90
91   public boolean isPymolRunning()
92   {
93     return pymolManager.isPymolLaunched();
94   }
95
96   public void closeViewer(boolean closePymol)
97   {
98     getSsm().removeStructureViewerListener(this, this.getStructureFiles());
99     if (closePymol)
100     {
101       pymolManager.exitPymol();
102     }
103     pymolManager = null;
104
105     if (pymolMonitor != null)
106     {
107       pymolMonitor.interrupt();
108     }
109     releaseUIResources();
110   }
111
112   public boolean openSession(String pymolSessionFile)
113   {
114     StructureCommandI cmd = getCommandGenerator()
115             .loadFile(pymolSessionFile);
116     executeCommand(cmd, false);
117     return true;
118   }
119
120   public boolean launchPymol()
121   {
122     if (pymolManager.isPymolLaunched())
123     {
124       return true;
125     }
126
127     boolean launched = pymolManager.launchPymol();
128     if (launched)
129     {
130       // start listening for PyMOL selections - how??
131     }
132     else
133     {
134       System.err.println("Failed to launch PyMOL!");
135     }
136     return launched;
137   }
138
139   public void openFile(PDBEntry pe)
140   {
141     // todo : check not already open, remap / rename, etc
142     String file = pe.getFile();
143     StructureCommandI cmd = getCommandGenerator().loadFile(file);
144
145     /*
146      * a second parameter sets the pdbid as the loaded PyMOL object name
147      */
148     String pdbId = pe.getId();
149     cmd.addParameter(pdbId);
150
151     executeCommand(cmd, false);
152
153     pymolObjects.put(file, pdbId);
154     if (!structureFiles.contains(file))
155     {
156       structureFiles.add(file);
157     }
158     if (getSsm() != null)
159     {
160       getSsm().addStructureViewerListener(this);
161     }
162
163   }
164
165   @Override
166   protected String getModelId(int pdbfnum, String file)
167   {
168     return file;
169   }
170
171   /**
172    * Returns the file extension to use for a saved viewer session file (.pse)
173    * 
174    * @return
175    * @see https://pymolwiki.org/index.php/Save
176    */
177   @Override
178   public String getSessionFileExtension()
179   {
180     return ".pse";
181   }
182
183 }