From 94f7ba454440dd709c8934b804cbf05c2233f4ba Mon Sep 17 00:00:00 2001 From: Haibao Tang Date: Sun, 30 Jun 2024 06:57:25 +0800 Subject: [PATCH] don't use a class --- goatools/gosubdag/plot/go_name_shorten.py | 273 +++++++++++----------- goatools/gosubdag/plot/go_node.py | 7 +- tests/test_go_name_shorten.py | 4 +- 3 files changed, 140 insertions(+), 144 deletions(-) diff --git a/goatools/gosubdag/plot/go_name_shorten.py b/goatools/gosubdag/plot/go_name_shorten.py index 84c1465..37c63f3 100644 --- a/goatools/gosubdag/plot/go_name_shorten.py +++ b/goatools/gosubdag/plot/go_name_shorten.py @@ -4,154 +4,149 @@ __author__ = "DV Klopfenstein" -class ShortenText: - """Shorten text for concise display.""" - - greek2uni = { - "alpha": "α", - "beta": "β", - "gamma": "γ", - "delta": "δ", - } - greek2tex = { - "alpha": r"$\alpha$", - "beta": r"$\beta$", - "gamma": r"$\gamma$", - "delta": r"$\delta$", - } - keep = [ - "defense response to protozoan", - "defense response to bacterium", - "cellular response to interferon-beta", - "defense response to virus", - "response to interferon-gamma", - "innate immune response", - "inflammatory response", - "response to virus", - "immune response", - ] - - @classmethod - def get_short_plot_name(cls, goobj): - """Shorten some GO names so plots are smaller.""" - name = goobj.name - if cls._keep_this(name): - return cls.replace_greek(name) - name = name.replace( - "cellular response to chemical stimulus", "cellular rsp. to chemical stim." - ) - depth = goobj.depth - if depth > 1: - name = name.replace("regulation of", "reg. of") - name = name.replace("positive reg", "+reg") - name = name.replace("negative reg", "-reg") - name = name.replace("involved in", "in") - if depth > 2: - name = name.replace("antigen processing and presentation", "a.p.p") - name = name.replace("MHC class I", "MHC-I") - if depth == 4: - if goobj.id == "GO:0002460": - before = " ".join( - [ - "adaptive immune response based on somatic recombination of", - "immune receptors built from immunoglobulin superfamily domains", - ] - ) - name = name.replace( - before, - "rsp. based on somatic recombination of Ig immune receptors", - ) - if depth > 3: - name = name.replace("signaling pathway", "sig. pw.") - name = name.replace("response", "rsp.") - name = name.replace("immunoglobulin superfamily domains", "Ig domains") - name = name.replace("immunoglobulin", "Ig") - if depth > 4: - name = name.replace("production", "prod.") - if depth == 6 or depth == 5: - name = name.replace("tumor necrosis factor", "TNF") - name = cls.replace_greek(name) - return name +greek2uni = { + "alpha": "α", + "beta": "β", + "gamma": "γ", + "delta": "δ", +} +greek2tex = { + "alpha": r"$\alpha$", + "beta": r"$\beta$", + "gamma": r"$\gamma$", + "delta": r"$\delta$", +} +keep = [ + "defense response to protozoan", + "defense response to bacterium", + "cellular response to interferon-beta", + "defense response to virus", + "response to interferon-gamma", + "innate immune response", + "inflammatory response", + "response to virus", + "immune response", +] - @classmethod - def shorten_go_name_ptbl1(cls, name): - """Shorten GO name for tables in paper.""" - if cls._keep_this(name): - return name - name = name.replace("negative", "neg.") - name = name.replace("positive", "pos.") - name = name.replace("response", "rsp.") - name = name.replace("regulation", "reg.") - name = name.replace("antigen processing and presentation", "app.") - return name - @classmethod - def shorten_go_name_ptbl3(cls, name, dcnt): - """Shorten GO description for Table 3 in manuscript.""" - if cls._keep_this(name): - return name - name = name.replace( - "positive regulation of immune system process", - "+ reg. of immune sys. process", - ) - name = name.replace( - "positive regulation of immune response", "+ reg. of immune response" - ) - name = name.replace( - "positive regulation of cytokine production", - "+ reg. of cytokine production", - ) - if dcnt < 40: - name = name.replace("antigen processing and presentation", "a.p.p.") - if dcnt < 10: - name = name.replace("negative", "-") - name = name.replace("positive", "+") - name = name.replace("tumor necrosis factor production", "TNF production") - if dcnt < 4: - name = name.replace("regulation", "reg.") - name = name.replace("exogenous ", "") - name = name.replace(" via ", " w/") - name = name.replace( - "T cell mediated cytotoxicity", "cytotoxicity via T cell" - ) +def get_short_plot_name(goobj): + """Shorten some GO names so plots are smaller.""" + name = goobj.name + if _keep_this(name): + return replace_greek(name) + name = name.replace( + "cellular response to chemical stimulus", "cellular rsp. to chemical stim." + ) + depth = goobj.depth + if depth > 1: + name = name.replace("regulation of", "reg. of") + name = name.replace("positive reg", "+reg") + name = name.replace("negative reg", "-reg") name = name.replace("involved in", "in") - name = name.replace("-positive", "+") - return name + if depth > 2: + name = name.replace("antigen processing and presentation", "a.p.p") + name = name.replace("MHC class I", "MHC-I") + if depth == 4: + if goobj.id == "GO:0002460": + before = " ".join( + [ + "adaptive immune response based on somatic recombination of", + "immune receptors built from immunoglobulin superfamily domains", + ] + ) + name = name.replace( + before, + "rsp. based on somatic recombination of Ig immune receptors", + ) + if depth > 3: + name = name.replace("signaling pathway", "sig. pw.") + name = name.replace("response", "rsp.") + name = name.replace("immunoglobulin superfamily domains", "Ig domains") + name = name.replace("immunoglobulin", "Ig") + if depth > 4: + name = name.replace("production", "prod.") + if depth == 6 or depth == 5: + name = name.replace("tumor necrosis factor", "TNF") + name = replace_greek(name) + return name - @classmethod - def replace_greek(cls, name): - """Replace text representing greek letters with greek letters.""" - name = name.replace("gamma-delta", "gammadelta") - name = name.replace("interleukin-1 beta", "interleukin-1beta") - for greek_txt, uni in cls.greek2uni.items(): - if greek_txt in name: - name = name.replace(greek_txt, uni) - return name - @classmethod - def replace_greek_tex(cls, name): - """Replace text representing greek letters with greek letters.""" - name = name.replace("gamma-delta", "gammadelta") - name = name.replace("interleukin-1 beta", "interleukin-1beta") - for greek_txt, tex in cls.greek2tex.items(): - if greek_txt in name: - name = name.replace(greek_txt, tex) +def shorten_go_name_ptbl1(name): + """Shorten GO name for tables in paper.""" + if _keep_this(name): return name + name = name.replace("negative", "neg.") + name = name.replace("positive", "pos.") + name = name.replace("response", "rsp.") + name = name.replace("regulation", "reg.") + name = name.replace("antigen processing and presentation", "app.") + return name - @classmethod - def shorten_go_name_all(cls, name): - """Shorten GO name for tables in paper, supplemental materials, and plots.""" - name = cls.replace_greek(name) - name = name.replace("MHC class I", "MHC-I") + +def shorten_go_name_ptbl3(name, dcnt): + """Shorten GO description for Table 3 in manuscript.""" + if _keep_this(name): return name + name = name.replace( + "positive regulation of immune system process", + "+ reg. of immune sys. process", + ) + name = name.replace( + "positive regulation of immune response", "+ reg. of immune response" + ) + name = name.replace( + "positive regulation of cytokine production", + "+ reg. of cytokine production", + ) + if dcnt < 40: + name = name.replace("antigen processing and presentation", "a.p.p.") + if dcnt < 10: + name = name.replace("negative", "-") + name = name.replace("positive", "+") + name = name.replace("tumor necrosis factor production", "TNF production") + if dcnt < 4: + name = name.replace("regulation", "reg.") + name = name.replace("exogenous ", "") + name = name.replace(" via ", " w/") + name = name.replace("T cell mediated cytotoxicity", "cytotoxicity via T cell") + name = name.replace("involved in", "in") + name = name.replace("-positive", "+") + return name + + +def replace_greek(name): + """Replace text representing greek letters with greek letters.""" + name = name.replace("gamma-delta", "gammadelta") + name = name.replace("interleukin-1 beta", "interleukin-1beta") + for greek_txt, uni in greek2uni.items(): + if greek_txt in name: + name = name.replace(greek_txt, uni) + return name + + +def replace_greek_tex(name): + """Replace text representing greek letters with greek letters.""" + name = name.replace("gamma-delta", "gammadelta") + name = name.replace("interleukin-1 beta", "interleukin-1beta") + for greek_txt, tex in greek2tex.items(): + if greek_txt in name: + name = name.replace(greek_txt, tex) + return name + + +def shorten_go_name_all(name): + """Shorten GO name for tables in paper, supplemental materials, and plots.""" + name = replace_greek(name) + name = name.replace("MHC class I", "MHC-I") + return name + - @classmethod - def _keep_this(cls, name): - """Return True if there are to be no modifications to name.""" - for keep_name in cls.keep: - if name == keep_name: - return True - return False +def _keep_this(name): + """Return True if there are to be no modifications to name.""" + for keep_name in keep: + if name == keep_name: + return True + return False # Copyright (C) 2016-2017, DV Klopfenstein, H Tang, All rights reserved. diff --git a/goatools/gosubdag/plot/go_node.py b/goatools/gosubdag/plot/go_node.py index 52591bc..ef9948c 100644 --- a/goatools/gosubdag/plot/go_node.py +++ b/goatools/gosubdag/plot/go_node.py @@ -6,8 +6,9 @@ __author__ = "DV Klopfenstein" import pydot -from goatools.gosubdag.plot.go_name_shorten import ShortenText -from goatools.gosubdag.utils import extract_kwargs + +from ..utils import extract_kwargs +from .go_name_shorten import get_short_plot_name class GoNodeOpts: @@ -37,7 +38,7 @@ def get_kws(self): ret = self.kws["dict"].copy() act_set = self.kws["set"] if "shorten" in act_set and "goobj2fncname" not in ret: - ret["goobj2fncname"] = ShortenText.get_short_plot_name + ret["goobj2fncname"] = get_short_plot_name if "dict" in self.kws and "go2txt" in self.kws["dict"]: self._init_go2txt_altgos(self.kws["dict"]["go2txt"]) return ret diff --git a/tests/test_go_name_shorten.py b/tests/test_go_name_shorten.py index 9d5ba35..387cc4d 100644 --- a/tests/test_go_name_shorten.py +++ b/tests/test_go_name_shorten.py @@ -1,6 +1,6 @@ import pytest -from goatools.gosubdag.plot.go_name_shorten import ShortenText +from goatools.gosubdag.plot.go_name_shorten import get_short_plot_name @pytest.mark.parametrize( @@ -27,4 +27,4 @@ ) def test_get_short_plot_name(inp: str, outp: str): goobj = type("goobj", (object,), {"name": inp, "depth": 6, "id": "GO:0000000"}) - assert ShortenText.get_short_plot_name(goobj) == outp + assert get_short_plot_name(goobj) == outp