
    DUf`                         d dl Z d dlmZ d dlZd dlmZ d dlZ	d dl
Zd dlmZ ddlmZ dZdZdd	gZd
eeef         defdZ	 ddZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZdS )    N)Union)to_rgb   )arropsskybluedimgrayplot_intervalsto_ucsc_colorstringcolorreturnc                     t          j        |           rdS d                    d t          |           D                       S )a]  
    Convert any matplotlib color identifier into a UCSC itemRgb color string.

    Parameters
    ----------
    color : str or tuple
        Any valid matplotlib color representation (e.g. 'red', 'tomato',
        '#ff0000', '#ff00', "#ff000055", (1, 0, 0), (1, 0, 0, 0.5))

    Returns
    -------
    str
        A UCSC itemRgb colorstring of the form "r,g,b" where r, g, and b are
        integers between 0 and 255, inclusive.

    Notes
    -----
    The alpha (opacity) channel is ignored if represented in the input.

    Null values are converted to "0", which is shorthand for "0,0,0" (black).
    Note that BED9+ files with uninformative itemRgb values should use "0" as
    the itemRgb value on every data line.

    Examples
    --------
    >>> to_ucsc_colorstring("red")
    '255,0,0'
    >>> to_ucsc_colorstring("tomato")
    '255,99,71'
    >>> df["itemRgb"] = df["color"].apply(to_ucsc_colorstring)
    >>> df
    chrom  start  end  color  itemRgb
    chr1   0      10   red    255,0,0
    chr1   10     20   blue   0,0,255
    chr2   0      10   green  0,128,0
    chr2   10     20   None   0
    0,c              3   T   K   | ]#}t          t          |d z                      V  $dS )   N)strint).0xs     I/var/www/html/software/conda/lib/python3.11/site-packages/bioframe/vis.py	<genexpr>z&to_ucsc_colorstring.<locals>.<genexpr>;   s4      AAaCCLL))AAAAAA    )pdisnulljoinr   )r   s    r   r
   r
      sD    L 
y BsxxAA6%==AAAAAAr   333333?c           	          |t           n|}|t          n|}|t          j                    n|}|                    t
          j                            | ||dz  z
  f|| z
  |||                     d S )N   )	facecolor	edgecolor)DEFAULT_FACECOLORDEFAULT_EDGECOLORpltgca	add_patchmplpatches	Rectangle)startendlevelr   r    heightaxs          r   _plot_intervalr.   >   s     &/%6!!II%.%6!!IIjbBLLEFQJ&'%K 	 	
 	
    r   F
   r   c           	         t          j        |           } t          j        |          }|t          j        |           |t	          j        | |          }nt          j        |          }t          |t                    s|t          j	        |g          }nt          j	        |          }t          |t                    s|t          j	        |g          }nt          j	        |          }t          t          | ||||                    D ]?\  }\  }	}
}}}t          |	|
||           |t          j        |	|
z   dz  ||dd           @t          j        dt          j        |          dz              |+t          j        dt          j        |          dz              n!t          j        |d	         |d
                    t          j                                        d
           t          j                                        d           t          j        g            |rdS t          j        g            dS )a  
    Plot a collection of intervals.

    Parameters
    ----------
    starts, ends : np.ndarray
        A collection of intervals.

    levels : iterable or None
        The level of each interval, i.e. the y-coordinate at which the interval
        must be plotted. If None, it will be determined automatically.

    labels : str or iterable or None
        The label of each interval.

    colors : str or iterable or None.
        The color of each interval.

    xlim : (float, float) or None
        The x-span of the plot.

    show_coords : bool
        If True, plot x-ticks.

    figsize : (float, float) or None.
        The size of the figure. If None, plot within the current figure.

    N)figsize)r   r   center)horizontalalignmentverticalalignmentg      g      ?r   r   F)npasarrayr#   figurer   stack_intervals
isinstancer   	itertoolscycle	enumeratezipr.   textylimmaxxlimr$   
set_aspectset_frame_onyticksxticks)startsendslevelslabelscolorsrB   show_coordsr2   ir)   r*   r+   r   labels                 r   plot_intervals_arrrO   P   s"   L ZF:dD
7####~'55F##&# )6>&**((&# )6>&**((09FD&&&111 1  ,,E3ue 	uc5E::::H!$,"*    HT26&>>C'(((|rvd||c)****a$q'"""GIIGII5!!!JrNNN 
2r   c                    |                      dd          }|                     d                               dd          }|D ]\  }}	|j        |                                         }
t	          |t
          t          j        t          j	        f          rt          j
        |          |
         }n$|d}nt          dt          |                     t	          |t
          t          j        t          j	        f          rt          j
        |          |
         }n$|d}nt          dt          |                     t	          |t
          t          j        t          j	        f          rt          j
        |          |
         }n9|t	          |t                    r|}nt          dt          |                     t          |	j        |	j        ||||||	           t#          j        |           dS )
a  
    Plot a collection of intervals, one plot per chromosome.

    Parameters
    ----------
    df : pandas.DataFrame
        A collection of intervals.

    levels : iterable or None
        The level of each interval, i.e. the y-coordinate at which the interval
        must be plotted. If None, it will be determined automatically.

    labels : str or iterable or None
        The label of each interval.

    colors : str or iterable or None.
        The color of each interval.

    xlim : (float, float) or None
        The x-span of the plot.

    show_coords : bool
        If True, plot x-ticks.

    figsize : (float, float) or None.
        The size of the figure. If None, plot within the current figure.

    chromT)observed)dropNzUnknown type of levels: zUnknown type of labels: zUnknown type of colors: )rI   rJ   rK   rB   rL   r2   )groupbyreset_indexgroupsto_numpyr:   listr   Seriesr6   ndarrayr7   
ValueErrortyper   rO   r)   r*   r#   title)dfrI   rJ   rK   rB   rL   r2   chrom_gbrQ   chrom_dfchrom_indiceschrom_levelschrom_labelschrom_colorss                 r   r	   r	      s   J zz'Dz11H~~4~((0040HHH# ! !x .7799ftRY
;<< 	H:f--m<LL^LLFVFFGGGftRY
;<< 	H:f--m<LL^LLFVFFGGGftRY
;<< 	H:f--m<LL^z&#66^!LLFVFFGGGNL#		
 		
 		
 		
 		%C! !r   )NNr   N)NNNNFr/   )r;   typingr   
matplotlibr&   matplotlib.pyplotpyplotr#   numpyr6   pandasr   matplotlib.colorsr   corer   r!   r"   __all__r   tupler
   r.   rO   r	    r   r   <module>rp      sF                               $ $ $ $ $ $        2
3)BuS%Z0 )BS )B )B )B )BZ GK   * 	T T T Tr 	H H H H H Hr   