【问题标题】:how to calculate the correlation between one gene and the other genes如何计算一个基因与其他基因之间的相关性
【发布时间】:2021-02-04 01:38:00
【问题描述】:

我想找到一个基因与其他基因的相关性。最后我会把这个函数变成闪亮的。

我的意思是当我选择输入一个基因时,我可以得到有多少其他基因与这个基因密切相关。并且这些相关基因可以在主面板输出,甚至可以作为文本或excel文件下载。

请原谅我不恰当的表达,因为我是新手。

我的样本 FPKM 基因计数如下:

##  library(shiny)
##  library(dplyr)
##  library(tidyr)
##  library(ggplot2)

###
mean_data <- data.frame(
  Name = c(paste0("Gene_", LETTERS[1:20])),
  matx <- matrix(sample(1:1000, 1000, replace = T), nrow = 20)
)
names(mean_data)[-1] <- c(paste0("Sample_", 1:50))
rownames(mean_data)<-mean_data[,1]
mean_data<-mean_data[,-1]

## I don't know the code below is right or not . But I wanna the correlation between the input one and the others 

corResult=apply(mean_data,1,function(x){
  cor(x[1:25],x[26:50],method="spearman")
})
hist(corResult)


corResult_test=apply(mean_data,1,function(x){
  cor.test(x[1:25],x[26:50],method="spearman",exact = F)$p.value
})
table(abs(corResult)>0.65 & corResult_test<0.05)

有人可以帮助我吗?给我一个合适的方法。

感激不尽。

【问题讨论】:

    标签: r shiny correlation


    【解决方案1】:

    psych 包中查看corr.test;那应该做你想做的事:

    library(psych)
    set.seed(7)
    mean_data <- data.frame(
        Name = c(paste0("Gene_", LETTERS[1:20])),
        matx <- matrix(sample(1:1000, 1000, replace = T), nrow = 20)
    )
    names(mean_data)[-1] <- c(paste0("Sample_", 1:50))
    rownames(mean_data) <- mean_data[,1]
    mean_data <- mean_data[,-1]
    
    myCor <- function(x="Gene_A", mat=mean_data, pval=.05, R=.65, method="spearman"){
        tm <- corr.test(t(mat[x,,drop=FALSE]),
                               y = t(mat), use = "pairwise", method=method, adjust="holm", 
                               alpha=pval, ci=TRUE, minlength=5)
        res <- setdiff(colnames(tm$r)[which(with(tm, abs(r) > R & p < pval))], x)
       if(length(res) > 0) res 
    }
    
    res <- sapply(rownames(mean_data), myCor, pval=.1, R=.3) 
    res[lengths(res) > 0]
    #> $Gene_A
    #> [1] "Gene_K"
    #> 
    #> $Gene_H
    #> [1] "Gene_K"
    #> 
    #> $Gene_K
    #> [1] "Gene_A" "Gene_H"
    

    reprex package (v1.0.0) 于 2021 年 2 月 3 日创建

    【讨论】:

    • 谢谢。我认为它对我有用。但是“R=.65”我不明白。你能解释一下吗?而且我不知道是否应该将 FPKM 归一化基因表达矩阵或 DEG 矩阵与 corr.test 一起使用?为什么前后的 R 和 pval 不一致?请原谅我。我是这部分的新手。
    • 您可以通过这种方式将附加参数传递给sapply 函数中的myCor 函数。如果您不传递任何参数,它将使用myCor 函数中设置的默认值。在这里,我使用 R=.3 和 pval=.1 来演示输出,因为您的示例不会产生您想要的截止值。
    • 是的,我明白你所说的 R. Vary 的意思,谢谢。你能回答这个问题吗:我不知道我是否应该使用 FPKM 归一化基因表达矩阵或 DEG 矩阵与 corr.test ?我认为选择 FPKM 归一化基因表达矩阵作为 corr.test 的输入对象是绝对正确的吗?我是对还是错?
    • 看看例如hbctraining.github.io/DGE_workshop_salmon/lessons/…biostars.org/p/328345 了解什么适合您的场景。我个人可能会使用 vst 转换的计数。
    • 感激不尽。我从你身上学到了很多。我的最后一个问题:当我每次输入一个基因作为 x 时,我可以得到这个基因与其他基因之间相关性的结果。我希望结果将在数据框中显示基因名称、相关性和 p 值。如果是这样,我到底应该怎么处理res?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    相关资源
    最近更新 更多