
:>"^¾  ã               @   s    d  Z  Gd d „  d e ƒ Z d S)zDApproximate calculation of appropriate thresholds for motif finding.c               @   s‹   e  Z d  Z d Z d d d d d d „ Z d d d	 „ Z d
 d „  Z d d „  Z d d „  Z d d „  Z	 d 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 k re t  d | j ƒ  ƒ |  _ t d | j ƒ  ƒ |  j |  _ | | j |  _ | j ƒ  |  _ nS t  d | j  ƒ |  _ t d | j ƒ |  j |  _ | | j |  _ | j | ƒ |  _ |  j |  j d |  _	 d g |  j |  _
 d |  j
 |  j |  j ƒ <d g |  j |  _ d |  j |  j |  j ƒ <| d k rzx[t | j ƒ  | j ƒ  ƒ D]" \ } } |  j | | | j ƒ qQWnxt | j ƒ D]} d g |  j } d g |  j }	 | d d … | f } x´ | j ƒ  D]¦ \ }
 } | |
 } t d | |
 | f ƒ | } |  j | ƒ } xa t |  j ƒ D]P } | |  j | | ƒ |  j
 | | 7<|	 |  j | | ƒ |  j | | 7<q%WqÓW| |  _
 |	 |  _ qŠWd S)zInitialize the class.Ng        é   g      ð?é   )ÚminÚ	min_scoreÚmaxZ	max_scoreÚintervalÚlengthÚn_pointsÚicZmeanÚstepÚ
mo_densityÚ_index_diffÚ
bg_densityÚzipZlog_oddsZpwmÚmodifyÚ
backgroundÚrangeÚitemsÚpowÚ_add)ÚselfZmotifZ	precisionZpssmr   ÚloÚmoÚpositionÚmo_newÚbg_newÚletterÚscoreÚbgÚdÚi© r#   ú:/tmp/pip-build-ww9dw3qa/biopython/Bio/motifs/thresholds.pyÚ__init__   s<    (
%-	zScoreDistribution.__init__g        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  } x• | j ƒ  D]‡ \ } } |  j | ƒ } xi t |  j  ƒ D]X }	 | |  j |	 | ƒ |  j |	 | | 7<| |  j |	 | ƒ |  j |	 | | 7<qX Wq- W| |  _ | |  _ d S)z%Modify motifs and background density.g        N)r   r   r   r   r   r   r   )
r   ZscoresZmo_probsZbg_probsr   r   ÚkÚvr!   r"   r#   r#   r$   r   :   s    )1	zScoreDistribution.modifyc             C   sO   |  j  } d } x+ | | k  r< | d 8} | |  j | 7} q W|  j | |  j S)zVApproximate the log-odds threshold which makes the type I error (false positive rate).g        r   )r   r   r   r   )r   Úfprr"   Úprobr#   r#   r$   Úthreshold_fprF   s    	
zScoreDistribution.threshold_fprc             C   sL   d } d } x+ | | k  r9 | d 7} | |  j  | 7} q W|  j | |  j S)zWApproximate the log-odds threshold which makes the type II error (false negative rate).r   g        éÿÿÿÿ)r   r   r   )r   Úfnrr"   r.   r#   r#   r$   Úthreshold_fnrO   s    
zScoreDistribution.threshold_fnrg      ð?Fc             C   sŒ   |  j  } d } d } x@ | | | k  rW | d 8} | |  j | 7} | |  j | 8} q W| rv |  j | |  j | f S|  j | |  j Sd S)zMApproximate log-odds threshold making FNR equal to FPR times rate_proportion.g        g      ð?r   N)r   r   r   r   r   )r   Zrate_proportionZreturn_rater"   r-   r1   r#   r#   r$   Úthreshold_balancedX   s    	
z$ScoreDistribution.threshold_balancedc             C   s   |  j  d d |  j ƒ 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   r#   r#   r$   Úthreshold_patserf   s    z"ScoreDistribution.threshold_patseriè  )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r%   r   r   r   r/   r2   r3   r4   r#   r#   r#   r$   r   	   s   #		r   N)r8   Úobjectr   r#   r#   r#   r$   Ú<module>   s   