AtomSelectionPlugin.py 15.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

''' 
Created on Mar 30, 2015

:author: Eric C. Pellegrini
'''

import collections
import os

import wx
import wx.aui as wxaui

from MDANSE import LOGGER, REGISTRY
from MDANSE.Externals.pubsub import pub
from MDANSE.Framework.AtomSelectionParser import AtomSelectionParser, AtomSelectionParserError
from MDANSE.Framework.Plugins.DataPlugin import get_data_plugin 
from MDANSE.Framework.Widgets.UserDefinitionWidget import UserDefinitionsPlugin
from MDANSE.MolecularDynamics.Trajectory import sorted_atoms

class Query(object):
        
    def __init__(self):
        
        self._list = []
        
        self._parser = None

    def get_expression(self):

        s = []
                 
        for v in self._list:
            
            if isinstance(v, list):
                keyword,arguments = v
                arguments = ",".join([str(val) for val in arguments])
                s.append("%s %s" % (keyword,arguments))
            else:
                s.append(v)
        
        return "".join(s)
        
    @property
    def list(self):
        return self._list
    
    @property
    def parser(self):
        return self._parser

    def add_operator(self, operator):

        if operator == "(":
            if self._list:
                if self._list[-1] == ")" or isinstance(self._list[-1], list):
                    return
            self._list.append("(")
            
        elif operator == ")":
            if self._list:
                if self._list[-1] == ")" or isinstance(self._list[-1], list):
                    if self._list.count(")") < self._list.count("("):
                        self._list.append(")")
        
        elif operator == "not":
            if self._list:
                if self._list[-1] == ")":
                    return
            self._list.append(" not ")
            
        elif operator in ["and","or"]:       
            if self._list:
                if self._list[-1] == ")" or isinstance(self._list[-1], list):
                    self._list.append(" %s " % operator)
    
    def add_query(self, query):

        if not self._list:
            if query is not None:
                self._list.append(query)            
        else:
            
            keyword, values = query

            if isinstance(self._list[-1], list):

                if self._list[-1][0] == keyword:
                    if values:
                        self._list[-1] = query
                    else:
                        del self._list[-1]
                else:
                    self._list.extend([' or ', query])
                        
            elif isinstance(self._list[-1], basestring):
                if self._list[-1] in [' and ',' or ','(',' not ']:
                    self._list.append(query)                    

    def clear(self):
        self._list = []
        
    def is_empty(self):
        
        return len(self._list) == 0
            
    def parse(self):
        
        if self._parser is None:
            return []
                            
        try:
            expression = self.get_expression()
            selection = self._parser.parse(expression)
        except AtomSelectionParserError:
            return ("",[])
        else:
            return (expression,selection)
            
    def pop(self):
        return self._list.pop()
    
    def set_parser(self, parser):
        
        self._parser = parser
                            
class AtomSelectionPlugin(UserDefinitionsPlugin):
    
    type = 'atom_selection'
    
    label = "Atom selection"
    
    ancestor = ["molecular_viewer"]
    
    def __init__(self, parent, *args, **kwargs):

        self._query = Query()

        self._selectors = {}
                                        
        self._selection = []

        pub.subscribe(self.msg_select_atoms_from_viewer, 'msg_select_atoms_from_viewer')

        UserDefinitionsPlugin.__init__(self,parent,size=(800,500))
        
    def build_panel(self):
                                                
        self._mainPanel = wx.ScrolledWindow(self, wx.ID_ANY, size=self.GetSize())
        self._mainPanel.SetScrollbars(20,20,50,50)

        sizer = wx.BoxSizer(wx.VERTICAL)
        
        settingsPanel = wx.Panel(self._mainPanel,wx.ID_ANY)
                
        # Build the widgets used to build a selection from selection strings and operators
        self._queryPanel = wx.Panel(settingsPanel)
                              
        self.filterTree = wx.TreeCtrl(self._queryPanel, 1, wx.DefaultPosition, style=wx.TR_HIDE_ROOT|wx.TR_HAS_BUTTONS)
          
        root = self.filterTree.AddRoot("filters")
        filters = self.filterTree.AppendItem(root, "Filter by")
        selectors = REGISTRY["selector"].values()
        self.__filters = collections.OrderedDict()
        for selector in selectors:            
            if selector.section is not None:
                if self.__filters.has_key(selector.section):
                    self.__filters[selector.section].append(selector.type)
                else:
                    self.__filters[selector.section] = [selector.type]
         
        for section, subsections in sorted(self.__filters.items()):
            section_node = self.filterTree.AppendItem(filters, section)
            for subsection in sorted(subsections):
                self.filterTree.AppendItem(section_node,subsection)
         
        self.values = wx.ListBox(self._queryPanel, wx.ID_ANY, style = wx.LB_MULTIPLE|wx.LB_NEEDED_SB)
                                 
        leftBraceLinker = wx.Button(self._queryPanel, wx.ID_ANY, label = "(")        
        rightBraceLinker = wx.Button(self._queryPanel, wx.ID_ANY, label = ")")
        andLinker = wx.Button(self._queryPanel, wx.ID_ANY, label = "and")
        orLinker = wx.Button(self._queryPanel, wx.ID_ANY, label = "or")
        notLinker = wx.Button(self._queryPanel, wx.ID_ANY, label = "not")

        selectionStringSizer = wx.BoxSizer(wx.VERTICAL)
        keywordsValuesSizer = wx.BoxSizer(wx.HORIZONTAL)
        keywordsValuesSizer.Add(self.filterTree, 1, wx.ALIGN_CENTER_VERTICAL|wx.LEFT|wx.RIGHT|wx.EXPAND, 2)
        keywordsValuesSizer.Add(self.values, 3, wx.ALIGN_CENTER_VERTICAL|wx.LEFT|wx.RIGHT|wx.EXPAND, 2)
        linkersSizer = wx.BoxSizer(wx.HORIZONTAL)
        linkersSizer.Add(leftBraceLinker, 1, flag=wx.ALL|wx.EXPAND)
        linkersSizer.Add(rightBraceLinker, 1, flag=wx.ALL|wx.EXPAND)
        linkersSizer.Add(andLinker, 1, flag=wx.ALL|wx.EXPAND)
        linkersSizer.Add(orLinker, 1, flag=wx.ALL|wx.EXPAND)
        linkersSizer.Add(notLinker, 1, flag=wx.ALL|wx.EXPAND)
        selectionStringSizer.Add(keywordsValuesSizer,1,wx.ALL|wx.EXPAND,5)
        selectionStringSizer.Add(linkersSizer,0,wx.ALL|wx.EXPAND,5)
        self._queryPanel.SetSizer(selectionStringSizer)
                                         
        selectionSizer = wx.BoxSizer(wx.VERTICAL)
        selectionSizer.Add(self._queryPanel,1,wx.ALL|wx.EXPAND,5)
         
        settingsPanel.SetSizer(selectionSizer)

        # The widgets related to the selection being performed        
        selectionExpressionStaticBox = wx.StaticBox(self._mainPanel, wx.ID_ANY, label = "Selection")                
        selectionExpressionStaticBoxSizer = wx.StaticBoxSizer(selectionExpressionStaticBox, wx.HORIZONTAL)

        self.selectionTextCtrl = wx.TextCtrl(self._mainPanel, wx.ID_ANY, style = wx.TE_READONLY)
        clearButton = wx.Button(self._mainPanel, wx.ID_ANY, label="Clear")
        
        self._selectionExpressionSizer = wx.GridBagSizer(5,5)        
        self._selectionExpressionSizer.AddGrowableCol(0)
        self._selectionExpressionSizer.Add(self.selectionTextCtrl, pos=(0,0), span=(1,2),flag=wx.ALL|wx.EXPAND)
        self._selectionExpressionSizer.Add(clearButton, pos=(0,2), flag=wx.ALL)
        selectionExpressionStaticBoxSizer.Add(self._selectionExpressionSizer,1,wx.ALL|wx.EXPAND,5)
                    
        self._selectionSummary = wx.TextCtrl(self._mainPanel,wx.ID_ANY,style=wx.TE_LINEWRAP|wx.TE_MULTILINE|wx.TE_READONLY)  
                                                   
        sizer.Add(settingsPanel, 2, wx.ALL|wx.EXPAND, 5)
        sizer.Add(selectionExpressionStaticBoxSizer, 0, wx.ALL|wx.EXPAND, 5)
        sizer.Add(self._selectionSummary, 1, wx.ALL|wx.EXPAND, 5)

        self._mainPanel.SetSizer(sizer)

        self._mainSizer = wx.BoxSizer(wx.VERTICAL)                                                                              
        self._mainSizer.Add(self._mainPanel, 1, wx.EXPAND|wx.ALL, 5)        
        self.SetSizer(self._mainSizer)            
        
        self._mgr.AddPane(self._mainPanel, wxaui.AuiPaneInfo().DestroyOnClose().Center().Dock().CaptionVisible(False).CloseButton(False).BestSize(self.GetSize()))
        self._mgr.Update()
                                                                                      
        self.Bind(wx.EVT_TREE_SEL_CHANGED, self.on_display_keyword_values, self.filterTree)
        self.Bind(wx.EVT_LISTBOX, self.on_insert_keyword_values, self.values)
  
        self.Bind(wx.EVT_BUTTON, self.on_add_operator, leftBraceLinker)
        self.Bind(wx.EVT_BUTTON, self.on_add_operator, rightBraceLinker)
        self.Bind(wx.EVT_BUTTON, self.on_add_operator, orLinker)
        self.Bind(wx.EVT_BUTTON, self.on_add_operator, andLinker)
        self.Bind(wx.EVT_BUTTON, self.on_add_operator, notLinker)
            
        self.Bind(wx.EVT_BUTTON, self.on_clear, clearButton)
        
    def plug(self):
        
        self.parent.mgr.GetPane(self).Float().Dockable(False).CloseButton(True).BestSize((600,600))
        
        self.parent.mgr.Update()
        
        self.set_trajectory(self.dataproxy.data)
                                
    def set_trajectory(self,trajectory):

        self._trajectory = trajectory 

        self._query.set_parser(AtomSelectionParser(self._trajectory.universe))

        self._atoms = sorted_atoms(self._trajectory.universe)        

        self._target = os.path.basename(self._trajectory.filename)

    def display_selection_summary(self):
        
        self._selectionSummary.Clear()
        
        nSelectedAtoms = len(self._selection)
        
        self._selectionSummary.AppendText("Number of selected atoms: %d\n\n" % nSelectedAtoms)
        
        if nSelectedAtoms == 0:
            return
        
        self._selectionSummary.AppendText("List of selected atoms:\n")
        for idx in self._selection:
            self._selectionSummary.AppendText("\t%s (MMTK index: %d)\n" % (self._atoms[idx].fullName(),self._atoms[idx].index))

    def insert_keyword_values(self, keyword, values):

        self._query.add_query([keyword,values])
                
        self.set_selection()

        self.selectionTextCtrl.SetValue(self._query.get_expression())
        
        self.display_selection_summary()
        
    def close(self):
                
        pub.sendMessage('msg_clear_selection',plugin=self)                                
        
    def on_add_operator(self, event=None):

        operator = event.GetEventObject().GetLabel()

        self._query.add_operator(operator)        
                
        self.selectionTextCtrl.SetValue(self._query.get_expression())
        
        self.values.DeselectAll()

    def on_browse_python_script(self, event):
        
        dlg = wx.FileDialog(self, "Browse python selection script", defaultDir=os.getcwd(), wildcard="Python files (*.py)|*.py",style=wx.FD_OPEN|wx.FD_MULTIPLE)

        if dlg.ShowModal() == wx.ID_OK:        
            self.insert_keyword_values("pythonscript", dlg.GetPaths())
                                                                                                                                                           
    def on_clear(self, event=None):
        
        self._query.clear()

        self.selectionTextCtrl.Clear()
                
        self.values.DeselectAll()
        
        self._selectionSummary.Clear()

        pub.sendMessage('msg_clear_selection',plugin=self)                                
                
    def on_display_keyword_values(self, event=None):
        
        if self._trajectory is None:
            return
        
        item = event.GetItem()

        selectionFilter = self.filterTree.GetItemText(item)
        
        if not REGISTRY["selector"].has_key(selectionFilter):
            return
                                
        if not self._selectors.has_key(selectionFilter):
            self._selectors[selectionFilter] = [str(v) for v in REGISTRY["selector"][selectionFilter](self._trajectory.universe).choices]
        
        self.values.Set(self._selectors[selectionFilter])

    def on_insert_keyword_values(self, event):
                                
        item = self.filterTree.GetSelection()
                
        keyword = str(self.filterTree.GetItemText(item))
                                
        values = [str(self.values.GetString(v)) for v in self.values.GetSelections()]

        self.insert_keyword_values(keyword, values)

    def on_mode_selection(self, event=None): 
        
        self.selectedMode = str(event.GetEventObject().GetLabelText()).lower()
        
        self.keywords.DeselectAll()
        self.values.DeselectAll()

    def msg_select_atoms_from_viewer(self, message):

        dataPlugin,selection = message
        
        if dataPlugin != get_data_plugin(self):
            return
        
        self._query.clear()
        
        self.insert_keyword_values("atom_picked", selection)
                
    def set_selection(self):

        _, self._selection = self._query.parse()
                
        pub.sendMessage("msg_set_selection", plugin=self)
    
    @property
    def selection(self):
        
        return self._selection
    
    def validate(self):

        if not self._selection:
            LOGGER("The current selection is empty", "error", ["dialog"])
            return None
                
        return {'indexes':self._selection}