U
    md                  
   @   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{Gz?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
 |rt|  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|  |rt|  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        "    