簡體   English   中英

根據目錄中所有文件中的數據計算成對spearman的等級相關性

[英]Calculate pairwise spearman's rank correlation from data present in all files in a directory

我正在嘗試計算Spearman的等級相關性,其中每個實驗的數據(名稱和等級的tsv)存儲在目錄中的單獨文件中。

以下是輸入文件的格式:

#header not present
#geneName   value
ENSMUSG00000026179.14   14.5648627685587
ENSMUSG00000026179.14   0.652158034413075
ENSMUSG00000026179.14   0.652158034413075
ENSMUSG00000026179.14   1.852158034413075
ENSMUSG00000026176.13   4.13033421794948
ENSMUSG00000026176.13   4.13033421794948
ENSMUSG00000026176.13   15.4344068144428
ENSMUSG00000026176.13   15.4344068144428
ENSMUSG00000026176.13   6.9563523670728
...

我的問題是密鑰(基因名稱)是重復的,每個實驗文件包含不同但重疊的基因名稱集。 我需要的是每個對的基因名稱的交集,同時執行相關並刪除重復,可能類似於這個偽代碼:

# Find correlation for all possible pairs of input(i.e. files in directory)
files = list_Of_files("directory")
for(i in files) {
    for(k in files) {
    CommonGenes <- intersect (i,k)
    tempi <- removeRepetitive(i, CommonGenes) #Keep the gene with highest value and remove all other repeating genes. Also, keep only common genes.
    tempk <- removeRepetitive(k, CommonGenes) #Keep the gene with highest value and remove all other repeating genes. Also, keep only common genes. 
    correlationArray[] <- spearman(tempi, tempk) #Perform correlation for only the common genes
}
}

最后,我想使用corrplotqtlcharts繪制相關矩陣。

首先,將所有數據讀入數據幀列表,有關詳細信息,請參閱此帖子 ,這里我們只是創建一個虛擬數據。

library(dplyr)

# dummy data
set.seed(1)
myDfs <- list(
  data.frame(geneName = sample(LETTERS[1:4], 15, replace = TRUE), value = runif(15)),
  data.frame(geneName = sample(LETTERS[1:4], 15, replace = TRUE), value = runif(15)),
  data.frame(geneName = sample(LETTERS[1:4], 15, replace = TRUE), value = runif(15)),
  data.frame(geneName = sample(LETTERS[1:4], 15, replace = TRUE), value = runif(15)),
  data.frame(geneName = sample(LETTERS[1:4], 15, replace = TRUE), value = runif(15))
)

然后,就像你的兩個嵌套for循環一樣,我們這里有兩個嵌套的apply函數。 在循環內,我們聚合並獲得匹配的合並基因名稱的相關性。

res <- sapply(myDfs, function(i){
  # group by gene, get max value
  imax <- i %>% group_by(geneName) %>% summarise(i_Max = max(value))
  sapply(myDfs, function(j){
    # group by gene, get max value
    jmax <- j %>% group_by(geneName) %>% summarise(j_Max = max(value))
    # get overlapping genes
    ij <- merge(imax, jmax, by = "geneName")
    # return correlation
    cor(ij$i_Max, ij$j_Max, method = "spearman")
  })
})

res將具有相關矩陣。

res

#      [,1] [,2] [,3] [,4] [,5]
# [1,]  1.0 -0.2  1.0  0.4 -0.4
# [2,] -0.2  1.0 -0.2  0.8  0.0
# [3,]  1.0 -0.2  1.0  0.4 -0.4
# [4,]  0.4  0.8  0.4  1.0 -0.4
# [5,] -0.4  0.0 -0.4 -0.4  1.0

對於相關圖,有許多選擇可供選擇 這里我們使用corrplot作為例子:

corrplot::corrplot(res)

在此輸入圖像描述

這是另一種解決方案。 它使用expand.grid創建組合,然后使用<dplyr>動詞的管道來計算主表子集的相關性,而不是嵌套循環。

這種方法既有優點也有缺點。 最重要的是,它非常適合“整潔的數據”方法,並且有些人主張盡可能多地處理整潔的數據 實際代碼大約與zx8754一樣長。

library(dplyr)

genes = sprintf('ENSMUSG%011d', 1 : 50)
my_dfs = replicate(4, tibble(Gene = sample(genes, 20, replace = TRUE), Value = runif(20)),
                   simplify = FALSE)

首先,我們希望使基因名稱獨特,因為每個表隨后都需要每個表獨特的基因:

my_dfs = lapply(my_dfs, function (x) summarize(group_by(x, Gene), Value = max(Value)))

現在我們可以創建此列表的所有排列:

combinations = bind_cols(expand.grid(i = seq_along(my_dfs), j = seq_along(my_dfs)),
                         expand.grid(x = my_dfs, y = my_dfs))

此時,我們有一個表,其中包含所有成對組合ij的索引,以及組合本身作為列表列:

# A tibble: 16 x 4
       i     j                 x                 y
   <int> <int>            <list>            <list>
 1     1     1 <tibble [17 x 2]> <tibble [17 x 2]>
 2     2     1 <tibble [18 x 2]> <tibble [17 x 2]>
 3     3     1 <tibble [19 x 2]> <tibble [17 x 2]>
…

我們現在按索引進行分組,並按基因名稱連接每個組中的單個列表列:

correlations = combinations %>%
    group_by(i, j) %>%
    do(inner_join(.$x[[1]], .$y[[1]], by = 'Gene')) %>%
    print() %>%
    summarize(Cor = cor(Value.x, Value.y, method = 'spearman'))

間歇:在print()行,我們留下了一個完全展開的表,列出了所有基因表的所有成對組合(兩個原始表的Value列分別重命名為Value.xValue.y ):

# A tibble: 182 x 5
# Groups:   i, j [16]
       i     j               Gene    Value.x    Value.y
   <int> <int>              <chr>      <dbl>      <dbl>
 1     1     1 ENSMUSG00000000014 0.93470523 0.93470523
 2     1     1 ENSMUSG00000000019 0.21214252 0.21214252
 3     1     1 ENSMUSG00000000028 0.65167377 0.65167377
 4     1     1 ENSMUSG00000000043 0.12555510 0.12555510
 5     1     1 ENSMUSG00000000010 0.26722067 0.26722067
 6     1     1 ENSMUSG00000000041 0.38611409 0.38611409
 7     1     1 ENSMUSG00000000042 0.01339033 0.01339033
…

下一行使用相同的組輕松計算這些表的成對相關性。 由於整個表格為長格式,因此可以使用<ggplot2>方便地繪制:

library(ggplot2)

ggplot(correlations) +
    aes(i, j, color = Cor) +
    geom_tile() +
    scale_color_gradient2()

在此輸入圖像描述

...但是如果你需要這個作為方形相關矩陣,那么沒有什么比這更容易了:

corr_mat = with(correlations, matrix(Cor, nrow = max(i)))
      [,1]  [,2]  [,3]  [,4]
[1,]  1.00  1.00 -0.20 -0.26
[2,]  1.00  1.00 -0.43 -0.50
[3,] -0.20 -0.43  1.00 -0.90
[4,] -0.26 -0.50 -0.90  1.00

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM