I have an object of S4 class like below:
> gadem
Object of class 'gadem'
This object has the following slots:
motifs,pwm,consensus,align,name,seq,chr,start,end,strand,seqID,pos,pval,fastaHeader
> gadem[[1]] An object of class "motif" Slot "pwm": 1 2 3 4 5 6 7 8 9 10 11 A 0.3404 0 0.0000 0.6375 0.2723 0.3173 0 0.0002 0.3126 0 0.4969 C 0.4281 0 0.8708 0.1474 0.0767 0.1122 0 0.0000 0.0981 1 0.2558 G 0.1414 0 0.0000 0.0361 0.4153 0.5088 0 0.1134 0.0532 0 0.0000 T 0.0901 1 0.1292 0.1790 0.2357 0.0617 1 0.8864 0.5361 0 0.2473 Slot "consensus": [1] "mTCAnrTTwCm" Slot "alignList": [[1]] An object of class "align" Slot "seq": [1] "CTCAGGTTTCA" Slot "chr": [1] "chr12" Slot "start": [1] 29470324 Slot "end": [1] 29470423 Slot "strand": [1] "+" Slot "seqID": [1] 5239 Slot "pos": [1] 67 Slot "pval": [1] 1.862121e-09 Slot "fastaHeader": [1] 5239
[[2]] An object of class "align" Slot "seq": [1] "CTCAGGTTTCA" Slot "chr": [1] "chr18" Slot "start": [1] 4862453 Slot "end": [1] 4862571 Slot "strand": [1] "+" Slot "seqID": [1] 12645 Slot "pos": [1] 68 Slot "pval": [1] 1.862121e-09 Slot "fastaHeader": [1] 12645
From this object, I would like to generate a file with columns of Slot Seq | Slot chr | Slot start | Slot end | Slot strand | Slot seqID | Slot pos | Slot pval | Slot fastaHeader.
How can I generate and write such a .txt file from above S4 object?
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 ...
来源:https://stackoverflow.com/questions/8095595/how-to-split-and-write-to-a-file-for-s4-object-in-r