From 4462ea5a3549f9f1ae67faa4c993d05b816df102 Mon Sep 17 00:00:00 2001 From: Haibao Tang Date: Tue, 18 Jun 2024 11:05:34 -0700 Subject: [PATCH] formatting --- goatools/godag/go_tasks.py | 70 +++++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 19 deletions(-) diff --git a/goatools/godag/go_tasks.py b/goatools/godag/go_tasks.py index 6981c6c4..e370eb3f 100644 --- a/goatools/godag/go_tasks.py +++ b/goatools/godag/go_tasks.py @@ -1,15 +1,21 @@ """item-DAG tasks.""" -__copyright__ = "Copyright (C) 2010-present, DV Klopfenstein, H Tang, All rights reserved." +__copyright__ = ( + "Copyright (C) 2010-present, DV Klopfenstein, H Tang, All rights reserved." +) __author__ = "DV Klopfenstein" -from goatools.godag.consts import RELATIONSHIP_SET +from ..godag.consts import RELATIONSHIP_SET # ------------------------------------------------------------------------------------ def get_go2parents(go2obj, relationships): """Get set of parents GO IDs, including parents through user-specfied relationships""" - if go2obj and not hasattr(next(iter(go2obj.values())), 'relationship') or not relationships: + if ( + go2obj + and not hasattr(next(iter(go2obj.values())), "relationship") + or not relationships + ): return get_go2parents_isa(go2obj) go2parents = {} for goid_main, goterm in go2obj.items(): @@ -21,10 +27,15 @@ def get_go2parents(go2obj, relationships): go2parents[goid_main] = parents_goids return go2parents + # ------------------------------------------------------------------------------------ def get_go2children(go2obj, relationships): """Get set of children GO IDs, including children through user-specfied relationships""" - if go2obj and not hasattr(next(iter(go2obj.values())), 'relationship') or not relationships: + if ( + go2obj + and not hasattr(next(iter(go2obj.values())), "relationship") + or not relationships + ): return get_go2children_isa(go2obj) go2children = {} for goid_main, goterm in go2obj.items(): @@ -36,6 +47,7 @@ def get_go2children(go2obj, relationships): go2children[goid_main] = children_goids return go2children + # ------------------------------------------------------------------------------------ def get_go2parents_isa(go2obj): """Get set of immediate parents GO IDs""" @@ -46,6 +58,7 @@ def get_go2parents_isa(go2obj): go2parents[goid_main] = parents_goids return go2parents + # ------------------------------------------------------------------------------------ def get_go2children_isa(go2obj): """Get set of immediate children GO IDs""" @@ -56,84 +69,95 @@ def get_go2children_isa(go2obj): go2children[goid_main] = children_goids return go2children + # ------------------------------------------------------------------------------------ def get_go2ancestors(terms, relationships, prt=None): """Get GO-to- ancestors (all parents)""" if not relationships: if prt is not None: - prt.write('up: is_a\n') + prt.write("up: is_a\n") return get_id2parents(terms) if relationships == RELATIONSHIP_SET or relationships is True: if prt is not None: - prt.write('up: is_a and {Rs}\n'.format( - Rs=' '.join(sorted(RELATIONSHIP_SET)))) + prt.write( + "up: is_a and {Rs}\n".format(Rs=" ".join(sorted(RELATIONSHIP_SET))) + ) return get_id2upper(terms) if prt is not None: - prt.write('up: is_a and {Rs}\n'.format( - Rs=' '.join(sorted(relationships)))) + prt.write("up: is_a and {Rs}\n".format(Rs=" ".join(sorted(relationships)))) return get_id2upperselect(terms, relationships) + def get_go2descendants(terms, relationships, prt=None): """Get GO-to- descendants""" if not relationships: if prt is not None: - prt.write('down: is_a\n') + prt.write("down: is_a\n") return get_id2children(terms) if relationships == RELATIONSHIP_SET or relationships is True: if prt is not None: - prt.write('down: is_a and {Rs}\n'.format( - Rs=' '.join(sorted(RELATIONSHIP_SET)))) + prt.write( + "down: is_a and {Rs}\n".format(Rs=" ".join(sorted(RELATIONSHIP_SET))) + ) return get_id2lower(terms) if prt is not None: - prt.write('down: is_a and {Rs}\n'.format( - Rs=' '.join(sorted(relationships)))) + prt.write("down: is_a and {Rs}\n".format(Rs=" ".join(sorted(relationships)))) return get_id2lowerselect(terms, relationships) + # ------------------------------------------------------------------------------------ def get_go2depth(goobjs, relationships): """Get depth of each object""" if not relationships: - return {o.item_id:o.depth for o in goobjs} + return {o.item_id: o.depth for o in goobjs} from goatools.godag.reldepth import get_go2reldepth + return get_go2reldepth(goobjs, relationships) + # ------------------------------------------------------------------------------------ def get_id2parents(objs): """Get all parent IDs up the hierarchy""" id2parents = {} for obj in objs: _get_id2parents(id2parents, obj.item_id, obj) - return {e:es for e, es in id2parents.items() if es} + return {e: es for e, es in id2parents.items() if es} + def get_id2children(objs): """Get all child IDs down the hierarchy""" id2children = {} for obj in objs: _get_id2children(id2children, obj.item_id, obj) - return {e:es for e, es in id2children.items() if es} + return {e: es for e, es in id2children.items() if es} + def get_id2upper(objs): """Get all ancestor IDs, including all parents and IDs up all relationships""" id2upper = {} for obj in objs: _get_id2upper(id2upper, obj.item_id, obj) - return {e:es for e, es in id2upper.items() if es} + return {e: es for e, es in id2upper.items() if es} + def get_id2lower(objs): """Get all descendant IDs, including all children and IDs down all relationships""" id2lower = {} for obj in objs: _get_id2lower(id2lower, obj.item_id, obj) - return {e:es for e, es in id2lower.items() if es} + return {e: es for e, es in id2lower.items() if es} + def get_id2upperselect(objs, relationship_set): """Get all ancestor IDs, including all parents and IDs up selected relationships""" return IdToUpperSelect(objs, relationship_set).id2upperselect + def get_id2lowerselect(objs, relationship_set): """Get all descendant IDs, including all children and IDs down selected relationships""" return IdToLowerSelect(objs, relationship_set).id2lowerselect + def get_relationship_targets(item_ids, relationships, id2rec): """Get item ID set of item IDs in a relationship target set""" # Requirements to use this function: @@ -148,6 +172,7 @@ def get_relationship_targets(item_ids, relationships, id2rec): reltgt_objs_all.update(reltgt_objs_cur) return reltgt_objs_all + # ------------------------------------------------------------------------------------ # pylint: disable=too-few-public-methods class IdToUpperSelect: @@ -178,6 +203,7 @@ def _get_id2upperselect(self, item_id, item_obj): id2upperselect[item_id] = parent_ids return parent_ids + class IdToLowerSelect: """Get all descendant IDs, including all children and IDs down selected relationships""" @@ -206,8 +232,10 @@ def _get_id2lowerselect(self, item_id, item_obj): id2lowerselect[item_id] = child_ids return child_ids + # ------------------------------------------------------------------------------------ + def _get_id2parents(id2parents, item_id, item_obj): """Add the parent item IDs for one item object and their parents.""" if item_id in id2parents: @@ -220,6 +248,7 @@ def _get_id2parents(id2parents, item_id, item_obj): id2parents[item_id] = parent_ids return parent_ids + def _get_id2children(id2children, item_id, item_obj): """Add the child item IDs for one item object and their children.""" if item_id in id2children: @@ -232,6 +261,7 @@ def _get_id2children(id2children, item_id, item_obj): id2children[item_id] = child_ids return child_ids + def _get_id2upper(id2upper, item_id, item_obj): """Add the parent item IDs for one item object and their upper.""" if item_id in id2upper: @@ -244,6 +274,7 @@ def _get_id2upper(id2upper, item_id, item_obj): id2upper[item_id] = upper_ids return upper_ids + def _get_id2lower(id2lower, item_id, item_obj): """Add the lower item IDs for one item object and the objects below them.""" if item_id in id2lower: @@ -256,6 +287,7 @@ def _get_id2lower(id2lower, item_id, item_obj): id2lower[item_id] = lower_ids return lower_ids + # ------------------------------------------------------------------------------------ class CurNHigher: """Fill id2obj with item IDs in relationships."""