如何 json 序列化 ExpressionSet?

How to json serialize an ExpressionSet?

我想 JSON 序列化一个 expressionSet。我尝试了以下方法:

# create expression set based on the link above
library("Biobase")

ExpressionSet()

ExpressionSet(assayData=matrix(runif(1000), nrow=100, ncol=10))

# update an existing ExpressionSet
data(sample.ExpressionSet)
updateObject(sample.ExpressionSet)

# information about assay and sample data
featureNames(sample.ExpressionSet)[1:10]
sampleNames(sample.ExpressionSet)[1:5]
experimentData(sample.ExpressionSet)

# subset: first 10 genes, samples 2, 4, and 10
expressionSet <- sample.ExpressionSet[1:10,c(2,4,10)]

当我这样做时(使用与 dataframes 相同的方法):

library(jsonlite)

toJSON(expressionSet)

我明白了

Error: No method for S4 class:ExpressionSet

有没有办法完成这项工作,或者我是否必须编写自定义序列化程序?

我最终使用了这样的命名列表:

expressionset_to_json <- function(eset) {

  expression_data <- Biobase::exprs(eset)
  sample_info <- Biobase::pData(eset)
  feature_data <- Biobase::fData(eset)  

  templist = list(
    expression_data=as.data.frame(expression_data),
    sample_info=sample_info,
    feature_data=feature_data
  )

  return(jsonlite::toJSON(templist))
}

然后

expressionset_to_json(expressionSet)

产量

{"expression_data":[{"B":85.7533,"D":135.575,"J":135.608,"_row":"AFFX-MurIL2_at"},{"B":126.196,"D":93.3713,"J":90.4838,"_row":"AFFX-MurIL10_at"},{"B":8.8314,"D":28.7072,"J":34.4874,"_row":"AFFX-MurIL4_at"},{"B":3.6009,"D":12.3397,"J":4.5498,"_row":"AFFX-MurFAS_at"},{"B":30.438,"D":70.9319,"J":46.352,"_row":"AFFX-BioB-5_at"},{"B":25.8461,"D":69.9766,"J":91.5307,"_row":"AFFX-BioB-M_at"},{"B":181.08,"D":161.469,"J":229.671,"_row":"AFFX-BioB-3_at"},{"B":57.2889,"D":77.2207,"J":66.7302,"_row":"AFFX-BioC-5_at"},{"B":16.8006,"D":46.5272,"J":39.7419,"_row":"AFFX-BioC-3_at"},{"B":16.1789,"D":9.7364,"J":0.3988,"_row":"AFFX-BioDn-5_at"}],"sample_info":[{"sex":"Male","type":"Case","score":0.4,"_row":"B"},{"sex":"Male","type":"Case","score":0.42,"_row":"D"},{"sex":"Male","type":"Control","score":0.63,"_row":"J"}],"feature_data":[{"_row":"AFFX-MurIL2_at"},{"_row":"AFFX-MurIL10_at"},{"_row":"AFFX-MurIL4_at"},{"_row":"AFFX-MurFAS_at"},{"_row":"AFFX-BioB-5_at"},{"_row":"AFFX-BioB-M_at"},{"_row":"AFFX-BioB-3_at"},{"_row":"AFFX-BioC-5_at"},{"_row":"AFFX-BioC-3_at"},{"_row":"AFFX-BioDn-5_at"}]}

我认为这可以满足您的需求。我不太了解这个领域,所以如果 JSON 对象不符合预期,请发表评论,我可以尝试更新它。

我解决此问题的方法涉及将 ExpressionSet class 的对象转换为数据框,以便我们可以在其上使用 toJSON()。我在这里找到了这个想法:https://support.bioconductor.org/p/77432/

# create expression set based on the link above
library(Biobase)

ExpressionSet()

ExpressionSet(assayData=matrix(runif(1000), nrow=100, ncol=10))

# update an existing ExpressionSet
data(sample.ExpressionSet)
updateObject(sample.ExpressionSet)

# information about assay and sample data
featureNames(sample.ExpressionSet)[1:10]
sampleNames(sample.ExpressionSet)[1:5]
experimentData(sample.ExpressionSet)

# subset: first 10 genes, samples 2, 4, and 10
expressionSet <- sample.ExpressionSet[1:10,c(2,4,10)]

# this code is inspired from here: https://support.bioconductor.org/p/77432/
m <- exprs(eset) # matrix of intensities
pdata <- pData(eset) # data.frame of phenotypic information.
d <- cbind(pdata, t(m))

library(jsonlite)
toJSON(d)

[{"sex":"Male","type":"Case","score":0.4,"AFFX-MurIL2_at":85.7533,"AFFX-MurIL10_at":126.196,"AFFX-MurIL4_at":8.8314,"AFFX-MurFAS_at":3.6009,"AFFX-BioB-5_at":30.438,"AFFX-BioB-M_at":25.8461,"AFFX-BioB-3_at":181.08,"AFFX-BioC-5_at":57.2889,"AFFX-BioC-3_at":16.8006,"AFFX-BioDn-5_at":16.1789,"_row":"B"},{"sex":"Male","type":"Case","score":0.42,"AFFX-MurIL2_at":135.575,"AFFX-MurIL10_at":93.3713,"AFFX-MurIL4_at":28.7072,"AFFX-MurFAS_at":12.3397,"AFFX-BioB-5_at":70.9319,"AFFX-BioB-M_at":69.9766,"AFFX-BioB-3_at":161.469,"AFFX-BioC-5_at":77.2207,"AFFX-BioC-3_at":46.5272,"AFFX-BioDn-5_at":9.7364,"_row":"D"},{"sex":"Male","type":"Control","score":0.63,"AFFX-MurIL2_at":135.608,"AFFX-MurIL10_at":90.4838,"AFFX-MurIL4_at":34.4874,"AFFX-MurFAS_at":4.5498,"AFFX-BioB-5_at":46.352,"AFFX-BioB-M_at":91.5307,"AFFX-BioB-3_at":229.671,"AFFX-BioC-5_at":66.7302,"AFFX-BioC-3_at":39.7419,"AFFX-BioDn-5_at":0.3988,"_row":"J"}]