o
    Rŀg                     @   s   d Z G dd dZdS )zDApproximate calculation of appropriate thresholds for motif finding.c                   @   sV   e Zd ZdZdddZdddZd	d
 Zdd Zdd Zdd Z	dddZ
dd ZdS )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.
    N  c                 C   s  |du r#t d| | _td| | j | _||j | _| | _nt d|j | _td|j| j | _||j | _||| _| j| jd  | _	dg| j | _
d| j
| | j < dg| j | _d| j| | j < |du rt| | D ]\}}| |||j qxdS t|jD ]c}dg| j }dg| j }	|dd|f }| D ]B\}
}||
 }td||
|f | }| |}t| jD ]$}|| ||  | j
| | 7  < |	| ||  | j| | 7  < qq|| _
|	| _qdS )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i r+   I/var/www/html/myenv/lib/python3.10/site-packages/Bio/motifs/thresholds.py__init__   sB   
"$zScoreDistribution.__init__r   c                 C   s   t || d| j  | j S )Ng      ?)intr   )r   xyr+   r+   r,   r   4   s   zScoreDistribution._index_diffc                 C   s   t dt| jd || S )N    r   )r	   r   r   )r   r*   jr+   r+   r,   r   7   s   zScoreDistribution._addc           
      C   s   dg| j  }dg| j  }| D ]7\}}| |}t| j D ](}	|| |	|  | j|	 ||  7  < || |	|  | j|	 ||  7  < qq|| _|| _dS )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*   r+   r+   r,   r   :   s   
&(
zScoreDistribution.modifyc                 C   s@   | j }d}||k r|d8 }|| j| 7 }||k s	| j|| j  S )zVApproximate the log-odds threshold which makes the type I error (false positive rate).r   r   )r   r   r   r   )r   fprr*   probr+   r+   r,   threshold_fprF   s   zScoreDistribution.threshold_fprc                 C   s>   d}d}||k r|d7 }|| j | 7 }||k s| j|| j  S )zWApproximate the log-odds threshold which makes the type II error (false negative rate).r   r   )r   r   r   )r   fnrr*   r9   r+   r+   r,   threshold_fnrO   s   zScoreDistribution.threshold_fnrr   Fc                 C   sr   | j }d}d}|| |k r%|d8 }|| j| 7 }|| j| 8 }|| |k s|r1| j|| j  |fS | j|| j  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*   r8   r<   r+   r+   r,   threshold_balancedX   s   z$ScoreDistribution.threshold_balancedc                 C   s   | j d| j  d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   )r8   )r:   r   )r   r+   r+   r,   threshold_patserf   s   z"ScoreDistribution.threshold_patser)Nr   NN)r   )r   F)__name__
__module____qualname____doc__r-   r   r   r   r:   r=   r@   rA   r+   r+   r+   r,   r   	   s    

#	
	r   N)rE   r   r+   r+   r+   r,   <module>   s   