B
    b$                 @   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 G dd dZG dd dZG dd d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               @   s:   e Zd ZdZdddZdd Zdd Zd	d
 Zdd ZdS )DistributionPagezDisplay a grouping of distributions on a page.

    This organizes Distributions, and will display them nicely
    on a single page.
    pdfc             C   s"   g | _ d| _t| _d| _|| _dS )zInitialize the class.      N)distributionsnumber_of_columnsr   	page_size
title_sizeoutput_format)selfr    r   8lib/python3.7/site-packages/Bio/Graphics/Distribution.py__init__'   s
    zDistributionPage.__init__c             C   s   | j \}}t||}| |||| td }|td  }|dt  }dt }	|| t| j }
ttt| j	t| j }||	 | }| 
|||
||| | |dt | t||| jS )zDraw 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   mathZceillenr   _draw_distributions_draw_legendr
   r   )r   Z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   s6   t |d |t |}d|_| j|_d|_|| 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   xt t|D ]}|d | j t| jkr@t| j|| j  }n| j}xbt |D ]V}	|| j |	 }
| j|
 }||	|  }|| }|||  }|| }|||||| qPW qW d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)r   )	__name__
__module____qualname____doc__r   r&   r   r   r   r   r   r   r   r       s   
 	+r   c               @   s2   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   s4   |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   r9   r   r   r   r      s    	zBarChartDistribution.__init__c             C   s*  t  }| jr"| || j|||| | ||||\}}}	}
||_|	|_t|| |_t|	|
 |_| j	|_
t| j	d |j_t| j	d |j_xN| j	dd D ]<}t||jjk rt||j_t||jjkrt||j_qW t| j	dkrd|_tt}d|_tj|_tj|d _||_|| 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    r9   dataminZ	valueAxisZvalueMinmaxZvalueMaxr   ZgroupSpacingr	   r   ZstrokeWidthr   ZgreenZstrokeColorZ	fillColorZbarsr,   )r   r!   start_xr1   end_xend_yZ	bar_chartZx_startZx_endZy_startZy_endZdata_setZstyler   r   r   r&      s4    zBarChartDistribution.drawc       
      C   sT   ||| d  }|| j ||  d  }t|||}	d|	_| j|	_d|	_||	 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   rD   r1   rE   rF   Zx_centerr0   r-   r   r   r   r      s    z BarChartDistribution._draw_titlec             C   s^   | j ||  }| j ||  }|| }|| }| jrB|| | 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   rD   r1   rE   rF   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)N)r2   r3   r4   r5   r   r&   r   r=   r   r   r   r   r6      s
   
,r6   c               @   s    e Zd ZdZdd Zdd ZdS )LineDistributionzDisplay 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 the class.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!   rD   r1   rE   rF   r   r   r   r&      s    zLineDistribution.drawN)r2   r3   r4   r5   r   r&   r   r   r   r   rG      s   rG   )r5   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
   r   r6   rG   r   r   r   r   <module>   s   nf