# Buggy code
def store_sequences_for_hmm_hits(self):
    if self.summary.quick:
        return

    s = SequencesForHMMHits(
        self.summary.contigs_db_path,
        split_names_of_interest=self.split_names,
        progress=progress_quiet,
    )
    hmm_sequences_dict = s.get_sequences_dict_for_hmm_hits_in_splits(
        {self.bin_id: self.split_names}
    )

    if self.summary.reformat_contig_names:
        for entry_id in hmm_sequences_dict:
            reformatted_contig_name = self.contig_name_conversion_dict[
                hmm_sequences_dict[entry_id]["contig"]
            ]["reformatted_contig_name"]
            hmm_sequences_dict[entry_id]["contig"] = reformatted_contig_name

    single_copy_gene_hmm_sources = [
        hmm_search_source
        for hmm_search_type, hmm_search_source in self.summary.hmm_searches_header
    ]
    non_single_copy_gene_hmm_sources = self.summary.completeness.sources

    for hmm_search_source in (
        single_copy_gene_hmm_sources + non_single_copy_gene_hmm_sources
    ):
        filtered_hmm_sequences_dict = utils.get_filtered_dict(
            hmm_sequences_dict, "source", set([hmm_search_source])
        )

        output_file_obj = self.get_output_file_handle(
            "%s-hmm-sequences.txt" % hmm_search_source, key=hmm_search_source
        )

        for gene_unique_id in filtered_hmm_sequences_dict:
            header, sequence = s.get_FASTA_header_and_sequence_for_gene_unique_id(
                hmm_sequences_dict, gene_unique_id
            )
            output_file_obj.write(">%s\n%s\n" % (header, sequence))


# Traceback (KeyError)
[09 Dec 20 09:32:25 Summarizing 104 of 159: 'Bin_3_32'] Creating the FASTA file ...                                      ETA: 1m18sTraceback (most recent call last):
File "/workdir/lab_envs/anvio/bin/anvi-summarize", line 118, in <module>
main(args)
File "/workdir/lab_envs/anvio/bin/anvi-summarize", line 63, in main
summary.process()
File "/workdir/lab_envs/anvio/lib/python3.6/site-packages/anvio/summarizer.py", line 874, in process
self.summary['collection'][bin_id] = bin.create()
File "/workdir/lab_envs/anvio/lib/python3.6/site-packages/anvio/summarizer.py", line 1273, in create
self.store_sequences_for_hmm_hits()
File "/workdir/lab_envs/anvio/lib/python3.6/site-packages/anvio/summarizer.py", line 1533, in store_sequences_for_hmm_hits
hmm_sequences_dict = s.get_sequences_dict_for_hmm_hits_in_splits({self.bin_id: self.split_names})
File "/workdir/lab_envs/anvio/lib/python3.6/site-packages/anvio/hmmops.py", line 336, in get_sequences_dict_for_hmm_hits_in_splits
hmm_hit = self.hmm_hits[split_entry['hmm_hit_entry_id']]
KeyError: 17702

# Fixed code
def store_sequences_for_hmm_hits(self):
    if self.summary.quick:
        return

    s = SequencesForHMMHits(
        self.summary.contigs_db_path,
        split_names_of_interest=self.split_names,
        progress=progress_quiet,
        bin_name=self.bin_id,
    )
    hmm_sequences_dict = s.get_sequences_dict_for_hmm_hits_in_splits(
        {self.bin_id: self.split_names}
    )

    if self.summary.reformat_contig_names:
        for entry_id in hmm_sequences_dict:
            reformatted_contig_name = self.contig_name_conversion_dict[
                hmm_sequences_dict[entry_id]["contig"]
            ]["reformatted_contig_name"]
            hmm_sequences_dict[entry_id]["contig"] = reformatted_contig_name

    single_copy_gene_hmm_sources = [
        hmm_search_source
        for hmm_search_type, hmm_search_source in self.summary.hmm_searches_header
    ]
    non_single_copy_gene_hmm_sources = self.summary.completeness.sources

    for hmm_search_source in (
        single_copy_gene_hmm_sources + non_single_copy_gene_hmm_sources
    ):
        filtered_hmm_sequences_dict = utils.get_filtered_dict(
            hmm_sequences_dict, "source", set([hmm_search_source])
        )

        output_file_obj = self.get_output_file_handle(
            "%s-hmm-sequences.txt" % hmm_search_source, key=hmm_search_source
        )

        for gene_unique_id in filtered_hmm_sequences_dict:
            header, sequence = s.get_FASTA_header_and_sequence_for_gene_unique_id(
                hmm_sequences_dict, gene_unique_id
            )
            output_file_obj.write(">%s\n%s\n" % (header, sequence))