U
    ÅmœdÜ  ã                	   @   s€   d dl mZ d dlmZmZmZ d dlZd dl	Z
d dlmZ ddlmZ deeeeee eeef d
œdd„Zedœdd„ZdS )é    N)ÚIterableÚ
CollectionÚUnion)ÚAnnDataé   )ÚloggingTé   ©é   é   r   éú   )ÚadataÚ
start_cellÚbranchÚkÚ
componentsÚnum_waypointsc                 C   sF  zddl m} W n tk
r,   tdƒ‚Y nX t | j|k¡d }t|ƒdkr^td|› d�ƒ‚t|t	j
ƒr˜t || jj¡}|jdkr®t d¡ | ¡ }n|| jd kr®tdƒ‚|d }t|ƒ}|| jd d	d	…|f |||||d
�}	|	d }
|
t |
¡ t |
¡t |
¡  }
t |
¡| jd< |�rB|	d  t¡}t |¡| jd< d	S )a•	      Wishbone identifies bifurcating developmental trajectories from single-cell data
    [Setty16]_.

    Wishbone is an algorithm for positioning single cells along bifurcating
    developmental trajectories with high resolution. Wishbone uses multi-dimensional
    single-cell data, such as mass cytometry or RNA-Seq data, as input and orders cells
    according to their developmental progression, and it pinpoints bifurcation points
    by labeling each cell as pre-bifurcation or as one of two post-bifurcation cell
    fates.

    .. note::
       More information and bug reports `here
       <https://github.com/dpeerlab/wishbone>`__.

    Parameters
    ----------
    adata
        Annotated data matrix.
    start_cell
        Desired start cell from `obs_names`.
    branch
        Use True for Wishbone and False for Wanderlust.
    k
        Number of nearest neighbors for graph construction.
    components
        Components to use for running Wishbone.
    num_waypoints
        Number of waypoints to sample.

    Returns
    -------
    Updates `adata` with the following fields:

    `trajectory_wishbone` : (`adata.obs`, dtype `float64`)
        Computed trajectory positions.
    `branch_wishbone` : (`adata.obs`, dtype `int64`)
        Assigned branches.

    Example
    -------

    >>> import scanpy.external as sce
    >>> import scanpy as sc

    **Loading Data and Pre-processing**

    >>> adata = sc.datasets.pbmc3k()
    >>> sc.pp.normalize_per_cell(adata)
    >>> sc.pp.pca(adata)
    >>> sc.tl.tsne(adata=adata, n_pcs=5, perplexity=30)
    >>> sc.pp.neighbors(adata, n_pcs=15, n_neighbors=10)
    >>> sc.tl.diffmap(adata, n_comps=10)

    **Running Wishbone Core Function**

    Usually, the start cell for a dataset should be chosen based on high expression of
    the gene of interest:

    >>> sce.tl.wishbone(
    ...     adata=adata, start_cell='ACAAGAGACTTATC-1',
    ...     components=[2, 3], num_waypoints=150,
    ... )

    **Visualizing Wishbone results**

    >>> sc.pl.tsne(adata, color=['trajectory_wishbone', 'branch_wishbone'])
    >>> markers = ['C1QA', 'PSAP', 'CD79A', 'CD79B', 'CST3', 'LYZ', 'MALAT1']
    >>> sce.pl.wishbone_marker_trajectory(adata, markers, show=True)

    For further demonstration of Wishbone methods and visualization please follow the
    notebooks in the package `Wishbone_for_single_cell_RNAseq.ipynb
    <https://github.com/dpeerlab/wishbone/tree/master/notebooks>`_.    r   )Úwishbonez@
please install wishbone:

	https://github.com/dpeerlab/wishbonezStart cell z9 not found in data. Please rerun with correct start cell.zJSome of the specified waypoints are not in the data. These will be removedzinum_waypoints parameter is higher than the number of cells in the dataset. Please select a smaller numberÚ	X_diffmapN)Úsr   Úlr   r   Z
TrajectoryÚtrajectory_wishboneZBranchesÚbranch_wishbone)Zwishbone.corer   ÚImportErrorÚnpÚwhereÚ	obs_namesÚlenÚRuntimeErrorÚ
isinstanceÚcabcr   Z	setdiff1dÚobsÚindexÚsizer   ÚwarningÚtolistÚshapeÚlistÚobsmÚminÚmaxZasarrayZastypeÚint)r   r   r   r   r   r   Z
c_wishboner   ÚdiffÚresÚ
trajectoryZbranches© r/   úU/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/scanpy/external/tl/_wishbone.pyr      sP    Rÿ

ÿ
ÿ
ÿú
ÿr   )r   c                 C   sX   ddl m}m} ||  ¡ ƒ}tj| jd | jd�|_||ƒ}| j	d |_
| j	d |_|S )Nr   )ÚSCDataÚWishboner   )r"   r   r   )Zwishbone.wbr1   r2   Zto_dfÚpdZ	DataFramer(   r   Zdiffusion_eigenvectorsr!   r.   r   )r   r1   r2   ZscdataÚwbr/   r/   r0   Ú_anndata_to_wishbone’   s     ÿr5   )Tr   r	   r   )Úcollections.abcÚabcr    Útypingr   r   r   Únumpyr   Zpandasr3   Zanndatar   Ú r   ÚstrÚboolr+   r   r5   r/   r/   r/   r0   Ú<module>   s&       ú
ú 