Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gene selection plot for Pearson Residuals #2176

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

jlause
Copy link
Contributor

@jlause jlause commented Mar 15, 2022

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
Copy link

codecov bot commented Mar 15, 2022

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
Copy link

codecov bot commented Oct 25, 2022

Codecov Report

Merging #2176 (86eca00) into master (d7e1302) will decrease coverage by 0.15%.
The diff coverage is 19.44%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2176      +/-   ##
==========================================
- Coverage   71.80%   71.64%   -0.16%     
==========================================
  Files          97      100       +3     
  Lines       11522    11578      +56     
==========================================
+ Hits         8273     8295      +22     
- Misses       3249     3283      +34     
Impacted Files Coverage Δ
scanpy/experimental/pl/_preprocessing.py 14.70% <14.70%> (ø)
scanpy/experimental/__init__.py 100.00% <100.00%> (ø)
scanpy/experimental/pl/__init__.py 100.00% <100.00%> (ø)
scanpy/external/pp/_scrublet.py 84.69% <0.00%> (-6.13%) ⬇️
scanpy/preprocessing/_simple.py 77.91% <0.00%> (-0.25%) ⬇️
scanpy/__init__.py 100.00% <0.00%> (ø)
scanpy/preprocessing/_normalization.py 86.04% <0.00%> (+2.32%) ⬆️

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

@giovp giovp left a comment

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
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
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
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
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
Copy link
Member

@giovp giovp left a comment

Choose a reason for hiding this comment

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

few changes

@Zethson
Copy link
Member

Zethson commented Feb 15, 2023

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

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