Skip to content

Commit 2b6d375

Browse files
committed
Extend visualization tests from coverage review
1 parent 3336a85 commit 2b6d375

1 file changed

Lines changed: 122 additions & 0 deletions

File tree

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

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module ProteinInferenceVisTests
22

33
open System
44
open System.IO
5+
open System.Text.RegularExpressions
56
open Expecto
67
open BioFSharp.Mz
78
open BioFSharp.PeptideClassification
@@ -138,4 +139,125 @@ let tests =
138139
(abs (Array.sum yValues - 1.0) <= 1e-6)
139140
"the decoy relative frequencies sum to one"
140141
)
142+
143+
testCase "the target histogram bins target scores with the requested bandwidth, normalized over targets" <| fun _ ->
144+
let target score =
145+
ProteinInference.createInferredProteinClassItemScored
146+
(ProteinInference.proteinGroupToString [|sprintf "T%g" score|])
147+
cl
148+
[|"target-peptide"|]
149+
score
150+
-50.0
151+
false
152+
false
153+
true
154+
|> fun item -> ProteinInference.createInferredProteinClassItemQValue item 0.01
155+
let targetItems = [|target 0.0; target 10.0; target 20.0|]
156+
let decoyItems = items |> Array.filter (fun item -> item.InfProtClassItem.Decoy)
157+
let fixture = Array.append targetItems decoyItems
158+
let parseNumbers (value: string) =
159+
value.Split([|','|], StringSplitOptions.RemoveEmptyEntries)
160+
|> Array.map (fun number -> Double.Parse(number.Trim(), Globalization.CultureInfo.InvariantCulture))
161+
let extractTargetY html =
162+
let trace =
163+
Regex.Match(
164+
html,
165+
"\"y\":\\[([^\\]]*)\\](?:[^{}]|\\{[^{}]*\\})*?\"name\":\"Target\"")
166+
Expect.isTrue trace.Success "the Target trace with a y array is present"
167+
if trace.Success then parseNumbers trace.Groups.[1].Value else [||]
168+
169+
withTempDirectory (fun tempDir ->
170+
let basePath1 = Path.Combine(tempDir, "target-bandwidth-1")
171+
let basePath2 = Path.Combine(tempDir, "target-bandwidth-100")
172+
BioFSharp.Mz.Vis.ProteinInference.qValueHitsVisualization 1.0 fixture basePath1 false
173+
BioFSharp.Mz.Vis.ProteinInference.qValueHitsVisualization 100.0 fixture basePath2 false
174+
let y1 = File.ReadAllText(basePath1 + "_QValueGraph.html") |> extractTargetY
175+
let y100 = File.ReadAllText(basePath2 + "_QValueGraph.html") |> extractTargetY
176+
Expect.equal y1.Length 3 "unit bandwidth leaves the three distinct target score bins"
177+
y1 |> Array.iter (fun value -> Expect.isTrue (abs (value - (1.0 / 3.0)) <= 1e-6) "each unit-bandwidth target bin has relative frequency one third")
178+
Expect.equal y100.Length 1 "bandwidth 100 pools the target scores into one bin"
179+
if y100.Length = 1 then
180+
Expect.isTrue (abs (y100.[0] - 1.0) <= 1e-6) "the pooled target bin has relative frequency one"
181+
// three distinct scores with unit bandwidth occupy three bins of relative frequency 1/3 each; a 100-wide bandwidth pools them into one bin of frequency 1 - counting, not implementation output. The -50 decoy scores prove the target trace reads TargetScore (target normalization is correct today; only the decoy side is pended).
182+
)
183+
184+
testCase "the q-value scatter pairs each protein's identity-appropriate score with its q-value" <| fun _ ->
185+
let target =
186+
ProteinInference.createInferredProteinClassItemScored
187+
(ProteinInference.proteinGroupToString [|"TARGET"|])
188+
cl
189+
[|"target-peptide"|]
190+
12.0
191+
-1.0
192+
false
193+
false
194+
true
195+
|> fun item -> ProteinInference.createInferredProteinClassItemQValue item 0.01
196+
let decoy =
197+
ProteinInference.createInferredProteinClassItemScored
198+
(ProteinInference.proteinGroupToString [|"DECOY"|])
199+
cl
200+
[|"decoy-peptide"|]
201+
99.0
202+
3.0
203+
true
204+
true
205+
true
206+
|> fun item -> ProteinInference.createInferredProteinClassItemQValue item 0.20
207+
let fixture = [|target; decoy|]
208+
let parseNumbers (value: string) =
209+
value.Split([|','|], StringSplitOptions.RemoveEmptyEntries)
210+
|> Array.map (fun number -> Double.Parse(number.Trim(), Globalization.CultureInfo.InvariantCulture))
211+
withTempDirectory (fun tempDir ->
212+
let basePath = Path.Combine(tempDir, "q-value-pairs")
213+
BioFSharp.Mz.Vis.ProteinInference.qValueHitsVisualization 1.0 fixture basePath false
214+
let html = File.ReadAllText(basePath + "_QValueGraph.html")
215+
let trace =
216+
Regex.Match(
217+
html,
218+
"\"x\":\\[([^\\]]*)\\](?:[^{}]|\\{[^{}]*\\})*?\"y\":\\[([^\\]]*)\\](?:[^{}]|\\{[^{}]*\\})*?\"name\":\"Q-Values\"")
219+
Expect.isTrue trace.Success "the Q-Values trace with x and y arrays is present"
220+
if trace.Success then
221+
let xValues = parseNumbers trace.Groups.[1].Value
222+
let yValues = parseNumbers trace.Groups.[2].Value
223+
Expect.equal xValues.Length yValues.Length "the Q-Values x and y arrays have equal length"
224+
let pairs = Array.zip xValues yValues
225+
let hasPair x y = pairs |> Array.exists (fun (actualX, actualY) -> abs (actualX - x) <= 1e-9 && abs (actualY - y) <= 1e-9)
226+
Expect.isTrue (hasPair 12.0 0.01) "the target is paired with its target score and q-value"
227+
Expect.isTrue (hasPair 3.0 0.20) "the decoy is paired with its decoy score and q-value"
228+
Expect.isFalse (hasPair 99.0 0.20) "the decoy is not plotted at its target score"
229+
// a target is plotted at its target score, a decoy at its decoy score, each with its own q-value - the central inference result of the report; pair membership is order-agnostic.
230+
)
231+
232+
testCase "the absolute-frequency axis carries the raw target and decoy counts" <| fun _ ->
233+
let target score =
234+
ProteinInference.createInferredProteinClassItemScored
235+
(ProteinInference.proteinGroupToString [|sprintf "T%g" score|])
236+
cl
237+
[|"target-peptide"|]
238+
score
239+
-50.0
240+
false
241+
false
242+
true
243+
|> fun item -> ProteinInference.createInferredProteinClassItemQValue item 0.01
244+
let fixture = Array.append [|target 0.0; target 10.0; target 20.0|] (items |> Array.filter (fun item -> item.InfProtClassItem.Decoy))
245+
let parseNumbers (value: string) =
246+
value.Split([|','|], StringSplitOptions.RemoveEmptyEntries)
247+
|> Array.map (fun number -> Double.Parse(number.Trim(), Globalization.CultureInfo.InvariantCulture))
248+
withTempDirectory (fun tempDir ->
249+
let basePath = Path.Combine(tempDir, "absolute-frequency")
250+
BioFSharp.Mz.Vis.ProteinInference.qValueHitsVisualization 1.0 fixture basePath false
251+
let html = File.ReadAllText(basePath + "_QValueGraph.html")
252+
let y2Traces =
253+
Regex.Matches(
254+
html,
255+
"\"y\":\\[([^\\]]*)\\](?:[^{}]|\\{[^{}]*\\})*?\"yaxis\":\"y2\"")
256+
|> Seq.cast<Match>
257+
|> Seq.map (fun trace -> parseNumbers trace.Groups.[1].Value)
258+
|> Seq.toArray
259+
let sums = y2Traces |> Array.map Array.sum |> Array.sort
260+
Expect.equal sums [|2.0; 3.0|] "the absolute-frequency traces sum to the two raw partition counts"
261+
// the report exposes an absolute-frequency axis; its traces must total the actual 3 targets and 2 decoys - input counting, robust to bin positions and trace order.
262+
)
141263
]

0 commit comments

Comments
 (0)