diff --git a/workflows/geneontology.py b/workflows/geneontology.py index 05475cb..a9a06fe 100644 --- a/workflows/geneontology.py +++ b/workflows/geneontology.py @@ -1,3 +1,5 @@ +import gobject +import gtk import networkx import re @@ -38,15 +40,18 @@ class GeneOntology(networkx.XDiGraph): def link_ontology(self, linkattr, obsolete=False): for node in self.nodes(): for link in node[linkattr]: - self.add_edge(node, self.by_id[link], linkattr) + self.add_edge(self.by_id[link], node, linkattr) def get_bp(self): + """Returns the root node of the biological_process tree""" return self.by_id['GO:0008150'] def get_cc(self): + """Returns the root node of the cellular_component tree""" return self.by_id['id: GO:0005575'] def get_mf(self): + """Returns the root node of the molecular_function tree""" return self.by_id['GO:0003674'] @@ -106,7 +111,7 @@ def read_gene_ontology(fd): section = s if s == 'Term': term = GOTerm() - print "[Term]" +# print "[Term]" else: term = None print "ignoring: %s" %s @@ -116,7 +121,7 @@ def read_gene_ontology(fd): # print " %s: %s" % (k, v) else: print "no term: ignoring: %s" %line - print '.', +# print '.', line = fd.readline() if term: @@ -127,6 +132,68 @@ def read_gene_ontology(fd): def read_default_go(): f = open("/usr/share/gene-ontology/gene_ontology.obo") go = read_gene_ontology(f) - + go.link_ontology('is_a') f.close() return go + +def _add_subgraphs(treestore, ontology, parent, nodes): + for n in nodes: + i = treestore.insert(parent, 0, (n['id'], n['name'], n)) + _add_subgraphs(treestore, ontology, i, ontology.successors(n)) + +def get_go_treestore(ontology): + ts = gtk.TreeStore(gobject.TYPE_STRING, ## ID + gobject.TYPE_STRING, ## Name + gobject.TYPE_PYOBJECT) ## Node + _add_subgraphs(ts, ontology, None, [ontology.get_bp()]) + return ts + +class NetworkTreeModel(gtk.GenericTreeModel): + + def __init__(self, network, root): + gtk.GenericTreeModel.__init__(self) + self._network = network + self._root = root + + def on_get_flags(self): + return 0 + + def on_get_n_columns(self): + return 1 + + def on_get_column_type(self, index): + if index==0: + return gobject.TYPE_STRING + + def on_get_iter(self, path): + node = self._root + for p in path[1:]: + children = self._network.predecessors(node) + node = children[p] + return node + + def on_get_path(self, rowref): + pass + + def on_get_value(self, rowref, column): + print 'get_value' + return rowref['id'] + + def on_iter_next(self, rowref): + pass + + def on_iter_children(self, parent): + pass + + def on_iter_has_child(self, rowref): + pass + + def on_iter_n_children(self, rowref): + pass + + def on_iter_nth_child(self, parent, n): + pass + + def on_iter_parent(self, child): + pass + diff --git a/workflows/go_workflow.py b/workflows/go_workflow.py index 37c9f8a..6a65a46 100644 --- a/workflows/go_workflow.py +++ b/workflows/go_workflow.py @@ -1,6 +1,6 @@ import gtk from fluents import dataset, logger, plots, workflow, fluents -#import geneontology +import geneontology #import gostat from scipy import array, randn, log, ones import networkx @@ -9,20 +9,59 @@ class GoTermView (gtk.Frame): def __init__(self): gtk.Frame.__init__(self) - self.set_label('GO Term') + tab = gtk.Table(2, 2, False) + self._table = tab + self._name = gtk.Label('') + tab.attach(gtk.Label('Name:'), 0, 1, 0, 1) + tab.attach(self._name, 1, 2, 0, 1) + + self.add(tab) + self.set_go_term(None) + + def set_go_term(self, term): + if term: + self.set_label(term['id']) + self._name.set_text(term['name']) + else: + self.set_label('GO Term') + self._name.set_text('') + class GeneOntologyTree (gtk.HPaned): - def __init__(self): + def __init__(self, network): gtk.HPaned.__init__(self) - self._tree_view = gtk.TreeView() + + treemodel = geneontology.get_go_treestore(network) + self._treemodel = treemodel + self._tree_view = gtk.TreeView(treemodel) + + renderer = gtk.CellRendererText() + go_column = gtk.TreeViewColumn('GO ID', renderer, text=0) + self._tree_view.insert_column(go_column, 0) + + renderer = gtk.CellRendererText() + go_column = gtk.TreeViewColumn('Name', renderer, text=1) + self._tree_view.insert_column(go_column, 1) + self._desc_view = GoTermView() - self.add1(self._tree_view) + self._tree_view.connect('cursor-changed', self._on_cursor_changed) + + scrolled_window = gtk.ScrolledWindow() + scrolled_window.add(self._tree_view) + self.add1(scrolled_window) self.add2(self._desc_view) self.show_all() + def _on_cursor_changed(self, tree): + path, col = self._tree_view.get_cursor() + current = self._treemodel.get_iter(path) + term = self._treemodel.get_value(current, 2) + self._desc_view.set_go_term(term) + + class GoWorkflow (workflow.Workflow): name = 'Gene Ontology' @@ -44,7 +83,9 @@ class LoadGOFunction(workflow.Function): workflow.Function.__init__(self, 'load-go', 'Load Gene Ontology') def run(self): - browser = GeneOntologyTree() + global go + go = geneontology.read_default_go() + browser = GeneOntologyTree(go) label = gtk.Label('_Gene Ontology') label.set_use_underline(True) fluents.app['bottom_notebook'].append_page(browser, label)