1+ # ' JSON file wrapper class
2+ # ' @title JsonWrapper Class
3+ # ' @docType class
4+ # ' @description
5+ # ' Class representing a JSON file in a Vitessce dataset.
6+ # '
7+ # ' @rdname JsonWrapper
8+ # ' @export
9+ JsonWrapper <- R6 :: R6Class(" JsonWrapper" ,
10+ inherit = AbstractWrapper ,
11+ public = list (
12+ # ' @field json_path The path to a local JSON file.
13+ # ' @keywords internal
14+ json_path = NULL ,
15+ # ' @field json_url The URL to a remote JSON file.
16+ # ' @keywords internal
17+ json_url = NULL ,
18+ # ' @field local_json_uid The unique identifier for the local JSON file.
19+ # ' @keywords internal
20+ local_json_uid = NULL ,
21+ # ' @field data_type The Vitessce data type for this file.
22+ # ' @keywords internal
23+ data_type = NULL ,
24+ # ' @field options A list of options to pass to the Vitessce file definition.
25+ # ' @keywords internal
26+ options = NULL ,
27+ # ' @field coordination_values A list of coordination values to pass to the Vitessce file definition.
28+ # ' @keywords internal
29+ coordination_values = NULL ,
30+ # ' @field request_init A list of requestInit values to pass to fetch when loading the JSON over HTTP.
31+ # ' @keywords internal
32+ request_init = NULL ,
33+ # ' @description
34+ # ' Create a wrapper around a JSON file.
35+ # ' @param json_path The path to a local JSON file.
36+ # ' @param json_url The URL to a remote JSON file.
37+ # ' @param data_type The Vitessce data type for this file.
38+ # ' @param options A list of options to pass to the Vitessce file definition.
39+ # ' @param coordination_values A list of coordination values to pass to the Vitessce file definition.
40+ # ' @param request_init A list of requestInit values to pass to fetch when loading the JSON over HTTP.
41+ # ' @param ... Parameters inherited from `AbstractWrapper`.
42+ # ' @return A new `JsonWrapper` object.
43+ initialize = function (json_path = NA , json_url = NA , data_type = NA , options = NA , coordination_values = NA , request_init = NA , ... ) {
44+ super $ initialize(... )
45+ self $ json_path <- json_path
46+ self $ json_url <- json_url
47+
48+ if (is_na(data_type )) {
49+ stop(" Expected data_type to be provided." )
50+ }
51+
52+ if (! is.na(json_url ) && ! is.na(json_path )) {
53+ stop(" Did not expect json_url to be provided with json_path." )
54+ }
55+
56+ if (is.na(json_url ) && is.na(json_path )) {
57+ stop(" Expected either json_url or json_path to be provided." )
58+ }
59+
60+ if (! is.na(json_path )) {
61+ self $ is_remote <- FALSE
62+ } else {
63+ self $ is_remote <- TRUE
64+ }
65+
66+ self $ local_json_uid <- make_unique_filename(" .json" )
67+
68+ self $ data_type <- data_type
69+ self $ options <- options
70+ self $ coordination_values <- coordination_values
71+ self $ request_init <- request_init
72+ },
73+ # ' @description
74+ # ' Create the JSON output files, web server routes, and file definition creators.
75+ # ' @param dataset_uid The ID for this dataset.
76+ # ' @param obj_i The index of this data object within the dataset.
77+ # ' @param base_dir A base directory for local data.
78+ convert_and_save = function (dataset_uid , obj_i , base_dir = NA ) {
79+ if (! self $ is_remote ) {
80+ super $ convert_and_save(dataset_uid , obj_i , base_dir = base_dir )
81+ }
82+
83+ # Get the file definition creator functions.
84+ file_def_creator <- self $ make_file_def_creator(dataset_uid , obj_i )
85+
86+ # Append the new file definition creators functions to the main list.
87+ self $ file_def_creators <- append(self $ file_def_creators , file_def_creator )
88+
89+ # Create a web server route object for the directory of JSON files.
90+ new_routes <- self $ make_routes(dataset_uid , obj_i )
91+ for (route in new_routes ) {
92+ self $ routes <- append(self $ routes , route )
93+ }
94+ },
95+ # ' @description
96+ # ' Get a list of server route objects.
97+ # ' @param dataset_uid The ID for this dataset.
98+ # ' @param obj_i The index of this data object within the dataset.
99+ make_routes = function (dataset_uid , obj_i ) {
100+ return (self $ get_local_file_route(dataset_uid , obj_i , self $ json_path , self $ local_json_uid ))
101+ },
102+ # ' @description
103+ # ' Get the URL to the JSON file, to fill in the file URL in the file definitions.
104+ # ' @param base_url The base URL, on which the route will be served.
105+ # ' @param dataset_uid The ID for this dataset.
106+ # ' @param obj_i The index of this data object within the dataset.
107+ # ' @keywords internal
108+ # ' @return A URL as a string.
109+ get_json_url = function (base_url , dataset_uid , obj_i ) {
110+ if (self $ is_remote ) {
111+ return (self $ json_url )
112+ } else {
113+ return (self $ get_local_file_url(base_url , dataset_uid , obj_i , self $ json_path , self $ local_json_uid ))
114+ }
115+ },
116+ # ' @description
117+ # ' Make the file definition creator function for the JSON data type.
118+ # ' @param dataset_uid The ID for this dataset.
119+ # ' @param obj_i The index of this data object within the dataset.
120+ # ' @return A file definition creator function which takes a `base_url` parameter.
121+ make_file_def_creator = function (dataset_uid , obj_i ) {
122+ get_json <- function (base_url ) {
123+ file_def <- list (
124+ fileType = paste0(self $ data_type , " .json" ),
125+ url = self $ get_json_url(base_url , dataset_uid , obj_i )
126+ )
127+ if (! is_na(self $ options )) {
128+ file_def [[' options' ]] <- self $ options
129+ }
130+ if (! is_na(self $ request_init )) {
131+ file_def [[' requestInit' ]] <- self $ request_init
132+ }
133+ if (! is_na(self $ coordination_values )) {
134+ file_def [[' coordinationValues' ]] <- self $ coordination_values
135+ }
136+ return (file_def )
137+ }
138+ return (get_json )
139+ }
140+ ),
141+ )
0 commit comments