我有500万个序列(探针具体)如下.我需要从每个字符串中提取名称.
这里的名字是1007_s_at:123:381,10073_s_at:128:385等等..
我正在使用lapply函数,但它花费了太多时间.我还有其他几个类似的文件.你会建议一个更快的方法来做到这一点.
nm = c(
"probe:HG-Focus:1007_s_at:123:381; Interrogation_Position=3570; Antisense;",
"probe:HG-Focus:1007_s_at:128:385; Interrogation_Position=3615; Antisense;",
"probe:HG-Focus:1007_s_at:133:441; Interrogation_Position=3786; Antisense;",
"probe:HG-Focus:1007_s_at:142:13; Interrogation_Position=3878; Antisense;" ,
"probe:HG-Focus:1007_s_at:156:191; Interrogation_Position=3443; Antisense;",
"probe:HTABC:1007_s_at:244:391; Interrogation_Position=3793; Antisense;")
extractProbe <- function(x) sub("probe:", "", strsplit(x, ";", fixed=TRUE)[[1]][1], ignore.case=TRUE)
pr = lapply(nm, extractProbe)
Run Code Online (Sandbox Code Playgroud)
产量
1007_s_at:123:381
1007_s_at:128:385
1007_s_at:133:441
1007_s_at:142:13
1007_s_at:156:191
1007_s_at:244:391
Run Code Online (Sandbox Code Playgroud)
使用正则表达式:
sub("probe:(.*?):(.*?);.*$", "\\2", nm, perl = TRUE)
Run Code Online (Sandbox Code Playgroud)
一点解释:
. 意思是"任何角色"..* 表示"任意数量的字符"..*? 意思是"任意数量的字符,但不要贪婪.\\1,\\2等$ 表示行的末尾(或字符串).所以在这里,模式匹配整行,并通过两个来捕获两件事(.*?):HG-Focus你不想要的(或其他)\\1和你的id \\2.通过将替换设置为\\2,我们将使用您的id有效地替换整个字符串.
我现在意识到没有必要抓住第一件事,所以这也会起作用:
sub("probe:.*?:(.*?);.*$", "\\1", nm, perl = TRUE)
Run Code Online (Sandbox Code Playgroud)