def wr_py_goea_results(self, fout_py, goea_results, **kws): """Save GOEA results into Python package containing list of namedtuples.""" var_name = kws.get("var_name", "goea_results") docstring = kws.get("docstring", "") sortby = kws.get("sortby", None) if goea_results: from goatools.nt_utils import wr_py_nts nts_goea = goea_results # If list has GOEnrichmentRecords or verbose namedtuples, exclude some fields. if hasattr(goea_results[0], "_fldsdefprt") or hasattr( goea_results[0], 'goterm'): # Exclude some attributes from the namedtuple when saving results # to a Python file because the information is redundant or verbose. nts_goea = MgrNtGOEAs(goea_results).get_goea_nts_prt(**kws) docstring = "\n".join( [docstring, "# {VER}\n\n".format(VER=self.obo_dag.version)]) assert hasattr(nts_goea[0], '_fields') if sortby is None: sortby = lambda nt: [ getattr(nt, 'namespace'), getattr(nt, 'enrichment'), getattr(nt, 'p_uncorrected'), getattr(nt, 'depth'), getattr(nt, 'GO') ] nts_goea = sorted(nts_goea, key=sortby) wr_py_nts(fout_py, nts_goea, docstring, var_name)
def wr_py_goea_results(self, fout_py, goea_results, **kws): """Save GOEA results into Python package containing list of namedtuples.""" var_name = kws.get("var_name", "goea_results") docstring = kws.get("docstring", "") sortby = kws.get("sortby", None) if goea_results: from goatools.nt_utils import wr_py_nts nts_goea = goea_results # If list has GOEnrichmentRecords or verbose namedtuples, exclude some fields. if hasattr(goea_results[0], "_fldsdefprt") or hasattr(goea_results[0], 'goterm'): # Exclude some attributes from the namedtuple when saving results # to a Python file because the information is redundant or verbose. nts_goea = get_goea_nts_prt(goea_results) docstring = "\n".join([docstring, "# {OBO_VER}\n\n".format(OBO_VER=self.obo_dag.version)]) assert hasattr(nts_goea[0], '_fields') if sortby is None: sortby = lambda nt: getattr(nt, 'p_uncorrected') nts_goea = sorted(nts_goea, key=sortby) wr_py_nts(fout_py, nts_goea, docstring, var_name)
def wr_py_goea_results(self, fout_py, goea_results, **kws): """Save GOEA results into Python package containing list of namedtuples.""" var_name = "goea_results" if "var_name" not in kws else kws["var_name"] docstring = "" if "docstring" not in kws else kws["docstring"] if goea_results: from goatools.nt_utils import wr_py_nts nts_goea = goea_results # If list has GOEnrichmentRecords or verbose namedtuples, exclude some fields. if hasattr(goea_results[0], "_fldsdefprt") or hasattr( goea_results[0], 'goterm'): # Exclude some attributes from the namedtuple when saving results # to a Python file because the information is redundant or verbose. nts_goea = get_goea_nts_prt(goea_results) docstring = "\n".join([ docstring, "# {OBO_VER}\n\n".format(OBO_VER=self.obo_dag.version) ]) assert hasattr(nts_goea[0], '_fields') nts_goea = sorted(nts_goea, key=lambda nt: getattr(nt, 'p_uncorrected')) wr_py_nts(fout_py, nts_goea, docstring, var_name)
def test_wrpy(): """Test writing GOATOOLS GOEA results to a Python module as a list of nts.""" # 1. Run GOATOOLS Gene Ontology Enrichment Analysis nature_data = get_goea_results() # 2. Convert GOATOOLS GOEA results into a list of namedtuples goea_results = nature_data['goea_results'] nts_goea = get_goea_nts_prt(goea_results) # 3. Save GOATOOLS GOEA into a Python module # 3a. Python module name module_name = "nbt3102_goea" fout_py = get_fout_py(module_name) # 3b. Save GOATOOLS GOEA into a Python module wr_py_nts(fout_py, nts_goea, varname="nts") # 4. Check results nts_py = importlib.import_module(module_name).nts assert len(nts_goea) == len(nts_py) # Alternatively, save module through goea object module_name = "nbt3102_goea_alt" fout_py = get_fout_py(module_name) nature_data['goeaobj'].wr_py_goea_results(fout_py, goea_results) nts_py = importlib.import_module(module_name).goea_results assert len(nts_goea) == len(nts_py)