R 两个 SummarizedExperiment 对象的比较

标签 r unit-testing comparison r-s4 testthat

我的单元测试在比较引用和预期的 SummarizedExperiment 对象时失败。错误信息:

> expect_identical(target, current)
Error: `target` not identical to `current`.
Attributes: < Component “assays”: Class definitions are not identical >

MWE:

LINK TO DATASET

代码:

load("se-comparison.Rdata")
library(SummarizedExperiment)
library(testthat)

expect_identical(target, current)
# expect_identical() uses attr.all.equal() to compare S4 objects so check this
attr.all.equal(target, current)
# ok, check the attributes
cur <- attributes(current)
tar <- attributes(target)

class(cur$assays)
class(tar$assays)

expect_identical(
  cur$assays,
  tar$assays
)

expect_identical(
  class(cur$assays),
  class(tar$assays)
)

输出:

> library(SummarizedExperiment)
> library(testthat)
> expect_identical(target, current)
Error: `target` not identical to `current`.
Attributes: < Component “assays”: Class definitions are not identical >
> # expect_identical() uses attr.all.equal() to compare S4 objects so check this
> attr.all.equal(target, current)
[1] "Attributes: < Component “assays”: Class definitions are not identical >"
> # ok, check the attributes
> cur <- attributes(current)
> tar <- attributes(target)
> class(cur$assays)
[1] "ShallowSimpleListAssays"
attr(,"package")
[1] "SummarizedExperiment"
> class(tar$assays)
[1] "ShallowSimpleListAssays"
attr(,"package")
[1] "SummarizedExperiment"
> cur$assays
Reference class object of class "ShallowSimpleListAssays"
Field "data":
List of length 1
names(1): counts
> tar$assays
Reference class object of class "ShallowSimpleListAssays"
Field "data":
List of length 1
names(1): counts
> expect_identical(
+   cur$assays,
+   tar$assays
+ )
Error: cur$assays not identical to tar$assays.
Class definitions are not identical
> expect_identical(
+   class(cur$assays),
+   class(tar$assays)
+ )
>

有什么想法为什么比较失败?

最佳答案

不确定为什么它通常会失败,但 attr.all.equal 会比较 getClass 方法返回的类定义(如果可用)。在这种情况下,每个 $assays 对象的 getClass 返回在 fieldPrototypesrefMethods 中不同的类定义(环境字符串不同)。这会导致identical失败。

比较:

str(class(cur$assays))
str(cur$assays$getClass())

解决方法是跳过检查属性:expect_equal(target, current, check.attributes = FALSE)

作为旁注,这也可以正常工作(请注意,我在这里使用 SummarizedExperiment 类的 getter 方法而不是 $assays):

expect_equal(
  assays(current),
  assays(target)
)

关于R 两个 SummarizedExperiment 对象的比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49286862/

相关文章:

r - 如何在 lubridate 中表示当月的第 15 天

c - R 中的 n 维积分,限制为函数

python - 使用自定义比较的排序键函数

javascript - jQuery 功能到 YUI 3 的映射?

comparison - Erlang 相对于(类似于)node.js 的优势?

r - 如何从 elrm 摘要输出中提取系数

r - 对表中的多个变量应用 t 检验和 Wilcoxon 检验

javascript - 单元测试 express.js 路由

java - 自动(半)创建单元测试?

Python - 测试属性抛出异常