U
    Åmœd^
  ã                   @   s6   d Z ddlmZ ddlmZ d
eeeedœdd„Zd	S )z<
Use harmony to integrate cells from different experiments.
é    )ÚOptional)ÚAnnDataÚX_pcaÚX_pca_harmony)ÚadataÚkeyÚbasisÚadjusted_basisc                 K   sV   zddl }W n tk
r(   tdƒ‚Y nX |j| j| | j|f|Ž}|jj| j|< dS )a(      Use harmonypy [Korunsky19]_ to integrate different experiments.

    Harmony [Korunsky19]_ is an algorithm for integrating single-cell
    data from multiple experiments. This function uses the python
    port of Harmony, ``harmonypy``, to integrate single-cell data
    stored in an AnnData object. As Harmony works by adjusting the
    principal components, this function should be run after performing
    PCA but before computing the neighbor graph, as illustrated in the
    example below.

    Parameters
    ----------
    adata
        The annotated data matrix.
    key
        The name of the column in ``adata.obs`` that differentiates
        among experiments/batches.
    basis
        The name of the field in ``adata.obsm`` where the PCA table is
        stored. Defaults to ``'X_pca'``, which is the default for
        ``sc.tl.pca()``.
    adjusted_basis
        The name of the field in ``adata.obsm`` where the adjusted PCA
        table will be stored after running this function. Defaults to
        ``X_pca_harmony``.
    kwargs
        Any additional arguments will be passed to
        ``harmonypy.run_harmony()``.

    Returns
    -------
    Updates adata with the field ``adata.obsm[obsm_out_field]``,
    containing principal components adjusted by Harmony such that
    different experiments are integrated.

    Example
    -------
    First, load libraries and example dataset, and preprocess.

    >>> import scanpy as sc
    >>> import scanpy.external as sce
    >>> adata = sc.datasets.pbmc3k()
    >>> sc.pp.recipe_zheng17(adata)
    >>> sc.tl.pca(adata)

    We now arbitrarily assign a batch metadata variable to each cell
    for the sake of example, but during real usage there would already
    be a column in ``adata.obs`` giving the experiment each cell came
    from.

    >>> adata.obs['batch'] = 1350*['a'] + 1350*['b']

    Finally, run harmony. Afterwards, there will be a new table in
    ``adata.obsm`` containing the adjusted PC's.

    >>> sce.pp.harmony_integrate(adata, 'batch')
    >>> 'X_pca_harmony' in adata.obsm
    True
    r   Nz2
please install harmonypy:

	pip install harmonypy)Ú	harmonypyÚImportErrorZrun_harmonyZobsmZobsZZ_corrÚT)r   r   r   r	   Úkwargsr
   Zharmony_out© r   ú^/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/scanpy/external/pp/_harmony_integrate.pyÚharmony_integrate
   s    Cr   N)r   r   )Ú__doc__Útypingr   Zanndatar   Ústrr   r   r   r   r   Ú<module>   s     üü