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

Commit

Permalink
add pseudobulk
Browse files Browse the repository at this point in the history
Signed-off-by: zethson <[email protected]>
  • Loading branch information
Zethson committed Jun 8, 2021
1 parent bef4a78 commit 0cd35ff
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions sc_toolbox/api/calc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,35 @@ def extended_marker_table(
return all_markers_df


def generate_pseudobulk(adata: AnnData, group_key: str = "identifier", sep="\t", save: str = None) -> pd.DataFrame:
"""
Generates a pseudobulk for a given key of groups in the AnnData object.
Looks like:
Genes group_member_1 group_member_2
1 gene_1 value_1 value_2
2 gene_2 value_3 value_4
Args:
adata: AnnData object
group_key: The key to group by. E.g. by mice, by condition, ... (default: 'identifier')
sep: Separator to use when saving the pseudobulk table (default: '\t')
save: Path to save the pseudobulk table to (default: None)
Returns:
A Pandas DataFrame containing the pseudobulk table
"""
pseudobulk = pd.DataFrame(data=adata.var_names.values, columns=["Genes"])

for i in adata.obs.loc[:, group_key].cat.categories:
temp = adata.obs.loc[:, group_key] == i
pseudobulk[i] = adata[temp].X.sum(0, dtype=int) # column sums (genes)

if save:
pseudobulk.to_csv(save, sep=sep, index=False)

return pseudobulk


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

0 comments on commit 0cd35ff

Please sign in to comment.