def parse_gaf(path_or_buffer, gene_ontology, valid_genes=None,<EOL>db=None, ev_codes=None):
<EOL>assert isinstance(gene_ontology, GeneOntology)<EOL>if db is not None:<EOL><INDENT>assert isinstance(db, (str, _oldstr))<EOL><DEDENT>if (ev_codes is not None) and ev_codes:<EOL><INDENT>assert isinstance(ev_codes, (str, _oldstr)) orisinstance(ev_codes, Iterable)<EOL><DEDENT>if isinstance(ev_codes, str):<EOL><INDENT>ev_codes = set([ev_codes])<EOL><DEDENT>elif (ev_codes is not None) and ev_codes:<EOL><INDENT>ev_codes = set(ev_codes)<EOL><DEDENT>else:<EOL><INDENT>ev_codes = None<EOL><DEDENT>if isinstance(path_or_buffer, (str, _oldstr)):<EOL><INDENT>buffer = misc.gzip_open_text(path_or_buffer, encoding='<STR_LIT:ascii>')<EOL><DEDENT>else:<EOL><INDENT>buffer = path_or_buffer<EOL><DEDENT>if valid_genes is not None:<EOL><INDENT>valid_genes = set(valid_genes)<EOL><DEDENT>df = pd.read_csv(buffer, sep='<STR_LIT:\t>', comment='<STR_LIT:!>', header=None, dtype=_oldstr)<EOL>df.fillna('<STR_LIT>', inplace=True)<EOL>all_go_term_ids = set(gene_ontology._term_dict.keys())<EOL>sel = df.iloc[:, <NUM_LIT:4>].isin(all_go_term_ids)<EOL>logger.info(<EOL>'<STR_LIT>',<EOL>(~sel).sum(), sel.size, <NUM_LIT:100>*((~sel).sum()/float(sel.size)))<EOL>df = df.loc[sel]<EOL>if valid_genes is not None:<EOL><INDENT>sel = df.iloc[:, <NUM_LIT:2>].isin(valid_genes)<EOL>logger.info(<EOL>'<STR_LIT>',<EOL>(~sel).sum(), sel.size, <NUM_LIT:100>*((~sel).sum()/float(sel.size)))<EOL>df = df.loc[sel]<EOL><DEDENT>if db is not None:<EOL><INDENT>sel = (df.iloc[:, <NUM_LIT:0>] == db)<EOL>logger.info(<EOL>'<STR_LIT>',<EOL>(~sel).sum(), sel.size, <NUM_LIT:100>*((~sel).sum()/float(sel.size)))<EOL>df = df.loc[sel]<EOL><DEDENT>if ev_codes is not None:<EOL><INDENT>sel = (df.iloc[:, <NUM_LIT:6>].isin(ev_codes))<EOL>logger.info(<EOL>'<STR_LIT>',<EOL>(~sel).sum(), sel.size, <NUM_LIT:100>*((~sel).sum()/float(sel.size)))<EOL>df = df.loc[sel]<EOL><DEDENT>go_annotations = []<EOL>for i, l in df.iterrows():<EOL><INDENT>ann = GOAnnotation.from_list(gene_ontology, l.tolist())<EOL>go_annotations.append(ann)<EOL><DEDENT>logger.info('<STR_LIT>', len(go_annotations))<EOL>return go_annotations<EOL>
Parse a GAF 2.1 file containing GO annotations.

    Parameters
    ----------
    path_or_buffer : str or buffer
        The GAF file.
    gene_ontology : `GeneOntology`
        The Gene Ontology.
    valid_genes : Iterable of str, optional
        A list of valid gene names. [None]
    db : str, optional
        Select only annotations with this "DB"" value. [None]
    ev_codes : str or set of str, optional
        Select only annotations with this/these evidence codes. [None]

    Returns
    -------
    list of `GOAnnotation`
        The list of GO annotations.