pydeseq2.utils

Functions

build_design_matrix(metadata[, ...])

Build design_matrix matrix for DEA.

dispersion_trend(normed_mean, coeffs)

Return dispersion trend from normalized counts.

dnb_nll(counts, mu, alpha)

Gradient of the negative log-likelihood of a negative binomial.

fit_alpha_mle(counts, design_matrix, mu, ...)

Estimate the dispersion parameter of a negative binomial GLM.

fit_lin_mu(counts, size_factors, design_matrix)

Estimate mean of negative binomial model using a linear regression.

fit_moments_dispersions(normed_counts, ...)

Dispersion estimates based on moments, as per the R code.

fit_rough_dispersions(normed_counts, ...)

Rough dispersion estimates from linear model, as per the R code.

get_num_processes([n_cpus])

Return the number of processes to use for multiprocessing.

irls_solver(counts, size_factors, ...[, ...])

Fit a NB GLM wit log-link to predict counts from the design matrix.

load_example_data([modality, dataset, ...])

Load synthetic example data.

make_scatter(disps, legend_labels, x_val[, ...])

Create a scatter plot using matplotlib.

nb_nll(counts, mu, alpha)

Neg log-likelihood of a negative binomial of parameters mu and alpha.

nbinomFn(beta, design_matrix, counts, size, ...)

Return the NB negative likelihood with apeGLM prior.

nbinomGLM(design_matrix, counts, size, ...)

Fit a negative binomial MAP LFC using an apeGLM prior.

robust_method_of_moments_disp(normed_counts, ...)

Perform dispersion estimation using a method of trimmed moments.

test_valid_counts(counts)

Test that the count matrix contains valid inputs.

trimmed_cell_variance(counts, cells)

Return trimmed variance of counts according to condition.

trimmed_mean(x[, trim])

Return trimmed mean.

trimmed_variance(x[, trim, axis])

Return trimmed variance.

wald_test(design_matrix, disp, lfc, mu, ...)

Run Wald test for differential expression.

build_design_matrix(metadata, design_factors='condition', ref_level=None, continuous_factors=None, expanded=False, intercept=True)

Build design_matrix matrix for DEA.

Unless specified, the reference factor is chosen alphabetically.

Parameters
  • metadata (pandas.DataFrame) – DataFrame containing metadata information. Must be indexed by sample barcodes.

  • design_factors (str or list) – Name of the columns of metadata to be used as design_matrix variables. (default: "condition").

  • ref_level (dict or None) – An optional list of two strings of the form ["factor", "ref_level"] specifying the factor of interest and the desired reference level, e.g. ["condition", "A"]. (default: None).

  • continuous_factors (list or None) – An optional list of continuous (as opposed to categorical) factors. Any factor not in continuous_factors will be considered categorical (default: None).

  • expanded (bool) – If true, use one column per category. Else, use n-1 columns, for each n-level categorical factor. (default: False).

  • intercept (bool) – If true, add an intercept (a column containing only ones). (default: True).

Returns

A DataFrame with experiment design information (to split cohorts). Indexed by sample barcodes.

Return type

pandas.DataFrame

dispersion_trend(normed_mean, coeffs)

Return dispersion trend from normalized counts.

\(a_1/ \mu + a_0\).

Parameters
  • normed_mean (float or ndarray) – Mean of normalized counts for a given gene or set of genes.

  • coeffs (ndarray or pd.Series) – Fitted dispersion trend coefficients \(a_0\) and \(a_1\).

Returns

Dispersion trend \(a_1/ \mu + a_0\).

Return type

float or ndarray

dnb_nll(counts, mu, alpha)

Gradient of the negative log-likelihood of a negative binomial.

Unvectorized.

Parameters
  • counts (ndarray) – Observations.

  • mu (float) – Mean of the distribution.

  • alpha (float) – Dispersion of the distribution, s.t. the variance is \(\\mu + \\alpha * \\mu^2\).

Returns

Derivative of negative log likelihood of NB w.r.t. \(\\alpha\).

Return type

float

fit_alpha_mle(counts, design_matrix, mu, alpha_hat, min_disp, max_disp, prior_disp_var=None, cr_reg=True, prior_reg=False, optimizer='L-BFGS-B')

Estimate the dispersion parameter of a negative binomial GLM.

Note: it is possible to pass counts, design_matrix and mu arguments in the form of pandas Series, but using numpy arrays makes the code significantly faster.

Parameters
  • counts (ndarray) – Raw counts for a given gene.

  • design_matrix (ndarray) – Design matrix.

  • mu (ndarray) – Mean estimation for the NB model.

  • alpha_hat (float) – Initial dispersion estimate.

  • min_disp (float) – Lower threshold for dispersion parameters.

  • max_disp (float) – Upper threshold for dispersion parameters.

  • prior_disp_var (float) – Prior dispersion variance.

  • cr_reg (bool) – Whether to use Cox-Reid regularization. (default: True).

  • prior_reg (bool) – Whether to use prior log-residual regularization. (default: False).

  • optimizer (str) – Optimizing method to use. Accepted values: ‘BFGS’ or ‘L-BFGS-B’. (default: 'L-BFGS-B').

Return type

Tuple[float, bool]

Returns

  • float – Dispersion estimate.

  • bool – Whether L-BFGS-B converged. If not, dispersion is estimated using grid search.

fit_lin_mu(counts, size_factors, design_matrix, min_mu=0.5)

Estimate mean of negative binomial model using a linear regression.

Used to initialize genewise dispersion models.

Parameters
  • counts (ndarray) – Raw counts for a given gene.

  • size_factors (ndarray) – Sample-wise scaling factors (obtained from median-of-ratios).

  • design_matrix (ndarray) – Design matrix.

  • min_mu (float) – Lower threshold for fitted means, for numerical stability. (default: 0.5).

Returns

Estimated mean.

Return type

ndarray

fit_moments_dispersions(normed_counts, size_factors)

Dispersion estimates based on moments, as per the R code.

Used as initial estimates in DeseqDataSet.fit_genewise_dispersions().

Parameters
  • normed_counts (ndarray) – Array of deseq2-normalized read counts. Rows: samples, columns: genes.

  • size_factors (ndarray) – DESeq2 normalization factors.

Returns

Estimated dispersion parameter for each gene.

Return type

ndarray

fit_rough_dispersions(normed_counts, design_matrix)

Rough dispersion estimates from linear model, as per the R code.

Used as initial estimates in DeseqDataSet.fit_genewise_dispersions().

Parameters
  • normed_counts (ndarray) – Array of deseq2-normalized read counts. Rows: samples, columns: genes.

  • design_matrix (pandas.DataFrame) – A DataFrame with experiment design information (to split cohorts). Indexed by sample barcodes. Unexpanded, with intercept.

Returns

Estimated dispersion parameter for each gene.

Return type

ndarray

get_num_processes(n_cpus=None)

Return the number of processes to use for multiprocessing.

Returns the maximum number of available cpus by default.

Parameters

n_cpus (int or None) – Desired number of cpus. If None, will return the number of available cpus. (default: None).

Returns

Number of processes to spawn.

Return type

int

irls_solver(counts, size_factors, design_matrix, disp, min_mu=0.5, beta_tol=1e-08, min_beta=- 30, max_beta=30, optimizer='L-BFGS-B', maxiter=250)

Fit a NB GLM wit log-link to predict counts from the design matrix.

See equations (1-2) in the DESeq2 paper.

Parameters
  • counts (ndarray) – Raw counts for a given gene.

  • size_factors (ndarray) – Sample-wise scaling factors (obtained from median-of-ratios).

  • design_matrix (ndarray) – Design matrix.

  • disp (float) – Gene-wise dispersion prior.

  • min_mu (float) – Lower bound on estimated means, to ensure numerical stability. (default: 0.5).

  • beta_tol (float) – Stopping criterion for IRWLS: \(\vert dev - dev_{old}\vert / \vert dev + 0.1 \vert < \beta_{tol}\). (default: 1e-8).

  • min_beta (float) – Lower-bound on LFC. (default: -30).

  • max_beta (float) – Upper-bound on LFC. (default: -30).

  • optimizer (str) – Optimizing method to use in case IRLS starts diverging. Accepted values: ‘BFGS’ or ‘L-BFGS-B’. NB: only ‘L-BFGS-B’ ensures that LFCS will lay in the [min_beta, max_beta] range. (default: 'L-BFGS-B').

  • maxiter (int) – Maximum number of IRLS iterations to perform before switching to L-BFGS-B. (default: 250).

Return type

Tuple[ndarray, ndarray, ndarray, bool]

Returns

  • beta (ndarray) – Fitted (basemean, lfc) coefficients of negative binomial GLM.

  • mu (ndarray) – Means estimated from size factors and beta: \(\mu = s_{ij} \exp(\beta^t X)\).

  • H (ndarray) – Diagonal of the \(W^{1/2} X (X^t W X)^-1 X^t W^{1/2}\) covariance matrix.

  • converged (bool) – Whether IRLS or the optimizer converged. If not and if dimension allows it, perform grid search.

load_example_data(modality='raw_counts', dataset='synthetic', debug=False, debug_seed=42)

Load synthetic example data.

May load either metadata or rna-seq data. For now, this function may only return the synthetic data provided as part of this repo, but new datasets might be added in the future.

Parameters
  • modality (str) – Data modality. “raw_counts” or “metadata”.

  • dataset (str) – The dataset for which to return gene expression data. If “synthetic”, will return the synthetic data that is used for CI unit tests. (default: "synthetic").

  • debug (bool) – If true, subsample 10 samples and 100 genes at random. (Note that the “synthetic” dataset is already 10 x 100.) (default: False).

  • debug_seed (int) – Seed for the debug mode. (default: 42).

Returns

Requested data modality.

Return type

pandas.DataFrame

make_MA_plot(results_df, padj_thresh=0.05, log=True, save_path=None, lfc_null=0, alt_hypothesis=None, **kwargs)

Create an log ratio (M)-average (A) plot using matplotlib.

Useful for looking at log fold-change versus mean expression between two groups/samples/etc. Uses matplotlib to emulate make_MA() function in DESeq2 in R.

Parameters
  • results_df (pd.DataFrame) – Resultant dataframe after running DeseqStats() and .summary().

  • padj_thresh (float) – P-value threshold to subset scatterplot colors on.

  • log (bool) – Whether or not to log scale x and y axes (default=True).

  • save_path (str or None) – The path where to save the plot. If left None, the plot won’t be saved (default=None).

  • lfc_null (float) – The (log2) log fold change under the null hypothesis. (default: 0).

  • alt_hypothesis (str or None) – The alternative hypothesis for computing wald p-values. (default: None).

  • **kwargs – Matplotlib keyword arguments for the scatter plot.

Return type

None

make_scatter(disps, legend_labels, x_val, log=True, save_path=None, **kwargs)

Create a scatter plot using matplotlib.

Used in pydeseq2.dds.DeseqDataSet.plot_dispersions().

Parameters
  • disps (list) – List of ndarrays to plot.

  • legend_labels (list) – List of strings that correspond to plotted y values for legend.

  • x_val (ndarray) – 1D array to plot (example: dds.varm['_normed_means']).

  • log (bool) – Whether or not to log scale x and y axes (default=True).

  • save_path (str or None) – The path where to save the plot. If left None, the plot won’t be saved (default=None).

  • **kwargs – Matplotlib keyword arguments for the scatter plot.

Return type

None

mean_absolute_deviation(x)

Compute a scaled estimator of the mean absolute deviation.

Used in pydeseq2.dds.DeseqDataSet.fit_dispersion_prior().

Parameters

x (ndarray) – 1D array whose MAD to compute.

Returns

Mean absolute deviation estimator.

Return type

float

nb_nll(counts, mu, alpha)

Neg log-likelihood of a negative binomial of parameters mu and alpha.

Mathematically, if counts is a vector of counting entries \(y_i\) then the likelihood of each entry \(y_i\) to be drawn from a negative binomial \(NB(\\mu, \\alpha)\) is [1]

\[\begin{split}p(y_i | \\mu, \\alpha) = \\frac{\\Gamma(y_i + \\alpha^{-1})}{ \\Gamma(y_i + 1)\\Gamma(\\alpha^{-1}) } \\left(\\frac{1}{1 + \\alpha \\mu} \\right)^{1/\\alpha} \\left(\\frac{\\mu}{\\alpha^{-1} + \\mu} \\right)^{y_i}\end{split}\]

As a consequence, assuming there are \(n\) entries, the total negative log-likelihood for counts is

\[\begin{split}\\ell(\\mu, \\alpha) = \\frac{n}{\\alpha} \\log(\\alpha) + \\sum_i \\left \\lbrace - \\log \\left( \\frac{\\Gamma(y_i + \\alpha^{-1})}{ \\Gamma(y_i + 1)\\Gamma(\\alpha^{-1}) } \\right) + (\\alpha^{-1} + y_i) \\log (\\alpha^{-1} + y_i) - y_i \\log \\mu \\right \\rbrace\end{split}\]

This is implemented in this function.

Parameters
  • counts (ndarray) – Observations.

  • mu (ndarray) – Mean of the distribution \(\\mu\).

  • alpha (float or ndarray) – Dispersion of the distribution \(\\alpha\), s.t. the variance is \(\\mu + \\alpha \\mu^2\).

Returns

Negative log likelihood of the observations counts following \(NB(\\mu, \\alpha)\).

Return type

float or ndarray

Notes

[1] https://en.wikipedia.org/wiki/Negative_binomial_distribution

nbinomFn(beta, design_matrix, counts, size, offset, prior_no_shrink_scale, prior_scale, shrink_index=1)

Return the NB negative likelihood with apeGLM prior.

Use for LFC shrinkage.

Parameters
  • beta (ndarray) – 2-element array: intercept and LFC coefficients.

  • design_matrix (ndarray) – Design matrix.

  • counts (ndarray) – Raw counts.

  • size (ndarray) – Size parameter of NB family (inverse of dispersion).

  • offset (ndarray) – Natural logarithm of size factor.

  • prior_no_shrink_scale (float) – Prior variance for the intercept.

  • prior_scale (float) – Prior variance for the intercept.

  • shrink_index (int) – Index of the LFC coordinate to shrink. (default: 1).

Returns

Sum of the NB negative likelihood and apeGLM prior.

Return type

float

nbinomGLM(design_matrix, counts, size, offset, prior_no_shrink_scale, prior_scale, optimizer='L-BFGS-B', shrink_index=1)

Fit a negative binomial MAP LFC using an apeGLM prior.

Only the LFC is shrinked, and not the intercept.

Parameters
  • design_matrix (ndarray) – Design matrix.

  • counts (ndarray) – Raw counts.

  • size (ndarray) – Size parameter of NB family (inverse of dispersion).

  • offset (ndarray) – Natural logarithm of size factor.

  • prior_no_shrink_scale (float) – Prior variance for the intercept.

  • prior_scale (float) – Prior variance for the LFC parameter.

  • optimizer (str) – Optimizing method to use in case IRLS starts diverging. Accepted values: ‘L-BFGS-B’, ‘BFGS’ or ‘Newton-CG’. (default: 'Newton-CG').

  • shrink_index (int) – Index of the LFC coordinate to shrink. (default: 1).

Return type

Tuple[ndarray, ndarray, bool]

Returns

  • beta (ndarray) – 2-element array, containing the intercept (first) and the LFC (second).

  • inv_hessian (ndarray) – Inverse of the Hessian of the objective at the estimated MAP LFC.

  • converged (bool) – Whether L-BFGS-B converged.

replace_underscores(factors)

Replace all underscores from strings in a list by hyphens.

To be used on design factors to avoid bugs due to the reliance on str.split(“_”) in parts of the code.

Parameters

factors (list) – A list of strings which may contain underscores.

Returns

A list of strings in which underscores were replaced by hyphens.

Return type

list

robust_method_of_moments_disp(normed_counts, design_matrix)

Perform dispersion estimation using a method of trimmed moments.

Used for outlier detection based on Cook’s distance.

Parameters
  • normed_counts (pandas.DataFrame) – DF of deseq2-normalized read counts. Rows: samples, columns: genes.

  • design_matrix (pandas.DataFrame) – A DataFrame with experiment design information (to split cohorts). Indexed by sample barcodes. Unexpanded, with intercept.

Returns

Trimmed method of moment dispersion estimates. Used for outlier detection based on Cook’s distance.

Return type

pandas.Series

test_valid_counts(counts)

Test that the count matrix contains valid inputs.

More precisely, test that inputs are non-negative integers.

Parameters

counts (pandas.DataFrame or ndarray) – Raw counts. One column per gene, rows are indexed by sample barcodes.

Return type

None

trimmed_cell_variance(counts, cells)

Return trimmed variance of counts according to condition.

Compute the variance after trimming data of its smallest and largest elements, grouped by cohorts, and return the max across cohorts. The trim factor is a function of data size.

Parameters
Returns

Gene-wise trimmed variance estimate.

Return type

pandas.Series

trimmed_mean(x, trim=0.1, **kwargs)

Return trimmed mean.

Compute the mean after trimming data of its smallest and largest quantiles.

Parameters
  • x (ndarray) – Data whose mean to compute.

  • trim (float) – Fraction of data to trim at each end. (default: 0.1).

  • **kwargs – Keyword arguments, useful to pass axis.

Returns

Trimmed mean.

Return type

float or ndarray

trimmed_variance(x, trim=0.125, axis=0)

Return trimmed variance.

Compute the variance after trimming data of its smallest and largest quantiles.

Parameters
  • x (ndarray) – Data whose trimmed variance to compute.

  • trim (float) – Fraction of data to trim at each end. (default: 0.125).

  • axis (int) – Dimension along which to compute variance. (default: 0).

Returns

Trimmed variances.

Return type

float or ndarray

wald_test(design_matrix, disp, lfc, mu, ridge_factor, contrast, lfc_null, alt_hypothesis)

Run Wald test for differential expression.

Computes Wald statistics, standard error and p-values from dispersion and LFC estimates.

Parameters
  • design_matrix (ndarray) – Design matrix.

  • disp (float) – Dispersion estimate.

  • lfc (ndarray) – Log-fold change estimate (in natural log scale).

  • mu (float) – Mean estimation for the NB model.

  • ridge_factor (ndarray) – Regularization factors.

  • contrast (ndarray) – Vector encoding the contrast that is being tested.

  • lfc_null (float) – The (log2) log fold change under the null hypothesis.

  • alt_hypothesis (str or None) – The alternative hypothesis for computing wald p-values.

Return type

Tuple[float, float, float]

Returns

  • wald_p_value (float) – Estimated p-value.

  • wald_statistic (float) – Wald statistic.

  • wald_se (float) – Standard error of the Wald statistic.