Search code examples
rmutation

separating specific strings from HGVS format in R


I'm trying to separate specific strings that comes before and after ">" sign from HGVS short mutational format, examples as follow:

"p.1258_1259EE>E"       "p.286_287RR>R"         "p.57_58KK>K"           "p.287_288AA>A"

What I want to achieve is say from the first string "p.1258_1259EE>E", I can get the following:

starting_position    end_position    initial_aa    mutant_aa
1258                 1259            EE            E
286                  287             RR            R
57                   58              KK            K

Seems a little more complicated than I initially thought. Hope someone here can help. Thanks, Tongtong


Solution

  • If the patterns are the same, a base R option would be to format the string with sub to introduce a common sep and use it in read.csv

    df1 <- read.csv(text= sub("^[^0-9]+([0-9]+)_([0-9]+)([A-Z]+)>([A-Z]+)", 
        "\\1,\\2,\\3,\\4", v1), header = FALSE, stringsAsFactors = FALSE, 
        col.names = c('starting_position', 'end_position', 'initial_aa', 'mutant_aa'))
    df1
    #   starting_position end_position initial_aa mutant_aa
    #1              1258         1259         EE         E
    #2               286          287         RR         R
    #3                57           58         KK         K
    #4               287          288         AA         A
    

    data

    v1 <- c("p.1258_1259EE>E", "p.286_287RR>R", "p.57_58KK>K",  "p.287_288AA>A")