<jupyter_start><jupyter_text>Reading Annotations from a _GO Association File_ (GAF) 1. Download a GAF file2. Load the GAF file into the GafReader3. Get Annotations**Bonus: Each line in the GAF file is stored in a namedtuple**:  * Namedtuple fields  * Print a subset of the namedtuple fields  1) Download a GAF file<jupyter_code># import os
# if not os.path.exists('goa_human.gaf.gz'):
#     !wget http://current.geneontology.org/annotations/goa_human.gaf.gz
#     !gunzip goa_human.gaf.gz<jupyter_output><empty_output><jupyter_text>2) Load the GAF file into the GafReader<jupyter_code>from goatools.anno.gaf_reader import GafReader

dir_source = "data/"

gaf_reader = {}
gaf_reader[3702] = GafReader(dir_source + "goa_3702_arabidopsis.gaf")
gaf_reader[4896] = GafReader(dir_source + "goa_4896_pombe.gaf")
gaf_reader[6239] = GafReader(dir_source + "goa_6239_celegans.gaf")
gaf_reader[7227] = GafReader(dir_source + "goa_7227_dmelanogaster.gaf")
gaf_reader[7955] = GafReader(dir_source + "goa_7955_zfish.gaf")
gaf_reader[9606] = GafReader(dir_source + "goa_9606_human.gaf")
gaf_reader[559292] = GafReader(dir_source + "goa_559292_cerevisiae.gaf")<jupyter_output>HMS:0:00:03.294371 226,662 annotations READ: data/goa_3702_arabidopsis.gaf 
HMS:0:00:01.478451  55,625 annotations READ: data/goa_4896_pombe.gaf 
HMS:0:00:03.345486 126,324 annotations READ: data/goa_6239_celegans.gaf 
HMS:0:00:03.589365 116,575 annotations READ: data/goa_7227_dmelanogaster.gaf 
HMS:0:00:04.822762 260,705 annotations READ: data/goa_7955_zfish.gaf 
HMS:0:00:15.238094 619,347 annotations READ: data/goa_9606_human.gaf 
HMS:0:00:01.533428 120,173 annotations READ: data/goa_559292_cerevisiae.gaf<jupyter_text>3) Get AnnotationsThe annotations will be stored in three dicts, one for each GODAG branch, where:  * the key is the protein ID and   * the value is a list of GO IDs associated with the protein.<jupyter_code>ns2assoc_dict = {}
ns2assoc_dict['3702'] = gaf_reader[3702].get_ns2assc()
# ns2assoc_dict['3702']
# ns2assc = ogaf.get_ns2assc()
for namespace, associations in gaf_reader[4896].get_ns2assc().items():
    for protein_id, go_ids in sorted(associations.items())[:15]:
        print("{NS} {PROT:7} : {GOs}".format(
            NS=namespace,
            PROT=protein_id,
            GOs=' '.join(sorted(go_ids))))<jupyter_output>BP P50525  : GO:0006284
BP Q9P3E3  : GO:0006090 GO:0006108
BP SPAC1002.01 : GO:0140053
BP SPAC1002.02 : GO:0006913
BP SPAC1002.03c : GO:0006487 GO:0006491
BP SPAC1002.04c : GO:0051123
BP SPAC1002.05c : GO:0000122 GO:0006338 GO:0016577
BP SPAC1002.06c : GO:0032121 GO:0045141
BP SPAC1002.07c : GO:0006473 GO:0009447
BP SPAC1002.08c : GO:0006391 GO:0045944 GO:1903109
BP SPAC1002.09c : GO:0006086 GO:0006091 GO:0006103 GO:0019464 GO:0045454
BP SPAC1002.10c : GO:0045292
BP SPAC1002.11 : GO:0016255
BP SPAC1002.12c : GO:0006540 GO:0009450
BP SPAC1002.13c : GO:0000272 GO:0031505 GO:0071852
MF P50525  : GO:0003906 GO:0008081
MF Q9P3E3  : GO:0004470 GO:0004471
MF SPAC1002.03c : GO:0004553 GO:0030246 GO:0033919 GO:0090599
MF SPAC1002.04c : GO:0003713 GO:0008134 GO:0016251
MF SPAC1002.05c : GO:0003677 GO:0008270 GO:0032452 GO:0032453 GO:0034647 GO:0034648 GO:0051864
MF SPAC1002.06c : GO:0005515 GO:0030674
MF SPAC1002.07c : GO:0004145 GO:0008080
MF SPAC1002.08c : GO:0016433
MF SPAC1002.09c : GO:00041[...]<jupyter_text>Bonus: The GAF is stored as a list of named tuplesThe list of namedtuples is stored in the **GafReader** data member named **_associations_**.Each namedtuple stores data for one line in the GAF file.<jupyter_code># Sort the list of GAF namedtuples by ID
nts = sorted(ogaf.associations, key=lambda nt:nt.DB_ID)

# Print one namedtuple
print(nts[1012])<jupyter_output>ntgafobj(DB='UniProtKB', DB_ID='A0A0B4J1Y9', DB_Symbol='IGHV3-72', Qualifier={'enables'}, GO_ID='GO:0003823', DB_Reference={'PMID:21873635'}, Evidence_Code='IBA', With_From={'MGI:MGI:2686979', 'MGI:MGI:96446', 'PANTHER:PTN001510949', 'MGI:MGI:96445', 'UniProtKB:A0A075B6A3', 'MGI:MGI:96443', 'MGI:MGI:96448'}, NS='MF', DB_Name={'Immunoglobulin heavy variable 3-72'}, DB_Synonym={'UniProtKB:A0A0B4J1Y9', 'PTN002510257'}, DB_Type='protein', Taxon=[9606], Date=datetime.date(2017, 2, 28), Assigned_By='GO_Central', Extension=None, Gene_Product_Form_ID=set())<jupyter_text>Namedtuple fields```DB               0 required 1              UniProtKBDB_ID            1 required 1              P12345DB_Symbol        2 required 1              PHO3Qualifier        3 optional 0 or greater   NOTGO_ID            4 required 1              GO:0003993DB_Reference     5 required 1 or greater   PMID:2676709Evidence_Code    6 required 1              IMPWith_From        7 optional 0 or greater   GO:0000346Aspect           8 required 1              FDB_Name          9 optional 0 or 1         Toll-like receptor 4DB_Synonym      10 optional 0 or greater   hToll|TollboothDB_Type         11 required 1              proteinTaxon           12 required 1 or 2         taxon:9606Date            13 required 1              20090118Assigned_By     14 required 1              SGDAnnotation_Extension  15 optional 0 or greater part_of(CL:0000576)Gene_Product_Form_ID  16 optional 0 or 1       UniProtKB:P12345-2```  Print a subset of the namedtuple fields<jupyter_code>fmtpat = '{DB_ID} {DB_Symbol:13} {GO_ID} {Qualifier} {Evidence_Code} {Date} {Assigned_By}'
for nt_line in nts[:100]:
    print(fmtpat.format(**nt_line._asdict()))<jupyter_output>A0A024R1R8 hCG_2014768   GO:0002181 {'involved_in'} IBA 2017-11-02 GO_Central
A0A024RBG1 NUDT4B        GO:0003723 {'enables'} IEA 2021-04-17 UniProt
A0A024RBG1 NUDT4B        GO:0046872 {'enables'} IEA 2021-04-17 UniProt
A0A024RBG1 NUDT4B        GO:0052840 {'enables'} IEA 2021-04-17 UniProt
A0A024RBG1 NUDT4B        GO:0052842 {'enables'} IEA 2021-04-17 UniProt
A0A024RBG1 NUDT4B        GO:0005829 {'located_in'} IDA 2016-12-04 HPA
A0A024RBG1 NUDT4B        GO:0034432 {'enables'} IBA 2017-02-28 GO_Central
A0A024RBG1 NUDT4B        GO:0050072 {'enables'} IBA 2017-02-28 GO_Central
A0A024RBG1 NUDT4B        GO:0000298 {'enables'} IBA 2017-02-28 GO_Central
A0A024RBG1 NUDT4B        GO:1901911 {'involved_in'} IBA 2017-02-28 GO_Central
A0A024RBG1 NUDT4B        GO:0071543 {'involved_in'} IBA 2017-02-28 GO_Central
A0A024RBG1 NUDT4B        GO:1901907 {'involved_in'} IBA 2017-02-28 GO_Central
A0A024RBG1 NUDT4B        GO:0008486 {'enables'} IBA 2017-02-28 GO_Central
A0A024RBG1 NUDT4B        GO:1901909 {'[...]
{'fraction_non_alphanumeric': 0.10482115085536547, 'fraction_numerical': 0.19346811819595647, 'mean_word_length': 4.459252971137521, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '551435', 'license': 'Apache-2.0', 'n_tokens_mistral': 3344, 'n_tokens_neox': 2542, 'n_words': 556, 'path': 'goatools_slim/zoli_annotations_gaf_file.ipynb', 'repo_name': 'ZoliQua/orthologs', 'size': '6430'}