`MAP_dispersions` of refitted genes keep the value from before outlier replacement

オープン 初心者向け
#519 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@aviralgarg05 がすでに取り組んでいます。

2026年11月17日 から。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
75/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python

調査の方向性

pydeseq2/dds.py の _refit_without_outliers(1398-1409 行あたり)から始め、そこでは refit の列がコピーし戻されており、すべての列をコピーする DESeq2 の refitWithoutOutliers と比較する。コピーを MAP_dispersions、_genewise_converged、_MAP_converged、_LFC_converged、_outlier_genes に拡張し、その後で再現を再実行して g1/g2 の MAP_dispersions が dispersions と一致することを確認し、dispersion/refit テストを実行する。

索引モデルが issue の本文から書いたものです。

説明

bug

After deseq2() with refit_cooks=True, a refitted gene's MAP_dispersions is still the value from before outlier replacement, while dispersions comes from the refit: g1 has MAP_dispersions 0.59995 with and without the refit, and dispersions 0.11337 after it; DESeq2 1.50.2 reports dispMAP and dispersion of 0.11336 for g1, because refitWithoutOutliers copies every column of the refit back (mcols(object)[refitReplace, idx] <- mcols(objectSub)).
_refit_without_outliers (dds.py:1398-1409) copies _normed_means, LFC, genewise_dispersions, fitted_dispersions and dispersions, but not MAP_dispersions or the _genewise_converged, _MAP_converged, _LFC_converged and _outlier_genes columns; copying those too would fix it.

Source line numbers refer to commit 2ca3b74.

Reproduction
import numpy as np
import pandas as pd
from pydeseq2.dds import DeseqDataSet


def simulate(n_genes=200, groups=(4, 4), seed=0):
	"""Counts (samples x genes) and metadata; dispersion 0.05 + 1 / mean."""
	rng = np.random.default_rng(seed)
	group = np.repeat(np.arange(len(groups)), groups)
	base = rng.lognormal(4, 1.5, n_genes)
	lfc = rng.normal(0, 0.5, (len(groups), n_genes))
	lfc[0] = 0
	mu = base * np.exp(lfc[group])
	disp = 0.05 + 1 / base
	counts = rng.negative_binomial(1 / disp, 1 / (1 + disp * mu))
	samples = [f"s{i}" for i in range(len(group))]
	counts = pd.DataFrame(counts, index=samples, columns=[f"g{j}" for j in range(n_genes)])
	metadata = pd.DataFrame({"condition": np.array(list("ABCDEF"))[group]}, index=samples)
	return counts, metadata


def dataset(counts, metadata, **kwargs):
	return DeseqDataSet(counts=counts, metadata=metadata, n_cpus=1, quiet=True, **kwargs)


counts, metadata = simulate(n_genes=100, groups=(7, 7), seed=2)
counts.loc["s0", ["g1", "g2"]] *= 20
counts.to_csv("counts.csv")
metadata.to_csv("metadata.csv")
for refit in [True, False]:
	dds = dataset(counts, metadata, refit_cooks=refit)
	dds.deseq2()
	columns = ["MAP_dispersions", "dispersions"] if refit else ["MAP_dispersions"]
	print(f"refit_cooks={refit}:", dds.var.loc[["g1", "g2", "g4"], columns].round(5).to_dict())

Expected: after the refit, MAP_dispersions equal to dispersions for g1 and g2, as in DESeq2.

Actual, with PyDESeq2 0.6.0:

refit_cooks=True: {'MAP_dispersions': {'g1': 0.59995, 'g2': 0.41028, 'g4': 0.06673}, 'dispersions': {'g1': 0.11337, 'g2': 0.07503, 'g4': 0.06673}}
refit_cooks=False: {'MAP_dispersions': {'g1': 0.59995, 'g2': 0.41028, 'g4': 0.06673}}

DESeq2 1.50.2 on the counts that the reproduction writes:

suppressMessages(library(DESeq2))
counts <- t(as.matrix(read.csv("counts.csv", row.names = 1, check.names = FALSE)))
coldata <- read.csv("metadata.csv", row.names = 1, stringsAsFactors = TRUE)
dds <- DESeq(DESeqDataSetFromMatrix(counts, coldata, ~condition), quiet = TRUE)
round(as.data.frame(mcols(dds)[c("g1", "g2", "g4"), c("dispMAP", "dispersion")]), 5)
#    dispMAP dispersion
# g1 0.11336    0.11336
# g2 0.07500    0.07500
# g4 0.06669    0.06669

Split out from #491.

主要言語
Python
スター
770
フォーク
98
平均マージ
19時間 55分
マージ済み PR(30日)
45

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

scverse/PyDESeq2 のほかの issue

scverse/PyDESeq2 の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。