test_ipyrelatonships_in_the_go()   F
last analyzed

Complexity

Conditions 11

Size

Total Lines 94

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
dl 0
loc 94
rs 3.9763
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like test_ipyrelatonships_in_the_go() 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
#!/usr/bin/env python
2
"""Command-line test for notebooks/relatonships_in_the_go.ipynb."""
3
4
from __future__ import print_function
5
6
import os
7
from collections import defaultdict
8
from goatools.base import get_godag
9
10
# pylint: disable=line-too-long
11
def test_ipyrelatonships_in_the_go():
12
    """Command-line test for notebooks/relatonships_in_the_go.ipynb."""
13
    # Loading GO graph with the relationship tags
14
    repo = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
15
    godag = get_godag(os.path.join(repo, "go-basic.obo"), optional_attrs=['relationship'])
16
17
    print("\n## Viewing relationships in the GO graph")
18
    eg_term = godag['GO:1901990']
19
    print(set([eg_term]))
20
    # relationship_rev: 0 items
21
    # name:regulation of mitotic cell cycle phase transition
22
    # relationship: 1 items
23
    #   regulates: 1 items
24
    #     GO:0044772        level-04        depth-04        mitotic cell cycle phase transition [biological_process]
25
    # level:6
26
    # is_obsolete:False
27
    # namespace:biological_process
28
    # id:GO:1901990
29
    # reldepth:7
30
    # depth:7
31
    # parents: 2 items
32
    #   GO:1901987  level-06        depth-06        regulation of cell cycle phase transition [biological_process]
33
    #   GO:0007346  level-05        depth-05        regulation of mitotic cell cycle [biological_process]
34
    # children: 6 items
35
    #   GO:0010389  level-07        depth-08        regulation of G2/M transition of mitotic cell cycle [biological_process]
36
    #   GO:2000045  level-07        depth-08        regulation of G1/S transition of mitotic cell cycle [biological_process]
37
    #   GO:0007096  level-07        depth-08        regulation of exit from mitosis [biological_process]
38
    #   GO:0030071  level-07        depth-10        regulation of mitotic metaphase/anaphase transition [biological_process]
39
    #   GO:1901991  level-07        depth-08        negative regulation of mitotic cell cycle phase transition [biological_process]
40
    #   GO:1901992  level-07        depth-08        positive regulation of mitotic cell cycle phase transition [biological_process]
41
    # _parents: 2 items
42
    #   GO:1901987
43
    #   GO:0007346
44
    # alt_ids: 0 items])
45
46
47
    print("\n## Relationship dictionary")
48
    print(eg_term.relationship.keys())
49
    print(eg_term.relationship['regulates'])
50
51
    ## Example use case: GO:0007124
52
    #
53
    # $ prt_terms GO:0007124
54
    # [Term]
55
    # id: GO:0007124
56
    # name: pseudohyphal growth
57
    # namespace: biological_process
58
    # def: "A pattern of cell growth that occurs in conditions of nitrogen limitation
59
    # and abundant fermentable carbon source. Cells become elongated, switch to a
60
    # unipolar budding pattern, remain physically attached to each other, and invade
61
    # the growth substrate." [GOC:krc, PMID:11104818]
62
    # subset: goslim_candida
63
    # subset: goslim_yeast
64
    # is_a: GO:0016049 ! cell growth
65
    # is_a: GO:0070783 ! growth of unicellular organism as a thread of attached cells
66
    term_of_interest = godag['GO:0007124']
67
68
    # First, find the relationship types which contain "regulates":
69
    regulates = frozenset([typedef
70
                           for typedef in godag.typedefs.keys()
71
                           if 'regulates' in typedef])
72
    print(regulates)
73
    assert regulates == frozenset(['regulates', 'negatively_regulates', 'positively_regulates'])
74
75
    # Now search through the terms in the tree for those with a relationship in
76
    # this list and add them to a dictionary dependent on the type of regulation.
77
    regulating_terms = defaultdict(list)
78
    for goterm in godag.values():
79
        if hasattr(goterm, 'relationship'):
80
            for typedef in regulates.intersection(goterm.relationship.keys()):
81
                if term_of_interest in goterm.relationship[typedef]:
82
                    regulating_terms['{:s}d_by'.format(typedef[:-1])].append(goterm)
83
84
    # Now regulating_terms contains the GO terms which relate to regulating
85
    # protein localization to the nucleolus.
86
87
    # pseudohyphal growth (GO:0007124) is:
88
    #
89
    #   - negatively_regulated_by:
90
    #     -- GO:2000221 negative regulation of pseudohyphal growth
91
    #
92
    #   - regulated_by:
93
    #     -- GO:2000220 regulation of pseudohyphal growth
94
    #
95
    #   - positively_regulated_by:
96
    #     -- GO:2000222 positive regulation of pseudohyphal growth
97
    #
98
    print('{:s} ({:s}) is:'.format(term_of_interest.name, term_of_interest.id))
99
    for reg_desc, goterms in regulating_terms.items():
100
        print('\n  - {:s}:'.format(reg_desc))
101
        for goterm in goterms:
102
            print('    -- {:s} {:s}'.format(goterm.id, goterm.name))
103
            for gochild in goterm.children:
104
                print('    -- {:s} {:s}'.format(gochild.id, gochild.name))
105
106
107
# pseudohyphal growth (GO:0007124) is:
108
#
109
#   - negatively_regulated_by:
110
#     -- GO:2000221 negative regulation of pseudohyphal growth
111
#     -- GO:0100042 negative regulation of pseudohyphal growth by transcription from RNA polymerase II promoter
112
#     -- GO:1900462 negative regulation of pseudohyphal growth by negative regulation of transcription from RNA polymerase II promoter
113
#
114
#   - regulated_by:
115
#     -- GO:2000220 regulation of pseudohyphal growth
116
#
117
#   - positively_regulated_by:
118
#     -- GO:2000222 positive regulation of pseudohyphal growth
119
#     -- GO:0100041 positive regulation of pseudohyphal growth by transcription from RNA polymerase II promoter
120
#     -- GO:1900461 positive regulation of pseudohyphal growth by positive regulation of transcription from RNA polymerase II promoter
121
122
123
if __name__ == '__main__':
124
    test_ipyrelatonships_in_the_go()
125