Skip to content

Exporting rankgenegroups to pandas dataframes #3486

Closed
@Fougere87

Description

@Fougere87

What kind of feature would you like to request?

Additional function parameters / changed functionality / changed defaults?

Please describe your wishes

Hi scanpy and thank you for your work :-) ,

I created a small function to export rank_gene_groups genes to panda Dataframes, so I'm proposing it:

Here is the code:

def get_df_of_rank_gene_groups(adata, key): 
    """Returns a dictionnary containing the differentially expressed genes in "key" in dataframes."""    
    rgg=adata.uns[key].copy()    
    if "params" in rgg :
        params=rgg.pop("params")     
    groups=list(rgg["names"].dtype.names) 
    d_return={}     
    for g in groups :
        df_g = pd.DataFrame({key:rgg.get(key)[g]for key in rgg.keys() if key not in ["pts", "pts_rest"]},                  
                            columns= rgg.keys())     
        df_g.index=df_g.pop("names") 
        
        if "pts" in rgg.keys():
            df_g.pop("pts") 
            if "pts_rest" in rgg.keys():
                df_g.pop("pts_rest")
                pts_df=pd.DataFrame({"pts":rgg.get("pts")[g],"pts_rest":rgg.get("pts_rest")[g]}, index=adata.var_names )
            else:
                pts_ref=rgg.get("pts")[rgg.get("pts").columns.difference(rgg["names"].dtype.names)]
                pts_df=pd.DataFrame({"pts":rgg.get("pts")[g], "pts_rest":pts_ref.iloc[:,0]}, index=adata.var_names )
            df_g=pd.concat([df_g,pts_df], axis=1)
        d_return[g]=df_g  
    
    if len(groups) > 1:         
        return d_return     
    else :         
        return d_return[groups[0]]

Depending on the number of groups, it returns a dictionnary, or a single dataframe, with genes names, pvalues and proportions of cells expressing.

I'm sure it needs improvments but it can really be useful to export DEGs for publication or sharing.

Best

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions