def _wr_3fmt_wrtbl(tsv_nts, goea_results, wr_params, log):
    """Using functions in the wr_tbl pkg, demonstrate printing a subset of namedtuple fields."""
    from goatools.go_enrichment import get_goea_nts_prt
    goea_nts = get_goea_nts_prt(goea_results)
    # List of all fields, printable or not, in namedtuple (nt):
    log.write("\nnamedtuple FIELDS: {F}\n".format(F=" ".join(goea_nts[0]._fields)))
    # Print to Excel Spreadsheet
    title = "Print subset of namedtuple fields"
    wr_xlsx("nbt3102_subset_nt.xlsx", goea_nts, title=title, **wr_params)
    # Print to tab-separated file
    wr_tsv(tsv_nts, goea_nts, **wr_params)
Exemple #2
0
def _wr_3fmt_wrtbl(goea_results, wr_params, log):
    """Using functions in the wr_tbl pkg, demonstrate printing a subset of namedtuple fields."""
    from goatools.go_enrichment import get_goea_nts_prt
    goea_nts = get_goea_nts_prt(goea_results)
    # List of all fields, printable or not, in namedtuple (nt):
    log.write("\nnamedtuple FIELDS: {F}\n".format(F=" ".join(goea_nts[0]._fields)))
    # Print to Excel Spreadsheet
    title = "Print subset of namedtuple fields"
    wr_xlsx("nbt3102_subset_nt.xlsx", goea_nts, title=title, **wr_params)
    # Print to tab-separated file
    wr_tsv("nbt3102_subset_nt.tsv", goea_nts, **wr_params)
Exemple #3
0
def test_combine_nt_lists():
    """Test combining lists whose elements are namedtuples or class objects."""
    ntobj = cx.namedtuple("Nt", "idx")
    goea_results = get_goea_results()
    # Zip a list of namedtuples and another list of namedtuples
    goea_nts = get_goea_nts_prt(goea_results)
    lst2_nts = [ntobj._make([i]) for i in range(len(goea_nts))]
    # Combine lists into a single list whose elements are a namedtuple
    flds = lst2_nts[0]._fields + goea_nts[0]._fields
    lst_all = combine_nt_lists([lst2_nts, goea_nts], flds)
    assert lst_all[0]._fields == lst2_nts[0]._fields + goea_nts[0]._fields
    # Combine list contains a subset of namedtuple fields
    hdrs = ['idx', 'NS', 'level', 'depth', 'GO',
            'study_count', 'study_n', 'pop_count', 'pop_n', 'p_fdr_bh', 'name']
    lst_sub = combine_nt_lists([lst2_nts, goea_nts], hdrs)
    assert list(lst_sub[0]._fields) == hdrs, "{F} {H}".format(F=lst_sub[0]._fields, H=hdrs)
 def run_one(self, genes, tabulate=True):
     if self.gene_converter is not None:
         genes = self._convert(genes)
     res = self.goe_obj.run_study(genes)
     if self.gene_converter is not None:
         # convert the study_items entry back
         for t in res:
             # if statement not required, but may be faster here?
             if len(t.study_items) > 0:
                 t.study_items = self.gene_converter.index[
                     self.gene_converter.isin(t.study_items)].tolist()
             else:
                 t.study_items = []
     if tabulate:
         prt_flds = self.goe_obj.get_prtflds_default(res)
         res = pd.DataFrame(go_enrichment.get_goea_nts_prt(res, prt_flds))
         res.set_index('GO', inplace=True)
     return res
def test_combine_nt_lists():
    """Test combining lists whose elements are namedtuples or class objects."""
    ntobj = cx.namedtuple("Nt", "idx")
    goea_results = get_goea_results()
    # Zip a list of namedtuples and another list of namedtuples
    goea_nts = get_goea_nts_prt(goea_results)
    lst2_nts = [ntobj._make([i]) for i in range(len(goea_nts))]
    # Combine lists into a single list whose elements are a namedtuple
    flds = lst2_nts[0]._fields + goea_nts[0]._fields
    lst_all = combine_nt_lists([lst2_nts, goea_nts], flds)
    assert lst_all[0]._fields == lst2_nts[0]._fields + goea_nts[0]._fields
    # Combine list contains a subset of namedtuple fields
    hdrs = [
        'idx', 'NS', 'level', 'depth', 'GO', 'study_count', 'study_n',
        'pop_count', 'pop_n', 'p_fdr_bh', 'name'
    ]
    lst_sub = combine_nt_lists([lst2_nts, goea_nts], hdrs)
    assert list(lst_sub[0]._fields) == hdrs, "{F} {H}".format(
        F=lst_sub[0]._fields, H=hdrs)
Exemple #6
0
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)
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)