ó
	WáWc           @   sÝ   d  Z  d d l Z e j d ƒ i d d 6Z e j d e  d d l j Z d d l j	 Z
 d d l j Z d d l m Z m Z d d l j j Z d d l Z d d l Z d d l Z d „  Z d	 „  Z d
 „  Z d „  Z d S(   s@   
Created on 2015-03-27

@author: Ian McDowell, Dinesh Manandhar
iÿÿÿÿNt   Aggi   t   sizet   font(   t   linkaget
   dendrogramc         C   s«   xF |  j  j ƒ  D]5 \ } } | | k r8 | j d ƒ q | j d ƒ q Wd | k rh |  j j d ƒ n |  j j g  ƒ d | k r— |  j j d ƒ n |  j j g  ƒ d S(   so    
    see matplotlib examples:
    http://matplotlib.org/examples/pylab_examples/spine_placement_demo.html
    t   outwardi
   t   nonet   leftt   bottomN(   R   i
   (   t   spinest   itemst   set_positiont	   set_colort   yaxist   set_ticks_positiont	   set_tickst   xaxis(   t   axR	   t   loct   spine(    (    s   DP_GP/plot.pyt   adjust_spines   s    c         C   së  t  |  ƒ } t | ƒ } d }	 t t j | t |	 ƒ ƒ ƒ }
 d } t j |	 | ƒ } g  t d t | ƒ |	 ƒ D] } | | | |	 !^ qo } d } xRt | ƒ D]D\ } } t j	 d d d d5 d	 d
 d d d d ƒ } xÉt | ƒ D]»\ } } | j | | | d ƒ } t j t j t | ƒ t | ƒ d d ƒƒ } |  | j j | d t d |  | j j ƒ\ } } t j | j d d ƒ ƒ } | d d … d f } t j j j j | | | d | d | d | d d | ƒ| j t | ƒ t | ƒ f ƒ | s	| j d6 ƒ n  t j d d d d d d d ƒxG t |  | j ƒ D]2 } | j  | t j! | j" | ƒ d d d d ƒq<W| j  | | d d ƒt# j$ d d g d d d d  d d d! d d d" ƒ} t% j& g  g  d d d# d$ ƒ} | j' | j( d | | g d% d& d$ g d' d( d) t d* i d d+ 6ƒt) | d, d- g ƒ | d. k rJ| j* d/ ƒ n | j* d0 | ƒ | j+ | ƒ | j, | ƒ | j- d1 ƒ | j. d2 | ƒ | d 7} qâ Wt j/ ƒ  x5 | D]- } t j0 | d3 t1 | d ƒ d4 | ƒ q²WqŸ Wd S(7   s   
    Plot gene expression over a time course with a panel for each cluster. Each panel contains
    transparent red lines for the expression of each individual gene within the cluster, the
    cluster mean, and a ribbon twice the standard deviation about the cluster mean.  This is
    essentially a wrapper function for GPy.plotting.matplot_dep.base_plots.gpplot.
    
    :param clusters: dictionary of dp_cluster objects
    :type clusters: dict
    :param gene_expression_matrix: expression over timecourse of dimension |genes|x|timepoints|
    :type gene_expression_matrix: pandas dataframe
    :param t: sampled timepoints
    :type t: numpy array of floats
    :param time_unit: time units in reference to t, e.g. 'days','hr.','min.','sec.'
    :type t: str
    :param output_path_prefix: absolute path to output
    :type output_path_prefix: str
    :param plot_types: plots to be generated, e.g. ['png','pdf','svg'] or simply ['png']
    :type plot_types: list of strings
    
    :rtype: None (output is saved to file(s))
    
    i   i   i    i   t   numt   figsizei   i   t   dpii,  t	   facecolort   wt	   edgecolort   kiô  t   full_covt   kernt   axisNg      à?R   iýÿÿÿi   t   colort   blackt   lss   --t   alphat   redgš™™™™™¹?t   blues   #33CCFFt   lwg333333Ó?t   labels   individual gene trajectorys   cluster meanu   cluster mean Â± 2 x std. dev.R   i   t   frameont   propR   R   R   t    t   Times
   Time in %ss   Gene expressions
   Cluster %st   _gene_expression_fig_t   .(   i   i   (   iýÿÿÿi   (2   t   sortedt   lent   intt   npt   ceilt   floatt   xranget	   enumeratet   pltt   figuret   Nonet   add_subplott   vstackt   linspacet   mint   maxt   modelt   predictt   FalseR   t   hstackt   meant   GPyt   plottingt   matplot_dept
   base_plotst   gpplott   set_xlimt   set_ylimt   axhlinet   listt   memberst   plott   arrayt   ixt   mpatchest	   Rectanglet   mlinest   Line2Dt   legendt   linesR   t
   set_xlabelt
   set_xtickst   set_xticklabelst
   set_ylabelt	   set_titlet   tight_layoutt   savefigt   str(   t   clusterst   gene_expression_matrixt   tt	   time_unitt   output_path_prefixt
   plot_typest   do_not_mean_centert   IDst   total_subplotst   subplots_per_figt   total_no_of_figst
   total_colst
   total_rowst   it	   IDs_splitt   indext   ct   figt   IDR   t   Xgridt   mut   vt   genet   light_blue_patcht   red_linet	   plot_type(    (    s   DP_GP/plot.pyt   plot_cluster_gene_expression-   sR    6*-2=03
c         C   s  d |  } t  j d g ƒ t j d d ƒ } | j d d d d g ƒ } t  j | d	 d
 ƒ} t  j | d d d t j ƒ} | j	 g  ƒ | j
 g  ƒ | j d ƒ | j d d d d g ƒ } t  j | d t j ƒ}	 t  j d g ƒ | j	 g  ƒ | j
 g  ƒ | j d ƒ | j d d d d g ƒ }
 | d } |	 d } | | d d … f } | d d … | f } |
 j |  d d d d d t j j ƒ} |
 j	 g  ƒ |
 j
 g  ƒ | j d d d d g ƒ } t j | d | ƒ| j d d d d ƒ x) | D]! } t j | d | d d ƒqòW| S(   sí   
    Plot the posterior similarity matrix as heatmap with dendrogram. 
    
    dim(S) = n x n, where n = total number of genes.
    S[i,j] = (# samples gene i in cluster with gene j)/(# total samples)
    
    Hierarchically cluster by complete linkage for orderliness of visualization.
    Although of questionable usefulness, function returns all gene names in
    the order in which they were clustered. This list might be used to visually inspect heatmap,
    yet heatmap is largely intended for high-level view of data.
    
    :param sim_mat: sim_mat[i,j] = (# samples gene i in cluster with gene j)/(# total samples)
    :type sim_mat: numpy array of (0-1) floats
    :param output_path_prefix: absolute path to output
    :type output_path_prefix: str
    :param plot_types: plots to be generated, e.g. ['png','pdf','svg'] or simply ['png']
    :type plot_types: list of strings
    
    :rtype: array-like, names of genes in order of clustering (both left to right and top to bottom)
    
    i   R    R   i   i    g{®Gáz”?gš™™™™™É?g333333ã?t   methodt   completet   orientationt   rightt   color_thresholdt   offt   leavesNt   aspectt   autot   origint   lowert   cmapgìQ¸…ëé?t   caxt   wspacet   hspaces%   _posterior_similarity_matrix_heatmap.t   bbox_inches(   i   i   (   t   scht   set_link_color_paletteR5   R6   t   add_axesR   R   R0   t   infRV   t
   set_yticksR   t   matshowt   cmt   binaryt   colorbart   subplots_adjustR[   (   t   sim_matRa   Rb   t   dist_matRn   t   ax1t   Yt   Z1t   ax2t   Z2t   axmatrixt   idx1t   idx2t   imt   axcolorRv   (    (    s   DP_GP/plot.pyt   plot_similarity_matrix€   s:    


'c      	   C   s  |  d d d … f } |  d d … d d … f j  d ƒ }  t j |  ƒ | } t t t j |  ƒ ƒ t t | ƒ ƒ ƒ } i  }	 xZ | D]R }
 t j |  |
 k ƒ \ } } t j	 | ƒ } | j
 d ƒ \ \ } } | |	 |
 <q† Wi  } x= t t |  ƒ ƒ D]) } t j	 |  | d d … f ƒ | | <qõ Wt |	 j ƒ  ƒ } | } t j d d d d d	 d
 d d ƒ } | j d d d ƒ } t t j j t j d d t | ƒ ƒ ƒ ƒ } xÏ | D]Ç } t | ƒ } | |	 | d } t j t | j ƒ  ƒ ƒ } x= t | ƒ D]/ } | | | k ró| | | d | | <qóqóW| j t j t | ƒ ƒ | | | | | d	 | d d ƒ| |	 | } q§W| j d ƒ | j d ƒ t j | d d d d d d ƒt j ƒ  t j d i d d 6ƒ } | j  ƒ  j! d ƒ x) | D]! } t j" | d | d d ƒqèWd S(   sÓ   
    Plot size of clusters over GS iterations after burn-in phase I 
    where x-axis is iteration and vertical thickness of band is proportional to 
    size of cluster. Each cluster has unique color. Burn-in phase II
    is indicated with a vertical line.
    
    :param all_clusterings: all_clusterings[i,j] is the cluster to which gene j belongs at iteration i
    :type all_clusterings: numpy array of ints
    :param burnIn_phaseI: iteration at which first burn-in phase ends
    :type burnIn_phaseI: int
    :param burnIn_phaseII: iteration at which second burn-in phase ends
    :type burnIn_phaseII: int
    :param m: number of "empty table" at each Gibbs sampling iteration
    :type m: int
    :param output_path_prefix: absolute path to output
    :type output_path_prefix: str
    :param plot_types: plots to be generated, e.g. ['png','pdf','svg'] or simply ['png']
    :type plot_types: list of strings
    
    :rtype: None (output is saved to file(s))
    
    i    Ni   R/   R   i   R   i,  R   R   R   R   g      à?R"   g      è?t
   Iterationss   Cluster sizeR   t   bR&   s   Burn-in phase II endsR(   i   R   g        s   _cluster_sizes.R‡   (   i   i   (#   t   astypeR0   R<   RJ   t   sett   uniquet   ranget   wheret   collectionst   Countert   most_commonR.   t   sumt   valuesR5   R6   R8   t   iterRŽ   t   rainbowR:   t   nextt   zerost   keysR-   t   fill_betweenRM   RU   RX   t   axvlineRZ   RS   t	   get_framet   set_linewidthR[   (   t   all_clusteringst   burnIn_phaseIt   burnIn_phaseIIt   mRa   Rb   t
   gene_namest   highest_cluster_numbert   all_clusterIDst   max_num_genes_per_clustert	   clusterIDt   GSiterst   genest   countst   GSitert   numbert   cluster_size_over_iterationst   iter_numt   total_heightt   current_heightRn   t   aR   t   clusterRm   t
   mid_line_yt"   half_num_genes_in_clust_over_iterst   legRv   (    (    s   DP_GP/plot.pyt"   plot_cluster_sizes_over_iterationsÂ   sL    %+'$-

(   t   __doc__t
   matplotlibt   useR   t   rct   matplotlib.pyplott   pyplotR5   t   matplotlib.patchest   patchesRO   t   matplotlib.linesRT   RQ   t   scipy.cluster.hierarchyR   R   RÇ   t	   hierarchyRˆ   t   numpyR0   R¦   RB   R   Rw   Rž   RË   (    (    (    s   DP_GP/plot.pyt   <module>   s    		S	B