
:>"^¥$  ã               @   sÞ   d  Z  d d l Z d d l m Z d d l m Z d d l m Z d d l m	 Z	 m
 Z
 d d l m Z d d l m Z d d	 l m Z d d
 l m Z Gd d „  d e ƒ Z Gd d „  d e ƒ Z Gd d „  d e ƒ Z d S)as  Display information distributed across a Chromosome-like object.

These classes are meant to show the distribution of some kind of information
as it changes across any kind of segment. It was designed with chromosome
distributions in mind, but could also work for chromosome regions, BAC clones
or anything similar.

Reportlab is used for producing the graphical output.
é    N)Úletter)Úinch)Úcolors)ÚDrawingÚString)ÚVerticalBarChart)ÚBarChartProperties)ÚTypedPropertyCollection)Ú_writec               @   sU   e  Z d  Z d Z d d d „ Z d d „  Z d d „  Z d	 d
 „  Z d d „  Z d S)ÚDistributionPagez‰Display a grouping of distributions on a page.

    This organizes Distributions, and will display them nicely
    on a single page.
    Zpdfc             C   s1   g  |  _  d |  _ t |  _ d |  _ | |  _ d S)zInitialize.é   é   N)ÚdistributionsÚnumber_of_columnsr   Ú	page_sizeÚ
title_sizeÚoutput_format)Úselfr   © r   ú>/tmp/pip-build-ww9dw3qa/biopython/Bio/Graphics/Distribution.pyÚ__init__'   s
    				zDistributionPage.__init__c             C   sú   |  j  \ } } t | | ƒ } |  j | | | | ƒ t d } | t d } | d t } d t }	 | | t |  j ƒ }
 t j t t |  j	 ƒ ƒ t |  j ƒ ƒ } | |	 | } |  j
 | | |
 | | | ƒ |  j | d t | ƒ t | | |  j ƒ S)zêDraw out the distribution information.

        Arguments:
         - output_file - The name of the file to output the information to,
           or a handle to write to.
         - title - A title to display on the graphic.

        g      à?g      ø?g      @)r   r   Ú_draw_titler   Úfloatr   ÚmathÚceilÚlenr   Ú_draw_distributionsÚ_draw_legendr
   r   )r   Úoutput_fileÚtitleÚwidthÚheightÚcur_drawingZ	cur_x_posÚ	end_x_posZ	cur_y_posÚ	end_y_posÚx_pos_changeZ
num_y_rowsÚy_pos_changer   r   r   Údraw2   s    	

%zDistributionPage.drawc             C   sI   t  | d | t | ƒ } d | _ |  j | _ d | _ | j | ƒ d S)z5Add the title of the figure to the drawing (PRIVATE).é   zHelvetica-BoldÚmiddleN)r   r   ÚfontNamer   ÚfontSizeÚ
textAnchorÚadd)r   r"   r   r    r!   Útitle_stringr   r   r   r   R   s
    		zDistributionPage._draw_titlec             C   så   xÞ t  t | ƒ ƒ D]Ê } | d |  j t |  j ƒ k rV t |  j ƒ | |  j } n	 |  j } x{ t  | ƒ D]m }	 | |  j |	 }
 |  j |
 } | |	 | } | | } | | | } | | } | j | | | | | ƒ ql Wq Wd S)a  Draw all of the distributions on the page (PRIVATE).

        Arguments:
         - cur_drawing - The drawing we are working with.
         - start_x_pos - The x position on the page to start drawing at.
         - x_pos_change - The change in x position between each figure.
         - start_y_pos - The y position on the page to start drawing at.
         - y_pos_change - The change in y position between each figure.
         - num_y_drawings - The number of drawings we'll have in the y
           (up/down) direction.

        r   N)ÚrangeÚintr   r   r   r'   )r   r"   Zstart_x_posr%   Zstart_y_posr&   Znum_y_drawingsZ	y_drawingZnum_x_drawingsZ	x_drawingZdist_numZcur_distributionZx_posr#   r$   Úy_posr   r   r   r   [   s     	

z$DistributionPage._draw_distributionsc             C   s   d S)zqAdd a legend to the figure (PRIVATE).

        Subclasses can implement to provide a specialized legend.
        Nr   )r   r"   Ústart_yr    r   r   r   r   †   s    zDistributionPage._draw_legendN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r'   r   r   r   r   r   r   r   r       s    	+r   c               @   sI   e  Z d  Z d Z d d d „ Z d d „  Z d d „  Z d	 d
 „  Z d S)ÚBarChartDistributionz6Display the distribution of values as a bunch of bars.Nc             C   sL   | d k r g  } | |  _  d |  _ d |  _ d |  _ d |  _ d |  _ d S)a'  Initialize a Bar Chart display of distribution info.

        Attributes:
         - display_info - the information to be displayed in the distribution.
           This should be ordered as a list of lists, where each internal list
           is a data set to display in the bar chart.

        NÚ é
   g333333Ã?)Údisplay_infoZx_axis_titleZy_axis_titleÚchart_titleÚchart_title_sizeÚpadding_percent)r   r:   r   r   r   r   ‘   s    						zBarChartDistribution.__init__c             C   s¦  t  ƒ  } |  j r1 |  j | |  j | | | | ƒ |  j | | | | ƒ \ } } }	 }
 | | _ |	 | _ t | | ƒ | _ t |	 |
 ƒ | _ |  j	 | _
 t |  j	 d ƒ | j _ t |  j	 d ƒ | j _ xo |  j	 d d … D]Z } t | ƒ | j j k  rt | ƒ | j _ t | ƒ | j j k rß t | ƒ | j _ qß Wt |  j	 ƒ d k r•d | _ t t ƒ } d | _ t j | _ t j | d _ | | _ | j | ƒ d S)z6Draw a bar chart with the info in the specified range.r   r   N)r   r;   r   Ú_determine_positionÚxÚyÚabsr    r!   r:   ÚdataÚminZ	valueAxisZvalueMinÚmaxZvalueMaxr   ZgroupSpacingr	   r   ZstrokeWidthr   ZgreenZstrokeColorZ	fillColorZbarsr-   )r   r"   Ústart_xr2   Úend_xÚend_yZ	bar_chartZx_startZx_endZy_startZy_endZdata_setÚstyler   r   r   r'   ¥   s4    							zBarChartDistribution.drawc       
      C   sl   | | | d } | |  j  | | d } t | | | ƒ }	 d |	 _ |  j |	 _ d |	 _ | j |	 ƒ d S)z5Add the title of the figure to the drawing (PRIVATE).r(   zHelvetica-Boldr)   N)r=   r   r*   r<   r+   r,   r-   )
r   r"   r   rE   r2   rF   rG   Zx_centerr1   r.   r   r   r   r   Ñ   s    		z BarChartDistribution._draw_titlec             C   sw   |  j  | | } |  j  | | } | | } | | } |  j rS | | |  j }	 n
 | | }	 | | }
 | | |	 |
 f S)a-  Calculate the position of the chart with blank space (PRIVATE).

        This uses some padding around the chart, and takes into account
        whether the chart has a title. It returns 4 values, which are,
        in order, the x_start, x_end, y_start and y_end of the chart
        itself.
        )r=   r;   r<   )r   rE   r2   rF   rG   Z	x_paddingZ	y_paddingZnew_x_startZ	new_x_endZnew_y_startZ	new_y_endr   r   r   r>   Ü   s    

	

z(BarChartDistribution._determine_position)r3   r4   r5   r6   r   r'   r   r>   r   r   r   r   r7   Ž   s
   ,r7   c               @   s.   e  Z d  Z d Z d d „  Z d d „  Z d S)ÚLineDistributionzßDisplay the distribution of values as connected lines.

    This distribution displays the change in values across the object as
    lines. This also allows multiple distributions to be displayed on a
    single graph.
    c             C   s   d S)zInitialize.Nr   )r   r   r   r   r   ü   s    zLineDistribution.__init__c             C   s   d S)z2Draw a line distribution into the current drawing.Nr   )r   r"   rE   r2   rF   rG   r   r   r   r'      s    zLineDistribution.drawN)r3   r4   r5   r6   r   r'   r   r   r   r   rI   ô   s   rI   )r6   r   Zreportlab.lib.pagesizesr   Zreportlab.lib.unitsr   Zreportlab.libr   Zreportlab.graphics.shapesr   r   Z#reportlab.graphics.charts.barchartsr   r   Zreportlab.graphics.widgetbaser	   ZBio.Graphicsr
   Úobjectr   r7   rI   r   r   r   r   Ú<module>   s   nf