af4afb0d31a281d4a2aa8a906709e345a4eab607
[jalview.git] / src / jalview / gui / PymolBindingModel.java
1 package jalview.gui;
2
3 import jalview.api.AlignmentViewPanel;
4 import jalview.datamodel.PDBEntry;
5 import jalview.datamodel.SequenceI;
6 import jalview.ext.pymol.PymolCommands;
7 import jalview.ext.pymol.PymolManager;
8 import jalview.gui.StructureViewer.ViewerType;
9 import jalview.structure.AtomSpec;
10 import jalview.structure.StructureCommandI;
11 import jalview.structure.StructureSelectionManager;
12 import jalview.structures.models.AAStructureBindingModel;
13
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
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     // if (this.pymolListener != null)
104     // {
105     // pymolListener.shutdown();
106     // pymolListener = null;
107     // }
108     pymolManager = null;
109
110     if (pymolMonitor != null)
111     {
112       pymolMonitor.interrupt();
113     }
114     releaseUIResources();
115   }
116
117   public boolean openSession(String pymolSessionFile)
118   {
119     StructureCommandI cmd = getCommandGenerator()
120             .loadFile(pymolSessionFile);
121     executeCommand(cmd, false);
122     return true;
123   }
124
125   public boolean launchPymol()
126   {
127     if (pymolManager.isPymolLaunched())
128     {
129       return true;
130     }
131
132     boolean launched = pymolManager.launchPymol();
133     if (launched)
134     {
135       // start listening for PyMOL selections - how??
136     }
137     else
138     {
139       System.err.println("Failed to launch PyMOL!");
140     }
141     return launched;
142   }
143
144   public void openFile(PDBEntry pe)
145   {
146     // todo : check not already open, remap / rename, etc
147     String file = pe.getFile();
148     StructureCommandI cmd = getCommandGenerator().loadFile(file);
149
150     /*
151      * a second parameter sets the pdbid as the loaded PyMOL object name
152      */
153     String pdbId = pe.getId();
154     cmd.addParameter(pdbId);
155
156     executeCommand(cmd, false);
157
158     pymolObjects.put(file, pdbId);
159     if (!structureFiles.contains(file))
160     {
161       structureFiles.add(file);
162     }
163     if (getSsm() != null)
164     {
165       getSsm().addStructureViewerListener(this);
166     }
167
168   }
169
170   @Override
171   protected String getModelId(int pdbfnum, String file)
172   {
173     return file;
174   }
175
176 }