Completed
Push — master ( 0f596f...821888 )
by
unknown
01:11
created

Go2Color   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 88
rs 10
wmc 22

4 Methods

Rating   Name   Duplication   Size   Complexity  
B _init_equiv() 0 21 6
A __init__() 0 12 3
A get_bordercolor() 0 3 1
F init_goid2color() 0 29 12
1
"""Manages GO Term fill colors and bordercolors."""
2
3
__copyright__ = "Copyright (C) 2016-2018, DV Klopfenstein, H Tang, All rights reserved."
4
__author__ = "DV Klopfenstein"
5
6
import collections as cx
7
from goatools.gosubdag.utils import get_kwargs
8
9
10
class Go2Color(object):
11
    """Manages GO Term fill colors and bordercolors."""
12
13
    kws_dct = set(['go2color', 'go2bordercolor', 'dflt_bordercolor', 'key2col'])
14
15
    alpha2col = cx.OrderedDict([
16
        # GOEA GO terms that are significant
17
        (0.005, 'mistyrose'),
18
        (0.010, 'moccasin'),
19
        (0.050, 'lemonchiffon1'),
20
        # GOEA GO terms that are not significant
21
        (1.000, 'grey95'),
22
    ])
23
24
    key2col = {
25
        'level_01': '#f1fbfd', # Very light blue
26
        'go_sources': '#ffffe4', # xkcd off white
27
    }
28
29
30
    def __init__(self, gosubdag, objgoea=None, **kws):
31
        # kws: go2color go2bordercolor dflt_bordercolor
32
        self.kws = get_kwargs(kws, self.kws_dct, None)
33
        # Use default key coloring if user does not specify to turn it off (key2col=False)
34
        if 'key2col' not in kws or kws['key2col']:
35
            self.kws['key2col'] = self.key2col
36
        self.gosubdag = gosubdag  # GoSubDag
37
        self.objgoea = objgoea    # GoeaResults
38
        self.dflt_bordercolor = kws.get('dflt_bordercolor', 'mediumseagreen')
39
        self.go2color = self.init_goid2color()
40
        self.go2bordercolor = kws.get('go2bordercolor', {})
41
        self._init_equiv()
42
43
    def get_bordercolor(self, goid):
44
        """Return GO Term border color."""
45
        return self.go2bordercolor.get(goid, self.dflt_bordercolor)
46
47
    def init_goid2color(self):
48
        """Set colors of GO terms."""
49
        goid2color = {}
50
        # 1. User-specified colors for each GO term
51
        if 'go2color' in self.kws:
52
            for goid, color in self.kws['go2color'].items():
53
                goid2color[goid] = color
54
        # 2. colors based on p-value override colors based on source GO
55
        if self.objgoea is not None:
56
            self.objgoea.set_goid2color_pval(goid2color)
57
        key2color = self.kws.get('key2col')
58
        if key2color is not None:
59
            # 3. Default GO source color
60
            if 'go_sources' in key2color:
61
                color = key2color['go_sources']
62
                go2obj = self.gosubdag.go2obj
63
                for goid in self.gosubdag.go_sources:
64
                    if goid not in goid2color:
65
                        goobj = go2obj[goid]
66
                        goid2color[goobj.id] = color
67
                        goid2color[goid] = color
68
            # 4. Level-01 GO color
69
            if 'level_01' in key2color:
70
                color = key2color['level_01']
71
                for goid, goobj in self.gosubdag.go2obj.items():
72
                    if goobj.level == 1:
73
                        if goid not in goid2color:
74
                            goid2color[goid] = color
75
        return goid2color
76
77
    def _init_equiv(self):
78
        """Add equivalent GO IDs to go2color, if necessary."""
79
        gocolored_all = set(self.go2color)
80
        go2obj_usr = self.gosubdag.go2obj
81
        go2color_add = {}
82
        for gocolored_cur, color in self.go2color.items():
83
            # Ignore GOs in go2color that are not in the user set
84
            if gocolored_cur in go2obj_usr:
85
                goobj = go2obj_usr[gocolored_cur]
86
                goids_equiv = goobj.alt_ids.union([goobj.id])
87
                # mrk_alt = "*" if gocolored_cur != goobj.id else ""
88
                # print("COLORED({}) KEY({}){:1} ALL({})".format(
89
                #     gocolored_cur, goobj.id, mrk_alt, goids_equiv))
90
                # Loop through GO IDs which are not colored, but are equivalent to colored GO IDs.
91
                for goid_add in goids_equiv.difference(gocolored_all):
92
                    if goid_add in go2color_add:
93
                        print('**TBD: TWO DIFFERENT COLORS FOR EQUIV GO ID') # pylint: disable=superfluous-parens
94
                    go2color_add[goid_add] = color
95
        # print("ADDING {N} GO IDs TO go2color".format(N=len(go2color_add)))
96
        for goid, color in go2color_add.items():
97
            self.go2color[goid] = color
98
99
100
# Copyright (C) 2016-2018, DV Klopfenstein, H Tang, All rights reserved.
101