Scanpy: filter cells by a specific gene value

Created on 9 Apr 2019  路  3Comments  路  Source: theislab/scanpy

Hi guys,

I would like to filter cells by an arbitrary threshold set on the expression of a specific gene (elav) at the very begging of the pipeline. I am new to scanpy and relatively new to python.

this is what I do at the begining but I am having trouble getting it to work (see third command) with setting the corresponding threshold:

filtering by n_genes and percent mito

adata = adata[adata.obs['n_genes'] < 3500, :]
adata = adata[adata.obs['percent_mito'] < 0.5, :]

filtering by elav

adata = adata[adata.var['elav'] > 0.5, :]

This last part doesn't seem to work. All help appreciated!

Most helpful comment

Try

adata = adata[adata[: , 'elav'].X > 0.5, :] 

var only contain the metadata for the genes. The expression is housed in X, just as the metadata for the observations (cells) are stored in obs.

And you should be able to index adata with gene names directly as adata[:, <list of names>]. if they exist in the var_names list, adata.var_names.

All 3 comments

Try

adata = adata[adata[: , 'elav'].X > 0.5, :] 

var only contain the metadata for the genes. The expression is housed in X, just as the metadata for the observations (cells) are stored in obs.

And you should be able to index adata with gene names directly as adata[:, <list of names>]. if they exist in the var_names list, adata.var_names.

For me,
adata[(adata[:,'elav'].X>0).flatten(), : ] works

otherwise, it gives error.

This issue has been mentioned on Scanpy. There might be relevant details there:

https://scanpy.discourse.group/t/filtering-cells-by-expression-of-specific-gene/256/2

Was this page helpful?
0 / 5 - 0 ratings