2011-11-11 113 views
1

我有S4類的對象象下面這樣:如何分割,並寫入了S4對象的文件中的R

> 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 

從這個對象,我想生成一個文件與時隙Seq |列插槽chr |插槽開始|插槽結束|插槽鏈|插槽seqID |插槽pos |插槽pval |插槽fastaHeader。

如何從S4對象生成和寫入這樣的.txt文件?

+1

可以提取使用'@'或'槽(對象,廣告位名稱)'槽。在這裏稍微介紹一下S4對象:http://cran.r-project.org/doc/contrib/Genolini-S4tutorialV0-5en.pdf –

+4

更好的做法是使用軟件包作者提供的方法,或許在' class?gadem',來訪問數據。 –

回答

1

帶有這些插槽的gadem對象中的項目位於alignList插槽中。在rGADEM軟件包中似乎沒有很多提取器函數,所以@Martin Morgans評論是正確的,但在這裏並不完全有幫助。我從showMethods(classes="gadem")得到很多幫助。如果你想顯示的「gadem」的alignList插槽的第一對準一流的對象,你可以鍵入:

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

你可以得到這樣的對象的數量與:

length(gadem[[1]]@alignList) 

如果你想將它們保存到一個二進制R檔,這樣他們可以load -ed在以後的時間,你會使用類似:

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

使用例如在文章「發現和分析DNA序列圖案:rGADEM軟件包「。由Arnaud所有權和拉斐爾聖哥達將有可能對alignList項目提取到一個數據幀定期與此循環:

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 ...