def calculate_CHI(seq_target, seq_source, df_target_freq, df_source_freq):
    """Calculate Codon Harmonization Index (CHI)"""

    df_source_freq = df_source_freq.set_index('codon')
    df_target_freq = df_target_freq.set_index('codon')

    diffsum = 0
    for i in range(0, len(seq_source), 3):
        codon_target = str(seq_target[i:i+3])
        codon_source = str(seq_source[i:i+3])
        diffsum += abs(df_target_freq.loc[codon_target, 'freq'] -
                       df_source_freq.loc[codon_source, 'freq'])

    return diffsum/(len(seq_source)/3)