2006-08-08 10:48:55 +02:00
|
|
|
import pygtk
|
|
|
|
import gtk
|
|
|
|
import gtk.gdk
|
|
|
|
import gtk.glade
|
|
|
|
import gnome
|
|
|
|
import gnome.ui
|
|
|
|
import gobject
|
2008-02-01 12:09:10 +01:00
|
|
|
import scipy
|
2006-08-08 10:48:55 +02:00
|
|
|
|
2007-07-26 14:35:59 +02:00
|
|
|
import logger, dataset, main
|
2007-02-21 13:41:37 +01:00
|
|
|
import annotations
|
|
|
|
from lib import hypergeom
|
|
|
|
|
|
|
|
|
2006-08-30 01:57:21 +02:00
|
|
|
class SimpleMenu(gtk.Menu):
|
|
|
|
def __init__(self):
|
|
|
|
gtk.Menu.__init__(self)
|
|
|
|
|
2007-02-23 11:46:04 +01:00
|
|
|
def add_simple_item(self, title, function, *args):
|
2006-08-30 01:57:21 +02:00
|
|
|
item = gtk.MenuItem(title)
|
2007-02-23 11:46:04 +01:00
|
|
|
item.connect('activate', function, *args)
|
2006-08-30 01:57:21 +02:00
|
|
|
self.append(item)
|
|
|
|
item.show()
|
|
|
|
|
2006-09-08 20:25:03 +02:00
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
class IdListController:
|
|
|
|
"""Controller class for the identifier list."""
|
|
|
|
|
|
|
|
def __init__(self, idlist):
|
|
|
|
self._idlist = idlist
|
2007-08-22 15:41:04 +02:00
|
|
|
self._idlist.get_selection().set_mode(gtk.SELECTION_MULTIPLE)
|
|
|
|
self._idlist.set_rubber_banding(True)
|
2007-02-21 01:00:26 +01:00
|
|
|
|
|
|
|
# dimname: current_annotation_name
|
|
|
|
self._annotation = {}
|
|
|
|
|
|
|
|
# current dimension
|
|
|
|
self._dimension = None
|
|
|
|
|
|
|
|
# id, annotation
|
|
|
|
self._idstore = gtk.ListStore(gobject.TYPE_STRING,
|
2008-02-01 12:09:10 +01:00
|
|
|
gobject.TYPE_STRING)
|
2007-08-22 15:41:04 +02:00
|
|
|
self._idstore.set_sort_func(0, self._numeric_compare)
|
2006-09-08 20:25:03 +02:00
|
|
|
|
2007-02-21 17:39:04 +01:00
|
|
|
# Annotation tree column
|
|
|
|
self._annotation_column = None
|
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
## Set up identifier list
|
|
|
|
idlist.set_model(self._idstore)
|
2006-09-08 20:25:03 +02:00
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
renderer = gtk.CellRendererText()
|
|
|
|
dim_column = gtk.TreeViewColumn('Identifiers', renderer, text=0)
|
2007-08-22 15:41:04 +02:00
|
|
|
dim_column.set_sort_indicator(True)
|
|
|
|
dim_column.set_sort_column_id(0)
|
|
|
|
dim_column.set_sort_order(gtk.SORT_ASCENDING)
|
2007-02-21 01:00:26 +01:00
|
|
|
idlist.insert_column(dim_column, 0)
|
|
|
|
idlist.connect('button-press-event', self._button_pressed)
|
2006-09-08 20:25:03 +02:00
|
|
|
|
2008-02-01 12:09:10 +01:00
|
|
|
## Enable dropping
|
|
|
|
idlist.drag_dest_set(gtk.DEST_DEFAULT_ALL,
|
|
|
|
[("GTK_TREE_MODEL_ROW", gtk.TARGET_SAME_APP, 7)],
|
|
|
|
gtk.gdk.ACTION_LINK)
|
|
|
|
idlist.connect('drag-data-received', self._drag_data_received)
|
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
## Set up identifier list context menu
|
|
|
|
menu = self._menu = SimpleMenu()
|
|
|
|
menu.add_simple_item('Import...', self._on_import_list)
|
|
|
|
menu.add_simple_item('Export...', self._on_export_list)
|
2007-08-22 15:41:04 +02:00
|
|
|
menu.add_simple_item('Add to selection', self._on_make_selection)
|
2007-02-21 01:00:26 +01:00
|
|
|
item = gtk.MenuItem('Show annotations')
|
|
|
|
menu.append(item)
|
|
|
|
item.show()
|
|
|
|
self._menu_ann = item
|
|
|
|
|
|
|
|
##
|
|
|
|
## Public interface
|
|
|
|
##
|
|
|
|
def set_dimension(self, dimname):
|
|
|
|
"""Set dimension"""
|
|
|
|
if dimname == self._dimension:
|
|
|
|
return
|
2006-09-08 20:25:03 +02:00
|
|
|
|
2007-12-11 14:47:29 +01:00
|
|
|
self._dimension = dimname
|
2007-02-21 17:39:04 +01:00
|
|
|
self.set_annotation(self._annotation.get(dimname, None))
|
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
if not self._annotation.has_key(dimname):
|
|
|
|
self._annotation[dimname] = None
|
|
|
|
|
|
|
|
def set_annotation(self, annotation):
|
2007-02-21 12:08:20 +01:00
|
|
|
"""Set the displayed annotation to annotation. If annotation is None,
|
|
|
|
the annotation column is hidden. Otherwise the annotation column is
|
|
|
|
shown and filled with values from the given annotation field."""
|
2007-02-21 01:00:26 +01:00
|
|
|
|
2007-02-21 17:39:04 +01:00
|
|
|
if annotation == None:
|
|
|
|
if self._annotation_column != None:
|
|
|
|
self._idlist.remove_column(self._annotation_column)
|
|
|
|
self._annotation_column = None
|
2007-02-21 01:00:26 +01:00
|
|
|
else:
|
2007-02-21 17:39:04 +01:00
|
|
|
|
|
|
|
idlist = [x[0] for x in self._idstore]
|
|
|
|
annlist = annotations.get_dim_annotations(self._dimension,
|
|
|
|
annotation,
|
|
|
|
idlist)
|
|
|
|
|
|
|
|
for i, x in enumerate(self._idstore):
|
|
|
|
x[1] = annlist[i]
|
|
|
|
|
|
|
|
if self._annotation_column == None:
|
|
|
|
renderer = gtk.CellRendererText()
|
|
|
|
col = gtk.TreeViewColumn(annotation, renderer, text=1)
|
2007-08-22 15:41:04 +02:00
|
|
|
col.set_sort_indicator(True)
|
|
|
|
col.set_sort_column_id(1)
|
|
|
|
col.set_sort_order(gtk.SORT_ASCENDING)
|
2007-02-21 17:39:04 +01:00
|
|
|
self._idlist.append_column(col)
|
|
|
|
self._annotation_column = col
|
|
|
|
self._annotation_column.set_title(annotation)
|
|
|
|
|
|
|
|
self._annotation[self._dimension] = annotation
|
2006-09-08 20:25:03 +02:00
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
def set_selection(self, selection):
|
|
|
|
"""Set the selection to be displayed.
|
|
|
|
The selection is not stored, the values are copied into the TreeStore"""
|
|
|
|
self._idstore.clear()
|
2007-02-26 19:02:34 +01:00
|
|
|
|
|
|
|
# Return if no selection
|
|
|
|
if selection == None:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Otherwise show selection, possibly with annotations.
|
2007-08-22 15:41:04 +02:00
|
|
|
#id_list = list(selection[self._dimension])
|
2007-02-21 01:00:26 +01:00
|
|
|
idlist = list(selection[self._dimension])
|
|
|
|
if self._annotation[self._dimension] != None:
|
|
|
|
annlist = annotations.get_dim_annotations(self._dimension,
|
2007-02-21 12:08:20 +01:00
|
|
|
self._annotation[self._dimension],
|
|
|
|
idlist)
|
2007-02-21 01:00:26 +01:00
|
|
|
for id, ann in zip(idlist, annlist):
|
|
|
|
self._idstore.append((id, ann))
|
|
|
|
else:
|
|
|
|
for e in idlist:
|
|
|
|
self._idstore.append((e, None))
|
|
|
|
|
|
|
|
##
|
|
|
|
## Private interface
|
|
|
|
##
|
|
|
|
def _update_annotations_menu(self):
|
2007-02-21 12:08:20 +01:00
|
|
|
"""Updates the annotations menu with the available annotations for the
|
2007-02-21 01:00:26 +01:00
|
|
|
current dim."""
|
|
|
|
|
|
|
|
dim_h = annotations.get_dim_handler(self._dimension)
|
|
|
|
if not dim_h:
|
2007-03-14 18:26:40 +01:00
|
|
|
print "set_sensitive(False)"
|
2007-02-21 01:00:26 +01:00
|
|
|
self._menu_ann.set_sensitive(False)
|
|
|
|
else:
|
|
|
|
annotations_menu = gtk.Menu()
|
2007-03-14 18:26:40 +01:00
|
|
|
print "set_sensitive(True)"
|
2007-02-21 01:00:26 +01:00
|
|
|
self._menu_ann.set_sensitive(True)
|
2007-02-21 12:08:20 +01:00
|
|
|
dh = annotations.get_dim_handler(self._dimension)
|
|
|
|
ann_names = dh.get_annotation_names()
|
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
for ann in ann_names:
|
|
|
|
item = gtk.MenuItem(ann)
|
|
|
|
item.connect('activate', self._on_annotation_activated, ann)
|
|
|
|
annotations_menu.append(item)
|
|
|
|
item.show()
|
2006-09-08 20:25:03 +02:00
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
self._menu_ann.set_submenu(annotations_menu)
|
2007-08-22 15:41:04 +02:00
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
|
2007-02-26 15:35:31 +01:00
|
|
|
def import_annotation_file(self):
|
|
|
|
"""Pops up a file dialog and ask the user to select the annotation
|
|
|
|
file to be loaded. Only one file can be selected. The file is loaded
|
|
|
|
into a annotations.AnnotationDictHandler object"""
|
|
|
|
|
|
|
|
dialog = gtk.FileChooserDialog('Load annotations')
|
|
|
|
dialog.set_action(gtk.FILE_CHOOSER_ACTION_OPEN)
|
|
|
|
dialog.add_buttons(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
|
|
|
|
gtk.STOCK_OPEN, gtk.RESPONSE_OK)
|
|
|
|
dialog.set_select_multiple(True)
|
|
|
|
retval = dialog.run()
|
|
|
|
if retval in [gtk.RESPONSE_CANCEL, gtk.RESPONSE_DELETE_EVENT]:
|
|
|
|
pass
|
|
|
|
elif retval == gtk.RESPONSE_OK:
|
|
|
|
for filename in dialog.get_filenames():
|
|
|
|
annotations.read_annotations_file(filename)
|
|
|
|
else:
|
|
|
|
print "unknown; ", retval
|
|
|
|
dialog.destroy()
|
|
|
|
|
2008-02-01 12:09:10 +01:00
|
|
|
def set_rank(self, ds):
|
|
|
|
print "Set rank."
|
|
|
|
|
|
|
|
ra = scipy.sum(ds.asarray(), 1)
|
|
|
|
ranks = {}
|
|
|
|
dim = ds.get_dim_name()[0]
|
|
|
|
for key, value in ds[dim].items():
|
|
|
|
ranks[key] = ra[value]
|
|
|
|
|
|
|
|
ann_h = annotations.get_dim_handler(self._dimension)
|
|
|
|
if ann_h is None:
|
|
|
|
ann_h = annotations.DictAnnotationHandler()
|
|
|
|
annotations.set_dim_handler(self._dimension, ann_h)
|
|
|
|
|
|
|
|
ann_h.add_annotations('Rank', ranks)
|
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
##
|
|
|
|
## GTK Callbacks
|
|
|
|
##
|
2007-08-22 15:41:04 +02:00
|
|
|
|
|
|
|
def _numeric_compare(self, treemodel, iter1, iter2):
|
|
|
|
column = treemodel.get_sort_column_id()[0]
|
|
|
|
|
|
|
|
item1 = treemodel.get_value(iter1, column)
|
|
|
|
item2 = treemodel.get_value(iter2, column)
|
|
|
|
|
|
|
|
try:
|
|
|
|
item1 = float(item1)
|
|
|
|
item2 = float(item2)
|
|
|
|
except:
|
|
|
|
logger.log("notice", "Could not convert to float: %s, %s" %(item1, item2))
|
|
|
|
|
2007-09-20 18:12:58 +02:00
|
|
|
return cmp(item1, item2)
|
2007-08-22 15:41:04 +02:00
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
def _popup_menu(self, *rest):
|
|
|
|
self._update_annotations_menu()
|
|
|
|
self._menu.popup(None, None, None, 0, 0)
|
2006-09-08 20:25:03 +02:00
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
def _on_annotation_activated(self, menuitem, annotation):
|
|
|
|
self.set_annotation(annotation)
|
|
|
|
|
|
|
|
def _button_pressed(self, widget, event):
|
|
|
|
if event.button == 3:
|
|
|
|
self._update_annotations_menu()
|
|
|
|
self._menu.popup(None, None, None, event.button, event.time)
|
|
|
|
|
|
|
|
def _on_export_list(self, menuitem):
|
|
|
|
print "export stuff"
|
|
|
|
|
|
|
|
def _on_import_list(self, menuitem):
|
2007-02-26 15:35:31 +01:00
|
|
|
self.import_annotation_file()
|
2007-08-22 15:41:04 +02:00
|
|
|
|
|
|
|
def _on_make_selection(self, menuitem):
|
|
|
|
selection = self._idlist.get_selection()
|
|
|
|
model, paths = selection.get_selected_rows()
|
|
|
|
if paths==None: return
|
|
|
|
iters = [self._idstore.get_iter(p) for p in paths]
|
|
|
|
ids = [self._idstore.get_value(i, 0) for i in iters]
|
|
|
|
main.project.set_selection(self._dimension, ids)
|
2007-02-26 15:35:31 +01:00
|
|
|
|
2008-02-01 12:09:10 +01:00
|
|
|
def _drag_data_received(self, widget, drag_context, x, y,
|
|
|
|
selection, info, timestamp):
|
|
|
|
print "drag_data_received"
|
|
|
|
|
|
|
|
treestore, path = selection.tree_get_row_drag_data()
|
|
|
|
i = treestore.get_iter(path)
|
|
|
|
obj = treestore.get_value(i, 2)
|
|
|
|
if isinstance(obj, dataset.Dataset):
|
|
|
|
print "is dataset"
|
|
|
|
if self._dimension in obj.get_dim_name():
|
|
|
|
print "has correct dimensions"
|
|
|
|
self.set_rank(obj)
|
|
|
|
widget.emit_stop_by_name('drag-data-received')
|
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
|
|
|
|
class SelectionListController:
|
|
|
|
def __init__(self, seltree, idlist_controller):
|
|
|
|
self._seltree = seltree
|
|
|
|
self._sel_stores = {}
|
2007-02-23 11:46:04 +01:00
|
|
|
self._detail_cols = []
|
2007-02-21 01:00:26 +01:00
|
|
|
self._dimension = None
|
|
|
|
self._idlist_controller = idlist_controller
|
2007-02-23 11:46:04 +01:00
|
|
|
self._details_on = False
|
|
|
|
|
|
|
|
# Selection column
|
2006-09-08 20:25:03 +02:00
|
|
|
renderer = gtk.CellRendererText()
|
2006-09-15 16:17:17 +02:00
|
|
|
sel_column = gtk.TreeViewColumn('Selection', renderer, text=0)
|
2007-03-14 17:34:50 +01:00
|
|
|
sel_column.set_resizable(True)
|
|
|
|
sel_column.set_max_width(200)
|
2006-09-08 20:25:03 +02:00
|
|
|
seltree.insert_column(sel_column, 0)
|
|
|
|
|
2007-02-23 11:46:04 +01:00
|
|
|
# Detail columns
|
|
|
|
cols = [('In CS', 3), ('All', 4), ('Rank', 5)]
|
2007-03-14 17:34:50 +01:00
|
|
|
for name, store_col_num in cols:
|
|
|
|
col = gtk.TreeViewColumn(name, renderer, text=store_col_num)
|
|
|
|
col.set_sort_indicator(True)
|
|
|
|
col.set_sort_column_id(store_col_num)
|
|
|
|
col.set_sort_order(gtk.SORT_ASCENDING)
|
|
|
|
|
|
|
|
self._detail_cols.append(col)
|
2007-02-21 13:41:37 +01:00
|
|
|
# Signals
|
2007-02-21 01:00:26 +01:00
|
|
|
seltree.connect('row-activated', self._on_row_activated)
|
|
|
|
seltree.connect('cursor-changed', self._on_cursor_changed)
|
2007-02-21 13:41:37 +01:00
|
|
|
seltree.connect('button-press-event', self._on_button_pressed)
|
2006-09-08 20:25:03 +02:00
|
|
|
seltree.drag_dest_set(gtk.DEST_DEFAULT_ALL,
|
|
|
|
[("GTK_TREE_MODEL_ROW", gtk.TARGET_SAME_APP, 7)],
|
|
|
|
gtk.gdk.ACTION_LINK)
|
2006-09-08 21:42:32 +02:00
|
|
|
|
|
|
|
seltree.connect('drag-data-received', self._drag_data_received)
|
2006-09-08 20:25:03 +02:00
|
|
|
|
2007-02-21 13:41:37 +01:00
|
|
|
# Selections context menu
|
|
|
|
self._seltree_menu = SimpleMenu()
|
2007-02-26 15:35:31 +01:00
|
|
|
self._seltree_menu.add_simple_item('Sort by selection',
|
|
|
|
self._on_seltree_sort)
|
|
|
|
self._seltree_menu.add_simple_item('Show details',
|
|
|
|
self._enable_details, True)
|
|
|
|
self._seltree_menu.add_simple_item('Hide details',
|
|
|
|
self._enable_details, False)
|
|
|
|
|
2007-02-21 13:41:37 +01:00
|
|
|
#
|
|
|
|
# Public interface
|
|
|
|
#
|
2007-02-26 19:02:34 +01:00
|
|
|
def activate(self):
|
|
|
|
self._seltree.set_cursor((0,))
|
|
|
|
|
2007-02-21 13:41:37 +01:00
|
|
|
def set_project(self, project):
|
|
|
|
"""Dependency injection."""
|
2007-07-26 14:35:59 +02:00
|
|
|
main.project.add_selection_observer(self)
|
2007-02-21 13:41:37 +01:00
|
|
|
|
2007-02-21 12:43:00 +01:00
|
|
|
def set_dimlist_controller(self, dimlist_controller):
|
2007-02-26 15:35:31 +01:00
|
|
|
"""Dependency injection of the dimension list controller."""
|
2007-02-21 12:43:00 +01:00
|
|
|
self._dimlist_controller = dimlist_controller
|
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
def set_dimension(self, dim):
|
2007-02-26 15:35:31 +01:00
|
|
|
"""Set the current dimension, changing the model of the treeview
|
|
|
|
to match dim. After this the current dimension of the identifier list
|
|
|
|
is updated."""
|
2007-02-21 01:00:26 +01:00
|
|
|
self._ensure_selection_store(dim)
|
|
|
|
self._seltree.set_model(self._sel_stores[dim])
|
|
|
|
self._idlist_controller.set_dimension(dim)
|
|
|
|
self._dimension = dim
|
|
|
|
|
|
|
|
def selection_changed(self, dimname, selection):
|
|
|
|
"""Callback function from Project."""
|
|
|
|
for dim in selection.dims():
|
|
|
|
self._ensure_selection_store(dim)
|
|
|
|
store = self._sel_stores[dim]
|
|
|
|
|
|
|
|
if not self._get_current_selection_iter(selection, dim):
|
2007-02-21 13:41:37 +01:00
|
|
|
n = len(selection[dim])
|
2007-02-23 11:46:04 +01:00
|
|
|
values = (selection.title, selection, dim, n, n, 0)
|
2007-02-21 01:00:26 +01:00
|
|
|
store.insert_after(None, None, values)
|
2007-02-23 12:12:03 +01:00
|
|
|
else:
|
|
|
|
# update size of current selection
|
|
|
|
for row in store:
|
|
|
|
if row[1]==selection:
|
|
|
|
row[3] = row[4] = len(selection[dim])
|
2007-02-21 01:00:26 +01:00
|
|
|
|
2007-02-26 19:02:34 +01:00
|
|
|
path = self._seltree.get_cursor()
|
|
|
|
if path and self._sel_stores.has_key(self._dimension):
|
|
|
|
it = self._sel_stores[self._dimension].get_iter(path[0])
|
|
|
|
sel = self._sel_stores[self._dimension].get_value(it, 1)
|
|
|
|
self._idlist_controller.set_selection(sel)
|
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
def add_dataset(self, dataset):
|
2007-02-26 15:35:31 +01:00
|
|
|
"""Converts a CategoryDataset to Selection objects and adds it to
|
|
|
|
the selection tree. The name of the dataset will be the parent
|
|
|
|
node in the tree, and the identifers along the first axis will
|
|
|
|
be added as the names of the subselections."""
|
2007-02-21 13:41:37 +01:00
|
|
|
dim_name = dataset.get_dim_name(0)
|
|
|
|
self._ensure_selection_store(dim_name)
|
|
|
|
store = self._sel_stores[dim_name]
|
2007-02-21 01:00:26 +01:00
|
|
|
di = self._get_dataset_iter(dataset)
|
|
|
|
if not di:
|
2007-02-21 13:41:37 +01:00
|
|
|
n_tot = dataset.shape[0]
|
2007-07-26 14:35:59 +02:00
|
|
|
selection = main.project.get_selection().get(dim_name)
|
2007-02-21 13:41:37 +01:00
|
|
|
ds_idents = dataset.get_identifiers(dim_name)
|
|
|
|
n_cs = len(selection.intersection(ds_idents))
|
|
|
|
values = (dataset.get_name(), dataset, dim_name, n_cs, n_tot, 2)
|
2007-02-21 01:00:26 +01:00
|
|
|
|
|
|
|
i = store.insert_after(None, None, values)
|
|
|
|
for selection in dataset.as_selections():
|
2007-02-21 13:41:37 +01:00
|
|
|
n_sel = len(selection[dim_name])
|
|
|
|
values = (selection.title, selection, dim_name, 0, n_sel, 0)
|
2007-02-21 01:00:26 +01:00
|
|
|
store.insert_after(i, None, values)
|
2006-09-08 20:25:03 +02:00
|
|
|
|
2007-02-23 11:46:04 +01:00
|
|
|
#
|
|
|
|
# Private interface
|
|
|
|
#
|
2007-02-21 01:00:26 +01:00
|
|
|
def _add_selection_store(self, dim):
|
|
|
|
"""Add a new gtk.TreeStore for the selections on a dimension."""
|
|
|
|
# Create new store
|
2007-02-21 13:41:37 +01:00
|
|
|
# Two types of lines, one for CategoryDatasets and one for
|
|
|
|
# Selections. The elements are title, link to dataset or selection,
|
|
|
|
# name of dimension, num. members in selection, num. in
|
|
|
|
# intersection with current selection and the rank of selection.
|
2007-02-21 01:00:26 +01:00
|
|
|
store = gtk.TreeStore(gobject.TYPE_STRING,
|
|
|
|
gobject.TYPE_PYOBJECT,
|
2007-02-21 13:41:37 +01:00
|
|
|
gobject.TYPE_STRING,
|
|
|
|
gobject.TYPE_INT,
|
|
|
|
gobject.TYPE_INT,
|
|
|
|
gobject.TYPE_FLOAT)
|
2007-02-21 01:00:26 +01:00
|
|
|
|
|
|
|
# Set selection store for this dimension
|
|
|
|
self._sel_stores[dim] = store
|
2006-09-08 20:25:03 +02:00
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
def _ensure_selection_store(self, dim):
|
|
|
|
"""Ensure that the object has a gtk.TreeStore for the given dimension"""
|
|
|
|
# Do not overwrite existing stores
|
|
|
|
if self._sel_stores.has_key(dim):
|
|
|
|
return
|
|
|
|
self._add_selection_store(dim)
|
|
|
|
|
|
|
|
def _get_dataset_iter(self, ds):
|
2006-09-08 20:25:03 +02:00
|
|
|
"""Returns the iterator to the selection tree row containing a
|
|
|
|
given dataset."""
|
2007-02-21 01:00:26 +01:00
|
|
|
|
|
|
|
store = self._sel_stores[ds.get_dim_name(0)]
|
|
|
|
|
|
|
|
i = store.get_iter_first()
|
2006-09-08 20:25:03 +02:00
|
|
|
while i:
|
2007-02-21 01:00:26 +01:00
|
|
|
if store.get_value(i, 1) == ds:
|
2006-09-08 20:25:03 +02:00
|
|
|
return i
|
2007-02-21 01:00:26 +01:00
|
|
|
i = store.iter_next(i)
|
2006-09-08 20:25:03 +02:00
|
|
|
return None
|
2006-09-08 21:42:32 +02:00
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
def _get_current_selection_iter(self, selection, dimension):
|
|
|
|
if not self._sel_stores.has_key(dimension):
|
|
|
|
return None
|
|
|
|
|
|
|
|
store = self._sel_stores[dimension]
|
|
|
|
|
|
|
|
i = store.get_iter_first()
|
2006-09-08 21:42:32 +02:00
|
|
|
while i:
|
2007-02-21 01:00:26 +01:00
|
|
|
if store.get_value(i, 1) == selection:
|
|
|
|
if store.get_value(i, 2) == dimension:
|
2006-09-08 21:42:32 +02:00
|
|
|
return i
|
2007-02-21 01:00:26 +01:00
|
|
|
i = store.iter_next(i)
|
2006-09-08 21:42:32 +02:00
|
|
|
return None
|
2007-02-21 13:41:37 +01:00
|
|
|
|
|
|
|
def _sort_selections(self, dataset):
|
|
|
|
"""Ranks selections by intersection with current selection.
|
|
|
|
Ranks determined by the hypergeometric distribution.
|
|
|
|
"""
|
|
|
|
dim_name = dataset.get_dim_name(0)
|
|
|
|
sel_store = self._sel_stores[dim_name]
|
2007-07-26 14:35:59 +02:00
|
|
|
selection_obj = main.project.get_selection()
|
2007-02-21 13:41:37 +01:00
|
|
|
current_selection = selection_obj.get(dim_name)
|
|
|
|
if current_selection==None: return
|
|
|
|
|
|
|
|
pvals = hypergeom.gene_hypergeo_test(current_selection, dataset)
|
|
|
|
|
|
|
|
for row in sel_store:
|
|
|
|
if row[1]==dataset:
|
|
|
|
for child in row.iterchildren():
|
|
|
|
name = child[0]
|
|
|
|
child[3] = pvals[name][0]
|
|
|
|
child[4] = pvals[name][1]
|
|
|
|
child[5] = pvals[name][2]
|
|
|
|
|
|
|
|
sel_store.set_sort_column_id(5, gtk.SORT_ASCENDING)
|
|
|
|
|
|
|
|
#
|
|
|
|
# GTK callbacks
|
|
|
|
#
|
2007-02-23 11:46:04 +01:00
|
|
|
def _enable_details(self, widget, bool):
|
|
|
|
if self._details_on == bool : return
|
|
|
|
self._details_on = bool
|
|
|
|
if bool==True:
|
|
|
|
for col in self._detail_cols:
|
|
|
|
self._seltree.insert_column(col, -1)
|
|
|
|
else:
|
|
|
|
for col in self._detail_cols:
|
|
|
|
self._seltree.remove_column(col)
|
2007-02-21 13:41:37 +01:00
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
def _drag_data_received(self, widget, drag_context, x, y,
|
|
|
|
selection, info, timestamp):
|
|
|
|
|
|
|
|
treestore, path = selection.tree_get_row_drag_data()
|
|
|
|
i = treestore.get_iter(path)
|
|
|
|
obj = treestore.get_value(i, 2)
|
|
|
|
if isinstance(obj, dataset.CategoryDataset):
|
|
|
|
self.add_dataset(obj)
|
2007-02-21 12:43:00 +01:00
|
|
|
self._dimlist_controller.set_dimension(obj.get_dim_name(0))
|
2007-02-21 01:00:26 +01:00
|
|
|
widget.emit_stop_by_name('drag-data-received')
|
|
|
|
|
|
|
|
def _on_cursor_changed(self, widget):
|
|
|
|
"Show the list of identifier strings."
|
|
|
|
store = self._sel_stores[self._dimension]
|
|
|
|
|
|
|
|
p = self._seltree.get_cursor()[0]
|
|
|
|
i = store.get_iter(p)
|
|
|
|
obj = store.get_value(i, 1)
|
|
|
|
|
|
|
|
if isinstance(obj, dataset.Selection):
|
|
|
|
self._idlist_controller.set_selection(obj)
|
2007-02-26 19:02:34 +01:00
|
|
|
else:
|
|
|
|
self._idlist_controller.set_selection(None)
|
2007-02-21 01:00:26 +01:00
|
|
|
|
|
|
|
def _on_row_activated(self, widget, path, column):
|
|
|
|
store = self._sel_stores[self._dimension]
|
|
|
|
i = store.get_iter(path)
|
|
|
|
obj = store.get_value(i, 1)
|
|
|
|
if isinstance(obj, dataset.Dataset):
|
|
|
|
seltree = self._seltree
|
|
|
|
if seltree.row_expanded(path):
|
|
|
|
seltree.collapse_row(path)
|
|
|
|
else:
|
|
|
|
seltree.expand_row(path, True)
|
|
|
|
elif isinstance(obj, dataset.Selection):
|
2007-07-26 14:35:59 +02:00
|
|
|
main.project.set_selection(self._dimension,
|
2007-02-21 01:00:26 +01:00
|
|
|
obj[self._dimension])
|
2007-02-21 13:41:37 +01:00
|
|
|
|
|
|
|
def _on_button_pressed(self, widget, event):
|
|
|
|
"""Button press callbak."""
|
|
|
|
if event.button == 3:
|
|
|
|
self._seltree_menu.popup(None, None, None, event.button, event.time)
|
|
|
|
|
|
|
|
def _on_seltree_sort(self, menuitem):
|
|
|
|
"""Sort selection tree if row is category dataset."""
|
|
|
|
store = self._sel_stores[self._dimension]
|
|
|
|
p = self._seltree.get_cursor()[0]
|
|
|
|
i = store.get_iter(p)
|
|
|
|
obj = store.get_value(i, 1)
|
|
|
|
if isinstance(obj, dataset.CategoryDataset):
|
|
|
|
self._sort_selections(obj)
|
2007-02-21 01:00:26 +01:00
|
|
|
|
2007-02-26 15:35:31 +01:00
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
class DimListController:
|
|
|
|
def __init__(self, dimlist, seltree_controller):
|
|
|
|
|
|
|
|
self._current_dim = None
|
|
|
|
self._seltree_controller = seltree_controller
|
|
|
|
|
2007-03-01 16:06:33 +01:00
|
|
|
self.show_hidden = False
|
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
## dimstore is a list of all dimensions in the application
|
|
|
|
self.dimstore = gtk.ListStore(gobject.TYPE_STRING)
|
|
|
|
|
2007-02-21 20:57:27 +01:00
|
|
|
# filter for hiding dims prefixed with underscore
|
|
|
|
self.dimstore_filter = self.dimstore.filter_new()
|
|
|
|
self.dimstore_filter.set_visible_func(self._dimension_filter)
|
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
## The widgets we are controlling
|
|
|
|
self.dimlist = dimlist
|
|
|
|
|
|
|
|
## Set up dimensions list
|
2007-02-21 20:57:27 +01:00
|
|
|
dimlist.set_model(self.dimstore_filter)
|
2007-02-21 01:00:26 +01:00
|
|
|
|
|
|
|
renderer = gtk.CellRendererText()
|
|
|
|
dim_column = gtk.TreeViewColumn('Dimension', renderer, text=0)
|
|
|
|
dimlist.insert_column(dim_column, 0)
|
|
|
|
|
2007-02-21 20:57:27 +01:00
|
|
|
# Signals
|
2007-02-21 01:00:26 +01:00
|
|
|
dimlist.connect('row-activated', self._dim_row_activated)
|
|
|
|
dimlist.connect('cursor-changed', self._dim_cursor_changed)
|
2007-02-21 20:57:27 +01:00
|
|
|
dimlist.connect('button-press-event', self._dimlist_button_pressed)
|
|
|
|
|
|
|
|
# Set up dimension context menu
|
|
|
|
self._dimlist_menu = SimpleMenu()
|
|
|
|
self._dimlist_menu.add_simple_item('Hide', self._on_dim_hide)
|
|
|
|
self._dimlist_menu.add_simple_item('Show all', self._on_dim_show)
|
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
|
|
|
|
##
|
|
|
|
## Public interface
|
|
|
|
##
|
2006-09-08 20:25:03 +02:00
|
|
|
def set_project(self, project):
|
|
|
|
"""Dependency injection."""
|
2007-07-26 14:35:59 +02:00
|
|
|
# self.project = project
|
2006-09-08 20:25:03 +02:00
|
|
|
self.dim_names = project.dim_names
|
|
|
|
self.update_dims()
|
|
|
|
project.add_dataset_observer(self)
|
|
|
|
|
|
|
|
def get_dimension(self, dim):
|
|
|
|
"""Returns the iterator to the dimension with the given name, or
|
|
|
|
None if not found."""
|
|
|
|
|
2007-03-01 16:06:33 +01:00
|
|
|
i = self.dimstore_filter.get_iter_first()
|
2006-09-08 20:25:03 +02:00
|
|
|
while i:
|
2007-03-01 16:06:33 +01:00
|
|
|
if self.dimstore_filter.get_value(i, 0) == dim:
|
2006-09-08 20:25:03 +02:00
|
|
|
return i
|
2007-03-01 16:06:33 +01:00
|
|
|
i = self.dimstore_filter.iter_next(i)
|
2006-09-08 20:25:03 +02:00
|
|
|
return None
|
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
def set_dimension(self, dimname):
|
2007-02-26 15:35:31 +01:00
|
|
|
"""Sets the current dimension."""
|
2007-02-21 01:00:26 +01:00
|
|
|
self._current_dim = dimname
|
2007-02-23 11:46:04 +01:00
|
|
|
|
2007-02-21 01:00:26 +01:00
|
|
|
dim = self.get_dimension(self._current_dim)
|
2007-03-01 16:06:33 +01:00
|
|
|
path = self.dimstore_filter.get_path(dim)
|
2007-02-21 01:00:26 +01:00
|
|
|
|
|
|
|
if self.dimlist.get_cursor()[0] != path:
|
2007-03-01 16:06:33 +01:00
|
|
|
self.dimlist.set_cursor(self.dimstore_filter.get_path(dim))
|
2007-02-21 01:00:26 +01:00
|
|
|
self._seltree_controller.set_dimension(dimname)
|
|
|
|
|
|
|
|
def dataset_changed(self):
|
|
|
|
"""Callback function from Project."""
|
|
|
|
self.update_dims()
|
2007-02-23 11:46:04 +01:00
|
|
|
|
2006-09-08 20:25:03 +02:00
|
|
|
def update_dims(self):
|
|
|
|
"""Update the list of dimensions shown"""
|
|
|
|
for dim in self.dim_names:
|
|
|
|
if not self.get_dimension(dim):
|
|
|
|
self.dimstore.insert_after(None, (dim,))
|
2007-03-01 16:06:33 +01:00
|
|
|
self.dimstore_filter.refilter()
|
2006-09-08 20:25:03 +02:00
|
|
|
|
2007-02-23 11:46:04 +01:00
|
|
|
#
|
|
|
|
# Private interface
|
|
|
|
#
|
2007-02-21 20:57:27 +01:00
|
|
|
def _dimension_filter(self, store, row):
|
2007-02-26 15:35:31 +01:00
|
|
|
"""Filters out dimensions with underscore prefix."""
|
2007-03-01 16:06:33 +01:00
|
|
|
if self.show_hidden:
|
|
|
|
return True
|
|
|
|
|
2007-02-21 20:57:27 +01:00
|
|
|
visible = False
|
|
|
|
name = store.get_value(row, 0)
|
|
|
|
if name != None:
|
|
|
|
visible = name[0]!="_"
|
|
|
|
return visible
|
|
|
|
|
2007-02-26 15:35:31 +01:00
|
|
|
#
|
|
|
|
# GTK Callbacks.
|
|
|
|
#
|
2007-02-21 20:57:27 +01:00
|
|
|
def _on_dim_hide(self, menuitem):
|
2007-02-26 15:35:31 +01:00
|
|
|
"""Menu item callback function which hides underscore prefixed
|
|
|
|
dimensions."""
|
2007-03-01 16:06:33 +01:00
|
|
|
self.show_hidden = False
|
2007-02-21 20:57:27 +01:00
|
|
|
self.dimstore_filter.refilter()
|
|
|
|
|
|
|
|
def _on_dim_show(self, menuitem):
|
2007-02-26 15:35:31 +01:00
|
|
|
"""Menu item callback function that shows underscore prefixed
|
|
|
|
dimension names."""
|
2007-03-01 16:06:33 +01:00
|
|
|
self.show_hidden = True
|
|
|
|
self.dimstore_filter.refilter()
|
2007-02-21 20:57:27 +01:00
|
|
|
|
2006-09-08 20:25:03 +02:00
|
|
|
def _dim_cursor_changed(self, widget):
|
|
|
|
cursor = self.dimlist.get_cursor()[0]
|
2007-03-01 16:06:33 +01:00
|
|
|
i = self.dimstore_filter.get_iter(cursor)
|
|
|
|
row = self.dimstore_filter.get_value(i, 0)
|
2006-09-08 20:25:03 +02:00
|
|
|
self.set_dimension(row)
|
2007-02-26 19:02:34 +01:00
|
|
|
self._seltree_controller.activate()
|
2006-09-08 20:25:03 +02:00
|
|
|
|
|
|
|
def _dim_row_activated(self, widget, path, column):
|
2007-09-20 18:12:58 +02:00
|
|
|
#self._seltree_controller.set_dimension(dim)
|
|
|
|
pass
|
2007-02-23 11:46:04 +01:00
|
|
|
|
2007-02-21 20:57:27 +01:00
|
|
|
def _dimlist_button_pressed(self, widget, event):
|
|
|
|
if event.button == 3:
|
|
|
|
self._dimlist_menu.popup(None, None, None, event.button, event.time)
|
2007-02-26 15:35:31 +01:00
|
|
|
|