Skip to content

Commit

Permalink
Merge pull request #33 from dvklopfenstein/master
Browse files Browse the repository at this point in the history
Added new GOTerm data member, depth, which tracks the depth of the longe...
  • Loading branch information
tanghaibao committed Apr 1, 2015
2 parents dde5da8 + 7179e6c commit 93a0416
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
28 changes: 20 additions & 8 deletions goatools/obo_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,15 @@ def __init__(self):
self._parents = [] # is_a basestring of parents
self.parents = [] # parent records
self.children = [] # children records
self.level = -1 # distance from root node
self.level = None # shortest distance from root node
self.depth = None # longest distance from root node
self.is_obsolete = False # is_obsolete
self.alt_ids = [] # alternative identifiers

def __str__(self):
obsolete = "obsolete" if self.is_obsolete else ""
return "%s\tlevel-%02d\t%s [%s] %s" % (self.id, self.level, self.name,
self.namespace, obsolete)
return "%s\tlevel-%02d\tdepth-%02d\t%s [%s] %s" % (self.id, self.level, self.depth,
self.name, self.namespace, obsolete)

def __repr__(self):
return "GOTerm('%s')" % (self.id)
Expand Down Expand Up @@ -184,14 +185,22 @@ def load_obo_file(self, obo_file):

def populate_terms(self):

def depth(rec):
if rec.level < 0:
def _init_level(rec):
if rec.level is None:
if not rec.parents:
rec.level = 0
else:
rec.level = min(depth(rec) for rec in rec.parents) + 1
rec.level = min(_init_level(rec) for rec in rec.parents) + 1
return rec.level

def _init_depth(rec):
if rec.depth is None:
if not rec.parents:
rec.depth = 0
else:
rec.depth = max(_init_depth(rec) for rec in rec.parents) + 1
return rec.depth

# make the parents references to the GO terms
for rec in self.values():
rec.parents = [self[x] for x in rec._parents]
Expand All @@ -201,8 +210,11 @@ def depth(rec):
for p in rec.parents:
p.children.append(rec)

if rec.level < 0:
depth(rec)
if rec.level is None:
_init_level(rec)

if rec.depth is None:
_init_depth(rec)

def write_dag(self, out=sys.stdout):
for rec_id, rec in sorted(self.items()):
Expand Down
16 changes: 14 additions & 2 deletions tests/test_get_paths.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@

from goatools.obo_parser import GODag

import sys

# Test local version of goatools
sys.path.insert(0, '..')

from goatools.obo_parser import GODag

def prt_paths(paths, PRT=sys.stdout):
for path in paths:
PRT.write('\n')
for GO in path:
PRT.write('{}\n'.format(GO))

def chk_results(actual_paths, expected_paths):
for actual_path in actual_paths:
# GOTerm -> list of Strings
Expand All @@ -10,14 +21,15 @@ def chk_results(actual_paths, expected_paths):
raise Exception('ACTUAL {} NOT FOUND IN EXPECTED RESULTS\n'.format(actual))

def test_paths_to_top():
#dag = GODag("./tests/data/mini_obo.obo")
dag = GODag("./data/mini_obo.obo")
expected_paths = [
['GO:0000001', 'GO:0000002', 'GO:0000005', 'GO:0000010'],
['GO:0000001', 'GO:0000003', 'GO:0000005', 'GO:0000010'],
['GO:0000001', 'GO:0000003', 'GO:0000006', 'GO:0000008', 'GO:0000010'] ]
actual_paths = dag.paths_to_top("GO:0000010")
chk_results(actual_paths, expected_paths)
prt_paths(actual_paths)

if __name__ == '__main__':
test_paths_to_top()

0 comments on commit 93a0416

Please sign in to comment.