Skip to content

Commit

Permalink
don't use a class
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghaibao committed Jun 29, 2024
1 parent 3cf106b commit 94f7ba4
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 144 deletions.
273 changes: 134 additions & 139 deletions goatools/gosubdag/plot/go_name_shorten.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
7 changes: 4 additions & 3 deletions goatools/gosubdag/plot/go_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/test_go_name_shorten.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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

0 comments on commit 94f7ba4

Please sign in to comment.