Skip to content

Treat DESeq design factors as categorical - #117

Open
crtag wants to merge 2 commits into
ArcInstitute:mainfrom
zettalabs-nz:deseq-string-factors
Open

crtag wants to merge 2 commits into
ArcInstitute:mainfrom
zettalabs-nz:deseq-string-factors

Conversation

@crtag

@crtag crtag commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up from #116. pydeseq2 before 0.5 cast design factors to strings; from 0.5 an integer column such as replicate is fitted as a continuous covariate. runDESeq now casts integer factors to strings on a copy of obs, so the caller's adata is left as it was, and the docstring says which columns are treated as categorical. The contrast is on condition, so the order of the replicate levels does not matter.

tests/test_deseq.py checks that an integer replicate gives the same results as the same column stored as strings. It fails on main and passes here, on Python 3.11 and 3.12.

Difference from main before #116 (pydeseq2 0.4.12) on a seeded synthetic table (400 guides, A vs B, replicate stored as an integer), with runDESeq(adata, ['replicate', 'condition']) and extractDESeqResults on B vs A:

2 replicates 3 replicates
main today (pydeseq2 0.5.4) log2FoldChange up to 1.4e-05 log2FoldChange up to 0.043, raw p-values up to 0.23; 42 guides at p < 0.05 instead of 41
this PR (pydeseq2 0.5.4) below 1e-12 below 1e-10

With this PR, pydeseq2 0.7.0 on Python 3.12 gives the same numbers as 0.5.4. calculateDrugScreenDESeq passes only condition, a string column, so its results do not change.

pydeseq2 before 0.5 cast design factors to strings. From 0.5 an integer
column such as replicate is fitted as a continuous covariate, which
changes the model with three or more replicates. runDESeq now casts the
factors to strings on a copy of obs, as before.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the runDESeq function to copy metadata and cast design factors to strings, ensuring they are treated as categorical variables in pydeseq2 0.5+, and adds a unit test to verify this behavior. The review feedback points out that unconditionally casting all design factors to strings will break continuous covariates (such as float columns) and suggests only casting integer columns to strings.

Comment thread screenpro/phenoscore/deseq.py Outdated
Comment on lines +25 to +26
metadata = adata.obs.copy()
metadata[factors] = metadata[factors].astype(str)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Unconditionally casting all design factors to strings using metadata[factors].astype(str) will break the ability to use continuous covariates (such as float columns representing batch scores, QC metrics, or surrogate variables) in the DESeq2 design. If a float column is cast to string, each unique float value will be treated as a separate category, which can lead to model fitting failures (e.g., non-full-rank design matrix) or incorrect results.

To preserve the ability to use continuous covariates while ensuring integer design factors (like replicate) are treated as categorical, we should only cast integer columns to strings.

    metadata = adata.obs.copy()
    for factor in factors:
        if pd.api.types.is_integer_dtype(metadata[factor]):
            metadata[factor] = metadata[factor].astype(str)
References
  1. When upgrading pydeseq2 to version 0.5.0 or higher, design factors in metadata (obs) should be cast to strings if they contain integer values (such as replicate numbers) to prevent them from being fitted as numeric variables, which can alter the statistical model when there are 3 or more replicates.

Float columns stay numeric covariates; integer ones such as replicate
are still treated as categorical.
@crtag

crtag commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

Adopted the suggestion by Gemini above: runDESeq now casts only integer columns to strings, so replicate stays categorical and float covariates stay numeric. The test and the synthetic check give the same results as before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant