Skip to content

Commit

Permalink
update median of ratios results type
Browse files Browse the repository at this point in the history
  • Loading branch information
bvenn committed Jan 25, 2023
1 parent e4486b4 commit 278bd95
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
28 changes: 18 additions & 10 deletions src/FSharp.Stats/Signal/Normalization.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ module Normalization =
let std = Seq.stDev yVal
yVal |> Vector.map (fun x -> (x - yMean) / std)

/// Summary of the median of ratios (mor) normalization with normed data and determined correctionfactors.
type MorResult = {
CorrFactors : seq<float>
NormedData : Matrix<float>
} with static member Create cf nd = {CorrFactors=cf;NormedData=nd}

/// As used by Deseq2, see: https://github.com/hbctraining/DGE_workshop/blob/master/lessons/02_DGE_count_normalization.md
///
/// Rows are genes, columns are samples
Expand All @@ -33,11 +39,12 @@ module Normalization =
)
|> Matrix.ofRows
|> Matrix.mapiCols (fun _ v -> Vector.median v)
printfn "%A" sampleWiseCorrectionFactors
data
|> Matrix.mapi (fun r c v ->
v / sampleWiseCorrectionFactors.[c]
)
let normedData =
data
|> Matrix.mapi (fun r c v ->
v / sampleWiseCorrectionFactors.[c]
)
MorResult.Create sampleWiseCorrectionFactors normedData

/// As used by Deseq2, see: https://github.com/hbctraining/DGE_workshop/blob/master/lessons/02_DGE_count_normalization.md
///
Expand All @@ -60,11 +67,12 @@ module Normalization =
)
|> Matrix.ofCols
|> Matrix.mapiRows (fun _ v -> Seq.median v)
printfn "%A" sampleWiseCorrectionFactors
data
|> Matrix.mapi (fun r c v ->
v / sampleWiseCorrectionFactors.[r]
)
let normedData =
data
|> Matrix.mapi (fun r c v ->
v / sampleWiseCorrectionFactors.[r]
)
MorResult.Create sampleWiseCorrectionFactors normedData

/// As used by Deseq2, see: https://github.com/hbctraining/DGE_workshop/blob/master/lessons/02_DGE_count_normalization.md
///
Expand Down
7 changes: 4 additions & 3 deletions tests/FSharp.Stats.Tests/Signal.fs
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ let normalizationTests =

let result = Normalization.medianOfRatios table

TestExtensions.sequenceEqual 4 result expectedNormalizedTable "Value was not normalized correctly"
TestExtensions.sequenceEqual 4 result.NormedData expectedNormalizedTable "Value was not normalized correctly"

testCase "MedianOfRatiosIgnoreNans" <| fun() ->

let result = Normalization.medianOfRatiosBy (fun x -> if System.Double.IsNaN x then 0.1 else x) tableWithNan

Expect.hasCountOf result 2u System.Double.IsNaN "Only initial nan values should be nans afterwards"
Expect.hasCountOf result.NormedData 2u System.Double.IsNaN "Only initial nan values should be nans afterwards"

testCase "MedianOfRatioWides" <| fun() ->

Expand All @@ -134,7 +134,8 @@ let normalizationTests =
table
|> Matrix.transpose
|> Normalization.medianOfRatios
|> fun x -> x.NormedData
|> Matrix.transpose
TestExtensions.sequenceEqual 4 result expected "Wide method should return the same result as the non wide method on a transposed matrix"
TestExtensions.sequenceEqual 4 result.NormedData expected "Wide method should return the same result as the non wide method on a transposed matrix"
]

0 comments on commit 278bd95

Please sign in to comment.