ó
?îec           @   st   d  Z  d d l m Z m Z d d l Z d d l m Z d d l j Z	 d e
 f d „  ƒ  YZ d d	 d „  ƒ  YZ d S(
   sG   
VMat Class

@author: Alicia Schep, Greenleaf Lab, Stanford University
iÿÿÿÿ(   t   signalt   ndimageN(   t   copyt
   VMat_Errorc           B   s    e  Z d  Z d „  Z d „  Z RS(   s!   Class for errors in VMat functionc         C   s   | |  _  d  S(   N(   t   value(   t   selfR   (    (    s1   /tmp/pip-install-bGcd2k/NucleoATAC/pyatac/VMat.pyt   __init__   s    c         C   s   t  |  j ƒ S(   N(   t   reprR   (   R   (    (    s1   /tmp/pip-install-bGcd2k/NucleoATAC/pyatac/VMat.pyt   __str__   s    (   t   __name__t
   __module__t   __doc__R   R   (    (    (    s1   /tmp/pip-install-bGcd2k/NucleoATAC/pyatac/VMat.pyR      s   	t   VMatc           B   s­   e  Z d  Z d „  Z d „  Z d „  Z d d „ Z d d „ Z d d d „ Z d	 „  Z	 d
 „  Z
 d „  Z d d d d „ Z d d „ Z d d „ Z d „  Z e d „  ƒ Z RS(   s.   Class for storing and processing V-plot matrixc         C   sY   | j  d | | k r& t d ƒ ‚ n  | |  _ | |  _ | |  _ | j  d d |  _ d S(   sà   
        Assumes Vplot is centered!
        Inputs:
        mat = matrix (as numpy array)
        lower = lower bound of insert sizes represented by mat
        upper = upper bound of insert sizes represented by mat
        i    s.   mat shape is not consistent with insert limitsi   i   N(   t   shapeR   t   matt   uppert   lowert   w(   R   R   R   R   (    (    s1   /tmp/pip-install-bGcd2k/NucleoATAC/pyatac/VMat.pyR      s    			c         C   sÌ   | |  j  } | |  j  } |  j | } |  j | d } | |  j j d k s| | d k  s| | d k  s| | |  j j d k r‹ t d ƒ ‚ n  |  j | | … | | … f |  _ | |  _  | |  _ | |  _ d S(   s—   reduce the size of the vplot

        lower is new lower bound
        upper is new upper bound
        w is new flanking region around center
        i   i    s    Mat is smaller than desired trimN(   R   R   R   R   R   R   (   R   R   R   R   t   upt   dnt   leftt   right(    (    s1   /tmp/pip-install-bGcd2k/NucleoATAC/pyatac/VMat.pyt   trim&   s    D"		c         C   sT  xMt  |  j |  j ƒ D]6} | |  j } | d d k rÀ |  j | d |  j d … f |  j | |  j d … f d d d … d } t j | | d  d d d … f ƒ |  j | d d … f <q |  j | |  j d … f |  j | d |  j … f d d d … d } t j | d d d … | |  j | d f f ƒ |  j | d d … f <q Wd S(   s    Force the V-plot to be symmetrici   i   Niÿÿÿÿg      à?(   t   rangeR   R   R   R   t   npt   hstack(   R   t   jt   it   lefthalft	   righthalf(    (    s1   /tmp/pip-install-bGcd2k/NucleoATAC/pyatac/VMat.pyt
   symmetrize7   s    K<Gt   samec         C   s  | d k r	t  j |  j j ƒ } xÙ t |  j |  j ƒ D]Â } | |  j } | d d k rŠ |  j | f d d d … | | d d … f <q7 |  j | d d … f d d d … | | d d … f <t  j |  j | d f |  j | d f g ƒ | | d f <q7 W| |  _ n| d k rt  j |  j j d |  j j d d f ƒ } x­ t |  j |  j ƒ D]– } | |  j } | d d k r´|  j | d d … f d d d … | | d d … f <qX|  j | d d … f d d d … d | | d d … f <qXW| |  _ |  j d 7_ n t	 d ƒ ‚ d S(	   s   Flip V-plotR   i   i   Niÿÿÿÿt   validi    s%   Mode must be one of 'same' or 'valid'(
   R   t   zerosR   R   R   R   R   t   meanR   t	   Exception(   R   t   modet   newR   R   (    (    s1   /tmp/pip-install-bGcd2k/NucleoATAC/pyatac/VMat.pyt   flipA   s&    06=-9>	i   c         C   s%   t  j j |  j | d d ƒ|  _ d S(   s#   smooth v-plot using gaussian kernelR$   t   constantN(   R   t   filterst   gaussian_filterR   (   R   t   sd(    (    s1   /tmp/pip-install-bGcd2k/NucleoATAC/pyatac/VMat.pyt   smoothZ   s    c         C   s(   t  j j |  j | | d d ƒ|  _ d S(   s!   smooth v-plot along one axis onlyR$   t   nearestN(   R   R(   t   gaussian_filter1dR   (   R   R*   t   axis(    (    s1   /tmp/pip-install-bGcd2k/NucleoATAC/pyatac/VMat.pyt   smooth1d^   s    c         C   s’   |  j  t j |  j  ƒ } t j |  j  j ƒ d |  j  j } |  j  t j |  j  | ƒ t j |  j  | ƒ |  _  |  j  |  j  j d d |  _  d S(   sa   normalize v matrix so that signal minus even background will be 1 divided by base pairs in windowg      ð?i   g      $@N(   R   R   t   sumt   onesR   t   size(   R   t   tmp1t   tmp2(    (    s1   /tmp/pip-install-bGcd2k/NucleoATAC/pyatac/VMat.pyt   normb   s    #4c         C   sd   x] t  |  j j d ƒ D]E } |  j | | j d | |  j ƒ t j |  j | ƒ |  j | <q Wd S(   s;   normalize vplot so insertsize matches supplied distributioni    R2   N(   R   R   R   t   getR   R   R0   (   R   t   distR   (    (    s1   /tmp/pip-install-bGcd2k/NucleoATAC/pyatac/VMat.pyt   norm_yh   s    c         C   s×  t  j |  j |  j d d |  j d ƒ |  _ |  j d |  j } xtt |  j j d ƒ D]\} xSt |  j j d ƒ D];} | |  j } t	 |  j | | f ƒ } | d d k r|  j | |  j | | d c | 7<|  j | |  j | | d c | 7<qw |  j | |  j | | d c | d 7<|  j | |  j | | d c | d 7<|  j | |  j | | d d c | d 7<|  j | |  j | | d d c | d 7<qw WqZ W|  j t
 |  j ƒ |  _ d S(   s:   convert the 2d matrix to a 1d representation of insertionsi   i   i    g      à?N(   R   R!   R   R   t   one_dR   R   R   R   R   R0   (   R   t   centerR   R   t   ilent   val(    (    s1   /tmp/pip-install-bGcd2k/NucleoATAC/pyatac/VMat.pyt
   converto1dl   s    /&)**.6c         C   s
  | d k r |  j } n9 | j |  j |  j |  j d d f k rQ t d ƒ ‚ n  t j ƒ  } t j	 | d d d d d |  j |  j |  j |  j d g ƒt j
 d	 ƒ t j d
 ƒ | rÉ t j | ƒ n  t j d d ƒ | rü | j | ƒ t j | ƒ n
 | j ƒ  d S(   sA   Plot current main matrix or specified matrix (of same dimensions)i   i   sP   dimensions of input mat should match                                 dim of vmatt   originR   t   interpolationR,   t   extents   Position relative to dyads   Insert sizet   shrinkgš™™™™™é?N(   t   NoneR   R   R   R   R   R   t   pltt   figuret   imshowt   xlabelt   ylabelt   titlet   colorbart   savefigt   closet   show(   R   R   RH   t   filenamet   fig(    (    s1   /tmp/pip-install-bGcd2k/NucleoATAC/pyatac/VMat.pyt   plot}   s"    *$c         C   s  t  j ƒ  } t |  j ƒ d } t  j t | | d ƒ |  j ƒ t  j d d t |  j ƒ d d d ƒt  j d d t |  j ƒ d d d ƒt  j d	 ƒ t  j	 d
 ƒ | r| j
 | ƒ t  j | ƒ d j | j d ƒ d  d g ƒ } t j | |  j d d ƒn
 | j ƒ  d S(   s2   plot the 1d insertion representation of the matrixi   i   i·ÿÿÿi    gš™™™™™ñ?t
   linestylest   dashediI   s   Position relative to dyads   Insertion Frequencyt   .iÿÿÿÿt   txtt	   delimiters   	N(   RC   RD   t   lenR9   RO   R   t   vlinest   maxRF   RG   RJ   RK   t   joint   splitR   t   savetxtRL   (   R   RM   RN   t   xlimt	   filename2(    (    s1   /tmp/pip-install-bGcd2k/NucleoATAC/pyatac/VMat.pyt   plot_1d‘   s    !&&#c         C   s×   t  j ƒ  } t j |  j d d ƒ} | t | ƒ } t  j t |  j |  j ƒ | ƒ t  j	 d ƒ t  j
 d ƒ | rÉ | j | ƒ t  j | ƒ d j | j d ƒ d  d g ƒ } t j | | d d	 ƒn
 | j ƒ  d
 S(   s3   plot the insert size disribution in the main matrixR.   i   s   Insert Sizet	   FrequencyRR   iÿÿÿÿRS   RT   s   	N(   RC   RD   R   R0   R   RO   R   R   R   RF   RG   RJ   RK   RX   RY   RZ   RL   (   R   RM   RN   t   insR\   (    (    s1   /tmp/pip-install-bGcd2k/NucleoATAC/pyatac/VMat.pyt   plot_insertsize¢   s    #c         C   sÉ   t  | d ƒ } | j d ƒ | j d ƒ | j d ƒ | j t |  j ƒ d ƒ | j d ƒ | j t |  j ƒ d ƒ | j d ƒ x4 |  j D]) } | j d j t t | ƒ ƒ d ƒ qŽ W| j ƒ  d	 S(
   s7   write text output description of VMat object attributesR   s   #VMat Descriptor File
s)   #Contains VMat and pertinent information
s   #lower
s   
s   #upper
s   #mat
s   	N(	   t   opent   writet   strR   R   R   RX   t   mapRK   (   R   RM   t   outt   row(    (    s1   /tmp/pip-install-bGcd2k/NucleoATAC/pyatac/VMat.pyt   save²   s    'c         C   sL  t  |  d ƒ } d } g  } xá | D]Ù } d | k r= d } q" d | k rR d } q" d | k rg d } q" d	 | k r| d
 } q" | d k r  t | j d ƒ ƒ } q" | d k rÄ t | j d ƒ ƒ } q" | d k r" | j t t | j d ƒ j d ƒ ƒ ƒ q" q" Wy t t j	 | ƒ | | ƒ } Wn t
 k
 r=t d ƒ ‚ n X| j ƒ  | S(   s,   Create VMat object from text descriptor filet   rt    s   #lowerR   s   #upperR   s   #matR   t   #t   others   
s   	s>   VMat decriptor file appeas to be missing someneeded components(   Ra   t   intt   stript   appendRd   t   floatRY   R   R   t   arrayt	   NameErrorR   RK   (   RM   t   infilet   stateR   t   lineR   R   R%   (    (    s1   /tmp/pip-install-bGcd2k/NucleoATAC/pyatac/VMat.pyRa   ¿   s2    				/
N(   R	   R
   R   R   R   R   R&   R+   R/   R5   R8   R=   RB   RO   R]   R`   Rg   t   staticmethodRa   (    (    (    s1   /tmp/pip-install-bGcd2k/NucleoATAC/pyatac/VMat.pyR      s   			
				(    (   R   t   scipyR    R   t   numpyR   R   t   matplotlib.pyplott   pyplotRC   R#   R   R   (    (    (    s1   /tmp/pip-install-bGcd2k/NucleoATAC/pyatac/VMat.pyt   <module>   s   