<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Run a GOEA. Print study genes as either IDs symbols
We use data from a 2014 Nature paper
Step1:  1b. Download Associations, if necessary
Step2:  2. Load Ontologies, Associations and Background gene set
2a. Load Ontologies
Step3:  2b. Load Associations
Step4:  2c. Load Background gene set
In this example, the background is all mouse protein-codinge genes
Step5:  3. Initialize a GOEA object
The GOEA object holds the Ontologies, Associations, and background.  
Numerous studies can then be run withough needing to re-load the above items.  
In this case, we only run one GOEA.
Step6:  4. Read study genes
~400 genes from the Nature paper supplemental table 4
Step7:  5. Run Gene Ontology Enrichment Analysis (GOEA)
You may choose to keep all results or just the significant results. In this example, we choose to keep only the significant results.
Step8:  6. Write results to an Excel file and to a text file
<ASSISTANT_TASK:>
Python Code:
# Get http://geneontology.org/ontology/go-basic.obo
from goatools.base import download_go_basic_obo
obo_fname = download_go_basic_obo()
Explanation: Run a GOEA. Print study genes as either IDs symbols
We use data from a 2014 Nature paper:  
Computational analysis of cell-to-cell heterogeneity
in single-cell RNA-sequencing data reveals hidden 
subpopulations of cells
Note: you must have the Python package, xlrd, installed to run this example. 
1. Download Ontologies and Associations
1a. Download Ontologies, if necessary
End of explanation
# Get ftp://ftp.ncbi.nlm.nih.gov/gene/DATA/gene2go.gz
from goatools.base import download_ncbi_associations
gene2go = download_ncbi_associations()
Explanation: 1b. Download Associations, if necessary
End of explanation
from goatools.obo_parser import GODag
obodag = GODag("go-basic.obo")
Explanation: 2. Load Ontologies, Associations and Background gene set
2a. Load Ontologies
End of explanation
from __future__ import print_function
from goatools.associations import read_ncbi_gene2go
geneid2gos_mouse = read_ncbi_gene2go("gene2go", taxids=[10090])
print("{N:,} annotated mouse genes".format(N=len(geneid2gos_mouse)))
Explanation: 2b. Load Associations
End of explanation
from goatools.test_data.genes_NCBI_10090_ProteinCoding import GeneID2nt as GeneID2nt_mus
Explanation: 2c. Load Background gene set
In this example, the background is all mouse protein-codinge genes
End of explanation
from goatools.go_enrichment import GOEnrichmentStudy
goeaobj = GOEnrichmentStudy(
        GeneID2nt_mus.keys(), # List of mouse protein-coding genes
        geneid2gos_mouse, # geneid/GO associations
        obodag, # Ontologies
        propagate_counts = False,
        alpha = 0.05, # default significance cut-off
        methods = ['fdr_bh']) # defult multipletest correction method
Explanation: 3. Initialize a GOEA object
The GOEA object holds the Ontologies, Associations, and background.  
Numerous studies can then be run withough needing to re-load the above items.  
In this case, we only run one GOEA.
End of explanation
# Data will be stored in this variable
import os
geneid2symbol = {}
# Get xlsx filename where data is stored
ROOT = os.path.dirname(os.getcwd()) # go up 1 level from current working directory
din_xlsx = os.path.join(ROOT, "goatools/test_data/nbt_3102/nbt.3102-S4_GeneIDs.xlsx")
# Read data
if os.path.isfile(din_xlsx):  
    import xlrd
    book = xlrd.open_workbook(din_xlsx)
    pg = book.sheet_by_index(0)
    for r in range(pg.nrows):
        symbol, geneid, pval = [pg.cell_value(r, c) for c in range(pg.ncols)]
        if geneid:
            geneid2symbol[int(geneid)] = symbol
Explanation: 4. Read study genes
~400 genes from the Nature paper supplemental table 4
End of explanation
# 'p_' means "pvalue". 'fdr_bh' is the multipletest method we are currently using.
geneids_study = geneid2symbol.keys()
goea_results_all = goeaobj.run_study(geneids_study)
goea_results_sig = [r for r in goea_results_all if r.p_fdr_bh < 0.05]
Explanation: 5. Run Gene Ontology Enrichment Analysis (GOEA)
You may choose to keep all results or just the significant results. In this example, we choose to keep only the significant results.
End of explanation
goeaobj.wr_xlsx("nbt3102_symbols.xlsx", goea_results_sig, itemid2name=geneid2symbol)
goeaobj.wr_xlsx("nbt3102_geneids.xlsx", goea_results_sig)
Explanation: 6. Write results to an Excel file and to a text file
End of explanation
<END_TASK>