-
Notifications
You must be signed in to change notification settings - Fork 0
/
getseqs.R
79 lines (61 loc) · 1.2 KB
/
getseqs.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#Converts a nonnegative decimal integer to base b
#
#Args
#num A nonnegative decimal integer
#b THe desired base
#
#Output
#A vector of the integers 0, 1, ..., b-1 representing the digits of the base b number
#
#Note; no error checking of inputs
#
dec_to_baseb<-function(num,b)
{
res<-(num %% b)
num<-(num-res)/b
while (num!=0)
{
h<-(num %% b)
res<-c(h,res)
num<-(num-h)/b
}
return(res)
}
#Given a length, finds all sequences of A, T, G, C of that length
#
#Args
#len A single number
#
#Output
#A vector of all of 'em
#
#Note: no error checking
#
getseqs<-function(len)
{
bases<-c("A","C","G","T")
res<-c()
for (counter in 0:(4^len-1))
{
h<-dec_to_baseb(counter,4)
if (length(h)<len)
{
h<-c(rep(0,len-length(h)),h)
}
res<-c(res,paste0(bases[h+1],collapse=""))
}
return(res)
}
#y1<-getseqs(2)
# Input and Outputs are in same format as of getseqs function
my_getseqs<-function(len){
bp_list<-vector("list",length = len) # create an empty list
bp<-c("A","C","G","T")
for(i in c(1:len)){
bp_list[[i]]<-bp
}
x<-expand.grid(bp_list)
res<-apply( x[,1:len] , 1, paste , collapse = "" )
return(res)
}
#my_getseqs(4)