max(len(tf_wise_motifs[x]) for x in set_tf)

ctcf_motifs = tf_wise_motifs['CTCF']
ctcf_1 = ctcf_motifs[0]
ctcf_2 = ctcf_motifs[1]

m1 = (str(ctcf_1.degenerate_consensus))
m2 = (str(ctcf_2.degenerate_consensus))
print(m1)
print(m2)

for a in pairwise2.align.localms('YNNCCASYAGGKGGCRSNN', 'CNNYAGRTGGCGC', 2, 1, -3, -3): 
    print(format_alignment(*a)) 

"""
Explanation: Maximum number of species belong to a certain TF
End of explanation
"""


A = ['A', 'T', 'C', 'G']
import numpy as np
def calculate_pearson_corr(motif1,motif2, start=None):
    """
    Arguments
    ----------
    motif1: Bio.motifs
        motif instance of first motif
        
    motif2: Bio.motifs
        motif instance of seocnd
        
    start: int
        position of motif1 where alignment starts
        
    end: int
        position of motif1 where alignment ends
    Questions
    ---------
    This simply ignores the non-overlapping sequence of the motifs. I need
    to see what appraoches are taken
    """
    pwm1 = motif1.pwm
    pwm2 = motif2.pwm
    assert motif1.length >= motif2.length
    end = motif2.length+start
    #This step is mostly redundant
    xbar = [1/4*sum(pwm1[x][i] for x in A) for i in range(start,end)]
    ybar = [1/4*sum(pwm2[x][i] for x in A) for i in range(0,motif2.length)]
    
    
    sxy = np.array([sum((pwm1[x][i]-xbar[i])*(pwm2[x][i]-ybar[i]) for x in A) for i in range(0,motif2.length)])
    
    
    sxx = np.array([sum((pwm1[x][i]-xbar[i])**2 for x in A) for i in range(0,motif2.length)])
    syy = np.array([sum((pwm2[x][i]-ybar[i])**2 for x in A) for i in range(0,motif2.length)])
    
    pcc = sxy/(np.sqrt(sxx*syy))
    return pcc

pwm = ctcf_1.pwm
pwm['A'][0]+pwm['C'][0]+pwm['T'][0]+pwm['G'][0]

aln = pairwise2.align.localms('YNNCCASYAGGKGGCRSNN', 'CNNYAGRTGGCGC', 2, 1, -3, -3)

x=aln[0]

calculate_pearson_corr(ctcf_1, ctcf_2,4)

display_motifs([ctcf_1,ctcf_2])

"""
Explanation: Pearson Correlation Coefficient
\begin{align}
\mathcal{A} &= {A,C,T,G}\
PCC(X,Y) &= \frac{\sum_{a \in A} (X_a-\bar{X})(Y_b-\bar{Y})}{\sum_{a \in A} (X_a-\bar{X})^2 \sum_{a \in A}(Y_b-\bar{Y})}\
\bar{X} &= \frac{1}{|\mathcal{A}|} \sum_{a\in A} X_a\
\bar{Y} &= \frac{1}{|\mathcal{A}|} \sum_{a\in A} Y_a\
\end{align}
End of explanation
"""


