How to split and write to a file for S4 object in R

跟風遠走 提交于 2019-12-04 18:56:17

The items in a gadem-object with those slots are in the alignList slot. There do not seem to be very many extractor functions described in the rGADEM package, so @Martin Morgans comments are correct but not entirely helpful here. I didn't get much help from showMethods( classes="gadem"). If you wanted to display the first align-class object in the alignList slot of 'gadem' you would type:

gadem[[1]]@alignList[[1]]

You can get the number of such objects with:

length(gadem[[1]]@alignList)

If you wanted to save them to a binary R file so that they could be load-ed at a later time, you would use something like:

algns <- gadem[[1]]@alignList
save(algns, file="testgadem.rdta")

Using the example in the article "Discovering and analyzing DNA sequence motifs: The rGADEM package." by Arnaud Droit and Raphael Gottardo it would be possible to extract the alignList items into a regular dataframe with this loop:

dfrm <- data.frame( Seq=rep(NA, 54), chr=NA, start =NA, end =NA, strand=NA, 
                    seqID=NA, pos=NA, pval =NA, fastaHeader=NA)  

for (i in 1:54) {  dfrm[i, "Seq"] <- gadem[[1]]@alignList[[i]]@seq
               dfrm[i, "chr"] <- gadem[[1]]@alignList[[i]]@chr
              dfrm[i, "start"] <- gadem[[1]]@alignList[[i]]@start
              dfrm[i, "end"] <- gadem[[1]]@alignList[[i]]@end
              dfrm[i, "start"] <- gadem[[1]]@alignList[[i]]@start
              dfrm[i, "strand"] <- gadem[[1]]@alignList[[i]]@strand
              dfrm[i, "seqID"] <- gadem[[1]]@alignList[[i]]@seqID
              dfrm[i, "pos"] <- gadem[[1]]@alignList[[i]]@pos
              dfrm[i, "pval"] <- gadem[[1]]@alignList[[i]]@pval
              dfrm[i, "fastaHeader"] <- gadem[[1]]@alignList[[i]]@fastaHeader}
 str(dfrm)
#--------------------
'data.frame':   54 obs. of  9 variables:
 $ Seq        : chr  "CTGTGTCAACAG" "CTGTGTAAACAC" "CTGAGTCAACAC" "GTGAGTCAACAG" ...
 $ chr        : chr  "chr1" "chr1" "chr1" "chr1" ...
 $ start      : int  202320096 21451068 22547577 117197889 188010599 36725231 144254018 35024860 35024860 43552181 ...
 $ end        : int  202320297 21451269 22547778 117198090 188010800 36725432 144254219 35025061 35025061 43552382 ...
 $ strand     : chr  "+" "-" "-" "-" ...
 $ seqID      : int  23 26 9 8 45 15 30 50 50 38 ...
 $ pos        : int  93 98 121 82 183 160 142 21 117 104 ...
 $ pval       : num  2.48e-07 4.44e-07 5.68e-07 6.85e-07 1.31e-06 ...
 $ fastaHeader: int  23 26 9 8 45 15 30 50 50 38 ...
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!