从向量中提取基因名称和 ID 号

Extracting gene name and ID number from a vector

我可以在 R 中使用什么 gsub 函数从如下所示的向量中获取基因名称和 ID 号?

head(colnames(cn), 20)
 [1] "A1BG (1)"               "NAT2 (10)"              "ADA (100)"              "CDH2 (1000)"            "AKT3 (10000)"           "GAGE12F (100008586)"   
 [7] "RNA5-8SN5 (100008587)"  "RNA18SN5 (100008588)"   "RNA28SN5 (100008589)"   "LINC02584 (100009613)"  "POU5F1P5 (100009667)"   "ZBTB11-AS1 (100009676)"
[13] "MED6 (10001)"           "NR2E3 (10002)"          "NAALAD2 (10003)"        "DUXB (100033411)"       "SNORD116-1 (100033413)" "SNORD116-2 (100033414)"
[19] "SNORD116-3 (100033415)" "SNORD116-4 (100033416)"

首先,以后请使用dput()命令分享您的数据。有关详细信息,请参阅 this

其次,这是提取您需要的部分的一种解决方案:

library(tidyverse)

g<-c("A1BG (1)","NAT2 (10)","ADA (100)"  , "RNA18SN5 (100008588)",   "RNA28SN5 (100008589)")

gnumber<-stringr::str_extract(g,"(?=\().*?(?<=\))")
gnumber     

gname<-stringr::str_extract(g, "[:alpha:]+")
gname

# or, to get the whole first word:
gname<-stringr::word(g,1,1)
gname

1) 假设最后注释中给出的输入 s 我们可以使用 read.table 指定字段由 ( 和 ) 分隔是注释特点。我们还去除字段周围的白色 space 并给出有意义的列名。没有使用包。

DF <- read.table(text = s, sep = "(", comment.char = ")", 
  strip.white = TRUE, col.names = c("Gene", "Id"))
DF

给出这个数据框,所以 DF$Gene 是基因,DF$Id 是 id。

         Gene        Id
1        A1BG         1
2        NAT2        10
3         ADA       100
4        CDH2      1000
5        AKT3     10000
6     GAGE12F 100008586
7   RNA5-8SN5 100008587
8    RNA18SN5 100008588
9    RNA28SN5 100008589
10  LINC02584 100009613
11   POU5F1P5 100009667
12 ZBTB11-AS1 100009676
13       MED6     10001
14      NR2E3     10002
15    NAALAD2     10003
16       DUXB 100033411
17 SNORD116-1 100033413
18 SNORD116-2 100033414
19 SNORD116-3 100033415
20 SNORD116-4 100033416

2) 上面的一个变体是先去掉括号,然后读取它得到相同的结果。请注意,chartr 的第二个参数包含两个 space,因此每个括号都被转换为 space.

read.table(text = chartr("()", "  ", s), col.names = c("Gene", "Id"))

备注

Lines <-  '[1] "A1BG (1)"               "NAT2 (10)"              "ADA (100)"              "CDH2 (1000)"            "AKT3 (10000)"           "GAGE12F (100008586)"   
 [7] "RNA5-8SN5 (100008587)"  "RNA18SN5 (100008588)"   "RNA28SN5 (100008589)"   "LINC02584 (100009613)"  "POU5F1P5 (100009667)"   "ZBTB11-AS1 (100009676)"
[13] "MED6 (10001)"           "NR2E3 (10002)"          "NAALAD2 (10003)"        "DUXB (100033411)"       "SNORD116-1 (100033413)" "SNORD116-2 (100033414)"
[19] "SNORD116-3 (100033415)" "SNORD116-4 (100033416)" '

L <- Lines |>
  textConnection() |>
  readLines() |>
  gsub(pattern = "\[\d+\]", replacement = "")
s <- scan(text = L, what = "")

所以 s 看起来像这样:

> dput(s)
c("A1BG (1)", "NAT2 (10)", "ADA (100)", "CDH2 (1000)", "AKT3 (10000)", 
"GAGE12F (100008586)", "RNA5-8SN5 (100008587)", "RNA18SN5 (100008588)", 
"RNA28SN5 (100008589)", "LINC02584 (100009613)", "POU5F1P5 (100009667)", 
"ZBTB11-AS1 (100009676)", "MED6 (10001)", "NR2E3 (10002)", "NAALAD2 (10003)", 
"DUXB (100033411)", "SNORD116-1 (100033413)", "SNORD116-2 (100033414)", 
"SNORD116-3 (100033415)", "SNORD116-4 (100033416)")