DESeq2中的标准化方法---vst

首先,vst也是基于负二项分布的一种标准化方法

我们为什么在大样本数据中需要采用vst的标准化方法呢?这是因为:

1.It is a one-size-fits-all solution, ignoring the measurement noise characteristics associated with each instrument and each run.

2.Negative values that frequently result from background correction of low-intensity signals have to be reset before taking the logarithm, and thus they are artificially truncated.

3.logarithmic transformation inflates variances when the intensities are close to zero although it stabilizes the variances at higher intensities

4.A 2-fold difference can be very significant when the intensities are high; however, when the intensities are close to the background level a 2-fold difference can be within the expected measurement error.

总结起来就是rlog标准化方法对于大样本来说运算较慢,并且对于count的值比较敏感,因此引入vst的标准化来得到一个近似为同方差的值矩阵

在Biostars上也给出了rlog与vst之间的差别:

This function calculates a variance stabilizing transformation (VST) from the fitted dispersion-mean relation(s) and then transforms the count data (normalized by division by the size factors or normalization factors), yielding a matrix of values which are now approximately homoskedastic (having constant variance along the range of mean values). The transformation also normalizes with respect to library size. The rlog is less sensitive to size factors, which can be an issue when size factors vary widely. These transformations are useful when checking for outliers or as input for machine learning techniques such as clustering or linear discriminant analysis.

variance-stabilizing transformation

方差稳定变换是2014年提出的一种标准化方式(发表于NAR),具体可参见:Model-based variance-stabilizing transformation for Illumina microarray data

这篇文章主要介绍了vst标准化的model:

1.首先,该model对于每一个基因计算在各个样本中的均值以及方差

image

image

上式是对每一个基因按不同的sample分别计算均值和方差

2.估计均值与方差之间的函数关系

image

image

其中 v 代表方差,u 代表均值;而 v(u) 代表的是方差 v 是关于均值 u 的函数

那么根据数学推导,可以的到h(y)函数为:

image

image

这个公式3是根据渐进理论(delta method)以及公式2推算而来的,经过转换后可以得到一个各个基因间方差近似相等的矩阵(因为转换的过程事实上是进一步压缩,所以各个基因间方差的差异被缩小)

image

image

而delta method的作用是利用经过h(y)变化前的data分布来拟合经过h(y)变化后的data分布

由泰勒公式知,y1和y2分别表示两个基因的表达量,那么转换前的差异为y1-y2,转换后的差异为h(y1)-h(y2),那么由上式得,基因表达量之间的差异经过转换以后可以缩小,h’()相对于缩小,从而达到一个各个基因间方差近似相等的情况

参考:Estimating Transformations for Regression via

Additivity and Variance Stabilization

3.寻找到一个合适的转换函数,将标准化前的矩阵(Y)转换为标准后的矩阵(Y~,波浪线应该打在上面),我们将公式2做一个恒等变形:

image

image

我们发现等式右边是一个线性模型,因此可以利用每个基因的方差和均值拟合线性模型,从而得到c1,c2和c3

4.将c1,c2和c3反带入公式3中,求解积分得:

image

image

这样我们就得到标准化的转换公式h(y)了,即就可以利用h(y)进行标准化了

vst代码详解

DESeq2中,vst的代码如下:

vst <- function(object, blind=TRUE, nsub=1000, fitType="parametric") {
  if (nrow(object) < nsub) {
    stop("less than 'nsub' rows,
  it is recommended to use varianceStabilizingTransformation directly")
  }
  if (is.null(colnames(object))) {
    colnames(object) <- seq_len(ncol(object))
  }
  if (is.matrix(object)) {
    matrixIn <- TRUE
    object <- DESeqDataSetFromMatrix(object, DataFrame(row.names=colnames(object)), ~ 1)
  } else {
    if (blind) {
      design(object) <- ~ 1
    }
    matrixIn <- FALSE
  }
  if (is.null(sizeFactors(object)) & is.null(normalizationFactors(object))) {
    object <- estimateSizeFactors(object)
  }
  baseMean <- rowMeans(counts(object, normalized=TRUE))
  if (sum(baseMean > 5) < nsub) {
    stop("less than 'nsub' rows with mean normalized count > 5, 
  it is recommended to use varianceStabilizingTransformation directly")
  }

  # subset to a specified number of genes with mean normalized count > 5
  object.sub <- object[baseMean > 5,]
  baseMean <- baseMean[baseMean > 5]
  o <- order(baseMean)
  idx <- o[round(seq(from=1, to=length(o), length=nsub))]
  object.sub <- object.sub[idx,]

  # estimate dispersion trend
  object.sub <- estimateDispersionsGeneEst(object.sub, quiet=TRUE)
  object.sub <- estimateDispersionsFit(object.sub, fitType=fitType, quiet=TRUE)

  # assign to the full object
  suppressMessages({dispersionFunction(object) <- dispersionFunction(object.sub)})

  # calculate and apply the VST
  vsd <- varianceStabilizingTransformation(object, blind=FALSE)
  if (matrixIn) {
    return(assay(vsd))
  } else {
    return(vsd)
  }
}

最核心的代码是这一句:

# calculate and apply the VST
  vsd <- varianceStabilizingTransformation(object, blind=FALSE)

varianceStabilizingTransformation的主要功能就是进行vst的标准化,我们不妨看看varianceStabilizingTransformation的内部函数:

varianceStabilizingTransformation <- function (object, blind=TRUE, fitType="parametric") {
  if (is.null(colnames(object))) {
    colnames(object) <- seq_len(ncol(object))
  }
  if (is.matrix(object)) {
    matrixIn <- TRUE
    object <- DESeqDataSetFromMatrix(object, DataFrame(row.names=colnames(object)), ~1)
  } else {
    matrixIn <- FALSE
  }
  if (is.null(sizeFactors(object)) & is.null(normalizationFactors(object))) {
    object <- estimateSizeFactors(object)
  }
  if (blind) {
    design(object) <- ~ 1
  }
  if (blind | is.null(attr(dispersionFunction(object),"fitType"))) {
    object <- estimateDispersionsGeneEst(object, quiet=TRUE)
    object <- estimateDispersionsFit(object, quiet=TRUE, fitType)
  }
  vsd <- getVarianceStabilizedData(object)
  if (matrixIn) {
    return(vsd)
  }
  se <- SummarizedExperiment(
    assays = vsd,
    colData = colData(object),
    rowRanges = rowRanges(object),
    metadata = metadata(object))
  DESeqTransform(se)
}

其中最核心的是

 vsd <- getVarianceStabilizedData(object)

而我们打开getVarianceStabilizedData后发现,其中的核心代码:

ncounts <- counts(object, normalized=TRUE)
sf <- sizeFactors(object)
xg <- sinh( seq( asinh(0), asinh(max(ncounts)), length.out=1000 ) )[-1]
xim <- mean( 1/sf )
baseVarsAtGrid <- dispersionFunction(object)( xg ) * xg^2 + xim * xg
integrand <- 1 / sqrt( baseVarsAtGrid )

splf <- splinefun(asinh( ( xg[-1] + xg[-length(xg)] )/2 ),
      cumsum((xg[-1] - xg[-length(xg)]) * (integrand[-1] + integrand[-length(integrand)] )/2 )
)

h1 <- quantile( rowMeans(ncounts), .95 )
h2 <- quantile( rowMeans(ncounts), .999 )
eta <- ( log2(h2) - log2(h1) ) / ( splf(asinh(h2)) - splf(asinh(h1)))
xi <- log2(h1) - eta * splf(asinh(h1))
tc <- sapply( colnames(counts(object)), function(clm) {
eta * splf(asinh(ncounts[,clm])) + xi
    })
rownames( tc ) <- rownames( counts(object) )
#函数返回值为tc
tc

其中splinefun()为插值样条函数,是数值分析求近似解的一种方法:

#example
x <- c(1,8,14,21,28,35,42,65)
y <- c(65,30,70,150,40,0,15,0)
f <- splinefun(x, y)

z = c(2,9,16,25,39,55)
f(z)

这一段其实就是在做公式4的运算,注意看:

#先拟合好插值样条函数
splf <- splinefun(asinh( ( xg[-1] + xg[-length(xg)] )/2 ),
      cumsum((xg[-1] - xg[-length(xg)]) * (integrand[-1] + integrand[-length(integrand)] )/2 )
)
#计算标准化因子
eta <- ( log2(h2) - log2(h1) ) / ( splf(asinh(h2)) - splf(asinh(h1)) )

#对ncounts的每一列做标准化处理
tc <- sapply( colnames(counts(object)), function(clm) {
eta * splf(asinh(ncounts[,clm])) + xi
    })

这一步主要是用方差平稳转换来标准化,即用asinh(ncounts[,clm])的值作为输入做方差平稳转换,ncount[,clm])是经过文库矫正后的count矩阵(clm代表该矩阵的列),然后乘标准因子,最后返回的结果是经过vst标准化后的矩阵

作者:Seurat_Satija
链接:https://www.jianshu.com/p/c515ef000946
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,527评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,314评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,535评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,006评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,961评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,220评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,664评论 3 392
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,351评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,481评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,397评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,443评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,123评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,713评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,801评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,010评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,494评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,075评论 2 341

推荐阅读更多精彩内容