o
    Rŀg                     @   s.   d Z ddlmZ ddlmZ G dd dZdS )zGraph module.

Provides:
 - GraphData - Contains data from which a graph will be drawn, and
   information about its presentation

For drawing capabilities, this module uses reportlab to draw and write
the diagram: http://www.reportlab.com
    )sqrt)colorsc                	   @   s   e Zd ZdZddddejejdddf	ddZdd Zdd	 Z	d
d Z
dd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )	GraphDataa  Graph Data.

    Attributes:
     - id    Unique identifier for the data
     - data  Dictionary of describing the data, keyed by position
     - name  String describing the data
     - style String ('bar', 'heat', 'line') describing how to draw the data
     - poscolor     colors.Color for drawing high (some styles) or all
       values
     - negcolor     colors.Color for drawing low values (some styles)
     - linewidth     Int, thickness to draw the line in 'line' styles

    Nbarc
           
      C   s^   |dur|}|	dur|	}|| _ i | _|dur| | || _|| _|| _|| _d| _|| _dS )a  Initialize.

        Arguments:
         - id    Unique ID for the graph
         - data  List of (position, value) tuples
         - name  String describing the graph
         - style String describing the presentation style ('bar', 'line',
           'heat')
         - color   colors.Color describing the color to draw all or the
           'high' (some styles) values (overridden by backwards
           compatible argument with UK spelling, colour).
         - altcolor colors.Color describing the color to draw the 'low'
           values (some styles only) (overridden by backwards
           compatible argument with UK spelling, colour).
         - center Value at which x-axis crosses y-axis.

        N   )	iddataset_datanamestyleposcolornegcolor	linewidthcenter)
selfr   r   r
   r   coloraltcolorr   colour	altcolour r   U/var/www/html/myenv/lib/python3.10/site-packages/Bio/Graphics/GenomeDiagram/_Graph.py__init__,   s   

zGraphData.__init__c                 C   s   |D ]	\}}|| j |< qdS )z/Add data as a list of (position, value) tuples.Nr   )r   r   posvalr   r   r   r	   \   s   zGraphData.set_datac                 C   s4   g }| j D ]}| j | }|||f q|  |S )z9Return data as a list of sorted (position, value) tuples.)r   appendsort)r   r   xvalyvalr   r   r   get_dataa   s   

zGraphData.get_datac                 C   s   |\}}|| j |< dS )zCAdd a single point to the set of data as a (position, value) tuple.Nr   )r   pointr   r   r   r   r   	add_pointj   s   zGraphData.add_pointc                 C   sH   t | j }t|}|d ||d  ||d  |d| d  |d fS )zCReturn (minimum, lowerQ, medianQ, upperQ, maximum) values as tuple.r      r      )sortedr   valueslen)r   r   datalenr   r   r   	quartileso   s   

zGraphData.quartilesc                 C   s   t | j}|d |d fS )zReturn range of data as (start, end) tuple.

        Returns the range of the data, i.e. its start and end points on
        the genome as a (start, end) tuple.
        r   r$   )r%   r   )r   	positionsr   r   r   range{   s   
zGraphData.rangec                 C   s   t | j }t|t| S )z2Return the mean value for the data points (float).)listr   r&   sumr'   )r   r   r   r   r   mean   s   zGraphData.meanc                 C   sH   t | j }|  }d}|D ]
}||| d 7 }qt|t|d  S )z:Return the sample standard deviation for the data (float).g        r      )r,   r   r&   r.   r   r'   )r   r   mruntotalentryr   r   r   stdev   s   zGraphData.stdevc                 C   s
   t | jS )z,Return the number of points in the data set.)r'   r   )r   r   r   r   __len__   s   
zGraphData.__len__c                 C   s   t |tr
| j| S t |tr?|j}|j}|jdur!|jdkr!tg }t| jD ]}||kr<||kr<|	|| j| f q(|S t
d)a@  Return data value(s) at the given position.

        Given an integer representing position on the sequence
        returns a float - the data value at the passed position.

        If a slice, returns graph data from the region as a list or
        (position, value) tuples. Slices with step are not supported.
        Nr/   zNeed an integer or a slice)
isinstanceintr   slicestartstopstep
ValueErrorr%   r   	TypeError)r   indexlowhighoutlistr   r   r   r   __getitem__   s   
	

zGraphData.__getitem__c                 C   s   d| j  d| j g}|dt| j  |d|    |d|  d |d|    |d|    d		|S )
z*Return a string describing the graph data.z
GraphData: z, ID: zNumber of points: %dzMean data value: zSample SD: z.3fz,Minimum: %s
1Q: %s
2Q: %s
3Q: %s
Maximum: %szSequence Range: %s..%s
)
r
   r   r   r'   r   r.   r3   r)   r+   join)r   outstrr   r   r   __str__   s   

zGraphData.__str__)__name__
__module____qualname____doc__r   
lightgreendarkseagreenr   r	   r   r!   r)   r+   r.   r3   r4   rA   rE   r   r   r   r   r      s,    
0	r   N)rI   mathr   reportlab.libr   r   r   r   r   r   <module>   s   
