Giacomo Bregni
Giacomo Bregni

Reputation: 11

How do I perform a permutation in R with fixed objects in a nucleotide string?

I need to see all possible variations of these nucleotide string: GCGCTAAGCAAAAAACAT with two caveats: 1. everything that is not Bold is fixed 2. Bold can be either A or C

I used the permutations function:

library(gtools)
x <- c('a', 'c')
permutations(n=2, r=8, v=x, repeats.allwed=T)

It works, the problem is that I would need the list of results to be in the nucleotide sequence, or else I will spend more time copying and pasting the results in the nucleotide sequence than doing the permutation by hand. Thanks a lot!!! Giacomo

Upvotes: 1

Views: 85

Answers (2)

Roland
Roland

Reputation: 132706

I would use sprintf:

library(gtools)
x <- c('a', 'c')
p <- permutations(n=2, r=8, v=x, repeats.allowed=T)

#split columns
p <- asplit(p, 2)

#insert into format string
do.call(sprintf, c(p, fmt = "GCGCT%s%sGC%s%s%s%s%s%sCAT"))
#[1] "GCGCTaaGCaaaaaaCAT" "GCGCTaaGCaaaaacCAT" "GCGCTaaGCaaaacaCAT" "GCGCTaaGCaaaaccCAT" "GCGCTaaGCaaacaaCAT" ...

You might want to create the permutations from upper-case letters.

Upvotes: 2

jlesuffleur
jlesuffleur

Reputation: 1253

Here is a solution, resulting in a matrix with each line being a permutation

library(gtools)
x <- c('A', 'C')
perms <- permutations(n = 2, r = 8, v = x, repeats.allowed = TRUE)

nucleotid <- c("G", "C", "G", "C", "T", "A",
               "A", "G", "C", "A", "A", "A", 
               "A", "A", "A", "C", "A", "T") 

# Looping throught each permutation to create the string
result <- apply(perms, 1, function(x){
  y <- nucleotid
  y[c(6,7,10:15)] <- x
  y
})
t(result)

Upvotes: 0

Related Questions