1"""Plot a GoSubDag.
2
3   GO Terms in a plot contain text. The first two lines appear by default:
4
5        GO:0015618 L07 D13 p2 c2 d3
6        potassium-transporting ATPase activity
7
8   GO:0015618 => GO ID
9
10   First line:
11
12   LNN        => The "level" of the go term.
13                 The length of the shortest path(s) from the top to the current term.
14
15   DNN        => The "depth" of the go term.
16                 The length of the longest path(s) from the top to the current term.
17
18   pN         => Optional (parentcnt arg): number of immediate parent terms if the number
19                 of parents plotted is different than the number of parents in the obo.
20                 The default is to plot all higher-level parent terms from the
21                 source GO IDs to best show the hierarchy.
22                 But with some plots, plotting all parent GO terms can result in
23                 a GO DAG which is too large to be clearly readable. The user can then
24                 force some parent GO terms to not be plotting using the init_dag=path keyword.
25                 If some parent terms are not plotted, using the "parentcnt" option
26                 can help the user know where the plot was cut for readability.
27
28   cN         => Optional (childcnt arg): number of immediate child terms.
29                 Child hierarchy is not traversed. The default is to not plot all lower-level
30                 child terms to prevent the GO plots from being massive and unreadable.
31                 So knowing the total number of immediate child terms present (with
32                 most not on the plot) can give the user a better sense of the qualities
33                 of their plot.
34
35   dN         => Optional (rcntobj arg): total number of all levels of child terms.
36                 Child hierarchy is traversed to the bottom or the leaf-level of the graph.
37                 Knowing the total number of all descendants succinctly gives the user
38                 a sense of how close a GO term is to the bottom of the graph.
39                 "Descendants Count" is used a proxy for understanding if the
40                 GO term is a "broad" or "specific". If the GO term broadly
41                 describes a biological process, it most always has hundreds or thousands
42                 of total child terms. If the GO term specifically describes
43                 a biological process, it often has tens or less of total child terms.
44
45"""
46
47__copyright__ = "Copyright (C) 2016-2017, DV Klopfenstein, H Tang, All rights reserved."
48__author__ = "DV Klopfenstein"
49
50import collections as cx
51from goatools.gosubdag.gosubdag import GoSubDag
52from goatools.gosubdag.plot.gosubdag_plot import GoSubDagPlot
53
54
55def plt_goids(gosubdag, fout_img, goids, **kws_plt):
56    """Plot GO IDs in a DAG (Directed Acyclic Graph)."""
57    gosubdag_plt = GoSubDag(goids, gosubdag.go2obj, rcntobj=gosubdag.rcntobj, **kws_plt)
58    godagplot = GoSubDagPlot(gosubdag_plt, **kws_plt)
59    godagplot.plt_dag(fout_img)
60    return godagplot
61
62def plot_gos(fout_img, goids, go2obj, **kws):
63    """Given GO ids and the obo_dag, create a plot of paths from GO ids."""
64    gosubdag = GoSubDag(goids, go2obj, rcntobj=True)
65    godagplot = GoSubDagPlot(gosubdag, **kws)
66    godagplot.plt_dag(fout_img)
67
68def plot_results(fout_img, goea_results, **kws):
69    """Given a list of GOEA results, plot result GOs up to top."""
70    if "{NS}" not in fout_img:
71        plt_goea_results(fout_img, goea_results, **kws)
72    else:
73        # Plot separately by NS: BP, MF, CC
74        ns2goea_results = cx.defaultdict(list)
75        for rec in goea_results:
76            ns2goea_results[rec.NS].append(rec)
77        for ns_name, ns_res in ns2goea_results.items():
78            fout = fout_img.format(NS=ns_name)
79            plt_goea_results(fout, ns_res, **kws)
80
81def plt_goea_results(fout_img, goea_results, **kws):
82    """Plot a single page."""
83    go_sources = [rec.GO for rec in goea_results]
84    go2obj = {rec.GO:rec.goterm for rec in goea_results}
85    gosubdag = GoSubDag(go_sources, go2obj, rcntobj=True)
86    godagplot = GoSubDagPlot(gosubdag, goea_results=goea_results, **kws)
87    godagplot.plt_dag(fout_img)
88
89
90# Copyright (C) 2016-2017, DV Klopfenstein, H Tang, All rights reserved.
91