[英]If position from File B is found in interval of file A, print to new dataframe
我有兩個文件。
帶間隔的文件 A (基因組上的區域)
chr startpos endpos nMajor nMinor
1 1 762273 120612006 1 0
2 1 144854594 187610698 2 1
3 1 193051685 249120684 1 1
4 2 45895 242836535 1 1
5 3 361508 197566254 1 1
6 4 86022 190862182 1 1
帶有位置(突變)的文件 B
mutation_id chr start end ref_counts var_counts
1 1_3649563_G/T 1 3649563 3649564 551 159
2 1_6196895_G/C 1 6196895 6196895 85 30
3 1_10678395_C/T 1 10678395 10678395 274 60
4 1_11090913_G/C 1 11090913 11090914 70 41
5 1_11772423_G/A 1 11772423 11772423 146 55
6 1_12316528_C/G 1 12316528 12316528 110 88
現在我想將這兩個文件合並到文件 C 中,如果 position 落入相應的間隔,則將 nMajor 和 nMinor 的信息從 FileA 添加到 FileB。
所以我需要先檢查染色體是否相同,然后檢查FileB中的開始和結束position是否在FileA的區間內。
我的output應該是:文件C
mutation_id chr start end ref_counts var_counts nMajor nMinor
1 1_3649563_G/T 1 3649563 3649563 551 159 1 0
2 1_6196895_G/C 1 6196895 6196895 85 30 1 0
3 1_10678395_C/T 1 10678395 10678395 274 60 1 0
4 1_11090913_G/C 1 11090913 11090913 70 41 1 0
5 1_11772423_G/A 1 11772423 11772423 146 55 1 0
6 1_12316528_C/G 1 12316528 12316528 110 88 1 0
對於在 FileB 的區間內找不到的 FileB 行,我想打印“X”作為占位符。
您可以使用fuzzyjoin
來完成此任務:
library(dplyr)
library(fuzzyjoin)
file_b %>%
fuzzy_left_join(file_a,
by = c("chr" = "chr",
"start" = "startpos",
"end" = "endpos",
"start" = "endpos",
"end" = "startpos"),
match_fun = list(`==`, `>`, `<`, `<`, `>`)) %>%
select(-startpos, -endpos, -chr.y) %>%
rename(chr = chr.x)
我沒有為不匹配創建X
,因為這會破壞nMinor
和 nMinor 列的nMajor
並將它們轉換為字符串/字符。 我認為這不是一個好主意,而且NA
值很容易處理。
這返回
# A tibble: 7 x 8
mutation_id chr start end ref_counts var_counts nMajor nMinor
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1_3649563_G/T 1 3649563 3649564 551 159 1 0
2 1_6196895_G/C 1 6196895 6196895 85 30 1 0
3 1_10678395_C/T 1 10678395 10678395 274 60 1 0
4 1_11090913_G/C 1 11090913 11090914 70 41 1 0
5 1_11772423_G/A 1 11772423 11772423 146 55 1 0
6 1_12316528_C/G 1 12316528 12316528 110 88 1 0
7 ABC 2 123 456 2 3 NA NA
file_a <- structure(list(chr = c(1, 1, 1, 2, 3, 4), startpos = c(762273,
144854594, 193051685, 45895, 361508, 86022), endpos = c(120612006,
187610698, 249120684, 242836535, 197566254, 190862182), nMajor = c(1,
2, 1, 1, 1, 1), nMinor = c(0, 1, 1, 1, 1, 1)), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
在file_b
,我添加了一個假數據集來引發不匹配:
file_b <- structure(list(mutation_id = c("1_3649563_G/T", "1_6196895_G/C",
"1_10678395_C/T", "1_11090913_G/C", "1_11772423_G/A", "1_12316528_C/G",
"ABC"), chr = c(1, 1, 1, 1, 1, 1, 2), start = c(3649563, 6196895,
10678395, 11090913, 11772423, 12316528, 123), end = c(3649564,
6196895, 10678395, 11090914, 11772423, 12316528, 456), ref_counts = c(551,
85, 274, 70, 146, 110, 2), var_counts = c(159, 30, 60, 41, 55,
88, 3)), row.names = c(NA, -7L), class = c("tbl_df", "tbl", "data.frame"
))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.