# Buggy code
def is_amino_acid_functionally_conserved(amino_acid_residue_1, amino_acid_residue_2):
    """Checks if two amino acid residues are part of the same biochemical property group"""
    group = constants.amino_acid_property_group[amino_acid_residue_1]
    conserved_group = constants.conserved_amino_acid_groups[group]

    if amino_acid_residue_2 in conserved_group:
        return True
    if (
        group == "Polar and Nonpolar"
    ):  # they fall in more than one group, multiple tests needed
        if amino_acid_residue_1 == "H" and (
            amino_acid_residue_2 in constants.conserved_amino_acid_groups["Nonpolar"]
            or amino_acid_residue_2 in constants.conserved_amino_acid_groups["Bases"]
        ):
            return True
        if amino_acid_residue_1 == "Y" and (
            amino_acid_residue_2 in constants.conserved_amino_acid_groups["Aromatic"]
        ):
            return True
    return False


# Traceback (TypeError)
* Gene clusters are initialized for all 121 gene clusters in the database.

[06 Feb 20 18:17:25 Computing gene cluster homogeneity indices] Initializing 1 threads...                                                                                         ETA: ∞:∞:∞
WARNING
==============================================================
Homogeneity indices computation for gene cluster GC_00000101 failed. This can
happen due to one of three reasons: (1) this gene cluster is named incorrectly,
does not exist in the database, or is formatted into the input dictionary
incorrectly, (2) there is an alignment mistake in the gene cluster, and not all
genes are aligned to be the same lenght; or (3) the homogeneity calculator was
initialized incorrectly. As you can see, this is a rare circumstance, and anvi'o
will set this gene cluster's homogeneity indices to `-1` so things can move on,
but we highly recommend you to take a look at your data to make sure you are
satisfied with your analysis.

Process Process-4:
Traceback (most recent call last):
File "/usr/local/projdata/0718/projects/jon/conda_envs/github/anvio/anvio/dbops.py", line 1165, in homogeneity_worker
funct_index, geo_index, combined_index = homogeneity_calculator.get_homogeneity_dicts(gene_cluster)
File "/usr/local/projdata/0718/projects/jon/conda_envs/github/anvio/anvio/homogeneityindex.py", line 174, in get_homogeneity_dicts
fun = self.compute_functional_index(cluster_sequences)
File "/usr/local/projdata/0718/projects/jon/conda_envs/github/anvio/anvio/homogeneityindex.py", line 61, in compute_functional_index
elif utils.is_amino_acid_functionally_conserved(amino_acid_residue_1,amino_acid_residue_2):
File "/usr/local/projdata/0718/projects/jon/conda_envs/github/anvio/anvio/utils.py", line 1564, in is_amino_acid_functionally_conserved
if amino_acid_residue_2 in conserved_group:
TypeError: argument of type 'NoneType' is not iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/projdata/0718/projects/jon/conda_envs/envs/anvio-master/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/local/projdata/0718/projects/jon/conda_envs/envs/anvio-master/lib/python3.6/multiprocessing/process.py", line 93, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/projdata/0718/projects/jon/conda_envs/github/anvio/anvio/dbops.py", line 1176, in homogeneity_worker
combined_index[gene_cluster_name] = -1
UnboundLocalError: local variable 'combined_index' referenced before assignment

# Fixed code
def is_amino_acid_functionally_conserved(amino_acid_residue_1, amino_acid_residue_2):
    """Checks if two amino acid residues are part of the same biochemical property group"""
    group = constants.amino_acid_property_group[amino_acid_residue_1]
    conserved_group = constants.conserved_amino_acid_groups[group]

    if conserved_group and amino_acid_residue_2 in conserved_group:
        return True
    if (
        group == "Polar and Nonpolar"
    ):  # they fall in more than one group, multiple tests needed
        if amino_acid_residue_1 == "H" and (
            amino_acid_residue_2 in constants.conserved_amino_acid_groups["Nonpolar"]
            or amino_acid_residue_2 in constants.conserved_amino_acid_groups["Bases"]
        ):
            return True
        if amino_acid_residue_1 == "Y" and (
            amino_acid_residue_2 in constants.conserved_amino_acid_groups["Aromatic"]
        ):
            return True
    return False