o
    RŀgU$                     @   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 ddl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)BarChartProperties)VerticalBarChart)Drawing)String)TypedPropertyCollection)colors)letter)inch)_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 )Initialize the class.      N)distributionsnumber_of_columnsr   	page_size
title_sizeoutput_format)selfr    r   M/var/www/html/myenv/lib/python3.10/site-packages/Bio/Graphics/Distribution.py__init__&   s
   
zDistributionPage.__init__c                 C   s   | j \}}t||}| |||| td }|td  }|dt  }dt }	|| | j }
tt| j| 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	   r   mathceillenr   _draw_distributions_draw_legendr
   r   )r   output_filetitlewidthheightcur_drawing	cur_x_pos	end_x_pos	cur_y_pos	end_y_posx_pos_change
num_y_rowsy_pos_changer   r   r   draw1   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   O   s
   zDistributionPage._draw_titlec              	   C   s   t t|D ]L}|d | j t| jkrt| j|| j  }n| j}t |D ]+}	|| j |	 }
| j|
 }||	|  }|| }|||  }|| }|||||| q&q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#   start_x_posr(   start_y_posr*   num_y_drawings	y_drawingnum_x_drawings	x_drawingdist_numcur_distributionx_posr%   r'   y_posr   r   r   r   X   s   
z$DistributionPage._draw_distributionsc                 C      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          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u 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_infox_axis_titley_axis_titlechart_titlechart_title_sizepadding_percent)r   rJ   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_| j	dd D ]}t||jjk r^t||j_t||jjkrlt||j_qNt| j	dkrd|_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   rM   r   _determine_positionxyabsr!   r"   rJ   datamin	valueAxisvalueMinmaxvalueMaxr   groupSpacingr   r   strokeWidthr   greenstrokeColor	fillColorbarsr2   )r   r#   start_xrA   end_xend_y	bar_chartx_startx_endy_starty_enddata_setstyler   r   r   r+      s:   zBarChartDistribution.drawc           
      C   sT   ||| d  }|| j ||  d  }t|||}	d|	_| j|	_d|	_||	 dS r,   )rO   r   r/   rN   r0   r1   r2   )
r   r#   r    r`   rA   ra   rb   x_centerr?   r3   r   r   r   r      s   z BarChartDistribution._draw_titlec                 C   s^   | j ||  }| j ||  }|| }|| }| jr!|| | 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.
        )rO   rM   rN   )r   r`   rA   ra   rb   	x_padding	y_paddingnew_x_start	new_x_endnew_y_start	new_y_endr   r   r   rP      s   z(BarChartDistribution._determine_position)N)rC   rD   rE   rF   r   r+   r   rP   r   r   r   r   rG      s    
,rG   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   r@   )r   Nr   )r   r   r   r   r      rB   zLineDistribution.__init__c                 C   r@   )z2Draw a line distribution into the current drawing.Nr   )r   r#   r`   rA   ra   rb   r   r   r   r+      rB   zLineDistribution.drawN)rC   rD   rE   rF   r   r+   r   r   r   r   rq      s    rq   )rF   r   #reportlab.graphics.charts.barchartsr   r   reportlab.graphics.shapesr   r   reportlab.graphics.widgetbaser   reportlab.libr   reportlab.lib.pagesizesr   reportlab.lib.unitsr	   Bio.Graphicsr
   r   rG   rq   r   r   r   r   <module>   s   
kf