chart_stat_controler.py 24.6 KB
Newer Older
VIGNET Pierre's avatar
VIGNET Pierre committed
1
2
3
## Filename    : chart_stat_controler.py
## Author(s)   : Michel Le Borgne
## Created     : 08/2012
4
5
## Revision    :
## Source      :
VIGNET Pierre's avatar
VIGNET Pierre committed
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
##
## Copyright 2012 : IRISA/IRSET
##
## This library is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License  published
## by the Free Software Foundation; either version 2.1 of the License, or
## 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.  The software and
## documentation provided here under is on an "as is" basis, and IRISA has
## no obligations to provide maintenance, support, updates, enhancements
## or modifications.
## In no event shall IRISA be liable to any party for direct, indirect,
## special, incidental or consequential damages, including lost profits,
## arising out of the use of this software and its documentation, even if
## IRISA have been advised of the possibility of such damage.  See
## the GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this library; if not, write to the Free Software Foundation,
## Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
##
## The original code contained here was initially developed by:
##
##     Michel Le Borgne.
##     IRISA
##     Symbiose team
##     IRISA  Campus de Beaulieu
36
37
##     35042 RENNES Cedex, FRANCE
##
VIGNET Pierre's avatar
VIGNET Pierre committed
38
39
40
41
42
43
44
##
## Contributor(s): Geoffroy Andrieux
##
"""
Collection of widgets and controlers for static analysis
"""
import gtk
45
from cadbiom.models.guard_transitions.analyser.static_analysis import StaticAnalyzer
46
47
48
from cadbiom_gui.gt_gui.utils.reporter import CompilReporter

import pkg_resources
VIGNET Pierre's avatar
VIGNET Pierre committed
49
50
51
52
53
54
55
56
57
58
59
60
61

import sys
sys.setrecursionlimit(10000)
BG_COLOR = "#FFFF99"

class ChartStatControler(object):
    """
    Control of static analysis
    """
    def __init__(self, emvc, reporter):
        self.edit_mvc = emvc
        self.reporter = reporter                   # display error messages
        # graphical interface
62
63
64
65
66
        template = pkg_resources.resource_filename(
            __name__,
            "../chart_glade/static_analysis.glade"
        )
        self.wtree = gtk.glade.XML(template)
VIGNET Pierre's avatar
VIGNET Pierre committed
67
68
69
70
71
72
73
74
75
76
        self.main_window = self.wtree.get_widget("window1")
        title = "Static analysis - " + self.edit_mvc.model.name
        self.main_window.set_title(title)
        if (self.main_window):
            self.main_window.connect("destroy", self.on_destroy)
        self.main_window.set_position(gtk.WIN_POS_CENTER)
        #self.main_window.set_keep_above(True)
        color = gtk.gdk.color_parse(BG_COLOR)
        self.main_window.modify_bg(gtk.STATE_NORMAL, color)
        self.main_window.resize(150, 300)
77

VIGNET Pierre's avatar
VIGNET Pierre committed
78
79
80
81
82
83
84
        # completion button
#        button = self.wtree.get_widget("completionbutton")
#        button.connect("clicked", self.on_completion)

        # frontier button
        button = self.wtree.get_widget("frontierbutton")
        button.connect("clicked", self.on_frontier)
85

VIGNET Pierre's avatar
VIGNET Pierre committed
86
87
88
        #  basal activated genes button
        button = self.wtree.get_widget("bgenebutton")
        button.connect("clicked", self.on_b_gene)
89

VIGNET Pierre's avatar
VIGNET Pierre committed
90
91
92
        # dependance graph button
        button = self.wtree.get_widget("depgraphbutton")
        button.connect("clicked", self.on_depgraph)
93

VIGNET Pierre's avatar
VIGNET Pierre committed
94
95
96
        # stat button
        button = self.wtree.get_widget("statbutton")
        button.connect("clicked", self.on_stat)
97

VIGNET Pierre's avatar
VIGNET Pierre committed
98
99
100
        # register as auxiliary window
        self.edit_mvc.win_register(self)
        self.subwin = []
101

VIGNET Pierre's avatar
VIGNET Pierre committed
102
103
104
        # display
        self.main_window.show_all()

105

VIGNET Pierre's avatar
VIGNET Pierre committed
106
107
108
109
110
#    def on_completion(self, widget):
#        """
#        TODO
#        """
#        pass
111

VIGNET Pierre's avatar
VIGNET Pierre committed
112
113
114
115
116
117
118
119
120
121
    def on_frontier(self, widget):
        """
        Compute connected components which are on the frontier
        """
        reporter = CompilReporter()
        stan = StaticAnalyzer(reporter)
        stan.build_from_chart_model(self.edit_mvc.model)
        # errors??
        lscc = stan.get_frontier_scc()
        display_w =  SCCWindow(lscc, self.edit_mvc, self.reporter, self)
122
123


VIGNET Pierre's avatar
VIGNET Pierre committed
124
125
126
127
128
129
130
131
132
    def on_b_gene(self, widget):
        """
        Compute basal activated genes
        """
        reporter = CompilReporter()
        stan = StaticAnalyzer(reporter)
        stan.build_from_chart_model(self.edit_mvc.model)
        lwbag = stan.get_why_basal_genes()
        display_w =  BAGWindow(lwbag, self.edit_mvc, self.reporter, self)
133

VIGNET Pierre's avatar
VIGNET Pierre committed
134
135
136
137
138
    def on_depgraph(self, widget):
        """
        Computation and export of the dependance graph
        """
        DependencyGraphWindow(self.edit_mvc, self.reporter, self)
139

VIGNET Pierre's avatar
VIGNET Pierre committed
140
141
    def on_stat(self, widget):
        """
142
        Fill window with model informations.
VIGNET Pierre's avatar
VIGNET Pierre committed
143
144
        """
        reporter = CompilReporter()
145
        # get stats from StaticAnalyzer
VIGNET Pierre's avatar
VIGNET Pierre committed
146
147
        stan = StaticAnalyzer(reporter)
        stan.build_from_chart_model(self.edit_mvc.model)
148
149
        ststat = stan.get_statistics()
        display_w = STATWindow(ststat, self.edit_mvc, self.reporter, self)
150

VIGNET Pierre's avatar
VIGNET Pierre committed
151
152
153
154
155
    def win_register(self, win):
        """
        register a sub wundow
        """
        self.subwin.append(win)
156

VIGNET Pierre's avatar
VIGNET Pierre committed
157
158
159
160
161
    def win_remove(self, win):
        """
        unregister a sub window
        """
        self.subwin.remove(win)
162

VIGNET Pierre's avatar
VIGNET Pierre committed
163
164
    def clean_subwin(self):
        """
165
        sub windows management
VIGNET Pierre's avatar
VIGNET Pierre committed
166
167
168
169
        """
        for win in self.subwin:
            win.destroy()
        self.subwin = []
170

VIGNET Pierre's avatar
VIGNET Pierre committed
171
172
173
174
175
176
177
    def on_destroy(self, widget):
        """
        Standard call back
        """
        self.clean_subwin()
        if self.main_window:
            self.main_window.destroy()
178

VIGNET Pierre's avatar
VIGNET Pierre committed
179
180
181
182
183
    def destroy(self):
        """
        Standard call back
        """
        self.on_destroy(None)
184
185


VIGNET Pierre's avatar
VIGNET Pierre committed
186
187
188
189
190
191
192
193
class  DependencyGraphWindow(object):
    """
    Dialog for dependency graph analysis and export
    """
    def __init__(self, emvc, reporter, parent):
        self.emvc = emvc
        self.reporter = reporter
        self.parent = parent
194

VIGNET Pierre's avatar
VIGNET Pierre committed
195
        # window creation
196
197
198
199
200
        template = pkg_resources.resource_filename(
            __name__,
            "../chart_glade/dependency_graph.glade"
        )
        self.wtree = gtk.glade.XML(template)
VIGNET Pierre's avatar
VIGNET Pierre committed
201
202
203
204
205
206
207
208
209
        self.main_window = self.wtree.get_widget("window1")
        self.main_window.set_title("Dependency graph - "+self.emvc.model.name)

        self.main_window.set_resizable(True)
        screen_h = gtk.gdk.screen_height()
        screen_h = int(screen_h * 0.20)
        color = gtk.gdk.color_parse(BG_COLOR)
        self.main_window.modify_bg(gtk.STATE_NORMAL, color)
        self.main_window.set_size_request(300, screen_h)
210

VIGNET Pierre's avatar
VIGNET Pierre committed
211
212
        if (self.main_window):
            self.main_window.connect("destroy", self.on_destroy)
213

VIGNET Pierre's avatar
VIGNET Pierre committed
214
        self.main_window.set_position(gtk.WIN_POS_CENTER)
215

VIGNET Pierre's avatar
VIGNET Pierre committed
216
217
218
219
        # transition graph radio button
        rbutton = self.wtree.get_widget("transGraph_rb")
        rb_name = rbutton.get_name()
        rbutton.connect("toggled", self.rb_callback, rb_name)
220

VIGNET Pierre's avatar
VIGNET Pierre committed
221
222
223
224
        # dependency graph radio button
        rbutton = self.wtree.get_widget("dpGraph_rb")
        rb_name = rbutton.get_name()
        rbutton.connect("toggled", self.rb_callback, rb_name)
225

VIGNET Pierre's avatar
VIGNET Pierre committed
226
227
228
229
        # full dependency graph radio button
        rbutton = self.wtree.get_widget("fullDpGraph_rb")
        rb_name = rbutton.get_name()
        rbutton.connect("toggled", self.rb_callback, rb_name)
230

VIGNET Pierre's avatar
VIGNET Pierre committed
231
232
233
        # DOT button
        button = self.wtree.get_widget("dotButton")
        button.connect("clicked", self.on_save_dot)
234

VIGNET Pierre's avatar
VIGNET Pierre committed
235
236
237
        # graphml button
        button = self.wtree.get_widget("graphmlButton")
        button.connect("clicked", self.on_save_graphml)
238

VIGNET Pierre's avatar
VIGNET Pierre committed
239
240
241
        # CREATE STATIC ANALYZER AND LOAD MODEL
        self.stan = StaticAnalyzer(reporter)
        self.stan.build_from_chart_model(self.emvc.model)
242

VIGNET Pierre's avatar
VIGNET Pierre committed
243
        self.graph_type = "transGraph_rb"
244

VIGNET Pierre's avatar
VIGNET Pierre committed
245
246
        # register as auxiliary window
        self.parent.win_register(self.main_window)
247

VIGNET Pierre's avatar
VIGNET Pierre committed
248
249
        # display
        self.main_window.show_all()
250

VIGNET Pierre's avatar
VIGNET Pierre committed
251
252
253
254
255
    def rb_callback(self, widget, name):
        """
        ???
        """
        self.graph_type = name
256

VIGNET Pierre's avatar
VIGNET Pierre committed
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
    def on_save_dot(self, widget):
        """
        compute a dependency graph and open a window to save as dot file
        """
        if self.graph_type == "transGraph_rb":
            graph = self.stan.make_transition_dg()
        elif self.graph_type == "dpGraph_rb":
            graph = self.stan.make_dependence_dg(True)
        else :
            graph = self.stan.make_full_dependence_dg(True)
        ch_opt = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE,
                   gtk.RESPONSE_OK)
        choice = gtk.FileChooserDialog("Save as dot file",
                                       None,
                                       gtk.FILE_CHOOSER_ACTION_SAVE, ch_opt)
        choice.set_default_response(gtk.RESPONSE_OK)
273

VIGNET Pierre's avatar
VIGNET Pierre committed
274
275
276
277
278
        #add a filter to see only dot files (*.dot)
        gfilter = gtk.FileFilter()
        gfilter.set_name("dot files")
        gfilter.add_pattern("*.dot")
        choice.add_filter(gfilter)
279

VIGNET Pierre's avatar
VIGNET Pierre committed
280
281
282
283
284
        #add a filter to see all
        no_filter = gtk.FileFilter()
        no_filter.set_name("all")
        no_filter.add_pattern("*")
        choice.add_filter(no_filter)
285

VIGNET Pierre's avatar
VIGNET Pierre committed
286
287
288
289
290
291
292
        response = choice.run()
        if response == gtk.RESPONSE_OK:
            self.stan.export_2_dot(graph, choice.get_filename())
        elif response == gtk.RESPONSE_CANCEL:
            pass
            #print 'Closed, no files selected'
        choice.destroy()
293

VIGNET Pierre's avatar
VIGNET Pierre committed
294
295
296
297
298
299
300
301
302
303
    def on_save_graphml(self, widget):
        """
        compute a dependency graph and open a window to save as graphml file
        """
        if self.graph_type == "transGraph_rb":
            graph = self.stan.make_transition_dg()
        elif self.graph_type == "dpGraph_rb":
            graph = self.stan.make_dependence_dg(True)
        else :
            graph = self.stan.make_full_dependence_dg(True)
304

VIGNET Pierre's avatar
VIGNET Pierre committed
305
306
        ch_opt = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE,
                   gtk.RESPONSE_OK)
307
        choice = gtk.FileChooserDialog("Save as graphml file", None,
VIGNET Pierre's avatar
VIGNET Pierre committed
308
309
                                       gtk.FILE_CHOOSER_ACTION_SAVE, ch_opt)
        choice.set_default_response(gtk.RESPONSE_OK)
310

VIGNET Pierre's avatar
VIGNET Pierre committed
311
312
313
314
315
        #add a filter to see only graphml files (*.graphml)
        filter = gtk.FileFilter()
        filter.set_name("graphml files")
        filter.add_pattern("*.graphml")
        choice.add_filter(filter)
316

VIGNET Pierre's avatar
VIGNET Pierre committed
317
318
319
320
321
        #add a filter to see all
        no_filter = gtk.FileFilter()
        no_filter.set_name("all")
        no_filter.add_pattern("*")
        choice.add_filter(no_filter)
322

VIGNET Pierre's avatar
VIGNET Pierre committed
323
324
325
326
327
328
329
        response = choice.run()
        if response == gtk.RESPONSE_OK:
            self.stan.export_2_graphml(graph, choice.get_filename())
        elif response == gtk.RESPONSE_CANCEL:
            pass
            #print 'Closed, no files selected'
        choice.destroy()
330

VIGNET Pierre's avatar
VIGNET Pierre committed
331
332
333
334
335
336
337
    def on_destroy(self, widget):
        """
        Standard call back
        """
        if self.main_window:
            self.parent.win_remove(self.main_window)
            self.main_window.destroy()
338

VIGNET Pierre's avatar
VIGNET Pierre committed
339
340
341
342
343
344
    def destroy(self):
        """
        For linked windows
        """
        self.on_destroy(None)

345
346


VIGNET Pierre's avatar
VIGNET Pierre committed
347
348
349
350
351
352
353
354
class SCCWindow(object):
    """
    Class used for displaying frontier isolated components
    """
    def __init__(self, l_fsol, emvc, reporter, parent):
        """
        @param l_fsol: list<list<string>>
        @param emvc: edit_mvc
355
356
        @param reporter: Classical reporter
        @param parent: parent widget
VIGNET Pierre's avatar
VIGNET Pierre committed
357
358
359
360
361
362
363
364
365
366
        """
        if not l_fsol:
            return
        self.emvc = emvc
        self.reporter = reporter

        self.compt = 0
        self.choice = 0
        self.textview_list = []
        self.l_solutions = l_fsol
367

VIGNET Pierre's avatar
VIGNET Pierre committed
368
        # window creation
369
370
371
372
373
        template = pkg_resources.resource_filename(
            __name__,
            "../chart_glade/checker_solutions.glade"
        )
        self.wtree = gtk.glade.XML(template)
VIGNET Pierre's avatar
VIGNET Pierre committed
374
375
376
        self.window = self.wtree.get_widget("window1")

        self.window.set_resizable(True)
377
378
        self.window.set_title("Isolated Strongly Connected Components (SCC) - "\
                              + self.emvc.model.name)
VIGNET Pierre's avatar
VIGNET Pierre committed
379
380
381
382
383
        height = gtk.gdk.screen_height()
        height = int(height * 0.30)
        color = gtk.gdk.color_parse(BG_COLOR)
        self.window.modify_bg(gtk.STATE_NORMAL, color)
        self.window.set_size_request(700, height)
384
385


VIGNET Pierre's avatar
VIGNET Pierre committed
386
387
388
        # button save choice
        button = self.wtree.get_widget("but_sv_choice")
        button.connect("clicked", self.on_save, False)
389

VIGNET Pierre's avatar
VIGNET Pierre committed
390
391
392
393
394
        # button save all
        button = self.wtree.get_widget("but_sv_all")
        button.connect("clicked", self.on_save, True)
        if len(self.l_solutions) == 1:
            button.set_sensitive(False)
395

VIGNET Pierre's avatar
VIGNET Pierre committed
396
397
        # display
        self.display_info()
398

VIGNET Pierre's avatar
VIGNET Pierre committed
399
400
401
402
        # remove bottom buttons
        hbbox =  self.wtree.get_widget("hbuttonbox2")
        vbox = self.wtree.get_widget("vbox1")
        vbox.remove(hbbox)
403

VIGNET Pierre's avatar
VIGNET Pierre committed
404
405
406
407

        # register
        parent.win_register(self.window)
        self.parent = parent
408

VIGNET Pierre's avatar
VIGNET Pierre committed
409
410
411
        # display
        self.window.show_all()
        #gtk.main()
412

VIGNET Pierre's avatar
VIGNET Pierre committed
413
414
415
416
417
418
419
420
    def on_destroy(self, widget):
        """
        when leaving the window
        """
        self.clean_subwin()
        self.parent.win_remove(self)
        if self.window:
            self.window.destroy()
421

VIGNET Pierre's avatar
VIGNET Pierre committed
422
423
424
425
426
    def destroy(self):
        """
        when leaving the parent window
        """
        self.on_destroy(None)
427
428


VIGNET Pierre's avatar
VIGNET Pierre committed
429
430
431
432
    def display_info(self):
        """
        Display info on isolated SCC
        """
433
434
        SCC_found = (len(self.l_solutions[0]) != 0)

VIGNET Pierre's avatar
VIGNET Pierre committed
435
436
        # info label
        info = self.wtree.get_widget("label_nbsol")
437
438
439
440
441
442
443
444
        if not SCC_found:
            info.set_text("No isolated Strongly Connected Components found.")
            return
        else:
            info.set_text(str(len(self.l_solutions)) + \
                          " isolated Strongly Connected Components found.\n"
                          "(Only 1 node of each of them requires an input "
                          "transition with a StartNode)")
445

VIGNET Pierre's avatar
VIGNET Pierre committed
446
447
448
449
450
        self.frame_sol =  self.wtree.get_widget("frame_sol")
        # button frame
        vbox = gtk.VBox(False, 0)
        self.frame_sol.add(vbox)
        vbox.show()
451

VIGNET Pierre's avatar
VIGNET Pierre committed
452
453
        scroll = gtk.ScrolledWindow()
        vbox.pack_start(scroll)
454

VIGNET Pierre's avatar
VIGNET Pierre committed
455
456
457
458
        vbox2 = gtk.VBox()
        scroll.add_with_viewport(vbox2)
        scroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.button_group = None
459

VIGNET Pierre's avatar
VIGNET Pierre committed
460
        frame = self.build_button_frame(self.l_solutions[0])
461
462
        vbox2.pack_start(frame)

VIGNET Pierre's avatar
VIGNET Pierre committed
463
464
465
        for sol in self.l_solutions[1:]:
            hsep = gtk.HSeparator()
            vbox2.pack_start(hsep, False, False)
466

VIGNET Pierre's avatar
VIGNET Pierre committed
467
            frame = self.build_button_frame(sol, self.button_group)
468
            vbox2.pack_start(frame)
VIGNET Pierre's avatar
VIGNET Pierre committed
469
470
471
472
473
474


    def build_button_frame(self, state_list, group = None):
        """
        Display info on each isolated SCC
        """
475
476
477
478
479
        template = pkg_resources.resource_filename(
            __name__,
            "../chart_glade/button_frame.glade"
        )
        wtree = gtk.glade.XML(template)
VIGNET Pierre's avatar
VIGNET Pierre committed
480
481
482
        wid = wtree.get_widget("button_frame")
        frame = wid.get_child()
        wid.remove(frame)
483

VIGNET Pierre's avatar
VIGNET Pierre committed
484
485
486
487
        #textview
        tname = 'tw'+str(self.compt)
        text = wtree.get_widget("textview")
        text.set_name(tname)
488

VIGNET Pierre's avatar
VIGNET Pierre committed
489
490
491
492
493
494
495
496
497
        text_buffer = text.get_buffer()
        str_state = '\n'
        for state in state_list :
            str_state += state + '\t'
        str_state = str_state + '\n'
        text_buffer.set_text(str_state)
        text.set_editable(False)

        self.textview_list.append(state_list)
498

VIGNET Pierre's avatar
VIGNET Pierre committed
499
500
501
502
        #radio button
        rname = 'rb' + str(self.compt)
        rbut = wtree.get_widget("rButton")
        rbut.set_name(rname)
503

VIGNET Pierre's avatar
VIGNET Pierre committed
504
505
506
507
508
        if group:
            rbut.set_group(group)
        else :
            self.button_group = rbut
            rbut.set_active(True)
509

VIGNET Pierre's avatar
VIGNET Pierre committed
510
511
512
        rbut.connect("toggled", self.rb_callback, self.compt)
        self.compt += 1
        return frame
513

VIGNET Pierre's avatar
VIGNET Pierre committed
514
515
516
517
518
    def rb_callback(self, widget, id_frame):
        """
        radio button call_back
        """
        self.choice = id_frame
519

VIGNET Pierre's avatar
VIGNET Pierre committed
520
521
522
523
524
525
526
    def on_save(self, widget, all_sol=True):
        """
        open a window to save as xml file
        @param all_sol : boolean - True if we take all solutions
        """
        choice = gtk.FileChooserDialog("Save solution", None ,
                                       gtk.FILE_CHOOSER_ACTION_SAVE,
527
                                       ( gtk.STOCK_CANCEL,
VIGNET Pierre's avatar
VIGNET Pierre committed
528
                                         gtk.RESPONSE_CANCEL,
529
                                         gtk.STOCK_SAVE,
VIGNET Pierre's avatar
VIGNET Pierre committed
530
531
                                         gtk.RESPONSE_OK))
        choice.set_default_response(gtk.RESPONSE_OK)
532

VIGNET Pierre's avatar
VIGNET Pierre committed
533
534
535
536
537
        #add a filter to see only xml files for biochart (*.txt)
        ffilter = gtk.FileFilter()
        ffilter.set_name("txt files")
        ffilter.add_pattern("*.txt")
        choice.add_filter(ffilter)
538

VIGNET Pierre's avatar
VIGNET Pierre committed
539
540
541
542
543
        #add a filter to see all
        no_filter = gtk.FileFilter()
        no_filter.set_name("all")
        no_filter.add_pattern("*")
        choice.add_filter(no_filter)
544

VIGNET Pierre's avatar
VIGNET Pierre committed
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
        response = choice.run()
        if response == gtk.RESPONSE_OK:
            self.create_solution_file(choice.get_filename(), all_sol)
        elif response == gtk.RESPONSE_CANCEL:
            pass
            #print 'Closed, no files selected'
        choice.destroy()

    def create_solution_file(self, sol_file, all_bool):
        """
        Print solutions in a file
        """
        if all_bool:
            scc_places = self.l_solutions
        else :
            scc_places = [self.l_solutions[self.choice]]

        sfile = open(sol_file,'w')
        for i in range(len(scc_places)):
            spla = scc_places[i]
            for elemt in spla :
                sfile.write(str(elemt)+'\t')

            sfile.write('\n-------------------------------------------\n\n\n')
        sfile.close()
570
571

    # sub windows management
VIGNET Pierre's avatar
VIGNET Pierre committed
572
573
574
575
576
577
578
    def win_register(self, win):
        """
        sub windows management
        """
        if win.__class__ == ChartSimulControler:
            self.simul = win
        self.aux_win.append(win)
579

VIGNET Pierre's avatar
VIGNET Pierre committed
580
581
    def win_remove(self, win):
        """
582
        sub windows management
VIGNET Pierre's avatar
VIGNET Pierre committed
583
584
585
586
587
        """
        if win in self.aux_win:
            self.aux_win.remove(win)
        if win == self.simul:
            self.simul = None
588

VIGNET Pierre's avatar
VIGNET Pierre committed
589
590
    def clean_subwin(self):
        """
591
        sub windows management
VIGNET Pierre's avatar
VIGNET Pierre committed
592
593
594
595
596
597
598
599
600
601
602
        """
        for win in self.aux_win:
            win.destroy()
        self.aux_win = []


class BAGWindow(SCCWindow):
    def __init__(self, l_fsol, emvc, reporter, parent):
        """
        @param l_fsol: list<list<string>>
        @param emvc: edit_mvc
603
604
        @param reporter: Classical reporter
        @param parent: parent widget
VIGNET Pierre's avatar
VIGNET Pierre committed
605
606
607
608
        """
        if l_fsol:
            SCCWindow.__init__(self, l_fsol, emvc, reporter, parent)
            self.window.set_title("Basal activated genes - "+self.emvc.model.name)
609
610


VIGNET Pierre's avatar
VIGNET Pierre committed
611
612
613
614
615
616
617
    def display_info(self):
        """
        ???
        """
        # info label
        info = self.wtree.get_widget("label_nbsol")
        info.set_text(str(len(self.l_solutions))+" basal activated genes found")
618

VIGNET Pierre's avatar
VIGNET Pierre committed
619
620
621
622
623
        self.frame_sol =  self.wtree.get_widget("frame_sol")
        # button frame
        vbox = gtk.VBox(False, 0)
        self.frame_sol.add(vbox)
        vbox.show()
624

VIGNET Pierre's avatar
VIGNET Pierre committed
625
626
        scroll = gtk.ScrolledWindow()
        vbox.pack_start(scroll)
627

VIGNET Pierre's avatar
VIGNET Pierre committed
628
629
630
631
        vbox2 = gtk.VBox()
        scroll.add_with_viewport(vbox2)
        scroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.button_group = None
632

VIGNET Pierre's avatar
VIGNET Pierre committed
633
        frame = self.build_button_frame(self.l_solutions[0])
634
635
        vbox2.pack_start(frame)

VIGNET Pierre's avatar
VIGNET Pierre committed
636
637
638
        for sol in self.l_solutions[1:]:
            hsep = gtk.HSeparator()
            vbox2.pack_start(hsep, False, False)
639

VIGNET Pierre's avatar
VIGNET Pierre committed
640
            frame = self.build_button_frame(sol, self.button_group)
641
            vbox2.pack_start(frame)
VIGNET Pierre's avatar
VIGNET Pierre committed
642
643
644
645
646
647


    def build_button_frame(self, pair, group = None):
        """
        ???
        """
648
649
650
651
652
        template = pkg_resources.resource_filename(
            __name__,
            "../chart_glade/button_frame.glade"
        )
        wtree = gtk.glade.XML(template)
VIGNET Pierre's avatar
VIGNET Pierre committed
653
654
655
        wid = wtree.get_widget("button_frame")
        frame = wid.get_child()
        wid.remove(frame)
656

VIGNET Pierre's avatar
VIGNET Pierre committed
657
658
659
660
        #textview
        tname = 'tw'+str(self.compt)
        text = wtree.get_widget("textview")
        text.set_name(tname)
661

VIGNET Pierre's avatar
VIGNET Pierre committed
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
        text_buffer = text.get_buffer()
        mess = '\nGene:'
        mess = mess + '\t' + pair[0]
        why = pair[1]
        mess = mess + "\n" + "Essential Inhib.: "
        for act in why[0]:
            mess = mess + "\t" + act
        mess = mess + "\n" + "Dominant Inhib.: "
        for act in why[1]:
            mess = mess + "\t" + act
        mess = mess + "\n" + "Essential Activ.: "
        for act in why[2]:
            mess = mess + "\t" + act
        mess = mess + "\n" + "Dominant Activ.: "
        for act in why[3]:
            mess = mess + "\t" + act
        mess = mess + '\n'
        text_buffer.set_text(mess)
        text.set_editable(False)
681

VIGNET Pierre's avatar
VIGNET Pierre committed
682
683
684
685
        #radio button
        rname = 'rb' + str(self.compt)
        rbut = wtree.get_widget("rButton")
        rbut.set_name(rname)
686

VIGNET Pierre's avatar
VIGNET Pierre committed
687
688
689
690
691
        if group:
            rbut.set_group(group)
        else :
            self.button_group = rbut
            rbut.set_active(True)
692

VIGNET Pierre's avatar
VIGNET Pierre committed
693
694
695
        rbut.connect("toggled", self.rb_callback, self.compt)
        self.compt += 1
        return frame
696
697


VIGNET Pierre's avatar
VIGNET Pierre committed
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
    def create_solution_file(self, sol_file, all_bool):
        """
        Print solutions in a file
        """
        if all_bool:
            scc_places = self.l_solutions
        else :
            scc_places = [self.l_solutions[self.choice]]

        sfile = open(sol_file,'w')
        for i in range(len(scc_places)):
            pair = scc_places[i]
            mess = '\nGene:'
            mess = mess + '\t' + pair[0]
            why = pair[1]
            mess = mess + "\n" + "Isolated Inhib.:"
            for act in why[0]:
                mess = mess + "\t" + act
            mess = mess + "\n" + "Dominant Inhib.:"
            for act in why[1]:
                mess = mess + "\t" + act
            mess = mess + "\n" + "Essential Activ.:"
            for act in why[2]:
                mess = mess + "\t" + act
            mess = mess + "\n" + "Dominant Activ.:"
            for act in why[3]:
                mess = mess + "\t" + act
            mess = mess + '\n'
            sfile.write(mess)
            sfile.write('\n-------------------------------------------\n\n\n')
        sfile.close()
729
730


VIGNET Pierre's avatar
VIGNET Pierre committed
731
732
733
734
735
736
737
738
class STATWindow(BAGWindow):
    """
    Class for displaying statistics
    """
    def __init__(self, stat, emvc, reporter, parent):
        """
        @param l_fsol: list<list<string>>
        @param emvc: edit_mvc
739
740
        @param reporter: Classical reporter
        @param parent: parent widget
VIGNET Pierre's avatar
VIGNET Pierre committed
741
742
        """
        BAGWindow.__init__(self, stat, emvc, reporter, parent)
743

VIGNET Pierre's avatar
VIGNET Pierre committed
744
745
746
747
        self.window.set_title("Statistics - "+self.emvc.model.name)
        button = self.wtree.get_widget("but_sv_choice")
        hbbox = self.wtree.get_widget("hbuttonbox1")
        hbbox.remove(button)
748

VIGNET Pierre's avatar
VIGNET Pierre committed
749
750
751
752
753
754
    def display_info(self):
        """
        ???
        """
        # info label
        info = self.wtree.get_widget("label_nbsol")
755
756
757
758
759
        info.set_text(
            "Model Informations\n"
            "(Please note that some elements may have more than one locations; "
            "take a look at /tmp/logs/cadbiom*)"
        )
760

VIGNET Pierre's avatar
VIGNET Pierre committed
761
762
763
764
765
        self.frame_sol =  self.wtree.get_widget("frame_sol")
        # button frame
        vbox = gtk.VBox(False, 0)
        self.frame_sol.add(vbox)
        vbox.show()
766

VIGNET Pierre's avatar
VIGNET Pierre committed
767
768
        scroll = gtk.ScrolledWindow()
        vbox.pack_start(scroll)
769

VIGNET Pierre's avatar
VIGNET Pierre committed
770
771
772
773
        vbox2 = gtk.VBox()
        scroll.add_with_viewport(vbox2)
        scroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.button_group = None
774

VIGNET Pierre's avatar
VIGNET Pierre committed
775
        frame = self.build_button_frame(self.l_solutions)
776
777
        vbox2.pack_start(frame)

VIGNET Pierre's avatar
VIGNET Pierre committed
778
779
780
781
    def build_button_frame(self, stat_str, group = None):
        """
        ???
        """
782
783
784
785
786
        template = pkg_resources.resource_filename(
            __name__,
            "../chart_glade/button_frame.glade"
        )
        wtree = gtk.glade.XML(template)
VIGNET Pierre's avatar
VIGNET Pierre committed
787
788
789
        wid = wtree.get_widget("button_frame")
        frame = wid.get_child()
        wid.remove(frame)
790

VIGNET Pierre's avatar
VIGNET Pierre committed
791
792
        #textview
        text = wtree.get_widget("textview")
793

VIGNET Pierre's avatar
VIGNET Pierre committed
794
        text_buffer = text.get_buffer()
795
        mess = str(stat_str) + '\n'
VIGNET Pierre's avatar
VIGNET Pierre committed
796
797
        text_buffer.set_text(mess)
        text.set_editable(False)
798

VIGNET Pierre's avatar
VIGNET Pierre committed
799
800
801
802
803
804
805
        #radio button removed
        rname = 'rb' + str(self.compt)
        rbut = wtree.get_widget("rButton")
        hbox = wtree.get_widget("hbox")
        hbox.remove(rbut)

        return frame
806
807


VIGNET Pierre's avatar
VIGNET Pierre committed
808
809
810
811
812
813
814
815
816
817
    def create_solution_file(self, sol_file, all_bool):
        """
        Print solutions in a file
        """
        sfile = open(sol_file,'w')
        mess = self.l_solutions
        mess = mess + '\n'
        sfile.write(mess)
        sfile.write('\n-------------------------------------------\n\n\n')
        sfile.close()