6
6
import os
7
7
from jsonschema import validate
8
8
from jsonschema .exceptions import ValidationError , SchemaError
9
- from typing import Union
9
+ from typing import Union , Any , Optional , List
10
10
11
11
# -- #
12
12
# Hardcoded values
21
21
# -- #
22
22
# Code blocks
23
23
# -- #
24
- def load_json_file (file ) :
24
+ def load_json_file (file : str ) -> Any :
25
25
"""
26
26
Function to load a JSON file as a dictionary.
27
27
Args:
@@ -46,7 +46,7 @@ def load_json_file(file):
46
46
)
47
47
48
48
49
- def handle_input_dict (input ) :
49
+ def handle_input_dict (input : dict [ str , str ]) -> Optional [ dict [ str , str ]] :
50
50
"""
51
51
Function to handle the input: assert that it's either a dictionary or
52
52
the filepath to an existing file containing the dictionary
@@ -73,7 +73,7 @@ def handle_input_dict(input):
73
73
raise ValueError (f"The file '{ input } ' is not a valid JSON file." )
74
74
75
75
76
- def get_header (token ) :
76
+ def get_header (token : str ) -> dict [ str , str ] :
77
77
"""
78
78
Obtain the header using a token.
79
79
@@ -90,7 +90,7 @@ def get_header(token):
90
90
}
91
91
92
92
93
- def validate_bs_accession (accession_str ) :
93
+ def validate_bs_accession (accession_str : str ) -> None :
94
94
"""
95
95
Validates that the given accession string conforms to the specified regex format.
96
96
See: https://registry.identifiers.org/registry/biosample
@@ -108,8 +108,8 @@ def validate_bs_accession(accession_str):
108
108
109
109
110
110
def validate_json_against_schema (
111
- json_doc : Union [dict , str ], json_schema : Union [dict , str ]
112
- ):
111
+ json_doc : Union [dict [ str , List [ str ]], str ], json_schema : Union [dict [ str , str ] , str ]
112
+ ) -> Optional [ bool ] :
113
113
"""
114
114
Validates a JSON document against a given JSON Schema.
115
115
@@ -150,7 +150,7 @@ class BiosamplesRecord:
150
150
production: boolean indicating environment mode
151
151
"""
152
152
153
- def __init__ (self , bs_accession ) :
153
+ def __init__ (self , bs_accession : str ) -> None :
154
154
"""
155
155
Initialize the BiosamplesRecord with provided arguments.
156
156
@@ -159,16 +159,19 @@ def __init__(self, bs_accession):
159
159
"""
160
160
validate_bs_accession (bs_accession )
161
161
self .bs_accession = bs_accession
162
+ self .biosamples_credentials : Optional [dict [str , str ]] = None
163
+ self .biosamples_externalReferences : List [str ] = []
164
+ self .production : bool = False
162
165
163
- def display (self ):
166
+ def display (self ) -> None :
164
167
"""
165
168
Display the attributes for demonstration purposes.
166
169
"""
167
170
print ("Biosamples Credentials:" , self .biosamples_credentials )
168
171
print ("Biosamples External References:" , self .biosamples_externalReferences )
169
172
print ("Production Mode:" , self .production )
170
173
171
- def fetch_bs_json (self , biosamples_endpoint ) :
174
+ def fetch_bs_json (self , biosamples_endpoint : str ) -> Optional [ dict [ str , str ]] :
172
175
"""
173
176
Fetches the BioSample's record (JSON) of the accession.
174
177
@@ -206,47 +209,49 @@ def fetch_bs_json(self, biosamples_endpoint):
206
209
self .bs_json = response_json
207
210
return self .bs_json
208
211
209
- def load_bs_json (self , bs_json_file : str = None , bs_json : dict = None ):
212
+ def load_bs_json (
213
+ self , bs_json : Union [str , dict [str , str ]]
214
+ ) -> Optional [dict [str , str ]]:
210
215
"""
211
216
Loads a given JSON, or the file containing it, as the BioSample's record (JSON) for this instance.
212
217
It is an alternative to fetching it directly from BioSample.
213
218
214
219
Args:
215
- bs_json_file (str): The file containing the Biosamples JSON metadata of the accession
216
- bs_json (dict): The already loaded Biosamples JSON metadata of the accession
220
+ bs_json Union[str, dict]: The already Biosamples JSON metadata of the accession either path to file or dictionary.
217
221
"""
218
- if bs_json :
219
- if isinstance (bs_json , dict ):
220
- self .bs_json = bs_json
221
- return self .bs_json
222
- else :
223
- raise TypeError (
224
- f"Given 'bs_json' is of type '{ type (bs_json )} ' instead of type 'dict'."
225
- )
226
- elif bs_json_file :
227
- bs_json = load_json_file (bs_json_file )
222
+ if isinstance (bs_json , dict ):
228
223
self .bs_json = bs_json
229
224
return self .bs_json
225
+ elif isinstance (bs_json , str ):
226
+ bs_json_data = load_json_file (bs_json )
227
+ self .bs_json = bs_json_data
228
+ return self .bs_json
230
229
else :
231
230
raise ValueError (
232
231
"Neither the file containing the Biosamples JSON nor the Biosamples JSON itself were given to load it into the instance."
233
232
)
234
233
235
- def pop_links (self ):
234
+ def pop_links (self ) -> dict [ str , str ] :
236
235
"""
237
236
Removes "_links" array (which is added automatically after updating the biosamples on the BioSample's side).
238
237
"""
239
238
240
- if "_links" not in self .bs_json :
241
- return self .bs_json
239
+ if "_links" in self .bs_json :
240
+ self .bs_json . pop ( "_links" )
242
241
243
- self .bs_json .pop ("_links" )
244
242
return self .bs_json
245
243
246
- def extend_externalReferences (self , new_ext_refs_list ):
244
+ def extend_externalReferences (
245
+ self , new_ext_refs_list : List [dict [str , str ]]
246
+ ) -> dict [str , str ]:
247
247
"""Extends the JSON of the BioSample's record with new externalReferences"""
248
248
if not self .bs_json :
249
- self .fetch_bs_json ()
249
+ endpoint = (
250
+ biosamples_endpoints ["prod" ]
251
+ if self .production
252
+ else biosamples_endpoints ["dev" ]
253
+ )
254
+ self .fetch_bs_json (endpoint )
250
255
self .pop_links ()
251
256
252
257
if "externalReferences" not in self .bs_json :
@@ -265,7 +270,9 @@ def extend_externalReferences(self, new_ext_refs_list):
265
270
self .bs_json ["externalReferences" ] = ext_refs_list
266
271
return self .bs_json
267
272
268
- def update_remote_record (self , header , webin_auth = "?authProvider=WEBIN" ):
273
+ def update_remote_record (
274
+ self , header : dict [str , str ], webin_auth : str = "?authProvider=WEBIN"
275
+ ) -> Optional [str ]:
269
276
"""
270
277
Updates the remote record of the BioSample's accession with the current sample JSON.
271
278
0 commit comments