Import highly variable genes elsewhere

Hi scanpy team,
How should I import highly variable genes elsewhere into anndata?

No idea how to deal with adata.var…

Many thanks in advance!

Hi, I got a solution by my friend who is good at python.
I post it here, but any much easier way is welcome.

#first read hvg.csv by pandas

z=hvg.values.flatten()# make it one row
ttt=adata.var.copy()## extract adata.var and label TRUE or FALSE by hvg
ttt.loc[:]=False
ttt.loc[z]=True
adata.var=ttt
pd.value_counts(adata.var['highly_variable'])
1 Like

Hey!

Sorry for completely overlooking this post.

Maybe a quicker solution:
adata.var['highly_variable'] = [True if g in my_hvg_list else False for g in adata.var_names]

Where my_hvg_list is your list of highly variable genes. Note that these genes must match the gene names in adata.var_names.

Hey
Thanks for reply.
I think your code will work.
Also, I figure out another way to import HVGs, especially for loom file converted from Seurat.
adata.var['highly_variable']=adata.var['Selected'].astype('bool')

1 Like

this looks good too! In general it’s quite nice to move been R and python in the same jupyter notebook using anndata2ri. You could check that out at www.github.com/theislab/anndata2ri