This is a test version of Biostars. For the public version, visit https://www.biostars.org.
R partial string matching with constraints

I would like to find matches between two list of AA strings so that 1 of these 3 rules is matched :

1) query string has to be a substring of subject string

2) query string has to end with the same pattern the subject string starts (any number of characters)

3) query string has to start with the same pattern the subject string end (any number of characters)

Example of what constitute a match or not

r grep pattern matching

What have you tried? Right now, it looks like you're asking us to solve your problem, and that is not a worthwhile use of the community's time.

1 answer

Probably there is a more concise way but this works:

queries=c('GTASQ','QSD','DRARTK','GTASQW')
subject=c('ASQSDRA')

lapply(queries,function(X) 
#peptides have to fall completely into sequence
grepl(X,subject) |
# or start with the sequence end  
any(unlist(lapply(c(nchar(X):1),function(i) 
    grepl(paste(c('^',str_sub(X,-i,-1)),collapse=''),subject) ))) |
# or end with the sequence start  
any(unlist(lapply(c(1:nchar(X)),function(i)
    grepl(paste(c(str_sub(X,1,i),"$"),collapse=''),subject) )))   
)

Log in to answer this question.