Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #61 from schillerlab/feature/generate_extended_mar…
Browse files Browse the repository at this point in the history
…ker_table

Add generate_extended_marker_table
  • Loading branch information
Zethson authored May 27, 2021
2 parents 5194b10 + bf3c2ba commit 70c959d
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions sc_toolbox/api/calc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,52 @@ def correlate_means_to_gene(means: pd.DataFrame, corr_gene: str = "EOMES"):
return cors


def generate_extended_marker_table(
adata: AnnData,
qval_thresh: float = 0.05,
cell_type_label: str = "cell_type",
gene_ranks_key: str = "rank_genes_groups",
):
"""
Generates an extended marker table with cell types and percentages of expressed cell types per cluster.
Run scanpy.tl.rank_genes_groups before using this function.
Args:
adata: AnnData object containing ranked genes
qval_thresh: Threshold to filter the log fold change for
cell_type_label: Label containing all cell types
gene_ranks_key: Key for the ranked gene groups (generated by sc.tl.rank_genes_groups)
Returns:
A Pandas DataFrame
"""
result = adata.uns[gene_ranks_key]
all_markers = []

for cluster in result["names"].dtype.names:
current = pd.DataFrame(
{
"gene": result["names"][cluster],
"score": result["scores"][cluster],
"log_FC": result["logfoldchanges"][cluster],
"pval": result["pvals"][cluster],
"pval_adj": result["pvals_adj"][cluster],
"cell_type": cluster,
}
)

# Add percentage expressed per cell type
adata.obs["group"] = ["within" if ct == cluster else "outside" for ct in adata.obs.loc[:, cell_type_label]]
current = add_percentages(adata, table=current, group_by="group", gene_label="gene", ids=["within", "outside"])

all_markers.append(current)

all_markers_df = pd.concat(all_markers)
all_markers_df = all_markers[all_markers_df.pval_adj < qval_thresh].copy()

return all_markers_df


def automated_marker_annotation(
adata: AnnData,
organism: str,
Expand Down

0 comments on commit 70c959d

Please sign in to comment.