<gh_stars>0
"""
Creates a pseudobulked cell type gene signature from scRNAseq data
From sc-rna-tools package

Created on Mon Jan 10 15:57:46 2022

@author: <NAME> (<EMAIL>)
"""

# external imports
from anndata import AnnData
from typing import Optional
import pandas as pd

# sc-rna-tools package imports
from .._configs import configs
from .._utils import debug, check_path

logger = configs.create_logger(__name__.split('_', 1)[1])


# -------------------------------------------------------function----------------------------------------------------- #


@debug(logger, configs)
def create_cell_type_signature(
        adata: AnnData,
        save_path: Optional[str] = None,
        cell_type_labels: str = "cell_type",
        data_loc: str = "X",
) -> pd.DataFrame:
    """

    Parameters
    ----------
        adata
            The AnnData object containing the cell type expression data, with cell labels as a key in 'adata.obs' and
            library size corrected and log-normalized counts in adata.X, adata.raw, or a layer in adata.layers.
        save_path
            The path to save a csv containing the cell type gene signatures to
        cell_type_labels
            The column name in 'adata.obs' containing the cell type labels for each cell. Default is 'cell_type'
        data_loc
            The location of the library size corrected and log-normalized gene expression data in 'adata'. Default is
            'X' but can also be 'raw' or a valid key from 'adata.layers'
    Returns
    -------
    A DataFrame containing the psuedobulked gene signatures for each cell type in adata.obs[cell_type_labels] with
    columns containing data each cell type and rows containing the average expression for each gene within that call
    type.
    """
    if cell_type_labels not in adata.obs:
        raise ValueError(f"{cell_type_labels} not a valid key in 'adata.obs'")
    signatures = pd.DataFrame(index=adata.var_names)
    for cell_type in adata.obs[cell_type_labels].unique():
        if data_loc == "X":
            signatures[cell_type] = adata[adata.obs[cell_type_labels] == cell_type].X.mean(axis=0)
        elif data_loc == "raw":
            signatures[cell_type] = adata[adata.obs[cell_type_labels] == cell_type].raw.X.mean(axis=0)
        else:
            if data_loc in adata.layers:
                signatures[cell_type] = adata[adata.obs[cell_type_labels] == cell_type].layers[data_loc].mean(axis=0)
            else:
                raise ValueError(f"{data_loc} not 'X', 'raw', or a valid layer in 'adata.layers'")
    if save_path is not None:
        check_path(save_path.rsplit("/")[0])
        logger.info(f"Saving signature DataFrame to {save_path}")
        signatures.to_csv(save_path)
    return signatures

"""
Creates a pseudobulked cell type gene signature from scRNAseq data
From sc-rna-tools package

Created on Mon Jan 10 15:57:46 2022

@author: <NAME> (<EMAIL>)
"""

# external imports
from anndata import AnnData
from typing import Optional
import pandas as pd

# sc-rna-tools package imports
from .._configs import configs
from .._utils import debug, check_path

logger = configs.create_logger(__name__.split('_', 1)[1])


# -------------------------------------------------------function----------------------------------------------------- #


@debug(logger, configs)
def create_cell_type_signature(
        adata: AnnData,
        save_path: Optional[str] = None,
        cell_type_labels: str = "cell_type",
        data_loc: str = "X",
) -> pd.DataFrame:
    """

    Parameters
    ----------
        adata
            The AnnData object containing the cell type expression data, with cell labels as a key in 'adata.obs' and
            library size corrected and log-normalized counts in adata.X, adata.raw, or a layer in adata.layers.
        save_path
            The path to save a csv containing the cell type gene signatures to
        cell_type_labels
            The column name in 'adata.obs' containing the cell type labels for each cell. Default is 'cell_type'
        data_loc
            The location of the library size corrected and log-normalized gene expression data in 'adata'. Default is
            'X' but can also be 'raw' or a valid key from 'adata.layers'
    Returns
    -------
    A DataFrame containing the psuedobulked gene signatures for each cell type in adata.obs[cell_type_labels] with
    columns containing data each cell type and rows containing the average expression for each gene within that call
    type.
    """
    if cell_type_labels not in adata.obs:
        raise ValueError(f"{cell_type_labels} not a valid key in 'adata.obs'")
    signatures = pd.DataFrame(index=adata.var_names)
    for cell_type in adata.obs[cell_type_labels].unique():
        if data_loc == "X":
            signatures[cell_type] = adata[adata.obs[cell_type_labels] == cell_type].X.mean(axis=0)
        elif data_loc == "raw":
            signatures[cell_type] = adata[adata.obs[cell_type_labels] == cell_type].raw.X.mean(axis=0)
        else:
            if data_loc in adata.layers:
                signatures[cell_type] = adata[adata.obs[cell_type_labels] == cell_type].layers[data_loc].mean(axis=0)
            else:
                raise ValueError(f"{data_loc} not 'X', 'raw', or a valid layer in 'adata.layers'")
    if save_path is not None:
        check_path(save_path.rsplit("/")[0])
        logger.info(f"Saving signature DataFrame to {save_path}")
        signatures.to_csv(save_path)
    return signatures
Creates a pseudobulked cell type gene signature from scRNAseq data From sc-rna-tools package  Created on Mon Jan 10 15:57:46 2022  @author: <NAME> (<EMAIL>) # external imports # sc-rna-tools package imports # -------------------------------------------------------function----------------------------------------------------- # Parameters     ----------         adata             The AnnData object containing the cell type expression data, with cell labels as a key in 'adata.obs' and             library size corrected and log-normalized counts in adata.X, adata.raw, or a layer in adata.layers.         save_path             The path to save a csv containing the cell type gene signatures to         cell_type_labels             The column name in 'adata.obs' containing the cell type labels for each cell. Default is 'cell_type'         data_loc             The location of the library size corrected and log-normalized gene expression data in 'adata'. Default is             'X' but can also be 'raw' or a valid key from 'adata.layers'     Returns     -------     A DataFrame containing the psuedobulked gene signatures for each cell type in adata.obs[cell_type_labels] with     columns containing data each cell type and rows containing the average expression for each gene within that call     type.