
    Ri                     4    d Z ddlmZ ddlmZ  G d d      Zy)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            	           e Zd ZdZddddej
                  ej                  dddf	dZd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd Zy)	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
                     ||}|	|	}|| _         i | _        || j                  |       || _        || _        || _        || _        d| _        || _        y)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	altcolours
             Y/home/agent/.friday_env/lib/python3.12/site-packages/Bio/Graphics/GenomeDiagram/_Graph.py__init__zGraphData.__init__,   sf    < E  H	MM$	 
     c                 6    |D ]  \  }}|| j                   |<    y)z/Add data as a list of (position, value) tuples.Nr   )r   r   posvals       r   r   zGraphData.set_data\   s"     	!HC DIIcN	!r   c                     g }| j                   D ]$  }| j                   |   }|j                  ||f       & |j                          |S )z9Return data as a list of sorted (position, value) tuples.)r   appendsort)r   r   xvalyvals       r   get_datazGraphData.get_dataa   sG    II 	&D99T?DKKt%	& 			r   c                 ,    |\  }}|| j                   |<   y)zCAdd a single point to the set of data as a (position, value) tuple.Nr   )r   pointr   r   s       r   	add_pointzGraphData.add_pointj   s    S		#r   c                     t        | j                  j                               }t        |      }|d   ||dz     ||dz     |d|z  dz     |d   fS )zCReturn (minimum, lowerQ, medianQ, upperQ, maximum) values as tuple.r      r	      )sortedr   valueslen)r   r   datalens      r   	quartileszGraphData.quartileso   sc    dii&&()d)GAAW!"H
 	
r   c                 @    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   	positionss     r   rangezGraphData.range{   s'     499%	 !im,,r   c                 v    t        | j                  j                               }t        |      t	        |      z  S )z2Return the mean value for the data points (float).)listr   r-   sumr.   )r   r   s     r   meanzGraphData.mean   s,    DII$$&'4y3t9$$r   c                     t        | j                  j                               }| j                         }d}|D ]  }|||z
  dz  z  } t	        |t        |      dz
  z        S )z:Return the sample standard deviation for the data (float).g        r	      )r5   r   r-   r7   r   r.   )r   r   mruntotalentrys        r   stdevzGraphData.stdev   se    DII$$&'IIK 	)Eq((H	) HD	A.//r   c                 ,    t        | j                        S )z,Return the number of points in the data set.)r.   r   )r   s    r   __len__zGraphData.__len__   s    499~r   c                 |   t        |t              r| j                  |   S t        |t              r|j                  }|j
                  }|j                  |j                  dk7  rt        g }t        | j                        D ].  }||k\  s	||k  s|j                  || j                  |   f       0 |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.
        r9   zNeed an integer or a slice)
isinstanceintr   slicestartstopstep
ValueErrorr,   r    	TypeError)r   indexlowhighoutlistr   s         r   __getitem__zGraphData.__getitem__   s     eS!99U##u% ++C::Dzz%%**/  Gdii( :#:#+NNC3#89: N899r   c                    d| j                    d| j                   g}|j                  dt        | j                        z         |j                  d| j                                 |j                  d| j                         d       |j                  d| j                         z         |j                  d| j                         z         d	j                  |      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   r7   r=   r0   r3   join)r   outstrs     r   __str__zGraphData.__str__   s    !$))F477)<=,s499~=>)$))+78DJJL#567>AQQ	
 	.=>yy  r   )__name__
__module____qualname____doc__r   
lightgreendarkseagreenr   r   r$   r'   r0   r3   r7   r=   r?   rM   rR    r   r   r   r      sh      $$.`!



	-%
	0:4
!r   r   N)rV   mathr   reportlab.libr   r   rY   r   r   <module>r\      s      a! a!r   