将两个sampleID的相应值加入到新的单个列中

时间:2018-05-28 17:43:53

标签: r dataframe join dplyr bioinformatics

我有一个如下所示的数据框sampleManifest

SampleName          Status          Role          Sex
AU056001_00HI1299A  unaffected      sibling       female
AU056002_00HI1301A  unaffected      proband       male  
AU0780201_00HI1775A unaffected      father        male  
AU0780202_00HI1777A unaffected      mother        female
AU0780301_00HI1778A affected        proband       male  
.
.
.

成对样本比较的单独数据框kinshipEstimates

FID    ID1                      ID2             Kinship Relationship    
AU0560 AU056001_00HI1299A  AU056002_00HI1301A   0.0283  full-sibling   
AU0780 AU0780201_00HI1775A AU0780202_00HI1777A -0.00160 unrelated   
AU0780 AU0780201_00HI1775A AU0780301_00HI1778A  0.284   parent-child
AU0780 AU0780202_00HI1777A AU0780301_00HI1778A  0.246   parent-child
.
.
.

我想构建一个新的数据框,sampleManifest$Role每行中两个样本中的每一个都kinshipEstimates,所以它看起来像这样:

FID    ID1                      ID2             Roles           Kinship Relationship    
AU0560 AU056001_00HI1299A  AU056002_00HI1301A   sibling-proband 0.0283  full-sibling   
AU0780 AU0780201_00HI1775A AU0780202_00HI1777A  father-mother  -0.00160 unrelated   
AU0780 AU0780201_00HI1775A AU0780301_00HI1778A  father-proband  0.284   parent-child
AU0780 AU0780202_00HI1777A AU0780301_00HI1778A  mother-proband  0.246   parent-child
.
.
.

我一直在尝试使用left_join,但不知道如何将对中每个样本的相应Role合并为单个值。

2 个答案:

答案 0 :(得分:3)

A solution is to use double left_join using tidyverse package. First join kinshipEstimates with sampleManifest on ID1 and SampleName. Again join sampleManifest with result on ID2 and SampleName. Finally, use tidyr::unite to merge Role.x and Role.y.

library(tidyverse)

left_join(kinshipEstimates, sampleManifest, by=c("ID1" = "SampleName")) %>%
  select(-Status, -Sex) %>%
  left_join(sampleManifest, by=c("ID2" = "SampleName")) %>%
  unite(Roles, Role.x, Role.y, sep="-") %>%
  select(-Sex, -Status)


#      FID                 ID1                 ID2 Kinship Relationship           Roles
# 1 AU0560  AU056001_00HI1299A  AU056002_00HI1301A  0.0283 full-sibling sibling-proband
# 2 AU0780 AU0780201_00HI1775A AU0780202_00HI1777A -0.0016    unrelated   father-mother
# 3 AU0780 AU0780201_00HI1775A AU0780301_00HI1778A  0.2840 parent-child  father-proband
# 4 AU0780 AU0780202_00HI1777A AU0780301_00HI1778A  0.2460 parent-child  mother-proband

Data:

sampleManifest <- read.table(text = 
"SampleName          Status          Role          Sex
AU056001_00HI1299A  unaffected      sibling       female
AU056002_00HI1301A  unaffected      proband       male  
AU0780201_00HI1775A unaffected      father        male  
AU0780202_00HI1777A unaffected      mother        female
AU0780301_00HI1778A affected        proband       male",
stringsAsFactors = FALSE, header = TRUE)

kinshipEstimates <- read.table(text = 
"FID    ID1                      ID2             Kinship Relationship    
AU0560 AU056001_00HI1299A  AU056002_00HI1301A   0.0283  full-sibling   
AU0780 AU0780201_00HI1775A AU0780202_00HI1777A -0.00160 unrelated   
AU0780 AU0780201_00HI1775A AU0780301_00HI1778A  0.284   parent-child
AU0780 AU0780202_00HI1777A AU0780301_00HI1778A  0.246   parent-child",
stringsAsFactors = FALSE, header = TRUE)

答案 1 :(得分:1)

这是一种使用gather,一个inner_joingroup_by的方法。
添加行号可让我们在分组时跟踪ID1 / ID2对:

kinshipEstimates %>%
  mutate(row_num = row_number()) %>%
  gather(which_id, id, -row_num, -FID, -Kinship, -Relationship) %>%
  inner_join(sampleManifest, by=c("id" = "SampleName")) %>%
  group_by(FID, row_num) %>%
  summarise(Roles = paste(Role, collapse="-"),
            Kinship = first(Kinship),
            Relationship = first(Relationship))

  FID    row_num Roles            Kinship Relationship
  <chr>    <int> <chr>              <dbl> <chr>       
1 AU0560       1 sibling-proband  0.0283  full-sibling
2 AU0780       2 father-mother   -0.00160 unrelated   
3 AU0780       3 father-proband   0.284   parent-child
4 AU0780       4 mother-proband   0.246   parent-child