ShortenText.get_short_plot_name()   C
last analyzed

Complexity

Conditions 10

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
dl 0
loc 35
rs 5.9999
c 1
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like ShortenText.get_short_plot_name() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
"""Functions for shortening GO names."""
2
3
__copyright__ = "Copyright (C) 2016-2017, DV Klopfenstein, H Tang, All rights reserved."
4
__author__ = "DV Klopfenstein"
5
6
7
class ShortenText(object):
8
    """Shorten text for concise display."""
9
10
    greek2hex = {'alpha':0x03b1, 'beta':0x03b2, 'gamma':0x03b3, 'delta':0x03b4}
11
    greek2tex = {'alpha':r'$\alpha$', 'beta':r'$\beta$', 'gamma':r'$\gamma$', 'delta':r'$\delta$'}
12
13
    def __init__(self):
14
        self.greek2uni = {g:unichr(h).encode('utf-8') for g, h in self.greek2hex.items()}
15
        self.keep = [
16
            "defense response to protozoan",
17
            "defense response to bacterium",
18
            "cellular response to interferon-beta",
19
            "defense response to virus",
20
            "response to interferon-gamma",
21
            "innate immune response",
22
            "inflammatory response",
23
            "response to virus",
24
            "immune response"]
25
26
    def get_short_plot_name(self, goobj):
27
        """Shorten some GO names so plots are smaller."""
28
        name = goobj.name
29
        if self._keep_this(name):
30
            return self.replace_greek(name)
31
        name = name.replace("cellular response to chemical stimulus",
32
                            "cellular rsp. to chemical stim.")
33
        depth = goobj.depth
34
        if depth > 1:
35
            name = name.replace("regulation of ", "reg. of ")
36
            name = name.replace("positive reg", "+reg")
37
            name = name.replace("negative reg", "-reg")
38
            name = name.replace("involved in", "in")
39
        if depth > 2:
40
            name = name.replace("antigen processing and presentation", "a.p.p")
41
            name = name.replace("MHC class I", "MHC-I")
42
            if depth == 4:
43
                if goobj.id == "GO:0002460":
44
                    before = " ".join([
45
                        "adaptive immune response based on somatic recombination of",
46
                        "immune receptors built from immunoglobulin superfamily domains"])
47
                    name = name.replace(
48
                        before,
49
                        "rsp. based on somatic recombination of Ig immune receptors")
50
            if depth > 3:
51
                name = name.replace("signaling pathway", "sig. pw.")
52
                name = name.replace("response", "rsp.")
53
                name = name.replace("immunoglobulin superfamily domains", "Ig domains")
54
                name = name.replace("immunoglobulin", "Ig")
55
            if depth > 4:
56
                name = name.replace("production", "prod.")
57
            if depth == 6 or depth == 5:
58
                name = name.replace("tumor necrosis factor", "TNF")
59
        name = self.replace_greek(name)
60
        return name
61
62
    def shorten_go_name_ptbl1(self, name):
63
        """Shorten GO name for tables in paper."""
64
        if self._keep_this(name):
65
            return name
66
        name = name.replace("negative", "neg.")
67
        name = name.replace("positive", "pos.")
68
        name = name.replace("response", "rsp.")
69
        name = name.replace("regulation", "reg.")
70
        name = name.replace("antigen processing and presentation", "app.")
71
        return name
72
73
    def shorten_go_name_ptbl3(self, name, dcnt):
74
        """Shorten GO description for Table 3 in manuscript."""
75
        if self._keep_this(name):
76
            return name
77
        name = name.replace("positive regulation of immune system process",
78
                            "+ reg. of immune sys. process")
79
        name = name.replace("positive regulation of immune response",
80
                            "+ reg. of immune response")
81
        name = name.replace("positive regulation of cytokine production",
82
                            "+ reg. of cytokine production")
83
        if dcnt < 40:
84
            name = name.replace("antigen processing and presentation", "a.p.p.")
85
        if dcnt < 10:
86
            name = name.replace("negative", "-")
87
            name = name.replace("positive", "+")
88
            #name = name.replace("tumor necrosis factor production", "tumor necrosis factor prod.")
89
            name = name.replace("tumor necrosis factor production", "TNF production")
90
        if dcnt < 4:
91
            name = name.replace("regulation", "reg.")
92
            name = name.replace("exogenous ", "")
93
            name = name.replace(" via ", " w/")
94
            name = name.replace("T cell mediated cytotoxicity", "cytotoxicity via T cell")
95
        name = name.replace('involved in', 'in')
96
        name = name.replace('-positive', '+')
97
        return name
98
99
    def replace_greek(self, name):
100
        """Replace text representing greek letters with greek letters."""
101
        name = name.replace('gamma-delta', 'gammadelta')
102
        name = name.replace('interleukin-1 beta', 'interleukin-1beta')
103
        greek_present = False
104
        for greek_txt, uni in self.greek2uni.items():
105
            if greek_txt in name:
106
                greek_present = True
107
                name = name.replace(greek_txt, "{B}".format(B=uni))
108
        if greek_present is True:
109
            name = unicode(name, 'utf-8') # For writing to xlsx
110
        return name
111
112
    def replace_greek_tex(self, name):
113
        """Replace text representing greek letters with greek letters."""
114
        name = name.replace('gamma-delta', 'gammadelta')
115
        name = name.replace('interleukin-1 beta', 'interleukin-1beta')
116
        # greek_present = False
117
        for greek_txt, tex in self.greek2tex.items():
118
            if greek_txt in name:
119
                # greek_present = True
120
                name = name.replace(greek_txt, "{B}".format(B=tex))
121
        # if greek_present is True:
122
        #     name = texcode(name, 'utf-8') # For writing to xlsx
123
        return name
124
125
    def shorten_go_name_all(self, name):
126
        """Shorten GO name for tables in paper, supplemental materials, and plots."""
127
        name = self.replace_greek(name)
128
        name = name.replace("MHC class I", "MHC-I")
129
        return name
130
131
    def _keep_this(self, name):
132
        """Return True if there are to be no modifications to name."""
133
        for keep_name in self.keep:
134
            if name == keep_name:
135
                return True
136
        return False
137
138
    #@staticmethod
139
    #def _shorten_go_name(name):
140
    #    name = name.replace(' and ', ' & ')
141
    #    if gont.dcnt < 15:
142
    #        name = name.replace('MHC class I', 'MHC-I')
143
    #        name = name.replace('antigen processing & presentation', 'a. p. p.')
144
    #        name = name.replace('positive regulation', 'pos. reg.')
145
    #    elif gont.depth > 2:
146
    #        name = name.replace(' & presentation', ' & pres.')
147
    #    return name
148
149
# Copyright (C) 2016-2017, DV Klopfenstein, H Tang, All rights reserved.
150