This is a test version of Biostars. For the public version, visit https://www.biostars.org.
dealing with insertion in a data set

Hello, I don't know if this is the best place to ask this question, but I will try. I have a data set with single nucleotide mutations, but I have some rows that contains insertion, something like this:

chr1  T  C     A
chr1  T  C+G   A 
chr1  T  C+T   T

I would like to store in one variable the mutations without insertions and in other the mutations with them. What I've done so far is:

xx <- x %>% filter(across(everything(), ~ !str_detect( "+")))

but it did not worked, Any clues? Thank you!

insertion grep dyplr

2 answers

Thank you for the feedback, What worked for me was

insertions <- x %>% filter_all(any_vars(str_detect(.,  "\\+")))

In base R:

# creating example dataframe
chr = (rep("Chr1",3))
refAllele = (rep("T",3))
altAllele = c("C", "C+G", "C+T")
otherAllele = c("A", "A", "T")
df = data.frame(chr = chr, refAllele=  refAllele, altAllele = altAllele, otherAllele= otherAllele)
# subsetting dataframe based on the length of the altAllele: Insertion variants have nchar more than 1 
noInsertionVar = df[nchar(df$altAllele) == 1,]
noInsertionVar
#chr refAllele altAllele otherAllele
#1 Chr1         T         C           A
insertionVar = df[nchar(df$altAllele) != 1,]
insertionVar
#chr refAllele altAllele otherAllele
#2 Chr1         T       C+G           A
#3 Chr1         T       C+T           T

Updating with tidyverse functions per the request in below comment:

insertionVar <- df %>% dplyr::filter(across(everything(), ~ str_detect(altAllele, "\\+")))
insertionVar
#chr refAllele altAllele otherAllele
#1 Chr1         T       C+G           A
#2 Chr1         T       C+T           T

noInsertionVar <- df %>% dplyr::filter(across(everything(), ~ !str_detect(altAllele, "\\+")))
#chr refAllele altAllele otherAllele
# 1 Chr1         T         C           A

I would like a dplyr approach, if possible. What worked for me is

non_insertion_var  <- df %>% filter(across(everything(), ~ !str_detect(., "\\+")))

However, for insertion doesn't work

insertion_var  <- df %>% filter(across(everything(), ~ str_detect(., "\\+")))

Any ideas what I'm doing wrong? Thanks!

I have updated the my answer. if you specifiy which column to look for the "+", your code work well.

Log in to answer this question.