您可以使用chartr
RO替換字符
#Your string
x <- "ATGCTGATCGAGCTANATCGATCGGACTACY"
# Get all combinations of replacement strings
# paste together for chartr function
ex <- do.call(paste0, expand.grid(N = c("A", "T", "G", "C"), Y = c("C", "T")))
# koop through each combination , replacing the string
sapply(ex, chartr, old="NY", x=x)
# AC TC
# "ATGCTGATCGAGCTAAATCGATCGGACTACC" "ATGCTGATCGAGCTATATCGATCGGACTACC"
# GC CC
# "ATGCTGATCGAGCTAGATCGATCGGACTACC" "ATGCTGATCGAGCTACATCGATCGGACTACC"
# AT TT
# "ATGCTGATCGAGCTAAATCGATCGGACTACT" "ATGCTGATCGAGCTATATCGATCGGACTACT"
# GT CT
# "ATGCTGATCGAGCTAGATCGATCGGACTACT" "ATGCTGATCGAGCTACATCGATCGGACTACT"
更新了您的擴展的例子
# lookup table of replacements
lookup <- list(
A = 'A' ,
C = 'C' ,
G = 'G' ,
T = 'T',
W = c('A', 'T') ,
S = c('C', 'G') ,
M = c('A', 'C') ,
K = c('G', 'T'),
R = c('A', 'G') ,
Y = c('C', 'T') ,
B = c('C', 'G', 'T') ,
D = c('A', 'G', 'T') ,
H = c('A', 'C', 'T') ,
V = c('A', 'C', 'G') ,
N = c('A', 'C', 'G', 'T'))
# Get unique character that are in sequence
yourseq <- "ATGTTTGARCCACGYATHCCTAC" # example 1
uniq.char <- unique(strsplit(yourseq, "")[[1]])
# subset look up to only use characters found in sequence
# this will keep the exapnd.grid replacements more reasonable size
# find all combinations of these
# and paste together
ex <- do.call(expand.grid, lookup[uniq.char])
vec <- do.call(paste0, ex)
# Get all sequences
sapply(vec, chartr, old=paste(names(ex), collapse=""), x=yourseq)
'expand.grid'是你的朋友。祝你好運。 – 989