Skip to content

Commit

Permalink
add deseq2 medianOfRatios normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Mar 29, 2021
1 parent 613404b commit 8c82d85
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/FSharp.Stats/Signal/Normalization.fs
Expand Up @@ -17,3 +17,20 @@ module Normalization =
let std = Seq.stDev yVal
yVal |> Vector.map (fun x -> (x - yMean) / std)

/// 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
let medianOfRatios (data:Matrix<float>) =
let sampleWiseCorrectionFactors =
data
|> Matrix.mapiRows (fun _ v ->
let geometricMean = Seq.meanGeometric v
Seq.map (fun s -> s / geometricMean) v
)
|> matrix
|> Matrix.mapiCols (fun _ v -> Vector.median v)
|> vector
data
|> Matrix.mapi (fun r c v ->
v / sampleWiseCorrectionFactors.[c]
)

0 comments on commit 8c82d85

Please sign in to comment.