Completed
Push — master ( 10b167...031df5 )
by
unknown
58s
created

_Run.plot_all()   B

Complexity

Conditions 2

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 32
rs 8.8571
1
#!/usr/bin/env python
2
"""Plot both the standard 'is_a' field and the optional 'part_of' relationship."""
3
4
from __future__ import print_function
5
6
__copyright__ = "Copyright (C) 2016-2018, DV Klopfenstein, H Tang, All rights reserved."
7
8
import os
9
import sys
10
from goatools.test_data.wr_subobo import WrSubObo
11
from goatools.base import download_go_basic_obo
12
from goatools.obo_parser import GODag
13
from goatools.gosubdag.gosubdag import GoSubDag
14
from goatools.gosubdag.plot.gosubdag_plot import GoSubDagPlot
15
16
17
18
REPO = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../")
19
20
NAME2GOIDS = {
21
    'smell': set([
22
        "GO:0007608",   # sensory perception of smell
23
        "GO:0050911"]), # detection of chemical stimulus involved in sensory perception of smell
24
25
    'secretory':set([
26
        "GO:0030141",   # CC 20 L06 D07 AC  P... p... secretory granule
27
        "GO:0034774"]), # CC  8 L05 D06 ABD P... .... secretory granule lumen
28
29
    # MISSING Edge source(GO:0007507); target(GO:0072359)
30
    # MISSING:  GO:0061371  # BP 0 L06 D06 B P p determination of heart left/right asymmetry
31
    # ERROR:    GO:0007507
32
    'heartjogging':set([
33
        "GO:0003304",  # BP 0 L06 D06 A P . myocardial epithelial involution in heart jogging
34
        "GO:0003146"]) # BP 0 L05 D07 A P p heart jogging
35
}
36
37
def test_plot_part_of():
38
    """Plot both the standard 'is_a' field and the 'part_of' relationship."""
39
    fout_log = "plot_relationship_part_of.log"
40
    obj = _Run()
41
    names = NAME2GOIDS
42
    # names = ["heartjogging"]
43
    with open(fout_log, 'w') as prt:
44
        for name in names:
45
            goids = NAME2GOIDS[name]
46
            obj.plot_all(goids, name, prt)
47
        print("  WROTE: {LOG}\n".format(LOG=fout_log))
48
49
50
# pylint: disable=too-few-public-methods
51
class _Run(object):
52
    """Holds data and steps for this test."""
53
54
    obopat = "../goatools/data/{NAME}.obo"
55
56
    def __init__(self):
57
        _fin_obo = os.path.join(REPO, "go-basic.obo")
58
        self.go2obj = GODag(_fin_obo, optional_attrs=['relationship'])
59
60
    def plot_all(self, goids, name, prt=sys.stdout):
61
        """Create plots with various numbers of relationships."""
62
        prt.write("\nCreate GoSubDag not loading any relationship")
63
        gosubdag_orig = GoSubDag(goids, self.go2obj, relationships=False, prt=prt)
64
        gosubdag_orig.prt_goids(gosubdag_orig.go2obj, prt=prt)
65
        prt.write("{N} GO IDS".format(N=len(gosubdag_orig.go2obj)))
66
        gopltdag = GoSubDagPlot(gosubdag_orig, mark_alt_id=True)
67
        gopltdag.plt_dag(os.path.join(REPO, "a_relationship_{NAME}_r0.png".format(NAME=name)))
68
69
        # goids.update(['GO:0007507'], ['GO:0072359'])
70
        prt.write("\nCreate GoSubDag while loading only the 'part_of' relationship")
71
        gosubdag = GoSubDag(goids, self.go2obj, relationships=['part_of'], prt=prt)
72
        gosubdag.prt_goids(gosubdag.go2obj, prt=prt)
73
        prt.write("{N} GO IDS".format(N=len(gosubdag.go2obj)))
74
        gopltdag = GoSubDagPlot(gosubdag, mark_alt_id=True)
75
        prt.write("GO SOURCES:")
76
        gosubdag.prt_goids(gosubdag.go_sources, prt=prt)
77
        gopltdag.plt_dag(os.path.join(REPO, "a_relationship_{NAME}_partof.png".format(NAME=name)))
78
79
        prt.write("\nCreate GoSubDag while loading all relationships")
80
        gosubdag = GoSubDag(goids, self.go2obj, relationships=True, prt=prt)
81
        prt.write("ALL {N} GO IDS:".format(N=len(gosubdag.go2obj)))
82
        gosubdag.prt_goids(gosubdag.go2obj, prt=prt)
83
        prt.write("2 GO SOURCES:")
84
        gosubdag.prt_goids(gosubdag.go_sources, prt=prt)
85
        goids_new = set(gosubdag.go2obj).difference(set(gosubdag_orig.go2obj))
86
        go2color = {go:'#d5ffff' for go in goids_new}
87
        prt.write("{N} NEW GO IDS:".format(N=len(goids_new)))
88
        gosubdag.prt_goids(goids_new, prt=prt)
89
        prt.write("{N} GO IDS".format(N=len(gosubdag.go2obj)))
90
        gopltdag = GoSubDagPlot(gosubdag, mark_alt_id=True, go2color=go2color)
91
        gopltdag.plt_dag(os.path.join(REPO, "a_relationship_{NAME}_r1.png".format(NAME=name)))
92
93
    def wr_subobo(self):
94
        """Write a subset obo to be used for testing."""
95
        # Load GO-DAG: Load optional 'relationship'
96
        for name, goids in NAME2GOIDS.items():
97
            fout_obo = self.get_obo_name(name)
98
            fin_obo = os.path.join(REPO, "go-basic.obo")
99
            download_go_basic_obo(fin_obo, prt=sys.stdout, loading_bar=None)
100
            obj = WrSubObo(fin_obo, optional_attrs=['relationship'])
101
            # obj = WrSubObo(fin_obo)
102
            obj.wrobo(fout_obo, goids)
103
104
    def get_obo_name(self, name):
105
        """Get the name of the obo for a small subset."""
106
        return os.path.join(REPO, self.obopat.format(NAME=name))
107
108
109
if __name__ == '__main__':
110
    #_Run().wr_subobo()
111
    test_plot_part_of()
112
113
# Copyright (C) 2016-2018, DV Klopfenstein, H Tang, All rights reserved.
114