#!/usr/bin/env python3

import pandas as pd

DEBUG = False
#%%
#def find_missense(df, ref_allele1, alt_allele0):
def find_missense(df, ref_allele_column, alt_allele_column, n_diff_colname = 'n_diff', tot_diff_colname = 'tot_diff', ref_a_colname = 'ref_allele', alt_a_colname = 'alt_allele'):


    """Find mismatches in pairwise comparison of strings b/w col_a and col_b
    
    Case insensitive, converts strings to uppercase before comparison
    
    @df: df containing columns to compare
    @type: pandas df
    
    @ref_allele_column: column containing ref allele str
    @type: str (converts to uppercase)
    
    @alt_allele_column: column containing alt_allele str
    @type: str (converts to uppercase)
    
    @n_diff_colname: user defined colname for no. of char diff b/w ref_allele_str and alt_allele_str
    @type: str

    @tot_diff_colname: user defined colname abs diff to indicate if strings are of equal length
    @type: str

    @ref_a_colname: user defined colname containing extracted referece allele
    @type: str

    @alt_a_colname: user defined colname containing extracted alt allele
    @type: str
    
    returns df: with 4 added columns. If column names clash, the function column 
                name will override original column
    @rtype: pandas df
    """
    for ind, val in df.iterrows():
        if DEBUG:
            print('index:', ind, 'value:', val
                  , '\n============================================================')
        ref_a = val[ref_allele_column].upper()
        alt_a = val[alt_allele_column].upper()
        if DEBUG:
            print('ref_allele_string:', ref_a, 'alt_allele_string:', alt_a)
        difference = sum(1 for e in zip(ref_a, alt_a) if e[0] != e[1])
        df.at[ind, n_diff_colname] = difference # adding column
        tot_difference = difference + abs(len(ref_a) - len(alt_a))
        df.at[ind, tot_diff_colname] = tot_difference # adding column
        if difference != tot_difference:
            print('WARNING: lengths of ref_allele and alt_allele differ at index:', ind
                  , '\nNon-missense muts detected')
                  
        # Now finding the mismatched char
        ref_aln = ''
        alt_aln = ''
        if ref_a == alt_a:
            ##df.at[ind, 'ref_allele'] = 'no_change' # adding column
            ##df.at[ind, 'alt_allele'] = 'no_change' # adding column
            df.at[ind, ref_a_colname] = 'no_change' # adding column
            df.at[ind, alt_a_colname] = 'no_change' # adding column
        elif len(ref_a) == len(alt_a) and len(ref_a) > 0:
            print('ref:', ref_a, 'alt:', alt_a)
            for n in range(len(ref_a)):
                if ref_a[n] != alt_a[n]:
                    ref_aln += ref_a[n]
                    alt_aln += alt_a[n]
            ##df.at[ind, 'ref_allele'] = ref_aln
            ##df.at[ind, 'alt_allele'] = alt_aln
            df.at[ind, ref_a_colname] = ref_aln
            df.at[ind, alt_a_colname] = alt_aln
            print('ref:', ref_aln)
            print('alt:', alt_aln)
        else:
            ##df.at[ind, 'ref_allele'] = 'ERROR_Not_nsSNP'
            ##df.at[ind, 'alt_allele'] = 'ERROR_Not_nsSNP'
            df.at[ind, ref_a_colname] = 'ERROR_Not_nsSNP'
            df.at[ind, alt_a_colname] = 'ERROR_Not_nsSNP'

    return df
#========================================
# a representative example
#eg_df = pd.read_csv('pnca_assoc.txt', sep = '\t', nrows = 10, header = 0, index_col = False)
#eg_df = pd.DataFrame(eg_df)
#eg_df = {'chromosome_number': [2288719, 2288766, 2288775, 2288779, 2288827, 1111111, 2222222],
#           'ref_allele1': ['Tc', 'AG', 'AGCACCCTG', 'CCCTGGTGGCC', 'CACA', 'AA', 'CAT'],
#           'alt_allele0': ['CC', 'CA', 'GGCACCCTGZ','TCCTGGTGGCCAAD', 'TACA', 'AA', 'TCZ']}
   
#def main():
    #find_missense(eg_df, ref_allele1 = 'ref_allele', alt_allele0 = 'alt_allele')
#    find_missense(df = eg_df, ref_allele_column = 'ref_allele1', alt_allele_column = 'alt_allele0')
#    print(eg_df)

#if __name__ == '__main__':
#    main()
