Skip to content

Commit 3336a85

Browse files
committed
Extend protein inference tests from coverage review
1 parent a43c948 commit 3336a85

1 file changed

Lines changed: 181 additions & 0 deletions

File tree

‎tests/BioFSharp.Mz.Tests/ProteinInferenceTests.fs‎

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ open BioFSharp.IO.GFF3
77

88
let private classC1a = BioFSharp.PeptideClassification.PeptideEvidenceClass.C1a
99

10+
let private mkPsm pepSequenceID sequence score : ProteinInference.PSMInput =
11+
{ PepSequenceID = pepSequenceID; Seq = sequence; Score = score }
12+
13+
let private mkScored decoyBetter =
14+
ProteinInference.createInferredProteinClassItemScored
15+
(ProteinInference.proteinGroupToString [|"P"|])
16+
classC1a
17+
[|"pep"|]
18+
10.0
19+
1.0
20+
false
21+
decoyBetter
22+
true
23+
1024
[<Tests>]
1125
let tests =
1226
testList "ProteinInferenceTests" [
@@ -276,6 +290,115 @@ let tests =
276290
Expect.isTrue
277291
(Set.isSubset (set ["x"; "u"]) allPeptides)
278292
"the union of inferred groups contains every input peptide"
293+
294+
testCase "maximal integration preserves every nested group with superset peptide assignment" <| fun _ ->
295+
let items =
296+
[
297+
ProteinInference.createProteinClassItem [|"P1"|] classC1a "pa"
298+
ProteinInference.createProteinClassItem [|"P1";"P2"|] classC1a "pb"
299+
ProteinInference.createProteinClassItem [|"P1";"P2";"P3"|] classC1a "pc"
300+
]
301+
let result =
302+
ProteinInference.inferSequences
303+
ProteinInference.IntegrationStrictness.Maximal
304+
ProteinInference.PeptideUsageForQuantification.Maximal
305+
items
306+
|> List.ofSeq
307+
let groupProteins (item: ProteinInference.InferredProteinClassItem<string>) =
308+
item.GroupOfProteinIDs.Split(';') |> Set.ofArray
309+
let requireGroup expected =
310+
match result |> List.tryFind (fun item -> groupProteins item = expected) with
311+
| Some group -> group
312+
| None -> failtestf "D18 observed group structure: %A" result
313+
let p1 = requireGroup (set ["P1"])
314+
let p1p2 = requireGroup (set ["P1";"P2"])
315+
let p1p2p3 = requireGroup (set ["P1";"P2";"P3"])
316+
Expect.equal (Set.ofArray p1.PeptideSequence) (set ["pa";"pb";"pc"]) "the singleton group receives every containing-source peptide"
317+
Expect.equal (Set.ofArray p1p2.PeptideSequence) (set ["pb";"pc"]) "the two-protein group receives every containing-source peptide"
318+
Expect.equal (Set.ofArray p1p2p3.PeptideSequence) (set ["pc"]) "the three-protein group receives its containing-source peptide"
319+
// documented: Maximal keeps all groups intact and usage-Maximal assigns each group every peptide whose source set CONTAINS it - the nested construction makes each expected set distinct.
320+
321+
testCase "minimal integration collapses overlapping groups to their intersection" <| fun _ ->
322+
let items =
323+
[
324+
ProteinInference.createProteinClassItem [|"P1";"P2"|] classC1a "x"
325+
ProteinInference.createProteinClassItem [|"P2";"P3"|] classC1a "y"
326+
]
327+
let run orderedItems =
328+
let result =
329+
ProteinInference.inferSequences
330+
ProteinInference.IntegrationStrictness.Minimal
331+
ProteinInference.PeptideUsageForQuantification.Maximal
332+
orderedItems
333+
|> List.ofSeq
334+
let groupProteins (item: ProteinInference.InferredProteinClassItem<string>) =
335+
item.GroupOfProteinIDs.Split(';') |> Set.ofArray
336+
match result |> List.tryFind (fun item -> groupProteins item = set ["P2"]) with
337+
| Some group -> result, group
338+
| None -> failtestf "D19 observed group structure: %A" result
339+
let resultA, groupA = run items
340+
let resultB, groupB = run (List.rev items)
341+
Expect.equal (Set.ofArray groupA.PeptideSequence) (set ["x";"y"]) "the intersection group carries both overlapping peptides"
342+
Expect.equal (Set.ofArray groupB.PeptideSequence) (set ["x";"y"]) "reversing evidence order preserves the intersection group's peptides"
343+
Expect.equal (resultA |> Seq.collect (fun item -> item.PeptideSequence) |> Set.ofSeq) (set ["x";"y"]) "the first input order retains the union of peptides"
344+
Expect.equal (resultB |> Seq.collect (fun item -> item.PeptideSequence) |> Set.ofSeq) (set ["x";"y"]) "the reversed input order retains the union of peptides"
345+
// parsimony: P2 alone explains both peptides, so minimal integration must report the intersection - and inference must not depend on evidence order.
346+
347+
testCase "usage Minimal assigns each group only its best-matching source evidence" <| fun _ ->
348+
let items =
349+
[
350+
ProteinInference.createProteinClassItem [|"P1"|] classC1a "pa"
351+
ProteinInference.createProteinClassItem [|"P1";"P2"|] classC1a "pb"
352+
ProteinInference.createProteinClassItem [|"P1";"P2";"P3"|] classC1a "pc"
353+
]
354+
let result =
355+
ProteinInference.inferSequences
356+
ProteinInference.IntegrationStrictness.Maximal
357+
ProteinInference.PeptideUsageForQuantification.Minimal
358+
items
359+
|> List.ofSeq
360+
let groupProteins (item: ProteinInference.InferredProteinClassItem<string>) =
361+
item.GroupOfProteinIDs.Split(';') |> Set.ofArray
362+
let requireGroup expected =
363+
match result |> List.tryFind (fun item -> groupProteins item = expected) with
364+
| Some group -> group
365+
| None -> failtestf "D20 observed group structure: %A" result
366+
let p1 = requireGroup (set ["P1"])
367+
let p1p2 = requireGroup (set ["P1";"P2"])
368+
let p1p2p3 = requireGroup (set ["P1";"P2";"P3"])
369+
Expect.equal (Set.ofArray p1.PeptideSequence) (set ["pa"]) "the singleton group gets only its best-matching source evidence"
370+
Expect.equal (Set.ofArray p1p2.PeptideSequence) (set ["pb"]) "the two-protein group gets only its best-matching source evidence"
371+
Expect.equal (Set.ofArray p1p2p3.PeptideSequence) (set ["pc"]) "the three-protein group gets only its best-matching source evidence"
372+
// documented: usage-Minimal restricts each group's candidates (sources containing the group) to those with the SMALLEST source set - for nested sources that is exactly the group's own evidence.
373+
374+
testCase "higher-confidence evidence classes are not displaced by overlapping lower classes" <| fun _ ->
375+
let items =
376+
[
377+
ProteinInference.createProteinClassItem [|"P1";"P2"|] classC1a "strong"
378+
ProteinInference.createProteinClassItem [|"P2";"P3"|] PeptideEvidenceClass.C2a "weak"
379+
]
380+
let run orderedItems =
381+
let result =
382+
ProteinInference.inferSequences
383+
ProteinInference.IntegrationStrictness.Minimal
384+
ProteinInference.PeptideUsageForQuantification.Maximal
385+
orderedItems
386+
|> List.ofSeq
387+
let candidate =
388+
result
389+
|> List.tryFind (fun item ->
390+
let proteins = item.GroupOfProteinIDs.Split(';') |> Set.ofArray
391+
Set.isSubset (set ["P1";"P2"]) proteins)
392+
match candidate with
393+
| Some group -> group
394+
| None -> failtestf "D21 observed group structure: %A" result
395+
let groupA = run items
396+
let groupB = run (List.rev items)
397+
Expect.equal groupA.Class classC1a "the stronger C1a evidence class survives the first order"
398+
Expect.equal groupB.Class classC1a "the stronger C1a evidence class survives the reversed order"
399+
Expect.isTrue (Array.contains "strong" groupA.PeptideSequence) "the stronger peptide survives the first order"
400+
Expect.isTrue (Array.contains "strong" groupB.PeptideSequence) "the stronger peptide survives the reversed order"
401+
// evidence-class precedence: C1a outranks C2a, so overlapping weaker evidence must not overwrite or displace the stronger group's class or membership.
279402
]
280403

281404
testList "FDR" [
@@ -302,6 +425,64 @@ let tests =
302425
"no decoy wins gives zero estimated FDR"
303426
// The standard decoy/target FDR estimate is hand-counted here as 1/2 and 0.
304427

428+
testCase "modified PSM sequences match unmodified reverse-digest peptides" <| fun _ ->
429+
let m =
430+
ProteinInference.createReverseProteinScores
431+
[| ("REV_P1", [|"MAGK"|]) |]
432+
(Map.ofList ["M[ox]AGK", 9.0])
433+
Expect.isTrue (m.ContainsKey "REV_P1") "the modified PSM matches the reverse-digest peptide after normalization"
434+
Expect.equal (fst m.["REV_P1"]) 9.0 "the normalized reverse protein receives the modified PSM score"
435+
// the score map's keys are normalized with removeModification before matching, so a modified observation must still transfer its evidence to the base-sequence decoy peptide.
436+
437+
testCase "the best PSM score survives when all scores are negative" <| fun _ ->
438+
let scoreMap =
439+
ProteinInference.createPeptideScoreMap [
440+
[ mkPsm 1 "AA" -5.0; mkPsm 2 "AA" -2.0; mkPsm 3 "AA" -7.0 ]
441+
]
442+
Expect.equal scoreMap.["AA"] -2.0 "the maximum negative PSM score is retained"
443+
// Percolator-style scores can be all-negative; a zero floor or first/last-wins would return the wrong evidence - the hand-evident maximum is -2.
444+
445+
testCase "MAYU length bins stratify by sequence length" <| fun _ ->
446+
let inferred =
447+
[|
448+
for i in 1..4 do
449+
yield
450+
ProteinInference.createInferredProteinClassItemScored
451+
(ProteinInference.proteinGroupToString [|sprintf "P%d" i|])
452+
classC1a
453+
[|sprintf "pep%d" i|]
454+
10.0
455+
1.0
456+
false
457+
false
458+
true
459+
|]
460+
let proteinsFromDB =
461+
[|
462+
("P1", String.replicate 10 "A")
463+
("P2", String.replicate 20 "A")
464+
("P3", String.replicate 100 "A")
465+
("P4", String.replicate 200 "A")
466+
|]
467+
let bins = ProteinInference.MAYU.binProteinsLength inferred proteinsFromDB 2.0
468+
let accessionSets =
469+
bins
470+
|> Array.map (fun bin ->
471+
bin
472+
|> Array.collect (fun item -> item.GroupOfProteinIDs.Split(';'))
473+
|> Set.ofArray)
474+
Expect.equal accessionSets [|set ["P1";"P2"]; set ["P3";"P4"]|] "the two shortest proteins share MAYU's low length bin"
475+
// MAYU's model conditions FDR on protein length: the two shortest must share the low bin - directly from the documented length stratification, no incidental fields locked.
476+
477+
// PENDING: with zero target wins, decoys/targets = 2/0 is IEEE Infinity; an FDR is a
478+
// proportion and its conservative bounded value is 1.0 (the module's MAYU path already applies
479+
// that bound).
480+
ptestCase "the decoy/target ratio is a bounded FDR" <| fun _ ->
481+
Expect.equal
482+
(ProteinInference.calculateFDRwithDecoyTargetRatio [| mkScored true; mkScored true |])
483+
1.0
484+
"an all-decoy result is conservatively bounded at one"
485+
305486
ptestCase "a decoy-free confident dataset has zero MAYU FDR" <| fun _ ->
306487
// PENDING: for a dataset of only confident targets (no decoy wins, all found in the DB) the
307488
// estimated number of false positives is 0, so the FDR must be 0. The implementation's

0 commit comments

Comments
 (0)