ó
ËdTc           @   sW   d  d l  m  Z  d  d l m Z d  d l Z d d d „  ƒ  YZ e d k rS d GHn  d S(	   iÿÿÿÿ(   t   time(   t   deepcopyNt   Summitsc           B   sA   e  Z d  „  Z d „  Z d d „ Z d d „ Z d d d „ Z RS(   c         C   s   i  |  _  d  S(   N(   t   data(   t   self(    (    sG   /oak/stanford/groups/akundaje/marinovg/programs/danpos-2.2.2/summits.pyt   __init__   s    c         C   sO  |  } xB| j  D]7} | j  | j d ƒ sx t j d g ƒ | j  | d <| j  | d j | j  | d j d d ƒn  | j  | d } | j } d } x¬ | | k  rF| | | j } | j  j | ƒ r$| | j  | j k r| j  | j | d d d ƒn  | j  | | | j  | d | <n d | j  | d | <| d 7} q› Wq Wd S(   sÉ   
        Description:
            retrieve occupancy value from a Wig class instance
        
        Parameter:
            wig: an Wig class instance
        
        Value:
            None
        t   vg        t   pt   refchecki    i   N(   R   t   has_keyt   numpyt   arrayt   resizet   sizet   step(   R   t   wigt   smtt   crt   pst   ltht   it   tp(    (    sG   /oak/stanford/groups/akundaje/marinovg/programs/danpos-2.2.2/summits.pyt   fetchValueFromWig
   s     /	 !#c         C   s‡  d d l  m } m } d d l m } d d l m } |  } | j } x;| j D]0}	 |	 GH| j |	 d }
 t	 j
 d g | d ƒ t	 j
 d g | d ƒ } } |
 j } | j | d d	 ƒ| j | d d	 ƒ| j j |	 ƒ r]| j |	 } d	 } xf | | k  rY| d
 |
 | d |
 | d |	 d | d d d | ƒ } | \ | | <| | <| d 7} q÷ Wn  | | j |	 d <| | j |	 d <qO Wd S(   s_  
        Description:
            Calculate nucleosome (summit) positioning score and P value.
        
        Parameter:
            wig: a Wig class instance containing nucleosome occupancy data
            rd: the half size of the region flanking each summit used to calculate nucleosome fuzziness
        
        Value:
            None
        iÿÿÿÿ(   t   rt   FloatVector(   t   shuffle(   t   log10fuztestR   g        i   R   i    t   pct   ptR   t   cwigt   twigt   rdi   t   st   pposN(   t   rpy2.robjectsR   R   t   randomR   t	   functionsR   R   R   R
   R   R   R   R	   t   None(   R   R   R   R   R   R   R   R   R   R   R   R    R!   R   t   wR   t   temp(    (    sG   /oak/stanford/groups/akundaje/marinovg/programs/danpos-2.2.2/summits.pyt   positioning&   s*    	5	5in   c         C   sê  | j  } t ƒ  } d } d } xÅ|  j D]º} | Gd G|  j | d } |  j | d }	 | | j 7} | j Gd GHd }
 x:|
 d k r³d }
 t j d g ƒ } t j d g ƒ } | j d } | j | d d	 d ƒ| j | d d	 d ƒ| d k  rý qz n  d } d } xb| | k  rm| | d | | } | | k rd| | |	 | | | <| | <| d 7} nü |
 d 7}
 | | d | | d } | | k  r¾| | |	 | | | <| | <| d 7} n¢ |	 | |	 | d k rý|	 | |	 | d <| | | | d <nc |	 | |	 | d k r`| | | | d d } | | | d <| j | | | j  |	 | d <n  | d 7} qW| d
 | d | k rÙ| d | d
 |	 d |	 d
 f \ | | <| | d <| | <| | d <| d 7} nÃ |	 d |	 d
 k r| d |	 d | | <| | <n{ |	 d |	 d
 k rk| d | d
 d | j | | d | d
 d | j  | | <| | <n | d
 |	 d
 | | <| | <| d 7} |
 d 7}
 | |  } | |  }	 qz W| j Gd GH| |  j | d <|	 |  j | d <q( Wt S(   s‚  
        Description:
            Merge neighboring summits whos distance from each other shorter than a specified distance.
            
        Parameter:
            wig: a Wig class instance containing nucleosome occupancy data.
            distance: merge neighboring summits whos distance from each other shorter than this value.
        
        Value:
            None.
        i    t   :R   R   s   summits, merging ...i   g        i   R   iÿÿÿÿiþÿÿÿt   left(   R   R    R   R   R
   R   R   t   True(   R   t   wgt   distanceR   t   ctimet   tnumt   onumR   R   t   vst   merget   npst   nvsR   R   t   nit   tdt   td2t   pos(    (    sG   /oak/stanford/groups/akundaje/marinovg/programs/danpos-2.2.2/summits.pyR2   N   sp    		 
#F J


i   c         C   s  | j  } | d | d } } t ƒ  } x_|  j D]T} g  }	 g  }
 g  } | Gd G|  j | d } |  j | d } | j d } d } xÃ | | k  rK| | d | | } | | k r>| | k  r>t | | d | | d | ƒ } | j | | | k r>|	 j | d ƒ |
 j | | ƒ | j | j | | ƒ q>n  | d 7} q‰ Wt |	 ƒ } | GH| d | } xU |  j | D]F } t |  j | | ƒ |  j | | <|  j | | j | d	 d ƒqyWd } x¹ | | k  r„|	 | | } x\ |  j | D]M } |  j | | | | d !|  j | | | d | +d |  j | | | <qôW|
 | |  j | d | <| | |  j | d | <| d 7} qÌWq1 Wt	 S(
   sì  
        Description:
            Insert a summit between two neighboring summits whose distance from each other larger than a specified range.
        
        Parameter:
            hight: require the nucleosome occupancy of the inserted summit to be higher than or equal than this value.
            distance: require the distance between neighboring summits to be larger than distance*2.5 and smaller than distance*3.5 to allow insertion.
        
        Value:
            None
        g      @g      @R)   R   R   i   i    g      à?R   (
   R   R    R   R   t   intt   appendt   lenR   R   R+   (   R   R,   t   heightR-   R   t   midist   madisR.   R   t   idt   npt   nvR   R1   R   R   t   gsR   t   altht   nltht   kt   j(    (    sG   /oak/stanford/groups/akundaje/marinovg/programs/danpos-2.2.2/summits.pyt   fillgap™   sJ    		$""2N(   t   __name__t
   __module__R   R   R%   R(   R2   RG   (    (    (    sG   /oak/stanford/groups/akundaje/marinovg/programs/danpos-2.2.2/summits.pyR      s
   		(Kt   __main__t    (    (   R    t   copyR   R
   R   RH   (    (    (    sG   /oak/stanford/groups/akundaje/marinovg/programs/danpos-2.2.2/summits.pyt   <module>   s
   Æ