Skip to content

Commit

Permalink
Merge pull request #35 from dvklopfenstein/master
Browse files Browse the repository at this point in the history
Make it easier to get reports of counts of levels and depths for any set of GO terms
  • Loading branch information
tanghaibao committed Apr 3, 2015
2 parents bee5206 + 51bbc7b commit c3ea35f
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions goatools/obo_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,20 +223,37 @@ def write_dag(self, out=sys.stdout):
for rec_id, rec in sorted(self.items()):
print(rec, file=out)

def write_summary_cnts(self, out=sys.stdout):
def write_summary_cnts(self, GO_ids, out=sys.stdout):
"""Write summary of level and depth counts for specific GO ids."""
cnts = GODag.get_cnts_levels_depths_recs([self[GO] for GO in GO_ids])
self._write_summary_cnts(cnts, out)

def write_summary_cnts_all(self, out=sys.stdout):
"""Write summary of level and depth counts for all active GO Terms."""
# Count level and depth values for all unique GO Terms.
cnts = self.get_cnts_levels_depths(set(self.values()))
max_val = max(max(dep for dep in cnts['depth']), max(lev for lev in cnts['level']))
cnts = self.get_cnts_levels_depths_recs(set(self.values()))
self._write_summary_cnts(cnts, out)

def write_summary_cnts_rec(self, out=sys.stdout):
"""Write summary of level and depth counts for active GO Terms."""
cnts = self.get_cnts_levels_depths_recs(recs)
self._write_summary_cnts(cnts, out)

def _write_summary_cnts(self, cnts, out=sys.stdout):
"""Write summary of level and depth counts for active GO Terms."""
# Count level(shortest path to root) and depth(longest path to root)
# values for all unique GO Terms.
max_val = max(max(dep for dep in cnts['depth']),
max(lev for lev in cnts['level']))
nss = ['biological_process', 'molecular_function', 'cellular_component']
out.write('Lev <-Depth Counts-> <-Level Counts->\n')
out.write('Dep BP MF CC BP MF CC\n')
out.write('Dep <-Depth Counts-> <-Level Counts->\n')
out.write('Lev BP MF CC BP MF CC\n')
out.write('--- ---- ---- ---- ---- ---- ----\n')
for i in range(max_val+1):
vals = ['{:>5}'.format(cnts[desc][i][ns]) for desc in cnts for ns in nss]
out.write('{:>02} {}\n'.format(i, ' '.join(vals)))

def get_cnts_levels_depths(self, recs):
@staticmethod
def get_cnts_levels_depths_recs(recs):
"""Collect counts of levels and depths in a Group of GO Terms."""
cnts = cx.defaultdict(lambda: cx.defaultdict(cx.Counter))
for rec in recs:
Expand All @@ -245,6 +262,11 @@ def get_cnts_levels_depths(self, recs):
cnts['depth'][rec.depth][rec.namespace] += 1
return cnts

staticmethod
def get_leaf_GO_ids(recs):
pass


def query_term(self, term, verbose=False):
if term not in self:
print("Term %s not found!" % term, file=sys.stderr)
Expand Down Expand Up @@ -424,3 +446,4 @@ def update_association(self, association):
terms.update(parents)
if bad_terms:
print("terms not found: %s" % (bad_terms,), file=sys.stderr)

0 comments on commit c3ea35f

Please sign in to comment.