Dan
Dan

Reputation: 21

Exporting a list of genes to .csv after carrying out sc.tl.rank_genes_groups in scanpy

I am relatively new to Python and Scanpy and recently i have generated a list of differentially expressed genes by using the

sc.tl.rank_genes_groups

function in scanpy.. I can then get these genes to be listed in the console, by carrying out this command set

    result = adata_subset.uns['rank_genes_groups']
`   groups = result['names'].dtype.names
    pd.DataFrame(
    {group + '_' + key[:1]: result[key][group]
    for group in groups for key in ['names','logfoldchanges','pvals','pvals_adj']})'

However, i want this to be accessible via a csv file, as this list doesnt show all the genes... Any help would be much appreciated! Thanks in advance

Upvotes: 2

Views: 2691

Answers (2)

fesel2
fesel2

Reputation: 31

If you don't like to use the dictionary comprehension there is also the function sc.get.rank_genes_groups_df which makes your life easier. Quick example:

sc.tl.rank_genes_groups(adata, group, key_added = "extract_later")
df = sc.get.rank_genes_groups_df(adata, group, key = "extract_later")
df.to_csv("filepath.csv")

Upvotes: 3

YotamW Constantini
YotamW Constantini

Reputation: 410

Once you've created the dataframe, you simply need to use the to_csv function:

result = adata_subset.uns['rank_genes_groups']
groups = result['names'].dtype.names
df = pd.DataFrame(
{group + '_' + key[:1]: result[key][group]
for group in groups for key in ['names','logfoldchanges','pvals','pvals_adj']})
df.to_csv('path/to/file.csv')

Upvotes: 0

Related Questions