Switch to unified view

a b/tests/testthat/test_conversion.R
1
# library
2
skip_on_ci() # dont test on GitHub actions
3
4
# file
5
h5ad_file <- tempfile(fileext = ".h5ad")
6
zarr_file <- tempfile(fileext = ".zarr")
7
ometiff_file <- tempfile(fileext = ".ome.tiff")
8
9
test_that("as.AnnData", {
10
11
  # get data
12
  data("visium_data")
13
  data("xenium_data")
14
15
  # xenium to anndata
16
  as.AnnData(xenium_data, file = h5ad_file)
17
  as.AnnData(xenium_data, file = h5ad_file, assay = "Assay1")
18
  as.AnnData(xenium_data, file = h5ad_file, flip_coordinates = TRUE)
19
20
  # visium to anndata
21
  as.AnnData(visium_data, file = h5ad_file)
22
  as.AnnData(visium_data, file = h5ad_file, assay = "Assay1")
23
  as.AnnData(visium_data, file = h5ad_file, flip_coordinates = TRUE)
24
  
25
  # xenium to anndata
26
  as.AnnData(xenium_data, file = zarr_file)
27
  as.AnnData(xenium_data, file = zarr_file, assay = "Assay1")
28
  as.AnnData(xenium_data, file = zarr_file, flip_coordinates = TRUE)
29
30
  # visium to anndata
31
  as.AnnData(visium_data, file = zarr_file)
32
  as.AnnData(visium_data, file = zarr_file, assay = "Assay1")
33
  as.AnnData(visium_data, file = zarr_file, flip_coordinates = TRUE)
34
  
35
  # clean file
36
  file.remove(h5ad_file)
37
  unlink(zarr_file, recursive = TRUE)
38
  expect_equal(1,1L)
39
})
40
41
test_that("as.AnnData, python path", {
42
  
43
  # get data
44
  data("visium_data")
45
  data("xenium_data")
46
  
47
  # python.path
48
  expect_error(as.AnnData(visium_data, file = h5ad_file, python.path = ""))
49
  
50
  # python.path
51
  python.path <- system("which python", intern = TRUE)
52
  expect_error(as.AnnData(visium_data, file = zarr_file, python.path = python.path))
53
  expect_error(as.AnnData(visium_data, file = zarr_file, python.path = ""))
54
  
55
  # options path
56
  options(voltron.python.path = python.path)
57
  expect_error(as.AnnData(visium_data, file = zarr_file))
58
  options(voltron.python.path = NULL)
59
  expect_true(as.AnnData(visium_data, file = zarr_file))
60
  
61
  # clean file
62
  expect_equal(1,1L)
63
})
64
65
test_that("as.ometiff", {
66
  
67
  # get image
68
  data.file <- system.file(file.path('extdata', 'DAPI.tif'), package='VoltRon')
69
  
70
  # save image
71
  as.OmeTiff(magick::image_read(data.file), out_path = ometiff_file)
72
  
73
  # clean file
74
  file.remove(ometiff_file)
75
  expect_equal(1,1L)
76
})
77
78
test_that("as.ometiff, python path", {
79
  
80
  # get image
81
  data.file <- system.file(file.path('extdata', 'DAPI.tif'), package='VoltRon')
82
  
83
  # save image
84
  as.OmeTiff(magick::image_read(data.file), out_path = ometiff_file)
85
  
86
  # python.path
87
  expect_error(as.OmeTiff(magick::image_read(data.file), out_path = ometiff_file, python.path = ""))
88
  
89
  # python.path
90
  python.path <- system("which python", intern = TRUE)
91
  expect_error(as.OmeTiff(magick::image_read(data.file), out_path = ometiff_file, python.path = python.path))
92
  
93
  # options path
94
  options(voltron.python.path = python.path)
95
  expect_error(as.OmeTiff(magick::image_read(data.file), out_path = ometiff_file))
96
  options(voltron.python.path = NULL)
97
  expect_true(as.OmeTiff(magick::image_read(data.file), out_path = ometiff_file))
98
  
99
  # clean file
100
  file.remove(ometiff_file)
101
  expect_equal(1,1L)
102
})
103
104
test_that("as.Seurat", {
105
  
106
  # one sample
107
  test1 <- VoltRon::as.Seurat(xenium_data, cell.assay = "Xenium", type = "image")
108
  test1_Voltron <- as.VoltRon(test1, assay_name = "Xenium")
109
  
110
  # multiple samples
111
  xenium_data2 <- xenium_data
112
  xenium_data2$Sample <- "sample1"
113
  xenium_data2 <- merge(xenium_data2, xenium_data)
114
  test1 <- VoltRon::as.Seurat(xenium_data2, cell.assay = "Xenium", type = "image")
115
  test1_Voltron <- as.VoltRon(test1, assay_name = "Xenium")
116
  
117
  expect_equal(1,1L)
118
  
119
})