R:稀疏?转换共生矩阵

时间:2018-05-25 20:33:24

标签: r matrix heatmap pheatmap

我是一名Bio大使用R生成一些可视化,显示哪些人类蛋白质(uniprots)被不同的细菌菌株靶向。

# sample data
human.uniprots <- c("P15311", "P0CG48", "Q8WYH8", "P42224", "Q9NXR8",
                    "P40763", "P05067", "P60709", "Q9UDW1", "Q9H160",
                    "Q9UKL0", "P26038", "P61244", "O95817", "Q09472",
                    "P15311","P05067", "P60709", "Q9UDW1", "Q9H160")
strains <- rep(c("A", "B", "C", "C"), each = 5)
final <- cbind(human.uniprots, strains)

我试图生成共生矩阵/热图......类似于

h.map <- data.frame(matrix(nrow = length(unique(human.uniprots)),
ncol = length(unique(strains)) + 1))
h.map.cols <- c("human_uniprots", "A", "B", "C")
colnames(h.map) <- h.map.cols

...其中柱具有菌株,行具有蛋白质,数据框架细胞填充有蛋白质与菌株相互作用的次数。因此,如果菌株A,B和C都与uniprot相互作用,那么它们的细胞中的所有uniprot行都应该具有3的值。

我尝试制作一个独特菌株和human_uniprots的元组列表,然后从我想要填充的矩阵中搜索与菌株和人类uniprot对匹配的元组,并添加&#34; 1&# 34;如果有匹配...但我不确定如何使用R中的元组。然后我看到了这个:Populating a co-occurrence matrix

这就是我想要的,但我不理解其用法或语法...稀疏()甚至是R中的函数?

另外......将所有蛋白质与所有菌株相互作用的蛋白质排列会很好。因此,与所有菌株相互作用的所有蛋白质应位于顶部,然后是与2个菌株相互作用的蛋白质,然后是1个菌株...

3 个答案:

答案 0 :(得分:1)

使用dplyr,您可group_bycountspread获得每个菌株数。然后使用rowSums()

将每个菌株计数替换为该行的总计数
library(dplyr)

as.data.frame(final) %>%
  group_by(human.uniprots, strains) %>%
  count() %>%
  spread(strains, n) %>%
  ungroup() %>%
  mutate(total_n = rowSums(.[2:ncol(.)])) %>%
  mutate_if(is.numeric, funs(ifelse(. == 0, 0, total_n))) %>%
  select(-total_n)

  # A tibble: 15 x 5
   human.uniprots     A     B     C     D
   <fct>          <dbl> <dbl> <dbl> <dbl>
 1 O95817            0.    0.    1.    0.
 2 P05067            0.    2.    0.    2.
 3 P0CG48            1.    0.    0.    0.
 4 P15311            2.    0.    0.    2.
 5 P26038            0.    0.    1.    0.
 6 P40763            0.    1.    0.    0.
 7 P42224            1.    0.    0.    0.
 8 P60709            0.    2.    0.    2.
 9 P61244            0.    0.    1.    0.
10 Q09472            0.    0.    1.    0.
11 Q8WYH8            1.    0.    0.    0.
12 Q9H160            0.    2.    0.    2.
13 Q9NXR8            1.    0.    0.    0.
14 Q9UDW1            0.    2.    0.    2.
15 Q9UKL0            0.    0.    1.    0.

答案 1 :(得分:1)

您可以使用table执行此操作,或者如果您希望它稀疏,则可以使用xtabs

因此,对于您的示例,您可以使用

tab <- table(final[,"human.uniprots"], final[,"strains"]) 
tab* rowSums(tab)

或稀疏

tab <- xtabs(~human.uniprots + strains, final, sparse=TRUE)
tab <- tab*Matrix::rowSums(tab)

然后您可以使用

绘制它
Matrix::image(tab, scales=list(y=list(at=1:nrow(tab), label=rownames(tab)),
                               x=list(at=1:ncol(tab), label=colnames(tab))),
              ylab="uniprots",
              xlab="strains")

enter image description here

您还可以按出现次数对行进行排名

r <- order(-Matrix::rowSums(tab))

# and then reorder the rows of the matrix and the labels
Matrix::image(tab[r,],
              scales=list(y=list(at=1:nrow(tab), label=rownames(tab)),
                          x=list(at=1:ncol(tab), label=colnames(tab)[r])),
                  ylab="uniprots",
                  xlab="strains")

答案 2 :(得分:1)

sparse()是一个MATLAB函数。您正在描述由关联矩阵表示的二分网络。

human.uniprots <- c("P15311", "P0CG48", "Q8WYH8", "P42224", "Q9NXR8",
                    "P40763", "P05067", "P60709", "Q9UDW1", "Q9H160",
                    "Q9UKL0", "P26038", "P61244", "O95817", "Q09472",
                    "P15311","P05067", "P60709", "Q9UDW1", "Q9H160")
strains <- rep(c("A", "B", "C", "D"), each = 5)
final <- cbind(human.uniprots, strains)

final_df <- as.data.frame(final)

library(igraph) # install.packages("igraph")
g <- graph_from_data_frame(final_df, directed = FALSE)
V(g)$type <- ifelse(V(g)$name %in% strains, FALSE, TRUE)

as_incidence_matrix(g)
#>   P15311 P0CG48 Q8WYH8 P42224 Q9NXR8 P40763 P05067 P60709 Q9UDW1 Q9H160
#> A      1      1      1      1      1      0      0      0      0      0
#> B      0      0      0      0      0      1      1      1      1      1
#> C      0      0      0      0      0      0      0      0      0      0
#> D      1      0      0      0      0      0      1      1      1      1
#>   Q9UKL0 P26038 P61244 O95817 Q09472
#> A      0      0      0      0      0
#> B      0      0      0      0      0
#> C      1      1      1      1      1
#> D      0      0      0      0      0

或.....

V(g)$type <- ifelse(V(g)$name %in% strains, TRUE, FALSE)
                                        # swap TRUE/FALSE

as_incidence_matrix(g)
#>        A B C D
#> P15311 1 0 0 1
#> P0CG48 1 0 0 0
#> Q8WYH8 1 0 0 0
#> P42224 1 0 0 0
#> Q9NXR8 1 0 0 0
#> P40763 0 1 0 0
#> P05067 0 1 0 1
#> P60709 0 1 0 1
#> Q9UDW1 0 1 0 1
#> Q9H160 0 1 0 1
#> Q9UKL0 0 0 1 0
#> P26038 0 0 1 0
#> P61244 0 0 1 0
#> O95817 0 0 1 0
#> Q09472 0 0 1 0

reprex package(v0.2.0)创建于2018-05-25。