from easy_ko.load_genomes import SharedData

from easy_ko.CRISPR_components import *
from easy_ko.find_gRNAs import *
from easy_ko.diagnostic_primers import *
from easy_ko.homology_primers import *

# perform this function on each inputted gene
def analyze_gene(data:SharedData, gene_id, gene_name):
    gene_id_A, gene_id_B = get_A_and_B_allele(gene_id)
    fig_info = {}

    # this array is not printed by default
    log = []
    
    if gene_id_A:
        A_allele = get_allelic_info(data, gene_id_A)
        if not(A_allele):
            gene_id_A = 0
    if gene_id_B:
        B_allele = get_allelic_info(data, gene_id_B)
        if not(B_allele):
            gene_id_B = 0
    
    if gene_id_A and not(gene_id_B):
        # for simplicity later convert B to A
        gene_id_B = gene_id_A
        B_allele = A_allele

    if gene_id_B and not(gene_id_A):
        # for simplicity later convert A to B
        gene_id_A = gene_id_B
        A_allele = B_allele

    if A_allele:
        chr_seq_A = str(data.genome_assembly_dict[A_allele['seqname']])
    if B_allele:
        chr_seq_B = str(data.genome_assembly_dict[B_allele['seqname']])

    guide, fig_info['backup_guides'] = find_guides(data, gene_id, gene_name, gene_id_A, gene_id_B, A_allele, B_allele)
    F_orf_primer, R_orf_primer = make_orf_primers(data, gene_id, gene_name, chr_seq_A, chr_seq_B, A_allele, B_allele)
    F_5flank_primer, R_3flank_primer = make_flank_primers(data, gene_id, gene_name, chr_seq_A, chr_seq_B, A_allele, B_allele)
    F_FLP_primer, R_SAT_primer = make_homology_primers(data, gene_id, gene_name, A_allele)

    df_gene_analysis = pd.DataFrame([guide, F_orf_primer, R_orf_primer, F_5flank_primer, R_3flank_primer, F_FLP_primer, R_SAT_primer])

    return df_gene_analysis, A_allele, fig_info