1
1
"""item-DAG tasks."""
2
2
3
- __copyright__ = "Copyright (C) 2010-present, DV Klopfenstein, H Tang, All rights reserved."
3
+ __copyright__ = (
4
+ "Copyright (C) 2010-present, DV Klopfenstein, H Tang, All rights reserved."
5
+ )
4
6
__author__ = "DV Klopfenstein"
5
7
6
- from goatools .godag .consts import RELATIONSHIP_SET
8
+ from . .godag .consts import RELATIONSHIP_SET
7
9
8
10
9
- # ------------------------------------------------------------------------------------
10
11
def get_go2parents (go2obj , relationships ):
11
12
"""Get set of parents GO IDs, including parents through user-specfied relationships"""
12
- if go2obj and not hasattr (next (iter (go2obj .values ())), 'relationship' ) or not relationships :
13
+ if (
14
+ go2obj
15
+ and not hasattr (next (iter (go2obj .values ())), "relationship" )
16
+ or not relationships
17
+ ):
13
18
return get_go2parents_isa (go2obj )
14
19
go2parents = {}
15
20
for goid_main , goterm in go2obj .items ():
@@ -21,10 +26,14 @@ def get_go2parents(go2obj, relationships):
21
26
go2parents [goid_main ] = parents_goids
22
27
return go2parents
23
28
24
- # ------------------------------------------------------------------------------------
29
+
25
30
def get_go2children (go2obj , relationships ):
26
31
"""Get set of children GO IDs, including children through user-specfied relationships"""
27
- if go2obj and not hasattr (next (iter (go2obj .values ())), 'relationship' ) or not relationships :
32
+ if (
33
+ go2obj
34
+ and not hasattr (next (iter (go2obj .values ())), "relationship" )
35
+ or not relationships
36
+ ):
28
37
return get_go2children_isa (go2obj )
29
38
go2children = {}
30
39
for goid_main , goterm in go2obj .items ():
@@ -36,7 +45,7 @@ def get_go2children(go2obj, relationships):
36
45
go2children [goid_main ] = children_goids
37
46
return go2children
38
47
39
- # ------------------------------------------------------------------------------------
48
+
40
49
def get_go2parents_isa (go2obj ):
41
50
"""Get set of immediate parents GO IDs"""
42
51
go2parents = {}
@@ -46,7 +55,7 @@ def get_go2parents_isa(go2obj):
46
55
go2parents [goid_main ] = parents_goids
47
56
return go2parents
48
57
49
- # ------------------------------------------------------------------------------------
58
+
50
59
def get_go2children_isa (go2obj ):
51
60
"""Get set of immediate children GO IDs"""
52
61
go2children = {}
@@ -56,84 +65,96 @@ def get_go2children_isa(go2obj):
56
65
go2children [goid_main ] = children_goids
57
66
return go2children
58
67
59
- # ------------------------------------------------------------------------------------
68
+
60
69
def get_go2ancestors (terms , relationships , prt = None ):
61
70
"""Get GO-to- ancestors (all parents)"""
62
71
if not relationships :
63
72
if prt is not None :
64
- prt .write (' up: is_a\n ' )
73
+ prt .write (" up: is_a\n " )
65
74
return get_id2parents (terms )
66
75
if relationships == RELATIONSHIP_SET or relationships is True :
67
76
if prt is not None :
68
- prt .write ('up: is_a and {Rs}\n ' .format (
69
- Rs = ' ' .join (sorted (RELATIONSHIP_SET ))))
77
+ prt .write (
78
+ "up: is_a and {Rs}\n " .format (Rs = " " .join (sorted (RELATIONSHIP_SET )))
79
+ )
70
80
return get_id2upper (terms )
71
81
if prt is not None :
72
- prt .write ('up: is_a and {Rs}\n ' .format (
73
- Rs = ' ' .join (sorted (relationships ))))
82
+ prt .write ("up: is_a and {Rs}\n " .format (Rs = " " .join (sorted (relationships ))))
74
83
return get_id2upperselect (terms , relationships )
75
84
85
+
76
86
def get_go2descendants (terms , relationships , prt = None ):
77
87
"""Get GO-to- descendants"""
78
88
if not relationships :
79
89
if prt is not None :
80
- prt .write (' down: is_a\n ' )
90
+ prt .write (" down: is_a\n " )
81
91
return get_id2children (terms )
82
92
if relationships == RELATIONSHIP_SET or relationships is True :
83
93
if prt is not None :
84
- prt .write ('down: is_a and {Rs}\n ' .format (
85
- Rs = ' ' .join (sorted (RELATIONSHIP_SET ))))
94
+ prt .write (
95
+ "down: is_a and {Rs}\n " .format (Rs = " " .join (sorted (RELATIONSHIP_SET )))
96
+ )
86
97
return get_id2lower (terms )
87
98
if prt is not None :
88
- prt .write ('down: is_a and {Rs}\n ' .format (
89
- Rs = ' ' .join (sorted (relationships ))))
99
+ prt .write ("down: is_a and {Rs}\n " .format (Rs = " " .join (sorted (relationships ))))
90
100
return get_id2lowerselect (terms , relationships )
91
101
92
- # ------------------------------------------------------------------------------------
102
+
93
103
def get_go2depth (goobjs , relationships ):
94
104
"""Get depth of each object"""
95
105
if not relationships :
96
- return {o .item_id :o .depth for o in goobjs }
106
+ return {o .item_id : o .depth for o in goobjs }
97
107
from goatools .godag .reldepth import get_go2reldepth
108
+
98
109
return get_go2reldepth (goobjs , relationships )
99
110
100
- # ------------------------------------------------------------------------------------
111
+
101
112
def get_id2parents (objs ):
102
113
"""Get all parent IDs up the hierarchy"""
103
114
id2parents = {}
104
115
for obj in objs :
105
116
_get_id2parents (id2parents , obj .item_id , obj )
106
- return {e :es for e , es in id2parents .items () if es }
117
+ return {e : es for e , es in id2parents .items () if es }
118
+
107
119
108
120
def get_id2children (objs ):
109
121
"""Get all child IDs down the hierarchy"""
110
122
id2children = {}
111
123
for obj in objs :
112
124
_get_id2children (id2children , obj .item_id , obj )
113
- return {e :es for e , es in id2children .items () if es }
125
+ return {e : es for e , es in id2children .items () if es }
126
+
114
127
115
128
def get_id2upper (objs ):
116
129
"""Get all ancestor IDs, including all parents and IDs up all relationships"""
117
130
id2upper = {}
118
131
for obj in objs :
119
132
_get_id2upper (id2upper , obj .item_id , obj )
120
- return {e :es for e , es in id2upper .items () if es }
133
+ return {e : es for e , es in id2upper .items () if es }
134
+
121
135
122
136
def get_id2lower (objs ):
123
137
"""Get all descendant IDs, including all children and IDs down all relationships"""
124
138
id2lower = {}
139
+ cache = set ()
125
140
for obj in objs :
126
- _get_id2lower (id2lower , obj .item_id , obj )
127
- return {e :es for e , es in id2lower .items () if es }
141
+ item_id = obj .item_id
142
+ if item_id in cache :
143
+ continue
144
+ _get_id2lower (id2lower , obj .item_id , obj , cache )
145
+ return {e : es for e , es in id2lower .items () if es }
146
+
128
147
129
148
def get_id2upperselect (objs , relationship_set ):
130
149
"""Get all ancestor IDs, including all parents and IDs up selected relationships"""
131
150
return IdToUpperSelect (objs , relationship_set ).id2upperselect
132
151
152
+
133
153
def get_id2lowerselect (objs , relationship_set ):
134
154
"""Get all descendant IDs, including all children and IDs down selected relationships"""
135
155
return IdToLowerSelect (objs , relationship_set ).id2lowerselect
136
156
157
+
137
158
def get_relationship_targets (item_ids , relationships , id2rec ):
138
159
"""Get item ID set of item IDs in a relationship target set"""
139
160
# Requirements to use this function:
@@ -148,7 +169,7 @@ def get_relationship_targets(item_ids, relationships, id2rec):
148
169
reltgt_objs_all .update (reltgt_objs_cur )
149
170
return reltgt_objs_all
150
171
151
- # ------------------------------------------------------------------------------------
172
+
152
173
# pylint: disable=too-few-public-methods
153
174
class IdToUpperSelect :
154
175
"""Get all ancestor IDs, including all parents and IDs up selected relationships"""
@@ -178,6 +199,7 @@ def _get_id2upperselect(self, item_id, item_obj):
178
199
id2upperselect [item_id ] = parent_ids
179
200
return parent_ids
180
201
202
+
181
203
class IdToLowerSelect :
182
204
"""Get all descendant IDs, including all children and IDs down selected relationships"""
183
205
@@ -206,7 +228,6 @@ def _get_id2lowerselect(self, item_id, item_obj):
206
228
id2lowerselect [item_id ] = child_ids
207
229
return child_ids
208
230
209
- # ------------------------------------------------------------------------------------
210
231
211
232
def _get_id2parents (id2parents , item_id , item_obj ):
212
233
"""Add the parent item IDs for one item object and their parents."""
@@ -220,6 +241,7 @@ def _get_id2parents(id2parents, item_id, item_obj):
220
241
id2parents [item_id ] = parent_ids
221
242
return parent_ids
222
243
244
+
223
245
def _get_id2children (id2children , item_id , item_obj ):
224
246
"""Add the child item IDs for one item object and their children."""
225
247
if item_id in id2children :
@@ -232,6 +254,7 @@ def _get_id2children(id2children, item_id, item_obj):
232
254
id2children [item_id ] = child_ids
233
255
return child_ids
234
256
257
+
235
258
def _get_id2upper (id2upper , item_id , item_obj ):
236
259
"""Add the parent item IDs for one item object and their upper."""
237
260
if item_id in id2upper :
@@ -244,19 +267,23 @@ def _get_id2upper(id2upper, item_id, item_obj):
244
267
id2upper [item_id ] = upper_ids
245
268
return upper_ids
246
269
247
- def _get_id2lower (id2lower , item_id , item_obj ):
270
+
271
+ def _get_id2lower (id2lower , item_id , item_obj , cache : set ):
248
272
"""Add the lower item IDs for one item object and the objects below them."""
249
273
if item_id in id2lower :
250
274
return id2lower [item_id ]
251
275
lower_ids = set ()
276
+ cache .add (item_id )
252
277
for lower_obj in item_obj .get_goterms_lower ():
253
278
lower_id = lower_obj .item_id
254
279
lower_ids .add (lower_id )
255
- lower_ids |= _get_id2lower (id2lower , lower_id , lower_obj )
280
+ if lower_id in cache :
281
+ continue
282
+ lower_ids |= _get_id2lower (id2lower , lower_id , lower_obj , cache )
256
283
id2lower [item_id ] = lower_ids
257
284
return lower_ids
258
285
259
- # ------------------------------------------------------------------------------------
286
+
260
287
class CurNHigher :
261
288
"""Fill id2obj with item IDs in relationships."""
262
289
0 commit comments