r - 将 Drugbank ID 替换为药物名称
问题描述
我有一个与来自 DrugBank 的药物相关的基因数据集。我希望简单地将所有药库 ID 转换为人类可读的药物名称。如您所见,我的主要问题是某些基因与多种甚至数百种药物有关。多个药物 ID 在同一个逗号分隔的“列”中 R studio“匹配”或“合并功能”仅适用于每列中的第一个标识符,因此有效地删除了同一列“单元格”中的其余部分。我已经找到了在 excel 上为我的最佳候选人手动执行此操作的方法,但对于我的 3000 个基因数据集来说这是不现实的。
理想情况下,我想做一些类似“文本到列”的操作,但是在行中,所以每一行都会保留它的所有其他值,但只有单元格中的多个药库 ID 中的一个,然后可以使用匹配函数来替换它们.
药物库词汇表 (.csv) 如下所示:[DBvocabulary.csv]
DrugBank.ID 通用名
DB00001 来匹卢定
DB00002 西妥昔单抗
DB00003 Dornase alfa
DB00004 Denileukin diftitox
DB00005 依那西普
DB00006 比伐卢定
我的数据集 (.csv) 有 15 列,但重要的是:
[all_ph_active.csv]
基因名称 DrugBank.ID
F8 DB09130
TCN2 DB00200
LDLR DB09270;DB11251; DB14003
ALB DB00070;DB00137; DB00159; DB00162; DB00214;
欢迎任何建议,在此先感谢!
解决方案
一种方法是将名称列连接到原始数据框。
我在下面提供了一个小例子
library(tidyverse)
Translation <- tribble(~"ID", ~"Name",
"I001", "name1",
"I002", "name2",
"I003", "name3",
"I004", "name4",)
df <- tribble(~"ID",
"I001",
"I001",
"I004",
"I004",
"I002",
"I002",
"I001",
"I002",
"I003",
"I003",
"I004",
"I002",
"I001"
)
right_join(df, Translation, by=c("ID" ="ID"))
#> # A tibble: 13 x 2
#> ID Name
#> <chr> <chr>
#> 1 I001 name1
#> 2 I001 name1
#> 3 I004 name4
#> 4 I004 name4
#> 5 I002 name2
#> 6 I002 name2
#> 7 I001 name1
#> 8 I002 name2
#> 9 I003 name3
#> 10 I003 name3
#> 11 I004 name4
#> 12 I002 name2
#> 13 I001 name1
由reprex 包于 2021-04-03 创建 (v2.0.0 )
但是,此示例并未考虑提供的多个潜在名称。解决此问题的一种方法是为每种药物临时创建多个条目,如下例所示,然后以原始格式格式化名称。
我所做的一个假设是,药物每次都列在一个字符数组中,分号后跟一个空格作为分隔符。请对此进行纠正,我将相应地更新代码:
library(tidyverse)
Translation <- tribble(~"ID", ~"Name",
"I001", "name1",
"I002", "name2",
"I003", "name3",
"I004", "name4",)
df <- tribble(~"ID",
"I001",
"I001",
"I004; I002",
"I004",
"I002",
"I002",
"I001; I003",
"I002",
"I003",
"I003",
"I004",
"I002; I001",
"I001"
)
df_with_uniqueID <- df %>%
#Creates unique identifier for each row
mutate(uniqueNum = 1: length(df$ID))
# Replace IDs in characters with array of IDs
df_with_uniqueID$ID <- strsplit(df_with_uniqueID$ID, split = "; ")
# Give each ID its own column
unnest(df_with_uniqueID, cols = c(ID)) %>%
#right_join the results
right_join(Translation, by = c("ID" = "ID")) %>%
#reduce the additional columns
nest(cols = c(ID, Name)) %>%
# Convert the array of names to a single string
mutate(names = map(cols, function(x) paste(x$Name, collapse = "; "))) %>%
# Unnest our strings to a column
unnest(names) %>%
# Remove the column we no longer need
select(-cols)
#> # A tibble: 13 x 2
#> uniqueNum names
#> <int> <chr>
#> 1 1 name1
#> 2 2 name1
#> 3 3 name4; name2
#> 4 4 name4
#> 5 5 name2
#> 6 6 name2
#> 7 7 name1; name3
#> 8 8 name2
#> 9 9 name3
#> 10 10 name3
#> 11 11 name4
#> 12 12 name2; name1
#> 13 13 name1
由reprex 包于 2021-04-03 创建 (v2.0.0 )
推荐阅读
- excel - 系统重新启动后工作簿计算恢复为手动
- reactjs - Tailwind 覆盖了 wordpress 主题样式
- firebase - 如何发出 Firestore REST API PATCH 请求?
- python - 删除具有缺失值的给定变量的行
- ios - 闭包是否应该在分配给变量或常量时返回值,后跟一对括号?
- wordpress - Wordpress 优惠券:对不起,此优惠券不适用于选定的产品
- android - MotionEvent android:如何获得从屏幕上移除的确切触摸次数?
- pine-script - 我可以只在酒吧关闭的时间范围内收到警报吗?
- laravel - laravel 7 中的 Predis\Connection\ConnectionException 错误
- python - argparse 自定义操作 OOP