-
Notifications
You must be signed in to change notification settings - Fork 175
Add adapt_vars_like to align .var between AnnData objects (issue #1697) #1986
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
06bb519
24da345
74b3ebd
aa2295f
16264bf
46b728f
c09cf63
d1910d4
0dd7878
5d6279e
a14af7f
a30c365
fa48833
11fdb50
715fd0f
63a2933
8c30646
ac4c067
3a65f62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -450,3 +450,48 @@ | |
return getattr(mod, new_path) | ||
msg = f"module {full_old_module_path} has no attribute {attr_name!r}" | ||
raise AttributeError(msg) | ||
|
||
|
||
def adapt_vars_like( | ||
source: AnnData, target: AnnData, fill_value: float = 0.0 | ||
) -> AnnData: | ||
# source = AnnData object that defines the desired genes | ||
# target = the data you want to reshape to match source | ||
# fill_vlaue = what value to use for missing genes (default set to 0.0) | ||
# returns a new AnnData object with the same genes as source | ||
amalia-k510 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
""" | ||
Make target have the same .var (genes) as source., missing genes are filled with fill_value. | ||
""" | ||
|
||
# importing here to avoid circular import issues | ||
from ._core.anndata import AnnData | ||
|
||
# needed to add it as when trying to call target.X[:, target.var.index] | ||
# it would raise an error if target.X is None | ||
if target.X is None: | ||
msg = "target.X is None; cannot adapt vars without a data matrix." | ||
raise ValueError(msg) | ||
amalia-k510 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
# this will define the gene list we want to match | ||
new_var = source.var.copy() | ||
# initializing a new dense np array of shape (number of target cells, number of genes in source) | ||
# filled with fill_value | ||
# this will become the new .X matrix. | ||
# It makes sure all genes in source are represented, and placeholders are ready for copying shared ones | ||
new_x = np.full((target.n_obs, new_var.shape[0]), fill_value, dtype=target.X.dtype) | ||
|
||
# finds gene names that appeare in both source and target | ||
shared_genes = source.var_names.intersection(target.var_names) | ||
# positions of shared genes in source | ||
source_idx = new_var.index.get_indexer(shared_genes) | ||
# positions of those same genes in target | ||
target_idx = target.var.index.get_indexer(shared_genes) | ||
# fills the new .X array for all target cells (rows) | ||
# also inserts expression values from target.X into the correct columns of new_x | ||
# for the shared genes | ||
# only genes in both source and target are copied over. | ||
# everything else remains at fill_value | ||
new_x[:, source_idx] = target.X[:, target_idx] | ||
# creates a new AnnData object with the new .X and .var | ||
# .X is the filled new_x array | ||
# .obs is a copy of the target.obs | ||
# .var is copied from source.var, making sure alignment of gene annotations | ||
new_adata = AnnData(X=new_x, obs=target.obs.copy(), var=new_var) | ||
|
||
return new_adata | ||
Uh oh!
There was an error while loading. Please reload this page.