2

I have data frame with a column:

nf1$Info = AC=1;AF=0.500;AN=2;BaseQRankSum=-1.026e+00;ClippingRankSum=-1.026e+00;DP=4;ExcessHet=3.0103;FS=0.000;MLEAC=1;MLEAF=0.500;MQ=28.25;MQRankSum=-1.026e+00;QD=10.18;ReadPosRankSum=1.03;SOR=0.693

I'm trying to extract a specific value from this column.

For e.g. I'm interested in "MQRankSum" and I used:

str_extract(nf1$Info,"[MQRankSum]+=[:punct:]+[0-9]+[.]+[0-9]+")

It returns value for BaseQRankSum instead of MQRankSum.

4
  • 1
    Why wrap char sequences with [...]? Remove them. "MQRankSum=[^;]+" Jul 2, 2018 at 9:12
  • Thanks you. it worked.
    – beginner
    Jul 2, 2018 at 9:14
  • 1
    Is this from a vcf file? How did you read the file into R? Maybe use vcfR package?
    – zx8754
    Jul 2, 2018 at 9:15
  • This is an annotated vcf file. I prefer filtering the .txt file. I am aware of the vcfR.
    – beginner
    Jul 2, 2018 at 9:17

2 Answers 2

4

Including characters into square brackets creates a character class matching any of the defined characters, so [yes]+ matches yyyyyyyyy, eyyyyss, etc.

What you want to do is to match a word MQRankSum, =, and then any chars other than ;:

str_extract(nf1$Info,"MQRankSum=[^;]+")

If you want to exlcude MQRankSum= from the match, use a lookbehind:

str_extract(nf1$Info,"(?<=MQRankSum=)[^;]+")
                      ^^^^^^^^^^^^^^^

The (?<=MQRankSum=) positive lookbehind will make sure there is MQRankSum= text immediately to the left of the current location, and only after that will match 1 or more chars other than ;.

1
  • 1
    Really helpful explanation.
    – beginner
    Jul 2, 2018 at 9:26
1

We could split INFO column into multiple columns then extract desired column:

# dummy data
df1 <- data.frame(x = 1:3,
                  info = c("AC=1;AF=0.500;MQRankSum=2;BaseQRankSum=-1.026e+00;ClippingRankSum=-1.026e+00;",
                           "AC=1;AF=0.500;MQRankSum=2;ClippingRankSum=-1.026e+00;DP=4;",
                           "AN=2;BaseQRankSum=-1.026e+00;"),
                  stringsAsFactors = FALSE)

# split INFO into seperate columns
df1_info <- data.table::rbindlist(
  lapply(strsplit(df1$info, ";|="), function(i)
    setNames(data.frame(t(as.numeric(i[ c(FALSE, TRUE) ]))), i[ c(TRUE, FALSE) ])
    ),
  fill = TRUE)

df1_info
#    AC  AF MQRankSum BaseQRankSum ClippingRankSum DP AN
# 1:  1 0.5         2       -1.026          -1.026 NA NA
# 2:  1 0.5         2           NA          -1.026  4 NA
# 3: NA  NA        NA       -1.026              NA NA  2

# extract required column 
df1_info$BaseQRankSum
# [1] -1.026     NA -1.026

VCF INFO standard:

Various site-level annotations. The annotations contained in the INFO field are represented as tag-value pairs, where the tag and value are separated by an equal sign, ie =, and pairs are separated by colons, ie ; as in this example:
MQ=99.00;MQ0=0;QD=17.94.

2
  • Very informative. From the above example I mentioned where I have more values in the Info column than you mentioned in you example, how should i split that in to separate column. Eg Info: "AC=1;AF=0.500;AN=2;BaseQRankSum=-1.026e+00;ClippingRankSum=-1.026e+00;DP=4;ExcessHet=3.0103;FS=0.000;MLEAC=1;MLEAF=0.500;MQ=28.25;MQRankSum=-1.026e+00;QD=10.18;ReadPosRankSum=1.03;SOR=0.693"
    – beginner
    Jul 2, 2018 at 13:35
  • @beginner above code should work with any number of values. If it doesn't please edit your post and provide reproducible example: dput(head(nf1$Info))
    – zx8754
    Jul 2, 2018 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.