示例#1
0
def _create_gpcca(*, backward: bool = False) -> Tuple[AnnData, GPCCA]:
    adata = _adata_medium.copy()

    sc.tl.paga(adata, groups="clusters")

    vk = VelocityKernel(
        adata, backward=backward).compute_transition_matrix(softmax_scale=4)
    ck = ConnectivityKernel(adata,
                            backward=backward).compute_transition_matrix()
    final_kernel = 0.8 * vk + 0.2 * ck

    mc = GPCCA(final_kernel)

    mc.compute_partition()
    mc.compute_eigendecomposition()
    mc.compute_schur(method="krylov")
    mc.compute_macrostates(n_states=2)
    mc.set_terminal_states_from_macrostates()
    mc.compute_absorption_probabilities()
    mc.compute_lineage_drivers(cluster_key="clusters", use_raw=False)

    assert adata is mc.adata
    if backward:
        assert str(AbsProbKey.BACKWARD) in adata.obsm
    else:
        assert str(AbsProbKey.FORWARD) in adata.obsm
    np.testing.assert_allclose(mc.absorption_probabilities.X.sum(1),
                               1.0,
                               rtol=1e-6)

    return adata, mc
示例#2
0
def lineage_drivers(
    adata: AnnData,
    backward: bool = False,
    lineages: Optional[Union[Sequence, str]] = None,
    method: str = TestMethod.FISCHER.s,
    cluster_key: Optional[str] = None,
    clusters: Optional[Union[Sequence, str]] = None,
    layer: str = "X",
    use_raw: bool = False,
    confidence_level: float = 0.95,
    n_perms: int = 1000,
    seed: Optional[int] = None,
    return_drivers: bool = True,
    **kwargs,
) -> Optional[pd.DataFrame]:  # noqa
    """
    %(lineage_drivers.full_desc)s

    Parameters
    ----------
    %(adata)s
    %(backward)s
    %(lineage_drivers.parameters)s

    Returns
    -------
    %(lineage_drivers.returns)s

    References
    ----------
    %(lineage_drivers.references)s
    """

    # create dummy kernel and estimator
    pk = DummyKernel(adata, backward=backward)
    g = GPCCA(pk, read_from_adata=True, write_to_adata=False)
    if g._get(P.ABS_PROBS) is None:
        raise RuntimeError(
            f"Compute absorption probabilities first as `cellrank.tl.lineages(..., backward={backward})`."
        )

    # call the underlying function to compute and store the lineage drivers
    return g.compute_lineage_drivers(
        method=method,
        lineages=lineages,
        cluster_key=cluster_key,
        clusters=clusters,
        layer=layer,
        use_raw=use_raw,
        confidence_level=confidence_level,
        n_perms=n_perms,
        seed=seed,
        return_drivers=return_drivers,
        **kwargs,
    )
示例#3
0
                     save='{}_schur.png'.format(key))
        g.compute_metastable_states(n_states=1, cluster_key=clusters)
        g.plot_metastable_states(show=False,
                                 dpi=300,
                                 save='{}_metastable.png'.format(key))

    g.plot_metastable_states(discrete=True,
                             show=False,
                             dpi=300,
                             legend_fontsize=0,
                             save='{}_discrete_metastable.pdf'.format(key),
                             size=28)
    g.set_final_states_from_metastable_states()
    if key == "H508_EV" or key == "HT29_EV":
        g.compute_absorption_probabilities()
        g.compute_lineage_drivers()
        samples[key].var.to_csv(key + "_lineages.tsv", sep='\t')
        print(samples[key].var.columns)
        if key == "HT29_EV":
            g.plot_lineage_drivers(
                '17', save='{}_17_lineage_drivers.png'.format(key))
            g.plot_lineage_drivers(
                '1', save='{}_10_lineage_drivers.png'.format(key))
            g.plot_lineage_drivers('11',
                                   save='{}_1_lineage_drivers.png'.format(key))
            g.plot_lineage_drivers('4',
                                   save='{}_4_lineage_drivers.png'.format(key))

        if key == "H508_EV":
            g.plot_lineage_drivers(
                '10', save='{}_10_lineage_drivers.png'.format(key))