def principal_curve(data, basis='<STR_LIT>', n_comps=<NUM_LIT:4>, clusters_list=None, copy=False):
adata = data.copy() if copy else data<EOL>import rpy2.robjects as robjects<EOL>from rpy2.robjects.packages import importr<EOL>if clusters_list is not None:<EOL><INDENT>cell_subset = np.array([label in clusters_list for label in adata.obs['<STR_LIT>']])<EOL>X_emb = adata[cell_subset].obsm['<STR_LIT>' + basis][:, :n_comps]<EOL><DEDENT>else:<EOL><INDENT>cell_subset = None<EOL>X_emb = adata.obsm['<STR_LIT>' + basis][:, :n_comps]<EOL><DEDENT>n_obs, n_dim = X_emb.shape<EOL>xvec = robjects.FloatVector(X_emb.T.reshape((X_emb.size)))<EOL>X_R = robjects.r.matrix(xvec, nrow=n_obs, ncol=n_dim)<EOL>fit = importr("<STR_LIT>").principal_curve(X_R)<EOL>adata.uns['<STR_LIT>'] = dict()<EOL>adata.uns['<STR_LIT>']['<STR_LIT>'] = ixsort = np.array(fit[<NUM_LIT:1>])-<NUM_LIT:1><EOL>adata.uns['<STR_LIT>']['<STR_LIT>'] = np.array(fit[<NUM_LIT:0>])[ixsort]<EOL>adata.uns['<STR_LIT>']['<STR_LIT>'] = np.array(fit[<NUM_LIT:2>])<EOL>adata.uns['<STR_LIT>']['<STR_LIT>'] = cell_subset<EOL>return adata if copy else None<EOL>
Computes the principal curve

    Arguments
    ---------
    data: :class:`~anndata.AnnData`
        Annotated data matrix.
    basis: `str` (default: `'pca'`)
        Basis to use for computing the principal curve.
    n_comps: `int` (default: 4)
        Number of pricipal components to be used.
    copy: `bool`, (default: `False`)
        Return a copy instead of writing to adata.

    Returns
    -------
    Returns or updates `adata` with the attributes
    principal_curve: `.uns`
        dictionary containing `projections`, `ixsort` and `arclength`