Skip to content

Commit b8e59a0

Browse files
committed
RESET: apply the decoy factor to --tdc-pep and --irls-pep PEPs
RESET keeps only the decoys that were not used for training, about half of them, so each remaining decoy stands for two incorrect targets. The q values account for this (nullTargetWinProb 2/3, decoy factor 2), and so do the default PEPs, which are derived from the q values. The PEPs from the target-decoy labels (--tdc-pep) used the decoy odds unscaled, and the logistic regression of --irls-pep had no factor at all (the TODO in Caller::run). Their PEPs were therefore half as large as they should be; where nearly all targets are incorrect they stayed at about 0.5 instead of 1. calcPep now multiplies the odds by nullTargetWinProb / (1 - nullTargetWinProb), which is 1 without RESET, in the --tdc-pep branch, and PosteriorEstimator::estimatePEP takes the factor as a new argument (default 1) for --irls-pep. Ground truth (500k concatenated PSMs, --shift-scale 0.6, and the easier --shift-scale 1 set), RESET, mean PEP of the targets at q < 0.01 / binned PEP error / Hosmer-Lemeshow chi2/df, before -> after; the observed FDR is 0.0097 and 0.0087: - med --tdc-pep: 0.0046 / 0.298 / 1038 -> 0.0093 / 0.0034 / 1.66 - med --irls-pep: 0.0050 / 0.305 / 1119 -> 0.0099 / 0.0063 / 0.67 - easy --tdc-pep: 0.0049 / 0.298 / 1105 -> 0.0097 / 0.0033 / 0.77 - easy --irls-pep: 0.0061 / 0.290 / 435 -> 0.0122 / 0.0056 / 5.54 The default PEPs (0.0095 / 0.0035 and 0.0098 / 0.0036) and all q values are unchanged. A new unit test checks, on data with one decoy per two incorrect targets, that the mean PEP of the targets below q = 0.05 is within 0.7-1.4 times their fraction of incorrect targets for all three methods (it fails for --tdc-pep and --irls-pep without this change), and that the tdc and irls PEPs are twice those with nullTargetWinProb 1/2. Output files without RESET are byte-identical, as are RESET outputs with the default PEPs. Interface: PosteriorEstimator::estimatePEP has a new last argument oddsFactor (default 1). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017yywhfE3wwkLwPUfoERjp6
1 parent 3fb38fa commit b8e59a0

5 files changed

Lines changed: 93 additions & 8 deletions

File tree

‎src/Caller.cpp‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,9 +1302,6 @@ int Caller::run() {
13021302
numFolds = 1u;
13031303
nestedXvalBins_ = 3u;
13041304
}
1305-
1306-
// TODO:
1307-
// - apply decoy factor in PosteriorEstimator::estimatePEP
13081305
}
13091306

13101307
// an explicit --num-folds 3 behaves like the default, which has never

‎src/PosteriorEstimator.cpp‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ double mymin(double a, double b) {
9292
}
9393

9494
void PosteriorEstimator::estimatePEP(vector<pair<double, bool> >& combined,
95-
bool usePi0, double pi0, vector<double>& peps, bool include_negative) {
95+
bool usePi0, double pi0, vector<double>& peps, bool include_negative,
96+
double oddsFactor) {
9697
// Logistic regression on the data
9798
LogisticRegression lr;
9899
estimate(combined, lr, usePi0, pi0);
@@ -114,15 +115,16 @@ void PosteriorEstimator::estimatePEP(vector<pair<double, bool> >& combined,
114115
#endif
115116
if (peps.size() == 0) return;
116117

117-
double top = min(1.0, exp(*max_element(peps.begin(), peps.end())));
118+
double top =
119+
min(1.0, oddsFactor * exp(*max_element(peps.begin(), peps.end())));
118120
vector<double>::iterator pep = peps.begin();
119121
bool crap = false;
120122
for (; pep != peps.end(); ++pep) {
121123
if (crap) {
122124
*pep = top;
123125
continue;
124126
}
125-
*pep = exp(*pep);
127+
*pep = oddsFactor * exp(*pep);
126128
if (*pep >= top) {
127129
*pep = top;
128130
crap = true;

‎src/PosteriorEstimator.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ class PosteriorEstimator {
3333
bool parseOptions(int argc, char** argv);
3434
string greeter();
3535
int run();
36+
// PEP = oddsFactor * (fitted decoy odds), capped at 1; oddsFactor is the
37+
// expected number of incorrect targets per decoy (2 with RESET)
3638
static void estimatePEP(std::vector<std::pair<double, bool> >& combined,
3739
bool usePi0, double pi0, std::vector<double>& peps,
38-
bool include_negative = false);
40+
bool include_negative = false, double oddsFactor = 1.0);
3941
static void getPValues(const std::vector<std::pair<double, bool> >& combined,
4042
std::vector<double>& p);
4143
static void getQValues(double pi0,

‎src/Scores.cpp‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,10 @@ void Scores::checkSeparationAndSetPi0() {
12001200

12011201
void Scores::calcPep(const bool spline, const bool interp, const bool pava,
12021202
const bool tdc) {
1203+
// Expected number of incorrect targets per decoy, as in the q values: 1
1204+
// in target-decoy competition, 2 with RESET, which keeps only the half
1205+
// of the decoys that were not used for training.
1206+
const double decoyFactor = nullTargetWinProb_ / (1.0 - nullTargetWinProb_);
12031207
if (!spline && tdc) {
12041208
// Fit the decoy rate p as a monotone function of the score and
12051209
// report the odds p / (1 - p) as PEP, for targets and decoys alike.
@@ -1219,6 +1223,7 @@ void Scores::calcPep(const bool spline, const bool interp, const bool pava,
12191223
for (double d : is_decoy) (d > 0.5 ? numDecoys : numTargets) += 1.0;
12201224
oddsFactor = pi0_ * numTargets / std::max(1.0, numDecoys);
12211225
}
1226+
oddsFactor *= decoyFactor;
12221227
InferPEP reg(!pava);
12231228
auto peps = interp ? reg.tdc_to_pep(is_decoy, sc, oddsFactor)
12241229
: reg.tdc_to_pep(is_decoy, {}, oddsFactor);
@@ -1266,7 +1271,8 @@ void Scores::calcPep(const bool spline, const bool interp, const bool pava,
12661271

12671272
std::vector<double> peps;
12681273
// Logistic regression on the data
1269-
PosteriorEstimator::estimatePEP(combined, usePi0_, pi0_, peps, true);
1274+
PosteriorEstimator::estimatePEP(combined, usePi0_, pi0_, peps, true,
1275+
decoyFactor);
12701276
for (size_t ix = 0; ix < scores_.size(); ix++) {
12711277
scores_[ix].pep = peps[ix];
12721278
}

‎tests/unit_tests/percolator/UnitTest_Percolator_Scores.cpp‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,3 +1049,81 @@ TEST(ScoresQvalueTest, RandomSetsEqualGetQValues)
10491049
what + " estimated pi0");
10501050
}
10511051
}
1052+
1053+
// RESET keeps only the decoys that were not used for training, about half
1054+
// of them, so each remaining decoy stands for two incorrect targets
1055+
// (nullTargetWinProb 2/3). The q values account for this; the PEPs from the
1056+
// target-decoy labels (--tdc-pep) and from logistic regression (--irls-pep)
1057+
// must too. In the data below, half of the PSMs are incorrect targets, a
1058+
// quarter correct targets and a quarter decoys. For every PEP method, the
1059+
// mean PEP of the targets below q = 0.05 must be close to their fraction of
1060+
// incorrect targets, and the tdc and irls PEPs must be twice those computed
1061+
// with nullTargetWinProb 1/2.
1062+
TEST(ScoresPepTest, ResetDecoyFactorScalesPeps)
1063+
{
1064+
const int numPsms = 20000;
1065+
std::vector<PSMDescription> psms(numPsms);
1066+
std::vector<bool> incorrect(numPsms);
1067+
unsigned int seed = 4242u;
1068+
auto uniform = [&seed]() {
1069+
seed = seed * 1103515245u + 12345u;
1070+
return ((seed >> 8) + 0.5) / 16777216.0;
1071+
};
1072+
auto normal = [&uniform]() {
1073+
return std::sqrt(-2.0 * std::log(uniform())) *
1074+
std::cos(6.283185307179586 * uniform());
1075+
};
1076+
// method 0: from q values (default), 1: tdc, 2: irls
1077+
for (int method = 0; method < 3; ++method) {
1078+
std::vector<double> pepHalf;
1079+
for (double nullTargetWinProb : {0.5, 2.0 / 3.0}) {
1080+
Scores scores(false);
1081+
seed = 4242u;
1082+
for (int i = 0; i < numPsms; ++i) {
1083+
psms[i].scan = static_cast<unsigned int>(i);
1084+
const bool isDecoy = uniform() < 0.25;
1085+
const bool correct = !isDecoy && uniform() < 1.0 / 3.0;
1086+
incorrect[i] = !isDecoy && !correct;
1087+
scores.addScoreHolder(ScoreHolder(normal() + (correct ? 3.0 : 0.0),
1088+
isDecoy ? LabelType::DECOY : LabelType::TARGET, &psms[i]));
1089+
}
1090+
scores.recalculateSizes();
1091+
scores.setNullTargetWinProb(nullTargetWinProb);
1092+
std::sort(scores.begin(), scores.end(), std::greater<ScoreHolder>());
1093+
scores.calcQvals(0.01);
1094+
scores.calcPep(method == 2, true, false, method == 1);
1095+
double sumPep = 0.0;
1096+
int n = 0, wrong = 0;
1097+
std::vector<double> peps;
1098+
for (const ScoreHolder& sh : scores) {
1099+
ASSERT_GE(sh.pep, 0.0);
1100+
ASSERT_LE(sh.pep, 1.0);
1101+
if (!sh.isTarget()) continue;
1102+
peps.push_back(sh.pep);
1103+
if (sh.q <= 0.05) {
1104+
sumPep += sh.pep;
1105+
wrong += incorrect[sh.pPSM - &psms[0]] ? 1 : 0;
1106+
++n;
1107+
}
1108+
}
1109+
if (nullTargetWinProb == 0.5) {
1110+
pepHalf = peps;
1111+
continue;
1112+
}
1113+
ASSERT_GT(n, 500);
1114+
ASSERT_GT(wrong, 20);
1115+
const double fdp = static_cast<double>(wrong) / n;
1116+
EXPECT_GT(sumPep / n, 0.7 * fdp) << "method " << method;
1117+
EXPECT_LT(sumPep / n, 1.4 * fdp) << "method " << method;
1118+
if (method > 0) {
1119+
ASSERT_EQ(peps.size(), pepHalf.size());
1120+
for (size_t i = 0; i < peps.size(); ++i) {
1121+
if (pepHalf[i] < 0.25) {
1122+
EXPECT_NEAR(peps[i], 2.0 * pepHalf[i], 1e-12)
1123+
<< "method " << method << " target " << i;
1124+
}
1125+
}
1126+
}
1127+
}
1128+
}
1129+
}

0 commit comments

Comments
 (0)