用R更改0和1信息的矩阵中的数据帧

时间:2019-02-05 09:36:07

标签: r dataframe matrix bioinformatics

我有一个数据框,例如:

Cluster sequence_name
1       species1
1       species1
1       species2
1       species3
1       species3
1       gene1
1       gene2
2       species4
2       species5
2       spciess5
2       species3
2       gene3
2       gene4

,我想得到一个矩阵,例如:

           gene1  gene2 gene3 gene4
species5   0      0     1     1
species4   0      0     1     1
species1   1      1     0     0
species2   1      1     0     0
species3   1      1     1     1

其中1表示存在speciesX的基因,而0表示不存在的基因。 存在表示speciesXsame clustergeneX中存在。例如,gene1在cluster1中以species1, 2 and 3的形式出现。 相反,species5 and 4中不存在cluster1

您也可以看到;有多个重复项(在同一簇中,一个物种可以代表多次)。 谢谢您的帮助。

真实数据如下:

cluster_names seq_names  
1             AP_000401.1  
1             NP_039001.1  
1             Canis_lupus  
1             Canis_familiaris
2             YP_0090909.1
2             Mustela_putorius
2             Mustela_furo
2             YP_0909200.1

....

...

AP和NP等XX个字母是基因 和属种

回应丹尼斯:

以下是真实数据的开头:

cluster_names  seq_names
1   scf7180005155889:2745-3053(-):Drosophia_melanogaster
1   IDBA_scaffold_72878:85-225:292707-293006(+):Orussu_sp
1   scaffold_3615:40850-41320(-):Canis_lupus
1   scaffold_8697:754-1209(-):homo_sapiens
1   scf7180005155889:72-1908(-):homo_sapiens
1   YP_003969716.1
1   NP_003986717.1
2   scaffold_17536:2745-3053(-):Drosophia_melanogaster
2   scf7180005155889:2000-8900(-):Drosophia_melanogaster
2   scaffold_8697:754-1209(-):homo_sapiens
2   YP_003956764.1
2   YP_004894416.1
2   YP_008958968.1

我应该得到的输出是:

output

对丹尼斯的回应:

> df <- read.table(text = "Cluster sequence_name
+ 1       :Drosophia_melanogaster
+                  1       scf7180005155889:2745-3053(-):Drosophila_melanogaster
+                  1       scf7180005155889:2745-3053(-):Orussu_sp
+                  1       scf7180005155889:2745-3053(-):Canis_lupus
+                  1       scf7180005155889:72-1908(-):Homo_sapiens
+                  1       scf7180005155889:2745-3053(-):Homo_sapiens
+                  1       YP_003970075.1
+                  1       YP_005070075.1
+                  2       scf7180005155889:72-1908(-):Drosophila_melanogaster
+                  2       scf7180005155889:72-1908(-):Drosophila_melanogaster
+                  2       scf7180005155889:72-1908(-):Homo_sapiens
+                  2       YP_039970075.1
+                  2       NP_003900075.1",header = T)
> df <- setDT(df)
> species <- df[grep("[0-9]+\\([+-]\\):[A-z ]+",sequence_name)]
> species[,sequence_name := str_extract(sequence_name,"(?<=[0-9]\\([+-]\\):)[A-z ]+")]
> genes <- df[grep("[0-9]+\\.1",sequence_name)]
> genes[,sequence_name :=sequence_name]
> plouf <- merge(genes,species,by = "Cluster",allow.cartesian=TRUE)
> result <- dcast(plouf,sequence_name.y~sequence_name.x,fun.aggregate = length)
Using 'sequence_name.y' as value column. Use 'value.var' to override
> row.names(result)<-result$sequence_name.y
> result$sequence_name.y<- NULL
> result
   NP_003900075.1 YP_003970075.1 YP_005070075.1 YP_039970075.1
1:              0              1              1              0
2:              2              1              1              2
3:              1              2              2              1
4:              0              1              1              0

2 个答案:

答案 0 :(得分:3)

library(data.table)
library(stringr)
df <- setDT(df)

我将在此处使用data.table。因此,我们的想法是创建两个数据框架,一个包含基因,一个包含物种

species <- df[grep("species",sequence_name)]
species[,sequence_name := str_extract(sequence_name,"(?<=:)[a-z0-9]+$")]
genes <- df[grep("gene",sequence_name)]

> species
   Cluster sequence_name
1:       1      species1
2:       1      species2
3:       1      species3
4:       2      species4
5:       2      species5
6:       2      species3
> genes
   Cluster sequence_name
1:       1         gene1
2:       1         gene2
3:       2         gene3
4:       2         gene4

您想使用allow.cartesian=TRUE通过群集将它们合并在一起,因为合并向量不是没有任何数据的单个标识符。

plouf <- merge(genes,species,by = "Cluster",allow.cartesian=TRUE)

    Cluster sequence_name.x sequence_name.y
 1:       1           gene1        species1
 2:       1           gene1        species2
 3:       1           gene1        species3
 4:       1           gene2        species1
 5:       1           gene2        species2
 6:       1           gene2        species3
 7:       2           gene3        species4
 8:       2           gene3        species5
 9:       2           gene3        species3
10:       2           gene4        species4
11:       2           gene4        species5
12:       2           gene4        species3

然后,在计算发生次数的同时,获取结果只是采用宽格式,您可以在此处使用dcast

result <- dcast(plouf,sequence_name.y~sequence_name.x,fun.aggregate = length)


   sequence_name.y gene1 gene2 gene3 gene4
1:        species1     1     1     0     0
2:        species2     1     1     0     0
3:        species3     1     1     1     1
4:        species4     0     0     1     1
5:        species5     0     0     1     1

等等。我让dplyr经验丰富的用户提出与dplyr等效/改进的解决方案。

数据:

df <- read.table(text = "Cluster sequence_name
1       Scaffold_1:species1
                 1       Scaffold_2:species2
                 1       Scaffold_3:species3
                 1       gene1
                 1       gene2
                 2       Scaffold_4:species4
                 2       Scaffold_5:species5
                 2       Scaffold_6:species3
                 2       gene3
                 2       gene4",header = T)

您将显示真实数据:

df <- read.table(text ="cluster_names  seq_names
                 1   scf7180005155889:2745-3053(-):Drosophia_melanogaster
                 1   scaffold_2484:292707-293006(+):Orussu_sp
                 1   scaffold_3615:40850-41320(-):Canis_lupus
                 1   scaffold_8697:754-1209(-):homo_sapiens
                 1   scf7180005155889:72-1908(-):homo_sapiens
                 1   YP_003969716.1
                 1   NP_003986717.1
                 2   scaffold_17536:2745-3053(-):Drosophia_melanogaster
                 2   scf7180005155889:2000-8900(-):Drosophia_melanogaster
                 2   scaffold_8697:754-1209(-):homo_sapiens
                 2   YP_003956764.1
                 2   YP_004894416.1
                 2   YP_008958968.1",header = T)

您应该通过以下方式更改创建两个数据表的步骤:

species <- df[grep("[0-9]+\\([+-]\\):[A-z ]+",seq_names)]
species[,sequence_name := str_extract(seq_names,"(?<=[0-9]\\([+-]\\):)[A-z ]+")]
genes <- df[grep("[0-9]+\\.1",seq_names)]
genes[,sequence_name :=seq_names]

这里"[0-9]+\\.1"假定所有基因都以1结尾,并且物种描述中没有意义。为了提取物种信息,我想它总是在数字后包含(+):(-)+

但这是一个正则表达式问题,如果您有问题,则应该是另一个问题。您在这里的问题是找到整形数据以获得结果的方法。我的回答是为您提供了处理示例数据的步骤:使用正则表达式创建两个基因和物种数据框,将它们合并并重新成形。

其余的作品:

plouf <- merge(genes,species,by = "cluster_names",allow.cartesian=TRUE)
result <- dcast(plouf,sequence_name.y~sequence_name.x,fun.aggregate = length)

答案 1 :(得分:3)

使用 tidyverse

# data
df1 <- read.table(text = "Cluster sequence_name
1       species1
                  1       species1
                  1       species2
                  1       species3
                  1       species3
                  1       gene1
                  1       gene2
                  2       species4
                  2       species5
                  2       species5
                  2       species3
                  2       gene3
                  2       gene4", header = TRUE, stringsAsFactors = FALSE)

# so that we know which row is species
species <- paste("species", 1:5, sep = "")
#[1] "species1" "species2" "species3" "species4" "species5"

library(tidyverse)

res <- reduce(split(df1, df1$sequence_name %in% species), left_join, by = "Cluster") %>% 
  unique() %>% 
  spread(key = "sequence_name.x", value = "Cluster") %>% 
  mutate_if(is.numeric,  funs(as.numeric(!is.na(.))))

res
#   sequence_name.y gene1 gene2 gene3 gene4
# 1        species1     1     1     0     0
# 2        species2     1     1     0     0
# 3        species3     1     1     1     1
# 4        species4     0     0     1     1
# 5        species5     0     0     1     1