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
|
|
|
class MgrNts(object): |
10
|
|
|
"""Manage GOATOOLS GOEA namedtuples.""" |
11
|
|
|
|
12
|
|
|
def __init__(self, nts): |
13
|
|
|
self.nts = list(nts) |
14
|
|
|
|
15
|
|
|
def get_set(self, fieldname): |
16
|
|
|
"""Get all study items (e.g., geneids).""" |
17
|
|
|
set_items = set() |
18
|
|
|
for ntdata in self.nts: |
19
|
|
|
set_items |= getattr(ntdata, fieldname) |
20
|
|
|
return set_items |
21
|
|
|
|
22
|
|
|
def mknts(self, add_dct): |
23
|
|
|
"""Add information from add_dct to a new copy of namedtuples stored in nts.""" |
24
|
|
|
nts = [] |
25
|
|
|
assert len(add_dct) == len(self.nts) |
26
|
|
|
flds = vars(next(iter(self.nts))).keys() + next(iter(add_dct)).keys() |
27
|
|
|
ntobj = cx.namedtuple("ntgoea", " ".join(flds)) |
28
|
|
|
for dct_new, ntgoea in zip(add_dct, self.nts): |
29
|
|
|
dct_curr = ntgoea._asdict() |
30
|
|
|
for key, val in dct_new.items(): |
31
|
|
|
dct_curr[key] = val |
32
|
|
|
nts.append(ntobj(**dct_curr)) |
33
|
|
|
return nts |
34
|
|
|
|
35
|
|
|
def add_f2str(self, dcts, srcfld, dstfld, dstfmt): |
36
|
|
|
"""Add a namedtuple field of type string generated from an existing namedtuple field.""" |
37
|
|
|
# Example: f2str = objntmgr.add_f2str(dcts, "p_fdr_bh", "s_fdr_bh", "{:8.2e}") |
38
|
|
|
# ntobj = self.get_ntobj() |
39
|
|
|
# print(ntobj) |
40
|
|
|
assert len(dcts) == len(self.nts) |
41
|
|
|
for dct, ntgoea in zip(dcts, self.nts): |
42
|
|
|
valorig = getattr(ntgoea, srcfld) |
43
|
|
|
valstr = dstfmt.format(valorig) |
44
|
|
|
dct[dstfld] = valstr |
45
|
|
|
|
46
|
|
|
def get_ntobj(self): |
47
|
|
|
"""Create namedtuple object with GOEA fields.""" |
48
|
|
|
if self.nts: |
49
|
|
|
return cx.namedtuple("ntgoea", " ".join(vars(next(iter(self.nts))).keys())) |
50
|
|
|
|
51
|
|
|
def init_dicts(self): |
52
|
|
|
"""Return a list of empty dicts to be filled with new data for revised namedtuples.""" |
53
|
|
|
return [{} for _ in self.nts] |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
# Copyright (C) 2010-2018, H Tang et al., All rights reserved. |
57
|
|
|
|