scmagnify.tools.RegDecomp#

class scmagnify.tools.RegDecomp(data, net_key='network', tensor_mode='multiscale', modes=None, filter=['quantile', 0.0, False], backend='numpy')#

A class for performing tensor decomposition on gene regulatory networks to identify regulatory factors (RegFactors).

Parameters:
  • data (AnnData | MuData | GRNMuData) – Single cell data object. Can be an anndata.AnnData, mudata.MuData, scmagnify.GRNMuData

  • net_key (str (default: 'network')) – net_key in data.uns where the network is stored.

  • tensor_mode (str (default: 'multiscale')) – The mode of the tensor to be constructed. Options are ‘multiscale’ or ‘ensemble’.

  • modes (Optional[list[str]] (default: None)) – List of mode names for the tensor. Default is [‘Lag’, ‘TF’, ‘TG’] for ‘multiscale’ and [‘TF’, ‘TG’] for ‘ensemble’.

  • filter (list[str] (default: ['quantile', 0.0, False])) – A list specifying the filtering method, parameter, and whether to binarize the network.

  • backend (str (default: 'numpy')) – The backend to use for tensor operations. Options are ‘numpy’, ‘pytorch’, ‘tensorflow’, or ‘jax’.

Attributes table#

get_tensor

Get the current tensor.

Methods table#

compute_activity([modal, layer, mode, ...])

Compute the activity of RegFactors in the dataset.

cp_decomposition(rank[, non_negative])

Perform CP decomposition on the tensor.

normalization(mode_name[, method])

Normalize the tensor along a specified mode.

rename_regfactors(rename_map[, sort, mod_key])

Rename RegFactors and optionally sort them.

tucker_decomposition([rank, non_negative, ...])

Perform Tucker decomposition on the tensor.

Attributes#

RegDecomp.get_tensor#

Get the current tensor.

Returns:

np.ndarray The current tensor.

Methods#

RegDecomp.compute_activity(modal='RNA', layer='log1p_norm', mode='TG', method='mlm', thres=0.0, mod_key='RegFactor', n_top=None)#

Compute the activity of RegFactors in the dataset.

Parameters:
  • modal (Literal['RNA', 'ATAC', 'GRN'] (default: 'RNA')) – Modality key (e.g., ‘RNA’, ‘ATAC’) when using multi-modal data. mudata.MuData or scmagnify.GRNMuData must be provided.

  • layer (str (default: 'log1p_norm')) – Layer in layers. If None, defaults to X.

  • mode (str (default: 'TG')) – The mode (‘TF’ or ‘TG’) to use for defining the RegFactors.

  • method (Literal['mlm', 'aucell'] (default: 'mlm')) – Method to compute activity.

  • thres (float (default: 0.0)) – Threshold for filtering weights.

  • mod_key (str (default: 'RegFactor')) – The key to store the activity matrix in data.mod.

  • n_top (Optional[int] (default: None)) – Number of top genes to consider for each RegFactor.

Return type:

None

Returns:

None

RegDecomp.cp_decomposition(rank, non_negative=False, **kwargs)#

Perform CP decomposition on the tensor.

Parameters:
  • rank (int) – The rank of the decomposition.

  • non_negative (bool (default: False)) – Whether to use non-negative decomposition.

  • **kwargs – additional keyword arguments for the decomposition algorithm.

Returns:

tuple A tuple containing weights, factors, and normalized root mean squared error (NRE).

RegDecomp.normalization(mode_name, method='proportion')#

Normalize the tensor along a specified mode.

Parameters:
  • mode_name (str) – The name of the mode along which normalization should be applied.

  • method (Literal['proportion', 'normalization', 'max'] (default: 'proportion')) – The normalization method.

Return type:

None

Returns:

None

RegDecomp.rename_regfactors(rename_map, sort=False, mod_key='RegFactor')#

Rename RegFactors and optionally sort them.

Parameters:
  • rename_map (dict[str, str]) – A dictionary mapping old RegFactor names to new names. Example: {‘RegFactor_1’: ‘Proliferation’, ‘RegFactor_2’: ‘Apoptosis’}

  • sort (Union[bool, list[str], Literal['alphabetical']] (default: False)) –

    • False: Only rename, do not change the order.

    • True or 'alphabetical': Sort the newly named factors alphabetically.

    • List[str]: Sort the factors according to the provided list of new names.

  • mod_key (str (default: 'RegFactor')) – The key where the activity AnnData object is stored in data.mod.

Return type:

None

RegDecomp.tucker_decomposition(rank=None, non_negative=False, regfactor_key='regfactors', **kwargs)#

Perform Tucker decomposition on the tensor.

Parameters:
  • rank (Union[int, tuple, list, None] (default: None)) – The rank of the decomposition for each mode.

  • non_negative (bool (default: False)) – Whether to use non-negative decomposition.

  • regfactor_key (str (default: 'regfactors')) – The key to store the resulting regulatory factors (RegFactors).

  • **kwargs – additional keyword arguments for the tensorly.decomposition algorithm.

Returns:

tuple A tuple containing core tensor, factors, and normalized root mean squared error (NRE).