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è)矩陣
行是樣本,總共25列福稳,前面22列代表22個(gè)免疫細(xì)胞。那么表格內(nèi)的數(shù)值是什么意思瑞侮?
3.上面的表格中的圆,最后還有三列
這里的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ù)穆趴,行是基因,列是樣本遇汞。
然后運(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;">
## 加載函數(shù)
source("CIBERSORT/CIBERSORT.R")
## 指定基因特征集
sig_matrix <- "CIBERSORT/LM22.txt"
## 指定表達(dá)矩陣
mixture_file = 'exprMat.txt'
## 運(yùn)行
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ì)算過程,就是緩解一些等待的焦慮
最后就得到了我們之前說的結(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;">
source("CIBERSORT/CIBERSORT.R")
</pre>
環(huán)境中出現(xiàn)了三個(gè)函數(shù)
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;">
CIBERSORT <- function(sig_matrix, mixture_file, perm=0, QN=TRUE){
print("Modified by Guozi")
print(paste0("Started at:",Sys.time()))
#read in data
X <- read.table(sig_matrix,header=T,sep="t",row.names=1,check.names=F)
Y <- read.table(mixture_file, header=T, sep="t", check.names=F)
Y <- Y[!duplicated(Y[,1]),]
rownames(Y)<-Y[,1]
Y<-Y[,-1]
X <- data.matrix(X)
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 <- 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;">
sig_matrix <- "CIBERSORT/LM22.txt"
mixture_file = 'exprMat.txt'
#read in data
## 讀入signature數(shù)據(jù)
X <- read.table(sig_matrix,header=T,sep="t",row.names=1,check.names=F)
## 讀入表達(dá)量數(shù)據(jù)
Y <- read.table(mixture_file, header=T, sep="t", check.names=F)
## 去除重復(fù)基因
Y <- Y[!duplicated(Y[,1]),]
## 第一列轉(zhuǎn)行名
rownames(Y)<-Y[,1]
Y<-Y[,-1]
## 數(shù)據(jù)框轉(zhuǎn)為矩陣
X <- data.matrix(X)
Y <- data.matrix(Y)
#order
## 行名排序
X <- X[order(rownames(X)),]
Y <- Y[order(rownames(Y)),]
</pre>
這些操作就是分別讀入表達(dá)矩陣和基因特征,最終產(chǎn)生基因數(shù)目一樣的兩個(gè)矩陣躏结。
X現(xiàn)在是這樣的
Y是這樣的
接下來(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;">
## 設(shè)置置換次數(shù)
## #number of permutations
perm <- 1000
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;">
#anti-log if max < 50 in mixture file
## 這里說明椰于,當(dāng)他檢測(cè)數(shù)據(jù)已經(jīng)被log化之后怠益,就會(huì)幫我們?nèi)og化
if(max(Y) < 50) {Y <- 2^Y}
</pre>
為什么要這么做,因?yàn)樗且曰蚣癁橐罁?jù)來(lái)推斷廉羔,我們看看基因特征的表達(dá)量發(fā)現(xiàn)溉痢。
這些數(shù)據(jù)有的很大,可以看出是沒有取過log的憋他。
接下里要對(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;">
QN=T
if(QN == TRUE){
tmpc <- colnames(Y)
tmpr <- rownames(Y)
Y <- preprocessCore::normalize.quantiles(Y)
colnames(Y) <- tmpc
rownames(Y) <- tmpr
}
</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;">
Xgns <- row.names(X)
Ygns <- row.names(Y)
YintX <- Ygns %in% Xgns
Y <- Y[YintX,]
XintY <- Xgns %in% row.names(Y)
X <- X[XintY,]
</pre>
現(xiàn)在X和Y的行都是517行
接下來(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;">
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;">
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]]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(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;">
## 提取一個(gè)樣本
y <- Y[,1]
y <- (y - mean(y)) / sd(y)
## 計(jì)算
result <- CoreAlg(X, y)
str(result)
</pre>
返回的結(jié)果記錄了三個(gè)結(jié)果轩娶,我們可以通過再次探索CoreAlg
來(lái)確定那是三個(gè)返回值
w,mix_rmse,mix_r
這一段框往,我寫不下去了鳄抒,會(huì)錄制成視頻更新在答疑群中。
簡(jiǎn)單來(lái)說
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;">
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;">
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次后是這樣的饵婆。
這個(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;">
doPerm <- function(perm, X, Y){
itor <- 1
Ylist <- as.list(data.matrix(Y))
dist <- matrix()
print(paste0("do permutations:",perm," in total"))
while(itor <= perm){
print(paste0("nperm:",itor))
#random mixture
yr <- as.numeric(Ylist[sample(length(Ylist),dim(X)[1])])
#standardize mixture
yr <- (yr - mean(yr)) / sd(yr)
#run CIBERSORT core algorithm
result <- CoreAlg(X, yr)
mix_r <- result$mix_r
#store correlation
if(itor == 1) {dist <- mix_r}
else {dist <- rbind(dist, mix_r)}
itor <- itor + 1
}
newList <- list("dist" = dist)
}
</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;">
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;">
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代替穆咐。
如果樣本多颤诀,我們還能可以先把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;">
library(dplyr)
library(tidyr)
library(tibble)
dd1 <- obj %>%
as.data.frame() %>%
rownames_to_column("sample") %>%
pivot_longer(cols=2:23,
names_to= "celltype",
values_to = "Proportion")
</pre>
現(xiàn)在的數(shù)據(jù)是這樣的
有了數(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;">
library(ggplot2)
ggplot(dd1,aes(sample,Proportion,fill = celltype)) +
geom_bar(position = "stack",stat = "identity")+
theme_bw()+
guides(fill=guide_legend(ncol=1))
</pre>
看縱坐標(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
直接去下載讀入R語(yǔ)言就可以用了眶明。
那么講這么長(zhǎng)有什么用呢艰毒?
是這樣的:
這個(gè)函數(shù)使用起來(lái)這么簡(jiǎn)單,只要我們替換掉基因特征數(shù)據(jù)搜囱,就可以遷移到其他物種丑瞧,
比如,我們想要量化小鼠的組織蜀肘,那怎么辦呢绊汹,只要找到小鼠免疫細(xì)胞的這個(gè)特征集就行了。
還好扮宠,這個(gè)事情西乖,不需要我們做,有人已經(jīng)做好了坛增,下次我們就講這個(gè)获雕。
量化免疫浸潤(rùn)時(shí)CIBERSORT的注意事項(xiàng)