Friday, 6 September 2013

R package, trying to lapply function to Biostring Views?

R package, trying to lapply function to Biostring Views?

First of all, I want to apologize if I am asking stupid questions, I have
been only working for 2 days with R but I have to do it for my job.
I am trying to apply a function (calculate a product of matrices indexed
by the name of a base pair) and I am using Biostrings to manage big
sequences of DNA. I have to "window" these sequences into shorter
sequences of 147 bp and apply a function to these shorter sequences.
So here's a MWE of what my code should look like (all this code is mine so
it might be wrong, and I still do not understand the difference between =
and <-):
MyMWEf <- function(seq, MAT){
#MAT has colnames and rownames "BPs", for example "AG", "TA", etc.
BPs = Views(seq, start = 1:(length(seq)-1), width = 2)
BPs = unlist(lapply(BPs,toString))
#With this I get a list of bps ("AG" "TA" etc.)
AVGs = AVG[BPs, ]
res=sum(AVGs)
}
MyMWE <- function(seq, MAT){
seq <- Biostrings::DNAString(seq)
windows <- Views(seq, start = 1:(length(seq)-146), width = 147)
res = unlist(lapply(windows, MyMWEf, MAT))
}
Now, if I build a package with this and install it and run it, it gives me
the following error:
error in evaluating the argument 'x' in selecting a method for function
'unlist': Error in as.list.default(X) :
No method to coerce this S4 class to a vector
So I started debugging, and found out that the problem comes in the lapply
actually: It says error during wrapup. More specifically, it seems that
the error comes when it tries to do as.list(windows). However, if I do the
same but inside the console there is no error! I can lapply without
problem for example lapply(windows,length) or even
lapply(windows,MyMWEf,MAT) and everything works just fine (if I defined
the function MyMWEf in the console), also if inside the console I write
`as.list(windows)' it works.
If anybody could give me some insight in what am I doing wrong I would
very much appreciate it!
Thank you
edited after narrowing down a bit (as.list problem).

No comments:

Post a Comment