Converting an S4 object into a dataframe in R
1
0
Entering edit mode
3.3 years ago
accibio ▴ 20

I have an S4 object named 'res' which I got while using an R package called RDAVIDWebService. I can't seem to find a way to convert this object into a dataframe in R.

I tried using the function 'as.data.frame(res)' but it throws this error:

> as.data.frame(res)
Error in as.data.frame.default(res) : 
cannot coerce class ‘structure("DAVIDFunctionalAnnotationTable", package = "RDAVIDWebService")’ to a data.frame

The structure of the object looks like this:

> str(res)
Formal class 'DAVIDFunctionalAnnotationTable' [package "RDAVIDWebService"] with 4 slots
..@ Genes     :'data.frame':    3011 obs. of  3 variables:
Formal class 'DAVIDGenes' [package "RDAVIDWebService"] with 5 slots
.. .. ..@ .Data    :List of 3
.. .. .. ..$ : chr [1:3011] "22574630" "3544383" "3544385" "3544382" ...
.. .. .. ..$ : chr [1:3011] "1,2-Dihydroxy-3-keto-5-methylthiopentene dioxygenase, 
putative(LPMP_204190)" "10 kDa heat shock protein(Tc00.1047053508209.100)" "10 kDa heat shock 
protein(Tc00.1047053508209.120)" "10 kDa heat shock protein(Tc00.1047053508209.90)" ...
.. .. .. ..$ : Factor w/ 11 levels "Leishmania braziliensis MHOM/BR/75/M2904",..: 6 10 10 10 
10 10 10 2 6 6 ...
.. .. ..@ names    : chr [1:3] "ID" "Name" "Species"
.. .. ..@ row.names: chr [1:3011] "1" "2" "3" "4" ...
.. .. ..@ .S3Class : chr "data.frame"
.. .. ..@ type     : chr "Gene List Report"
..@ Dictionary:List of 10
.. ..$ COG_ONTOLOGY    :'data.frame':   18 obs. of  2 variables:
.. .. ..$ ID  : chr [1:18] "Translation, ribosomal structure and biogenesis" "Lipid 
metabolism" "Cell division and chromosome partitioning" "General function prediction only" ...
.. .. ..$ Term: chr [1:18] "" "" "" "" ...
.. ..$ GOTERM_BP_DIRECT:'data.frame':   215 obs. of  2 variables:
.. .. ..$ ID  : chr [1:215] "GO:0006457" "GO:0051603" "GO:0008152" "GO:0006412" ...
.. .. ..$ Term: chr [1:215] "protein folding" "proteolysis involved in cellular protein 
catabolic process" "metabolic process" "translation" ...
.. ..$ GOTERM_CC_DIRECT:'data.frame':   84 obs. of  2 variables:
.. .. ..$ ID  : chr [1:84] "GO:0005737" "GO:0016021" "GO:0005634" "GO:0005839" ...
.. .. ..$ Term: chr [1:84] "cytoplasm" "integral component of membrane" "nucleus" "proteasome 
core complex" ...
.. ..$ GOTERM_MF_DIRECT:'data.frame':   222 obs. of  2 variables:
.. .. ..$ ID  : chr [1:222] "GO:0010309" "GO:0018580" "GO:0051213" "GO:0004298" ...
.. .. ..$ Term: chr [1:222] "acireductone dioxygenase [iron(II)-requiring] activity" 
"nitronate monooxygenase activity" "dioxygenase activity" "threonine-type endopeptidase 
activity" ...
.. ..$ INTERPRO        :'data.frame':   695 obs. of  2 variables:
.. .. ..$ ID  : chr [1:695] "IPR004313" "IPR011051" "IPR014710" "IPR011032" ...
.. .. ..$ Term: chr [1:695] "Acireductone dioxygenase ARD family" "RmlC-like cupin domain" 
"RmlC-like jelly roll fold" "GroES-like" ...
.. ..$ KEGG_PATHWAY    :'data.frame':   363 obs. of  2 variables:
.. .. ..$ ID  : chr [1:363] "ldo00071" "ldo00280" "ldo01100" "lmi00280" ...
.. .. ..$ Term: chr [1:363] "Fatty acid degradation" "Valine, leucine and isoleucine 
degradation" "Metabolic pathways" "Valine, leucine and isoleucine degradation" ...
.. ..$ PIR_SUPERFAMILY :'data.frame':   44 obs. of  2 variables:
.. .. ..$ ID  : chr [1:44] "PIRSF000868" "PIRSF002144" "PIRSF002134" "PIRSF002122" ...
.. .. ..$ Term: chr [1:44] "14-3-3 protein" "ribosomal protein, S19p/S19a/S15e/organellar S19 
types" "ribosomal protein, S13p/S13a/S18e/organellar S13 types" "ribosomal protein, 
S7p/S7a/S5e/organellar S7 types" ...
.. ..$ SMART           :'data.frame':   90 obs. of  2 variables:
.. .. ..$ ID  : chr [1:90] "SM00883" "SM00101" "SM01386" "SM01387" ...
.. .. ..$ Term: chr [1:90] "SM00883" "14_3_3" "SM01386" "SM01387" ...
.. ..$ UP_KEYWORDS     :'data.frame':   116 obs. of  2 variables:
.. .. ..$ ID  : chr [1:116] "Coiled coil" "Complete proteome" "Dioxygenase" "Oxidoreductase" 
...
.. .. ..$ Term: chr [1:116] "" "" "" "" ...
.. ..$ UP_SEQ_FEATURE  :'data.frame':   13 obs. of  2 variables:
.. .. ..$ ID  : chr [1:13] "chain:60S ribosomal protein L18" "chain:Probable eukaryotic 
initiation factor 4A" "domain:Helicase ATP-binding" "domain:Helicase C-terminal" ...
.. .. ..$ Term: chr [1:13] "" "" "" "" ...
..@ Membership:List of 10
.. ..$ COG_ONTOLOGY    : logi [1:3011, 1:18] FALSE FALSE FALSE FALSE FALSE FALSE ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : NULL
.. .. .. ..$ : chr [1:18] "Translation, ribosomal structure and biogenesis" "Lipid metabolism" 
"Cell division and chromosome partitioning" "General function prediction only" ...
.. ..$ GOTERM_BP_DIRECT: logi [1:3011, 1:215] FALSE TRUE TRUE TRUE TRUE TRUE ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : NULL
.. .. .. ..$ : chr [1:215] "GO:0006457" "GO:0051603" "GO:0008152" "GO:0006412" ...
.. ..$ GOTERM_CC_DIRECT: logi [1:3011, 1:84] FALSE TRUE TRUE TRUE TRUE TRUE ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : NULL
.. .. .. ..$ : chr [1:84] "GO:0005737" "GO:0016021" "GO:0005634" "GO:0005839" ...
.. ..$ GOTERM_MF_DIRECT: logi [1:3011, 1:222] TRUE FALSE FALSE FALSE FALSE FALSE ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : NULL
.. .. .. ..$ : chr [1:222] "GO:0010309" "GO:0018580" "GO:0051213" "GO:0004298" ...
.. ..$ INTERPRO        : logi [1:3011, 1:695] TRUE FALSE FALSE FALSE FALSE FALSE ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : NULL
.. .. .. ..$ : chr [1:695] "IPR004313" "IPR011051" "IPR014710" "IPR011032" ...
.. ..$ KEGG_PATHWAY    : logi [1:3011, 1:363] FALSE FALSE FALSE FALSE FALSE FALSE ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : NULL
.. .. .. ..$ : chr [1:363] "ldo00071" "ldo00280" "ldo01100" "lmi00280" ...
.. ..$ PIR_SUPERFAMILY : logi [1:3011, 1:44] FALSE FALSE FALSE FALSE FALSE FALSE ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : NULL
.. .. .. ..$ : chr [1:44] "PIRSF000868" "PIRSF002144" "PIRSF002134" "PIRSF002122" ...
.. ..$ SMART           : logi [1:3011, 1:90] FALSE TRUE TRUE TRUE TRUE TRUE ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : NULL
.. .. .. ..$ : chr [1:90] "SM00883" "SM00101" "SM01386" "SM01387" ...
.. ..$ UP_KEYWORDS     : logi [1:3011, 1:116] TRUE FALSE FALSE FALSE FALSE FALSE ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : NULL
.. .. .. ..$ : chr [1:116] "Coiled coil" "Complete proteome" "Dioxygenase" "Oxidoreductase" 
...
.. ..$ UP_SEQ_FEATURE  : logi [1:3011, 1:13] FALSE FALSE FALSE FALSE FALSE FALSE ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : NULL
.. .. .. ..$ : chr [1:13] "chain:60S ribosomal protein L18" "chain:Probable eukaryotic 
initiation factor 4A" "domain:Helicase ATP-binding" "domain:Helicase C-terminal" ...
..@ type      : chr "Functional Annotation Table"

Also, is there a generalized way of converting any S4 object into a dataframe without caring about the data inside the object? This is important because the S4 objects I'm fetching with this R package could have varying number of lists/variables/characters inside each of the 4 slots(i.e. @Genes, @Dictionary, @Membership and @type).

dataframe r S4 • 7.3k views
ADD COMMENT
0
Entering edit mode

Also, is there a generalized way of converting any S4 object into a dataframe without caring about the data inside the object?

The conversion won't really work unless somebody wrote a method for it. One thing you could try is check whether RDAVIDWebService has a function to write the data out to file, and then use that as a proxy to (re)import the data as a data.frame.

Maybe take a look at the discussions here.

P.S. I'm curious, but what happens when you call data.frame(res)?

ADD REPLY
1
Entering edit mode

Yes, RDAVIDWebService has a function to export the data, but it's not something that suits my objective. I'm trying to build an R package, which would involve fetching outputs ( which would be S4 objects) several times. So exporting the results every single time is not a good solution in my opinion, which is why I'm looking for an alternative.

P.S. I'm curious, but what happens when you call data.frame(res)?

The output looks like this:

Error in as.data.frame.default(res) : cannot coerce class ‘structure("DAVIDFunctionalAnnotationTable", package = "RDAVIDWebService")’ to a data.frame

ADD REPLY
0
Entering edit mode

Hmm, you're right. That definitely wouldn't work then. And looks like coercion won't play ball at all here.

I think you're just going to have to write a method yourself. Given you'd be using it in a package, I think it's a worthy investment.

Are you developing a replacement for RDAVIDWebService by any chance?

ADD REPLY
0
Entering edit mode

Please note that this package requires user registration with package provider, to get the results.

ADD REPLY
1
Entering edit mode
2.9 years ago
Jose Javier ▴ 10

you have to create your own S4 class that inherits from data.frame. For example, if you want to use a data.frame to store your objects you must create your class data.frame.DAVIDFunctionalAnnotationTable:

as.data.frame.DAVIDFunctionalAnnotationTable <-
    function(x, row.names=NULL, optional=FALSE, ...)
{
    if (is.null(row.names))
        row.names <- x@name
    value <- list(x)
    attr(value, "row.names") <- row.names
    class(value) <- "data.frame"
    value
}

format.DAVIDFunctionalAnnotationTable <- function(x, ...) paste0(x@name, ", ", .....)

+ Other way,....to convert your s4 object to a data.frame without inheriting directly:

dt<- DAVIDFunctionalAnnotationTable (...)

S4_to_data.frame <- function(mys4) {
  names <- slotNames (mys4)

  lt <- lapply(nms, function(names) slot(mys4, names))
  as.data.frame(setNames(lt, names))
}


S4_to_dataframe(dt)
ADD COMMENT

Login before adding your answer.

Traffic: 1091 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6