# -*- coding: utf-8 -*-
"""
Created on Sat Jan 27 10:16:59 2024

@author: sharl
"""

import pandas as pd
#from Bio import SeqIO
from Bio import Seq
import numpy as np
#import openpyxl
#from Bio import Align
import io




def codon_table_processor(filename):
    """Converts a text file with data copied from https://www.kazusa.or.jp/ codon tables into a dataframe.
    
    Codon tables must have selected a genetic code in the format options and text file does not include headers. 
    
    Note: uracils are changed to thymines
    
    Arguments 
    filename -- filename of the text file including extension as a string
    
    Outputs a dataframe"""
    
    
    raw_freq = open(filename, "r").read()

    headers = ["DNA", "Protein", "Fraction", "Frequency", "Number"]

    raw_freq_str = str(raw_freq)

    raw_freq_str_lines = raw_freq_str.replace(") ", ")\n").replace("\n ", "\n")
    raw_freq_str_lines = raw_freq_str_lines.replace("( ", "").replace(")","").replace("(", "").replace("  ", " ")
    raw_freq_str_lines_Ts = raw_freq_str_lines.replace("U", "T")
    raw_freq_str_lines_Ts_tabs = raw_freq_str_lines_Ts.replace(" ", "\t")

    df = pd.read_csv(io.StringIO(raw_freq_str_lines_Ts_tabs), sep="\t", header = None)
    df.columns = headers

    return df