U
    Åmœdj  ã                   @   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 þúù?