forked from BU-ISCIII/relecov-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_schema.py
More file actions
1455 lines (1299 loc) · 60.5 KB
/
build_schema.py
File metadata and controls
1455 lines (1299 loc) · 60.5 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
import rich.console
import pandas as pd
import os
import re
import openpyxl
import sys
import json
import difflib
import inspect
import relecov_tools.utils
import relecov_tools.assets.schema_utils.jsonschema_draft
import relecov_tools.assets.schema_utils.metadatalab_template
from relecov_tools.config_json import ConfigJson
from relecov_tools.base_module import BaseModule
from datetime import datetime
from openpyxl.worksheet.datavalidation import DataValidation
pd.set_option("future.no_silent_downcasting", True)
stderr = rich.console.Console(
stderr=True,
style="dim",
highlight=False,
force_terminal=relecov_tools.utils.rich_force_colors(),
)
class BuildSchema(BaseModule):
def __init__(
self,
input_file=None,
schema_base=None,
excel_template=None,
draft_version=None,
diff=False,
output_dir=None,
version=None,
project=None,
non_interactive=False,
):
"""
Initialize the SchemaBuilder class. This class generates a JSON Schema file based on the provided draft version.
It reads the database definition from an Excel file and allows customization of the schema generation process.
"""
super().__init__(output_dir=output_dir, called_module=__name__)
self.excel_file_path = input_file
self.non_interactive = non_interactive
# Validate params
if not self.excel_file_path or not os.path.isfile(self.excel_file_path):
self.log.error("A valid Excel file path must be provided.")
raise ValueError("A valid Excel file path must be provided.")
if not self.excel_file_path.endswith(".xlsx"):
self.log.error("The Excel file must have a .xlsx extension.")
raise ValueError("The Excel file must have a .xlsx extension.")
# No metadata is being processed so batch_id will be execution date
self.set_batch_id(self.basemod_date)
# Validate output folder creation
if not output_dir:
self.output_dir = relecov_tools.utils.prompt_create_outdir()
else:
self.output_dir = os.path.abspath(output_dir)
if not os.path.exists(self.output_dir):
self.output_dir = relecov_tools.utils.prompt_create_outdir()
# Get version option
if not version:
# If not defined, then ask via prompt
self.version = relecov_tools.utils.prompt_text(
"Write the desired version using semantic versioning:"
)
else:
self.version = version
if not relecov_tools.utils.validate_semantic_version(self.version):
raise ValueError("[red]Error: Invalid version format")
# Validate show diff option
if diff is False:
self.show_diff = None
else:
self.show_diff = True
# Validate json schema draft version
if not draft_version and self.non_interactive:
self.draft_version = "2020-12"
else:
self.draft_version = (
relecov_tools.assets.schema_utils.jsonschema_draft.check_valid_version(
draft_version
)
)
# Get version option
# Parse build-schema configuration
self.build_schema_json_file = os.path.join(
os.path.dirname(__file__), "conf", "build_schema_config.json"
)
if project is None:
project = relecov_tools.utils.prompt_text("Write the desired project:")
self.project = project
available_projects = self.get_available_projects(self.build_schema_json_file)
# Get collecting institutions and dropdown list
self._lab_dropdowns, self._lab_uniques = self._load_laboratory_addresses()
# Config params
config_build_schema = ConfigJson(self.build_schema_json_file)
config_data = config_build_schema.get_configuration("projects") or {}
self.configurables = (
config_build_schema.get_configuration("configurables") or {}
)
config_json = ConfigJson(extra_config=True)
if self.project in available_projects:
self.project_config = config_data.get(self.project, {})
else:
self.log.error(
f"No configuration available for '{self.project}'. Available projects: {', '.join(available_projects)}"
)
stderr.print(
f"[red]No configuration available for '{self.project}'. Available projects: {', '.join(available_projects)}"
)
raise ValueError(
f"No configuration available for '{self.project}'. Available projects: {', '.join(available_projects)}"
)
# Validate base schema
if schema_base is not None:
if relecov_tools.utils.file_exists(schema_base):
self.base_schema_path = schema_base
else:
self.log.error(
f"[Error]Defined base schema file not found: {schema_base}."
)
stderr.print(
f"[Error]Defined base schema file not found: {schema_base}. Exiting..."
)
raise FileNotFoundError(
f"Defined base schema file not found: {schema_base}."
)
else:
try:
relecov_schema = config_json.get_topic_data("generic", "relecov_schema")
except KeyError as key_error:
self.log.error(f"Configuration key error: {key_error}")
stderr.print(f"[orange]Configuration key error: {key_error}")
raise
try:
self.base_schema_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"schema",
relecov_schema,
)
except FileNotFoundError as fnf_error:
self.log.error(f"Configuration file not found: {fnf_error}")
stderr.print(f"[red]Configuration file not found: {fnf_error}")
raise
if not relecov_tools.utils.file_exists(self.base_schema_path):
self.log.error(
"[Error]Fatal error. Relecov schema were not found in current relecov-tools installation. Make sure relecov-tools command is functioning."
)
stderr.print(
"[Error]Fatal error. Relecov schema were not found in current relecov-tools installation. Make sure relecov-tools command is functioning. Exiting..."
)
raise FileNotFoundError(
"Fatal error. Relecov schema were not found in current relecov-tools installation. Make sure relecov-tools command is functioning."
)
self.log.info("RELECOV schema successfully found in the configuration.")
stderr.print(
"[green]RELECOV schema successfully found in the configuration."
)
# TODO: What if no previous template exist?
if excel_template:
self.excel_template = excel_template
else:
try:
excel_template_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)), "assets"
)
# FIXME: filenames should inherit project name.
excel_template = [
f
for f in os.listdir(excel_template_path)
if f.startswith("Relecov_metadata_template")
]
if len(excel_template) > 1:
self.log.error(
"[Error]Fatal error. More than one excel template was found in current relecov-tools installation (assets)"
)
stderr.print(
"[Error]Fatal error.More than one excel template was found in current relecov-tools installation (assets)..Exiting"
)
raise FileExistsError(
"Fatal error. More than one excel template was found in current relecov-tools installation (assets)"
)
self.excel_template = os.path.join(
excel_template_path, excel_template[0]
)
except (FileNotFoundError, IndexError):
self.log.error(
"[Error]Fatal error. Excel template was not found in current relecov-tools installation (assets)"
)
stderr.print(
"[Error]Fatal error. Excel template not found in current relecov-tools installation (assets). Exiting..."
)
raise
def _load_laboratory_addresses(self):
"""
Returns two dictionaries with key in the three special fields:
- dropdowns[field] ........ list ‘<name> [<city>] [<ccn>]’
- uniques[field] .......... unique names for schema enum
NOTE:
For RELECOV, laboratory_address.json stores institution names under
`collecting_institution`. We intentionally reuse that same source for
collecting/submitting/sequencing to keep the three schema enums aligned.
"""
json_path = os.path.join(
os.path.dirname(__file__),
"conf",
"laboratory_address.json",
)
with open(json_path, encoding="utf-8") as fh:
lab_data = json.load(fh)
fields = [
"collecting_institution",
"submitting_institution",
"sequencing_institution",
]
dropdowns = {f: [] for f in fields}
uniques = {f: set() for f in fields}
for ccn, info in lab_data.items():
city = info.get("geo_loc_city", "").strip()
name = info.get("collecting_institution", "").strip()
if not name:
continue
dropdown_entry = f"{name} [{city}] [{ccn}]"
for f in fields:
dropdowns[f].append(dropdown_entry)
uniques[f].add(name)
dropdowns = {k: sorted(v) for k, v in dropdowns.items()}
uniques = {k: sorted(v) for k, v in uniques.items()}
return dropdowns, uniques
def validate_database_definition(self, json_data):
"""Validate the mandatory features and ensure:
- No duplicate enum values in the JSON schema.
- All fields have an example.
- All examples should have the same type as JSON schema.
- Date formats follow 'YYYY-MM-DD'.
Args:
json_data (dict): The JSON data representing the database definition.
Returns:
dict: A dictionary containing errors found, categorized by:
- Missing features
- Duplicate enums
- Missing examples
- Invalid example types
- Incorrect date formats
"""
log_errors = {
"missing_features": {},
"missing_examples": {},
"duplicate_enums": {},
"invalid_example_types": {},
"invalid_date_formats": {},
}
mandatory_features = [
"enum",
"examples",
"ontology_id",
"type",
"description",
"classification",
"label_name",
"fill_mode",
"required (Y/N)",
"complex_field (Y/N)",
]
# Iterate over properties in json_data
for prop_name, prop_features in json_data.items():
missing_features = [
feature
for feature in mandatory_features
if feature not in prop_features
]
if missing_features:
log_errors["missing_features"][prop_name] = missing_features
# Check for duplicate enum values
if prop_features.get("enum"):
if not pd.isna(prop_features["enum"]):
enum_values = prop_features["enum"].split("; ")
# Verify that enum has no duplicates
if len(enum_values) != len(set(enum_values)):
duplicates = [
value
for value in set(enum_values)
if enum_values.count(value) > 1
]
log_errors["duplicate_enums"][prop_name] = duplicates
# Check for missing examples
example = prop_features.get("examples")
if example is None:
log_errors["missing_examples"][prop_name] = ["Missing example."]
feature_type = prop_features["type"]
match feature_type:
# Check date format for properties with type=string and format=date
case "string":
if "format:date" in str(prop_features.get("options", "")).replace(
" ", ""
):
if isinstance(example, datetime):
example = example.strftime("%Y-%m-%d")
if isinstance(example, str):
try:
datetime.strptime(example, "%Y-%m-%d")
except ValueError:
if prop_name not in log_errors["invalid_date_formats"]:
log_errors["invalid_date_formats"][prop_name] = []
log_errors["invalid_date_formats"][prop_name].append(
f"Invalid date format '{example}', expected 'YYYY-MM-DD'"
)
case "integer" | "number":
function_to_convert = float if feature_type == "number" else int
try:
example = function_to_convert(example)
except ValueError:
log_errors["invalid_example_types"][prop_name] = [
f"Value {example} is not a valid {feature_type}"
]
# return log errors if any
if any(log_errors.values()):
stderr.print("[red]\t- Database Validation Failed")
# Convert log_errors dictionary to DataFrame
df_errors = pd.DataFrame(
[
{
"Error Category": category,
"Field": field,
"Details": (
", ".join(details) if isinstance(details, list) else details
),
}
for category, errors in log_errors.items()
for field, details in errors.items()
]
)
# Save errors to file
error_file_path = f"{self.output_dir}/schema_validation_errors.csv"
df_errors.to_csv(error_file_path, index=False, encoding="utf-8")
# Provide errors to user in rich table format:
relecov_tools.utils.display_dataframe_to_user(
name="Schema Validation Errors", dataframe=df_errors
)
stderr.print(f"\t- Log errors saved to:\n\t{error_file_path}")
# Ask user whether to continue or stop execution
if self.non_interactive or relecov_tools.utils.prompt_yn_question(
"Errors found in database values. Do you want to continue? (Y/N)"
):
pass
else:
return log_errors
else:
stderr.print("[green]\t- Database validation passed")
# If no errors found
return None
def get_available_projects(self, json):
"""Get list of available software in configuration
Args:
json (str): Path to bioinfo configuration json file.
Returns:
available_software: List containing available software defined in json.
"""
config = relecov_tools.utils.read_json_file(json)
# available_software = list(config.keys())
available_projects = list(config.get("projects", {}).keys())
return available_projects
def read_database_definition(self, sheet_id="main"):
"""Reads the database definition from an Excel sheet and converts it into JSON format.
Args:
sheet_id (str): The sheet name or ID in the Excel file to read from. Defaults to "main".
Returns:
json_data (dict): The JSON data representing the database definition.
"""
caller_method = inspect.stack()[1][3]
# Read excel file
df = pd.read_excel(
self.excel_file_path,
sheet_name=sheet_id,
na_values=["nan", "N/A", "NA", ""],
)
# Convert database to json format
json_data = {}
for row in df.itertuples(index=False):
property_name = row[0]
values = row[1:]
json_data[property_name] = dict(zip(df.columns[1:], values))
# Check json is not empty
if len(json_data) == 0:
self.log.error(f"{caller_method}{sheet_id}) No data found in xlsx database")
stderr.print(
f"{caller_method}{sheet_id}) [red]No data found in xlsx database"
)
sys.exit(1)
# Perform validation of database content
validation_out = self.validate_database_definition(json_data)
if validation_out:
sys.exit()
else:
return json_data
def create_schema_draft_template(self):
"""
Create a JSON Schema draft template based on the draft version.
Available drafts: [2020-12]
Returns:
draft_template(dict): The JSON Schema draft template.
"""
draft_template = (
relecov_tools.assets.schema_utils.jsonschema_draft.create_draft(
draft_version=self.draft_version, required_items=True
)
)
return draft_template
def _cast_example_to_type(
self, property_id: str, expected_type: str | None, value: any
) -> any:
"""Cast a single example value to the declared JSON-schema type when possible."""
if not isinstance(expected_type, str):
return value
expected = expected_type.strip().lower()
if expected == "string":
return str(value)
if expected == "integer":
try:
parsed_number = float(value)
except (TypeError, ValueError):
self.log.warning(
"Example value %r for property '%s' does not match expected type 'integer'. Keeping original value.",
value,
property_id,
)
return value
if not parsed_number.is_integer():
self.log.warning(
"Example value %r for property '%s' does not match expected type 'integer'. Keeping original value.",
value,
property_id,
)
return value
return int(parsed_number)
if expected == "number":
try:
return float(value)
except (TypeError, ValueError):
self.log.warning(
"Example value %r for property '%s' does not match expected type 'number'. Keeping original value.",
value,
property_id,
)
return value
if expected == "boolean":
if isinstance(value, bool):
return value
if isinstance(value, str):
normalized = value.strip().lower()
if normalized in ("true", "1", "yes", "y"):
return True
if normalized in ("false", "0", "no", "n"):
return False
self.log.warning(
"Example value %r for property '%s' does not match expected type 'boolean'. Keeping original value.",
value,
property_id,
)
return value
return value
def _cast_examples_to_declared_type(
self, property_id: str, expected_type: str | None, values: list[any]
) -> list[any]:
return [
self._cast_example_to_type(property_id, expected_type, item)
for item in values
]
def jsonschema_object(
self,
property_id: str,
property_feature_key: str,
value: any,
expected_type: str | None = None,
) -> dict[str, any]:
"""
Process a property keyword with their value and return a dictionary with fields for a property.
Args:
property_id (str): Name of the property.
property_feature_key (str): Property keyword.
value (any): Property keyword value.
Returns:
jsonschema_value (dict): {keyword: value}, parsed for each of the options
"""
jsonschema_value = {}
# Match/Case statement to evaluate the key:value pairs in the database and transform them to schema-compliant dictionaries.
match property_feature_key, value:
case "options", str(value):
options_list = [option.split(":") for option in value.split(",")]
# Handling float/ints stored as str
for key, value in options_list:
key = key.strip()
value = value.strip()
try:
value = float(value)
value = int(value) if value.is_integer() else value
except ValueError:
pass
jsonschema_value[key] = value
# FIXME multiple examples will always be loaded as str, regardless of actual type
case "examples", str(value):
parsed_examples = value.split("; ")
parsed_examples = self._cast_examples_to_declared_type(
property_id, expected_type, parsed_examples
)
jsonschema_value = {property_feature_key: parsed_examples}
case "examples", datetime():
value = value.strftime("%Y-%m-%dT%H:%M:%S")
value = value.replace("T00:00:00", "")
parsed_examples = self._cast_examples_to_declared_type(
property_id, expected_type, [value]
)
jsonschema_value = {property_feature_key: parsed_examples}
case "examples", int(value) | float(value):
value = float(value)
parsed_examples = [int(value) if value.is_integer() else value]
parsed_examples = self._cast_examples_to_declared_type(
property_id, expected_type, parsed_examples
)
jsonschema_value = {property_feature_key: parsed_examples}
case "enum", str():
jsonschema_value = {"$ref": f"#/$defs/enums/{property_id}"}
case _, value if not pd.isna(value):
# Non-serializable JSON value check and parsing (e.g. datetimes)
try:
json.dumps(value)
except (TypeError, OverflowError):
value = str(value)
jsonschema_value = {property_feature_key: value}
case _, _:
pass
return jsonschema_value
def handle_properties(self, json_data: dict[str, dict]) -> tuple[dict, dict, dict]:
"""
Handle the generation of simple and nested properties from the database definition.
Args:
json_data (dict): dictionary with structure {property_name: database_definition_dictionary}
Returns:
jsonschema_value (tuple): tuple containing the properties, required properties identified during the handling, and enums.
"""
schema_property = {}
required_properties = []
definitions = {"$defs": {"enums": {}}}
mapping_features = self.configurables.get("database_mapping_features", {})
exclude_fields = self.configurables.get("database_exclude_features", [])
# Flag property values that belong outside the property:
# - is_required: if required, goes to root 'required' keyword
# - has_enum: if there is an enum, store it for '$defs'
for property_id, db_features_dic in json_data.items():
is_required = db_features_dic.get("required (Y/N)", "") == "Y"
has_enum = db_features_dic.get("enum", False)
if property_id in [
"collecting_institution",
"submitting_institution",
"sequencing_institution",
]:
lab_values = self._lab_uniques.get(property_id, [])
if lab_values:
has_enum = "; ".join(lab_values)
# Create empty placeholder
schema_property[property_id] = {}
# If property is complex, call build schema again; else, continue function
is_complex = db_features_dic.get("complex_field (Y/N)", "") == "Y"
if is_complex:
schema_draft = {"type": "object", "properties": {}, "required": []}
subschema = self.read_database_definition(property_id)
complex_json_feature = self.build_new_schema(
subschema, schema_draft, root_schema=False
)
if complex_json_feature:
if complex_json_feature.get("$defs"):
# Prune the defs from the complex property
complex_defs = complex_json_feature.pop("$defs")
complex_defs["enums"] = {property_id: complex_defs["enums"]}
definitions["$defs"]["enums"].update(complex_defs["enums"])
# Fix the "$refs" adding the name of the parent property
for property_key, value in complex_json_feature[
"properties"
].items():
if "$ref" in value:
value["$ref"] = value["$ref"].replace(
f"/{property_key}", f"/{property_id}/{property_key}"
)
schema_property[property_id]["type"] = "array"
schema_property[property_id]["items"] = complex_json_feature
else:
for db_feature_key, db_feature_value in db_features_dic.items():
if db_feature_key in exclude_fields:
continue
# Extra check to avoid non-mapping properties.
if db_feature_key in mapping_features:
std_json_feature = self.jsonschema_object(
property_id,
mapping_features[db_feature_key],
db_feature_value,
expected_type=db_features_dic.get("type"),
)
if std_json_feature:
schema_property[property_id].update(std_json_feature)
# If property is required, add it to list
if is_required:
required_properties.append(property_id)
# If there is an enum in the property, parse it and add it to definitions
if isinstance(has_enum, str):
enum = [value.strip() for value in has_enum.split("; ")]
definitions["$defs"]["enums"][property_id] = {}
definitions["$defs"]["enums"][property_id]["enum"] = enum
# Just to be completely sure, but it should be unique
required_properties = (
{"required": list(set(required_properties))} if required_properties else {}
)
# Check that there are definitions
definitions = definitions if definitions["$defs"]["enums"].values() else {}
return schema_property, required_properties, definitions
def schema_build_all_of(self, json_data: dict) -> dict:
"""
Build the subschemas in 'allOf' keyword from the database definition.
Args:
json_data (dict): dictionary with structure {property_name: database_definition_dictionary}
Returns:
all_of_base (list): list containing all the subschemas to test in 'allOf'
"""
all_of_base = []
# Generate all the anyOf within
all_any_of = []
conditional_required = {
key: value.get("conditional_required_group").strip()
for key, value in json_data.items()
if not pd.isna(value.get("conditional_required_group"))
}
groups = list(set(conditional_required.values()))
conditional_required_by_group = {
group: [
key
for key in conditional_required.keys()
if conditional_required[key] == group
]
for group in groups
}
for group, keys in conditional_required_by_group.items():
any_of = [{"required": [key]} for key in keys]
all_any_of.append({"anyOf": any_of})
all_of_base.extend(all_any_of)
# For future: generate if_then within (for required props when specific value)
# FUTURE: all_of_base.extend(all_if_then)
return {"allOf": all_of_base} if all_of_base else {}
def build_new_schema(
self, json_data: dict[str, dict], schema_draft: dict, root_schema: bool = True
) -> dict[str, any]:
"""
Build a new JSON Schema based on the provided JSON data and draft template, in three stages:
- Pre-properties: all the operations needed prior to handling the properties (e.g. creation of root properties)
- properties: handling both simple and complex properties on a separate function
- Post-properties: All the operations needed after handling properties (e.g. defining which properties are required)
Parameters:
json_data (dict): Dictionary containing the properties and values of the database definition.
schema_draft (dict): The JSON Schema draft template.
root_schema (bool): True if is root of schema, False if not (e.g. complex property generation)
Returns:
schema_draft (dict): The newly created JSON Schema.
"""
# Pre-properties
new_schema = schema_draft
if root_schema:
# Fill schema header
# FIXME: it gets 'relecov-tools' instead of RELECOV
project_name = relecov_tools.utils.get_package_name()
if isinstance(project_name, str):
project_name = project_name.strip()
if " " in project_name:
project_name = re.sub(r"\s+", "-", project_name)
new_schema["$id"] = relecov_tools.utils.get_schema_url()
new_schema["title"] = f"{project_name}-schema"
new_schema["description"] = (
f"Json Schema that specifies the structure, content, and validation rules for {project_name}"
)
new_schema["version"] = self.version
# Fill schema properties
# Properties
try:
properties, required, defs = self.handle_properties(json_data)
except Exception as e:
self.log.error(f"Error building properties: {str(e)}")
stderr.print(f"[red]Error building properties: {str(e)}")
raise e
# Post-properties
# Finally, send schema_property object to the new json schema draft.
new_schema["properties"] = properties
new_schema.update(required)
new_schema.update(defs)
# Build the allOf keyword
all_of = self.schema_build_all_of(json_data)
new_schema.update(all_of)
# From here it can be extended to build other keywords at the end following the example above
return new_schema
def verify_schema(self, schema):
"""
Verify that the given schema adheres to the JSON Schema specification for the specified draft version.
Args:
schema (dict): The JSON Schema to be verified.
Raises:
ValueError: If the schema does not conform to the JSON Schema specification.
"""
relecov_tools.assets.schema_utils.jsonschema_draft.check_schema_draft(
schema, self.draft_version
)
def get_schema_diff(self, base_schema, new_schema):
"""
Print the differences between the base schema and the newly generated schema.
Args:
base_schema (dict): The base JSON Schema to compare against.
new_schema (dict): The newly generated JSON Schema to compare.
Returns:
bool: True if differences are found, False otherwise.
"""
# Set diff input
base_schema_lines = json.dumps(base_schema, indent=4).splitlines()
new_schema_lines = json.dumps(new_schema, indent=4).splitlines()
# Get diff lines
diff_lines = list(
difflib.unified_diff(
base_schema_lines,
new_schema_lines,
fromfile="base_schema.json",
tofile="new_schema.json",
)
)
if not diff_lines:
self.log.info(
"No differences were found between already installed and new generated schema. Exiting. No changes made"
)
stderr.print(
"[yellow]No differences were found between already installed and new generated schema. Exiting. No changes made"
)
return None
else:
self.log.info(
"Differences found between the existing schema and the newly generated schema."
)
stderr.print(
"[yellow]Differences found between the existing schema and the newly generated schema."
)
if self.show_diff:
return self.print_save_schema_diff(diff_lines)
else:
return None
def print_save_schema_diff(self, diff_lines=None):
# Set user's choices
choices = ["Print to standard output (stdout)", "Save to file", "Both"]
diff_output_choice = (
"Save to file"
if self.non_interactive
else relecov_tools.utils.prompt_selection(
"How would you like to print the diff between schemes?:", choices
)
)
if diff_output_choice in ["Print to standard output (stdout)", "Both"]:
for line in diff_lines:
print(line)
return True
if diff_output_choice in ["Save to file", "Both"]:
diff_filepath = os.path.join(
os.path.realpath(self.output_dir) + "/build_schema_diff.txt"
)
with open(diff_filepath, "w") as diff_file:
diff_file.write("\n".join(diff_lines))
self.log.info(f"[green]Schema diff file saved to {diff_filepath}")
stderr.print(f"[green]Schema diff file saved to {diff_filepath}")
return True
# FIXME: Add version tag to file name
def save_new_schema(self, json_data):
"""
Save the generated JSON Schema to the output folder.
Args:
json_data (dict): The JSON Schema to be saved.
Returns:
bool: True if the schema was successfully saved, False otherwise.
"""
try:
path_to_save = f"{self.output_dir}/relecov_schema.json"
with open(path_to_save, "w") as schema_file:
json.dump(json_data, schema_file, ensure_ascii=False, indent=4)
self.log.info(f"New JSON schema saved to: {path_to_save}")
stderr.print(f"[green]New JSON schema saved to: {path_to_save} ")
return True
except PermissionError as perm_error:
self.log.error(f"Permission error: {perm_error}")
stderr.print(f"[red]Permission error: {perm_error}")
except IOError as io_error:
self.log.error(f"I/O error: {io_error}")
stderr.print(f"[red]I/O error: {io_error}")
except Exception as e:
self.log.error(f"An unexpected error occurred: {str(e)}")
stderr.print(f"[red]An unexpected error occurred: {str(e)}")
return False
# FIXME: overview-tab - FIX first column values
# FIXME: overview-tab - Still need to add the column that maps to tab metadatalab
def create_metadatalab_excel(self, json_schema):
"""
Generates an Excel template file for Metadata LAB with four sheets:
Overview, Metadata LAB, Data Validation, and Version History.
Args:
json_schema (dict): The JSON schema used to generate the template.
It should include properties and required fields.
Returns:
None: If an error occurs during the process.
"""
try:
notes_control_input = (
"Auto-generated update"
if self.non_interactive
else input(
"\033[93mEnter a note about changes made to the schema: \033[0m"
)
)
# ------------------------------------------------------------------ #
# 1. Versioning & paths
# ------------------------------------------------------------------ #
version_history = pd.DataFrame(
columns=["FILE_VERSION", "CODE", "NOTES CONTROL", "DATE"]
)
try:
wb = openpyxl.load_workbook(self.excel_template)
ws_version = wb["VERSION"]
data = ws_version.values
columns = next(data)
version_history = pd.DataFrame(data, columns=columns)
except Exception as e:
self.log.warning(
f"Error reading previous VERSION sheet: {e}. Setting 1.0.0 as default."
)
version_history = pd.DataFrame(
[
{
"FILE_VERSION": "Relecov_metadata_template_v1.0.0",
"CODE": "1.0.0",
"NOTES CONTROL": "Initial version",
"DATE": datetime.now().strftime("%Y-%m-%d"),
}
]
)
next_version = self.version
version_info = {
"FILE_VERSION": f"Relecov_metadata_template_v{next_version}",
"CODE": next_version,
"NOTES CONTROL": notes_control_input,
"DATE": datetime.now().strftime("%Y-%m-%d"),
}
version_history = pd.concat(
[version_history, pd.DataFrame([version_info])], ignore_index=True
)
out_file = os.path.join(
self.output_dir, f"Relecov_metadata_template_v{next_version}.xlsx"
)
# ------------------------------------------------------------------ #
# 2. Schema filtering and dataframe preparation
# ------------------------------------------------------------------ #
default_classification_filter = [
"Database Identifiers",
"Sample collection and processing",
"Host information",
"Sequencing",
"Pathogen diagnostic testing",
"Contributor Acknowledgement",
"Public databases",
"Bioinformatics and QC metrics fields",
]
required_properties = set(json_schema.get("required", []))
schema_properties = json_schema.get("properties")
enum_defs = json_schema.get("$defs", {}).get("enums", {})
try:
schema_properties_flatten = relecov_tools.assets.schema_utils.metadatalab_template.schema_to_flatten_json(
schema_properties, required_properties
)
df = relecov_tools.assets.schema_utils.metadatalab_template.schema_properties_to_df(
schema_properties_flatten
)
classification_overrides = self.configurables.get(
"classification_filters", {}
)
classification_filter = classification_overrides.get(
self.project, default_classification_filter
)
if classification_filter and "classification" in df.columns:
df = df[df["classification"].isin(classification_filter)]
if "is_required" in df.columns:
df["required"] = df["is_required"].apply(
lambda value: "Y" if bool(value) else "N"
)
else:
df["required"] = df["property_id"].apply(
lambda x: "Y" if x in required_properties else "N"
)
def clean_ontologies(enums):