提取特定词

时间:2019-03-05 11:56:20

标签: r regex bioinformatics

我有以下对象列表:

  dput(head(annotations))
    structure(list(X1 = c("KQ415659.1", "KQ415659.1", "KQ415659.1", 
    "KQ415659.1", "KQ415659.1", "KQ415659.1"), X2 = c("Genbank", 
    "Genbank", "Genbank", "Genbank", "Genbank", "Genbank"), X3 = c("exon", 
    "exon", "exon", "exon", "exon", "exon"), X4 = c(2986, 8779, 12123, 
    14982, 15303, 15780), X5 = c(3040, 8886, 12182, 15050, 15387, 
    15844), X6 = c(".", ".", ".", ".", ".", "."), X7 = c("+", "+", 
    "+", "+", "+", "+"), X8 = c(".", ".", ".", ".", ".", "."), X9 = c("transcript_id \"rna0\"; gene_id \"gene0\"; gene_name \"ICBIM_22028242mg\";", 
    "transcript_id \"rna0\"; gene_id \"gene0\"; gene_name \"ICBIM_22028243mg\";", 
    "transcript_id \"rna0\"; gene_id \"gene0\"; gene_name \"ICBIM_22028244mg\";", 
    "transcript_id \"rna0\"; gene_id \"gene0\"; gene_name \"ICBIM_22028245mg\";", 
    "transcript_id \"rna0\"; gene_id \"gene0\"; gene_name \"ICBIM_22028246mg\";", 
    "transcript_id \"rna0\"; gene_id \"gene0\"; gene_name \"ICBIM_22028247mg\";"
    )), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
    -6L))

我想从所有字符串中提取以“ ICBIM”开头并以“ mg”结尾的单词。

# A tibble: 6 x 1
  X9                                                                          
<chr>
        1 "transcript_id \"rna0\"; gene_id \"gene0\"; gene_name \"ICBIM_22028241mg\";"
        2 "transcript_id \"rna0\"; gene_id \"gene0\"; gene_name \"ICBIM_22028242mg\";"
        3 "transcript_id \"rna0\"; gene_id \"gene0\"; gene_name \"ICBIM_22028243mg\";"
        4 "transcript_id \"rna0\"; gene_id \"gene0\"; gene_name \"ICBIM_22028244mg\";"
        5 "transcript_id \"rna0\"; gene_id \"gene0\"; gene_name \"ICBIM_22028245mg\";"
        6 "transcript_id \"rna0\"; gene_id \"gene0\"; gene_name \"ICBIM_22028246mg\";"

4 个答案:

答案 0 :(得分:2)

R中的正则表达式匹配有点奇怪。您有6个功能(grepgreplsubgsubregexprgregeprregexec)与东西,但没有一个返回实际的字符串。您可以获得的最接近的信息是regexpr中匹配位置和长度的列表,可以将其传递给regmatches以获取字符串。

这就是我的做法:

your_data = c("transcript_id \"rna0\"; gene_id \"gene0\"; gene_name \"ICBIM_22028241mg\";", "transcript_id \"rna0\"; gene_id \"gene0\"; gene_name \"ICBIM_22028242mg\";")
matches = regexpr("ICBIM.*mg", your_data)
regmatches(your_data, matches)
[1] "ICBIM_22028241mg" "ICBIM_22028242mg"

答案 1 :(得分:1)

您可以在sub中使用此正则表达式:

sub(".*\\b(ICBIM.+mg)\\b.*", "\\1", annotations$X9)
# [1] "ICBIM_22028242mg" "ICBIM_22028243mg" "ICBIM_22028244mg" "ICBIM_22028245mg"
# [5] "ICBIM_22028246mg" "ICBIM_22028247mg"

在这里,\\b表示单词边框。

答案 2 :(得分:0)

  

以“ ICBIM”开头,以“ mg”结尾

ValidationError(PersistentVolume.spec.awsElasticBlockStore): missing required field "volumeID" in io.k8s.api.core.v1.AWSElasticBlockStoreVolumeSource;

See the regex demo

答案 3 :(得分:0)

假设行格式相同,但没有 regex

substring(annotations$X9, 51, 66)
# [1] "ICBIM_22028242mg" "ICBIM_22028243mg" "ICBIM_22028244mg" "ICBIM_22028245mg" "ICBIM_22028246mg" "ICBIM_22028247mg"

read.fwf(textConnection(annotations$X9), widths = c(50, 16), stringsAsFactors = FALSE)[, 2]
# [1] "ICBIM_22028242mg" "ICBIM_22028243mg" "ICBIM_22028244mg" "ICBIM_22028245mg" "ICBIM_22028246mg" "ICBIM_22028247mg"