Skip to content

Gene selection plot for Pearson Residuals - #2176

Open
jlause wants to merge 5 commits into
scverse:mainfrom
jlause:pearson_residuals_HVG_plot
Open

jlause wants to merge 5 commits into
scverse:mainfrom
jlause:pearson_residuals_HVG_plot

Conversation

@jlause

@jlause jlause commented Mar 15, 2022 •

Copy link
Copy Markdown
Contributor

Hi @giovp @ivirshup, (also CCing @dkobak)

as we discussed at #1715 and in scverse/scanpy-tutorials#43, I prepared a function pearson_residuals_hvg_scatter() that wraps sc.pl.scatter() to reproduce the "gene selection plot" from the tutorial. It can be used as a sanity check for both the HVG selection and the appropriateness of the used Pearson residual null model (as explained also in the tutorial notebook).

I also added a feature to show known marker genes on the plot, change plot aesthetics and which fields are used for plotting.

Looking forward to your thoughts on this one :)
Best,
Jan

PS: I prepared this PR on an independent branch from #1715 - hope that is the correct way in this situation!

import scanpy as sc
sc.settings.set_figure_params(dpi=80, facecolor="white")

#run pearson residuals gene selection
adata=sc.datasets.pbmc3k()
sc.pp.filter_genes(adata, min_cells=1)
sc.experimental.pp.highly_variable_genes(adata, flavor="pearson_residuals", n_top_genes=2000)

#basic plot
sc.experimental.pl.pearson_residuals_hvg_scatter(adata)

image

#modify some aesthetics
sc.experimental.pl.pearson_residuals_hvg_scatter(adata,kwargs_sc_pl_scatter=dict(size=30))

image

#highlight some marker genes
markers = ["IL7R", "LYZ", "CD14", "MS4A1", "CD8A",  "GNLY"]
sc.experimental.pl.pearson_residuals_hvg_scatter(adata,marker_names=markers,kwargs_sc_pl_scatter=dict(size=30))

image

#use custom fields in `adata` for x and y
#(there is also a similar option to use a different field for where HVG flag is stored)
sc.experimental.pl.pearson_residuals_hvg_scatter(adata,x='means',y='variances',return_ax=True)

image

#modify axis object after `sc.pl.scatter()` ran
ax = sc.experimental.pl.pearson_residuals_hvg_scatter(adata,return_ax=True)
ax.set_title('my title')
ax.set_xlabel('my x label')
ax.set_ylabel('my y label')

image

@jlause jlause changed the title Pearson residuals hvg plot Gene selection plot for Pearson Residuals Mar 15, 2022
@codecov

codecov Bot commented Mar 15, 2022 •

Copy link
Copy Markdown

Codecov Report

Merging #2176 (e643c54) into master (d805b41) will decrease coverage by 0.22%.
The diff coverage is 0.00%.

@@            Coverage Diff             @@
##           master    #2176      +/-   ##
==========================================
- Coverage   71.42%   71.19%   -0.23%     
==========================================
  Files          92       95       +3     
  Lines       11286    11322      +36     
==========================================
  Hits         8061     8061              
- Misses       3225     3261      +36     
Impacted Files Coverage Δ
scanpy/experimental/__init__.py 0.00% <0.00%> (ø)
scanpy/experimental/pl/__init__.py 0.00% <0.00%> (ø)
scanpy/experimental/pl/_preprocessing.py 0.00% <0.00%> (ø)

@codecov

codecov Bot commented Oct 25, 2022 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 19.44444% with 29 lines in your changes missing coverage. Please review.
✅ Project coverage is 71.64%. Comparing base (d7e1302) to head (86eca00).
⚠️ Report is 740 commits behind head on main.

Files with missing lines Patch % Lines
scanpy/experimental/pl/_preprocessing.py 14.70% 29 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2176      +/-   ##
==========================================
- Coverage   71.80%   71.64%   -0.16%     
==========================================
  Files          97      100       +3     
  Lines       11522    11578      +56     
==========================================
+ Hits         8273     8295      +22     
- Misses       3249     3283      +34     
Files with missing lines Coverage Δ
scanpy/experimental/__init__.py 100.00% <100.00%> (ø)
scanpy/experimental/pl/__init__.py 100.00% <100.00%> (ø)
scanpy/experimental/pl/_preprocessing.py 14.70% <14.70%> (ø)

... and 4 files with indirect coverage changes

@Zethson
Zethson requested a review from giovp January 9, 2023 18:56

@giovp giovp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apologies for late reply @jlause . Few minor comments, another thing that is missing are tests but I can help with that.

import numpy as np


def pearson_residuals_hvg_scatter(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove the scatter suffix.

def pearson_residuals_hvg_scatter(
adata: AnnData,
marker_names=None,
gene_name_key: Optional[str] = None,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please replace to var_names . In general, check the signature of already existing functions in order to harmonize arguments name.

x: str = 'means',
y: str = 'residual_variances',
hvg_key: str = 'highly_variable',
kwargs_sc_pl_scatter: dict = dict(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this just be called kwargs and in the docstrings specify that this is passed to sc.pl.scatter. ?

Comment on lines +64 to +67
def clean_helper_fields(ad):
del ad.var['hvg_marker_status']
if 'hvg_marker_status_colors' in ad.uns.keys():
del ad.uns['hvg_marker_status_colors']

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you elaborate what this is for?

@giovp
giovp self-requested a review January 10, 2023 07:46

@giovp giovp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

few changes

@Zethson

Zethson commented Feb 15, 2023

Copy link
Copy Markdown
Member

@jlause do you plan on tackling these to get this in?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants