1
|
|
|
"""Manage GOATOOLS GOEA namedtuples.""" |
2
|
|
|
|
3
|
|
|
__copyright__ = "Copyright (C) 2010-2018, H Tang et al., All rights reserved." |
4
|
|
|
__author__ = "DV Klopfenstein" |
5
|
|
|
|
6
|
|
|
import collections as cx |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
def get_study_items(goea_results): |
10
|
|
|
"""Get all study items found in a GOATOOLS GOEA (e.g., geneids).""" |
11
|
|
|
return MgrNtGOEAs(goea_results).get_study_items() |
12
|
|
|
|
13
|
|
|
def get_goea_nts_prt(goea_results, **kws): |
14
|
|
|
"""Get namedtuples containing user-specified (or default) data from GOATOOLS GOEA results.""" |
15
|
|
|
return MgrNtGOEAs(goea_results).get_goea_nts_prt(**kws) |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
class MgrNtGOEAs(object): |
19
|
|
|
"""Manage GOATOOLS GOEA namedtuples.""" |
20
|
|
|
|
21
|
|
|
def __init__(self, goea_results): |
22
|
|
|
self.goea_results = goea_results |
23
|
|
|
|
24
|
|
|
def get_study_items(self): |
25
|
|
|
"""Get all study items (e.g., geneids).""" |
26
|
|
|
study_items = set() |
27
|
|
|
for rec in self.goea_results: |
28
|
|
|
study_items |= rec.study_items |
29
|
|
|
return study_items |
30
|
|
|
|
31
|
|
|
def get_goea_nts_prt(self, fldnames=None, **usr_kws): |
32
|
|
|
"""Return list of namedtuples removing fields which are redundant or verbose.""" |
33
|
|
|
kws = usr_kws.copy() |
34
|
|
|
if 'not_fldnames' not in kws: |
35
|
|
|
kws['not_fldnames'] = ['goterm', 'parents', 'children', 'id'] |
36
|
|
|
if 'rpt_fmt' not in kws: |
37
|
|
|
kws['rpt_fmt'] = True |
38
|
|
|
return self.get_goea_nts_all(fldnames, **kws) |
39
|
|
|
|
40
|
|
|
def get_goea_nts_all(self, fldnames=None, **kws): |
41
|
|
|
"""Get namedtuples containing user-specified (or default) data from GOEA results. |
42
|
|
|
|
43
|
|
|
Reformats data from GOEnrichmentRecord objects into lists of |
44
|
|
|
namedtuples so the generic table writers may be used. |
45
|
|
|
""" |
46
|
|
|
# kws: prt_if indent itemid2name(study_items) |
47
|
|
|
data_nts = [] # A list of namedtuples containing GOEA results |
48
|
|
|
if not self.goea_results: |
49
|
|
|
return data_nts |
50
|
|
|
keep_if = kws.get('keep_if', None) |
51
|
|
|
rpt_fmt = kws.get('rpt_fmt', False) |
52
|
|
|
indent = kws.get('indent', False) |
53
|
|
|
# I. FIELD (column) NAMES |
54
|
|
|
not_fldnames = kws.get('not_fldnames', None) |
55
|
|
|
if fldnames is None: |
56
|
|
|
fldnames = self._get_fieldnames(self.goea_results[0]) |
57
|
|
|
# Ia. Explicitly exclude specific fields from named tuple |
58
|
|
|
if not_fldnames is not None: |
59
|
|
|
fldnames = [f for f in fldnames if f not in not_fldnames] |
60
|
|
|
nttyp = cx.namedtuple("NtGoeaResults", " ".join(fldnames)) |
61
|
|
|
goid_idx = fldnames.index("GO") if 'GO' in fldnames else None |
62
|
|
|
# II. Loop through GOEA results stored in a GOEnrichmentRecord object |
63
|
|
|
for goerec in self.goea_results: |
64
|
|
|
vals = self._get_field_values(goerec, fldnames, rpt_fmt, kws.get('itemid2name', None)) |
65
|
|
|
if indent: |
66
|
|
|
vals[goid_idx] = "".join([goerec.get_indent_dots(), vals[goid_idx]]) |
67
|
|
|
ntobj = nttyp._make(vals) |
68
|
|
|
if keep_if is None or keep_if(goerec): |
69
|
|
|
data_nts.append(ntobj) |
70
|
|
|
return data_nts |
71
|
|
|
|
72
|
|
|
@staticmethod |
73
|
|
|
def _get_field_values(item, fldnames, rpt_fmt=None, itemid2name=None): |
74
|
|
|
"""Return fieldnames and values of either a namedtuple or GOEnrichmentRecord.""" |
75
|
|
|
if hasattr(item, "_fldsdefprt"): # Is a GOEnrichmentRecord |
76
|
|
|
return item.get_field_values(fldnames, rpt_fmt, itemid2name) |
77
|
|
|
if hasattr(item, "_fields"): # Is a namedtuple |
78
|
|
|
return [getattr(item, f) for f in fldnames] |
79
|
|
|
|
80
|
|
|
@staticmethod |
81
|
|
|
def _get_fieldnames(item): |
82
|
|
|
"""Return fieldnames of either a namedtuple or GOEnrichmentRecord.""" |
83
|
|
|
if hasattr(item, "_fldsdefprt"): # Is a GOEnrichmentRecord |
84
|
|
|
return item.get_prtflds_all() |
85
|
|
|
if hasattr(item, "_fields"): # Is a namedtuple |
86
|
|
|
return item._fields |
87
|
|
|
|
88
|
|
|
# Copyright (C) 2010-2018, H Tang et al., All rights reserved. |
89
|
|
|
|