ó
°;çIc           @   s5   d  Z  d d l Z d d l Z d d d „  ƒ  YZ d S(   sD   Approximate calculation of appropriate thresholds for motif finding
iÿÿÿÿNt   ScoreDistributionc           B   sb   e  Z d  Z d d „ Z d d „ Z d „  Z d „  Z d „  Z d	 „  Z d
 e	 d „ Z
 d „  Z RS(   s   Class representing approximate score distribution for a given motif.

    Utilizes a dynamic programming approch to calculate the distribution of
    scores with a predefined precision. Provides a number of methods for calculating
    thresholds for motif occurences.
    i
   i   c         C   s  t  d | j ƒ  ƒ |  _ t d | j ƒ  ƒ |  j |  _ | | j |  _ |  j |  j d |  _ d g |  j |  _ d |  j |  j	 |  j ƒ <d g |  j |  _
 d |  j
 |  j	 |  j ƒ <| j ƒ  |  _ x? t | j ƒ  | j ƒ  ƒ D]" \ } } |  j | | | j ƒ qã Wd  S(   Ng        i   g      ð?(   t   mint	   min_scoret   maxt	   max_scoret   intervalt   lengtht   n_pointst   stept
   mo_densityt   _index_difft
   bg_densityt   ict   zipt   log_oddst   pwmt   modifyt
   background(   t   selft   motift	   precisiont   lot   mo(    (    s‡   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Motif/Thresholds.pyt   __init__   s    (g        c         C   s    t  | | d |  j |  j ƒ S(   Ng      à?(   t   intR   (   R   t   xt   y(    (    s‡   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Motif/Thresholds.pyR
      s    c         C   s!   t  d t |  j d | | ƒ ƒ S(   Ni    i   (   R   R   R   (   R   t   it   j(    (    s‡   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Motif/Thresholds.pyt   _add!   s    c   	      C   sÐ   d g |  j  } d g |  j  } x— | j ƒ  D]‰ } |  j | | ƒ } xm t |  j  ƒ D]\ } | |  j | | ƒ c |  j | | | 7<| |  j | | ƒ c |  j | | | 7<qV Wq- W| |  _ | |  _ d  S(   Ng        (   R   t   keysR
   t   rangeR   R	   R   (	   R   t   scorest   mo_probst   bg_probst   mo_newt   bg_newt   kt   dR   (    (    s‡   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Motif/Thresholds.pyR   $   s    +3	c         C   sO   |  j  } d } x+ | | k  r< | d 8} | |  j | 7} q W|  j | |  j S(   sh   
        Approximate the log-odds threshold which makes the type I error (false positive rate).
        g        i   (   R   R   R   R   (   R   t   fprR   t   prob(    (    s‡   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Motif/Thresholds.pyt   threshold_fpr/   s    	
c         C   sL   d } d } x+ | | k  r9 | d 7} | |  j  | 7} q W|  j | |  j S(   si   
        Approximate the log-odds threshold which makes the type II error (false negative rate).
        iÿÿÿÿg        i   (   R	   R   R   (   R   t   fnrR   R(   (    (    s‡   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Motif/Thresholds.pyt   threshold_fnr:   s    
g      ð?c         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(   sg   
        Approximate the log-odds threshold which makes FNR equal to FPR times rate_proportion
        g        g      ð?i   N(   R   R   R	   R   R   (   R   t   rate_proportiont   return_rateR   R'   R*   (    (    s‡   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Motif/Thresholds.pyt   threshold_balancedE   s    	
c         C   s   |  j  d d |  j ƒ S(   s*  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'   i   (   R)   R   (   R   (    (    s‡   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Motif/Thresholds.pyt   threshold_patserU   s    iè  (   t   __name__t
   __module__t   __doc__R   R
   R   R   R)   R+   t   FalseR.   R/   (    (    (    s‡   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Motif/Thresholds.pyR    
   s   				(    (   R2   t   matht   randomR    (    (    (    s‡   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Motif/Thresholds.pyt   <module>   s   