def intersect_keys(keys, reffile, cache=False, clean_accs=False):
<EOL>index = None<EOL>if cache:<EOL><INDENT>refcache = reffile + '<STR_LIT>'<EOL>if os.path.exists(refcache):<EOL><INDENT>if os.stat(refcache).st_mtime < os.stat(reffile).st_mtime:<EOL><INDENT>logging.warn("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>index = (SeqIO.index_db(refcache,<EOL>key_function=clean_accession)<EOL>if clean_accs<EOL>else SeqIO.index_db(refcache))<EOL><DEDENT>except Exception:<EOL><INDENT>logging.warn("<STR_LIT>")<EOL>index = None<EOL><DEDENT><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>refcache = '<STR_LIT>'<EOL><DEDENT>if index is None:<EOL><INDENT>index = (SeqIO.index_db(refcache, [reffile], '<STR_LIT>',<EOL>key_function=clean_accession)<EOL>if clean_accs<EOL>else SeqIO.index_db(refcache, [reffile], '<STR_LIT>'))<EOL><DEDENT>if clean_accs:<EOL><INDENT>keys = (clean_accession(k) for k in keys)<EOL><DEDENT>for key in keys:<EOL><INDENT>try:<EOL><INDENT>record = index[key]<EOL><DEDENT>except LookupError:<EOL><INDENT>logging.info("<STR_LIT>", repr(key))<EOL>continue<EOL><DEDENT>yield record<EOL><DEDENT>
Extract SeqRecords from the index by matching keys.

    keys - an iterable of sequence identifiers/accessions to select
    reffile - name of a FASTA file to extract the specified sequences from
    cache - save an index of the reference FASTA sequence offsets to disk?
    clean_accs - strip HMMer extensions from sequence accessions?