Repository navigation
Expand file tree
/
Copy pathSpatialDataContainerTest.java
More file actions
168 lines (137 loc) · 6.41 KB
/
Copy pathSpatialDataContainerTest.java
File metadata and controls
168 lines (137 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import gui.STDataAssembly;
import io.SpatialDataContainer;
import io.SpatialDataIO;
import io.SpatialDataException;
import org.janelia.saalfeldlab.n5.hdf5.N5HDF5Writer;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
public class SpatialDataContainerTest extends AbstractIOTest {
@Test
public void new_empty_container_is_empty() throws IOException {
final String path = getPlaygroundPath("container.n5");
SpatialDataContainer container = SpatialDataContainer.createNew(path, executorService);
assertTrue((new File(path)).exists(), "File '" + path + "' does not exist.");
assertTrue(SpatialDataContainer.isCompatibleContainer(path), "File '" + path + "' is not a compatible container.");
List<String> datasets = container.getDatasets();
assertTrue(datasets.isEmpty(), "Dataset is not empty.");
}
@Test
public void adding_and_deleting_dataset_works() throws IOException {
final String path = getPlaygroundPath("container.n5");
SpatialDataContainer container = SpatialDataContainer.createNew(path, executorService);
final String datasetName = "tmp.h5ad";
String fullPath = getPlaygroundPath(datasetName);
createAndWriteData(fullPath);
container.addExistingDataset(fullPath);
File datasetFile = new File(Paths.get(path, datasetName).toString());
assertTrue(container.getDatasets().contains(datasetName));
assertTrue(datasetFile.exists());
container.deleteDataset(datasetName);
assertFalse(container.getDatasets().contains(datasetName));
assertFalse(datasetFile.exists());
}
@Test
public void linking_does_not_move_dataset() throws IOException {
final String path = getPlaygroundPath("container.n5");
SpatialDataContainer container = SpatialDataContainer.createNew(path, executorService);
final String datasetName = "tmp.h5ad";
String fullPath = getPlaygroundPath(datasetName);
createAndWriteData(fullPath);
container.linkExistingDataset(fullPath);
File symbolicLink = new File(Paths.get(path, datasetName).toString());
File physicalFile = new File(fullPath);
assertTrue(container.getDatasets().contains(datasetName));
assertTrue(symbolicLink.exists());
assertTrue(physicalFile.exists());
container.deleteDataset(datasetName);
assertFalse(container.getDatasets().contains(datasetName));
assertFalse(symbolicLink.exists());
assertTrue(physicalFile.exists());
physicalFile.delete();
assertFalse(physicalFile.exists());
}
@Test
public void datasets_can_have_custom_storage_locations() throws IOException {
final String path = getPlaygroundPath("container.n5");
SpatialDataContainer container = SpatialDataContainer.createNew(path, executorService);
final String datasetName = "tmp.h5ad";
final String locationPath = "/obsm/test";
final String expressionPath = "/layers/exprTest";
final String annotationPath = "/obs/anotherTest";
final String geneAnnotationPath = "/var/anotherGeneTest";
SpatialDataIO sdio = SpatialDataIO.open(getPlaygroundPath(datasetName), executorService);
sdio.setDataPaths(locationPath, expressionPath, annotationPath, geneAnnotationPath);
STDataAssembly expected = new STDataAssembly(TestUtils.createTestDataSet());
sdio.writeData(expected);
String fullPath = getPlaygroundPath(datasetName);
container.addExistingDataset(fullPath, locationPath, expressionPath, annotationPath, geneAnnotationPath);
STDataAssembly actual = container.openDatasetReadOnly(datasetName).readData();
TestUtils.compareSTDataAssemblies(actual, expected);
}
@Test
public void opening_datasets_works() throws IOException {
SpatialDataContainer container = SpatialDataContainer.createNew(getPlaygroundPath("container.n5"), executorService);
final List<String> datasetNames = Arrays.asList("tmp1.h5ad", "tmp2.zarr");
final List<STDataAssembly> expectedData = new ArrayList<>();
for (String dataset : datasetNames) {
String fullPath = getPlaygroundPath(dataset);
expectedData.add(createAndWriteData(fullPath));
container.addExistingDataset(fullPath);
}
for (int k = 0; k < datasetNames.size(); k++) {
STDataAssembly actualData = container.openDatasetReadOnly(datasetNames.get(k)).readData();
TestUtils.compareSTDataAssemblies(expectedData.get(k), actualData);
}
List<SpatialDataIO> allDatasets = container.openAllDatasets();
assertEquals(2, allDatasets.size());
assertTrue(allDatasets.stream().noneMatch(Objects::isNull));
}
@Test
public void adding_existing_dataset_fails() throws IOException {
SpatialDataContainer container = SpatialDataContainer.createNew(getPlaygroundPath("container.n5"), executorService);
String datasetName = "tmp.h5ad";
String fullPath = getPlaygroundPath(datasetName);
try (N5HDF5Writer writer = new N5HDF5Writer(fullPath)) {
container.linkExistingDataset(fullPath);
assertThrows(SpatialDataException.class, () -> container.addExistingDataset(fullPath));
container.deleteDataset(datasetName);
(new File(fullPath)).delete();
} catch (Exception e) {
fail("Could not write / read file: ", e);
}
}
@Test
public void adding_non_existing_dataset_fails() throws IOException {
SpatialDataContainer container = SpatialDataContainer.createNew(getPlaygroundPath("container.n5"), executorService);
String datasetName = "tmp.h5ad";
String fullPath = getPlaygroundPath(datasetName);
try (N5HDF5Writer writer = new N5HDF5Writer(fullPath)) {
container.addExistingDataset(fullPath);
assertThrows(IllegalArgumentException.class, () -> container.addExistingDataset(fullPath));
container.deleteDataset(datasetName);
(new File(fullPath)).delete();
} catch (Exception e) {
fail("Could not write / read file: ", e);
}
}
protected STDataAssembly createAndWriteData(String path) throws IOException {
SpatialDataIO sdio = SpatialDataIO.open(getPlaygroundPath(path), executorService);
STDataAssembly data = new STDataAssembly(TestUtils.createTestDataSet());
sdio.writeData(data);
return data;
}
}