U
    Åmœd  ã                
   @   s¼   d Z ddlmZ ddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZ dd	lmZ dd
lmZ deeeeeeeee dœdd„Zdeeeeee dœdd„Zdeeeeeee dœdd„ZdS )z)Preprocessing recipes from the literatureé    )ÚOptional)ÚAnnDataé   )Úpreprocessingé   )Úfilter_genes_dispersionÚfilter_genes_cv_deprecated)Únormalize_total)Úlogging)Ú	AnyRandomTç{®Gáz„?é2   Ú
randomizedF)ÚadataÚlogÚmean_thresholdÚcv_thresholdÚn_pcsÚrandom_stateÚcopyÚreturnc                 C   s    ddl m}m}	 ddlm}
 |
| jƒr.tdƒ‚|r:|  ¡ } |rHt 	| ¡ || jddd�| _t
| j||ƒ}|  |¡ tj|	| jƒ|||d	�}|| jd
< |rœ| S dS )a#      Normalization and filtering as of [Weinreb17]_.

    Expects non-logarithmized data.
    If using logarithmized data, pass `log=False`.

    Parameters
    ----------
    adata
        Annotated data matrix.
    log
        Logarithmize data?
    copy
        Return a copy if true.
    r   )Ú'normalize_per_cell_weinreb16_deprecatedÚzscore_deprecatedr   )Úissparsez3`recipe_weinreb16 does not support sparse matrices.gš™™™™™©?T)Zmax_fractionZmult_with_mean)Zn_compsÚ
svd_solverr   ÚX_pcaN)Z_deprecatedr   r   Zscipy.sparser   ÚXÚ
ValueErrorr   ÚppÚlog1pr   Ú_inplace_subset_varZpcaZobsm)r   r   r   r   r   r   r   r   r   r   r   Úgene_subsetr   © r"   úV/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/scanpy/preprocessing/_recipes.pyÚrecipe_weinreb17   s.    

  ÿ
ü
r$   )r   r   Úplotr   r   c                 C   sž   |r|   ¡ } tj| dd� tj| dd� t| dd� t| jddd| d	�}|rjd
dlm} |j|| d� |  	|j
¡ |r„t | ¡ tj| dd� |rš| S dS )zÁ    Normalization and filtering as of Seurat [Satija15]_.

    This uses a particular preprocessing.

    Expects non-logarithmized data.
    If using logarithmized data, pass `log=False`.
    éÈ   )Z	min_genesé   )Z	min_cellsg     ˆÃ@)Z
target_sumgš™™™™™‰?g      à?)Zmin_meanZmax_meanZmin_dispr   r   ©Ú_preprocessing©r   é
   )Z	max_valueN)r   r   Zfilter_cellsÚfilter_genesr	   r   r   Úplottingr)   r    r!   r   Úscale)r   r   r%   r   Úfilter_resultÚpppr"   r"   r#   Úrecipe_seuratC   s(        ÿ
r1   éè  )r   Ún_top_genesr   r%   r   r   c                 C   s¦   t  d¡}|r|  ¡ } tj| dd� t| dd� t| jd|dd�}|r`d	d
lm	} |j|dd� |  
|j¡ t| ƒ |r‚t | ¡ t | ¡ t jd|d� |r¢| S dS )uº      Normalization and filtering as of [Zheng17]_.

    Reproduces the preprocessing of [Zheng17]_ â€“ the Cell Ranger R Kit of 10x
    Genomics.

    Expects non-logarithmized data.
    If using logarithmized data, pass `log=False`.

    The recipe runs the following steps

    .. code:: python

        sc.pp.filter_genes(adata, min_counts=1)         # only consider genes with more than 1 count
        sc.pp.normalize_per_cell(                       # normalize with total UMI count per cell
             adata, key_n_counts='n_counts_all'
        )
        filter_result = sc.pp.filter_genes_dispersion(  # select highly-variable genes
            adata.X, flavor='cell_ranger', n_top_genes=n_top_genes, log=False
        )
        adata = adata[:, filter_result.gene_subset]     # subset the genes
        sc.pp.normalize_per_cell(adata)                 # renormalize after filtering
        if log: sc.pp.log1p(adata)                      # log transform: adata.X = log(adata.X + 1)
        sc.pp.scale(adata)                              # scale to unit variance and shift to zero mean


    Parameters
    ----------
    adata
        Annotated data matrix.
    n_top_genes
        Number of genes to keep.
    log
        Take logarithm.
    plot
        Show a plot of the gene dispersion vs. mean relation.
    copy
        Return a copy of `adata` instead of updating it.

    Returns
    -------
    Returns or updates `adata` depending on `copy`.
    zrunning recipe zheng17r   )Z
min_countsZn_counts_all)Z	key_addedZcell_rangerF)Zflavorr3   r   r   r(   Tr*   z    finished)ÚtimeN)ÚloggÚinfor   r   r,   r	   r   r   r-   r)   r    r!   r   r.   )r   r3   r   r%   r   Ústartr/   r0   r"   r"   r#   Úrecipe_zheng17c   s*    2
   ÿ

r8   N)Tr   r   r   r   r   F)TFF)r2   TFF)Ú__doc__Útypingr   Zanndatar   Ú r   r   Z!_deprecated.highly_variable_genesr   r   Z_normalizationr	   r
   r5   Ú_utilsr   ÚboolÚfloatÚintr$   r1   r8   r"   r"   r"   r#   Ú<module>   s\          ø÷3     ÿ   þ"    ûú