
    Ri                         d Z  G d d      Zy)zDApproximate calculation of appropriate thresholds for motif finding.c                   F    e Zd ZdZddZddZd Zd Zd Zd Z	dd	Z
d
 Zy)ScoreDistributiona  Class representing approximate score distribution for a given motif.

    Utilizes a dynamic programming approach to calculate the distribution of
    scores with a predefined precision. Provides a number of methods for calculating
    thresholds for motif occurrences.
    Nc                    |ut        d|j                               | _        t        d|j                               | j                  z
  | _        ||j
                  z  | _        |j                         | _        nmt        d|j                         | _        t        d|j                        | j                  z
  | _        ||j
                  z  | _        |j                  |      | _        | j                  | j                  dz
  z  | _	        dg| j                  z  | _
        d| j                  | j                  | j                         <   dg| j                  z  | _        d| j                  | j                  | j                         <   |Nt        |j                         |j                               D ]"  \  }}| j!                  |||j"                         $ yt%        |j
                        D ]  }dg| j                  z  }dg| j                  z  }	|dd|f   }|j'                         D ]  \  }
}||
   }t)        d||
|f         |z  }| j                  |      }t%        | j                        D ]\  }|| j+                  ||      xx   | j                  |   |z  z  cc<   |	| j+                  ||      xx   | j                  |   |z  z  cc<   ^  || _
        |	| _         y)zInitialize the class.N                 ?   )min	min_scoremax	max_scoreintervallengthn_pointsicmeanstep
mo_density_index_diff
bg_densityziplog_oddspwmmodify
backgroundrangeitemspow_add)selfmotif	precisionpssmr   lomopositionmo_newbg_newletterscorebgdis                  M/home/agent/.friday_env/lib/python3.12/site-packages/Bio/motifs/thresholds.py__init__zScoreDistribution.__init__   su   < eoo&78DNU__%67$..HDM%4DMhhjDG dhh/DNTXX.?DM%3DMii
+DGMMT]]Q%67	%$--/=@))$..99:%$--/=@))$..99:<enn.		< 6BBE$4$456 "$++. )..!X+&%'XXZ KMFE#F+BQVX%5 67"<B((/A"4==1 KtyyA/4??13E3JJ/tyyA/4??13E3JJ/K	K #)"()    c                 X    t        ||z
  d| j                  z  z   | j                  z        S )Ng      ?)intr   )r   xys      r-   r   zScoreDistribution._index_diff4   s'    AEC$))O+		9::r/   c                 N    t        dt        | j                  dz
  ||z               S )N    r   )r   r	   r   )r   r,   js      r-   r   zScoreDistribution._add7   s#    1c$--!+QU344r/   c                    dg| j                   z  }dg| j                   z  }|j                         D ]  \  }}| j                  |      }t        | j                         D ]b  }	|| j	                  |	|      xx   | j
                  |	   ||   z  z  cc<   || j	                  |	|      xx   | j                  |	   ||   z  z  cc<   d  || _        || _        y)z%Modify motifs and background density.r   N)r   r   r   r   r   r   r   )
r   scoresmo_probsbg_probsr&   r'   kvr+   r,   s
             r-   r   zScoreDistribution.modify:   s    &&LLN 	LDAq  #A4==) LtyyA'4??1+=+KK'tyyA'4??1+=+KK'L	L
 ! r/   c                     | j                   }d}||k  r|dz  }|| j                  |   z  }||k  r| j                  || j                  z  z   S )zVApproximate the log-odds threshold which makes the type I error (false positive rate).r   r   )r   r   r
   r   )r   fprr,   probs       r-   threshold_fprzScoreDistribution.threshold_fprF   sU    MMSjFADOOA&&D Sj ~~DII--r/   c                     d}d}||k  r|dz  }|| j                   |   z  }||k  r| j                  || j                  z  z   S )zWApproximate the log-odds threshold which makes the type II error (false negative rate).r   r   )r   r
   r   )r   fnrr,   r?   s       r-   threshold_fnrzScoreDistribution.threshold_fnrO   sQ    SjFADOOA&&D Sj ~~DII--r/   c                    | j                   }d}d}||z  |k  r2|dz  }|| j                  |   z  }|| j                  |   z  }||z  |k  r2|r| j                  || j                  z  z   |fS | j                  || j                  z  z   S )zMApproximate log-odds threshold making FNR equal to FPR times rate_proportion.r   r   r   )r   r   r   r
   r   )r   rate_proportionreturn_rater,   r>   rC   s         r-   threshold_balancedz$ScoreDistribution.threshold_balancedX   s    MMO#c)FA4??1%%C4??1%%C O#c) >>A		M1366>>A		M11r/   c                 B    | j                  d| j                   z        S )a"  Threshold selection mimicking the behaviour of patser (Hertz, Stormo 1999) software.

        It selects such a threshold that the log(fpr)=-ic(M)
        note: the actual patser software uses natural logarithms instead of log_2, so the numbers
        are not directly comparable.
        r   )r>   )r@   r   )r   s    r-   threshold_patserz"ScoreDistribution.threshold_patserf   s"     !!a$''k!22r/   )Ni  NN)r   )r   F)__name__
__module____qualname____doc__r.   r   r   r   r@   rD   rH   rJ    r/   r-   r   r   	   s0    !)F;5
!..23r/   r   N)rN   r   rO   r/   r-   <module>rP      s    Kd3 d3r/   