|
1
|
|
|
"""_resultsTest writing GOATOOLS Gene Ontology Gene Enrichment results to a Python module.""" |
|
2
|
|
|
|
|
3
|
|
|
import os |
|
4
|
|
|
import importlib |
|
5
|
|
|
from goatools.test_data.nature3102_goea import get_goea_results |
|
6
|
|
|
from goatools.nt_utils import wr_py_nts |
|
7
|
|
|
from goatools.rpt.goea_nt_xfrm import get_goea_nts_prt |
|
8
|
|
|
|
|
9
|
|
|
def test_wrpy(): |
|
10
|
|
|
"""Test writing GOATOOLS GOEA results to a Python module as a list of nts.""" |
|
11
|
|
|
# 1. Run GOATOOLS Gene Ontology Enrichment Analysis |
|
12
|
|
|
nature_data = get_goea_results() |
|
13
|
|
|
# 2. Convert GOATOOLS GOEA results into a list of namedtuples |
|
14
|
|
|
goea_results = nature_data['goea_results'] |
|
15
|
|
|
nts_goea = get_goea_nts_prt(goea_results) |
|
16
|
|
|
|
|
17
|
|
|
# 3. Save GOATOOLS GOEA into a Python module |
|
18
|
|
|
# 3a. Python module name |
|
19
|
|
|
module_name = "nbt3102_goea" |
|
20
|
|
|
fout_py = get_fout_py(module_name) |
|
21
|
|
|
# 3b. Save GOATOOLS GOEA into a Python module |
|
22
|
|
|
wr_py_nts(fout_py, nts_goea, varname="nts") |
|
23
|
|
|
# 4. Check results |
|
24
|
|
|
nts_py = importlib.import_module(module_name).nts |
|
25
|
|
|
assert len(nts_goea) == len(nts_py) |
|
26
|
|
|
|
|
27
|
|
|
# Alternatively, save module through goea object |
|
28
|
|
|
module_name = "nbt3102_goea_alt" |
|
29
|
|
|
fout_py = get_fout_py(module_name) |
|
30
|
|
|
nature_data['goeaobj'].wr_py_goea_results(fout_py, goea_results) |
|
31
|
|
|
nts_py = importlib.import_module(module_name).goea_results |
|
32
|
|
|
assert len(nts_goea) == len(nts_py) |
|
33
|
|
|
|
|
34
|
|
|
def get_fout_py(module_name): |
|
35
|
|
|
"""Given a module name, return the name of the corresponding Python file.""" |
|
36
|
|
|
fout_py = "{MODULE}.py".format(MODULE=module_name) |
|
37
|
|
|
if os.path.isfile(fout_py): |
|
38
|
|
|
os.remove(fout_py) |
|
39
|
|
|
return fout_py |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
if __name__ == '__main__': |
|
43
|
|
|
test_wrpy() |
|
44
|
|
|
|