def test_wr_methods(log=sys.stdout): """Demonstrate printing a subset of all available fields using two methods.""" # 1. Gene Ontology Enrichment Analysis # 1a. Initialize: Load ontologies, associations, and population gene IDs nature_data = get_goea_results() goeaobj = nature_data['goeaobj'] goea_results = nature_data['goea_results'] # 2. Write results # Write parameters: # The format_string names below are the same names as in the namedtuple field_names. prtfmt = "{GO} {NS} {level:>2} {depth:>2} {p_fdr_bh:5.2e} {study_count:>5} {name}\n" wr_params = { # Format for printing in text format 'prtfmt': prtfmt, # Format for p-values in tsv and xlsx format 'fld2fmt': { 'p_fdr_bh': '{:8.2e}' }, # Print a subset namedtuple fields, don't print all fields in namedtuple. 'prt_flds': get_fmtflds(prtfmt) } # 2a. Use the write functions inside the GOEnrichmentStudy class. cwddir = os.getcwd() tsv_obj = os.path.join(cwddir, 'nbt3102_subset_obj.tsv') tsv_nts = os.path.join(cwddir, 'nbt3102_subset_nt.tsv') _wr_3fmt_goeaobj(tsv_obj, goea_results, goeaobj, wr_params, log) # 2b. Use the write functions from the wr_tbl package to print a list of namedtuples. _wr_3fmt_wrtbl(tsv_nts, goea_results, wr_params, log) assert filecmp.cmp(tsv_obj, tsv_nts)
def test_wr_methods(log=sys.stdout): """Demonstrate printing a subset of all available fields using two methods.""" # 1. Gene Ontology Enrichment Analysis # 1a. Initialize: Load ontologies, associations, and population gene IDs nature_data = get_goea_results() goeaobj = nature_data['goeaobj'] goea_results = nature_data['goea_results'] # 2. Write results # Write parameters: # The format_string names below are the same names as in the namedtuple field_names. prtfmt = "{GO} {NS} {level:>2} {depth:>2} {p_fdr_bh:5.2e} {study_count:>5} {name}\n" wr_params = { # Format for printing in text format 'prtfmt' : prtfmt, # Format for p-values in tsv and xlsx format 'fld2fmt' : {'p_fdr_bh' : '{:8.2e}'}, # Print a subset namedtuple fields, don't print all fields in namedtuple. 'prt_flds' : get_fmtflds(prtfmt) } # 2a. Use the write functions inside the GOEnrichmentStudy class. cwddir = os.getcwd() tsv_obj = os.path.join(cwddir, 'nbt3102_subset_obj.tsv') tsv_nts = os.path.join(cwddir, 'nbt3102_subset_nt.tsv') _wr_3fmt_goeaobj(tsv_obj, goea_results, goeaobj, wr_params, log) # 2b. Use the write functions from the wr_tbl package to print a list of namedtuples. _wr_3fmt_wrtbl(tsv_nts, goea_results, wr_params, log) assert filecmp.cmp(tsv_obj, tsv_nts)
def test_wrpy(): """Test writing GOATOOLS GOEA results to a Python module as a list of nts.""" # Run GOATOOLS Gene Ontology Enrichment Analysis nature_data = get_goea_results() # Convert GOATOOLS GOEA results into a list of namedtuples goea_results = [nt for nt in nature_data['goea_results'] if nt.p_fdr_bh < 0.05] nts_goea = get_goea_nts_prt(goea_results) _run(nts_goea, next(iter(nts_goea))._fields)
def test_wrpy(): """Test writing GOATOOLS GOEA results to a Python module as a list of nts.""" # Run GOATOOLS Gene Ontology Enrichment Analysis nature_data = get_goea_results() # Convert GOATOOLS GOEA results into a list of namedtuples goea_results = [ nt for nt in nature_data['goea_results'] if nt.p_fdr_bh < 0.05 ] nts_goea = get_goea_nts_prt(goea_results) _run(nts_goea, next(iter(nts_goea))._fields)
def test_gosubdag(): """Test all ways to make a GoSubDag.""" # Get data to use for test res = get_goea_results() # godag = res['obo_dag'] goea_results = res['goea_results'] goids = [r.GO for r in goea_results] num_goids = len(goids) # Test Arg: goea_results (list of GOEnrichmentRecord objects) go_sources = [rec.GO for rec in goea_results] go2obj = {rec.GO: rec.goterm for rec in goea_results} gosubdag = GoSubDag(go_sources, go2obj) assert len(gosubdag.go_sources) == len(goea_results) # Test Arg: godag (GODag object) gosubdag = GoSubDag(None, godag) assert len(gosubdag.go_sources) > 40000 # Test Arg: goids, godag gosubdag = GoSubDag(goids, godag) assert len(gosubdag.go_sources) == num_goids
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)