U
    Åmœd+  ã                
   @   s¶   d dl mZmZ d dl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 eed�deeee eeeef  ee ee edœdd„ƒZdS )é    )ÚOptionalÚUnionN)ÚAnnData)Úpyplot)ÚAxesé   )Ú_utils)Údoc_show_save_axé   )Únormalize_total)Ú_doc_params)Zshow_save_axé   F)ÚadataÚn_topÚshowÚsaveÚaxÚgene_symbolsÚlogc                 K   sb  ddl }ddlm}	 t| ddd�}
|	|
d ƒrt|
d jdd�j}t |¡ddd	… d|… }|
d dd…|f j}n@|
d jdd�}t |¡ddd	… d|… }|
d dd…|f }|dkrÆ| j	| n| j
| | }tj|| j|d
�}|�s|d d }tjd|fd�\}}|jf |d|ddœ|—Ž | d¡ |�r@| d¡ tjd||d� |dk�r^|S dS )u9      Fraction of counts assigned to each gene over all cells.

    Computes, for each gene, the fraction of counts assigned to that gene within
    a cell. The `n_top` genes with the highest mean fraction over all cells are
    plotted as boxplots.

    This plot is similar to the `scater` package function `plotHighestExprs(type
    = "highest-expression")`, see `here
    <https://bioconductor.org/packages/devel/bioc/vignettes/scater/inst/doc/vignette-qc.html>`__. Quoting
    from there:

        *We expect to see the â€œusual suspectsâ€�, i.e., mitochondrial genes, actin,
        ribosomal protein, MALAT1. A few spike-in transcripts may also be
        present here, though if all of the spike-ins are in the top 50, it
        suggests that too much spike-in RNA was added. A large number of
        pseudo-genes or predicted genes may indicate problems with alignment.*
        -- Davis McCarthy and Aaron Lun

    Parameters
    ----------
    adata
        Annotated data matrix.
    n_top
        Number of top
    {show_save_ax}
    gene_symbols
        Key for field in .var that stores gene symbols if you do not want to use .var_names.
    log
        Plot x-axis in log scale
    **kwds
        Are passed to :func:`~seaborn.boxplot`.

    Returns
    -------
    If `show==False` a :class:`~matplotlib.axes.Axes`.
    r   N)Úissparseéd   F)Z
target_sumZinplaceÚX)Zaxiséÿÿÿÿ)ÚindexÚcolumnsgš™™™™™É?g      ø?é   )ZfigsizeÚhr   )ÚdataZorientr   Z	fliersizez% of total countsr   Úhighest_expr_genes)r   r   )ZseabornZscipy.sparser   r   ZmeanÚA1ÚnpZargsortÚAZ	var_namesÚvarÚpdZ	DataFrameZ	obs_namesÚpltZsubplotsZboxplotZ
set_xlabelZ
set_xscaler   Zsavefig_or_show)r   r   r   r   r   r   r   ÚkwdsZsnsr   Z	norm_dictZmean_percentZtop_idxZcounts_top_genesr   ÚheightZfig© r'   úL/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/scanpy/plotting/_qc.pyr      s:    0ÿý  ÿ


r   )r   NNNNF)Útypingr   r   Únumpyr    Zpandasr#   Zanndatar   Z
matplotlibr   r$   Zmatplotlib.axesr   Ú r   Z_docsr	   Zpreprocessing._normalizationr   r   ÚintÚboolÚstrr   r'   r'   r'   r(   Ú<module>   s2         ùù