我尝试在R中使用类似PERL的正则表达式分割FASTA标头。
输入字符串的一些示例:
>P04259 SWISS-PROT:P04259 Tax_Id=9606 Gene_Symbol=KRT6B Keratin, type II cytoskeletal 6B
>ENSEMBL:ENSBTAP00000024146 (Bos taurus) similar to alpha-2-macroglobulin isoform 1
>ENSEMBL:ENSBTAP00000024462 (Bos taurus) 47 kDa protein;>ENSEMBL:ENSBTAP00000024466 (Bos taurus) 44 kDa protein
>Q3SX28 TREMBL:Q3SX28;Q5KR48 (Bos taurus) Tropomyosin 2
>P00761 SWISS-PROT:P00761|TRYP_PIG Trypsin - Sus scrofa (Pig).
我想获取信息
我建议的正则表达式采用R格式:
^(?:>.*?(?:ENSEMBL:|SWISS-PROT:|TREMBL:))([A-Z0-9.-]+)(?:.*?Gene_Symbol=(\\S+)|\\s+|;\\S+ |\\|\\S+)(.*?)(?:;>.*?|\\n)
并以pcre格式:
^(?:>.*?(?:ENSEMBL:|SWISS-PROT:|TREMBL:))([A-Z0-9.-]+)(?:.*?Gene_Symbol=(\S+)|\s|;\S+ |\|\S+)(.*?)(?:;>.*?|\n)
我在regex101.com上测试了正则表达式,结果正是我想要的。但是如果我在R中执行它,每个捕获组都包含完整的字符串。我认为不是每个条目都有基因符号的特殊情况会产生一些内部问题。也许这也是我在非捕获组中嵌套捕获组的问题。
> gsub(pattern = regex, replacement = "\\1", x = ">P04259 SWISS-PROT:P04259 Tax_Id=9606 Gene_Symbol=KRT6B Keratin, type II cytoskeletal 6B", perl = TRUE)
[1] ">P04259 SWISS-PROT:P04259 Tax_Id=9606 Gene_Symbol=KRT6B Keratin, type II cytoskeletal 6B"
> gsub(pattern = regex, replacement = "\\2", x = ">P04259 SWISS-PROT:P04259 Tax_Id=9606 Gene_Symbol=KRT6B Keratin, type II cytoskeletal 6B", perl = TRUE)
[1] ">P04259 SWISS-PROT:P04259 Tax_Id=9606 Gene_Symbol=KRT6B Keratin, type II cytoskeletal 6B"
> gsub(pattern = regex, replacement = "\\3", x = ">P04259 SWISS-PROT:P04259 Tax_Id=9606 Gene_Symbol=KRT6B Keratin, type II cytoskeletal 6B", perl = TRUE)
[1] ">P04259 SWISS-PROT:P04259 Tax_Id=9606 Gene_Symbol=KRT6B Keratin, type II cytoskeletal 6B"
所以最后我想要绿色块,红色块(如果可用)和橙色块作为结果。希望你能帮帮我。
答案 0 :(得分:1)
在在线正则表达式测试器中测试字符串时,您使用了多行字符串。正则表达式中的最后一个\n
与换行符匹配,因此您与>P04259 SWISS-PROT:P04259 Tax_Id=9606 Gene_Symbol=KRT6B Keratin, type II cytoskeletal 6B
匹配。
您只需要将\n
替换为字符串结尾锚点,以使其与gsub
一起使用:
x = ">P04259 SWISS-PROT:P04259 Tax_Id=9606 Gene_Symbol=KRT6B Keratin, type II cytoskeletal 6B"
gsub("^(?:>.*?(?:ENSEMBL:|SWISS-PROT:|TREMBL:))([A-Z0-9.-]+)(?:.*?Gene_Symbol=(\\S+)|\\s|;\\S+ |\\|\\S+)(.*?)(?:;>.*|$)", "\\1", x, perl = TRUE)
gsub("^(?:>.*?(?:ENSEMBL:|SWISS-PROT:|TREMBL:))([A-Z0-9.-]+)(?:.*?Gene_Symbol=(\\S+)|\\s|;\\S+ |\\|\\S+)(.*?)(?:;>.*|$)", "\\2", x, perl = TRUE)
gsub("^(?:>.*?(?:ENSEMBL:|SWISS-PROT:|TREMBL:))([A-Z0-9.-]+)(?:.*?Gene_Symbol=(\\S+)|\\s|;\\S+ |\\|\\S+)(.*?)(?:;>.*|$)", "\\3", x, perl = TRUE)
请参阅IDEONE demo
但是,由于您要提取文本,我建议您将正则表达式与 stringr 包中的str_match
一起使用:
> library(stringr)
> x = ">P04259 SWISS-PROT:P04259 Tax_Id=9606 Gene_Symbol=KRT6B Keratin, type II cytoskeletal 6B"
> str_match(x, "^(?:>.*?(?:ENSEMBL:|SWISS-PROT:|TREMBL:))([A-Z0-9.-]+)(?:.*?Gene_Symbol=(\\S+)|\\s|;\\S+ |\\|\\S+)(.*?)(?:;>.*|$)")
[,1] [,2] [,3]
[1,] ">P04259 SWISS-PROT:P04259 Tax_Id=9606 Gene_Symbol=KRT6B Keratin, type II cytoskeletal 6B" "P04259" "KRT6B"
[,4]
[1,] " Keratin, type II cytoskeletal 6B"