U
    mdj                     @   s   d Z ddlmZmZmZmZmZmZmZ ddl	Z
ddlmZ ddlmZ ddlmZ eeeeef  Zddddd	ee eeeef  eee ee eeeeeef  f d
ddZdddddeeeeeeeef  f  eeee
jdddZdd ZdS )zDCalculate scores based on relative expression change of maker pairs
    )MappingOptional
CollectionUnionTupleListDictN)AnnData)version   settingsg?)fractionfilter_genesfilter_samples)adata
annotationr   r   r   returnc                C   sX   t   ddlm} ddlm} tj|_tj|_tj|_tj|_tj	|_	|| ||||dS )a      Calculate marker pairs of genes. [Scialdone15]_ [Fechtner18]_.

    Calculates the pairs of genes serving as marker pairs for each phase,
    based on a matrix of gene counts and an annotation of known phases.

    This reproduces the approach of [Scialdone15]_ in the implementation of
    [Fechtner18]_.

    More information and bug reports `here
    <https://github.com/rfechtner/pypairs>`__.

    Parameters
    ----------
    adata
        The annotated data matrix.
    annotation
        Mapping from category to genes, e.g. `{'phase': [Gene1, ...]}`.
        Defaults to ``data.vars['category']``.
    fraction
        Fraction of cells per category where marker criteria must be satisfied.
    filter_genes
        Genes for sampling the reference set. Defaults to all genes.
    filter_samples
        Cells for sampling the reference set. Defaults to all samples.

    Returns
    -------
    A dict mapping from category to lists of marker pairs, e.g.:
    `{'Category_1': [(Gene_1, Gene_2), ...], ...}`.

    Examples
    --------
    >>> from scanpy.external.tl import sandbag
    >>> from pypairs import datasets
    >>> adata = datasets.leng15()
    >>> marker_pairs = sandbag(adata, fraction=0.5)
    r   )sandbagr   )datar   r   r   r   )
_check_importpypairs.pairsr   pypairsr   	verbosityn_jobswritedircachedirlogfile)r   r   r   r   r   r   pp_settings r   T/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/scanpy/external/tl/_pypairs.pyr      s    .r   i  d   2   )
iterationsmin_iter	min_pairs)r   marker_pairsr#   r$   r%   r   c                C   sX   t   ddlm} ddlm} tj|_tj|_tj|_tj|_tj	|_	|| ||||dS )aT      Assigns scores and predicted class to observations [Scialdone15]_ [Fechtner18]_.

    Calculates scores for each observation and each phase and assigns prediction
    based on marker pairs indentified by :func:`~scanpy.external.tl.sandbag`.

    This reproduces the approach of [Scialdone15]_ in the implementation of
    [Fechtner18]_.

    Parameters
    ----------
    adata
        The annotated data matrix.
    marker_pairs
        Mapping of categories to lists of marker pairs.
        See :func:`~scanpy.external.tl.sandbag` output.
    iterations
        An integer scalar specifying the number of
        iterations for random sampling to obtain a cycle score.
    min_iter
        An integer scalar specifying the minimum number of iterations
        for score estimation.
    min_pairs
        An integer scalar specifying the minimum number of pairs
        for score estimation.

    Returns
    -------
    A :class:`~pandas.DataFrame` with samples as index and categories as columns
    with scores for each category for each sample and a additional column with
    the name of the max scoring category for each sample.

    If `marker_pairs` contains only the cell cycle categories G1, S and G2M an
    additional column `pypairs_cc_prediction` will be added.
    Where category S is assigned to samples where G1 and G2M score are < 0.5.
    r   )cycloner   )r   r&   r#   r$   r%   )
r   r   r'   r   r   r   r   r   r   r   )r   r&   r#   r$   r%   r'   r   r   r   r    r'   Q   s    ,r'   c                  C   sV   zdd l } W n tk
r(   tdY nX td}t| j|k rRtd| d S )Nr   z*You need to install the package `pypairs`.z3.0.9zPlease only use `pypairs` >= )r   ImportErrorr
   parse__version__)r   min_versionr   r   r    r      s    
r   )N)N)__doc__typingr   r   r   r   r   r   r   ZpandaspdZanndatar	   	packagingr
    r   strintboolZGenesfloatr   Z	DataFramer'   r   r   r   r   r    <module>   sB   $ C ?