Repository navigation
Conversation
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.
There was a problem hiding this comment.
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.
| metadata = adata.obs.copy() | ||
| metadata[factors] = metadata[factors].astype(str) |
There was a problem hiding this comment.
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
- 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.
|
Adopted the suggestion by Gemini above: |
Follow-up from #116. pydeseq2 before 0.5 cast design factors to strings; from 0.5 an integer column such as
replicateis fitted as a continuous covariate.runDESeqnow casts integer factors to strings on a copy ofobs, so the caller'sadatais left as it was, and the docstring says which columns are treated as categorical. The contrast is oncondition, so the order of thereplicatelevels does not matter.tests/test_deseq.pychecks that an integerreplicategives 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,
replicatestored as an integer), withrunDESeq(adata, ['replicate', 'condition'])andextractDESeqResultson B vs A:With this PR, pydeseq2 0.7.0 on Python 3.12 gives the same numbers as 0.5.4.
calculateDrugScreenDESeqpasses onlycondition, a string column, so its results do not change.