scanpy icon indicating copy to clipboard operation
scanpy copied to clipboard

Add "sort_by" in rank_genes_groups

Open zhangnan0107 opened this issue 2 years ago • 1 comments

  • [x] Additional function parameters / changed functionality / changed defaults?
  • [ ] New analysis tool: A simple analysis tool you have been using and are missing in sc.tools?
  • [ ] New plotting function: A kind of plot you would like to seein sc.pl?
  • [ ] External tools: Do you know an existing package that should go into sc.external.*?
  • [ ] Other?

Hi,

Thanks for developing scanpy, which is quite handy. I am just wondering would it be possible to add a parameter like sort_by in tl.rank_genes_groups(), so sorting by logfoldchanges could be feasible for each group. Or I will appreciate it if you can help me with alternatives.

I understand this might be a minor thing, so please feel free to say no :)

Thanks, Nan

zhangnan0107 avatar Apr 29 '22 09:04 zhangnan0107

The tl.rank_genes_groups function already sorts genes by their statistic score from greatest to smallest. If you want to sort the genes by log fold change from greatest to smallest, you can get the pandas dataframe of genes for each group and sort them using pandas function.

df = sc.get.rank_genes_groups_df(adata, group="1")
df_sorted = df.sort_values('logfoldchanges', ascending=False)

racng avatar Jun 08 '22 21:06 racng