量化免疫浸潤(rùn)時(shí)CIBERSORT的注意事項(xiàng)

CIBERSORT的注意事項(xiàng)

CIBERSORT的使用呛凶,我們舉了幾個(gè)例子彪杉,但是還有一些疑問谣光,只要有疑問在冤议,就感覺心里不踏實(shí)野瘦。
比如:
1.CIBERSORT需要什么樣的數(shù)據(jù)腾节?
是芯片數(shù)據(jù)還是測(cè)序數(shù)據(jù)洁桌,是什么格式的,需要取log么氧敢?
2.CIBERSORT輸出的數(shù)據(jù)是表示什么熬芜?
在進(jìn)過一系列運(yùn)算后我們會(huì)得到一個(gè)矩陣

image

行是樣本,總共25列福稳,前面22列代表22個(gè)免疫細(xì)胞。那么表格內(nèi)的數(shù)值是什么意思瑞侮?
3.上面的表格中的圆,最后還有三列

image

這里的p值,相關(guān)性系數(shù)怎么算出來(lái)的半火?RMSE是什么霸铰琛?

帶著這些問題钮糖,我們就探索一下吧梅掠。

CIBERSORT究竟在干什么酌住?

我們能夠?qū)IBERSORT進(jìn)行探索,在于他除了有網(wǎng)頁(yè)版本之外阎抒,還提供了一個(gè)腳本酪我。
這個(gè)腳本在登陸官網(wǎng)后可以申請(qǐng)獲得。
我們先來(lái)展示一下且叁,在R語(yǔ)言里面如何運(yùn)行CIBERSORT
你需要三個(gè)東西:
第一都哭,這個(gè)腳本。
第二逞带,基因特征文本
第三欺矫,表達(dá)量矩陣。

前兩個(gè)都可以在官網(wǎng)下載展氓。
最后一個(gè)是我們自己的數(shù)據(jù)穆趴,行是基因,列是樣本遇汞。

image

然后運(yùn)行就可以了

<pre class="prettyprint linenums prettyprinted" style="border: 0px !important; font-family: "courier 10 pitch", Courier, monospace; font-size: inherit; font-style: normal; font-weight: normal; margin: 0px 0px 1.6em; outline: 0px; padding: 1.5em 1.5em 1.5em 50px; vertical-align: baseline; overflow-wrap: break-word; font-variant: normal; font-stretch: normal; line-height: inherit; position: relative; background: rgb(47, 54, 64); box-sizing: border-box; white-space: revert; border-radius: 3px; max-width: 100%; overflow: auto hidden; width: 728px; color: inherit;">

  1. ## 加載函數(shù)
  2. source("CIBERSORT/CIBERSORT.R")
  3. ## 指定基因特征集
  4. sig_matrix <- "CIBERSORT/LM22.txt"
  5. ## 指定表達(dá)矩陣
  6. mixture_file = 'exprMat.txt'
  7. ## 運(yùn)行
  8. res_cibersort <- CIBERSORT(sig_matrix, mixture_file, perm=10, QN=TRUE)

</pre>

其中nperm給的是置換的次數(shù)未妹,QN如果是芯片設(shè)置為T,如果是測(cè)序就設(shè)置為F勺疼,測(cè)序數(shù)據(jù)最好是TPM
因?yàn)槲覍?duì)這個(gè)小函數(shù)進(jìn)行了一點(diǎn)改造教寂,所以會(huì)展示計(jì)算過程,就是緩解一些等待的焦慮

image

最后就得到了我們之前說的結(jié)果执庐。就這么簡(jiǎn)單酪耕。
當(dāng)我們運(yùn)行第一句的時(shí)候

<pre class="prettyprint linenums prettyprinted" style="border: 0px !important; font-family: "courier 10 pitch", Courier, monospace; font-size: inherit; font-style: normal; font-weight: normal; margin: 0px 0px 1.6em; outline: 0px; padding: 1.5em 1.5em 1.5em 50px; vertical-align: baseline; overflow-wrap: break-word; font-variant: normal; font-stretch: normal; line-height: inherit; position: relative; background: rgb(47, 54, 64); box-sizing: border-box; white-space: revert; border-radius: 3px; max-width: 100%; overflow: auto hidden; width: 728px; color: inherit;">

  1. source("CIBERSORT/CIBERSORT.R")

</pre>

環(huán)境中出現(xiàn)了三個(gè)函數(shù)

image

CIBERSORT是主函數(shù),CoreAlg用來(lái)進(jìn)行SVM支持向量機(jī)分析轨淌,doPerm置換檢驗(yàn)迂烁,產(chǎn)生背景r值。

我們可以打開CIBERSORT函數(shù)看看

<pre class="prettyprint linenums prettyprinted" style="border: 0px !important; font-family: "courier 10 pitch", Courier, monospace; font-size: inherit; font-style: normal; font-weight: normal; margin: 0px 0px 1.6em; outline: 0px; padding: 1.5em 1.5em 1.5em 50px; vertical-align: baseline; overflow-wrap: break-word; font-variant: normal; font-stretch: normal; line-height: inherit; position: relative; background: rgb(47, 54, 64); box-sizing: border-box; white-space: revert; border-radius: 3px; max-width: 100%; overflow: auto hidden; width: 728px; color: inherit;">

  1. CIBERSORT <- function(sig_matrix, mixture_file, perm=0, QN=TRUE){
  2. print("Modified by Guozi")
  3. print(paste0("Started at:",Sys.time()))
  4. #read in data
  5. X <- read.table(sig_matrix,header=T,sep="t",row.names=1,check.names=F)
  6. Y <- read.table(mixture_file, header=T, sep="t", check.names=F)
  7. Y <- Y[!duplicated(Y[,1]),]
  8. rownames(Y)<-Y[,1]
  9. Y<-Y[,-1]
  10. X <- data.matrix(X)
  11. Y <- data.matrix(Y)

</pre>

order

X <- X[order(rownames(X)),]
Y <- Y[order(rownames(Y)),]

P <- perm #number of permutations

anti-log if max < 50 in mixture file

if(max(Y) < 50) {Y <- 2^Y}

quantile normalization of mixture file

if(QN == TRUE){
tmpc <- colnames(Y)
tmpr <- rownames(Y)
Y <- preprocessCore::normalize.quantiles(Y)
colnames(Y) <- tmpc
rownames(Y) <- tmpr
}

intersect genes

Xgns <- row.names(X)
Ygns <- row.names(Y)
YintX <- Ygns %in% Xgns
Y <- Y[YintX,]
XintY <- Xgns %in% row.names(Y)
X <- X[XintY,]

standardize sig matrix

X <- (X – mean(X)) / sd(as.vector(X))

矩陣X是LM22矩陣

矩陣Y是待預(yù)測(cè)的矩陣

然后對(duì)

empirical null distribution of correlation coefficients

if(P > 0) {nulldist <- sort(doPerm(P, X, Y)$dist)}

print(nulldist)

header <- c(‘Mixture’,colnames(X),“P-value”,“Correlation”,“RMSE”)

print(header)

output <- matrix()
itor <- 1
mixtures <- dim(Y)[2]
pval <- 9999

iterate through mixtures

print(paste0(length(colnames(Y)),” Samples in total”))
while(itor <= mixtures){

print(paste0(“Dealing with sample:”,itor))
y <- Y[,itor]

standardize mixture

y <- (y – mean(y)) / sd(y)

run SVR core algorithm

result <- CoreAlg(X, y)

get results

w <- resultw mix_r <- resultmix_r
mix_rmse <- result$mix_rmse

calculate p-value

if(P > 0) {pval <- 1 – (which.min(abs(nulldist – mix_r)) / length(nulldist))}

print output

out <- c(colnames(Y)[itor],w,pval,mix_r,mix_rmse)
if(itor == 1) {output <- out}
else {output <- rbind(output, out)}

itor <- itor + 1

}

save results

write.table(rbind(header,output), file=“CIBERSORT-Results.txt”, sep=“t”, row.names=F, col.names=F, quote=F)

return matrix object containing all results

obj <- rbind(header,output)
obj <- obj[,-1]
obj <- obj[-1,]
obj <- matrix(as.numeric(unlist(obj)),nrow=nrow(obj))
rownames(obj) <- colnames(Y)
colnames(obj) <- c(colnames(X),“P-value”,“Correlation”,“RMSE”)
print(paste0(“Finished at:”,Sys.time()))
obj
}

耐心一點(diǎn)的可以一句一句運(yùn)行探索這個(gè)函數(shù)
這個(gè)事情递鹉,我們常常做盟步。

<pre class="prettyprint linenums prettyprinted" style="border: 0px !important; font-family: "courier 10 pitch", Courier, monospace; font-size: inherit; font-style: normal; font-weight: normal; margin: 0px 0px 1.6em; outline: 0px; padding: 1.5em 1.5em 1.5em 50px; vertical-align: baseline; overflow-wrap: break-word; font-variant: normal; font-stretch: normal; line-height: inherit; position: relative; background: rgb(47, 54, 64); box-sizing: border-box; white-space: revert; border-radius: 3px; max-width: 100%; overflow: auto hidden; width: 728px; color: inherit;">

  1. sig_matrix <- "CIBERSORT/LM22.txt"

  2. mixture_file = 'exprMat.txt'

  3. #read in data

  4. ## 讀入signature數(shù)據(jù)

  5. X <- read.table(sig_matrix,header=T,sep="t",row.names=1,check.names=F)

  6. ## 讀入表達(dá)量數(shù)據(jù)

  7. Y <- read.table(mixture_file, header=T, sep="t", check.names=F)

  8. ## 去除重復(fù)基因

  9. Y <- Y[!duplicated(Y[,1]),]

  10. ## 第一列轉(zhuǎn)行名

  11. rownames(Y)<-Y[,1]

  12. Y<-Y[,-1]

  13. ## 數(shù)據(jù)框轉(zhuǎn)為矩陣

  14. X <- data.matrix(X)

  15. Y <- data.matrix(Y)

  16. #order

  17. ## 行名排序

  18. X <- X[order(rownames(X)),]

  19. Y <- Y[order(rownames(Y)),]

</pre>

這些操作就是分別讀入表達(dá)矩陣和基因特征,最終產(chǎn)生基因數(shù)目一樣的兩個(gè)矩陣躏结。
X現(xiàn)在是這樣的

image

Y是這樣的

image

接下來(lái)就是設(shè)置一下置換次數(shù)

<pre class="prettyprint linenums prettyprinted" style="border: 0px !important; font-family: "courier 10 pitch", Courier, monospace; font-size: inherit; font-style: normal; font-weight: normal; margin: 0px 0px 1.6em; outline: 0px; padding: 1.5em 1.5em 1.5em 50px; vertical-align: baseline; overflow-wrap: break-word; font-variant: normal; font-stretch: normal; line-height: inherit; position: relative; background: rgb(47, 54, 64); box-sizing: border-box; white-space: revert; border-radius: 3px; max-width: 100%; overflow: auto hidden; width: 728px; color: inherit;">

  1. ## 設(shè)置置換次數(shù)
  2. ## #number of permutations
  3. perm <- 1000
  4. P <- perm

</pre>

下面這一步很重要却盘,這個(gè)函數(shù)會(huì)自動(dòng)檢測(cè)你的數(shù)據(jù)。
如果你的數(shù)據(jù)被log話之后媳拴,他會(huì)自動(dòng)幫你去掉log,
所以黄橘,CIBERSORT接受的數(shù)據(jù)是不去掉log的,如果你一不小心取了log屈溉,也不要害怕塞关,他會(huì)幫你再去掉。
當(dāng)然檢測(cè)數(shù)據(jù)是否被log化子巾,我們有更好的方法帆赢,GEO已經(jīng)提供了小压。
來(lái)完成你的生信作業(yè),這是最有誠(chéng)意的GEO數(shù)據(jù)庫(kù)教程

<pre class="prettyprint linenums prettyprinted" style="border: 0px !important; font-family: "courier 10 pitch", Courier, monospace; font-size: inherit; font-style: normal; font-weight: normal; margin: 0px 0px 1.6em; outline: 0px; padding: 1.5em 1.5em 1.5em 50px; vertical-align: baseline; overflow-wrap: break-word; font-variant: normal; font-stretch: normal; line-height: inherit; position: relative; background: rgb(47, 54, 64); box-sizing: border-box; white-space: revert; border-radius: 3px; max-width: 100%; overflow: auto hidden; width: 728px; color: inherit;">

  1. #anti-log if max < 50 in mixture file
  2. ## 這里說明椰于,當(dāng)他檢測(cè)數(shù)據(jù)已經(jīng)被log化之后怠益,就會(huì)幫我們?nèi)og化
  3. if(max(Y) < 50) {Y <- 2^Y}

</pre>

為什么要這么做,因?yàn)樗且曰蚣癁橐罁?jù)來(lái)推斷廉羔,我們看看基因特征的表達(dá)量發(fā)現(xiàn)溉痢。
這些數(shù)據(jù)有的很大,可以看出是沒有取過log的憋他。

image

接下里要對(duì)數(shù)據(jù)進(jìn)行quantile 矯正
理解 Quntile Normalization

使用的是這個(gè)函數(shù)preprocessCore::normalize.quantile

<pre class="prettyprint linenums prettyprinted" style="border: 0px !important; font-family: "courier 10 pitch", Courier, monospace; font-size: inherit; font-style: normal; font-weight: normal; margin: 0px 0px 1.6em; outline: 0px; padding: 1.5em 1.5em 1.5em 50px; vertical-align: baseline; overflow-wrap: break-word; font-variant: normal; font-stretch: normal; line-height: inherit; position: relative; background: rgb(47, 54, 64); box-sizing: border-box; white-space: revert; border-radius: 3px; max-width: 100%; overflow: auto hidden; width: 728px; color: inherit;">

  1. QN=T
  2. if(QN == TRUE){
  3. tmpc <- colnames(Y)
  4. tmpr <- rownames(Y)
  5. Y <- preprocessCore::normalize.quantiles(Y)
  6. colnames(Y) <- tmpc
  7. rownames(Y) <- tmpr
  8. }

</pre>

需要強(qiáng)調(diào)的是孩饼,只有芯片是需要這樣做的,這跟芯片產(chǎn)生的過程有關(guān)竹挡。
而RNAseq數(shù)據(jù)镀娶,論文中推薦的是TPM

然后把兩個(gè)數(shù)據(jù)的基因取交集,然后以交集的基因再去調(diào)整兩個(gè)數(shù)據(jù)
用到的函數(shù)是%in%

<pre class="prettyprint linenums prettyprinted" style="border: 0px !important; font-family: "courier 10 pitch", Courier, monospace; font-size: inherit; font-style: normal; font-weight: normal; margin: 0px 0px 1.6em; outline: 0px; padding: 1.5em 1.5em 1.5em 50px; vertical-align: baseline; overflow-wrap: break-word; font-variant: normal; font-stretch: normal; line-height: inherit; position: relative; background: rgb(47, 54, 64); box-sizing: border-box; white-space: revert; border-radius: 3px; max-width: 100%; overflow: auto hidden; width: 728px; color: inherit;">

  1. Xgns <- row.names(X)
  2. Ygns <- row.names(Y)
  3. YintX <- Ygns %in% Xgns
  4. Y <- Y[YintX,]
  5. XintY <- Xgns %in% row.names(Y)
  6. X <- X[XintY,]

</pre>

現(xiàn)在X和Y的行都是517行

image

接下來(lái)他們對(duì)基因特征的數(shù)據(jù)進(jìn)行了z-score轉(zhuǎn)換
z-score的標(biāo)準(zhǔn)化究竟怎么弄揪罕?

<pre class="prettyprint linenums prettyprinted" style="border: 0px !important; font-family: "courier 10 pitch", Courier, monospace; font-size: inherit; font-style: normal; font-weight: normal; margin: 0px 0px 1.6em; outline: 0px; padding: 1.5em 1.5em 1.5em 50px; vertical-align: baseline; overflow-wrap: break-word; font-variant: normal; font-stretch: normal; line-height: inherit; position: relative; background: rgb(47, 54, 64); box-sizing: border-box; white-space: revert; border-radius: 3px; max-width: 100%; overflow: auto hidden; width: 728px; color: inherit;">

  1. X <- (X - mean(X)) / sd(as.vector(X))

</pre>

好了準(zhǔn)備工作做完了梯码,現(xiàn)在正式開始

核心函數(shù)CoreAlgd

這個(gè)函數(shù)是主要計(jì)算的函數(shù),他接受基因集以及一個(gè)樣本的數(shù)據(jù)好啰,返回一個(gè)列表

<pre class="prettyprint linenums prettyprinted" style="border: 0px !important; font-family: "courier 10 pitch", Courier, monospace; font-size: inherit; font-style: normal; font-weight: normal; margin: 0px 0px 1.6em; outline: 0px; padding: 1.5em 1.5em 1.5em 50px; vertical-align: baseline; overflow-wrap: break-word; font-variant: normal; font-stretch: normal; line-height: inherit; position: relative; background: rgb(47, 54, 64); box-sizing: border-box; white-space: revert; border-radius: 3px; max-width: 100%; overflow: auto hidden; width: 728px; color: inherit;">

  1. CoreAlg <- function(X, y){

</pre>

try different values of nu

svn_itor <- 3

res <- function(i){
if(i==1){nus <- 0.25}
if(i==2){nus <- 0.5}
if(i==3){nus <- 0.75}
model<-e1071::svm(X,y,type=“nu-regression”,kernel=“l(fā)inear”,nu=nus,scale=F)
model
}

if(Sys.info()[‘sysname’] == ‘Windows’) out <- parallel::mclapply(1:svn_itor, res, mc.cores=1) else
out <- parallel::mclapply(1:svn_itor, res, mc.cores=svn_itor)

nusvm <- rep(0,svn_itor)
corrv <- rep(0,svn_itor)

do cibersort

t <- 1
while(t <= svn_itor) {
weights = t(out[[t]]coefs) %*% out[[t]]SV
weights[which(weights<0)]<-0
w<-weights/sum(weights)
u <- sweep(X,MARGIN=2,w,‘*’)
k <- apply(u, 1, sum)
nusvm[t] <- sqrt((mean((k – y)^2)))
corrv[t] <- cor(k, y)
t <- t + 1
}

pick best model

rmses <- nusvm
mn <- which.min(rmses)
model <- out[[mn]]

get and normalize coefficients

q <- t(modelcoefs) %*% modelSV
q[which(q<0)]<-0
w <- (q/sum(q))

mix_rmse <- rmses[mn]
mix_r <- corrv[mn]

newList <- list(“w” = w, “mix_rmse” = mix_rmse, “mix_r” = mix_r)

}

我們可以測(cè)試這個(gè)函數(shù)的結(jié)果

<pre class="prettyprint linenums prettyprinted" style="border: 0px !important; font-family: "courier 10 pitch", Courier, monospace; font-size: inherit; font-style: normal; font-weight: normal; margin: 0px 0px 1.6em; outline: 0px; padding: 1.5em 1.5em 1.5em 50px; vertical-align: baseline; overflow-wrap: break-word; font-variant: normal; font-stretch: normal; line-height: inherit; position: relative; background: rgb(47, 54, 64); box-sizing: border-box; white-space: revert; border-radius: 3px; max-width: 100%; overflow: auto hidden; width: 728px; color: inherit;">

  1. ## 提取一個(gè)樣本
  2. y <- Y[,1]
  3. y <- (y - mean(y)) / sd(y)
  4. ## 計(jì)算
  5. result <- CoreAlg(X, y)
  6. str(result)

</pre>

image

返回的結(jié)果記錄了三個(gè)結(jié)果轩娶,我們可以通過再次探索CoreAlg來(lái)確定那是三個(gè)返回值
w,mix_rmse,mix_r

這一段框往,我寫不下去了鳄抒,會(huì)錄制成視頻更新在答疑群中。
簡(jiǎn)單來(lái)說

image

w實(shí)際上就是就是個(gè)人除以總數(shù)椰弊,體現(xiàn)在最后的22個(gè)免疫細(xì)胞的值是個(gè)率许溅,加起來(lái)等于1
RMSE就是均方根誤差(Root Mean Squared Error),越小效果越好秉版,解釋看這個(gè)帖子
花了100個(gè)小時(shí)學(xué)習(xí)線性回歸贤重,寫了個(gè)萬(wàn)字長(zhǎng)文作總結(jié)。
而相關(guān)性是這樣算的清焕。
首先這個(gè)樣本是有每個(gè)基因的表達(dá)量的并蝗。
然后算出來(lái)每個(gè)免疫細(xì)胞的占比后,用這個(gè)占比去把基因矩陣中的每個(gè)值都按照這個(gè)比值處理了

<pre class="prettyprint linenums prettyprinted" style="border: 0px !important; font-family: "courier 10 pitch", Courier, monospace; font-size: inherit; font-style: normal; font-weight: normal; margin: 0px 0px 1.6em; outline: 0px; padding: 1.5em 1.5em 1.5em 50px; vertical-align: baseline; overflow-wrap: break-word; font-variant: normal; font-stretch: normal; line-height: inherit; position: relative; background: rgb(47, 54, 64); box-sizing: border-box; white-space: revert; border-radius: 3px; max-width: 100%; overflow: auto hidden; width: 728px; color: inherit;">

  1. u <- sweep(X,MARGIN=2,w,'*')

</pre>

接下來(lái)秸妥,算出預(yù)測(cè)的單個(gè)樣本中每個(gè)基因的表達(dá)量

<pre class="prettyprint linenums prettyprinted" style="border: 0px !important; font-family: "courier 10 pitch", Courier, monospace; font-size: inherit; font-style: normal; font-weight: normal; margin: 0px 0px 1.6em; outline: 0px; padding: 1.5em 1.5em 1.5em 50px; vertical-align: baseline; overflow-wrap: break-word; font-variant: normal; font-stretch: normal; line-height: inherit; position: relative; background: rgb(47, 54, 64); box-sizing: border-box; white-space: revert; border-radius: 3px; max-width: 100%; overflow: auto hidden; width: 728px; color: inherit;">

  1. k <- apply(u, 1, sum)

</pre>

這個(gè)表達(dá)量k和本身的表達(dá)量y求相關(guān)性得到相關(guān)性系數(shù)借卧。

置換函數(shù)doPerm

那么現(xiàn)在只剩下p值了,p值是怎么算的呢筛峭?
使用置換檢驗(yàn)。
就是用蒙特卡羅方法陪每,不斷地從原來(lái)的數(shù)據(jù)中跟單個(gè)樣本等長(zhǎng)的數(shù)據(jù)影晓,模擬新樣本镰吵,進(jìn)行CoreAlg計(jì)算。
每次抽取出相關(guān)性系數(shù)r挂签,當(dāng)次數(shù)足夠多的時(shí)候疤祭,就形成背景相關(guān)性系數(shù)分布。
我做了1000次后是這樣的饵婆。

image

這個(gè)計(jì)算由doPerm函數(shù)完成勺馆,這個(gè)方法未來(lái)在GSEA課程的時(shí)候還會(huì)碰到。

<pre class="prettyprint linenums prettyprinted" style="border: 0px !important; font-family: "courier 10 pitch", Courier, monospace; font-size: inherit; font-style: normal; font-weight: normal; margin: 0px 0px 1.6em; outline: 0px; padding: 1.5em 1.5em 1.5em 50px; vertical-align: baseline; overflow-wrap: break-word; font-variant: normal; font-stretch: normal; line-height: inherit; position: relative; background: rgb(47, 54, 64); box-sizing: border-box; white-space: revert; border-radius: 3px; max-width: 100%; overflow: auto hidden; width: 728px; color: inherit;">

  1. doPerm <- function(perm, X, Y){
  2. itor <- 1
  3. Ylist <- as.list(data.matrix(Y))
  4. dist <- matrix()
  5. print(paste0("do permutations:",perm," in total"))
  6. while(itor <= perm){
  7. print(paste0("nperm:",itor))
  8. #random mixture
  9. yr <- as.numeric(Ylist[sample(length(Ylist),dim(X)[1])])
  10. #standardize mixture
  11. yr <- (yr - mean(yr)) / sd(yr)
  12. #run CIBERSORT core algorithm
  13. result <- CoreAlg(X, yr)
  14. mix_r <- result$mix_r
  15. #store correlation
  16. if(itor == 1) {dist <- mix_r}
  17. else {dist <- rbind(dist, mix_r)}
  18. itor <- itor + 1
  19. }
  20. newList <- list("dist" = dist)
  21. }

</pre>

這個(gè)函數(shù)做的工作剛才已經(jīng)講了侨核,很簡(jiǎn)單草穆。
就是通過sample命令不斷產(chǎn)生模擬樣本,進(jìn)行CoreAlg計(jì)算抽取r值搓译。

<pre class="prettyprint linenums prettyprinted" style="border: 0px !important; font-family: "courier 10 pitch", Courier, monospace; font-size: inherit; font-style: normal; font-weight: normal; margin: 0px 0px 1.6em; outline: 0px; padding: 1.5em 1.5em 1.5em 50px; vertical-align: baseline; overflow-wrap: break-word; font-variant: normal; font-stretch: normal; line-height: inherit; position: relative; background: rgb(47, 54, 64); box-sizing: border-box; white-space: revert; border-radius: 3px; max-width: 100%; overflow: auto hidden; width: 728px; color: inherit;">

  1. yr <- as.numeric(Ylist[sample(length(Ylist),dim(X)[1])])

</pre>

有了背景r分布悲柱,每次也能產(chǎn)生各個(gè)細(xì)胞的比率,有均方根誤差RMSE些己,有相關(guān)性系數(shù)豌鸡,還有p值,
這樣最終結(jié)果的一行就有了段标。
那么只要批量對(duì)每一個(gè)樣本計(jì)算即可得到所有結(jié)果
接下來(lái)的CIBERSORT主函數(shù)就用這句命令涯冠,計(jì)算的p值

<pre class="prettyprint linenums prettyprinted" style="border: 0px !important; font-family: "courier 10 pitch", Courier, monospace; font-size: inherit; font-style: normal; font-weight: normal; margin: 0px 0px 1.6em; outline: 0px; padding: 1.5em 1.5em 1.5em 50px; vertical-align: baseline; overflow-wrap: break-word; font-variant: normal; font-stretch: normal; line-height: inherit; position: relative; background: rgb(47, 54, 64); box-sizing: border-box; white-space: revert; border-radius: 3px; max-width: 100%; overflow: auto hidden; width: 728px; color: inherit;">

  1. if(P > 0) {pval <- 1 - (which.min(abs(nulldist - mix_r)) / length(nulldist))}

</pre>

我強(qiáng)行解釋一波,每次真正的樣本計(jì)算出來(lái)的r值逼庞,找到其在背景分布的位置蛇更,然后看右邊紅色的概率有多大。如果小于0.05往堡,說明產(chǎn)生這種極端值并不是偶然的械荷。
那么該樣本的反卷積解析就是成功的,可信的虑灰。

這樣批量每個(gè)樣本后吨瞎,就返回了我們想要的結(jié)果,以obj代替穆咐。

image

如果樣本多颤诀,我們還能可以先把p值小的樣本刪掉。
既然每個(gè)細(xì)胞的值是個(gè)比率对湃,加起來(lái)等于1崖叫,那么我們就可以畫圖來(lái)看了。
畫圖用到的技能
我喜歡的gather快要被淘汰了拍柒,迎面走來(lái)了更好用的寬長(zhǎng)轉(zhuǎn)換工具

<pre class="prettyprint linenums prettyprinted" style="border: 0px !important; font-family: "courier 10 pitch", Courier, monospace; font-size: inherit; font-style: normal; font-weight: normal; margin: 0px 0px 1.6em; outline: 0px; padding: 1.5em 1.5em 1.5em 50px; vertical-align: baseline; overflow-wrap: break-word; font-variant: normal; font-stretch: normal; line-height: inherit; position: relative; background: rgb(47, 54, 64); box-sizing: border-box; white-space: revert; border-radius: 3px; max-width: 100%; overflow: auto hidden; width: 728px; color: inherit;">

  1. library(dplyr)
  2. library(tidyr)
  3. library(tibble)
  4. dd1 <- obj %>%
  5. as.data.frame() %>%
  6. rownames_to_column("sample") %>%
  7. pivot_longer(cols=2:23,
  8. names_to= "celltype",
  9. values_to = "Proportion")

</pre>

現(xiàn)在的數(shù)據(jù)是這樣的

image

有了數(shù)據(jù)畫圖就很容易

<pre class="prettyprint linenums prettyprinted" style="border: 0px !important; font-family: "courier 10 pitch", Courier, monospace; font-size: inherit; font-style: normal; font-weight: normal; margin: 0px 0px 1.6em; outline: 0px; padding: 1.5em 1.5em 1.5em 50px; vertical-align: baseline; overflow-wrap: break-word; font-variant: normal; font-stretch: normal; line-height: inherit; position: relative; background: rgb(47, 54, 64); box-sizing: border-box; white-space: revert; border-radius: 3px; max-width: 100%; overflow: auto hidden; width: 728px; color: inherit;">

  1. library(ggplot2)
  2. ggplot(dd1,aes(sample,Proportion,fill = celltype)) +
  3. geom_bar(position = "stack",stat = "identity")+
  4. theme_bw()+
  5. guides(fill=guide_legend(ncol=1))

</pre>

image

看縱坐標(biāo)加起來(lái)就是1心傀。
但是ssGSEA的數(shù)據(jù),最后是富集分?jǐn)?shù)拆讯,畫這種圖是不合適的脂男。這個(gè)會(huì)在之后的帖子講解养叛。

現(xiàn)在CIBERSORT的輸入輸出都知道了。用起來(lái)會(huì)很有底氣宰翅。

還能怎么改進(jìn)呢弃甥?
置換那里速度太慢了,設(shè)置成1000次汁讼,就跟多了1000個(gè)樣本一樣淆攻,我們可以設(shè)置并行化加速。

要補(bǔ)充一點(diǎn)的是嘿架,如果研究的TCGA數(shù)據(jù)瓶珊,其實(shí)大部分軟件都把結(jié)果計(jì)算好了。比如CIBERSORT計(jì)算的 TCGA結(jié)果在這里
https://gdc.cancer.gov/about-data/publications/panimmune

image

直接去下載讀入R語(yǔ)言就可以用了眶明。
那么講這么長(zhǎng)有什么用呢艰毒?
是這樣的:
這個(gè)函數(shù)使用起來(lái)這么簡(jiǎn)單,只要我們替換掉基因特征數(shù)據(jù)搜囱,就可以遷移到其他物種丑瞧,
比如,我們想要量化小鼠的組織蜀肘,那怎么辦呢绊汹,只要找到小鼠免疫細(xì)胞的這個(gè)特征集就行了。
還好扮宠,這個(gè)事情西乖,不需要我們做,有人已經(jīng)做好了坛增,下次我們就講這個(gè)获雕。
量化免疫浸潤(rùn)時(shí)CIBERSORT的注意事項(xiàng)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市收捣,隨后出現(xiàn)的幾起案子届案,更是在濱河造成了極大的恐慌,老刑警劉巖罢艾,帶你破解...
    沈念sama閱讀 218,122評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件楣颠,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡咐蚯,警方通過查閱死者的電腦和手機(jī)童漩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)春锋,“玉大人矫膨,你說我怎么就攤上這事。” “怎么了豆拨?”我有些...
    開封第一講書人閱讀 164,491評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵直奋,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我施禾,道長(zhǎng),這世上最難降的妖魔是什么搁胆? 我笑而不...
    開封第一講書人閱讀 58,636評(píng)論 1 293
  • 正文 為了忘掉前任弥搞,我火速辦了婚禮,結(jié)果婚禮上渠旁,老公的妹妹穿的比我還像新娘攀例。我一直安慰自己,他們只是感情好顾腊,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,676評(píng)論 6 392
  • 文/花漫 我一把揭開白布粤铭。 她就那樣靜靜地躺著,像睡著了一般杂靶。 火紅的嫁衣襯著肌膚如雪梆惯。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,541評(píng)論 1 305
  • 那天吗垮,我揣著相機(jī)與錄音垛吗,去河邊找鬼。 笑死烁登,一個(gè)胖子當(dāng)著我的面吹牛怯屉,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播饵沧,決...
    沈念sama閱讀 40,292評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼锨络,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了狼牺?” 一聲冷哼從身側(cè)響起羡儿,我...
    開封第一講書人閱讀 39,211評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎锁右,沒想到半個(gè)月后失受,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,655評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡咏瑟,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,846評(píng)論 3 336
  • 正文 我和宋清朗相戀三年拂到,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片码泞。...
    茶點(diǎn)故事閱讀 39,965評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡兄旬,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情领铐,我是刑警寧澤悯森,帶...
    沈念sama閱讀 35,684評(píng)論 5 347
  • 正文 年R本政府宣布,位于F島的核電站绪撵,受9級(jí)特大地震影響瓢姻,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜音诈,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,295評(píng)論 3 329
  • 文/蒙蒙 一幻碱、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧细溅,春花似錦褥傍、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至誓篱,卻和暖如春朋贬,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背燕鸽。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工兄世, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人啊研。 一個(gè)月前我還...
    沈念sama閱讀 48,126評(píng)論 3 370
  • 正文 我出身青樓御滩,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親党远。 傳聞我的和親對(duì)象是個(gè)殘疾皇子削解,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,914評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容