aristoteleo / dynamo-release

Inclusive model of expression dynamics with conventional or metabolic labeling based scRNA-seq / multiomics, vector field reconstruction and differential geometry analyses
https://dynamo-release.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
414 stars 58 forks source link

KeyError: 'experiment_type' in dyn.tl.cell_velocities #218

Closed Roger-GOAT closed 3 years ago

Roger-GOAT commented 3 years ago

Hi, thanks for the software! I follow the process like below: dyn.pp.recipe_monocle(adata) dyn.tl.moments(adata) dyn.tl.dynamics(adata,model='stochastic', cores=8) dyn.tl.reduceDimension(adata) dyn.pl.umap(adata, color='leiden') dyn.pl.phase_portraits(adata, genes=adata.var_names[adata.var.use_for_dynamics][:4], figsize=(6, 4), color='leiden') all is going well. But happen like below.

dyn.tl.gene_wise_confidence(adata,group='leiden')
calculating gene velocity vectors confidence based on phase portrait location with priors of progenitor/mature cell types: 0it [00:00, ?it/s]
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_1801062/3361583128.py in <module>
----> 1 dyn.tl.gene_wise_confidence(adata,group='leiden')

~/miniconda3/envs/dyn/lib/python3.9/site-packages/dynamo_release-1.0.0-py3.9.egg/dynamo/tools/metric_velocity.py in gene_wise_confidence(adata, group, lineage_dict, genes, ekey, vkey, X_data, V_data, V_threshold)
    353         all_vals_v = V_data[:, i_gene].A if sparse_v else V_data[:, i_gene]
    354 
--> 355         for progenitors_groups, mature_cells_groups in lineage_dict.items():
    356             progenitors_groups = [progenitors_groups]
    357             if type(mature_cells_groups) is str:

AttributeError: 'NoneType' object has no attribute 'items'
dyn.tl.cell_velocities(adata, basis='umap'(or pca))
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/tmp/ipykernel_1801062/1091926121.py in <module>
----> 1 dyn.tl.cell_velocities(adata, basis='umap')

~/miniconda3/envs/dyn/lib/python3.9/site-packages/dynamo_release-1.0.0-py3.9.egg/dynamo/tools/cell_velocities.py in cell_velocities(adata, ekey, vkey, X, V, X_embedding, transition_matrix, use_mnn, n_pca_components, transition_genes, min_r2, min_alpha, min_gamma, min_delta, basis, neighbor_key_prefix, adj_key, add_transition_key, add_velocity_key, n_neighbors, method, neg_cells_trick, calc_rnd_vel, xy_grid_nums, correct_density, scale, sample_fraction, random_seed, enforce, preserve_len, **kernel_kwargs)
    197     mapper_r = get_mapper_inverse()
    198     layer = mapper_r[ekey] if (ekey is not None and ekey in mapper_r.keys()) else ekey
--> 199     ekey, vkey, layer = get_ekey_vkey_from_adata(adata) if (ekey is None or vkey is None) else (ekey, vkey, layer)
    200 
    201     if calc_rnd_vel:

~/miniconda3/envs/dyn/lib/python3.9/site-packages/dynamo_release-1.0.0-py3.9.egg/dynamo/tools/utils.py in get_ekey_vkey_from_adata(adata)
   1709     dynamics_key = [i for i in adata.uns.keys() if i.endswith("dynamics")][0]
   1710     experiment_type, use_smoothed = (
-> 1711         adata.uns[dynamics_key]["experiment_type"],
   1712         adata.uns[dynamics_key]["use_smoothed"],
   1713     )

KeyError: 'experiment_type'

with information

adata
AnnData object with n_obs × n_vars = 35195 × 2000
    obs: 'batch', 'initial_size_spliced', 'initial_size_unspliced', 'initial_size', 'n_counts', 'leiden', 'velocity_self_transition', 'S_score', 'G2M_score', 'phase', 'clusters_gradients', 'velocity_length', 'velocity_confidence', 'velocity_confidence_transition', 'nGenes', 'nCounts', 'pMito', 'use_for_pca', 'unspliced_Size_Factor', 'initial_unspliced_cell_size', 'Size_Factor', 'initial_cell_size', 'spliced_Size_Factor', 'initial_spliced_cell_size', 'ntr', 'cell_cycle_phase', 'pass_basic_filter'
    var: 'Accession', 'Chromosome', 'End', 'Start', 'Strand', 'gene_count_corr', 'means', 'dispersions', 'dispersions_norm', 'highly_variable', 'fit_r2', 'fit_alpha', 'fit_beta', 'fit_gamma', 'fit_t_', 'fit_scaling', 'fit_std_u', 'fit_std_s', 'fit_likelihood', 'fit_u0', 'fit_s0', 'fit_pval_steady', 'fit_steady_u', 'fit_steady_s', 'fit_variance', 'fit_alignment_scaling', 'velocity_genes', 'velocity_gamma', 'velocity_qreg_ratio', 'velocity_r2', 'nCells', 'nCounts', 'pass_basic_filter', 'frac', 'ntr', 'use_for_pca', 'beta', 'gamma', 'half_life', 'alpha_b', 'alpha_r2', 'gamma_b', 'gamma_r2', 'gamma_logLL', 'delta_b', 'delta_r2', 'bs', 'bf', 'uu0', 'ul0', 'su0', 'sl0', 'U0', 'S0', 'total0', 'use_for_dynamics'
    uns: 'PCs', 'cell_phase_genes', 'clusters_gradients_colors', 'draw_graph', 'explained_variance_ratio_', 'feature_selection', 'leiden', 'leiden_colors', 'neighbors', 'pca', 'pca_mean', 'pp', 'rank_genes_groups', 'recover_dynamics', 'umap', 'velocity_graph', 'velocity_graph_neg', 'velocity_params', 'pca_fit', 'dynamics'
    obsm: 'X', 'X_draw_graph_fa', 'X_pca', 'X_umap', 'cell_cycle_scores', 'velocity_umap'
    varm: 'PCs', 'loss'
    layers: 'Ms', 'Mu', 'X_spliced', 'X_unspliced', 'matrix', 'spliced', 'unspliced', 'M_u', 'M_uu', 'M_s', 'M_us', 'M_ss', 'velocity_S'
    obsp: 'connectivities', 'distances', 'moments_con'
dyn.get_all_dependencies_version()
<html>
<body>
<!--StartFragment-->

package | statsmodels | numba | seaborn | colorcet | tqdm | python-igraph | pynndescent | pre-commit | networkx | dynamo-release | cvxopt | hdbscan | loompy | matplotlib | numdifftools | numpy | pandas | scikit-learn | scipy | setuptools | trimap | umap-learn
-- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | --
0.12.2 | 0.54.0rc2 | 0.11.1 | 2.0.6 | 4.61.2 | 0.9.6 | 0.5.4 | 2.13.0 | 2.6.1 | 1.0.0 | 1.2.6 | 0.8.27 | 3.0.6 | 3.4.2 | 0.9.40 | 1.20.3 | 1.3.0 | 0.24.2 | 1.7.0 | 49.6.0.post20210108 | 1.0.15 | 0.5.1

<!--EndFragment-->
</body>
</html>
Xiaojieqiu commented 3 years ago

hey @Roger-GOAT the gene-wise confidence is designed to help you correct your RNA velocity directionality by providing some broad lineage relationship. Right now, the lineage_dict is required to ensure it will work. If you believe your lineage starts from leiden cluster 1 to 5, you can set lineage_dict as lineage_dict = {"1": [5]}. You can read the its documentation for more details via ?dyn.tl.gene_wise_confidence.

In general, you should only run this function when you find your RNA velocity directionality is problematic.

Roger-GOAT commented 3 years ago

@Xiaojieqiu thank you. It solved!

JiaruiMi commented 2 years ago

Hi, I ran into exactly the same issue when I run the code: dyn.tl.cell_velocities(adata) and here is the error information:

KeyError Traceback (most recent call last)

in 1 ### Velocity projection ----> 2 dyn.tl.cell_velocities(adata, layers=['X_unspliced', 'X_spliced']) ~/opt/anaconda3/lib/python3.8/site-packages/dynamo/tools/cell_velocities.py in cell_velocities(adata, ekey, vkey, X, V, X_embedding, transition_matrix, use_mnn, n_pca_components, transition_genes, min_r2, min_alpha, min_gamma, min_delta, basis, neighbor_key_prefix, adj_key, add_transition_key, add_velocity_key, n_neighbors, method, neg_cells_trick, calc_rnd_vel, xy_grid_nums, correct_density, scale, sample_fraction, random_seed, enforce, preserve_len, **kernel_kwargs) 191 mapper_r = get_mapper_inverse() 192 layer = mapper_r[ekey] if (ekey is not None and ekey in mapper_r.keys()) else ekey --> 193 ekey, vkey, layer = get_ekey_vkey_from_adata(adata) if (ekey is None or vkey is None) else (ekey, vkey, layer) 194 195 if calc_rnd_vel: ~/opt/anaconda3/lib/python3.8/site-packages/dynamo/tools/utils.py in get_ekey_vkey_from_adata(adata) 1809 dynamics_key = [i for i in adata.uns.keys() if i.endswith("dynamics")][0] 1810 experiment_type, use_smoothed = ( -> 1811 adata.uns[dynamics_key]["experiment_type"], 1812 adata.uns[dynamics_key]["use_smoothed"], 1813 ) KeyError: 'experiment_type' I have try to define the layer as the error message is about the experiment_type but always came up with the same error message. Thanks! J
JiaruiMi commented 2 years ago

Hi Roger,

Just wonder how you solve the second error message, thanks!

J

Xiaojieqiu commented 2 years ago

Thanks @JiaruiMi I am curious why do you have layers = ['X_unspliced', 'X_spliced'] in your call? Apparently we don't have this argument in tl.cell_velocities, see our API here: https://dynamo-release.readthedocs.io/en/latest/_autosummary/dynamo.tl.cell_velocities.html#dynamo.tl.cell_velocities

The experiment_type should have set up during the pp.receipe_monocle step.

can you type adata and paste the output here? I want to see how the data look like.

lastly, if you type adata.uns["dynamics"], what is the output?